]> git.sesse.net Git - vlc/blobdiff - modules/services_discovery/sap.c
* configure.ac: alsa is now really default enabled
[vlc] / modules / services_discovery / sap.c
index 351525c3e4b4df4fa46706beeb411119344742fe..a923eae355a3ad9832b0b45ae9d9ad0f418a4543 100644 (file)
@@ -32,6 +32,7 @@
 #include <vlc/input.h>
 
 #include "network.h"
+#include "charset.h"
 
 #include <errno.h>                                                 /* ENOMEM */
 
  * Module descriptor
  *****************************************************************************/
 #define SAP_ADDR_TEXT N_( "SAP multicast address" )
-#define SAP_ADDR_LONGTEXT N_( "SAP multicast address" )
+#define SAP_ADDR_LONGTEXT N_( "Listen for SAP announces on another address" )
 #define SAP_IPV4_TEXT N_( "IPv4-SAP listening" )
 #define SAP_IPV4_LONGTEXT N_( \
-      "Set this if you want the SAP module to listen to IPv4 announces" )
+      "Set this if you want the SAP module to listen to IPv4 announces " \
+      "on the standard address" )
 #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" )
+      "Set this if you want the SAP module to listen to IPv6 announces " \
+      "on the standard address" )
 #define SAP_SCOPE_TEXT N_( "IPv6 SAP scope" )
 #define SAP_SCOPE_LONGTEXT N_( \
        "Sets the scope for IPv6 announces (default is 8)" )
 #define SAP_PARSE_LONGTEXT N_( \
        "When SAP can it will try to parse the SAP. If you don't select " \
        "this, all announces will be parsed by the livedotcom module" )
+#define SAP_STRICT_TEXT N_( "SAP Strict mode" )
+#define SAP_STRICT_LONGTEXT N_( \
+       "When this is set, the SAP parser will discard some non-compliant " \
+       "announces" )
 #define SAP_CACHE_TEXT N_("Use SAP cache")
 #define SAP_CACHE_LONGTEXT N_( \
        "If this option is selected, a SAP caching mechanism will be used." \
     static void CloseDemux ( vlc_object_t * );
 
 vlc_module_begin();
-    set_description( _("SAP interface") );
+    set_shortname( _("SAP"));
+    set_description( _("SAP announces") );
+    set_category( CAT_PLAYLIST );
+    set_subcategory( SUBCAT_PLAYLIST_SD );
 
     add_string( "sap-addr", NULL, NULL,
                 SAP_ADDR_TEXT, SAP_ADDR_LONGTEXT, VLC_TRUE );
@@ -110,6 +120,8 @@ vlc_module_begin();
                  SAP_TIMEOUT_TEXT, SAP_TIMEOUT_LONGTEXT, VLC_TRUE );
     add_bool( "sap-parse", 1 , NULL,
                SAP_PARSE_TEXT,SAP_PARSE_LONGTEXT, VLC_TRUE );
+    add_bool( "sap-strict", 0 , NULL,
+               SAP_STRICT_TEXT,SAP_STRICT_LONGTEXT, VLC_TRUE );
     add_bool( "sap-cache", 0 , NULL,
                SAP_CACHE_TEXT,SAP_CACHE_LONGTEXT, VLC_TRUE );
 
@@ -157,6 +169,7 @@ struct  sdp_t
     int         i_in; /* IP version */
 
     int           i_media;
+    int           i_media_type;
 
     int           i_attributes;
     attribute_t  **pp_attributes;
@@ -190,6 +203,9 @@ struct services_discovery_sys_t
     /* playlist node */
     playlist_item_t *p_node;
 
+    /* charset conversion */
+    vlc_iconv_t iconvHandle;
+
     /* Table of announces */
     int i_announces;
     struct sap_announce_t **pp_announces;
@@ -201,6 +217,11 @@ struct services_discovery_sys_t
     int i_timeout;
 };
 
+struct demux_sys_t
+{
+    sdp_t *p_sdp;
+};
+
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
@@ -224,6 +245,8 @@ struct services_discovery_sys_t
 /* Helper functions */
    static char *GetAttribute( sdp_t *p_sdp, const char *psz_search );
    static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 );
+   static char *convert_from_utf8( struct services_discovery_t *p_sd,
+                                   char *psz_unicode );
    static int InitSocket( services_discovery_t *p_sd, char *psz_address, int i_port );
 #ifdef HAVE_ZLIB_H
    static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len );
@@ -246,17 +269,26 @@ static int Open( vlc_object_t *p_this )
 
     playlist_t          *p_playlist;
     playlist_view_t     *p_view;
+    char                *psz_addr, *psz_charset;
+    vlc_value_t         val;
 
     p_sys->i_timeout = config_GetInt( p_sd,"sap-timeout" );
 
+    vlc_current_charset( &psz_charset );
+    p_sys->iconvHandle = vlc_iconv_open( psz_charset, "UTF-8" );
+    free( psz_charset );
+    if( p_sys->iconvHandle == (vlc_iconv_t)(-1) )
+    {
+        msg_Warn( p_sd, "Unable to do requested conversion" );
+    }
+
     p_sd->pf_run = Run;
     p_sd->p_sys  = p_sys;
 
     p_sys->pi_fd = NULL;
     p_sys->i_fd = 0;
 
-    /* FIXME */
-    p_sys->b_strict = VLC_FALSE;
+    p_sys->b_strict = config_GetInt( p_sd, "sap-strict");
     p_sys->b_parse = config_GetInt( p_sd, "sap-parse" );
 
     if( config_GetInt( p_sd, "sap-cache" ) )
@@ -270,10 +302,29 @@ static int Open( vlc_object_t *p_this )
     }
     if( config_GetInt( p_sd, "sap-ipv6" ) )
     {
-        /* TODO */
+        /* [ + 8x4+7*':' + ] */
+        char psz_address[42];
+        char c_scope;
+        char *psz_scope = config_GetPsz( p_sd, "sap-ipv6-scope" );
+
+        if( psz_scope == NULL || *psz_scope == '\0')
+        {
+            c_scope = '8';
+        }
+        else
+        {
+            c_scope = psz_scope[0];
+        }
+        snprintf( psz_address, 42, "[%s%c%s]", IPV6_ADDR_1, c_scope,
+                                               IPV6_ADDR_2 );
+        InitSocket( p_sd, psz_address, SAP_PORT );
     }
 
-    /* TODO : Handle additionnal adresses */
+    psz_addr = config_GetPsz( p_sd, "sap-addr" );
+    if( psz_addr && *psz_addr )
+    {
+        InitSocket( p_sd, psz_addr, SAP_PORT );
+    }
 
     if( p_sys->i_fd == 0 )
     {
@@ -293,6 +344,9 @@ static int Open( vlc_object_t *p_this )
     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
     p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
                                          _("SAP"), p_view->p_root );
+    p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
+    val.b_bool = VLC_TRUE;
+    var_Set( p_playlist, "intf-change", val );
 
     vlc_object_release( p_playlist );
 
@@ -309,28 +363,87 @@ static int OpenDemux( vlc_object_t *p_this )
 {
     demux_t *p_demux = (demux_t *)p_this;
     uint8_t *p_peek;
+    int i_max_sdp = 1024;
+    int i_sdp = 0;
+    char *psz_sdp = NULL;
+    sdp_t *p_sdp = NULL;
 
     /* Probe for SDP */
     if( p_demux->s )
     {
-        if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 )
-        {
-            msg_Err( p_demux, "cannot peek" );
-            return VLC_EGENERIC;
-        }
+        if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
+
         if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) &&
             strncmp( (char*)p_peek, "v=0\n", 4 ) &&
             ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
         {
-            msg_Warn( p_demux, "SDP (UDP) module discarded" );
             return VLC_EGENERIC;
         }
     }
 
+    psz_sdp = (char *)malloc( i_max_sdp );
+    if( !psz_sdp ) return VLC_EGENERIC;
+
+    /* Gather the complete sdp file */
+    for( ;; )
+    {
+        int i_read = stream_Read( p_demux->s,
+                                  &psz_sdp[i_sdp], i_max_sdp - i_sdp - 1 );
+
+        if( i_read < 0 )
+        {
+            msg_Err( p_demux, "failed to read SDP" );
+            goto error;
+        }
+
+        i_sdp += i_read;
+
+        if( i_read < i_max_sdp - i_sdp - 1 )
+        {
+            psz_sdp[i_sdp] = '\0';
+            break;
+        }
+
+        i_max_sdp += 1000;
+        psz_sdp = (uint8_t*)realloc( psz_sdp, i_max_sdp );
+    }
+
+    p_sdp = ParseSDP( VLC_OBJECT(p_demux), psz_sdp );
+
+    if( !p_sdp )
+    {
+        msg_Warn( p_demux, "invalid SDP");
+        goto error;
+    }
+
+    if( p_sdp->i_media > 1 )
+    {
+        goto error;
+    }
+
+    if( ParseConnection( VLC_OBJECT( p_demux ), p_sdp ) )
+    {
+        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;
+
+    if( p_sdp->psz_uri == NULL ) goto error;
+
+    p_demux->p_sys = (demux_sys_t *)malloc( sizeof(demux_sys_t) );
+    p_demux->p_sys->p_sdp = p_sdp;
     p_demux->pf_control = Control;
     p_demux->pf_demux = Demux;
 
+    free( psz_sdp );
     return VLC_SUCCESS;
+
+error:
+    free( psz_sdp );
+    if( p_sdp ) FreeSDP( p_sdp );
+    stream_Seek( p_demux->s, 0 );
+    return VLC_EGENERIC;    
 }
 
 /*****************************************************************************
@@ -364,10 +477,13 @@ static void Close( vlc_object_t *p_this )
 
     if( p_playlist )
     {
-        playlist_NodeDelete( p_playlist, p_sys->p_node, VLC_TRUE );
+        playlist_NodeDelete( p_playlist, p_sys->p_node, VLC_TRUE , VLC_TRUE );
         vlc_object_release( p_playlist );
     }
 
+    if( p_sys->iconvHandle != (vlc_iconv_t)(-1) )
+        vlc_iconv_close( p_sys->iconvHandle );
+
     free( p_sys );
 }
 
@@ -396,6 +512,7 @@ static void Run( services_discovery_t *p_sd )
     /* read SAP packets */
     while( !p_sd->b_die )
     {
+        int i_read;
         p_buffer = (uint8_t *)malloc( MAX_SAP_BUFFER );
 
         if( !p_buffer )
@@ -405,9 +522,9 @@ static void Run( services_discovery_t *p_sd )
             continue;
         }
 
-        int i_read = net_Select( p_sd, p_sd->p_sys->pi_fd, NULL,
-                                 p_sd->p_sys->i_fd, p_buffer,
-                                 MAX_SAP_BUFFER, 500000 );
+        i_read = net_Select( p_sd, p_sd->p_sys->pi_fd, NULL,
+                             p_sd->p_sys->i_fd, p_buffer,
+                             MAX_SAP_BUFFER, 500000 );
 #if 0
         /* Check for items that need deletion */
         for( i = 0 ; i< p_sd->p_sys->i_announces ; i++ )
@@ -440,8 +557,7 @@ static void Run( services_discovery_t *p_sd )
 
               /* Remove the sap_announce from the array */
               REMOVE_ELEM( p_sd->p_sys->pp_announces,
-                           p_sd->p_sys->i_announces,
-                           i );
+                           p_sd->p_sys->i_announces, i );
 
               free( p_announce );
 
@@ -475,62 +591,8 @@ static void Run( services_discovery_t *p_sd )
  **********************************************************************/
 static int Demux( demux_t *p_demux )
 {
-   int i_max_sdp = 1024;
-   int i_sdp = 0;
-   char *psz_sdp = (char *)malloc( i_max_sdp );
-   sdp_t *p_sdp;
-
-   playlist_t *p_playlist;
-
-   if( !psz_sdp )
-   {
-        return -1;
-   }
-
-   /* Gather the complete sdp file */
-   for( ;; )
-   {
-        int i_read = stream_Read( p_demux->s,
-                                  &psz_sdp[i_sdp], i_max_sdp - i_sdp - 1 );
-
-        if( i_read < 0 )
-
-        {
-            msg_Err( p_demux, "failed to read SDP" );
-            return VLC_EGENERIC;
-        }
-
-        i_sdp += i_read;
-
-        if( i_read < i_max_sdp - i_sdp - 1 )
-        {
-            psz_sdp[i_sdp] = '\0';
-            break;
-        }
-
-        i_max_sdp += 1000;
-        psz_sdp = (uint8_t*)realloc( psz_sdp, i_max_sdp );
-   }
-
-   p_sdp = ParseSDP( VLC_OBJECT(p_demux), psz_sdp );
-
-   if( !p_sdp )
-   {
-       msg_Warn( p_demux, "invalid SDP");
-       return -1;
-   }
-
-   if( p_sdp->i_media > 1 )
-   {
-        return -1;
-   }
-
-   if( ParseConnection( VLC_OBJECT( p_demux ), p_sdp ) )
-   {
-       p_sdp->psz_uri = NULL;
-   }
-
-   if( p_sdp->psz_uri == NULL ) return VLC_EGENERIC;
+    sdp_t *p_sdp = p_demux->p_sys->p_sdp;
+    playlist_t *p_playlist;
 
    p_playlist = (playlist_t *)vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
                                                FIND_ANYWHERE );
@@ -541,9 +603,7 @@ static int Demux( demux_t *p_demux )
                  PLAYLIST_APPEND, PLAYLIST_END );
 
    vlc_object_release( p_playlist );
-
-   FreeSDP( p_sdp );
-   free( psz_sdp );
+   if( p_sdp ) FreeSDP( p_sdp );
 
    return VLC_SUCCESS;
 }
@@ -557,10 +617,12 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
  * Local functions
  **************************************************************/
 
+/* i_read is at least > 6 */
 static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
 {
     int                 i_version, i_address_type, i_hash, i;
     uint8_t             *psz_sdp;
+    uint8_t             *psz_initial_sdp;
     sdp_t               *p_sdp;
     vlc_bool_t          b_compressed;
     vlc_bool_t          b_need_delete = VLC_FALSE;
@@ -568,10 +630,10 @@ static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
     int                 i_decompressed_size;
     uint8_t             *p_decompressed_buffer;
 #endif
+    uint8_t             *psz_foo;
 
     /* First, check the sap announce is correct */
     i_version = p_buffer[0] >> 5;
-
     if( i_version != 1 )
     {
        msg_Dbg( p_sd, "strange sap version %d found", i_version );
@@ -608,14 +670,25 @@ static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
     }
 
     psz_sdp  = &p_buffer[4];
+    psz_initial_sdp = psz_sdp;
 
     if( i_address_type == 0 ) /* ipv4 source address */
     {
         psz_sdp += 4;
+        if( i_read <= 9 )
+        {
+            msg_Warn( p_sd,"too short SAP packet\n" );
+            return VLC_EGENERIC;
+        }
     }
     else /* ipv6 source address */
     {
         psz_sdp += 16;
+        if( i_read <= 21 )
+        {
+            msg_Warn( p_sd,"too short SAP packet\n" );
+            return VLC_EGENERIC;
+        }
     }
 
     if( b_compressed )
@@ -636,12 +709,22 @@ static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
     }
 
     /* Add the size of authentification info */
+    if( i_read < p_buffer[1] + (psz_sdp - psz_initial_sdp ) )
+    {
+        msg_Warn( p_sd, "too short SAP packet\n");
+        return VLC_EGENERIC;
+    }
     psz_sdp += p_buffer[1];
+    psz_foo = psz_sdp;
 
     /* Skip payload type */
     /* Handle announces without \0 between SAP and SDP */
     while( *psz_sdp != '\0' && ( psz_sdp[0] != 'v' && psz_sdp[1] != '=' ) )
     {
+        if( psz_sdp - psz_initial_sdp >= i_read - 5 )
+        {
+            msg_Warn( p_sd, "empty SDP ?");
+        }
         psz_sdp++;
     }
 
@@ -649,7 +732,15 @@ static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
     {
         psz_sdp++;
     }
-
+    if( psz_sdp != psz_foo && strcasecmp( psz_foo, "application/sdp" ) )
+    {
+        msg_Dbg( p_sd, "unhandled content type: %s", psz_foo );        
+    }
+    if( psz_sdp -p_buffer >= i_read )
+    {
+        msg_Warn( p_sd, "package without content" );
+        return VLC_EGENERIC;
+    }
 
     /* Parse SDP info */
     p_sdp = ParseSDP( VLC_OBJECT(p_sd), psz_sdp );
@@ -660,20 +751,21 @@ static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
     }
 
     /* Decide whether we should add a playlist item for this SDP */
+    /* Parse connection information (c= & m= ) */
+    if( ParseConnection( VLC_OBJECT(p_sd), p_sdp ) )
+    {
+        p_sdp->psz_uri = NULL;
+    }
 
     /* Multi-media or no-parse -> pass to LIVE.COM */
-    if( p_sdp->i_media > 1 || p_sd->p_sys->b_parse == VLC_FALSE )
+    if( p_sdp->i_media > 1 || ( p_sdp->i_media_type != 14 &&
+                                p_sdp->i_media_type != 32 &&
+                                p_sdp->i_media_type != 33) ||
+        p_sd->p_sys->b_parse == VLC_FALSE )
     {
+        if( p_sdp->psz_uri ) free( p_sdp->psz_uri );
         asprintf( &p_sdp->psz_uri, "sdp://%s", p_sdp->psz_sdp );
     }
-    else
-    {
-        /* Parse connection information (c= & m= ) */
-        if( ParseConnection( VLC_OBJECT(p_sd), p_sdp ) )
-        {
-            p_sdp->psz_uri = NULL;
-        }
-    }
 
     if( p_sdp->psz_uri == NULL ) return VLC_EGENERIC;
 
@@ -715,9 +807,13 @@ sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
     char                *psz_value;
     sap_announce_t *p_sap = (sap_announce_t *)malloc(
                                         sizeof(sap_announce_t ) );
-    if( !p_sap )
+
+    psz_value = convert_from_utf8( p_sd, p_sdp->psz_sessionname );
+    if( p_sap == NULL || psz_value == NULL )
     {
         msg_Err( p_sd, "out of memory");
+        FREE( p_sap );
+        FREE( psz_value );
         p_sd->b_die = VLC_TRUE;
         return NULL;
     }
@@ -727,14 +823,26 @@ sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
     p_sap->p_item = NULL;
 
     /* Create the playlist item here */
-    p_item = playlist_ItemNew( p_sd, p_sap->p_sdp->psz_uri,
-                               p_sap->p_sdp->psz_sessionname );
+    p_item = playlist_ItemNew( p_sd, p_sap->p_sdp->psz_uri, psz_value );
+    free( psz_value );
 
     if( !p_item )
     {
         return NULL;
     }
 
+    psz_value = GetAttribute( p_sap->p_sdp, "tool" );
+    if( psz_value != NULL )
+    {
+        vlc_input_item_AddInfo( &p_item->input, _("Session"),
+                                _("Tool"), psz_value );
+    }
+    if( strcmp( p_sdp->psz_username, "-" ) )
+    {
+        vlc_input_item_AddInfo( &p_item->input, _("Session"),
+                                _("User"), p_sdp->psz_username );
+    }
+
     psz_value = GetAttribute( p_sap->p_sdp, "x-plgroup" );
 
     if( psz_value == NULL )
@@ -754,12 +862,24 @@ sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
 
     if( psz_value != NULL )
     {
-        p_child = playlist_ChildSearchName( p_sd->p_sys->p_node, psz_value );
+        char *psz_grp = convert_from_utf8( p_sd, psz_value );
 
-        if( p_child == NULL )
+        if( psz_grp != NULL )
         {
-            p_child = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
-                                           psz_value, p_sd->p_sys->p_node );
+            p_child = playlist_ChildSearchName( p_sd->p_sys->p_node,
+                                                psz_grp );
+
+            if( p_child == NULL )
+                p_child = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
+                                               psz_grp, p_sd->p_sys->p_node );
+            free( psz_grp );
+        }
+        else
+        {
+            vlc_object_release( p_playlist );
+            msg_Err( p_sd, "out of memory");
+            free( p_sap );
+            return NULL;
         }
     }
     else
@@ -768,6 +888,7 @@ sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
     }
 
     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
+    p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
 
     playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_child,
                           PLAYLIST_APPEND, PLAYLIST_END );
@@ -777,8 +898,7 @@ sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
     p_sap->p_item = p_item;
 
     TAB_APPEND( p_sd->p_sys->i_announces,
-                p_sd->p_sys->pp_announces,
-                p_sap );
+                p_sd->p_sys->pp_announces, p_sap );
 
     return p_sap;
 }
@@ -859,7 +979,7 @@ static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
         }
         else
         {
-            msg_Dbg( p_obj, "incorrect c field");
+            msg_Dbg( p_obj, "incorrect c field, %s", p_sdp->psz_connection );
         }
         psz_uri = strdup( psz_parse );
 
@@ -921,12 +1041,18 @@ static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
         if( psz_eof )
         {
             *psz_eof = '\0';
+            psz_proto = strdup( psz_parse );
+
+            psz_parse = psz_eof + 1;
+            p_sdp->i_media_type = atoi( psz_parse );
+            
         }
         else
         {
-            msg_Dbg( p_obj, "incorrect m field");
+            msg_Dbg( p_obj, "incorrect m field, %s", p_sdp->psz_media );
+            p_sdp->i_media_type = 33;
+            psz_proto = strdup( psz_parse );
         }
-        psz_proto = strdup( psz_parse );
     }
 
     if( psz_proto && !strncmp( psz_proto, "RTP/AVP", 7 ) )
@@ -939,7 +1065,6 @@ static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
         free( psz_proto );
         psz_proto = strdup( "udp" );
     }
-                    
 
     /* FIXME: HTTP support */
 
@@ -970,7 +1095,7 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
 {
     sdp_t *p_sdp;
     vlc_bool_t b_invalid = VLC_FALSE;
-
+    vlc_bool_t b_end = VLC_FALSE;
     if( psz_sdp == NULL )
     {
         return NULL;
@@ -978,7 +1103,7 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
 
     if( psz_sdp[0] != 'v' || psz_sdp[1] != '=' )
     {
-        msg_Warn( p_obj, "bad SDP packet" );
+        msg_Warn( p_obj, "Bad packet" );
         return NULL;
     }
 
@@ -997,7 +1122,7 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
     p_sdp->i_attributes    = 0;
     p_sdp->pp_attributes   = NULL;
 
-    while( *psz_sdp != '\0'  )
+    while( *psz_sdp != '\0' && b_end == VLC_FALSE  )
     {
         char *psz_eol;
         char *psz_eof;
@@ -1013,6 +1138,7 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
         if( ( psz_eol = strchr( psz_sdp, '\n' ) ) == NULL )
         {
             psz_eol = psz_sdp + strlen( psz_sdp );
+            b_end = VLC_TRUE;
         }
         if( psz_eol > psz_sdp && *( psz_eol - 1 ) == '\r' )
         {
@@ -1059,6 +1185,10 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
                     { \
                         b_invalid = VLC_TRUE; break; \
                     } \
+                    else \
+                    { \
+                        store = strdup( psz_parse ); \
+                    } \
                 }; \
                 psz_parse = psz_eof + 1; i_field++;
 
@@ -1090,9 +1220,7 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
             case( 'a' ): /* attribute */
             {
                 char *psz_eon = strchr( &psz_sdp[2], ':' );
-
-                 attribute_t *p_attr = (attribute_t *)malloc(
-                                        sizeof( attribute_t ) );
+                attribute_t *p_attr = malloc( sizeof( attribute_t ) );
 
                 /* Attribute with value */
                 if( psz_eon )
@@ -1150,10 +1278,41 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
 }
 
 
+static char *convert_from_utf8( struct services_discovery_t *p_sd,
+                                char *psz_unicode )
+{
+    char *psz_local, *psz_in, *psz_out;
+    size_t ret, i_in, i_out;
+
+    if( psz_unicode == NULL )
+        return NULL;
+    if ( p_sd->p_sys->iconvHandle == (vlc_iconv_t)(-1) )
+        return strdup( psz_unicode );
+
+    psz_in = psz_unicode;
+    i_in = strlen( psz_unicode );
+
+    i_out = 2 * i_in;
+    psz_local = malloc( i_out + 1 );
+    if( psz_local == NULL )
+        return strdup( psz_unicode );
+    psz_out = psz_local;
+
+    ret = vlc_iconv( p_sd->p_sys->iconvHandle,
+                     &psz_in, &i_in, &psz_out, &i_out);
+    if( ret == (size_t)(-1) || i_in )
+    {
+        msg_Warn( p_sd, "Failed to convert \"%s\" from UTF-8", psz_unicode );
+        return strdup( psz_unicode );
+    }
+    *psz_out = '\0';
+    return psz_local;
+}
+
+
 /***********************************************************************
  * ismult: returns true if we have a multicast address
  ***********************************************************************/
-
 static int ismult( char *psz_uri )
 {
     char *psz_end;
@@ -1176,16 +1335,15 @@ static int ismult( char *psz_uri )
     return( i_value < 224 ? VLC_FALSE : VLC_TRUE );
 }
 
-static int InitSocket( services_discovery_t *p_sd, char *psz_address, int i_port )
+static int InitSocket( services_discovery_t *p_sd, char *psz_address,
+                       int i_port )
 {
     int i_fd = net_OpenUDP( p_sd, psz_address, i_port, "", 0 );
 
     if( i_fd != -1 )
     {
-        INSERT_ELEM(  p_sd->p_sys->pi_fd,
-                      p_sd->p_sys->i_fd,
-                      p_sd->p_sys->i_fd,
-                      i_fd );
+        INSERT_ELEM(  p_sd->p_sys->pi_fd, p_sd->p_sys->i_fd,
+                      p_sd->p_sys->i_fd, i_fd );
         return VLC_SUCCESS;
     }
 
@@ -1281,19 +1439,13 @@ static int RemoveAnnounce( services_discovery_t *p_sd,
     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
                                           VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
 
-    if( p_announce->p_sdp )
-    {
-        FreeSDP( p_announce->p_sdp );
-    }
+    if( p_announce->p_sdp ) FreeSDP( p_announce->p_sdp );
 
-    if( !p_playlist )
-    {
-        return VLC_EGENERIC;
-    }
+    if( !p_playlist ) return VLC_EGENERIC;
 
     if( p_announce->p_item )
     {
-        playlist_Delete( p_playlist, p_announce->p_item->input.i_id );
+        playlist_LockDelete( p_playlist, p_announce->p_item->input.i_id );
     }
 
     for( i = 0; i< p_sd->p_sys->i_announces; i++)
@@ -1322,8 +1474,7 @@ static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 )
         p_sdp1->psz_address_type && p_sdp2->psz_address_type &&
         p_sdp1->psz_address &&  p_sdp2->psz_address )
     {
-        if(
-           !strcmp( p_sdp1->psz_username , p_sdp2->psz_username ) &&
+        if(!strcmp( p_sdp1->psz_username , p_sdp2->psz_username ) &&
            !strcmp( p_sdp1->psz_network_type , p_sdp2->psz_network_type ) &&
            !strcmp( p_sdp1->psz_address_type , p_sdp2->psz_address_type ) &&
            !strcmp( p_sdp1->psz_address , p_sdp2->psz_address ) &&