1 /*****************************************************************************
2 * sap.c : SAP interface module
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
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> /* malloc(), free() */
30 #include <vlc/input.h>
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
50 /************************************************************************
51 * Macros and definitions
52 ************************************************************************/
54 #define MAX_LINE_LENGTH 256
56 /* SAP is always on that port */
58 /* Global-scope SAP address */
59 #define SAP_V4_GLOBAL_ADDRESS "224.2.127.254"
60 /* Organization-local SAP address */
61 #define SAP_V4_ORG_ADDRESS "239.195.255.255"
62 /* Local (smallest non-link-local scope) SAP address */
63 #define SAP_V4_LOCAL_ADDRESS "239.255.255.255"
64 /* Link-local SAP address */
65 #define SAP_V4_LINK_ADDRESS "224.0.0.255"
68 #define SAP_V6_1 "FF0"
69 /* Scope is inserted between them */
70 #define SAP_V6_2 "::2:7FFE"
71 /* See RFC3513 for list of valid scopes */
72 /* FIXME: find a way to listen to link-local scope */
73 static const char ipv6_scopes[] = "1456789ABCDE";
76 /*****************************************************************************
78 *****************************************************************************/
79 #define SAP_ADDR_TEXT N_( "SAP multicast address" )
80 #define SAP_ADDR_LONGTEXT N_( "Listen for SAP announcements on another address" )
81 #define SAP_IPV4_TEXT N_( "IPv4-SAP listening" )
82 #define SAP_IPV4_LONGTEXT N_( \
83 "Set this if you want the SAP module to listen to IPv4 announcements " \
84 "on the standard address." )
85 #define SAP_IPV6_TEXT N_( "IPv6-SAP listening" )
86 #define SAP_IPV6_LONGTEXT N_( \
87 "Set this if you want the SAP module to listen to IPv6 announcements " \
88 "on the standard address." )
89 #define SAP_SCOPE_TEXT N_( "IPv6 SAP scope" )
90 #define SAP_SCOPE_LONGTEXT N_( \
91 "Sets the scope for IPv6 announcements (default is 8)." )
92 #define SAP_TIMEOUT_TEXT N_( "SAP timeout (seconds)" )
93 #define SAP_TIMEOUT_LONGTEXT N_( \
94 "Sets the time before SAP items get deleted if no new announcement " \
96 #define SAP_PARSE_TEXT N_( "Try to parse the SAP" )
97 #define SAP_PARSE_LONGTEXT N_( \
98 "When SAP can it will try to parse the SAP. If you don't select " \
99 "this, all announcements will be parsed by the livedotcom module." )
100 #define SAP_STRICT_TEXT N_( "SAP Strict mode" )
101 #define SAP_STRICT_LONGTEXT N_( \
102 "When this is set, the SAP parser will discard some non-compliant " \
104 #define SAP_CACHE_TEXT N_("Use SAP cache")
105 #define SAP_CACHE_LONGTEXT N_( \
106 "If this option is selected, a SAP caching mechanism will be used. " \
107 "This will result in lower SAP startup time, but you could end up " \
108 "with items corresponding to legacy streams." )
109 #define SAP_TIMESHIFT_TEXT N_("Allow timeshifting")
110 #define SAP_TIMESHIFT_LONGTEXT N_( \
111 "Enable timeshifting automatically for streams " \
112 "discovered through SAP announcements." )
115 static int Open ( vlc_object_t * );
116 static void Close( vlc_object_t * );
117 static int OpenDemux ( vlc_object_t * );
118 static void CloseDemux ( vlc_object_t * );
121 set_shortname( _("SAP"));
122 set_description( _("SAP Announcements") );
123 set_category( CAT_PLAYLIST );
124 set_subcategory( SUBCAT_PLAYLIST_SD );
126 add_string( "sap-addr", NULL, NULL,
127 SAP_ADDR_TEXT, SAP_ADDR_LONGTEXT, VLC_TRUE );
128 add_bool( "sap-ipv4", 1 , NULL,
129 SAP_IPV4_TEXT,SAP_IPV4_LONGTEXT, VLC_TRUE );
130 add_bool( "sap-ipv6", 1 , NULL,
131 SAP_IPV6_TEXT, SAP_IPV6_LONGTEXT, VLC_TRUE );
132 add_integer( "sap-timeout", 1800, NULL,
133 SAP_TIMEOUT_TEXT, SAP_TIMEOUT_LONGTEXT, VLC_TRUE );
134 add_bool( "sap-parse", 1 , NULL,
135 SAP_PARSE_TEXT,SAP_PARSE_LONGTEXT, VLC_TRUE );
136 add_bool( "sap-strict", 0 , NULL,
137 SAP_STRICT_TEXT,SAP_STRICT_LONGTEXT, VLC_TRUE );
138 add_bool( "sap-cache", 0 , NULL,
139 SAP_CACHE_TEXT,SAP_CACHE_LONGTEXT, VLC_TRUE );
140 add_bool( "sap-timeshift", 0 , NULL,
141 SAP_TIMESHIFT_TEXT,SAP_TIMESHIFT_LONGTEXT, VLC_TRUE );
143 set_capability( "services_discovery", 0 );
144 set_callbacks( Open, Close );
147 set_description( _("SDP file parser for UDP") );
148 add_shortcut( "sdp" );
149 set_capability( "demux2", 51 );
150 set_callbacks( OpenDemux, CloseDemux );
154 /*****************************************************************************
156 *****************************************************************************/
158 typedef struct sdp_t sdp_t;
159 typedef struct attribute_t attribute_t;
160 typedef struct sap_announce_t sap_announce_t;
162 /* The structure that contains sdp information */
168 char *psz_sessionname;
170 /* Raw m= and c= fields */
171 char *psz_connection;
176 char *psz_network_type;
177 char *psz_address_type;
179 int64_t i_session_id;
184 int i_in; /* IP version */
190 attribute_t **pp_attributes;
199 struct sap_announce_t
204 uint32_t i_source[4];
206 /* SAP annnounces must only contain one SDP */
210 // playlist_item_t *p_item;
213 struct services_discovery_sys_t
215 /* Socket descriptors */
220 playlist_item_t *p_node;
221 playlist_t *p_playlist;
223 /* Table of announces */
225 struct sap_announce_t **pp_announces;
230 vlc_bool_t b_timeshift;
240 /*****************************************************************************
242 *****************************************************************************/
246 static int Demux( demux_t *p_demux );
247 static int Control( demux_t *, int, va_list );
248 static void Run ( services_discovery_t *p_sd );
250 /* Main parsing functions */
251 static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp );
252 static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read );
253 static sdp_t * ParseSDP( vlc_object_t *p_sd, char* psz_sdp );
254 static sap_announce_t *CreateAnnounce( services_discovery_t *, uint16_t, sdp_t * );
255 static int RemoveAnnounce( services_discovery_t *p_sd, sap_announce_t *p_announce );
258 static void CacheLoad( services_discovery_t *p_sd );
259 static void CacheSave( services_discovery_t *p_sd );
260 /* Helper functions */
261 static char *GetAttribute( sdp_t *p_sdp, const char *psz_search );
262 static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 );
263 static int InitSocket( services_discovery_t *p_sd, char *psz_address, int i_port );
265 static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len );
267 static void FreeSDP( sdp_t *p_sdp );
271 if( p ) { free( p ); (p) = NULL; }
272 /*****************************************************************************
273 * Open: initialize and create stuff
274 *****************************************************************************/
275 static int Open( vlc_object_t *p_this )
277 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
278 services_discovery_sys_t *p_sys = (services_discovery_sys_t *)
279 malloc( sizeof( services_discovery_sys_t ) );
281 playlist_view_t *p_view;
285 p_sys->i_timeout = var_CreateGetInteger( p_sd, "sap-timeout" );
293 p_sys->b_strict = var_CreateGetInteger( p_sd, "sap-strict");
294 p_sys->b_parse = var_CreateGetInteger( p_sd, "sap-parse" );
296 if( var_CreateGetInteger( p_sd, "sap-cache" ) )
301 if( var_CreateGetInteger( p_sd, "sap-ipv4" ) )
303 InitSocket( p_sd, SAP_V4_GLOBAL_ADDRESS, SAP_PORT );
304 InitSocket( p_sd, SAP_V4_ORG_ADDRESS, SAP_PORT );
305 InitSocket( p_sd, SAP_V4_LOCAL_ADDRESS, SAP_PORT );
306 InitSocket( p_sd, SAP_V4_LINK_ADDRESS, SAP_PORT );
308 if( var_CreateGetInteger( p_sd, "sap-ipv6" ) )
310 char psz_address[] = SAP_V6_1"0"SAP_V6_2;
313 for( c_scope = ipv6_scopes; *c_scope; c_scope++ )
315 psz_address[sizeof(SAP_V6_1) - 1] = *c_scope;
316 InitSocket( p_sd, psz_address, SAP_PORT );
320 psz_addr = var_CreateGetString( p_sd, "sap-addr" );
321 if( psz_addr && *psz_addr )
323 InitSocket( p_sd, psz_addr, SAP_PORT );
326 if( p_sys->i_fd == 0 )
328 msg_Err( p_sd, "unable to read on any address" );
332 /* Cache sap_timeshift value */
333 p_sys->b_timeshift = var_CreateGetInteger( p_sd, "sap-timeshift" )
334 ? VLC_TRUE : VLC_FALSE;
336 /* Create our playlist node */
337 p_sys->p_playlist = (playlist_t *)vlc_object_find( p_sd,
340 if( !p_sys->p_playlist )
342 msg_Warn( p_sd, "unable to find playlist, cancelling SAP listening");
346 p_view = playlist_ViewFind( p_sys->p_playlist, VIEW_CATEGORY );
347 p_sys->p_node = playlist_NodeCreate( p_sys->p_playlist, VIEW_CATEGORY,
348 _("Session Announcements (SAP)"), p_view->p_root );
349 p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
350 p_sys->p_node->i_flags &= ~PLAYLIST_SKIP_FLAG;
351 val.b_bool = VLC_TRUE;
352 var_Set( p_sys->p_playlist, "intf-change", val );
354 p_sys->i_announces = 0;
355 p_sys->pp_announces = NULL;
360 /*****************************************************************************
361 * OpenDemux: initialize and create stuff
362 *****************************************************************************/
363 static int OpenDemux( vlc_object_t *p_this )
365 demux_t *p_demux = (demux_t *)p_this;
367 int i_max_sdp = 1024;
369 char *psz_sdp = NULL;
372 if( !var_CreateGetInteger( p_demux, "sap-parse" ) )
374 /* We want livedotcom module to parse this SDP file */
381 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
383 if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) &&
384 strncmp( (char*)p_peek, "v=0\n", 4 ) &&
385 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
391 psz_sdp = (char *)malloc( i_max_sdp );
392 if( !psz_sdp ) return VLC_EGENERIC;
394 /* Gather the complete sdp file */
397 int i_read = stream_Read( p_demux->s,
398 &psz_sdp[i_sdp], i_max_sdp - i_sdp - 1 );
402 msg_Err( p_demux, "failed to read SDP" );
408 if( i_read < i_max_sdp - i_sdp - 1 )
410 psz_sdp[i_sdp] = '\0';
415 psz_sdp = (char *)realloc( psz_sdp, i_max_sdp );
418 p_sdp = ParseSDP( VLC_OBJECT(p_demux), psz_sdp );
422 msg_Warn( p_demux, "invalid SDP");
426 if( p_sdp->i_media > 1 )
431 if( ParseConnection( VLC_OBJECT( p_demux ), p_sdp ) )
433 p_sdp->psz_uri = NULL;
435 if( p_sdp->i_media_type != 33 && p_sdp->i_media_type != 32 &&
436 p_sdp->i_media_type != 14 )
439 if( p_sdp->psz_uri == NULL ) goto error;
441 p_demux->p_sys = (demux_sys_t *)malloc( sizeof(demux_sys_t) );
442 p_demux->p_sys->p_sdp = p_sdp;
443 p_demux->pf_control = Control;
444 p_demux->pf_demux = Demux;
451 if( p_sdp ) FreeSDP( p_sdp );
452 stream_Seek( p_demux->s, 0 );
456 /*****************************************************************************
458 *****************************************************************************/
459 static void Close( vlc_object_t *p_this )
461 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
462 services_discovery_sys_t *p_sys = p_sd->p_sys;
466 for( i = p_sys->i_fd-1 ; i >= 0 ; i-- )
468 net_Close( p_sys->pi_fd[i] );
470 FREE( p_sys->pi_fd );
472 if( config_GetInt( p_sd, "sap-cache" ) )
477 for( i = p_sys->i_announces - 1; i>= 0; i-- )
479 RemoveAnnounce( p_sd, p_sys->pp_announces[i] );
481 FREE( p_sys->pp_announces );
483 if( p_sys->p_playlist )
485 playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node, VLC_TRUE,
487 vlc_object_release( p_sys->p_playlist );
493 /*****************************************************************************
494 * CloseDemux: Close the demuxer
495 *****************************************************************************/
496 static void CloseDemux( vlc_object_t *p_this )
501 /*****************************************************************************
502 * Run: main SAP thread
503 *****************************************************************************
504 * Listens to SAP packets, and sends them to packet_handle
505 *****************************************************************************/
506 #define MAX_SAP_BUFFER 5000
508 static void Run( services_discovery_t *p_sd )
512 /* read SAP packets */
513 while( !p_sd->b_die )
516 uint8_t p_buffer[MAX_SAP_BUFFER+1];
518 i_read = net_Select( p_sd, p_sd->p_sys->pi_fd, NULL,
519 p_sd->p_sys->i_fd, p_buffer,
520 MAX_SAP_BUFFER, 500000 );
522 /* Check for items that need deletion */
523 for( i = 0; i < p_sd->p_sys->i_announces; i++ )
525 mtime_t i_timeout = ( mtime_t ) 1000000 * p_sd->p_sys->i_timeout;
527 if( mdate() - p_sd->p_sys->pp_announces[i]->i_last > i_timeout )
529 struct sap_announce_t *p_announce;
530 p_announce = p_sd->p_sys->pp_announces[i];
532 /* Remove the playlist item */
533 playlist_LockDelete( p_sd->p_sys->p_playlist,
534 p_announce->i_item_id );
536 /* Remove the sap_announce from the array */
537 REMOVE_ELEM( p_sd->p_sys->pp_announces,
538 p_sd->p_sys->i_announces, i );
544 /* Minimum length is > 6 */
549 msg_Warn( p_sd, "socket read error" );
554 p_buffer[i_read] = '\0';
556 /* Parse the packet */
557 ParseSAP( p_sd, p_buffer, i_read );
561 /**********************************************************************
562 * Demux: reads and demuxes data packets
563 * Return -1 if error, 0 if EOF, 1 else
564 **********************************************************************/
565 static int Demux( demux_t *p_demux )
567 sdp_t *p_sdp = p_demux->p_sys->p_sdp;
568 playlist_t *p_playlist;
570 p_playlist = (playlist_t *)vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
573 p_playlist->status.p_item->i_flags |= PLAYLIST_DEL_FLAG;
575 playlist_Add( p_playlist, p_sdp->psz_uri, p_sdp->psz_sessionname,
576 PLAYLIST_APPEND, PLAYLIST_END );
578 vlc_object_release( p_playlist );
579 if( p_sdp ) FreeSDP( p_sdp );
584 static int Control( demux_t *p_demux, int i_query, va_list args )
589 /**************************************************************
591 **************************************************************/
593 /* i_read is at least > 6 */
594 static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
596 int i_version, i_address_type, i_hash, i;
597 char *psz_sdp, *psz_foo, *psz_initial_sdp;
599 vlc_bool_t b_compressed;
600 vlc_bool_t b_need_delete = VLC_FALSE;
602 /* First, check the sap announce is correct */
603 i_version = p_buffer[0] >> 5;
606 msg_Dbg( p_sd, "strange sap version %d found", i_version );
609 i_address_type = p_buffer[0] & 0x10;
611 if( (p_buffer[0] & 0x08) != 0 )
613 msg_Dbg( p_sd, "reserved bit incorrectly set" );
617 if( (p_buffer[0] & 0x04) != 0 )
619 msg_Dbg( p_sd, "session deletion packet" );
620 b_need_delete = VLC_TRUE;
623 if( p_buffer[0] & 0x02 )
625 msg_Dbg( p_sd, "encrypted packet, unsupported" );
629 b_compressed = p_buffer[0] & 0x01;
631 i_hash = ( p_buffer[2] << 8 ) + p_buffer[3];
633 if( p_sd->p_sys->b_strict && i_hash == 0 )
635 msg_Dbg( p_sd, "strict mode, discarding announce with null id hash");
639 psz_sdp = (char *)p_buffer + 4;
640 psz_initial_sdp = psz_sdp;
642 if( i_address_type == 0 ) /* ipv4 source address */
647 msg_Warn( p_sd, "too short SAP packet\n" );
651 else /* ipv6 source address */
656 msg_Warn( p_sd, "too short SAP packet\n" );
664 uint8_t *p_decompressed_buffer = NULL;
665 int i_decompressed_size;
667 i_decompressed_size = Decompress( (uint8_t *)psz_sdp,
668 &p_decompressed_buffer, i_read - ( psz_sdp - (char *)p_buffer ) );
669 if( i_decompressed_size > 0 && i_decompressed_size < MAX_SAP_BUFFER )
671 memcpy( psz_sdp, p_decompressed_buffer, i_decompressed_size );
672 psz_sdp[i_decompressed_size] = '\0';
673 free( p_decompressed_buffer );
676 msg_Warn( p_sd, "Ignoring compressed sap packet" );
681 /* Add the size of authentification info */
682 if( i_read < p_buffer[1] + (psz_sdp - psz_initial_sdp ) )
684 msg_Warn( p_sd, "too short SAP packet\n");
687 psz_sdp += p_buffer[1];
690 /* Skip payload type */
691 /* Handle announces without \0 between SAP and SDP */
692 while( *psz_sdp != '\0' && ( psz_sdp[0] != 'v' && psz_sdp[1] != '=' ) )
694 if( psz_sdp - psz_initial_sdp >= i_read - 5 )
696 msg_Warn( p_sd, "empty SDP ?");
701 if( *psz_sdp == '\0' )
705 if( ( psz_sdp != psz_foo ) && strcasecmp( psz_foo, "application/sdp" ) )
707 msg_Dbg( p_sd, "unhandled content type: %s", psz_foo );
709 if( ( psz_sdp - (char *)p_buffer ) >= i_read )
711 msg_Warn( p_sd, "package without content" );
716 p_sdp = ParseSDP( VLC_OBJECT(p_sd), psz_sdp );
723 /* Decide whether we should add a playlist item for this SDP */
724 /* Parse connection information (c= & m= ) */
725 if( ParseConnection( VLC_OBJECT(p_sd), p_sdp ) )
727 p_sdp->psz_uri = NULL;
730 /* Multi-media or no-parse -> pass to LIVE.COM */
731 if( p_sdp->i_media > 1 || ( p_sdp->i_media_type != 14 &&
732 p_sdp->i_media_type != 32 &&
733 p_sdp->i_media_type != 33) ||
734 p_sd->p_sys->b_parse == VLC_FALSE )
736 if( p_sdp->psz_uri ) free( p_sdp->psz_uri );
737 asprintf( &p_sdp->psz_uri, "sdp://%s", p_sdp->psz_sdp );
740 if( p_sdp->psz_uri == NULL ) return VLC_EGENERIC;
742 for( i = 0 ; i< p_sd->p_sys->i_announces ; i++ )
745 /* FIXME: we create a new announce each time the sdp changes */
746 if( IsSameSession( p_sd->p_sys->pp_announces[i]->p_sdp, p_sdp ) )
750 RemoveAnnounce( p_sd, p_sd->p_sys->pp_announces[i]);
755 p_sd->p_sys->pp_announces[i]->i_last = mdate();
762 if( p_sdp->i_media > 1 )
764 msg_Dbg( p_sd, "passing to LIVE.COM" );
767 CreateAnnounce( p_sd, i_hash, p_sdp );
772 sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
775 playlist_item_t *p_item, *p_child;
777 sap_announce_t *p_sap = (sap_announce_t *)malloc(
778 sizeof(sap_announce_t ) );
779 services_discovery_sys_t *p_sys;
785 EnsureUTF8( p_sdp->psz_sessionname );
786 p_sap->i_last = mdate();
787 p_sap->i_hash = i_hash;
788 p_sap->p_sdp = p_sdp;
789 p_sap->i_item_id = -1;
791 /* Create the actual playlist item here */
792 p_item = playlist_ItemNew( p_sd, p_sap->p_sdp->psz_uri, p_sdp->psz_sessionname );
800 if( p_sys->b_timeshift )
801 playlist_ItemAddOption( p_item, ":access-filter=timeshift" );
803 psz_value = GetAttribute( p_sap->p_sdp, "tool" );
804 if( psz_value != NULL )
806 vlc_input_item_AddInfo( &p_item->input, _("Session"),
807 _("Tool"), psz_value );
809 if( strcmp( p_sdp->psz_username, "-" ) )
811 vlc_input_item_AddInfo( &p_item->input, _("Session"),
812 _("User"), p_sdp->psz_username );
815 psz_value = GetAttribute( p_sap->p_sdp, "x-plgroup" );
817 if( psz_value == NULL )
819 psz_value = GetAttribute( p_sap->p_sdp, "plgroup" );
822 /* Find or Create the group playlist non-playable item */
823 if( psz_value != NULL )
825 EnsureUTF8( psz_value );
827 p_child = playlist_ChildSearchName( p_sys->p_node, psz_value );
829 if( p_child == NULL )
831 p_child = playlist_NodeCreate( p_sys->p_playlist,
832 VIEW_CATEGORY, psz_value,
834 p_child->i_flags &= ~PLAYLIST_SKIP_FLAG;
839 p_child = p_sys->p_node;
842 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
843 p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
845 playlist_NodeAddItem( p_sys->p_playlist, p_item, VIEW_CATEGORY, p_child,
846 PLAYLIST_APPEND, PLAYLIST_END );
848 p_sap->i_item_id = p_item->input.i_id;
850 TAB_APPEND( p_sys->i_announces, p_sys->pp_announces, p_sap );
855 static char *GetAttribute( sdp_t *p_sdp, const char *psz_search )
859 for( i = 0 ; i< p_sdp->i_attributes; i++ )
861 if( !strncmp( p_sdp->pp_attributes[i]->psz_field, psz_search,
862 strlen( p_sdp->pp_attributes[i]->psz_field ) ) )
864 return p_sdp->pp_attributes[i]->psz_value;
871 /* Fill p_sdp->psz_uri */
872 static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
876 char *psz_uri = NULL;
877 char *psz_proto = NULL;
881 if( p_sdp->psz_connection )
883 psz_parse = p_sdp->psz_connection;
885 psz_eof = strchr( psz_parse, ' ' );
890 psz_parse = psz_eof + 1;
894 msg_Warn( p_obj, "unable to parse c field (1)");
898 psz_eof = strchr( psz_parse, ' ' );
903 if( !strncmp( psz_parse, "IP4", 3 ) )
907 else if( !strncmp( psz_parse, "IP6", 3 ) )
915 psz_parse = psz_eof + 1;
919 msg_Warn( p_obj, "unable to parse c field (2)");
923 psz_eof = strchr( psz_parse, '/' );
931 msg_Dbg( p_obj, "incorrect c field, %s", p_sdp->psz_connection );
933 if( p_sdp->i_in == 6 && ( isxdigit( *psz_parse ) || *psz_parse == ':' ) )
935 asprintf( &psz_uri, "[%s]", psz_parse );
937 else psz_uri = strdup( psz_parse );
942 if( p_sdp->psz_media )
944 psz_parse = p_sdp->psz_media;
946 psz_eof = strchr( psz_parse, ' ' );
952 if( strncmp( psz_parse, "audio", 5 ) &&
953 strncmp( psz_parse, "video", 5 ) )
955 msg_Warn( p_obj, "unhandled media type -%s-", psz_parse );
960 psz_parse = psz_eof + 1;
964 msg_Warn( p_obj, "unable to parse m field (1)");
969 psz_eof = strchr( psz_parse, ' ' );
975 /* FIXME : multiple port ! */
976 i_port = atoi( psz_parse );
978 if( i_port <= 0 || i_port >= 65536 )
980 msg_Warn( p_obj, "invalid transport port %i", i_port );
983 psz_parse = psz_eof + 1;
987 msg_Warn( p_obj, "unable to parse m field (2)");
992 psz_eof = strchr( psz_parse, ' ' );
997 psz_proto = strdup( psz_parse );
999 psz_parse = psz_eof + 1;
1000 p_sdp->i_media_type = atoi( psz_parse );
1005 msg_Dbg( p_obj, "incorrect m field, %s", p_sdp->psz_media );
1006 p_sdp->i_media_type = 33;
1007 psz_proto = strdup( psz_parse );
1011 if( psz_proto && !strncmp( psz_proto, "RTP/AVP", 7 ) )
1014 psz_proto = strdup( "rtp" );
1016 if( psz_proto && !strncasecmp( psz_proto, "UDP", 3 ) )
1019 psz_proto = strdup( "udp" );
1022 /* FIXME: HTTP support */
1029 asprintf( &p_sdp->psz_uri, "%s://@%s:%i", psz_proto, psz_uri, i_port );
1036 /***********************************************************************
1037 * ParseSDP : SDP parsing
1038 * *********************************************************************
1039 * Validate SDP and parse all fields
1040 ***********************************************************************/
1041 static sdp_t * ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
1044 vlc_bool_t b_invalid = VLC_FALSE;
1045 vlc_bool_t b_end = VLC_FALSE;
1046 if( psz_sdp == NULL )
1051 if( psz_sdp[0] != 'v' || psz_sdp[1] != '=' )
1053 msg_Warn( p_obj, "Bad packet" );
1057 p_sdp = (sdp_t *)malloc( sizeof( sdp_t ) );
1061 p_sdp->psz_sdp = strdup( psz_sdp );
1062 if( p_sdp->psz_sdp == NULL )
1068 p_sdp->psz_sessionname = NULL;
1069 p_sdp->psz_media = NULL;
1070 p_sdp->psz_connection = NULL;
1071 p_sdp->psz_uri = NULL;
1072 p_sdp->psz_address = NULL;
1073 p_sdp->psz_address_type= NULL;
1076 p_sdp->i_attributes = 0;
1077 p_sdp->pp_attributes = NULL;
1079 while( *psz_sdp != '\0' && b_end == VLC_FALSE )
1086 while( *psz_sdp == '\r' || *psz_sdp == '\n' ||
1087 *psz_sdp == ' ' || *psz_sdp == '\t' )
1092 if( ( psz_eol = strchr( psz_sdp, '\n' ) ) == NULL )
1094 psz_eol = psz_sdp + strlen( psz_sdp );
1097 if( psz_eol > psz_sdp && *( psz_eol - 1 ) == '\r' )
1102 if( psz_eol <= psz_sdp )
1108 /* no space allowed between fields */
1109 if( psz_sdp[1] != '=' )
1111 msg_Warn( p_obj, "invalid packet" ) ;
1116 /* Now parse each line */
1117 switch( psz_sdp[0] )
1122 p_sdp->psz_sessionname = strdup( &psz_sdp[2] );
1127 /* o field is <username> <session id> <version>
1128 * <network type> <address type> <address> */
1130 #define GET_FIELD( store ) \
1131 psz_eof = strchr( psz_parse, ' ' ); \
1134 *psz_eof=0; store = strdup( psz_parse ); \
1138 if( i_field != 5 ) \
1140 b_invalid = VLC_TRUE; break; \
1144 store = strdup( psz_parse ); \
1147 psz_parse = psz_eof + 1; i_field++;
1150 psz_parse = &psz_sdp[2];
1151 GET_FIELD( p_sdp->psz_username );
1152 GET_FIELD( psz_sess_id );
1154 p_sdp->i_session_id = atoll( psz_sess_id );
1156 FREE( psz_sess_id );
1158 GET_FIELD( psz_sess_id );
1159 FREE( psz_sess_id );
1161 GET_FIELD( p_sdp->psz_network_type );
1162 GET_FIELD( p_sdp->psz_address_type );
1163 GET_FIELD( p_sdp->psz_address );
1174 case( 'a' ): /* attribute */
1176 char *psz_eon = strchr( &psz_sdp[2], ':' );
1177 attribute_t *p_attr = malloc( sizeof( attribute_t ) );
1179 /* Attribute with value */
1184 p_attr->psz_field = strdup( &psz_sdp[2] );
1185 p_attr->psz_value = strdup( psz_eon );
1187 else /* Attribute without value */
1189 p_attr->psz_field = strdup( &psz_sdp[2] );
1190 p_attr->psz_value = NULL;
1193 TAB_APPEND( p_sdp->i_attributes, p_sdp->pp_attributes, p_attr );
1197 case( 'm' ): /* Media announcement */
1199 /* If we have several medias, we pass the announcement to
1200 * LIVE.COM, so just count them */
1202 if( p_sdp->i_media == 1 )
1204 p_sdp->psz_media = strdup( &psz_sdp[2] );
1211 if( p_sdp->i_media > 1 )
1214 p_sdp->psz_connection = strdup( &psz_sdp[2] );
1234 static int InitSocket( services_discovery_t *p_sd, char *psz_address,
1237 int i_fd = net_OpenUDP( p_sd, psz_address, i_port, "", 0 );
1241 net_StopSend( i_fd );
1242 INSERT_ELEM( p_sd->p_sys->pi_fd, p_sd->p_sys->i_fd,
1243 p_sd->p_sys->i_fd, i_fd );
1247 return VLC_EGENERIC;
1251 static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len )
1253 int i_result, i_dstsize, n;
1254 unsigned char *psz_dst;
1257 d_stream.zalloc = (alloc_func)0;
1258 d_stream.zfree = (free_func)0;
1259 d_stream.opaque = (voidpf)0;
1261 i_result = inflateInit(&d_stream);
1262 if( i_result != Z_OK )
1264 printf( "inflateInit() failed. Result: %d\n", i_result );
1268 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
1269 i_position = p_playlist->i_index;
1271 /* Gather the complete sdp file */
1274 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp], i_sdp_max - i_sdp - 1 );
1276 d_stream.next_in = (Bytef *)psz_src;
1277 d_stream.avail_in = i_len;
1285 psz_dst = (unsigned char *)realloc( psz_dst, n * 1000 );
1286 d_stream.next_out = (Bytef *)&psz_dst[(n - 1) * 1000];
1287 d_stream.avail_out = 1000;
1289 i_result = inflate(&d_stream, Z_NO_FLUSH);
1290 if( ( i_result != Z_OK ) && ( i_result != Z_STREAM_END ) )
1292 printf( "Zlib decompression failed. Result: %d\n", i_result );
1296 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
1297 ( i_result != Z_STREAM_END ) );
1299 i_dstsize = d_stream.total_out;
1300 inflateEnd( &d_stream );
1302 *_dst = (unsigned char *)realloc( psz_dst, i_dstsize );
1309 static void FreeSDP( sdp_t *p_sdp )
1312 FREE( p_sdp->psz_sdp );
1313 FREE( p_sdp->psz_sessionname );
1314 FREE( p_sdp->psz_connection );
1315 FREE( p_sdp->psz_media );
1316 FREE( p_sdp->psz_uri );
1317 FREE( p_sdp->psz_username );
1318 FREE( p_sdp->psz_network_type );
1320 FREE( p_sdp->psz_address );
1321 FREE( p_sdp->psz_address_type );
1323 for( i= p_sdp->i_attributes - 1; i >= 0 ; i-- )
1325 struct attribute_t *p_attr = p_sdp->pp_attributes[i];
1326 FREE( p_sdp->pp_attributes[i]->psz_field );
1327 FREE( p_sdp->pp_attributes[i]->psz_value );
1328 REMOVE_ELEM( p_sdp->pp_attributes, p_sdp->i_attributes, i);
1334 static int RemoveAnnounce( services_discovery_t *p_sd,
1335 sap_announce_t *p_announce )
1339 if( p_announce->p_sdp ) FreeSDP( p_announce->p_sdp );
1341 if( p_announce->i_item_id > -1 )
1343 playlist_LockDelete( p_sd->p_sys->p_playlist, p_announce->i_item_id );
1346 for( i = 0; i< p_sd->p_sys->i_announces; i++)
1348 if( p_sd->p_sys->pp_announces[i] == p_announce )
1350 REMOVE_ELEM( p_sd->p_sys->pp_announces, p_sd->p_sys->i_announces,
1361 static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 )
1363 /* A session is identified by
1364 * username, session_id, network type, address type and address */
1365 if( p_sdp1->psz_username && p_sdp2->psz_username &&
1366 p_sdp1->psz_network_type && p_sdp2->psz_network_type &&
1367 p_sdp1->psz_address_type && p_sdp2->psz_address_type &&
1368 p_sdp1->psz_address && p_sdp2->psz_address )
1370 if(!strcmp( p_sdp1->psz_username , p_sdp2->psz_username ) &&
1371 !strcmp( p_sdp1->psz_network_type , p_sdp2->psz_network_type ) &&
1372 !strcmp( p_sdp1->psz_address_type , p_sdp2->psz_address_type ) &&
1373 !strcmp( p_sdp1->psz_address , p_sdp2->psz_address ) &&
1374 p_sdp1->i_session_id == p_sdp2->i_session_id )
1390 static void CacheLoad( services_discovery_t *p_sd )
1392 msg_Warn( p_sd, "Cache not implemented") ;
1395 static void CacheSave( services_discovery_t *p_sd )
1397 msg_Warn( p_sd, "Cache not implemented") ;