]> git.sesse.net Git - vlc/blob - modules/control/netsync.c
455be184dcbb1f71c74630c12d707a43ecba0667
[vlc] / modules / control / netsync.c
1 /*****************************************************************************
2  * netsync.c: synchronisation between several network clients.
3  *****************************************************************************
4  * Copyright (C) 2004-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Jean-Paul Saman <jpsaman@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <assert.h>
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_interface.h>
36 #include <vlc_input.h>
37 #include <vlc_playlist.h>
38
39 #ifdef HAVE_UNISTD_H
40 #    include <unistd.h>
41 #endif
42 #include <sys/types.h>
43 #ifdef HAVE_POLL
44 #   include <poll.h>
45 #endif
46
47 #include <vlc_network.h>
48
49 #define NETSYNC_PORT 9875
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 static int  Open (vlc_object_t *);
55 static void Close(vlc_object_t *);
56
57 #define NETSYNC_TEXT N_("Network master clock")
58 #define NETSYNC_LONGTEXT N_("When set then " \
59   "this vlc instance shall dictate its clock for synchronisation" \
60   "over clients listening on the masters network ip address")
61
62 #define MIP_TEXT N_("Master server ip address")
63 #define MIP_LONGTEXT N_("The IP address of " \
64   "the network master clock to use for clock synchronisation.")
65
66 #define NETSYNC_TIMEOUT_TEXT N_("UDP timeout (in ms)")
67 #define NETSYNC_TIMEOUT_LONGTEXT N_("Amount of time (in ms) " \
68   "to wait before aborting network reception of data.")
69
70 vlc_module_begin()
71     set_shortname(N_("Network Sync"))
72     set_description(N_("Network synchronisation"))
73     set_category(CAT_ADVANCED)
74     set_subcategory(SUBCAT_ADVANCED_MISC)
75
76     add_bool("netsync-master", false, NULL,
77               NETSYNC_TEXT, NETSYNC_LONGTEXT, true)
78     add_string("netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,
79                 true)
80     add_integer("netsync-timeout", 500, NULL,
81                  NETSYNC_TIMEOUT_TEXT, NETSYNC_TIMEOUT_LONGTEXT, true)
82
83     set_capability("interface", 0)
84     set_callbacks(Open, Close)
85 vlc_module_end()
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90 struct intf_sys_t {
91     int            fd;
92     int            timeout;
93     bool           is_master;
94     playlist_t     *playlist;
95
96     /* */
97     input_thread_t *input;
98     vlc_thread_t   thread;
99 };
100
101 static int PlaylistEvent(vlc_object_t *, char const *cmd,
102                          vlc_value_t oldval, vlc_value_t newval, void *data);
103
104 /*****************************************************************************
105  * Activate: initialize and create stuff
106  *****************************************************************************/
107 static int Open(vlc_object_t *object)
108 {
109     intf_thread_t *intf = (intf_thread_t*)object;
110     intf_sys_t    *sys;
111     int fd;
112
113     if (!var_InheritBool(intf, "netsync-master")) {
114         char *psz_master = var_InheritString(intf, "netsync-master-ip");
115         if (psz_master == NULL) {
116             msg_Err(intf, "master address not specified");
117             return VLC_EGENERIC;
118         }
119         fd = net_ConnectUDP(VLC_OBJECT(intf), psz_master, NETSYNC_PORT, -1);
120         free(psz_master);
121     } else {
122         fd = net_ListenUDP1(VLC_OBJECT(intf), NULL, NETSYNC_PORT);
123     }
124
125     if (fd == -1) {
126         msg_Err(intf, "Netsync socket failure");
127         return VLC_EGENERIC;
128     }
129
130     intf->pf_run = NULL;
131     intf->p_sys = sys = malloc(sizeof(*sys));
132     if (!sys) {
133         net_Close(fd);
134         return VLC_ENOMEM;
135     }
136
137     sys->fd = fd;
138     sys->is_master = var_InheritBool(intf, "netsync-master");
139     sys->timeout = var_InheritInteger(intf, "netsync-timeout");
140     if (sys->timeout < 500)
141         sys->timeout = 500;
142     sys->playlist = pl_Get(intf);
143     sys->input = NULL;
144
145     var_AddCallback(sys->playlist, "input-current", PlaylistEvent, intf);
146     return VLC_SUCCESS;
147 }
148
149 /*****************************************************************************
150  * Close: destroy interface
151  *****************************************************************************/
152 void Close(vlc_object_t *object)
153 {
154     intf_thread_t *intf = (intf_thread_t*)object;
155     intf_sys_t *sys = intf->p_sys;
156
157     assert(sys->input == NULL);
158     var_DelCallback(sys->playlist, "input-current", PlaylistEvent, intf);
159     net_Close(sys->fd);
160     free(sys);
161 }
162
163 static mtime_t GetPcrSystem(input_thread_t *input)
164 {
165     int canc = vlc_savecancel();
166     /* TODO use the delay */
167     mtime_t system;
168     if (input_GetPcrSystem(input, &system, NULL))
169         system = -1;
170     vlc_restorecancel(canc);
171
172     return system;
173 }
174
175 static void *Master(void *handle)
176 {
177     intf_thread_t *intf = handle;
178     intf_sys_t *sys = intf->p_sys;
179     for (;;) {
180         struct pollfd ufd = { .fd = sys->fd, .events = POLLIN, };
181         uint64_t data[2];
182
183         if (poll(&ufd, 1, -1) <= 0)
184             continue;
185
186         /* We received something */
187         struct sockaddr_storage from;
188         unsigned struct_size = sizeof(from);
189         recvfrom(sys->fd, data, sizeof(data), 0,
190                  (struct sockaddr*)&from, &struct_size);
191
192         mtime_t master_system = GetPcrSystem(sys->input);
193         if (master_system < 0)
194             continue;
195
196         data[0] = hton64(mdate());
197         data[1] = hton64(master_system);
198
199         /* Reply to the sender */
200         sendto(sys->fd, data, sizeof(data), 0,
201                (struct sockaddr *)&from, struct_size);
202 #if 0
203         /* not sure we need the client information to sync,
204            since we are the master anyway */
205         mtime_t client_system = ntoh64(data[0]);
206         msg_Dbg(intf, "Master clockref: %"PRId64" -> %"PRId64", from %s "
207                  "(date: %"PRId64")", client_system, master_system,
208                  (from.ss_family == AF_INET) ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)
209                  : "non-IPv4", /*date*/ 0);
210 #endif
211     }
212 }
213
214 static void *Slave(void *handle)
215 {
216     intf_thread_t *intf = handle;
217     intf_sys_t *sys = intf->p_sys;
218
219     for (;;) {
220         struct pollfd ufd = { .fd = sys->fd, .events = POLLIN, };
221         uint64_t data[2];
222
223         mtime_t system = GetPcrSystem(sys->input);
224         if (system < 0)
225             goto wait;
226
227         /* Send clock request to the master */
228         data[0] = hton64(system);
229
230         const mtime_t send_date = mdate();
231         if (send(sys->fd, data, sizeof(data[0]), 0) <= 0)
232             goto wait;
233
234         /* Don't block */
235         int ret = poll(&ufd, 1, sys->timeout);
236         if (ret == 0)
237             continue;
238         if (ret < 0)
239             goto wait;
240
241         const mtime_t receive_date = mdate();
242         if (recv(sys->fd, data, sizeof(data), 0) <= 0)
243             goto wait;
244
245         const mtime_t master_date   = ntoh64(data[0]);
246         const mtime_t master_system = ntoh64(data[1]);
247         const mtime_t diff_date = receive_date -
248                                   ((receive_date - send_date) / 2 + master_date);
249
250         if (master_system > 0) {
251             int canc = vlc_savecancel();
252
253             mtime_t client_system;
254             if (!input_GetPcrSystem(sys->input, &client_system, NULL)) {
255                 const mtime_t diff_system = client_system - master_system - diff_date;
256                 if (diff_system != 0) {
257                     input_ModifyPcrSystem(sys->input, true, master_system - diff_date);
258 #if 0
259                     msg_Dbg(intf, "Slave clockref: %"PRId64" -> %"PRId64" -> %"PRId64","
260                              " clock diff: %"PRId64", diff: %"PRId64"",
261                              system, master_system, client_system,
262                              diff_system, diff_date);
263 #endif
264                 }
265             }
266             vlc_restorecancel(canc);
267         }
268     wait:
269         msleep(INTF_IDLE_SLEEP);
270     }
271 }
272
273 static int InputEvent(vlc_object_t *object, char const *cmd,
274                       vlc_value_t oldval, vlc_value_t newval, void *data)
275 {
276     VLC_UNUSED(cmd); VLC_UNUSED(oldval); VLC_UNUSED(object);
277     intf_thread_t  *intf = data;
278     intf_sys_t     *sys = intf->p_sys;
279
280     if (newval.i_int == INPUT_EVENT_DEAD && sys->input) {
281         msg_Err(intf, "InputEvent DEAD");
282         vlc_cancel(sys->thread);
283         vlc_join(sys->thread, NULL);
284         vlc_object_release(sys->input);
285         sys->input = NULL;
286     }
287     return VLC_SUCCESS;
288 }
289
290 static int PlaylistEvent(vlc_object_t *object, char const *cmd,
291                          vlc_value_t oldval, vlc_value_t newval, void *data)
292 {
293     VLC_UNUSED(cmd); VLC_UNUSED(oldval); VLC_UNUSED(object);
294     intf_thread_t  *intf = data;
295     intf_sys_t     *sys = intf->p_sys;
296
297     input_thread_t *input = newval.p_address;
298     assert(sys->input == NULL);
299     sys->input = vlc_object_hold(input);
300     if (vlc_clone(&sys->thread, sys->is_master ? Master : Slave, intf,
301                   VLC_THREAD_PRIORITY_INPUT)) {
302         vlc_object_release(input);
303         return VLC_SUCCESS;
304     }
305     var_AddCallback(input, "intf-event", InputEvent, intf);
306     return VLC_SUCCESS;
307 }
308