]> git.sesse.net Git - vlc/blobdiff - modules/misc/sap.c
* ALL: Better announce system
[vlc] / modules / misc / sap.c
index 0ac6568222552f66952dee8fd226fa3695b02b71..f4d145aaae8360c8b2c3bd1b58fa071178b4603b 100644 (file)
@@ -2,7 +2,7 @@
  * sap.c :  SAP interface module
  *****************************************************************************
  * Copyright (C) 2001 VideoLAN
- * $Id: sap.c,v 1.36 2003/11/20 23:13:28 sigmunau Exp $
+ * $Id$
  *
  * Authors: Arnaud Schauly <gitan@via.ecp.fr>
  *          ClĂ©ment Stenac <zorglub@via.ecp.fr>
 #define SAP_ADDR_TEXT N_("SAP multicast address")
 #define SAP_ADDR_LONGTEXT N_("SAP multicast address")
 #define SAP_IPV4_TEXT N_("IPv4-SAP listening")
-#define SAP_IPV4_LONGTEXT N_("Set this if you want SAP to listen for IPv4 announces")
-#define SAP_IPV6_TEXT N_("IPv6-SAP listening")
-#define SAP_IPV6_LONGTEXT N_("Set this if you want SAP to listen for IPv6 announces")
+#define SAP_IPV4_LONGTEXT N_( \
+      "Set this if you want the SAP module to listen to IPv4 announces")
+#define SAP_IPV6_TEXT N_( "IPv6-SAP listening")
+#define SAP_IPV6_LONGTEXT N_(\
+      "Set this if you want the SAP module to listen to IPv6 announces")
 #define SAP_SCOPE_TEXT N_("IPv6 SAP scope")
-#define SAP_SCOPE_LONGTEXT N_("Sets the scope for IPv6 announces (default is 8)")
+#define SAP_SCOPE_LONGTEXT N_( \
+       "Sets the scope for IPv6 announces (default is 8)")
+#define SAP_TIMEOUT_TEXT N_("SAP timeout (seconds)")
+#define SAP_TIMEOUT_LONGTEXT N_( \
+       "Sets the time before SAP items get deleted if no new announce " \
+       "is received.")
+#define SAP_PARSE_TEXT N_("Try to parse the SAP")
+#define SAP_PARSE_LONGTEXT N_( \
+       "When SAP can it will try to parse the SAP. Normal behaviour is" \
+       "to have livedotcom parse the announce." )
 
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
 vlc_module_begin();
-    add_category_hint( N_("SAP"), NULL, VLC_TRUE );
-        add_string( "sap-addr", NULL, NULL,
-                     SAP_ADDR_TEXT, SAP_ADDR_LONGTEXT, VLC_TRUE );
-
-        add_bool( "sap-ipv4", 1 , NULL,
-                     SAP_IPV4_TEXT,SAP_IPV4_LONGTEXT, VLC_TRUE);
-
-        add_bool( "sap-ipv6", 0 , NULL,
-                   SAP_IPV6_TEXT, SAP_IPV6_LONGTEXT, VLC_TRUE);
+    set_description( _("SAP interface") );
 
-        add_string( "sap-ipv6-scope", "8" , NULL,
-                    SAP_SCOPE_TEXT, SAP_SCOPE_LONGTEXT, VLC_TRUE);
+    add_string( "sap-addr", NULL, NULL,
+                SAP_ADDR_TEXT, SAP_ADDR_LONGTEXT, VLC_TRUE );
+    add_bool( "sap-ipv4", 1 , NULL,
+               SAP_IPV4_TEXT,SAP_IPV4_LONGTEXT, VLC_TRUE);
+    add_bool( "sap-ipv6", 0 , NULL,
+              SAP_IPV6_TEXT, SAP_IPV6_LONGTEXT, VLC_TRUE);
+    add_string( "sap-ipv6-scope", "8" , NULL,
+                SAP_SCOPE_TEXT, SAP_SCOPE_LONGTEXT, VLC_TRUE);
+    add_integer( "sap-timeout", 1800, NULL,
+                 SAP_TIMEOUT_TEXT, SAP_TIMEOUT_LONGTEXT, VLC_TRUE);
+    add_bool( "sap-parse", 1 , NULL,
+               SAP_PARSE_TEXT,SAP_PARSE_LONGTEXT, VLC_TRUE);
 
-    set_description( _("SAP interface") );
     set_capability( "interface", 0 );
     set_callbacks( Open, Close );
 vlc_module_end();
@@ -161,6 +173,14 @@ struct attr_descr_t
     char *psz_value;
 };
 
+struct sap_announce_t
+{
+    mtime_t i_last;
+    int i_id;
+    char *psz_name;
+    char *psz_uri;
+};
+
 struct intf_sys_t
 {
     /* IPV4 and IPV6 */
@@ -168,6 +188,12 @@ struct intf_sys_t
 
     /* playlist group */
     int i_group;
+
+    /* Table of announces */
+    int i_announces;
+    struct sap_announce_t **pp_announces;
+
+    int i_timeout;
 };
 
 #ifdef HAVE_ZLIB_H
@@ -221,6 +247,7 @@ static int Open( vlc_object_t *p_this )
 
     playlist_t          *p_playlist;
 
+    p_sys->i_timeout = config_GetInt(p_intf,"sap-timeout");
     p_sys->fd[0] = -1;
     p_sys->fd[1] = -1;
     if( config_GetInt( p_intf, "sap-ipv4" ) )
@@ -242,7 +269,7 @@ static int Open( vlc_object_t *p_this )
         sock.i_ttl             = 0;
         p_intf->p_private = (void*) &sock;
 
-        p_network = module_Need( p_intf, "network", "ipv4" );
+        p_network = module_Need( p_intf, "network", "ipv4", VLC_TRUE );
         if( p_network )
         {
             p_sys->fd[0] = sock.i_handle;
@@ -266,7 +293,8 @@ static int Open( vlc_object_t *p_this )
         {
             psz_scope = strdup( "8" );
         }
-        snprintf( psz_address, 100, "[%s%c%s]",IPV6_ADDR_1, psz_scope[0], IPV6_ADDR_2 );
+        snprintf( psz_address, 100, "[%s%c%s]",IPV6_ADDR_1,
+                  psz_scope[0], IPV6_ADDR_2 );
         free( psz_scope );
 
         sock.i_type            = NETWORK_UDP;
@@ -277,7 +305,7 @@ static int Open( vlc_object_t *p_this )
         sock.i_ttl             = 0;
         p_intf->p_private = (void*) &sock;
 
-        p_network = module_Need( p_intf, "network", "ipv6" );
+        p_network = module_Need( p_intf, "network", "ipv6", VLC_TRUE );
         if( p_network )
         {
             p_sys->fd[1] = sock.i_handle;
@@ -305,6 +333,9 @@ static int Open( vlc_object_t *p_this )
         vlc_object_release( p_playlist );
     }
 
+    p_sys->i_announces = 0;
+    p_sys->pp_announces = NULL;
+
     p_intf->pf_run = Run;
     p_intf->p_sys  = p_sys;
 
@@ -318,6 +349,7 @@ static void Close( vlc_object_t *p_this )
 {
     intf_thread_t *p_intf = (intf_thread_t*)p_this;
     intf_sys_t    *p_sys  = p_intf->p_sys;
+    int i;
 
     if( p_sys->fd[0] > 0 )
     {
@@ -328,6 +360,16 @@ static void Close( vlc_object_t *p_this )
         close( p_sys->fd[1] );
     }
 
+    for( i = 0 ; i< p_sys->i_announces ; i++ )
+    {
+        if( p_sys->pp_announces[i]->psz_name )
+           free( p_sys->pp_announces[i]->psz_name );
+        if( p_sys->pp_announces[i]->psz_uri )
+           free( p_sys->pp_announces[i]->psz_uri );
+        free( p_sys->pp_announces[i] );
+    }
+    free( p_sys->pp_announces );
+
     free( p_sys );
 }
 
@@ -347,6 +389,8 @@ static void Run( intf_thread_t *p_intf )
     /* read SAP packets */
     while( !p_intf->b_die )
     {
+        playlist_t *p_playlist= NULL;
+        int i;
         int i_read = NetRead( p_intf, p_sys->fd, buffer, MAX_SAP_BUFFER );
         uint8_t *p_sdp;
         int i_version;
@@ -355,10 +399,49 @@ static void Run( intf_thread_t *p_intf )
         int b_message_type;
         int b_encrypted;
         int b_compressed;
-        int i_mesg_id_hash;
         unsigned char *p_decompressed_buffer;
         int i_decompressed_size;
-        
+
+        /* Check for items that need deletion */
+        for( i = 0 ; i< p_intf->p_sys->i_announces ; i++ )
+        {
+           struct sap_announce_t *p_announce;
+           mtime_t i_timeout = (mtime_t)1000000*p_sys->i_timeout;
+           if( mdate() - p_intf->p_sys->pp_announces[i]->i_last > i_timeout )
+           {
+               msg_Dbg(p_intf,"Time out for %s, deleting (%i/%i)",
+                       p_intf->p_sys->pp_announces[i]->psz_name,
+                       i , p_intf->p_sys->i_announces );
+
+               /* Remove the playlist item */
+               p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
+                              FIND_ANYWHERE );
+               if( p_playlist )
+               {
+                   int i_pos = playlist_GetPositionById( p_playlist,
+                              p_intf->p_sys->pp_announces[i]->i_id );
+                   playlist_Delete( p_playlist, i_pos );
+               }
+
+               vlc_object_release( p_playlist );
+
+               /* Free the p_announce */
+               p_announce =  p_intf->p_sys->pp_announces[i];
+               if( p_announce->psz_name )
+                  free(  p_announce->psz_name );
+               if( p_announce->psz_uri )
+                  free(  p_announce->psz_uri );
+
+              /* Remove the sap_announce from the array */
+              REMOVE_ELEM( p_intf->p_sys->pp_announces,
+                           p_intf->p_sys->i_announces,
+                           i );
+
+              free( p_announce );
+
+           }
+        }
+
         /* Minimum length is > 6 */
         if( i_read <= 6 )
         {
@@ -408,7 +491,7 @@ static void Run( intf_thread_t *p_intf )
         {
 #ifdef HAVE_ZLIB_H
             i_decompressed_size = do_decompress( p_sdp, &p_decompressed_buffer, i_read - ( p_sdp - buffer ) );
-            if( i_decompressed_size > 0 )
+            if( i_decompressed_size > 0 && i_decompressed_size < MAX_SAP_BUFFER )
             {
                 memcpy( p_sdp, p_decompressed_buffer, i_decompressed_size );
                 p_sdp[i_decompressed_size] = '\0';
@@ -442,6 +525,8 @@ static void Run( intf_thread_t *p_intf )
         {
             msg_Warn( p_intf, "ditching sap packet" );
         }
+
+        memset( buffer, 0, MAX_SAP_BUFFER );
     }
 }
 
@@ -562,78 +647,82 @@ static void mfield_parse( char *psz_mfield, char **ppsz_proto,
 
 static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
 {
-    playlist_item_t * p_item;
-    char *psz_uri, *psz_proto;
+    struct sap_announce_t *p_announce;
+    char *psz_uri, *psz_proto, *psz_item_uri;
     char *psz_port;
     char *psz_uri_default;
-    int i_count , i;
+    int i_count, i, i_id = 0;
     vlc_bool_t b_http = VLC_FALSE;
     char *psz_http_path = NULL;
     playlist_t *p_playlist = NULL;
+    playlist_item_t *p_item;
 
     psz_uri_default = NULL;
-    if( p_sd->i_media > 1 )
+    if( p_sd->i_media > 1 || !config_GetInt( p_intf, "sap-parse" ) )
     {
-        p_item = malloc( sizeof( playlist_item_t ) );
-        if( p_item == NULL )
+        asprintf( &psz_uri, "sdp://%s", p_sd->psz_sdp );
+        /* Check if we have already added the item */
+        for( i = 0 ; i< p_intf->p_sys->i_announces ; i++ )
         {
-            msg_Warn( p_intf, "out of memory" );
-            return;
+            if( !strcmp(p_intf->p_sys->pp_announces[i]->psz_uri,
+                        psz_uri ) )
+            {
+                p_intf->p_sys->pp_announces[i]->i_last = mdate();
+                free(psz_uri);
+                return;
+            }
         }
-        p_item->psz_name    = strdup( p_sd->psz_sessionname );
-        p_item->psz_uri     = NULL;
-        p_item->i_duration  = -1;
-        p_item->ppsz_options= NULL;
-        p_item->i_options   = 0;
-
-        p_item->i_type      = 0;
-        p_item->i_status    = 0;
-        p_item->b_autodeletion = VLC_FALSE;
-        p_item->b_enabled   = VLC_TRUE;
-        p_item->i_group     = p_intf->p_sys->i_group;
-        p_item->psz_author  = strdup( "" );
-        psz_uri = malloc( strlen( p_sd->psz_sdp ) + 7 );
-        if( psz_uri == NULL )
+        /* Add it to the playlist */
+        p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
+                                      FIND_ANYWHERE );
+        i_id = playlist_Add( p_playlist, psz_uri, p_sd->psz_sessionname ,
+                      PLAYLIST_CHECK_INSERT, PLAYLIST_END );
+        if( i_id != -1 )
         {
-            msg_Warn( p_intf, "out of memory" );
-            free( p_item->psz_name );
-            free( p_item->psz_author );
-            free( p_item );
-            return;
+            playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
+            playlist_ItemSetGroup( p_item, p_intf->p_sys->i_group );
+        }
+
+        /* Remember it */
+        p_announce = (struct sap_announce_t *)malloc(
+                      sizeof(struct sap_announce_t) );
+        if( p_sd->psz_sessionname )
+        {
+            p_announce->psz_name = strdup(  p_sd->psz_sessionname );
         }
-        p_item->psz_uri = psz_uri;
-        memcpy( psz_uri, "sdp://", 6 );
-        psz_uri += 6;
-        memcpy( psz_uri, p_sd->psz_sdp, strlen( p_sd->psz_sdp ) + 1 );
-        /* Enqueueing p_item in the playlist */
-        p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
-        playlist_AddItem ( p_playlist, p_item, PLAYLIST_CHECK_INSERT, PLAYLIST_END );
+        else
+        {
+            p_announce->psz_name = strdup( "" );
+        }
+        if( psz_uri )
+        {
+            p_announce->psz_uri  = strdup( psz_uri );
+        }
+        else
+        {
+            p_announce->psz_uri = strdup( "" );
+        }
+        p_announce->i_id = i_id;
+        p_announce->i_last = mdate();
+
+        INSERT_ELEM( p_intf->p_sys->pp_announces,
+                     p_intf->p_sys->i_announces,
+                     p_intf->p_sys->i_announces,
+                     p_announce );
+
         vlc_object_release( p_playlist );
+        free( psz_uri );
         return;
-    }        
-        
+    }
+
     cfield_parse( p_sd->psz_connection, &psz_uri_default );
 
     for( i_count = 0 ; i_count < p_sd->i_media ; i_count++ )
     {
-        p_item = malloc( sizeof( playlist_item_t ) );
-        p_item->psz_name    = strdup( p_sd->psz_sessionname );
-        p_item->psz_uri     = NULL;
-        p_item->i_duration  = -1;
-        p_item->ppsz_options= NULL;
-        p_item->i_options   = 0;
-
-        p_item->i_type      = 0;
-        p_item->i_status    = 0;
-        p_item->b_autodeletion = VLC_FALSE;
-        p_item->b_enabled   = VLC_TRUE;
-        p_item->i_group     = p_intf->p_sys->i_group;
-        p_item->psz_author  = strdup( "" );
-
-        psz_uri = NULL;
+        int i_group = p_intf->p_sys->i_group;
 
-        /* Build what we have to put in p_item->psz_uri, with the m and
-         *  c fields  */
+        /* Build what we have to put in psz_item_uri, with the m and
+         * c fields  */
 
         if( !p_sd->pp_media[i_count] )
         {
@@ -676,7 +765,7 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
             }
             if(!strcasecmp( p_sd->pp_attributes[i]->psz_field , "plgroup"))
             {
-                int i_id;
+                int i_group_id;
                 p_playlist =
                 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                                         FIND_ANYWHERE );
@@ -685,37 +774,36 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
                     return;
                 }
 
-                i_id = playlist_GroupToId( p_playlist,
-                                           p_sd->pp_attributes[i]->psz_value);
-                if( i_id != 0 )
+                i_group_id = playlist_GroupToId( p_playlist,
+                                 p_sd->pp_attributes[i]->psz_value);
+                if( i_group_id != 0 )
                 {
-                    p_item->i_group = i_id;
+                    i_group = i_group_id;
                 }
                 else
                 {
                     playlist_group_t *p_group =
                             playlist_CreateGroup( p_playlist,
                                        p_sd->pp_attributes[i]->psz_value);
-                    p_item->i_group = p_group->i_id;
+                    i_group = p_group->i_id;
                 }
                 vlc_object_release( p_playlist );
             }
         }
 
-
-        /* Filling p_item->psz_uri */
+        /* Filling psz_uri */
         if( b_http == VLC_FALSE )
         {
-            p_item->psz_uri = malloc( strlen( psz_proto ) + strlen( psz_uri ) +
-                                      strlen( psz_port ) + 7 );
+            psz_item_uri = malloc( strlen( psz_proto ) + strlen( psz_uri ) +
+                                 strlen( psz_port ) + 7 );
             if( ismult( psz_uri ) )
             {
-                sprintf( p_item->psz_uri, "%s://@%s:%s",
+                sprintf( psz_item_uri, "%s://@%s:%s",
                          psz_proto, psz_uri, psz_port );
             }
             else
             {
-                sprintf( p_item->psz_uri, "%s://%s:%s",
+                sprintf( psz_item_uri, "%s://%s:%s",
                          psz_proto, psz_uri, psz_port );
             }
         }
@@ -725,11 +813,16 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
             {
                 psz_http_path = strdup( "/" );
             }
-
-            p_item->psz_uri = malloc( strlen( psz_proto ) + strlen( psz_uri ) +
-                                      strlen( psz_port ) + strlen(psz_http_path) + 5 );
-            sprintf( p_item->psz_uri, "%s://%s:%s%s", psz_proto,
-                            psz_uri, psz_port,psz_http_path );
+            if( *psz_http_path == '/' )
+            {
+                asprintf( &psz_item_uri, "%s://%s:%s%s", psz_proto,
+                         psz_uri, psz_port,psz_http_path );
+            }
+            else
+            {
+                asprintf( &psz_item_uri, "%s://%s:%s/%s", psz_proto, psz_uri,
+                          psz_port, psz_http_path );
+            }
 
             if( psz_http_path )
             {
@@ -737,10 +830,92 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
             }
         }
 
-        /* Enqueueing p_item in the playlist */
-        p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
-        playlist_AddItem ( p_playlist, p_item, PLAYLIST_CHECK_INSERT, PLAYLIST_END );
+        /* Check if we already know this item */
+         for( i = 0 ; i< p_intf->p_sys->i_announces ; i++ )
+         {
+            if( !strcmp( p_intf->p_sys->pp_announces[i]->psz_uri,
+                         psz_item_uri ) )
+            {
+                p_intf->p_sys->pp_announces[i]->i_last = mdate();
+
+                /* Check if the name changed */
+                if( strcmp( p_intf->p_sys->pp_announces[i]->psz_name,
+                             p_sd->psz_sessionname ) )
+                {
+                    playlist_item_t *p_item;
+                    p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
+                                                  FIND_ANYWHERE );
+
+                    msg_Dbg(p_intf, "Name changed (%s -> %s) for %s",
+                            p_intf->p_sys->pp_announces[i]->psz_name,
+                            p_sd->psz_sessionname,
+                            psz_item_uri );
+
+                    p_item = playlist_ItemGetById( p_playlist,
+                                    p_intf->p_sys->pp_announces[i]->i_id );
+
+                    /* Change the name in the item */
+                    if( p_item->input.psz_name )
+                        free( p_item->input.psz_name );
+                    p_item->input.psz_name = strdup( p_sd->psz_sessionname);
+
+                    /* Update the stored name */
+                    if( p_intf->p_sys->pp_announces[i]->psz_name )
+                        free( p_intf->p_sys->pp_announces[i]->psz_name );
+                    p_intf->p_sys->pp_announces[i]->psz_name =
+                                   strdup( p_sd->psz_sessionname );
+
+                    vlc_object_release( p_playlist );
+                }
+                free( psz_item_uri );
+                return;
+            }
+        }
+
+        /* Add the item in the playlist */
+        p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
+                                      FIND_ANYWHERE );
+        i_id = playlist_Add ( p_playlist, psz_item_uri ,
+                               p_sd->psz_sessionname,
+                               PLAYLIST_CHECK_INSERT, PLAYLIST_END );
+        p_item = playlist_ItemGetById( p_playlist, i_id );
+        if( p_item )
+        {
+            vlc_mutex_lock( &p_item->input.lock );
+            playlist_ItemSetGroup( p_item, i_group );
+            vlc_mutex_unlock( &p_item->input.lock );
+        }
+
+        /* Then remember it */
+        p_announce = (struct sap_announce_t *)malloc(
+                      sizeof(struct sap_announce_t) );
+        if(  p_sd->psz_sessionname )
+        {
+            p_announce->psz_name = strdup(  p_sd->psz_sessionname );
+        }
+        else
+        {
+            p_announce->psz_name = strdup( "" );
+        }
+        if( psz_item_uri )
+        {
+            p_announce->psz_uri  = strdup( psz_item_uri );
+        }
+        else
+        {
+            p_announce->psz_uri = strdup( "" );
+        }
+        p_announce->i_id = i_id;
+
+        p_announce->i_last = mdate();
+
         vlc_object_release( p_playlist );
+
+        INSERT_ELEM( p_intf->p_sys->pp_announces,
+                     p_intf->p_sys->i_announces,
+                     p_intf->p_sys->i_announces,
+                     p_announce );
+        free( psz_item_uri );
     }
 }
 
@@ -764,7 +939,7 @@ static sess_descr_t *  parse_sdp( intf_thread_t * p_intf, char *p_packet )
     sd->psz_sessionname = NULL;
     sd->psz_connection  = NULL;
     sd->psz_sdp         = strdup( p_packet );
-    
+
     sd->i_media         = 0;
     sd->pp_media        = NULL;
     sd->i_attributes    = 0;
@@ -797,7 +972,7 @@ static sess_descr_t *  parse_sdp( intf_thread_t * p_intf, char *p_packet )
 
         if( p_packet[1] != '=' )
         {
-            msg_Warn( p_intf, "packet invalid" );
+            msg_Warn( p_intf, "invalid packet") ;
             free_sd( sd );
             return NULL;
         }
@@ -876,16 +1051,19 @@ static void free_sd( sess_descr_t * p_sd )
 
     FREE( p_sd->psz_sessionname );
     FREE( p_sd->psz_connection );
+    FREE( p_sd->psz_sdp );
 
     for( i = 0; i < p_sd->i_media ; i++ )
     {
         FREE( p_sd->pp_media[i]->psz_medianame );
         FREE( p_sd->pp_media[i]->psz_mediaconnection );
+        FREE( p_sd->pp_media[i] );
     }
     for( i = 0; i < p_sd->i_attributes ; i++ )
     {
         FREE( p_sd->pp_attributes[i]->psz_field );
         FREE( p_sd->pp_attributes[i]->psz_value );
+        FREE( p_sd->pp_attributes[i] );
     }
     FREE( p_sd->pp_attributes );
     FREE( p_sd->pp_media );