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;
220 vlc_mutex_lock( &p_sap->object_lock );
222 /* If needed, build the SDP */
223 if( !p_session->psz_sdp )
225 if ( SDPGenerate( p_sap, p_session ) != VLC_SUCCESS )
227 vlc_mutex_unlock( &p_sap->object_lock );
232 if( !p_method->psz_address )
234 if( p_method->i_ip_version == 6 )
237 if( p_method->psz_ipv6_scope != NULL )
239 sz_scope = *p_method->psz_ipv6_scope;
243 sz_scope = DEFAULT_IPV6_SCOPE;
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 );
252 p_method->psz_address = (char*)malloc( 15*sizeof(char) );
253 snprintf(p_method->psz_address, 15, SAP_IPV4_ADDR );
256 msg_Dbg( p_sap, "using SAP address: %s",p_method->psz_address);
258 /* XXX: Check for dupes */
259 p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
261 p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
262 p_sap_session->i_last = 0;
264 /* Add the address to the buffer */
265 for( i = 0; i< p_sap->i_addresses; i++)
267 if( !strcmp( p_method->psz_address,
268 p_sap->pp_addresses[i]->psz_address ) )
270 p_sap_session->p_address = p_sap->pp_addresses[i];
275 if( b_found == VLC_FALSE )
277 sap_address_t *p_address = (sap_address_t *)
278 malloc( sizeof(sap_address_t) );
281 msg_Err( p_sap, "out of memory" );
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,
291 if( p_sap->b_control == VLC_TRUE )
293 p_address->i_rfd = net_OpenUDP( p_sap, p_method->psz_address,
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 */
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");
309 if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
310 && p_sap->b_control ) )
312 msg_Warn( p_sap, "disabling address" );
313 p_address->b_enabled = VLC_FALSE;
316 INSERT_ELEM( p_sap->pp_addresses,
320 p_sap_session->p_address = p_address;
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 ) );
328 msg_Err( p_sap, "out of memory" );
332 psz_head[0] = 0x20; /* Means SAPv1, IPv4, not encrypted, not compressed */
333 psz_head[1] = 0x00; /* No authentification length */
336 psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */
337 psz_head[3] = (i_hash & 0xFF); /* Msg id hash 2 */
339 if( p_method->i_ip_version == 6 )
341 /* in_addr_t ip_server = inet_addr( ip ); */
342 psz_head[0] |= 0x10; /* Set IPv6 */
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 */
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 */
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 */
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 */
364 strncpy( psz_head + 20, psz_type, 15 );
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 */
375 strncpy( psz_head + 8, psz_type, 15 );
378 psz_head[ i_header_size-1 ] = '\0';
379 p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);
381 p_sap_session->psz_data = (char *)malloc( sizeof(char)*
382 p_sap_session->i_length );
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) );
391 /* Enqueue the announce */
392 INSERT_ELEM( p_sap->pp_sessions,
396 msg_Dbg( p_sap,"Addresses: %i Sessions: %i",
397 p_sap->i_addresses,p_sap->i_sessions);
399 /* Remember the SAP session for later deletion */
400 p_session->p_sap = p_sap_session;
402 vlc_mutex_unlock( &p_sap->object_lock );
407 /* Remove a SAP Announce */
408 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
409 session_descriptor_t *p_session )
412 vlc_mutex_lock( &p_sap->object_lock );
414 msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap);
416 /* Dequeue the announce */
417 for( i = 0; i< p_sap->i_sessions; i++)
419 if( p_session->p_sap == p_sap->pp_sessions[i] )
421 REMOVE_ELEM( p_sap->pp_sessions,
428 /* XXX: Dequeue the address too if it is not used anymore
429 * TODO: - address refcount
430 - send a SAP deletion packet */
432 msg_Dbg( p_sap,"%i announces remaining", p_sap->i_sessions );
434 vlc_mutex_unlock( &p_sap->object_lock );
439 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
440 sap_session_t *p_session )
444 /* This announce has never been sent yet */
445 if( p_session->i_last == 0 )
447 p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
448 p_session->i_last = 1;
452 if( p_session->i_next < mdate() )
455 msg_Dbg( p_sap, "Sending announce");
457 i_ret = net_Write( p_sap, p_session->p_address->i_wfd,
459 p_session->i_length );
460 if( i_ret != p_session->i_length )
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 );
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;
477 static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
479 int64_t i_sdp_id = mdate();
480 int i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
482 /* see the lists in modules/stream_out/rtp.c for compliance stuff */
483 p_session->psz_sdp = (char *)malloc(
485 "o=- 45383436098 45398 IN IP4 127.0.0.1\r\n" /* FIXME */
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 )
496 msg_Err( p_sap, "out of memory" );
499 sprintf( p_session->psz_sdp,
501 "o=- "I64Fd" %d IN IP4 127.0.0.1\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,
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 );
517 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
520 char buffer[SAP_MAX_BUFFER];
525 if( p_address->t1 == 0 )
527 p_address->t1 = mdate();
532 /* Might be too slow if we have huge data */
533 i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, buffer,
536 } while( i_read > 0 && i_tot < SAP_MAX_BUFFER );
540 /* We calculate the rate every 5 seconds */
541 if( i_temp - p_address->t1 < 5000000 )
543 p_address->i_buff += i_tot;
548 i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
549 (i_temp - p_address->t1 ));
551 p_address->i_limit = 10000;
553 p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
554 (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
556 if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
558 p_address->i_interval = MAX_INTERVAL;
561 msg_Dbg( p_sap,"%s:%i : Rate=%i, Interval = %i s",
562 p_address->psz_address,p_address->i_port,
564 p_address->i_interval );
567 p_address->b_ready = VLC_TRUE;
569 p_address->t1 = i_temp;
570 p_address->i_buff = 0;