1 /*****************************************************************************
2 * netsync.c: synchronisation between several network clients.
3 *****************************************************************************
4 * Copyright (C) 2004-2009 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Jean-Paul Saman <jpsaman@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_input.h>
36 #include <vlc_playlist.h>
41 #include <sys/types.h>
46 #include <vlc_network.h>
48 #define NETSYNC_PORT 9875
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Open (vlc_object_t *);
54 static void Close(vlc_object_t *);
56 #define NETSYNC_TEXT N_("Network master clock")
57 #define NETSYNC_LONGTEXT N_("When set then " \
58 "this vlc instance shall dictate its clock for synchronisation" \
59 "over clients listening on the masters network ip address")
61 #define MIP_TEXT N_("Master server ip address")
62 #define MIP_LONGTEXT N_("The IP address of " \
63 "the network master clock to use for clock synchronisation.")
65 #define NETSYNC_TIMEOUT_TEXT N_("UDP timeout (in ms)")
66 #define NETSYNC_TIMEOUT_LONGTEXT N_("Amount of time (in ms) " \
67 "to wait before aborting network reception of data.")
70 set_shortname(N_("Network Sync"))
71 set_description(N_("Network synchronisation"))
72 set_category(CAT_ADVANCED)
73 set_subcategory(SUBCAT_ADVANCED_MISC)
75 add_bool("netsync-master", false, NULL,
76 NETSYNC_TEXT, NETSYNC_LONGTEXT, true)
77 add_string("netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
79 add_integer("netsync-timeout", 500, NULL,
80 NETSYNC_TIMEOUT_TEXT, NETSYNC_TIMEOUT_LONGTEXT, true)
82 set_capability("interface", 0)
83 set_callbacks(Open, Close)
86 /*****************************************************************************
88 *****************************************************************************/
94 input_thread_t *input;
96 static void Run(intf_thread_t *intf);
98 /*****************************************************************************
99 * Activate: initialize and create stuff
100 *****************************************************************************/
101 static int Open(vlc_object_t *object)
103 intf_thread_t *intf = (intf_thread_t*)object;
107 if (!var_InheritBool(intf, "netsync-master")) {
108 char *psz_master = var_InheritString(intf, "netsync-master-ip");
109 if (psz_master == NULL) {
110 msg_Err(intf, "master address not specified");
113 fd = net_ConnectUDP(VLC_OBJECT(intf), psz_master, NETSYNC_PORT, -1);
116 fd = net_ListenUDP1(VLC_OBJECT(intf), NULL, NETSYNC_PORT);
120 msg_Err(intf, "Netsync socket failure");
124 intf->p_sys = sys = malloc(sizeof(*sys));
132 sys->is_master = var_InheritBool(intf, "netsync-master");
133 sys->timeout = var_InheritInteger(intf, "netsync-timeout");
134 if (sys->timeout < 500)
136 sys->playlist = pl_Hold(intf);
142 /*****************************************************************************
143 * Close: destroy interface
144 *****************************************************************************/
145 void Close(vlc_object_t *object)
147 intf_thread_t *intf = (intf_thread_t*)object;
148 intf_sys_t *sys = intf->p_sys;
155 static void Master(intf_thread_t *intf)
157 intf_sys_t *sys = intf->p_sys;
158 struct pollfd ufd = { .fd = sys->fd, .events = POLLIN, };
162 if (poll(&ufd, 1, sys->timeout) <= 0)
165 /* We received something */
166 struct sockaddr_storage from;
167 unsigned struct_size = sizeof(from);
168 recvfrom(sys->fd, data, sizeof(data), 0,
169 (struct sockaddr*)&from, &struct_size);
171 mtime_t master_system;
172 if (input_GetPcrSystem(sys->input, &master_system))
175 data[0] = hton64(mdate());
176 data[1] = hton64(master_system);
178 /* Reply to the sender */
179 sendto(sys->fd, data, sizeof(data), 0,
180 (struct sockaddr *)&from, struct_size);
183 /* not sure we need the client information to sync,
184 since we are the master anyway */
185 mtime_t client_system = ntoh64(data[0]);
186 msg_Dbg(intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
187 "(date: %"PRId64")", client_system, master_system,
188 (from.ss_family == AF_INET) ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
193 static void Slave(intf_thread_t *intf)
195 intf_sys_t *sys = intf->p_sys;
196 struct pollfd ufd = { .fd = sys->fd, .events = POLLIN, };
200 if (input_GetPcrSystem(sys->input, &system))
203 /* Send clock request to the master */
204 data[0] = hton64(system);
206 const mtime_t send_date = mdate();
207 if (send(sys->fd, data, sizeof(data[0]), 0) <= 0)
211 int ret = poll(&ufd, 1, sys->timeout);
217 const mtime_t receive_date = mdate();
218 if (recv(sys->fd, data, sizeof(data), 0) <= 0)
221 const mtime_t master_date = ntoh64(data[0]);
222 const mtime_t master_system = ntoh64(data[1]);
223 const mtime_t diff_date = receive_date -
224 ((receive_date - send_date) / 2 + master_date);
226 if (master_system > 0) {
227 mtime_t client_system;
228 if (input_GetPcrSystem(sys->input, &client_system))
231 const mtime_t diff_system = client_system - master_system - diff_date;
232 if (diff_system != 0) {
233 input_ModifyPcrSystem(sys->input, true, master_system - diff_date);
235 msg_Dbg(intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64","
236 " clock diff: %"PRId64", diff: %"PRId64"",
237 system, master_system, client_system,
238 diff_system, diff_date);
243 msleep(INTF_IDLE_SLEEP);
247 /*****************************************************************************
248 * Run: interface thread
249 *****************************************************************************/
250 static void Run(intf_thread_t *intf)
252 intf_sys_t *sys = intf->p_sys;
254 int canc = vlc_savecancel();
256 /* High priority thread */
257 vlc_thread_set_priority(intf, VLC_THREAD_PRIORITY_INPUT);
259 while (vlc_object_alive(intf)) {
260 /* Update the input */
261 if (sys->input == NULL) {
262 sys->input = playlist_CurrentInput(sys->playlist);
263 } else if (sys->input->b_dead || !vlc_object_alive(sys->input)) {
264 vlc_object_release(sys->input);
268 if (sys->input == NULL) {
270 msleep(INTF_IDLE_SLEEP);
275 * We now have an input
284 vlc_object_release(sys->input);
285 vlc_restorecancel(canc);