]> git.sesse.net Git - vlc/blob - src/stream_output/sap.c
* Fixed the udp SDPs. More spec compliant now.
[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
219     vlc_mutex_lock( &p_sap->object_lock );
220
221     /* If needed, build the SDP */
222     if( !p_session->psz_sdp )
223     {
224         if ( SDPGenerate( p_sap, p_session ) != VLC_SUCCESS )
225         {
226             vlc_mutex_unlock( &p_sap->object_lock );
227             return VLC_EGENERIC;
228         }
229     }
230
231     if( !p_method->psz_address )
232     {
233         if( p_method->i_ip_version == 6 )
234         {
235             char sz_scope;
236             if( p_method->psz_ipv6_scope != NULL )
237             {
238                 sz_scope = *p_method->psz_ipv6_scope;
239             }
240             else
241             {
242                 sz_scope = DEFAULT_IPV6_SCOPE;
243             }
244             p_method->psz_address = (char*)malloc( 30*sizeof(char ));
245             sprintf( p_method->psz_address, "%s%c%s",
246                             SAP_IPV6_ADDR_1, sz_scope, SAP_IPV6_ADDR_2 );
247         }
248         else
249         {
250             /* IPv4 */
251             p_method->psz_address = (char*)malloc( 15*sizeof(char) );
252             sprintf(p_method->psz_address, SAP_IPV4_ADDR );
253         }
254     }
255     msg_Dbg( p_sap, "using SAP address: %s",p_method->psz_address);
256
257     /* XXX: Check for dupes */
258     p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
259
260     p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
261     p_sap_session->i_last = 0;
262
263     /* Add the address to the buffer */
264     for( i = 0; i< p_sap->i_addresses; i++)
265     {
266         if( !strcmp( p_method->psz_address,
267              p_sap->pp_addresses[i]->psz_address ) )
268         {
269             p_sap_session->p_address = p_sap->pp_addresses[i];
270             b_found = VLC_TRUE;
271             break;
272         }
273     }
274     if( b_found == VLC_FALSE )
275     {
276         sap_address_t *p_address = (sap_address_t *)
277                                     malloc( sizeof(sap_address_t) );
278         if( !p_address )
279         {
280             msg_Err( p_sap, "out of memory" );
281             return VLC_ENOMEM;
282         }
283         p_address->psz_address = strdup( p_method->psz_address );
284         p_address->i_port  =  9875;
285         p_address->i_wfd = net_OpenUDP( p_sap, "", 0,
286                                         p_address->psz_address,
287                                         p_address->i_port );
288
289         if( p_sap->b_control == VLC_TRUE )
290         {
291             p_address->i_rfd = net_OpenUDP( p_sap, p_method->psz_address,
292                                             p_address->i_port,
293                                             "", 0 );
294             p_address->i_buff = 0;
295             p_address->b_enabled = VLC_TRUE;
296             p_address->b_ready = VLC_FALSE;
297             p_address->i_limit = 10000; /* 10000 bps */
298             p_address->t1 = 0;
299         }
300         else
301         {
302             p_address->b_enabled = VLC_TRUE;
303             p_address->b_ready = VLC_TRUE;
304             p_address->i_interval = config_GetInt( p_sap,"sap-interval");
305         }
306
307         if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
308                                         && p_sap->b_control ) )
309         {
310             msg_Warn( p_sap, "disabling address" );
311             p_address->b_enabled = VLC_FALSE;
312         }
313
314         INSERT_ELEM( p_sap->pp_addresses,
315                      p_sap->i_addresses,
316                      p_sap->i_addresses,
317                      p_address );
318         p_sap_session->p_address = p_address;
319     }
320
321     /* Build the SAP Headers */
322     i_header_size = 8 + strlen( psz_type ) + 1;
323     psz_head = (char *) malloc( i_header_size * sizeof( char ) );
324     if( ! psz_head )
325     {
326         msg_Err( p_sap, "out of memory" );
327         return VLC_ENOMEM;
328     }
329
330     psz_head[0] = 0x20; /* Means IPv4, not encrypted, not compressed */
331     psz_head[1] = 0x00; /* No authentification */
332     psz_head[2] = 0x42; /* Msg id hash */
333     psz_head[3] = 0x12; /* Msg id hash 2 */
334
335     psz_head[4] = 0x01; /* Source IP  FIXME: we should get the real address */
336     psz_head[5] = 0x02; /* idem */
337     psz_head[6] = 0x03; /* idem */
338     psz_head[7] = 0x04; /* idem */
339
340     strncpy( psz_head + 8, psz_type, 15 );
341     psz_head[ i_header_size-1 ] = '\0';
342     p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);
343
344     p_sap_session->psz_data = (char *)malloc( sizeof(char)*
345                                               p_sap_session->i_length );
346
347     /* Build the final message */
348     memcpy( p_sap_session->psz_data, psz_head, i_header_size );
349     memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp,
350             strlen( p_sap_session->psz_sdp) );
351
352     /* Enqueue the announce */
353     INSERT_ELEM( p_sap->pp_sessions,
354                  p_sap->i_sessions,
355                  p_sap->i_sessions,
356                  p_sap_session );
357     msg_Dbg( p_sap,"Addresses: %i  Sessions: %i",
358                    p_sap->i_addresses,p_sap->i_sessions);
359
360     /* Remember the SAP session for later deletion */
361     p_session->p_sap = p_sap_session;
362
363     vlc_mutex_unlock( &p_sap->object_lock );
364
365     return VLC_SUCCESS;
366 }
367
368 /* Remove a SAP Announce */
369 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
370                              session_descriptor_t *p_session )
371 {
372     int i;
373     vlc_mutex_lock( &p_sap->object_lock );
374
375     msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap);
376
377     /* Dequeue the announce */
378     for( i = 0; i< p_sap->i_sessions; i++)
379     {
380         if( p_session->p_sap == p_sap->pp_sessions[i] )
381         {
382             REMOVE_ELEM( p_sap->pp_sessions,
383                          p_sap->i_sessions,
384                          i );
385             break;
386         }
387     }
388
389     /* XXX: Dequeue the adress too if it is not used anymore
390      * TODO: address refcount */
391
392     msg_Dbg( p_sap,"%i announces remaining", p_sap->i_sessions );
393
394     vlc_mutex_unlock( &p_sap->object_lock );
395
396     return VLC_SUCCESS;
397 }
398
399 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
400                                      sap_session_t *p_session )
401 {
402     int i_ret;
403
404     /* This announce has never been sent yet */
405     if( p_session->i_last == 0 )
406     {
407         p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
408         p_session->i_last = 1;
409         return VLC_SUCCESS;
410     }
411
412     if( p_session->i_next < mdate() )
413     {
414 #ifdef EXTRA_DEBUG
415         msg_Dbg( p_sap, "Sending announce");
416 #endif
417         i_ret = net_Write( p_sap, p_session->p_address->i_wfd,
418                            p_session->psz_data,
419                            p_session->i_length );
420         if( i_ret  != p_session->i_length )
421         {
422             msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
423                    p_session->p_address->psz_address,
424                    i_ret, p_session->i_length );
425         }
426         p_session->i_last = p_session->i_next;
427         p_session->i_next = p_session->i_last
428                             + p_session->p_address->i_interval*1000000;
429     }
430     else
431     {
432         return VLC_SUCCESS;
433     }
434     return VLC_SUCCESS;
435 }
436
437 static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
438 {
439     int64_t i_sdp_id = mdate();
440     int     i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
441
442     /* see the lists in modules/stream_out/rtp.c for compliance stuff */
443     p_session->psz_sdp = (char *)malloc(
444                             sizeof("v=0\r\n"
445                                    "o=- 45383436098 45398  IN IP4 127.0.0.1\r\n" /* FIXME */
446                                    "s=\r\n"
447                                    "t=0 0\r\n"
448                                    "c=IN IP4 /\r\n"
449                                    "m=video  udp\r\n"
450                                    "a=tool:"PACKAGE_STRING"\r\n"
451                                    "a=type:broadcast\r\n")
452                            + strlen( p_session->psz_name )
453                            + strlen( p_session->psz_uri ) + 300 );
454     if( !p_session->psz_sdp )
455     {
456         msg_Err( p_sap, "out of memory" );
457         return VLC_ENOMEM;
458     }
459     sprintf( p_session->psz_sdp,
460                             "v=0\r\n"
461                             "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n"
462                             "s=%s\r\n"
463                             "t=0 0\r\n"
464                             "c=IN IP4 %s/%d\r\n"
465                             "m=video %d udp %d\r\n"
466                             "a=tool:"PACKAGE_STRING"\r\n"
467                             "a=type:broadcast\r\n",
468                             i_sdp_id, i_sdp_version,
469                             p_session->psz_name,
470                             p_session->psz_uri, p_session->i_ttl,
471                             p_session->i_port, p_session->i_payload );
472     msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(p_session->psz_sdp),
473                     p_session->psz_sdp );
474     return VLC_SUCCESS;
475 }
476
477 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
478 {
479     int i_read;
480     char buffer[SAP_MAX_BUFFER];
481     int i_tot = 0;
482     mtime_t i_temp;
483     int i_rate;
484
485     if( p_address->t1 == 0 )
486     {
487         p_address->t1 = mdate();
488         return VLC_SUCCESS;
489     }
490     do
491     {
492         /* Might be too slow if we have huge data */
493         i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, buffer,
494                                    SAP_MAX_BUFFER, 0 );
495         i_tot += i_read;
496     } while( i_read > 0 && i_tot < SAP_MAX_BUFFER );
497
498     i_temp = mdate();
499
500     /* We calculate the rate every 5 seconds */
501     if( i_temp - p_address->t1 < 5000000 )
502     {
503         p_address->i_buff += i_tot;
504         return VLC_SUCCESS;
505     }
506
507     /* Bits/second */
508     i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
509                         (i_temp - p_address->t1 ));
510
511     p_address->i_limit = 10000;
512
513     p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
514                             (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
515
516     if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
517     {
518         p_address->i_interval = MAX_INTERVAL;
519     }
520 #ifdef EXTRA_DEBUG
521     msg_Dbg( p_sap,"%s:%i : Rate=%i, Interval = %i s",
522                     p_address->psz_address,p_address->i_port,
523                     i_rate,
524                     p_address->i_interval );
525 #endif
526
527     p_address->b_ready = VLC_TRUE;
528
529     p_address->t1 = i_temp;
530     p_address->i_buff = 0;
531
532     return VLC_SUCCESS;
533 }