]> git.sesse.net Git - vlc/blob - modules/control/netsync.c
17386956c335b4a6018db9d73b4f9832c4eafcd6
[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
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>
37
38 #ifdef HAVE_UNISTD_H
39 #    include <unistd.h>
40 #endif
41 #include <sys/types.h>
42 #ifdef HAVE_POLL
43 #   include <poll.h>
44 #endif
45
46 #include <vlc_network.h>
47
48 #define NETSYNC_PORT 9875
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 static int  Open (vlc_object_t *);
54 static void Close(vlc_object_t *);
55
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")
60
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.")
64
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.")
68
69 vlc_module_begin()
70     set_shortname(N_("Network Sync"))
71     set_description(N_("Network synchronisation"))
72     set_category(CAT_ADVANCED)
73     set_subcategory(SUBCAT_ADVANCED_MISC)
74
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,
78                 true)
79     add_integer("netsync-timeout", 500, NULL,
80                  NETSYNC_TIMEOUT_TEXT, NETSYNC_TIMEOUT_LONGTEXT, true)
81
82     set_capability("interface", 0)
83     set_callbacks(Open, Close)
84 vlc_module_end()
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89 struct intf_sys_t {
90     int            fd;
91     int            timeout;
92     bool           is_master;
93     playlist_t     *playlist;
94     input_thread_t *input;
95 };
96 static void Run(intf_thread_t *intf);
97
98 /*****************************************************************************
99  * Activate: initialize and create stuff
100  *****************************************************************************/
101 static int Open(vlc_object_t *object)
102 {
103     intf_thread_t *intf = (intf_thread_t*)object;
104     intf_sys_t    *sys;
105     int fd;
106
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");
111             return VLC_EGENERIC;
112         }
113         fd = net_ConnectUDP(VLC_OBJECT(intf), psz_master, NETSYNC_PORT, -1);
114         free(psz_master);
115     } else {
116         fd = net_ListenUDP1(VLC_OBJECT(intf), NULL, NETSYNC_PORT);
117     }
118
119     if (fd == -1) {
120         msg_Err(intf, "Netsync socket failure");
121         return VLC_EGENERIC;
122     }
123
124     intf->p_sys = sys = malloc(sizeof(*sys));
125     if (!sys) {
126         net_Close(fd);
127         return VLC_ENOMEM;
128     }
129     intf->pf_run = Run;
130
131     sys->fd = fd;
132     sys->is_master = var_InheritBool(intf, "netsync-master");
133     sys->timeout = var_InheritInteger(intf, "netsync-timeout");
134     if (sys->timeout < 500)
135         sys->timeout = 500;
136     sys->playlist = pl_Hold(intf);
137     sys->input = NULL;
138
139     return VLC_SUCCESS;
140 }
141
142 /*****************************************************************************
143  * Close: destroy interface
144  *****************************************************************************/
145 void Close(vlc_object_t *object)
146 {
147     intf_thread_t *intf = (intf_thread_t*)object;
148     intf_sys_t *sys = intf->p_sys;
149
150     pl_Release(intf);
151     net_Close(sys->fd);
152     free(sys);
153 }
154
155 static void Master(intf_thread_t *intf)
156 {
157     intf_sys_t *sys = intf->p_sys;
158     struct pollfd ufd = { .fd = sys->fd, .events = POLLIN, };
159     uint64_t data[2];
160
161     /* Don't block */
162     if (poll(&ufd, 1, sys->timeout) <= 0)
163         return;
164
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);
170
171     mtime_t master_system;
172     if (input_GetPcrSystem(sys->input, &master_system))
173         return;
174
175     data[0] = hton64(mdate());
176     data[1] = hton64(master_system);
177
178     /* Reply to the sender */
179     sendto(sys->fd, data, sizeof(data), 0,
180            (struct sockaddr *)&from, struct_size);
181
182 #if 0
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)
189              : "non-IPv4", date);
190 #endif
191 }
192
193 static void Slave(intf_thread_t *intf)
194 {
195     intf_sys_t *sys = intf->p_sys;
196     struct pollfd ufd = { .fd = sys->fd, .events = POLLIN, };
197     uint64_t data[2];
198
199     mtime_t system;
200     if (input_GetPcrSystem(sys->input, &system))
201         goto wait;
202
203     /* Send clock request to the master */
204     data[0] = hton64(system);
205
206     const mtime_t send_date = mdate();
207     if (send(sys->fd, data, sizeof(data[0]), 0) <= 0)
208         goto wait;
209
210     /* Don't block */
211     int ret = poll(&ufd, 1, sys->timeout);
212     if (ret == 0)
213         return;
214     if (ret < 0)
215         goto wait;
216
217     const mtime_t receive_date = mdate();
218     if (recv(sys->fd, data, sizeof(data), 0) <= 0)
219         goto wait;
220
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);
225
226     if (master_system > 0) {
227         mtime_t client_system;
228         if (input_GetPcrSystem(sys->input, &client_system))
229             goto wait;
230
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);
234 #if 0
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);
239 #endif
240         }
241     }
242 wait:
243     msleep(INTF_IDLE_SLEEP);
244 }
245
246
247 /*****************************************************************************
248  * Run: interface thread
249  *****************************************************************************/
250 static void Run(intf_thread_t *intf)
251 {
252     intf_sys_t *sys = intf->p_sys;
253
254     int canc = vlc_savecancel();
255
256     /* High priority thread */
257     vlc_thread_set_priority(intf, VLC_THREAD_PRIORITY_INPUT);
258
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);
265             sys->input = NULL;
266         }
267
268         if (sys->input == NULL) {
269             /* Wait a bit */
270             msleep(INTF_IDLE_SLEEP);
271             continue;
272         }
273
274         /*
275          * We now have an input
276          */
277         if (sys->is_master)
278             Master(intf);
279         else
280             Slave(intf);
281     }
282
283     if (sys->input)
284         vlc_object_release(sys->input);
285     vlc_restorecancel(canc);
286 }
287