]> git.sesse.net Git - vlc/blobdiff - modules/services_discovery/sap.c
avformat: set the "duration/length/ of a packet if known.
[vlc] / modules / services_discovery / sap.c
index beb50068ccc6422c4373257de6204c58667b889b..85f5f9bd9420f263a0d3874bf991cd2ae95c7ed1 100644 (file)
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <assert.h>
 
-#include <vlc_playlist.h>
 #include <vlc_demux.h>
+#include <vlc_services_discovery.h>
 
 #include <vlc_network.h>
 #include <vlc_charset.h>
 #define SAP_PARSE_TEXT N_( "Try to parse the announce" )
 #define SAP_PARSE_LONGTEXT N_( \
        "This enables actual parsing of the announces by the SAP module. " \
-       "Otherwise, all announcements are parsed by the \"livedotcom\" " \
+       "Otherwise, all announcements are parsed by the \"live555\" " \
        "(RTP/RTSP) module." )
 #define SAP_STRICT_TEXT N_( "SAP Strict mode" )
 #define SAP_STRICT_LONGTEXT N_( \
     static void CloseDemux ( vlc_object_t * );
 
 vlc_module_begin();
-    set_shortname( _("SAP"));
-    set_description( _("SAP Announcements") );
+    set_shortname( N_("SAP"));
+    set_description( N_("SAP Announcements") );
     set_category( CAT_PLAYLIST );
     set_subcategory( SUBCAT_PLAYLIST_SD );
 
@@ -151,7 +152,7 @@ vlc_module_begin();
     set_callbacks( Open, Close );
 
     add_submodule();
-        set_description( _("SDP Descriptions parser") );
+        set_description( N_("SDP Descriptions parser") );
         add_shortcut( "sdp" );
         set_capability( "demux", 51 );
         set_callbacks( OpenDemux, CloseDemux );
@@ -211,7 +212,7 @@ struct  sdp_t
 struct attribute_t
 {
     const char *value;
-    char name[0];
+    char name[];
 };
 
 struct sap_announce_t
@@ -226,7 +227,7 @@ struct sap_announce_t
     /* SAP annnounces must only contain one SDP */
     sdp_t       *p_sdp;
 
-    int i_input_id;
+    input_item_t * p_item;
 };
 
 struct services_discovery_sys_t
@@ -393,10 +394,19 @@ static int OpenDemux( vlc_object_t *p_this )
     {
         p_sdp->psz_uri = NULL;
     }
-    if( p_sdp->i_media_type != 33 && p_sdp->i_media_type != 32 &&
-        p_sdp->i_media_type != 14 )
-        goto error;
-
+    switch (p_sdp->i_media_type)
+    {   /* Should be in sync with modules/demux/rtp.c */
+        case  0: /* PCMU/8000 */
+        case  8: /* PCMA/8000 */
+        case 10: /* L16/44100/2 */
+        case 11: /* L16/44100 */
+        case 14: /* MPA/90000 */
+        case 32: /* MPV/90000 */
+        case 33: /* MP2/90000 */
+            break;
+        default:
+            goto error;
+    }
     if( p_sdp->psz_uri == NULL ) goto error;
 
     p_demux->p_sys = (demux_sys_t *)malloc( sizeof(demux_sys_t) );
@@ -471,6 +481,7 @@ static void Run( services_discovery_t *p_sd )
     char *psz_addr;
     int i;
     int timeout = -1;
+    int canc = vlc_savecancel ();
 
     /* Braindead Winsock DNS resolver will get stuck over 2 seconds per failed
      * DNS queries, even if the DNS server returns an error with milliseconds.
@@ -537,10 +548,8 @@ static void Run( services_discovery_t *p_sd )
 
     psz_addr = var_CreateGetString( p_sd, "sap-addr" );
     if( psz_addr && *psz_addr )
-    {
         InitSocket( p_sd, psz_addr, SAP_PORT );
-        free( psz_addr );
-    }
+    free( psz_addr );
 
     if( p_sd->p_sys->i_fd == 0 )
     {
@@ -548,13 +557,12 @@ static void Run( services_discovery_t *p_sd )
         return;
     }
 
-    vlc_object_lock( p_sd );
-
     /* read SAP packets */
-    while( vlc_object_alive( p_sd ) )
+    for (;;)
     {
+        vlc_restorecancel (canc);
         unsigned n = p_sd->p_sys->i_fd;
-        struct pollfd ufd[n+1];
+        struct pollfd ufd[n];
 
         for (unsigned i = 0; i < n; i++)
         {
@@ -563,23 +571,9 @@ static void Run( services_discovery_t *p_sd )
             ufd[i].revents = 0;
         }
 
-        /* Make sure we track vlc_object_signal() */
-        ufd[n].fd = vlc_object_waitpipe( p_sd );
-        ufd[n].events = POLLIN | POLLHUP;
-        ufd[n].revents = 0;
-
-        if( ufd[n].fd == -1 )
-        {
-            /* On windows, fd will be -1, as we can't select on a pipe()-ed
-             * fildes. Because we have no other solution to track that
-             * object is killed, we make sure the timeout won't be to long. */
-            if( timeout > 1000 || timeout == -1 )
-                timeout = 1000;
-        }
-
-        vlc_object_unlock( p_sd );
-
-        if (poll (ufd, n+1, timeout) > 0)
+        int val = poll (ufd, n, timeout);
+        canc = vlc_savecancel ();
+        if (val > 0)
         {
             for (unsigned i = 0; i < n; i++)
             {
@@ -636,10 +630,7 @@ static void Run( services_discovery_t *p_sd )
             timeout = -1; /* We can safely poll indefinitly. */
         else if( timeout < 200 )
             timeout = 200; /* Don't wakeup too fast. */
-
-        vlc_object_lock( p_sd );
     }
-    vlc_object_unlock( p_sd );
 }
 
 /**********************************************************************
@@ -652,7 +643,6 @@ static int Demux( demux_t *p_demux )
     input_thread_t *p_input;
     input_item_t *p_parent_input;
 
-    playlist_t *p_playlist = pl_Yield( p_demux );
     p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT,
                                                  FIND_PARENT );
     assert( p_input );
@@ -662,6 +652,8 @@ static int Demux( demux_t *p_demux )
         return VLC_EGENERIC;
     }
 
+    /* This item hasn't been yield by input_GetItem
+     * don't release it */
     p_parent_input = input_GetItem( p_input );
 
     input_item_SetURI( p_parent_input, p_sdp->psz_uri );
@@ -671,17 +663,7 @@ static int Demux( demux_t *p_demux )
 
     p_parent_input->i_type = ITEM_TYPE_NET;
 
-    if( p_playlist->status.p_item &&
-             p_playlist->status.p_item->p_input == p_parent_input )
-    {
-        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true,
-                          p_playlist->status.p_node, p_playlist->status.p_item );
-    }
-
     vlc_mutex_unlock( &p_parent_input->lock );
-    vlc_object_release( p_input );
-    vlc_object_release( p_playlist );
-
     return VLC_SUCCESS;
 }
 
@@ -807,7 +789,11 @@ static int ParseSAP( services_discovery_t *p_sd, const uint8_t *buf,
             p_sdp->psz_uri = NULL;
     }
 
-    if( p_sdp->psz_uri == NULL ) return VLC_EGENERIC;
+    if( p_sdp->psz_uri == NULL )
+    {
+        FreeSDP( p_sdp );
+        return VLC_EGENERIC;
+    }
 
     for( i = 0 ; i< p_sd->p_sys->i_announces ; i++ )
     {
@@ -833,8 +819,9 @@ static int ParseSAP( services_discovery_t *p_sd, const uint8_t *buf,
                     p_announce->i_period_trust++;
 
                 /* Compute the average period */
-                p_announce->i_period = (p_announce->i_period + (mdate() - p_announce->i_last)) / 2;
-                p_announce->i_last = mdate();
+                mtime_t now = mdate();
+                p_announce->i_period = (p_announce->i_period + (now - p_announce->i_last)) / 2;
+                p_announce->i_last = now;
             }
             FreeSDP( p_sdp ); p_sdp = NULL;
             return VLC_SUCCESS;
@@ -866,12 +853,12 @@ sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
     p_sap->i_hash = i_hash;
     p_sap->p_sdp = p_sdp;
 
-    /* Create the actual playlist item here */
-    p_input = input_ItemNewWithType( VLC_OBJECT(p_sd),
+    /* Released in RemoveAnnounce */
+    p_input = input_item_NewWithType( VLC_OBJECT(p_sd),
                                      p_sap->p_sdp->psz_uri,
                                      p_sdp->psz_sessionname,
                                      0, NULL, -1, ITEM_TYPE_NET );
-    p_sap->i_input_id = p_input->i_id;
+    p_sap->p_item = p_input;
     if( !p_input )
     {
         free( p_sap );
@@ -879,16 +866,16 @@ sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
     }
 
     if( p_sys->b_timeshift )
-        input_ItemAddOption( p_input, ":access-filter=timeshift" );
+        input_item_AddOption( p_input, ":access-filter=timeshift" );
 
     psz_value = GetAttribute( p_sap->p_sdp->pp_attributes, p_sap->p_sdp->i_attributes, "tool" );
     if( psz_value != NULL )
     {
-        input_ItemAddInfo( p_input, _("Session"), _("Tool"), "%s", psz_value );
+        input_item_AddInfo( p_input, _("Session"), _("Tool"), "%s", psz_value );
     }
     if( strcmp( p_sdp->username, "-" ) )
     {
-        input_ItemAddInfo( p_input, _("Session"), _("User"), "%s",
+        input_item_AddInfo( p_input, _("Session"), _("User"), "%s",
                            p_sdp->username );
     }
 
@@ -901,7 +888,7 @@ sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
     services_discovery_AddItem( p_sd, p_input, psz_value /* category name */ );
 
     TAB_APPEND( p_sys->i_announces, p_sys->pp_announces, p_sap );
-    vlc_gc_decref( p_input );
+
     return p_sap;
 }
 
@@ -1222,7 +1209,7 @@ static sdp_t *ParseSDP (vlc_object_t *p_obj, const char *psz_sdp)
                     goto error;
                 }
 
-                if ((sscanf (data, "%63s "I64Fu" "I64Fu" IN IP%u %1023s",
+                if ((sscanf (data, "%63s %"PRIu64" %"PRIu64" IN IP%u %1023s",
                              p_sdp->username, &p_sdp->session_id,
                              &p_sdp->session_version, &p_sdp->orig_ip_version,
                              p_sdp->orig_host) != 5)
@@ -1523,8 +1510,12 @@ static int RemoveAnnounce( services_discovery_t *p_sd,
         p_announce->p_sdp = NULL;
     }
 
-    if( p_announce->i_input_id > -1 )
-        playlist_DeleteFromInput( pl_Get(p_sd), p_announce->i_input_id, false );
+    if( p_announce->p_item )
+    {
+        services_discovery_RemoveItem( p_sd, p_announce->p_item );
+        vlc_gc_decref( p_announce->p_item );
+        p_announce->p_item = NULL;
+    }
 
     for( i = 0; i< p_sd->p_sys->i_announces; i++)
     {