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 *****************************************************************************/
89 static void Run(intf_thread_t *intf);
91 /*****************************************************************************
92 * Activate: initialize and create stuff
93 *****************************************************************************/
94 static int Open(vlc_object_t *object)
96 intf_thread_t *intf = (intf_thread_t*)object;
99 if (!var_InheritBool(intf, "netsync-master")) {
100 char *psz_master = var_InheritString(intf, "netsync-master-ip");
101 if (psz_master == NULL) {
102 msg_Err(intf, "master address not specified");
105 fd = net_ConnectUDP(VLC_OBJECT(intf), psz_master, NETSYNC_PORT, -1);
109 fd = net_ListenUDP1(VLC_OBJECT(intf), NULL, NETSYNC_PORT);
112 msg_Err(intf, "Netsync socket failure");
116 intf->p_sys = (void *)(intptr_t)fd;
121 /*****************************************************************************
122 * Close: destroy interface
123 *****************************************************************************/
124 void Close(vlc_object_t *object)
126 intf_thread_t *intf = (intf_thread_t*)object;
128 net_Close((intptr_t)intf->p_sys);
131 /*****************************************************************************
132 * Run: interface thread
133 *****************************************************************************/
134 static void Run(intf_thread_t *intf)
136 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))
137 int canc = vlc_savecancel();
138 input_thread_t *input = NULL;
139 char data[MAX_MSG_LENGTH];
140 int fd = (intptr_t)intf->p_sys;
142 playlist_t *playlist = pl_Hold(intf);
143 int timeout = var_InheritInteger(intf, "netsync-timeout");
146 bool is_master = var_InheritBool(intf, "netsync-master");
148 /* High priority thread */
149 vlc_thread_set_priority(intf, VLC_THREAD_PRIORITY_INPUT);
151 while (vlc_object_alive(intf)) {
152 /* Update the input */
154 input = playlist_CurrentInput(playlist);
155 } else if (input->b_dead || !vlc_object_alive(input)) {
156 vlc_object_release(input);
162 msleep(INTF_IDLE_SLEEP);
167 * We now have an input
170 /* Initialize file descriptor set and timeout (0.5s) */
171 /* FIXME: arbitrary tick */
172 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
175 struct sockaddr_storage from;
176 mtime_t master_system;
177 mtime_t client_system;
179 int struct_size, read_size, ret;
182 ret = poll(&ufd, 1, timeout);
186 /* We received something */
187 struct_size = sizeof(from);
188 read_size = recvfrom(fd, data, MAX_MSG_LENGTH, 0,
189 (struct sockaddr*)&from,
190 (unsigned int *)&struct_size);
192 /* not sure we need the client information to sync,
193 since we are the master anyway */
194 client_system = ntoh64(*(int64_t *)data);
198 if (input_GetPcrSystem(input, &master_system))
201 *((int64_t *)data) = hton64(date);
202 *(((int64_t *)data)+1) = hton64(master_system);
204 /* Reply to the sender */
205 sendto(fd, data, 2 * sizeof(int64_t), 0,
206 (struct sockaddr *)&from, struct_size);
209 msg_Dbg(intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
210 "(date: %"PRId64")", client_system, master_system,
211 (from.ss_family == AF_INET) ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
217 mtime_t master_system;
218 mtime_t client_system;
220 mtime_t send_date, receive_date;
221 mtime_t diff_date, master_date;
222 int sent, read_size, ret;
224 if (input_GetPcrSystem(input, &system)) {
225 msleep(INTF_IDLE_SLEEP);
229 /* Send clock request to the master */
231 *((int64_t *)data) = hton64(system);
233 sent = send(fd, data, sizeof(int64_t), 0);
235 msleep(INTF_IDLE_SLEEP);
240 ret = poll(&ufd, 1, timeout);
244 msleep(INTF_IDLE_SLEEP);
248 receive_date = mdate();
249 read_size = recv(fd, data, MAX_MSG_LENGTH, 0);
250 if (read_size <= 0) {
251 msleep(INTF_IDLE_SLEEP);
255 master_date = ntoh64(*(int64_t *)data);
256 master_system = ntoh64(*(((int64_t *)data)+1)); /* system date */
258 diff_date = receive_date -
259 ((receive_date - send_date) / 2 + master_date);
261 if (input && master_system > 0) {
264 if (input_GetPcrSystem(input, &client_system)) {
265 msleep(INTF_IDLE_SLEEP);
269 diff_system = client_system - master_system - diff_date;
270 if (diff_system != 0) {
271 input_ModifyPcrSystem(input, true, master_system - diff_date);
273 msg_Dbg(intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64","
274 " clock diff: %"PRId64", diff: %"PRId64"",
275 system, master_system, client_system,
276 diff_system, diff_date);
280 msleep(INTF_IDLE_SLEEP);
285 vlc_object_release(input);
287 vlc_restorecancel(canc);