]> git.sesse.net Git - vlc/blobdiff - src/playlist/control.c
playlist: refactor a bit and fix a crash
[vlc] / src / playlist / control.c
index 313a14506dbf966155ae66a8063ac6367f7c7c4c..3faa47293dd827cf268b578e93bde7f8a9270b7d 100644 (file)
@@ -39,22 +39,34 @@ static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args
  * Playlist control
  *****************************************************************************/
 
-playlist_t *__pl_Hold( vlc_object_t *p_this )
+static vlc_mutex_t global_lock = VLC_STATIC_MUTEX;
+
+#undef pl_Hold
+playlist_t *pl_Hold (vlc_object_t *obj)
 {
     playlist_t *pl;
+    libvlc_int_t *p_libvlc = obj->p_libvlc;
 
-    barrier();
-    pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
+    vlc_mutex_lock (&global_lock);
+    pl = libvlc_priv (p_libvlc)->p_playlist;
+    assert (pl != NULL);
 
-    assert( VLC_OBJECT(pl) != p_this /* This does not make sense to hold the playlist
-    using pl_Hold. use vlc_object_hold in this case */ );
+    if (!libvlc_priv (p_libvlc)->playlist_active)
+    {
+         playlist_Activate (pl);
+         libvlc_priv (p_libvlc)->playlist_active = true;
+    }
 
+    /* The playlist should hold itself with vlc_object_hold() if ever. */
+    assert (VLC_OBJECT (pl) != obj);
     if (pl)
         vlc_object_hold (pl);
+    vlc_mutex_unlock (&global_lock);
     return pl;
 }
 
-void __pl_Release( vlc_object_t *p_this )
+#undef pl_Release
+void pl_Release( vlc_object_t *p_this )
 {
     playlist_t *pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
     assert( pl != NULL );
@@ -66,6 +78,14 @@ void __pl_Release( vlc_object_t *p_this )
     vlc_object_release( pl );
 }
 
+void pl_Deactivate (libvlc_int_t *p_libvlc)
+{
+    vlc_mutex_lock (&global_lock);
+    if (libvlc_priv (p_libvlc)->playlist_active)
+        playlist_Deactivate (libvlc_priv (p_libvlc)->p_playlist);
+    vlc_mutex_unlock (&global_lock);
+}
+
 void playlist_Lock( playlist_t *pl )
 {
     vlc_mutex_lock( &pl_priv(pl)->lock );
@@ -104,7 +124,7 @@ static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args
     if( !vlc_object_alive( p_playlist ) )
         return VLC_EGENERIC;
 
-    if( playlist_IsEmpty( p_playlist ) )
+    if( playlist_IsEmpty( p_playlist ) && i_query != PLAYLIST_STOP )
         return VLC_EGENERIC;
 
     switch( i_query )
@@ -151,22 +171,21 @@ static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args
         break;
 
     case PLAYLIST_PAUSE:
-        if( pl_priv(p_playlist)->p_input &&
-            var_GetInteger( pl_priv(p_playlist)->p_input, "state" ) == PAUSE_S )
+        if( !pl_priv(p_playlist)->p_input )
+        {    /* FIXME: is this really useful without input? */
+             pl_priv(p_playlist)->status.i_status = PLAYLIST_PAUSED;
+             break;
+        }
+
+        if( var_GetInteger( pl_priv(p_playlist)->p_input, "state" ) == PAUSE_S )
         {
             pl_priv(p_playlist)->status.i_status = PLAYLIST_RUNNING;
-            if( pl_priv(p_playlist)->p_input )
-            {
-                var_SetInteger( pl_priv(p_playlist)->p_input, "state", PLAYING_S );
-            }
+            var_SetInteger( pl_priv(p_playlist)->p_input, "state", PLAYING_S );
         }
         else
         {
             pl_priv(p_playlist)->status.i_status = PLAYLIST_PAUSED;
-            if( pl_priv(p_playlist)->p_input )
-            {
-                var_SetInteger( pl_priv(p_playlist)->p_input, "state", PAUSE_S );
-            }
+            var_SetInteger( pl_priv(p_playlist)->p_input, "state", PAUSE_S );
         }
         break;