]> git.sesse.net Git - vlc/blobdiff - src/playlist/services_discovery.c
Don't unlock the playlist to lock it the next instruction.
[vlc] / src / playlist / services_discovery.c
index 0f4d72db4fcd5db652647d350435092eb6935fbb..4eecba32c0d54ff8936756cc921bb5cf8830ba92 100644 (file)
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <assert.h>
+
+#include <vlc_common.h>
 #include "vlc_playlist.h"
 #include "vlc_events.h"
 #include "playlist_internal.h"
+#include "../libvlc.h"
 
 static void RunSD( services_discovery_t *p_sd );
 
@@ -50,7 +56,9 @@ char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
 services_discovery_t *
 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
 {
-    services_discovery_t *p_sd = vlc_object_create( p_super, VLC_OBJECT_SD );
+    services_discovery_t *p_sd;
+    p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
+                              "services discovery" );
     if( !p_sd )
         return NULL;
 
@@ -62,17 +70,21 @@ services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name
             vlc_ServicesDiscoveryItemAdded );
     vlc_event_manager_register_event_type( &p_sd->event_manager,
             vlc_ServicesDiscoveryItemRemoved );
+    vlc_event_manager_register_event_type( &p_sd->event_manager,
+            vlc_ServicesDiscoveryStarted );
+    vlc_event_manager_register_event_type( &p_sd->event_manager,
+            vlc_ServicesDiscoveryEnded );
 
-    p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, VLC_TRUE );
+    p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, true );
 
     if( p_sd->p_module == NULL )
     {
         msg_Err( p_super, "no suitable services discovery module" );
-        vlc_object_destroy( p_sd );
+        vlc_object_release( p_sd );
         return NULL;
     }
     p_sd->psz_module = strdup( psz_module_name );
-    p_sd->b_die = VLC_FALSE; /* FIXME */
+    p_sd->b_die = false; /* FIXME */
 
     vlc_object_attach( p_sd, p_super );
     return p_sd;
@@ -89,7 +101,7 @@ void services_discovery_Destroy ( services_discovery_t * p_sd )
     free( p_sd->psz_localized_name );
 
     vlc_object_detach( p_sd );
-    vlc_object_destroy( p_sd );
+    vlc_object_release( p_sd );
 }
 
 /***********************************************************************
@@ -99,10 +111,10 @@ int services_discovery_Start ( services_discovery_t * p_sd )
 {
     if ((p_sd->pf_run != NULL)
         && vlc_thread_create( p_sd, "services_discovery", RunSD,
-                              VLC_THREAD_PRIORITY_LOW, VLC_FALSE))
+                              VLC_THREAD_PRIORITY_LOW, false))
     {
         msg_Err( p_sd, "cannot create services discovery thread" );
-        vlc_object_destroy( p_sd );
+        vlc_object_release( p_sd );
         return VLC_EGENERIC;
     }
     return VLC_SUCCESS;
@@ -134,7 +146,7 @@ services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
 void
 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
 {
-    if(p_sd->psz_localized_name) free( p_sd->psz_localized_name );
+    free( p_sd->psz_localized_name );
     p_sd->psz_localized_name = strdup(psz);
 }
 
@@ -180,7 +192,15 @@ services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_it
  ***********************************************************************/
 static void RunSD( services_discovery_t *p_sd )
 {
+    vlc_event_t event;
+
+    event.type = vlc_ServicesDiscoveryStarted;
+    vlc_event_send( &p_sd->event_manager, &event );
+
     p_sd->pf_run( p_sd );
+
+    event.type = vlc_ServicesDiscoveryEnded;
+    vlc_event_send( &p_sd->event_manager, &event );
     return;
 }
 
@@ -194,31 +214,37 @@ static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_dat
     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
     playlist_item_t *p_new_item, * p_parent = user_data;
+    playlist_t * p_playlist = p_parent->p_playlist;
 
-    msg_Dbg( p_parent->p_playlist, "Adding %s in %s",
+    msg_Dbg( p_playlist, "Adding %s in %s",
                 p_input->psz_name ? p_input->psz_name : "(null)",
                 psz_cat ? psz_cat : "(null)" );
 
+    PL_LOCK;
     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
     if( !EMPTY_STR(psz_cat) &&
-        p_parent->p_parent == p_parent->p_playlist->p_root_category )
+        p_parent->p_parent == p_playlist->p_root_category )
     {
         /* */
         playlist_item_t * p_cat;
         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
         if( !p_cat )
         {
-            p_cat = playlist_NodeCreate( p_parent->p_playlist, psz_cat,
+            p_cat = playlist_NodeCreate( p_playlist, psz_cat,
                                          p_parent, 0, NULL );
             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
         }
         p_parent = p_cat;
     }
 
-    p_new_item = playlist_NodeAddInput( p_parent->p_playlist, p_input, p_parent,
-                                        PLAYLIST_APPEND, PLAYLIST_END, VLC_FALSE );
-    p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
-    p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
+    p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
+                                        PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
+    if( p_new_item )
+    {
+        p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
+        p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
+    }
+    PL_UNLOCK;
 }
 
  /* A new item has been removed from a certain sd */
@@ -232,19 +258,20 @@ static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_d
      * XXX: Why don't we have a function to ensure that in the playlist code ? */
     vlc_object_lock( p_parent->p_playlist );
     p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
-            p_input->i_id, p_parent, VLC_FALSE );
+            p_input->i_id, p_parent, false );
 
     if( p_pl_item && p_pl_item->i_children > -1 )
     {
-        playlist_NodeDelete( p_parent->p_playlist, p_pl_item, VLC_TRUE, VLC_FALSE );
+        playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
         vlc_object_unlock( p_parent->p_playlist );
         return;
     }
-    vlc_object_unlock( p_parent->p_playlist );
 
     /* Delete the non-node item normally */
     playlist_DeleteInputInParent( p_parent->p_playlist, p_input->i_id,
-                                  p_parent, VLC_FALSE );
+                                  p_parent, true );
+
+    vlc_object_unlock( p_parent->p_playlist );
 }
 
 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modules )
@@ -283,8 +310,10 @@ int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modu
 
         char * psz = services_discovery_GetLocalizedName( p_sd );
         assert( psz );
+        PL_LOCK;
         playlist_NodesPairCreate( p_playlist, psz,
-                &p_cat, &p_one, VLC_FALSE );
+                &p_cat, &p_one, false );
+        PL_UNLOCK;
         free( psz );
 
         vlc_event_attach( services_discovery_EventManager( p_sd ),
@@ -312,10 +341,7 @@ int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modu
         /* Free in playlist_ServicesDiscoveryRemove */
         p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
         if( !p_sds )
-        {
-            msg_Err( p_playlist, "No more memory" );
             return VLC_ENOMEM;
-        }
         p_sds->p_sd = p_sd;
         p_sds->p_one = p_one;
         p_sds->p_cat = p_cat;
@@ -379,8 +405,8 @@ int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
     if( p_sds->p_cat != p_playlist->p_root_category &&
         p_sds->p_one != p_playlist->p_root_onelevel )
     {
-        playlist_NodeDelete( p_playlist, p_sds->p_cat, VLC_TRUE, VLC_FALSE );
-        playlist_NodeDelete( p_playlist, p_sds->p_one, VLC_TRUE, VLC_FALSE );
+        playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
+        playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
     }
     PL_UNLOCK;
 
@@ -389,7 +415,7 @@ int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
     return VLC_SUCCESS;
 }
 
-vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
+bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
                                               const char *psz_module )
 {
     int i;
@@ -400,10 +426,17 @@ vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
         {
             PL_UNLOCK;
-            return VLC_TRUE;
+            return true;
         }
     }
     PL_UNLOCK;
-    return VLC_FALSE;
+    return false;
+}
+
+void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
+{
+    while( p_playlist->i_sds > 0 )
+        playlist_ServicesDiscoveryRemove( p_playlist,
+                                     p_playlist->pp_sds[0]->p_sd->psz_module );
 }