]> git.sesse.net Git - vlc/blob - src/stream_output/sap.c
0cb06b281b366984d13281399ee8886a8a07d9fa
[vlc] / src / stream_output / sap.c
1 /*****************************************************************************
2  * sap.c : SAP announce handler
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* free() */
28 #include <stdio.h>                                              /* sprintf() */
29 #include <string.h>                                            /* strerror() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/sout.h>
33
34 #include <network.h>
35
36 #define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */
37 #define SAP_PORT 9875
38
39 #define SAP_IPV6_ADDR_1 "FF0"
40 #define SAP_IPV6_ADDR_2 "::2:7FFE"
41
42 #define DEFAULT_IPV6_SCOPE '8'
43
44 #define DEFAULT_PORT "1234"
45
46 #undef EXTRA_DEBUG
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static void RunThread( vlc_object_t *p_this);
52 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address );
53 static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session );
54
55 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
56                                      sap_session_t *p_session );
57
58
59 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
60                              session_descriptor_t *p_session,
61                              announce_method_t *p_method );
62
63 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
64                              session_descriptor_t *p_session );
65
66 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
67
68
69 /**
70  * Create the SAP handler
71  *
72  * \param p_announce the parent announce_handler
73  * \return the newly created SAP handler or NULL on error
74  */
75 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
76 {
77     sap_handler_t *p_sap;
78
79     p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );
80
81     if( !p_sap )
82     {
83         msg_Err( p_announce, "out of memory" );
84         return NULL;
85     }
86
87     vlc_mutex_init( p_sap, &p_sap->object_lock );
88
89     p_sap->pf_add = announce_SAPAnnounceAdd;
90     p_sap->pf_del = announce_SAPAnnounceDel;
91
92     p_sap->i_sessions = 0;
93     p_sap->i_addresses = 0;
94     p_sap->i_current_session = 0;
95
96     p_sap->b_control = config_GetInt( p_sap, "sap-flow-control");
97
98     if( vlc_thread_create( p_sap, "sap handler", RunThread,
99                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
100     {
101         msg_Dbg( p_announce, "Unable to spawn SAP handler thread");
102         free( p_sap );
103         return NULL;
104     };
105     msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions);
106     return p_sap;
107 }
108
109 /**
110  *  Destroy the SAP handler
111  *  \param p_this the SAP Handler to destroy
112  *  \return nothing
113  */
114 void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
115 {
116     int i;
117
118     vlc_mutex_destroy( &p_sap->object_lock );
119
120     /* Free the remaining sessions */
121     for( i = 0 ; i< p_sap->i_sessions ; i++)
122     {
123         sap_session_t *p_session = p_sap->pp_sessions[i];
124         FREE( p_session->psz_sdp );
125         FREE( p_session->psz_data );
126         REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );
127         FREE( p_session );
128     }
129
130     /* Free the remaining addresses */
131     for( i = 0 ; i< p_sap->i_addresses ; i++)
132     {
133         sap_address_t *p_address = p_sap->pp_addresses[i];
134         FREE( p_address->psz_address );
135         if( p_address->i_rfd > -1 )
136         {
137             net_Close( p_address->i_rfd );
138         }
139         if( p_address->i_wfd > -1 && p_sap->b_control )
140         {
141             net_Close( p_address->i_wfd );
142         }
143         REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );
144         FREE( p_address );
145     }
146
147     /* Free the structure */
148     vlc_object_destroy( p_sap );
149 }
150
151 /**
152  * main SAP handler thread
153  * \param p_this the SAP Handler object
154  * \return nothing
155  */
156 static void RunThread( vlc_object_t *p_this)
157 {
158     sap_handler_t *p_sap = (sap_handler_t*)p_this;
159     sap_session_t *p_session;
160
161     while( !p_sap->b_die )
162     {
163         int i;
164
165         /* If needed, get the rate info */
166         if( p_sap->b_control == VLC_TRUE )
167         {
168             for( i = 0 ; i< p_sap->i_addresses ; i++)
169             {
170                 if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE )
171                 {
172                     CalculateRate( p_sap, p_sap->pp_addresses[i] );
173                 }
174             }
175         }
176
177         /* Find the session to announce */
178         vlc_mutex_lock( &p_sap->object_lock );
179         if( p_sap->i_sessions > p_sap->i_current_session + 1)
180         {
181             p_sap->i_current_session++;
182         }
183         else if( p_sap->i_sessions > 0)
184         {
185             p_sap->i_current_session = 0;
186         }
187         else
188         {
189             vlc_mutex_unlock( &p_sap->object_lock );
190             msleep( SAP_IDLE );
191             continue;
192         }
193         p_session = p_sap->pp_sessions[p_sap->i_current_session];
194         vlc_mutex_unlock( &p_sap->object_lock );
195
196         /* And announce it */
197         if( p_session->p_address->b_enabled == VLC_TRUE &&
198             p_session->p_address->b_ready == VLC_TRUE )
199         {
200             announce_SendSAPAnnounce( p_sap, p_session );
201         }
202
203         msleep( SAP_IDLE );
204     }
205 }
206
207 /* Add a SAP announce */
208 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
209                              session_descriptor_t *p_session,
210                              announce_method_t *p_method )
211 {
212     int i;
213     char *psz_type = "application/sdp";
214     int i_header_size;
215     char *psz_head;
216     vlc_bool_t b_found = VLC_FALSE;
217     sap_session_t *p_sap_session;
218     mtime_t i_hash;
219
220     vlc_mutex_lock( &p_sap->object_lock );
221
222     /* If needed, build the SDP */
223     if( !p_session->psz_sdp )
224     {
225         if ( SDPGenerate( p_sap, p_session ) != VLC_SUCCESS )
226         {
227             vlc_mutex_unlock( &p_sap->object_lock );
228             return VLC_EGENERIC;
229         }
230     }
231
232     if( !p_method->psz_address )
233     {
234         if( p_method->i_ip_version == 6 )
235         {
236             char sz_scope;
237             if( p_method->psz_ipv6_scope != NULL )
238             {
239                 sz_scope = *p_method->psz_ipv6_scope;
240             }
241             else
242             {
243                 sz_scope = DEFAULT_IPV6_SCOPE;
244             }
245             p_method->psz_address = (char*)malloc( 30*sizeof(char ));
246             sprintf( p_method->psz_address, "%s%c%s",
247                             SAP_IPV6_ADDR_1, sz_scope, SAP_IPV6_ADDR_2 );
248         }
249         else
250         {
251             /* IPv4 */
252             p_method->psz_address = (char*)malloc( 15*sizeof(char) );
253             snprintf(p_method->psz_address, 15, SAP_IPV4_ADDR );
254         }
255     }
256     msg_Dbg( p_sap, "using SAP address: %s",p_method->psz_address);
257
258     /* XXX: Check for dupes */
259     p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
260
261     p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
262     p_sap_session->i_last = 0;
263
264     /* Add the address to the buffer */
265     for( i = 0; i< p_sap->i_addresses; i++)
266     {
267         if( !strcmp( p_method->psz_address,
268              p_sap->pp_addresses[i]->psz_address ) )
269         {
270             p_sap_session->p_address = p_sap->pp_addresses[i];
271             b_found = VLC_TRUE;
272             break;
273         }
274     }
275     if( b_found == VLC_FALSE )
276     {
277         sap_address_t *p_address = (sap_address_t *)
278                                     malloc( sizeof(sap_address_t) );
279         if( !p_address )
280         {
281             msg_Err( p_sap, "out of memory" );
282             return VLC_ENOMEM;
283         }
284         p_address->psz_address = strdup( p_method->psz_address );
285         p_address->i_ip_version = p_method->i_ip_version;
286         p_address->i_port  =  9875;
287         p_address->i_wfd = net_OpenUDP( p_sap, "", 0,
288                                         p_address->psz_address,
289                                         p_address->i_port );
290
291         if( p_sap->b_control == VLC_TRUE )
292         {
293             p_address->i_rfd = net_OpenUDP( p_sap, p_method->psz_address,
294                                             p_address->i_port,
295                                             "", 0 );
296             p_address->i_buff = 0;
297             p_address->b_enabled = VLC_TRUE;
298             p_address->b_ready = VLC_FALSE;
299             p_address->i_limit = 10000; /* 10000 bps */
300             p_address->t1 = 0;
301         }
302         else
303         {
304             p_address->b_enabled = VLC_TRUE;
305             p_address->b_ready = VLC_TRUE;
306             p_address->i_interval = config_GetInt( p_sap,"sap-interval");
307         }
308
309         if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
310                                         && p_sap->b_control ) )
311         {
312             msg_Warn( p_sap, "disabling address" );
313             p_address->b_enabled = VLC_FALSE;
314         }
315
316         INSERT_ELEM( p_sap->pp_addresses,
317                      p_sap->i_addresses,
318                      p_sap->i_addresses,
319                      p_address );
320         p_sap_session->p_address = p_address;
321     }
322
323     /* Build the SAP Headers */
324     i_header_size = ( p_method->i_ip_version == 6 ? 20 : 8 ) + strlen( psz_type ) + 1;
325     psz_head = (char *) malloc( i_header_size * sizeof( char ) );
326     if( ! psz_head )
327     {
328         msg_Err( p_sap, "out of memory" );
329         return VLC_ENOMEM;
330     }
331
332     psz_head[0] = 0x20; /* Means SAPv1, IPv4, not encrypted, not compressed */
333     psz_head[1] = 0x00; /* No authentification length */
334
335     i_hash = mdate();
336     psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */
337     psz_head[3] = (i_hash & 0xFF);        /* Msg id hash 2 */
338
339     if( p_method->i_ip_version == 6 )
340     {
341         /* in_addr_t ip_server = inet_addr( ip ); */
342         psz_head[0] |= 0x10; /* Set IPv6 */
343
344         psz_head[4] = 0x01; /* Source IP  FIXME: we should get the real address */
345         psz_head[5] = 0x02; /* idem */
346         psz_head[6] = 0x03; /* idem */
347         psz_head[7] = 0x04; /* idem */
348
349         psz_head[8] = 0x01; /* Source IP  FIXME: we should get the real address */
350         psz_head[9] = 0x02; /* idem */
351         psz_head[10] = 0x03; /* idem */
352         psz_head[11] = 0x04; /* idem */
353
354         psz_head[12] = 0x01; /* Source IP  FIXME: we should get the real address */
355         psz_head[13] = 0x02; /* idem */
356         psz_head[14] = 0x03; /* idem */
357         psz_head[15] = 0x04; /* idem */
358
359         psz_head[16] = 0x01; /* Source IP  FIXME: we should get the real address */
360         psz_head[17] = 0x02; /* idem */
361         psz_head[18] = 0x03; /* idem */
362         psz_head[19] = 0x04; /* idem */
363
364         strncpy( psz_head + 20, psz_type, 15 );
365     }
366     else
367     {
368         /* in_addr_t ip_server = inet_addr( ip) */
369         /* Source IP  FIXME: we should get the real address */
370         psz_head[4] = 0x01; /* ip_server */
371         psz_head[5] = 0x02; /* ip_server>>8 */
372         psz_head[6] = 0x03; /* ip_server>>16 */
373         psz_head[7] = 0x04; /* ip_server>>24 */
374
375         strncpy( psz_head + 8, psz_type, 15 );
376     }
377
378     psz_head[ i_header_size-1 ] = '\0';
379     p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);
380
381     p_sap_session->psz_data = (char *)malloc( sizeof(char)*
382                                               p_sap_session->i_length );
383
384     /* Build the final message */
385     memcpy( p_sap_session->psz_data, psz_head, i_header_size );
386     memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp,
387             strlen( p_sap_session->psz_sdp) );
388
389     free( psz_head );
390
391     /* Enqueue the announce */
392     INSERT_ELEM( p_sap->pp_sessions,
393                  p_sap->i_sessions,
394                  p_sap->i_sessions,
395                  p_sap_session );
396     msg_Dbg( p_sap,"Addresses: %i  Sessions: %i",
397                    p_sap->i_addresses,p_sap->i_sessions);
398
399     /* Remember the SAP session for later deletion */
400     p_session->p_sap = p_sap_session;
401
402     vlc_mutex_unlock( &p_sap->object_lock );
403
404     return VLC_SUCCESS;
405 }
406
407 /* Remove a SAP Announce */
408 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
409                              session_descriptor_t *p_session )
410 {
411     int i;
412     vlc_mutex_lock( &p_sap->object_lock );
413
414     msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap);
415
416     /* Dequeue the announce */
417     for( i = 0; i< p_sap->i_sessions; i++)
418     {
419         if( p_session->p_sap == p_sap->pp_sessions[i] )
420         {
421             REMOVE_ELEM( p_sap->pp_sessions,
422                          p_sap->i_sessions,
423                          i );
424             break;
425         }
426     }
427
428     /* XXX: Dequeue the address too if it is not used anymore
429      * TODO: - address refcount
430              - send a SAP deletion packet */
431
432     msg_Dbg( p_sap,"%i announces remaining", p_sap->i_sessions );
433
434     vlc_mutex_unlock( &p_sap->object_lock );
435
436     return VLC_SUCCESS;
437 }
438
439 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
440                                      sap_session_t *p_session )
441 {
442     int i_ret;
443
444     /* This announce has never been sent yet */
445     if( p_session->i_last == 0 )
446     {
447         p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
448         p_session->i_last = 1;
449         return VLC_SUCCESS;
450     }
451
452     if( p_session->i_next < mdate() )
453     {
454 #ifdef EXTRA_DEBUG
455         msg_Dbg( p_sap, "Sending announce");
456 #endif
457         i_ret = net_Write( p_sap, p_session->p_address->i_wfd,
458                            p_session->psz_data,
459                            p_session->i_length );
460         if( i_ret  != p_session->i_length )
461         {
462             msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
463                    p_session->p_address->psz_address,
464                    i_ret, p_session->i_length );
465         }
466         p_session->i_last = p_session->i_next;
467         p_session->i_next = p_session->i_last
468                             + p_session->p_address->i_interval*1000000;
469     }
470     else
471     {
472         return VLC_SUCCESS;
473     }
474     return VLC_SUCCESS;
475 }
476
477 static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
478 {
479     int64_t i_sdp_id = mdate();
480     int     i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
481
482     /* see the lists in modules/stream_out/rtp.c for compliance stuff */
483     p_session->psz_sdp = (char *)malloc(
484                             sizeof("v=0\r\n"
485                                    "o=- 45383436098 45398  IN IP4 127.0.0.1\r\n" /* FIXME */
486                                    "s=\r\n"
487                                    "t=0 0\r\n"
488                                    "c=IN IP4 /\r\n"
489                                    "m=video  udp\r\n"
490                                    "a=tool:"PACKAGE_STRING"\r\n"
491                                    "a=type:broadcast\r\n")
492                            + strlen( p_session->psz_name )
493                            + strlen( p_session->psz_uri ) + 300 );
494     if( !p_session->psz_sdp )
495     {
496         msg_Err( p_sap, "out of memory" );
497         return VLC_ENOMEM;
498     }
499     sprintf( p_session->psz_sdp,
500                             "v=0\r\n"
501                             "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n"
502                             "s=%s\r\n"
503                             "t=0 0\r\n"
504                             "c=IN IP4 %s/%d\r\n"
505                             "m=video %d udp %d\r\n"
506                             "a=tool:"PACKAGE_STRING"\r\n"
507                             "a=type:broadcast\r\n",
508                             i_sdp_id, i_sdp_version,
509                             p_session->psz_name,
510                             p_session->psz_uri, p_session->i_ttl,
511                             p_session->i_port, p_session->i_payload );
512     msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(p_session->psz_sdp),
513                     p_session->psz_sdp );
514     return VLC_SUCCESS;
515 }
516
517 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
518 {
519     int i_read;
520     char buffer[SAP_MAX_BUFFER];
521     int i_tot = 0;
522     mtime_t i_temp;
523     int i_rate;
524
525     if( p_address->t1 == 0 )
526     {
527         p_address->t1 = mdate();
528         return VLC_SUCCESS;
529     }
530     do
531     {
532         /* Might be too slow if we have huge data */
533         i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, buffer,
534                                    SAP_MAX_BUFFER, 0 );
535         i_tot += i_read;
536     } while( i_read > 0 && i_tot < SAP_MAX_BUFFER );
537
538     i_temp = mdate();
539
540     /* We calculate the rate every 5 seconds */
541     if( i_temp - p_address->t1 < 5000000 )
542     {
543         p_address->i_buff += i_tot;
544         return VLC_SUCCESS;
545     }
546
547     /* Bits/second */
548     i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
549                         (i_temp - p_address->t1 ));
550
551     p_address->i_limit = 10000;
552
553     p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
554                             (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
555
556     if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
557     {
558         p_address->i_interval = MAX_INTERVAL;
559     }
560 #ifdef EXTRA_DEBUG
561     msg_Dbg( p_sap,"%s:%i : Rate=%i, Interval = %i s",
562                     p_address->psz_address,p_address->i_port,
563                     i_rate,
564                     p_address->i_interval );
565 #endif
566
567     p_address->b_ready = VLC_TRUE;
568
569     p_address->t1 = i_temp;
570     p_address->i_buff = 0;
571
572     return VLC_SUCCESS;
573 }