1 /*****************************************************************************
2 * sap.c : SAP announce handler
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* free() */
28 #include <stdio.h> /* sprintf() */
29 #include <string.h> /* strerror() */
36 #define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */
39 #define SAP_IPV6_ADDR_1 "FF0"
40 #define SAP_IPV6_ADDR_2 "::2:7FFE"
42 #define DEFAULT_IPV6_SCOPE '8'
44 #define DEFAULT_PORT "1234"
48 /*****************************************************************************
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 );
55 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
56 sap_session_t *p_session );
59 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
60 session_descriptor_t *p_session,
61 announce_method_t *p_method );
63 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
64 session_descriptor_t *p_session );
66 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
70 * Create the SAP handler
72 * \param p_announce the parent announce_handler
73 * \return the newly created SAP handler or NULL on error
75 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
79 p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );
83 msg_Err( p_announce, "out of memory" );
87 vlc_mutex_init( p_sap, &p_sap->object_lock );
89 p_sap->pf_add = announce_SAPAnnounceAdd;
90 p_sap->pf_del = announce_SAPAnnounceDel;
92 p_sap->i_sessions = 0;
93 p_sap->i_addresses = 0;
94 p_sap->i_current_session = 0;
96 p_sap->b_control = config_GetInt( p_sap, "sap-flow-control");
98 if( vlc_thread_create( p_sap, "sap handler", RunThread,
99 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
101 msg_Dbg( p_announce, "Unable to spawn SAP handler thread");
105 msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions);
110 * Destroy the SAP handler
111 * \param p_this the SAP Handler to destroy
114 void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
118 vlc_mutex_destroy( &p_sap->object_lock );
120 /* Free the remaining sessions */
121 for( i = 0 ; i< p_sap->i_sessions ; i++)
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 );
130 /* Free the remaining addresses */
131 for( i = 0 ; i< p_sap->i_addresses ; i++)
133 sap_address_t *p_address = p_sap->pp_addresses[i];
134 FREE( p_address->psz_address );
135 if( p_address->i_rfd > -1 )
137 net_Close( p_address->i_rfd );
139 if( p_address->i_wfd > -1 && p_sap->b_control )
141 net_Close( p_address->i_wfd );
143 REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );
147 /* Free the structure */
148 vlc_object_destroy( p_sap );
152 * main SAP handler thread
153 * \param p_this the SAP Handler object
156 static void RunThread( vlc_object_t *p_this)
158 sap_handler_t *p_sap = (sap_handler_t*)p_this;
159 sap_session_t *p_session;
161 while( !p_sap->b_die )
165 /* If needed, get the rate info */
166 if( p_sap->b_control == VLC_TRUE )
168 for( i = 0 ; i< p_sap->i_addresses ; i++)
170 if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE )
172 CalculateRate( p_sap, p_sap->pp_addresses[i] );
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)
181 p_sap->i_current_session++;
183 else if( p_sap->i_sessions > 0)
185 p_sap->i_current_session = 0;
189 vlc_mutex_unlock( &p_sap->object_lock );
193 p_session = p_sap->pp_sessions[p_sap->i_current_session];
194 vlc_mutex_unlock( &p_sap->object_lock );
196 /* And announce it */
197 if( p_session->p_address->b_enabled == VLC_TRUE &&
198 p_session->p_address->b_ready == VLC_TRUE )
200 announce_SendSAPAnnounce( p_sap, p_session );
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 )
213 char *psz_type = "application/sdp";
216 vlc_bool_t b_found = VLC_FALSE;
217 sap_session_t *p_sap_session;
219 vlc_mutex_lock( &p_sap->object_lock );
221 /* If needed, build the SDP */
222 if( !p_session->psz_sdp )
224 if ( SDPGenerate( p_sap, p_session ) != VLC_SUCCESS )
226 vlc_mutex_unlock( &p_sap->object_lock );
231 if( !p_method->psz_address )
233 if( p_method->i_ip_version == 6 )
236 if( p_method->psz_ipv6_scope != NULL )
238 sz_scope = *p_method->psz_ipv6_scope;
242 sz_scope = DEFAULT_IPV6_SCOPE;
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 );
251 p_method->psz_address = (char*)malloc( 15*sizeof(char) );
252 sprintf(p_method->psz_address, SAP_IPV4_ADDR );
255 msg_Dbg( p_sap, "using SAP address: %s",p_method->psz_address);
257 /* XXX: Check for dupes */
258 p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
260 p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
261 p_sap_session->i_last = 0;
263 /* Add the address to the buffer */
264 for( i = 0; i< p_sap->i_addresses; i++)
266 if( !strcmp( p_method->psz_address,
267 p_sap->pp_addresses[i]->psz_address ) )
269 p_sap_session->p_address = p_sap->pp_addresses[i];
274 if( b_found == VLC_FALSE )
276 sap_address_t *p_address = (sap_address_t *)
277 malloc( sizeof(sap_address_t) );
280 msg_Err( p_sap, "out of memory" );
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,
289 if( p_sap->b_control == VLC_TRUE )
291 p_address->i_rfd = net_OpenUDP( p_sap, p_method->psz_address,
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 */
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");
307 if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
308 && p_sap->b_control ) )
310 msg_Warn( p_sap, "disabling address" );
311 p_address->b_enabled = VLC_FALSE;
314 INSERT_ELEM( p_sap->pp_addresses,
318 p_sap_session->p_address = p_address;
321 /* Build the SAP Headers */
322 i_header_size = 8 + strlen( psz_type ) + 1;
323 psz_head = (char *) malloc( i_header_size * sizeof( char ) );
326 msg_Err( p_sap, "out of memory" );
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 */
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 */
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);
344 p_sap_session->psz_data = (char *)malloc( sizeof(char)*
345 p_sap_session->i_length );
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) );
352 /* Enqueue the announce */
353 INSERT_ELEM( p_sap->pp_sessions,
357 msg_Dbg( p_sap,"Addresses: %i Sessions: %i",
358 p_sap->i_addresses,p_sap->i_sessions);
360 /* Remember the SAP session for later deletion */
361 p_session->p_sap = p_sap_session;
363 vlc_mutex_unlock( &p_sap->object_lock );
368 /* Remove a SAP Announce */
369 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
370 session_descriptor_t *p_session )
373 vlc_mutex_lock( &p_sap->object_lock );
375 msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap);
377 /* Dequeue the announce */
378 for( i = 0; i< p_sap->i_sessions; i++)
380 if( p_session->p_sap == p_sap->pp_sessions[i] )
382 REMOVE_ELEM( p_sap->pp_sessions,
389 /* XXX: Dequeue the adress too if it is not used anymore
390 * TODO: address refcount */
392 msg_Dbg( p_sap,"%i announces remaining", p_sap->i_sessions );
394 vlc_mutex_unlock( &p_sap->object_lock );
399 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
400 sap_session_t *p_session )
404 /* This announce has never been sent yet */
405 if( p_session->i_last == 0 )
407 p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
408 p_session->i_last = 1;
412 if( p_session->i_next < mdate() )
415 msg_Dbg( p_sap, "Sending announce");
417 i_ret = net_Write( p_sap, p_session->p_address->i_wfd,
419 p_session->i_length );
420 if( i_ret != p_session->i_length )
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 );
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;
437 static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
439 int64_t i_sdp_id = mdate();
440 int i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
442 /* see the lists in modules/stream_out/rtp.c for compliance stuff */
443 p_session->psz_sdp = (char *)malloc(
445 "o=- 45383436098 45398 IN IP4 127.0.0.1\r\n" /* FIXME */
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 )
456 msg_Err( p_sap, "out of memory" );
459 sprintf( p_session->psz_sdp,
461 "o=- "I64Fd" %d IN IP4 127.0.0.1\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,
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 );
477 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
480 char buffer[SAP_MAX_BUFFER];
485 if( p_address->t1 == 0 )
487 p_address->t1 = mdate();
492 /* Might be too slow if we have huge data */
493 i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, buffer,
496 } while( i_read > 0 && i_tot < SAP_MAX_BUFFER );
500 /* We calculate the rate every 5 seconds */
501 if( i_temp - p_address->t1 < 5000000 )
503 p_address->i_buff += i_tot;
508 i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
509 (i_temp - p_address->t1 ));
511 p_address->i_limit = 10000;
513 p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
514 (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
516 if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
518 p_address->i_interval = MAX_INTERVAL;
521 msg_Dbg( p_sap,"%s:%i : Rate=%i, Interval = %i s",
522 p_address->psz_address,p_address->i_port,
524 p_address->i_interval );
527 p_address->b_ready = VLC_TRUE;
529 p_address->t1 = i_temp;
530 p_address->i_buff = 0;