]> git.sesse.net Git - vlc/blob - modules/control/netsync.c
* netsync module no longer IPv4-specific
[vlc] / modules / control / netsync.c
1 /*****************************************************************************\r
2  * netsync.c: synchronisation between several network clients.\r
3  *****************************************************************************\r
4  * Copyright (C) 2004 VideoLAN\r
5  * $Id$\r
6  *\r
7  * Authors: Gildas Bazin <gbazin@videolan.org>\r
8  *\r
9  * This program is free software; you can redistribute it and/or modify\r
10  * it under the terms of the GNU General Public License as published by\r
11  * the Free Software Foundation; either version 2 of the License, or\r
12  * (at your option) any later version.\r
13  *\r
14  * This program is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU General Public License for more details.\r
18  *\r
19  * You should have received a copy of the GNU General Public License\r
20  * along with this program; if not, write to the Free Software\r
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.\r
22  *****************************************************************************/\r
23 \r
24 /*****************************************************************************\r
25  * Preamble\r
26  *****************************************************************************/\r
27 #include <stdlib.h>\r
28 #include <vlc/vlc.h>\r
29 #include <vlc/intf.h>\r
30 \r
31 #ifdef HAVE_UNISTD_H\r
32 #    include <unistd.h>\r
33 #endif\r
34 \r
35 #ifdef HAVE_SYS_TIME_H\r
36 #    include <sys/time.h>\r
37 #endif\r
38 #include <sys/types.h>\r
39 \r
40 #ifdef WIN32\r
41 #   include <winsock2.h>\r
42 #   include <ws2tcpip.h>\r
43 #   ifndef IN_MULTICAST\r
44 #       define IN_MULTICAST(a) IN_CLASSD(a)\r
45 #   endif\r
46 #else\r
47 #   include <sys/socket.h>\r
48 #   include <netinet/in.h>\r
49 #   if HAVE_ARPA_INET_H\r
50 #      include <arpa/inet.h>\r
51 #   elif defined( SYS_BEOS )\r
52 #      include <net/netdb.h>\r
53 #   endif\r
54 #endif\r
55 \r
56 #ifdef UNDER_CE\r
57 #   define close(a) CloseHandle(a);\r
58 #elif defined( WIN32 )\r
59 #   define close(a) closesocket(a);\r
60 #endif\r
61 \r
62 #include "network.h"\r
63 \r
64 #define NETSYNC_PORT_MASTER 9875\r
65 #define NETSYNC_PORT_SLAVE  9876\r
66 \r
67 /* Needed for Solaris */\r
68 #ifndef INADDR_NONE\r
69 #define INADDR_NONE 0xffffffff\r
70 #endif\r
71 \r
72 /*****************************************************************************\r
73  * Module descriptor\r
74  *****************************************************************************/\r
75 static int  Activate( vlc_object_t * );\r
76 static void Close   ( vlc_object_t * );\r
77 \r
78 static mtime_t GetClockRef( intf_thread_t *, mtime_t );\r
79 \r
80 #define NETSYNC_TEXT N_( "Act as master for network synchronisation" )\r
81 #define NETSYNC_LONGTEXT N_( "Allows you to specify if this client should " \\r
82   "act as the master client for the network synchronisation." )\r
83 \r
84 #define MIP_TEXT N_( "Master client ip address" )\r
85 #define MIP_LONGTEXT N_( "Allows you to specify the ip address of " \\r
86   "the master client used for the network synchronisation." )\r
87 \r
88 vlc_module_begin();\r
89     set_description( _("Network synchronisation") );\r
90 \r
91     add_bool( "netsync-master", 0, NULL,\r
92               NETSYNC_TEXT, NETSYNC_LONGTEXT, VLC_TRUE );\r
93     add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,\r
94                 VLC_TRUE );\r
95 \r
96     set_capability( "interface", 0 );\r
97     set_callbacks( Activate, Close );\r
98 vlc_module_end();\r
99 \r
100 struct intf_sys_t\r
101 {\r
102     input_thread_t *p_input;\r
103 };\r
104 \r
105 /*****************************************************************************\r
106  * Local prototypes\r
107  *****************************************************************************/\r
108 static void Run( intf_thread_t *p_intf );\r
109 \r
110 /*****************************************************************************\r
111  * Activate: initialize and create stuff\r
112  *****************************************************************************/\r
113 static int Activate( vlc_object_t *p_this )\r
114 {\r
115     intf_thread_t *p_intf = (intf_thread_t*)p_this;\r
116 \r
117     msg_Info( p_intf, "Using the netsync interface module..." );\r
118 \r
119     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );\r
120     if( !p_intf->p_sys )\r
121     {\r
122         msg_Err( p_intf, "no memory" );\r
123         return VLC_ENOMEM;\r
124     }\r
125 \r
126     p_intf->p_sys->p_input = NULL;\r
127 \r
128     p_intf->pf_run = Run;\r
129     return VLC_SUCCESS;\r
130 }\r
131 \r
132 /*****************************************************************************\r
133  * Close: destroy interface\r
134  *****************************************************************************/\r
135 void Close( vlc_object_t *p_this )\r
136 {\r
137     intf_thread_t *p_intf = (intf_thread_t*)p_this;\r
138 \r
139     free( p_intf->p_sys );\r
140 }\r
141 \r
142 /*****************************************************************************\r
143  * Run: interface thread\r
144  *****************************************************************************/\r
145 static void Run( intf_thread_t *p_intf )\r
146 {\r
147 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))\r
148 \r
149     vlc_bool_t b_master = config_GetInt( p_intf, "netsync-master" );\r
150     char *psz_master;\r
151     char p_data[MAX_MSG_LENGTH];\r
152     int i_socket;\r
153 \r
154     if( !b_master )\r
155     {\r
156         psz_master = config_GetPsz( p_intf, "netsync-master-ip" );\r
157         if( psz_master == NULL )\r
158         {\r
159             msg_Err( p_intf, "master address not specified" );\r
160             return;\r
161         }\r
162     }\r
163 \r
164     i_socket = net_OpenUDP( p_intf, NULL,\r
165                             b_master ? NETSYNC_PORT_MASTER : NETSYNC_PORT_SLAVE,\r
166                             b_master ? NULL : psz_master,\r
167                             b_master ? 0 : NETSYNC_PORT_MASTER );\r
168 \r
169     if( !b_master )\r
170         free( psz_master );\r
171     \r
172     if( i_socket < 0 )\r
173     {\r
174         msg_Err( p_intf, "failed opening UDP socket." );\r
175         return;\r
176     }\r
177 \r
178     /* High priority thread */\r
179     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );\r
180 \r
181     while( !p_intf->b_die )\r
182     {\r
183         struct timeval  timeout;\r
184         fd_set fds_r;\r
185 \r
186         /* Update the input */\r
187         if( p_intf->p_sys->p_input == NULL )\r
188         {\r
189             p_intf->p_sys->p_input =\r
190                 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,\r
191                                                    FIND_ANYWHERE );\r
192         }\r
193         else if( p_intf->p_sys->p_input->b_dead )\r
194         {\r
195             vlc_object_release( p_intf->p_sys->p_input );\r
196             p_intf->p_sys->p_input = NULL;\r
197         }\r
198 \r
199         if( p_intf->p_sys->p_input == NULL )\r
200         {\r
201             /* Wait a bit */\r
202             msleep( INTF_IDLE_SLEEP );\r
203             continue;\r
204         }\r
205 \r
206         /*\r
207          * We now have an input\r
208          */\r
209 \r
210         /* Initialize file descriptor set and timeout (0.5s) */\r
211         FD_ZERO( &fds_r );\r
212         FD_SET( i_socket, &fds_r );\r
213         timeout.tv_sec = 0;\r
214         timeout.tv_usec = 500000;\r
215 \r
216         if( b_master )\r
217         {\r
218             struct sockaddr_storage from;\r
219             mtime_t i_date, i_clockref, i_master_clockref;\r
220             int i_struct_size, i_read, i_ret;\r
221 \r
222             /* Don't block */\r
223             i_ret = select( i_socket + 1, &fds_r, 0, 0, &timeout );\r
224             if( i_ret == 0 ) continue;\r
225             if( i_ret < 0 )\r
226             {\r
227                 /* Wait a bit */\r
228                 msleep( INTF_IDLE_SLEEP );\r
229                 continue;\r
230             }\r
231 \r
232             /* We received something */\r
233             i_struct_size = sizeof( from );\r
234             i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,\r
235                                (struct sockaddr*)&from, &i_struct_size );\r
236 \r
237             i_clockref = ntoh64(*(int64_t *)p_data);\r
238 \r
239             i_date = mdate();\r
240             *(int64_t *)p_data = hton64( i_date );\r
241 \r
242             i_master_clockref = GetClockRef( p_intf, i_clockref );\r
243             *(((int64_t *)p_data)+1) = hton64( i_master_clockref );\r
244 \r
245             /* Reply to the sender */\r
246             sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,\r
247                     (struct sockaddr *)&from, i_struct_size );\r
248 \r
249             msg_Dbg( p_intf, "Master clockref: "I64Fd" -> "I64Fd", from %s "\r
250                      "(date: "I64Fd")", i_clockref, i_master_clockref,\r
251                      from.ss_family == AF_INET\r
252                      ? inet_ntoa(((struct sockaddr_in *)&from)->sin_addr)\r
253                      : "non-IPv4", i_date );\r
254         }\r
255         else\r
256         {\r
257             mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date;\r
258             mtime_t i_master_clockref, i_client_clockref, i_drift;\r
259             mtime_t i_clockref = 0;\r
260             int i_sent, i_read, i_ret;\r
261 \r
262             /* Send clock request to the master */\r
263             *(int64_t *)p_data = hton64( i_clockref );\r
264             i_send_date = mdate();\r
265 \r
266             i_sent = send( i_socket, p_data, sizeof(int64_t), 0 );\r
267             if( i_sent <= 0 )\r
268             {\r
269                 /* Wait a bit */\r
270                 msleep( INTF_IDLE_SLEEP );\r
271                 continue;\r
272             }\r
273 \r
274             /* Don't block */\r
275             i_ret = select(i_socket + 1, &fds_r, 0, 0, &timeout);\r
276             if( i_ret == 0 ) continue;\r
277             if( i_ret < 0 )\r
278             {\r
279                 /* Wait a bit */\r
280                 msleep( INTF_IDLE_SLEEP );\r
281                 continue;\r
282             }\r
283 \r
284             i_receive_date = mdate();\r
285 \r
286             i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );\r
287             if( i_read <= 0 )\r
288             {\r
289                 /* Wait a bit */\r
290                 msleep( INTF_IDLE_SLEEP );\r
291                 continue;\r
292             }\r
293 \r
294             i_master_date = ntoh64(*(int64_t *)p_data);\r
295             i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));\r
296 \r
297             i_diff_date = i_receive_date -\r
298                           ((i_receive_date - i_send_date) / 2 + i_master_date);\r
299 \r
300             i_client_clockref = i_drift = 0;\r
301             if( p_intf->p_sys->p_input && i_master_clockref )\r
302             {\r
303                 i_client_clockref = GetClockRef( p_intf, i_clockref );\r
304                 i_drift = i_client_clockref - i_master_clockref - i_diff_date;\r
305 \r
306                 /* Update our clock to match the master's one */\r
307                 if( i_client_clockref )\r
308                     p_intf->p_sys->p_input->i_pts_delay -= i_drift;\r
309             }\r
310 \r
311             msg_Dbg( p_intf, "Slave clockref: "I64Fd" -> "I64Fd" -> "I64Fd", "\r
312                      "clock diff: "I64Fd" drift: "I64Fd,\r
313                      i_clockref, i_master_clockref, \r
314                      i_client_clockref, i_diff_date, i_drift );\r
315 \r
316             /* Wait a bit */\r
317             msleep( INTF_IDLE_SLEEP );\r
318         }\r
319     }\r
320 \r
321     if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );\r
322     net_Close( i_socket );\r
323 }\r
324 \r
325 static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )\r
326 {\r
327     input_thread_t *p_input = p_intf->p_sys->p_input;\r
328     pgrm_descriptor_t *p_pgrm;\r
329 \r
330     if( !p_input ) return 0;\r
331 \r
332 #if 0\r
333     p_pgrm = p_input->stream.p_selected_program;\r
334     if( p_pgrm ) return input_ClockGetTS( p_input, p_pgrm, i_pts );\r
335 #else\r
336 #warning "This code is currently broken. FIXME!!!"\r
337 #endif\r
338 \r
339     return 0;\r
340 }\r