diff options
Diffstat (limited to '')
-rw-r--r-- | src/ntp_server.c | 135 | ||||
-rw-r--r-- | src/ntp_server.h | 50 | ||||
-rw-r--r-- | src/output/raop_output_plugin.c | 76 | ||||
-rw-r--r-- | src/output/raop_output_plugin.h | 11 |
4 files changed, 194 insertions, 78 deletions
diff --git a/src/ntp_server.c b/src/ntp_server.c new file mode 100644 index 000000000..3d8298f4a --- /dev/null +++ b/src/ntp_server.c @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "ntp_server.h" + +#include <glib.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> +#include <unistd.h> +#include <sys/time.h> + +#ifdef WIN32 +#define WINVER 0x0501 +#include <ws2tcpip.h> +#include <winsock.h> +#else +#include <sys/select.h> +#include <sys/socket.h> +#endif + +/* + * Calculate the current NTP time, store it in the buffer. + */ +static void +fill_int(unsigned char *buffer, uint32_t value) +{ + uint32_t be = GINT32_TO_BE(value); + memcpy(buffer, &be, sizeof(be)); +} + +/* + * Store time in the NTP format in the buffer + */ +static void +fill_time_buffer_with_time(unsigned char *buffer, struct timeval *tout) +{ + unsigned long secs_to_baseline = 964697997; + double fraction; + unsigned long long_fraction; + unsigned long secs; + + fraction = ((double) tout->tv_usec) / 1000000.0; + long_fraction = (unsigned long) (fraction * 256.0 * 256.0 * 256.0 * 256.0); + secs = secs_to_baseline + tout->tv_sec; + fill_int(buffer, secs); + fill_int(buffer + 4, long_fraction); +} + +/* + * Calculate the current NTP time, store it in the buffer. + */ +static void +fill_time_buffer(unsigned char *buffer) +{ + struct timeval current_time; + + gettimeofday(¤t_time,NULL); + fill_time_buffer_with_time(buffer, ¤t_time); +} + +bool +ntp_server_handle(struct ntp_server *ntp) +{ + unsigned char buf[32]; + struct sockaddr addr; + int iter; + unsigned int addr_len = sizeof(addr); + int num_bytes = recvfrom(ntp->fd, buf, sizeof(buf), 0, &addr, &addr_len); + if (num_bytes == 0) { + return false; + } + fill_time_buffer(buf + 16); + // set to response + buf[1] = 0xd3; + // copy request + for (iter = 0; iter < 8; iter++) { + buf[8 + iter] = buf[24 + iter]; + } + fill_time_buffer(buf + 24); + + num_bytes = sendto(ntp->fd, buf, num_bytes, 0, &addr, addr_len); + + return num_bytes == sizeof(buf); +} + +bool +ntp_server_check(struct ntp_server *ntp, struct timeval *tout) +{ + fd_set rdfds; + int fdmax = 0; + + FD_ZERO(&rdfds); + + FD_SET(ntp->fd, &rdfds); + fdmax = ntp->fd; + select(fdmax + 1, &rdfds,NULL, NULL, tout); + if (FD_ISSET(ntp->fd, &rdfds)) { + if (!ntp_server_handle(ntp)) { + g_debug("unable to send timing response\n"); + return false; + } + } + return true; +} + +void +ntp_server_init(struct ntp_server *ntp) +{ + ntp->port = 6002; + ntp->fd = -1; +} + +void +ntp_server_close(struct ntp_server *ntp) +{ + if (ntp->fd >= 0) + close(ntp->fd); +} diff --git a/src/ntp_server.h b/src/ntp_server.h new file mode 100644 index 000000000..56593fef0 --- /dev/null +++ b/src/ntp_server.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_NTP_SERVER_H +#define MPD_NTP_SERVER_H + +#include <stdbool.h> + +struct timeval; + +struct ntp_server { + unsigned short port; + int fd; +}; + +void +ntp_server_init(struct ntp_server *ntp); + +void +ntp_server_close(struct ntp_server *ntp); + +/* + * Recv the NTP datagram from the AirTunes, send back an NTP response. + */ +bool +ntp_server_handle(struct ntp_server *ntp); + +/* + * check to see if there are any timing requests, and respond if there are any + */ +bool +ntp_server_check(struct ntp_server *ntp, struct timeval *tout); + +#endif diff --git a/src/output/raop_output_plugin.c b/src/output/raop_output_plugin.c index dba89daef..865978c82 100644 --- a/src/output/raop_output_plugin.c +++ b/src/output/raop_output_plugin.c @@ -67,8 +67,7 @@ new_raop_data(GError **error_r) if (raop_session == NULL) { raop_session = g_new(struct raop_session_data, 1); raop_session->raop_list = NULL; - raop_session->ntp.port = 6002; - raop_session->ntp.fd = -1; + ntp_server_init(&raop_session->ntp); raop_session->ctrl.port = 6001; raop_session->ctrl.fd = -1; raop_session->play_state.playing = false; @@ -402,46 +401,6 @@ fill_time_buffer_with_time(unsigned char *buffer, struct timeval *tout) fill_int(buffer + 4, long_fraction); } -/* - * Calculate the current NTP time, store it in the buffer. - */ -static void -fill_time_buffer(unsigned char *buffer) -{ - struct timeval current_time; - - gettimeofday(¤t_time,NULL); - fill_time_buffer_with_time(buffer, ¤t_time); -} - -/* - * Recv the NTP datagram from the AirTunes, send back an NTP response. - */ -static bool -send_timing_response(int fd) -{ - unsigned char buf[32]; - struct sockaddr addr; - int iter; - unsigned int addr_len = sizeof(addr); - int num_bytes = recvfrom(fd, buf, sizeof(buf), 0, &addr, &addr_len); - if (num_bytes == 0) { - return false; - } - fill_time_buffer(buf + 16); - // set to response - buf[1] = 0xd3; - // copy request - for (iter = 0; iter < 8; iter++) { - buf[8 + iter] = buf[24 + iter]; - } - fill_time_buffer(buf + 24); - - num_bytes = sendto(fd, buf, num_bytes, 0, &addr, addr_len); - - return num_bytes == sizeof(buf); -} - static void get_time_for_rtp(struct play_state *state, struct timeval *tout) { @@ -498,29 +457,6 @@ send_control_command(struct control_data *ctrl, struct raop_data *rd, } /* - * check to see if there are any timing requests, and respond if there are any - */ -static bool -check_timing(struct timeval *tout) -{ - fd_set rdfds; - int fdmax = 0; - - FD_ZERO(&rdfds); - - FD_SET(raop_session->ntp.fd, &rdfds); - fdmax = raop_session->ntp.fd; - select(fdmax + 1, &rdfds,NULL, NULL, tout); - if (FD_ISSET(raop_session->ntp.fd, &rdfds)) { - if (!send_timing_response(raop_session->ntp.fd)) { - g_debug("unable to send timing response\n"); - return false; - } - } - return true; -} - -/* * send RTSP request, and get response if it's needed * if this gets a success, *kd is allocated or reallocated (if *kd is not NULL) */ @@ -605,7 +541,7 @@ exec_request(struct rtspcl_data *rtspcld, const char *cmd, break; } if (FD_ISSET(raop_session->ntp.fd, &rdfds)) { - send_timing_response(raop_session->ntp.fd); + ntp_server_handle(&raop_session->ntp); } } @@ -1141,7 +1077,7 @@ send_audio_data(int fd, GError **error_r) while (diff < -10000) { tout.tv_sec = 0; tout.tv_usec = -diff; - check_timing(&tout); + ntp_server_check(&raop_session->ntp, &tout); gettimeofday(¤t_time, NULL); diff = difference(¤t_time, &rtp_time); } @@ -1280,7 +1216,7 @@ raop_output_pause(void *data) struct timeval tout = {.tv_sec = 0, .tv_usec = 0}; struct raop_data *rd = (struct raop_data *) data; - check_timing(&tout); + ntp_server_check(&raop_session->ntp, &tout); rd->paused = true; return true; } @@ -1304,7 +1240,7 @@ raop_output_close(void *data) // TODO clean up everything else raop_session->play_state.playing = false; close(raop_session->data_fd); - close(raop_session->ntp.fd); + ntp_server_close(&raop_session->ntp); close(raop_session->ctrl.fd); } } @@ -1398,7 +1334,7 @@ raop_output_play(void *data, const void *chunk, size_t size, g_mutex_lock(raop_session->data_mutex); - check_timing(&tout); + ntp_server_check(&raop_session->ntp, &tout); if (raop_session->play_state.rtptime <= NUMSAMPLES) { // looped over, need new reference point to calculate correct times diff --git a/src/output/raop_output_plugin.h b/src/output/raop_output_plugin.h index ed655330c..3b1d9bdf7 100644 --- a/src/output/raop_output_plugin.h +++ b/src/output/raop_output_plugin.h @@ -20,6 +20,8 @@ #ifndef MPD_OUTPUT_RAOP_PLUGIN_H #define MPD_OUTPUT_RAOP_PLUGIN_H +#include "ntp_server.h" + #include <glib.h> #include <stdbool.h> #include <sys/time.h> @@ -89,13 +91,6 @@ struct encrypt_data { /*********************************************************************/ -struct ntp_data { - unsigned short port; - int fd; -}; - -/*********************************************************************/ - struct raop_data { struct rtspcl_data *rtspcl; const char *addr; // target host address @@ -132,7 +127,7 @@ struct control_data { // session struct raop_session_data { struct raop_data *raop_list; - struct ntp_data ntp; + struct ntp_server ntp; struct control_data ctrl; struct encrypt_data encrypt; struct play_state play_state; |