1 /*****************************************************************************
2 * sap.c : SAP interface module
3 *****************************************************************************
4 * Copyright (C) 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> /* malloc(), free() */
32 #include <vlc/input.h>
36 #include <errno.h> /* ENOMEM */
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
49 /************************************************************************
50 * Macros and definitions
51 ************************************************************************/
53 #define MAX_LINE_LENGTH 256
55 /* SAP is always on that port */
57 #define SAP_V4_ADDRESS "224.2.127.254"
60 #define IPV6_ADDR_1 "FF0" /* Scope is inserted between them */
61 #define IPV6_ADDR_2 "::2:7FFE"
64 /*****************************************************************************
66 *****************************************************************************/
67 #define SAP_ADDR_TEXT N_( "SAP multicast address" )
68 #define SAP_ADDR_LONGTEXT N_( "Listen for SAP announces on another address" )
69 #define SAP_IPV4_TEXT N_( "IPv4-SAP listening" )
70 #define SAP_IPV4_LONGTEXT N_( \
71 "Set this if you want the SAP module to listen to IPv4 announces " \
72 "on the standard address" )
73 #define SAP_IPV6_TEXT N_( "IPv6-SAP listening" )
74 #define SAP_IPV6_LONGTEXT N_( \
75 "Set this if you want the SAP module to listen to IPv6 announces " \
76 "on the standard address" )
77 #define SAP_SCOPE_TEXT N_( "IPv6 SAP scope" )
78 #define SAP_SCOPE_LONGTEXT N_( \
79 "Sets the scope for IPv6 announces (default is 8)" )
80 #define SAP_TIMEOUT_TEXT N_( "SAP timeout (seconds)" )
81 #define SAP_TIMEOUT_LONGTEXT N_( \
82 "Sets the time before SAP items get deleted if no new announce " \
84 #define SAP_PARSE_TEXT N_( "Try to parse the SAP" )
85 #define SAP_PARSE_LONGTEXT N_( \
86 "When SAP can it will try to parse the SAP. If you don't select " \
87 "this, all announces will be parsed by the livedotcom module" )
88 #define SAP_STRICT_TEXT N_( "SAP Strict mode" )
89 #define SAP_STRICT_LONGTEXT N_( \
90 "When this is set, the SAP parser will discard some non-compliant " \
92 #define SAP_CACHE_TEXT N_("Use SAP cache")
93 #define SAP_CACHE_LONGTEXT N_( \
94 "If this option is selected, a SAP caching mechanism will be used." \
95 "This will result in lower SAP startup time, but you could end up " \
96 "with items corresponding to legacy streams." )
99 static int Open ( vlc_object_t * );
100 static void Close( vlc_object_t * );
101 static int OpenDemux ( vlc_object_t * );
102 static void CloseDemux ( vlc_object_t * );
105 set_description( _("SAP interface") );
107 add_string( "sap-addr", NULL, NULL,
108 SAP_ADDR_TEXT, SAP_ADDR_LONGTEXT, VLC_TRUE );
109 add_bool( "sap-ipv4", 1 , NULL,
110 SAP_IPV4_TEXT,SAP_IPV4_LONGTEXT, VLC_TRUE );
111 add_bool( "sap-ipv6", 0 , NULL,
112 SAP_IPV6_TEXT, SAP_IPV6_LONGTEXT, VLC_TRUE );
113 add_string( "sap-ipv6-scope", "8" , NULL,
114 SAP_SCOPE_TEXT, SAP_SCOPE_LONGTEXT, VLC_TRUE);
115 add_integer( "sap-timeout", 1800, NULL,
116 SAP_TIMEOUT_TEXT, SAP_TIMEOUT_LONGTEXT, VLC_TRUE );
117 add_bool( "sap-parse", 1 , NULL,
118 SAP_PARSE_TEXT,SAP_PARSE_LONGTEXT, VLC_TRUE );
119 add_bool( "sap-strict", 0 , NULL,
120 SAP_STRICT_TEXT,SAP_STRICT_LONGTEXT, VLC_TRUE );
121 add_bool( "sap-cache", 0 , NULL,
122 SAP_CACHE_TEXT,SAP_CACHE_LONGTEXT, VLC_TRUE );
124 set_capability( "services_discovery", 0 );
125 set_callbacks( Open, Close );
128 set_description( _("SDP file parser for UDP") );
129 add_shortcut( "sdp" );
130 set_capability( "demux2", 51 );
131 set_callbacks( OpenDemux, CloseDemux );
135 /*****************************************************************************
137 *****************************************************************************/
139 typedef struct sdp_t sdp_t;
140 typedef struct attribute_t attribute_t;
141 typedef struct sap_announce_t sap_announce_t;
143 /* The structure that contains sdp information */
149 char *psz_sessionname;
151 /* Raw m= and c= fields */
152 char *psz_connection;
157 char *psz_network_type;
158 char *psz_address_type;
160 int64_t i_session_id;
165 int i_in; /* IP version */
171 attribute_t **pp_attributes;
180 struct sap_announce_t
185 uint32_t i_source[4];
187 /* SAP annnounces must only contain one SDP */
190 playlist_item_t *p_item;
193 struct services_discovery_sys_t
195 /* Socket descriptors */
200 playlist_item_t *p_node;
202 /* Table of announces */
204 struct sap_announce_t **pp_announces;
218 /*****************************************************************************
220 *****************************************************************************/
224 static int Demux( demux_t *p_demux );
225 static int Control( demux_t *, int, va_list );
226 static void Run ( services_discovery_t *p_sd );
228 /* Main parsing functions */
229 static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp );
230 static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read );
231 static sdp_t * ParseSDP( vlc_object_t *p_sd, char* psz_sdp );
232 static sap_announce_t *CreateAnnounce( services_discovery_t *, uint16_t, sdp_t * );
233 static int RemoveAnnounce( services_discovery_t *p_sd, sap_announce_t *p_announce );
236 static void CacheLoad( services_discovery_t *p_sd );
237 static void CacheSave( services_discovery_t *p_sd );
238 /* Helper functions */
239 static char *GetAttribute( sdp_t *p_sdp, const char *psz_search );
240 static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 );
241 static int InitSocket( services_discovery_t *p_sd, char *psz_address, int i_port );
243 static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len );
244 static void FreeSDP( sdp_t *p_sdp );
247 /* Detect multicast addresses */
248 static int ismult( char * );
251 if( p ) { free( p ); (p) = NULL; }
252 /*****************************************************************************
253 * Open: initialize and create stuff
254 *****************************************************************************/
255 static int Open( vlc_object_t *p_this )
257 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
258 services_discovery_sys_t *p_sys = (services_discovery_sys_t *)
259 malloc( sizeof( services_discovery_sys_t ) );
261 playlist_t *p_playlist;
262 playlist_view_t *p_view;
266 p_sys->i_timeout = config_GetInt( p_sd,"sap-timeout" );
274 p_sys->b_strict = config_GetInt( p_sd, "sap-strict");
275 p_sys->b_parse = config_GetInt( p_sd, "sap-parse" );
277 if( config_GetInt( p_sd, "sap-cache" ) )
282 if( config_GetInt( p_sd, "sap-ipv4" ) )
284 InitSocket( p_sd, SAP_V4_ADDRESS, SAP_PORT );
286 if( config_GetInt( p_sd, "sap-ipv6" ) )
288 /* [ + 8x4+7*':' + ] */
289 char psz_address[42];
291 char *psz_scope = config_GetPsz( p_sd, "sap-ipv6-scope" );
293 if( psz_scope == NULL || *psz_scope == '\0')
299 c_scope = psz_scope[0];
301 snprintf( psz_address, 42, "[%s%c%s]", IPV6_ADDR_1, c_scope,
303 InitSocket( p_sd, psz_address, SAP_PORT );
306 psz_addr = config_GetPsz( p_sd, "sap-addr" );
307 if( psz_addr && *psz_addr )
309 InitSocket( p_sd, psz_addr, SAP_PORT );
312 if( p_sys->i_fd == 0 )
314 msg_Err( p_sd, "unable to read on any address");
318 /* Create our playlist node */
319 p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
323 msg_Warn( p_sd, "unable to find playlist, cancelling SAP listening");
327 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
328 p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
329 _("SAP"), p_view->p_root );
330 val.b_bool = VLC_TRUE;
331 var_Set( p_playlist, "intf-change", val );
333 vlc_object_release( p_playlist );
335 p_sys->i_announces = 0;
336 p_sys->pp_announces = NULL;
341 /*****************************************************************************
342 * OpenDemux: initialize and create stuff
343 *****************************************************************************/
344 static int OpenDemux( vlc_object_t *p_this )
346 demux_t *p_demux = (demux_t *)p_this;
348 int i_max_sdp = 1024;
350 char *psz_sdp = (char *)malloc( i_max_sdp );
362 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 )
364 msg_Err( p_demux, "cannot peek" );
367 if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) &&
368 strncmp( (char*)p_peek, "v=0\n", 4 ) &&
369 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
371 msg_Warn( p_demux, "SDP (UDP) module discarded" );
376 /* Gather the complete sdp file */
379 int i_read = stream_Read( p_demux->s,
380 &psz_sdp[i_sdp], i_max_sdp - i_sdp - 1 );
385 msg_Err( p_demux, "failed to read SDP" );
391 if( i_read < i_max_sdp - i_sdp - 1 )
393 psz_sdp[i_sdp] = '\0';
398 psz_sdp = (uint8_t*)realloc( psz_sdp, i_max_sdp );
401 p_sdp = ParseSDP( VLC_OBJECT(p_demux), psz_sdp );
405 msg_Warn( p_demux, "invalid SDP");
409 if( p_sdp->i_media > 1 )
414 if( ParseConnection( VLC_OBJECT( p_demux ), p_sdp ) )
416 p_sdp->psz_uri = NULL;
418 if( p_sdp->i_media_type != 33 && p_sdp->i_media_type != 32 && p_sdp->i_media_type != 14 )
421 if( p_sdp->psz_uri == NULL ) goto error;
423 p_demux->p_sys = (demux_sys_t *)malloc( sizeof(demux_sys_t) );
424 p_demux->p_sys->p_sdp = p_sdp;
425 p_demux->pf_control = Control;
426 p_demux->pf_demux = Demux;
429 if( p_sdp ) FreeSDP( p_sdp );
434 if( p_sdp ) FreeSDP( p_sdp );
435 stream_Seek( p_demux->s, 0 );
439 /*****************************************************************************
441 *****************************************************************************/
442 static void Close( vlc_object_t *p_this )
444 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
445 services_discovery_sys_t *p_sys = p_sd->p_sys;
447 playlist_t *p_playlist;
450 for( i = p_sys->i_fd-1 ; i >= 0 ; i-- )
452 net_Close( p_sys->pi_fd[i] );
455 if( config_GetInt( p_sd, "sap-cache" ) )
460 for( i = p_sys->i_announces - 1; i>= 0; i-- )
462 RemoveAnnounce( p_sd, p_sys->pp_announces[i] );
465 p_playlist = (playlist_t *) vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
470 playlist_NodeDelete( p_playlist, p_sys->p_node, VLC_TRUE );
471 vlc_object_release( p_playlist );
477 /*****************************************************************************
478 * CloseDemux: Close the demuxer
479 *****************************************************************************/
480 static void CloseDemux( vlc_object_t *p_this )
485 /*****************************************************************************
486 * Run: main SAP thread
487 *****************************************************************************
488 * Listens to SAP packets, and sends them to packet_handle
489 *****************************************************************************/
490 #define MAX_SAP_BUFFER 5000
492 static void Run( services_discovery_t *p_sd )
495 /* Dirty hack to slow down the startup of the sap interface */
496 /* Unneeded now : our node is in no_select mode */
499 /* read SAP packets */
500 while( !p_sd->b_die )
503 p_buffer = (uint8_t *)malloc( MAX_SAP_BUFFER );
507 msg_Err( p_sd, "out of memory");
508 p_sd->b_die = VLC_TRUE;
512 i_read = net_Select( p_sd, p_sd->p_sys->pi_fd, NULL,
513 p_sd->p_sys->i_fd, p_buffer,
514 MAX_SAP_BUFFER, 500000 );
516 /* Check for items that need deletion */
517 for( i = 0 ; i< p_sd->p_sys->i_announces ; i++ )
519 struct sap_announce_t *p_announce;
520 mtime_t i_timeout = ( mtime_t ) 1000000*p_sys->i_timeout;
521 if( mdate() - p_sd->p_sys->pp_announces[i]->i_last > i_timeout )
523 msg_Dbg( p_sd,"Time out for %s, deleting (%i/%i)",
524 p_sd->p_sys->pp_announces[i]->psz_name,
525 i , p_sd->p_sys->i_announces );
527 /* Remove the playlist item */
528 p_playlist = vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
532 int i_pos = playlist_GetPositionById( p_playlist,
533 p_sd->p_sys->pp_announces[i]->i_id );
534 playlist_Delete( p_playlist, i_pos );
535 vlc_object_release( p_playlist );
538 /* Free the p_announce */
539 p_announce = p_sd->p_sys->pp_announces[i];
540 if( p_announce->psz_name )
541 free( p_announce->psz_name );
542 if( p_announce->psz_uri )
543 free( p_announce->psz_uri );
545 /* Remove the sap_announce from the array */
546 REMOVE_ELEM( p_sd->p_sys->pp_announces,
547 p_sd->p_sys->i_announces, i );
555 /* Minimum length is > 6 */
560 msg_Warn( p_sd, "socket read error" );
566 p_buffer[i_read] = '\0';
568 /* Parse the packet */
569 ParseSAP( p_sd, p_buffer, i_read );
575 /**********************************************************************
576 * Demux: reads and demuxes data packets
577 * Return -1 if error, 0 if EOF, 1 else
578 **********************************************************************/
579 static int Demux( demux_t *p_demux )
581 sdp_t *p_sdp = p_demux->p_sys->p_sdp;
582 playlist_t *p_playlist;
584 p_playlist = (playlist_t *)vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
587 p_playlist->status.p_item->i_flags |= PLAYLIST_DEL_FLAG;
589 playlist_Add( p_playlist, p_sdp->psz_uri, p_sdp->psz_sessionname,
590 PLAYLIST_APPEND, PLAYLIST_END );
592 vlc_object_release( p_playlist );
597 static int Control( demux_t *p_demux, int i_query, va_list args )
602 /**************************************************************
604 **************************************************************/
606 /* i_read is at least > 6 */
607 static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
609 int i_version, i_address_type, i_hash, i;
611 uint8_t *psz_initial_sdp;
613 vlc_bool_t b_compressed;
614 vlc_bool_t b_need_delete = VLC_FALSE;
616 int i_decompressed_size;
617 uint8_t *p_decompressed_buffer;
621 /* First, check the sap announce is correct */
622 i_version = p_buffer[0] >> 5;
626 msg_Dbg( p_sd, "strange sap version %d found", i_version );
629 i_address_type = p_buffer[0] & 0x10;
631 if( (p_buffer[0] & 0x08) != 0 )
633 msg_Dbg( p_sd, "reserved bit incorrectly set" );
637 if( (p_buffer[0] & 0x04) != 0 )
639 msg_Dbg( p_sd, "session deletion packet" );
640 b_need_delete = VLC_TRUE;
643 if( p_buffer[0] & 0x02 )
645 msg_Dbg( p_sd, "encrypted packet, unsupported" );
649 b_compressed = p_buffer[0] & 0x01;
651 i_hash = ( p_buffer[2] << 8 ) + p_buffer[3];
653 if( p_sd->p_sys->b_strict && i_hash == 0 )
655 msg_Dbg( p_sd, "strict mode, discarding announce with null id hash");
659 psz_sdp = &p_buffer[4];
660 psz_initial_sdp = psz_sdp;
662 if( i_address_type == 0 ) /* ipv4 source address */
667 msg_Warn( p_sd,"too short SAP packet\n" );
671 else /* ipv6 source address */
676 msg_Warn( p_sd,"too short SAP packet\n" );
684 i_decompressed_size = Decompress( psz_sdp,
685 &p_decompressed_buffer,i_read - ( psz_sdp - p_buffer ) );
686 if( i_decompressed_size > 0 && i_decompressed_size < MAX_SAP_BUFFER )
688 memcpy( psz_sdp, p_decompressed_buffer, i_decompressed_size );
689 psz_sdp[i_decompressed_size] = '\0';
690 free( p_decompressed_buffer );
693 msg_Warn( p_sd, "Ignoring compressed sap packet" );
698 /* Add the size of authentification info */
699 if( i_read < p_buffer[1] + (psz_sdp - psz_initial_sdp ) )
701 msg_Warn( p_sd, "too short SAP packet\n");
704 psz_sdp += p_buffer[1];
707 /* Skip payload type */
708 /* Handle announces without \0 between SAP and SDP */
709 while( *psz_sdp != '\0' && ( psz_sdp[0] != 'v' && psz_sdp[1] != '=' ) )
711 if( psz_sdp - psz_initial_sdp >= i_read - 5 )
713 msg_Warn( p_sd, "empty SDP ?");
718 if( *psz_sdp == '\0' )
722 if( psz_sdp != psz_foo && strcasecmp( psz_foo, "application/sdp" ) )
724 msg_Dbg( p_sd, "unhandled content type: %s", psz_foo );
726 if( psz_sdp -p_buffer >= i_read )
728 msg_Warn( p_sd, "package without content" );
733 p_sdp = ParseSDP( VLC_OBJECT(p_sd), psz_sdp );
740 /* Decide whether we should add a playlist item for this SDP */
741 /* Parse connection information (c= & m= ) */
742 if( ParseConnection( VLC_OBJECT(p_sd), p_sdp ) )
744 p_sdp->psz_uri = NULL;
747 /* Multi-media or no-parse -> pass to LIVE.COM */
748 if( p_sdp->i_media > 1 || ( p_sdp->i_media_type != 14 &&
749 p_sdp->i_media_type != 32 &&
750 p_sdp->i_media_type != 33) ||
751 p_sd->p_sys->b_parse == VLC_FALSE )
753 if( p_sdp->psz_uri ) free( p_sdp->psz_uri );
754 asprintf( &p_sdp->psz_uri, "sdp://%s", p_sdp->psz_sdp );
757 if( p_sdp->psz_uri == NULL ) return VLC_EGENERIC;
759 for( i = 0 ; i< p_sd->p_sys->i_announces ; i++ )
762 /* FIXME: we create a new announce each time the sdp changes */
763 if( IsSameSession( p_sd->p_sys->pp_announces[i]->p_sdp, p_sdp ) )
767 RemoveAnnounce( p_sd, p_sd->p_sys->pp_announces[i]);
772 p_sd->p_sys->pp_announces[i]->i_last = mdate();
779 if( p_sdp->i_media > 1 )
781 msg_Dbg( p_sd, "passing to LIVE.COM" );
784 CreateAnnounce( p_sd, i_hash, p_sdp );
789 sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
792 playlist_t *p_playlist;
793 playlist_item_t *p_item, *p_child;
795 sap_announce_t *p_sap = (sap_announce_t *)malloc(
796 sizeof(sap_announce_t ) );
799 msg_Err( p_sd, "out of memory");
800 p_sd->b_die = VLC_TRUE;
803 p_sap->i_last = mdate();
804 p_sap->i_hash = i_hash;
805 p_sap->p_sdp = p_sdp;
806 p_sap->p_item = NULL;
808 /* Create the playlist item here */
809 p_item = playlist_ItemNew( p_sd, p_sap->p_sdp->psz_uri,
810 p_sap->p_sdp->psz_sessionname );
817 psz_value = GetAttribute( p_sap->p_sdp, "x-plgroup" );
819 if( psz_value == NULL )
821 psz_value = GetAttribute( p_sap->p_sdp, "plgroup" );
824 p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
828 msg_Err( p_sd, "playlist not found" );
834 if( psz_value != NULL )
836 p_child = playlist_ChildSearchName( p_sd->p_sys->p_node, psz_value );
838 if( p_child == NULL )
840 p_child = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
841 psz_value, p_sd->p_sys->p_node );
846 p_child = p_sd->p_sys->p_node;
849 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
851 playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_child,
852 PLAYLIST_APPEND, PLAYLIST_END );
854 vlc_object_release( p_playlist );
856 p_sap->p_item = p_item;
858 TAB_APPEND( p_sd->p_sys->i_announces,
859 p_sd->p_sys->pp_announces, p_sap );
864 static char *GetAttribute( sdp_t *p_sdp, const char *psz_search )
868 for( i = 0 ; i< p_sdp->i_attributes; i++ )
870 if( !strncmp( p_sdp->pp_attributes[i]->psz_field, psz_search,
871 strlen( p_sdp->pp_attributes[i]->psz_field ) ) )
873 return p_sdp->pp_attributes[i]->psz_value;
880 /* Fill p_sdp->psz_uri */
881 static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
885 char *psz_uri = NULL;
886 char *psz_proto = NULL;
890 if( p_sdp->psz_connection )
892 psz_parse = p_sdp->psz_connection;
894 psz_eof = strchr( psz_parse, ' ' );
899 psz_parse = psz_eof + 1;
903 msg_Warn( p_obj, "unable to parse c field (1)");
907 psz_eof = strchr( psz_parse, ' ' );
912 if( !strncmp( psz_parse, "IP4", 3 ) )
916 else if( !strncmp( psz_parse, "IP6", 3 ) )
924 psz_parse = psz_eof + 1;
928 msg_Warn( p_obj, "unable to parse c field (2)");
932 psz_eof = strchr( psz_parse, '/' );
940 msg_Dbg( p_obj, "incorrect c field");
942 psz_uri = strdup( psz_parse );
947 if( p_sdp->psz_media )
949 psz_parse = p_sdp->psz_media;
951 psz_eof = strchr( psz_parse, ' ' );
957 if( strncmp( psz_parse, "audio", 5 ) &&
958 strncmp( psz_parse, "video",5 ) )
960 msg_Warn( p_obj, "unhandled media type -%s-", psz_parse );
965 psz_parse = psz_eof + 1;
969 msg_Warn( p_obj, "unable to parse m field (1)");
974 psz_eof = strchr( psz_parse, ' ' );
980 /* FIXME : multiple port ! */
981 i_port = atoi( psz_parse );
983 if( i_port <= 0 || i_port >= 65536 )
985 msg_Warn( p_obj, "invalid transport port %i", i_port );
988 psz_parse = psz_eof + 1;
992 msg_Warn( p_obj, "unable to parse m field (2)");
997 psz_eof = strchr( psz_parse, ' ' );
1002 psz_proto = strdup( psz_parse );
1004 psz_parse = psz_eof + 1;
1005 p_sdp->i_media_type = atoi( psz_parse );
1010 msg_Dbg( p_obj, "incorrect m field");
1011 p_sdp->i_media_type = 33;
1012 psz_proto = strdup( psz_parse );
1016 if( psz_proto && !strncmp( psz_proto, "RTP/AVP", 7 ) )
1019 psz_proto = strdup( "rtp" );
1021 if( psz_proto && !strncmp( psz_proto, "UDP", 3 ) )
1024 psz_proto = strdup( "udp" );
1028 /* FIXME: HTTP support */
1035 if( ismult( psz_uri ) )
1037 asprintf( &p_sdp->psz_uri, "%s://@%s:%i", psz_proto, psz_uri, i_port );
1041 asprintf( &p_sdp->psz_uri, "%s://%s:%i", psz_proto, psz_uri, i_port );
1048 /***********************************************************************
1049 * ParseSDP : SDP parsing
1050 * *********************************************************************
1051 * Validate SDP and parse all fields
1052 ***********************************************************************/
1053 static sdp_t * ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
1056 vlc_bool_t b_invalid = VLC_FALSE;
1057 vlc_bool_t b_end = VLC_FALSE;
1059 if( psz_sdp == NULL )
1064 if( psz_sdp[0] != 'v' || psz_sdp[1] != '=' )
1066 msg_Warn( p_obj, "Bad packet" );
1070 p_sdp = (sdp_t *)malloc( sizeof( sdp_t ) );
1072 p_sdp->psz_sdp = strdup( psz_sdp );
1074 p_sdp->psz_sessionname = NULL;
1075 p_sdp->psz_media = NULL;
1076 p_sdp->psz_connection = NULL;
1077 p_sdp->psz_uri = NULL;
1078 p_sdp->psz_address = NULL;
1079 p_sdp->psz_address_type= NULL;
1082 p_sdp->i_attributes = 0;
1083 p_sdp->pp_attributes = NULL;
1085 while( *psz_sdp != '\0' && b_end == VLC_FALSE )
1092 while( *psz_sdp == '\r' || *psz_sdp == '\n' ||
1093 *psz_sdp == ' ' || *psz_sdp == '\t' )
1098 if( ( psz_eol = strchr( psz_sdp, '\n' ) ) == NULL )
1100 psz_eol = psz_sdp + strlen( psz_sdp );
1103 if( psz_eol > psz_sdp && *( psz_eol - 1 ) == '\r' )
1108 if( psz_eol <= psz_sdp )
1114 /* no space allowed between fields */
1115 if( psz_sdp[1] != '=' )
1117 msg_Warn( p_obj, "invalid packet" ) ;
1122 /* Now parse each line */
1123 switch( psz_sdp[0] )
1128 p_sdp->psz_sessionname = strdup( &psz_sdp[2] );
1133 /* o field is <username> <session id> <version>
1134 * <network type> <address type> <address> */
1136 #define GET_FIELD( store ) \
1137 psz_eof = strchr( psz_parse, ' ' ); \
1140 *psz_eof=0; store = strdup( psz_parse ); \
1144 if( i_field != 5 ) \
1146 b_invalid = VLC_TRUE; break; \
1150 store = strdup( psz_parse ); \
1153 psz_parse = psz_eof + 1; i_field++;
1156 psz_parse = &psz_sdp[2];
1157 GET_FIELD( p_sdp->psz_username );
1158 GET_FIELD( psz_sess_id );
1160 p_sdp->i_session_id = atoll( psz_sess_id );
1162 FREE( psz_sess_id );
1164 GET_FIELD( psz_sess_id );
1165 FREE( psz_sess_id );
1167 GET_FIELD( p_sdp->psz_network_type );
1168 GET_FIELD( p_sdp->psz_address_type );
1169 GET_FIELD( p_sdp->psz_address );
1180 case( 'a' ): /* attribute */
1182 char *psz_eon = strchr( &psz_sdp[2], ':' );
1183 attribute_t *p_attr = malloc( sizeof( attribute_t ) );
1185 /* Attribute with value */
1190 p_attr->psz_field = strdup( &psz_sdp[2] );
1191 p_attr->psz_value = strdup( psz_eon );
1193 else /* Attribute without value */
1195 p_attr->psz_field = strdup( &psz_sdp[2] );
1196 p_attr->psz_value = NULL;
1199 TAB_APPEND( p_sdp->i_attributes, p_sdp->pp_attributes, p_attr );
1203 case( 'm' ): /* Media announcement */
1205 /* If we have several medias, we pass the announcement to
1206 * LIVE.COM, so just count them */
1208 if( p_sdp->i_media == 1 )
1210 p_sdp->psz_media = strdup( &psz_sdp[2] );
1217 if( p_sdp->i_media > 1 )
1220 p_sdp->psz_connection = strdup( &psz_sdp[2] );
1241 /***********************************************************************
1242 * ismult: returns true if we have a multicast address
1243 ***********************************************************************/
1244 static int ismult( char *psz_uri )
1249 i_value = strtol( psz_uri, &psz_end, 0 );
1252 if( psz_uri[0] == '[')
1254 if( strncasecmp( &psz_uri[1], "FF0" , 3) ||
1255 strncasecmp( &psz_uri[2], "FF0" , 3))
1258 return( VLC_FALSE );
1261 if( *psz_end != '.' ) { return( VLC_FALSE ); }
1263 return( i_value < 224 ? VLC_FALSE : VLC_TRUE );
1266 static int InitSocket( services_discovery_t *p_sd, char *psz_address,
1269 int i_fd = net_OpenUDP( p_sd, psz_address, i_port, "", 0 );
1273 INSERT_ELEM( p_sd->p_sys->pi_fd, p_sd->p_sys->i_fd,
1274 p_sd->p_sys->i_fd, i_fd );
1278 return VLC_EGENERIC;
1282 static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len )
1284 int i_result, i_dstsize, n;
1285 unsigned char *psz_dst;
1288 d_stream.zalloc = (alloc_func)0;
1289 d_stream.zfree = (free_func)0;
1290 d_stream.opaque = (voidpf)0;
1292 i_result = inflateInit(&d_stream);
1293 if( i_result != Z_OK )
1295 printf( "inflateInit() failed. Result: %d\n", i_result );
1299 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
1300 i_position = p_playlist->i_index;
1302 /* Gather the complete sdp file */
1305 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp], i_sdp_max - i_sdp - 1 );
1307 d_stream.next_in = (Bytef *)psz_src;
1308 d_stream.avail_in = i_len;
1316 psz_dst = (unsigned char *)realloc( psz_dst, n * 1000 );
1317 d_stream.next_out = (Bytef *)&psz_dst[(n - 1) * 1000];
1318 d_stream.avail_out = 1000;
1320 i_result = inflate(&d_stream, Z_NO_FLUSH);
1321 if( ( i_result != Z_OK ) && ( i_result != Z_STREAM_END ) )
1323 printf( "Zlib decompression failed. Result: %d\n", i_result );
1327 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
1328 ( i_result != Z_STREAM_END ) );
1330 i_dstsize = d_stream.total_out;
1331 inflateEnd( &d_stream );
1333 *_dst = (unsigned char *)realloc( psz_dst, i_dstsize );
1340 static void FreeSDP( sdp_t *p_sdp )
1343 FREE( p_sdp->psz_sdp );
1344 FREE( p_sdp->psz_sessionname );
1345 FREE( p_sdp->psz_connection );
1346 FREE( p_sdp->psz_media );
1347 FREE( p_sdp->psz_uri );
1349 FREE( p_sdp->psz_address );
1350 FREE( p_sdp->psz_address_type );
1352 for( i= p_sdp->i_attributes - 1; i >= 0 ; i-- )
1354 struct attribute_t *p_attr = p_sdp->pp_attributes[i];
1355 FREE( p_sdp->pp_attributes[i]->psz_field );
1356 FREE( p_sdp->pp_attributes[i]->psz_value );
1357 REMOVE_ELEM( p_sdp->pp_attributes, p_sdp->i_attributes, i);
1363 static int RemoveAnnounce( services_discovery_t *p_sd,
1364 sap_announce_t *p_announce )
1367 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
1368 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1370 if( p_announce->p_sdp ) FreeSDP( p_announce->p_sdp );
1372 if( !p_playlist ) return VLC_EGENERIC;
1374 if( p_announce->p_item )
1376 playlist_Delete( p_playlist, p_announce->p_item->input.i_id );
1379 for( i = 0; i< p_sd->p_sys->i_announces; i++)
1381 if( p_sd->p_sys->pp_announces[i] == p_announce )
1383 REMOVE_ELEM( p_sd->p_sys->pp_announces, p_sd->p_sys->i_announces,
1389 vlc_object_release( p_playlist );
1396 static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 )
1398 /* A session is identified by
1399 * username, session_id, network type, address type and address */
1400 if( p_sdp1->psz_username && p_sdp2->psz_username &&
1401 p_sdp1->psz_network_type && p_sdp2->psz_network_type &&
1402 p_sdp1->psz_address_type && p_sdp2->psz_address_type &&
1403 p_sdp1->psz_address && p_sdp2->psz_address )
1405 if(!strcmp( p_sdp1->psz_username , p_sdp2->psz_username ) &&
1406 !strcmp( p_sdp1->psz_network_type , p_sdp2->psz_network_type ) &&
1407 !strcmp( p_sdp1->psz_address_type , p_sdp2->psz_address_type ) &&
1408 !strcmp( p_sdp1->psz_address , p_sdp2->psz_address ) &&
1409 p_sdp1->i_session_id == p_sdp2->i_session_id )
1425 static void CacheLoad( services_discovery_t *p_sd )
1427 msg_Warn( p_sd, "Cache not implemented") ;
1430 static void CacheSave( services_discovery_t *p_sd )
1432 msg_Warn( p_sd, "Cache not implemented") ;