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