]> git.sesse.net Git - vlc/blob - modules/control/netsync.c
* modules/control/netsync.c: test before you commit.
[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 #include "input_ext-plugins.h"\r
64 \r
65 #define NETSYNC_PORT_MASTER 9875\r
66 #define NETSYNC_PORT_SLAVE  9876\r
67 \r
68 /*****************************************************************************\r
69  * Module descriptor\r
70  *****************************************************************************/\r
71 static int  Activate( vlc_object_t * );\r
72 static void Close   ( vlc_object_t * );\r
73 \r
74 static mtime_t GetClockRef( intf_thread_t *, mtime_t );\r
75 \r
76 #define NETSYNC_TEXT N_( "Act as master for network synchronisation" )\r
77 #define NETSYNC_LONGTEXT N_( "Allows you to specify if this client should " \\r
78   "act as the master client for the network synchronisation." )\r
79 \r
80 #define MIP_TEXT N_( "Master client ip address" )\r
81 #define MIP_LONGTEXT N_( "Allows you to specify the ip address of " \\r
82   "the master client used for the network synchronisation." )\r
83 \r
84 vlc_module_begin();\r
85     set_description( _("Network synchronisation") );\r
86 \r
87     add_bool( "netsync-master", 0, NULL,\r
88               NETSYNC_TEXT, NETSYNC_LONGTEXT, VLC_TRUE );\r
89     add_string( "netsync-master-ip", NULL, NULL, MIP_TEXT, MIP_LONGTEXT,\r
90                 VLC_TRUE );\r
91 \r
92     set_capability( "interface", 0 );\r
93     set_callbacks( Activate, Close );\r
94 vlc_module_end();\r
95 \r
96 struct intf_sys_t\r
97 {\r
98     input_thread_t *p_input;\r
99 };\r
100 \r
101 /*****************************************************************************\r
102  * Local prototypes\r
103  *****************************************************************************/\r
104 static void Run( intf_thread_t *p_intf );\r
105 \r
106 /*****************************************************************************\r
107  * Activate: initialize and create stuff\r
108  *****************************************************************************/\r
109 static int Activate( vlc_object_t *p_this )\r
110 {\r
111     intf_thread_t *p_intf = (intf_thread_t*)p_this;\r
112 \r
113     msg_Info( p_intf, "Using the netsync interface module..." );\r
114 \r
115     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );\r
116     if( !p_intf->p_sys )\r
117     {\r
118         msg_Err( p_intf, "no memory" );\r
119         return VLC_ENOMEM;\r
120     }\r
121 \r
122     p_intf->p_sys->p_input = NULL;\r
123 \r
124     p_intf->pf_run = Run;\r
125     return VLC_SUCCESS;\r
126 }\r
127 \r
128 /*****************************************************************************\r
129  * Close: destroy interface\r
130  *****************************************************************************/\r
131 void Close( vlc_object_t *p_this )\r
132 {\r
133     intf_thread_t *p_intf = (intf_thread_t*)p_this;\r
134 \r
135     free( p_intf->p_sys );\r
136 }\r
137 \r
138 /*****************************************************************************\r
139  * Run: interface thread\r
140  *****************************************************************************/\r
141 static void Run( intf_thread_t *p_intf )\r
142 {\r
143 #define MAX_MSG_LENGTH (2 * sizeof(int64_t))\r
144 \r
145     vlc_bool_t b_master = config_GetInt( p_intf, "netsync-master" );\r
146     char *psz_master = config_GetPsz( p_intf, "netsync-master-ip" );\r
147     struct sockaddr_in master_addr;\r
148     char p_data[MAX_MSG_LENGTH];\r
149     int i_socket;\r
150 \r
151     if( !psz_master || inet_addr( psz_master ) == INADDR_NONE )\r
152     {\r
153         if( !b_master )\r
154         {\r
155             if( psz_master ) free( psz_master );\r
156             msg_Err( p_intf, "invalid master address." );\r
157             return;\r
158         }\r
159 \r
160         if( !psz_master ) psz_master = strdup("");\r
161     }\r
162 \r
163     memset( &master_addr, 0, sizeof( struct sockaddr_in ) );\r
164     master_addr.sin_family = AF_INET;\r
165     master_addr.sin_port = htons( (uint16_t)NETSYNC_PORT_MASTER );\r
166     master_addr.sin_addr.s_addr = inet_addr( psz_master );\r
167     free( psz_master );\r
168 \r
169     i_socket = net_OpenUDP( p_intf, NULL, b_master ? NETSYNC_PORT_MASTER :\r
170                             NETSYNC_PORT_SLAVE, NULL, 0 );\r
171     if( i_socket < 0 )\r
172     {\r
173         msg_Err( p_intf, "failed opening UDP socket." );\r
174         return;\r
175     }\r
176 \r
177     /* High priority thread */\r
178     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_INPUT );\r
179 \r
180     while( !p_intf->b_die )\r
181     {\r
182         struct timeval  timeout;\r
183         fd_set fds_r;\r
184 \r
185         /* Update the input */\r
186         if( p_intf->p_sys->p_input == NULL )\r
187         {\r
188             p_intf->p_sys->p_input =\r
189                 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,\r
190                                                    FIND_ANYWHERE );\r
191         }\r
192         else if( p_intf->p_sys->p_input->b_dead )\r
193         {\r
194             vlc_object_release( p_intf->p_sys->p_input );\r
195             p_intf->p_sys->p_input = NULL;\r
196         }\r
197 \r
198         if( p_intf->p_sys->p_input == NULL )\r
199         {\r
200             /* Wait a bit */\r
201             msleep( INTF_IDLE_SLEEP );\r
202             continue;\r
203         }\r
204 \r
205         /*\r
206          * We now have an input\r
207          */\r
208 \r
209         /* Initialize file descriptor set and timeout (0.5s) */\r
210         FD_ZERO( &fds_r );\r
211         FD_SET( i_socket, &fds_r );\r
212         timeout.tv_sec = 0;\r
213         timeout.tv_usec = 500000;\r
214 \r
215         if( b_master )\r
216         {\r
217             struct sockaddr_in from;\r
218             mtime_t i_date, i_clockref, i_master_clockref;\r
219             int i_struct_size, i_read, i_ret;\r
220 \r
221             /* Don't block */\r
222             i_ret = select( i_socket + 1, &fds_r, 0, 0, &timeout );\r
223             if( i_ret == 0 ) continue;\r
224             if( i_ret < 0 )\r
225             {\r
226                 /* Wait a bit */\r
227                 msleep( INTF_IDLE_SLEEP );\r
228                 continue;\r
229             }\r
230 \r
231             /* We received something */\r
232             i_struct_size = sizeof(struct sockaddr_in);\r
233             i_read = recvfrom( i_socket, p_data, MAX_MSG_LENGTH, 0,\r
234                                (struct sockaddr*)&from, &i_struct_size );\r
235 \r
236             from.sin_port = htons( (uint16_t)NETSYNC_PORT_SLAVE );\r
237 \r
238             i_clockref = ntoh64(*(int64_t *)p_data);\r
239 \r
240             i_date = mdate();\r
241             *(int64_t *)p_data = hton64( i_date );\r
242 \r
243             i_master_clockref = GetClockRef( p_intf, i_clockref );\r
244             *(((int64_t *)p_data)+1) = hton64( i_master_clockref );\r
245 \r
246             /* Reply to the sender */\r
247             sendto( i_socket, p_data, 2 * sizeof(int64_t), 0,\r
248                     (struct sockaddr *)&from, sizeof(struct sockaddr_in) );\r
249 \r
250             msg_Dbg( p_intf, "Master clockref: "I64Fd" -> "I64Fd", from %s "\r
251                      "(date: "I64Fd")", i_clockref, i_master_clockref, \r
252                      inet_ntoa(from.sin_addr), i_date );\r
253         }\r
254         else\r
255         {\r
256             mtime_t i_send_date, i_receive_date, i_master_date, i_diff_date;\r
257             mtime_t i_master_clockref, i_client_clockref, i_drift;\r
258             mtime_t i_clockref = 0;\r
259             int i_sent, i_read, i_ret;\r
260 \r
261             /* Send clock request to the master */\r
262             *(int64_t *)p_data = hton64( i_clockref );\r
263             i_send_date = mdate();\r
264 \r
265             i_sent = sendto( i_socket, p_data, sizeof(int64_t), 0,\r
266                              (struct sockaddr *)&master_addr,\r
267                              sizeof(struct sockaddr_in) );\r
268             if( i_sent <= 0 )\r
269             {\r
270                 /* Wait a bit */\r
271                 msleep( INTF_IDLE_SLEEP );\r
272                 continue;\r
273             }\r
274 \r
275             /* Don't block */\r
276             i_ret = select(i_socket + 1, &fds_r, 0, 0, &timeout);\r
277             if( i_ret == 0 ) continue;\r
278             if( i_ret < 0 )\r
279             {\r
280                 /* Wait a bit */\r
281                 msleep( INTF_IDLE_SLEEP );\r
282                 continue;\r
283             }\r
284 \r
285             i_receive_date = mdate();\r
286 \r
287             i_read = recv( i_socket, p_data, MAX_MSG_LENGTH, 0 );\r
288             if( i_read <= 0 )\r
289             {\r
290                 /* Wait a bit */\r
291                 msleep( INTF_IDLE_SLEEP );\r
292                 continue;\r
293             }\r
294 \r
295             i_master_date = ntoh64(*(int64_t *)p_data);\r
296             i_master_clockref = ntoh64(*(((int64_t *)p_data)+1));\r
297 \r
298             i_diff_date = i_receive_date -\r
299                           ((i_receive_date - i_send_date) / 2 + i_master_date);\r
300 \r
301             i_client_clockref = i_drift = 0;\r
302             if( p_intf->p_sys->p_input && i_master_clockref )\r
303             {\r
304                 i_client_clockref = GetClockRef( p_intf, i_clockref );\r
305                 i_drift = i_client_clockref - i_master_clockref - i_diff_date;\r
306 \r
307                 /* Update our clock to match the master's one */\r
308                 if( i_client_clockref )\r
309                     p_intf->p_sys->p_input->i_pts_delay -= i_drift;\r
310             }\r
311 \r
312             msg_Dbg( p_intf, "Slave clockref: "I64Fd" -> "I64Fd" -> "I64Fd", "\r
313                      "clock diff: "I64Fd" drift: "I64Fd,\r
314                      i_clockref, i_master_clockref, \r
315                      i_client_clockref, i_diff_date, i_drift );\r
316 \r
317             /* Wait a bit */\r
318             msleep( INTF_IDLE_SLEEP );\r
319         }\r
320     }\r
321 \r
322     if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );\r
323     net_Close( i_socket );\r
324 }\r
325 \r
326 static mtime_t GetClockRef( intf_thread_t *p_intf, mtime_t i_pts )\r
327 {\r
328     input_thread_t *p_input = p_intf->p_sys->p_input;\r
329     pgrm_descriptor_t *p_pgrm;\r
330 \r
331     if( !p_input ) return 0;\r
332 \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 \r
336     return 0;\r
337 }\r