]> git.sesse.net Git - vlc/blobdiff - modules/misc/sap.c
* modules/control/telnet.c: removed translation of "VLM"
[vlc] / modules / misc / sap.c
index 415b0371495c75a84ae5f72bdaee230ca1474f9b..e1981bf4d86a426a67f400516304e6227e5c2ecc 100644 (file)
@@ -1,8 +1,9 @@
+
 /*****************************************************************************
  * sap.c :  SAP interface module
  *****************************************************************************
  * Copyright (C) 2001 VideoLAN
- * $Id: sap.c,v 1.44 2004/01/05 12:59:54 zorglub Exp $
+ * $Id$
  *
  * Authors: Arnaud Schauly <gitan@via.ecp.fr>
  *          ClĂ©ment Stenac <zorglub@via.ecp.fr>
 #define SAP_SCOPE_TEXT N_("IPv6 SAP scope")
 #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.")
 
 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);
 
-    set_description( _("SAP interface") );
     set_capability( "interface", 0 );
     set_callbacks( Open, Close );
 vlc_module_end();
@@ -183,6 +187,8 @@ struct intf_sys_t
     /* Table of announces */
     int i_announces;
     struct sap_announce_t **pp_announces;
+
+    int i_timeout;
 };
 
 #ifdef HAVE_ZLIB_H
@@ -236,6 +242,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" ) )
@@ -257,7 +264,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;
@@ -293,7 +300,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;
@@ -356,6 +363,7 @@ static void Close( vlc_object_t *p_this )
            free( p_sys->pp_announces[i]->psz_uri );
         free( p_sys->pp_announces[i] );
     }
+    free( p_sys->pp_announces );
 
     free( p_sys );
 }
@@ -393,13 +401,14 @@ static void Run( intf_thread_t *p_intf )
         for( i = 0 ; i< p_intf->p_sys->i_announces ; i++ )
         {
            struct sap_announce_t *p_announce;
-           if( mdate() - p_intf->p_sys->pp_announces[i]->i_last > 10000000 )
+           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 );
+               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 */
+               /* Remove the playlist item */
                p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                               FIND_ANYWHERE );
                if( p_playlist )
@@ -640,27 +649,21 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
     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 )
     {
-        psz_uri = malloc( strlen( p_sd->psz_sdp ) + 7 );
-        if( psz_uri == NULL )
-        {
-            msg_Warn( p_intf, "out of memory" );
-            return;
-        }
-        memcpy( psz_uri, "sdp://", 6 );
-        psz_uri += 6;
-        memcpy( psz_uri, p_sd->psz_sdp, strlen( p_sd->psz_sdp ) + 1 );
-
+        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++ )
         {
             if( !strcmp(p_intf->p_sys->pp_announces[i]->psz_uri,
                         psz_uri ) )
             {
-                    return;
+                p_intf->p_sys->pp_announces[i]->i_last = mdate();
+                free(psz_uri);
+                return;
             }
         }
         /* Add it to the playlist */
@@ -668,9 +671,11 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
                                       FIND_ANYWHERE );
         i_id = playlist_Add( p_playlist, psz_uri, p_sd->psz_sessionname ,
                       PLAYLIST_CHECK_INSERT, PLAYLIST_END );
-
-        i_pos = playlist_GetPositionById( p_playlist, i_id );
-        playlist_SetGroup( p_playlist, i_pos, p_intf->p_sys->i_group );
+        if( i_id != -1 )
+        {
+            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(
@@ -692,6 +697,7 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
             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,
@@ -699,6 +705,7 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
                      p_announce );
 
         vlc_object_release( p_playlist );
+        free( psz_uri );
         return;
     }
 
@@ -800,12 +807,16 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
             {
                 psz_http_path = strdup( "/" );
             }
-
-            psz_item_uri = malloc( strlen( psz_proto ) + strlen( psz_uri ) +
-                                   strlen( psz_port ) + strlen(psz_http_path) +
-                                    5 );
-            sprintf( psz_item_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 )
             {
@@ -834,13 +845,13 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
                             p_sd->psz_sessionname,
                             psz_item_uri );
 
-                    p_item = playlist_GetItemById( p_playlist,
+                    p_item = playlist_ItemGetById( p_playlist,
                                     p_intf->p_sys->pp_announces[i]->i_id );
 
                     /* Change the name in the item */
-                    if( p_item->psz_name )
-                        free( p_item->psz_name );
-                    p_item->psz_name = strdup( p_sd->psz_sessionname);
+                    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 )
@@ -850,6 +861,7 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
 
                     vlc_object_release( p_playlist );
                 }
+                free( psz_item_uri );
                 return;
             }
         }
@@ -860,8 +872,13 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
         i_id = playlist_Add ( p_playlist, psz_item_uri ,
                                p_sd->psz_sessionname,
                                PLAYLIST_CHECK_INSERT, PLAYLIST_END );
-        i_pos = playlist_GetPositionById( p_playlist, i_id );
-        playlist_SetGroup( p_playlist, i_pos, i_group );
+        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(
@@ -892,6 +909,7 @@ static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
                      p_intf->p_sys->i_announces,
                      p_intf->p_sys->i_announces,
                      p_announce );
+        free( psz_item_uri );
     }
 }
 
@@ -915,7 +933,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;