]> git.sesse.net Git - vlc/blobdiff - src/control/media_list_player.c
libvlc: ref_count should only be accessed in the lock.
[vlc] / src / control / media_list_player.c
index 10552673d42a2582f4577a72feb0f744ded377db..e532dec0336d1fa65f1622e534576ca7e6c7401e 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 "libvlc_internal.h"
+
 #include <vlc/libvlc.h>
+#include <vlc/libvlc_media.h>
+#include <vlc/libvlc_media_list.h>
+#include <vlc/libvlc_media_player.h>
+#include <vlc/libvlc_media_list_player.h>
+#include <vlc/libvlc_events.h>
+
+#include "libvlc_internal.h"
+
+#include "media_internal.h" // Abuse, could and should be removed
 #include "media_list_path.h"
 
+//#define DEBUG_MEDIA_LIST_PLAYER
+
+struct libvlc_media_list_player_t
+{
+    libvlc_event_manager_t *    p_event_manager;
+    libvlc_instance_t *         p_libvlc_instance;
+    int                         i_refcount;
+    vlc_mutex_t                 object_lock;
+    libvlc_media_list_path_t    current_playing_item_path;
+    libvlc_media_t *            p_current_playing_item;
+    libvlc_media_list_t *       p_mlist;
+    libvlc_media_player_t *     p_mi;
+};
+
 /*
  * Private functions
  */
@@ -43,7 +66,9 @@ get_next_path( libvlc_media_list_player_t * p_mlp )
 
     if ( !p_mlp->current_playing_item_path )
     {
-        p_mlp->current_playing_item_path = libvlc_media_list_path_empty();
+        if( !libvlc_media_list_count( p_mlp->p_mlist, NULL ) )
+            return NULL;
+        return libvlc_media_list_path_with_root_index(0);
     }
     
     p_sublist_of_playing_item = libvlc_media_list_sublist_at_path(
@@ -62,23 +87,26 @@ get_next_path( libvlc_media_list_player_t * p_mlp )
                             p_mlp->p_mlist,
                             p_mlp->current_playing_item_path );
 
-    int deepness = libvlc_media_list_path_deepness( p_mlp->current_playing_item_path );
-    if( deepness < 1 || !p_parent_of_playing_item )
+    int depth = libvlc_media_list_path_depth( p_mlp->current_playing_item_path );
+    if( depth < 1 || !p_parent_of_playing_item )
         return NULL;
 
     ret = libvlc_media_list_path_copy( p_mlp->current_playing_item_path );
 
-    while( ret[deepness-1] >= libvlc_media_list_count( p_parent_of_playing_item, NULL ) )
+    ret[depth-1]++; // Play next element
+
+    /* If this goes beyong the end of the list */
+    while( ret[depth-1] >= libvlc_media_list_count( p_parent_of_playing_item, NULL ) )
     {
-        deepness--;
-        if( deepness <= 0 )
+        depth--;
+        if( depth <= 0 )
         {
             free( ret );
             libvlc_media_list_release( p_parent_of_playing_item );
             return NULL;
         }
-        ret[deepness] = -1;
-        ret[deepness-1]++;
+        ret[depth] = -1;
+        ret[depth-1]++;
         p_parent_of_playing_item  = libvlc_media_list_parentlist_at_path(
                                         p_mlp->p_mlist,
                                         ret );
@@ -113,7 +141,10 @@ media_player_reached_end( const libvlc_event_t * p_event,
     }
     libvlc_media_release( p_md );
     libvlc_media_release( p_current_md );
-    libvlc_media_list_player_next( p_mlp, NULL );
+    libvlc_exception_t e;
+    libvlc_exception_init(&e);
+    libvlc_media_list_player_next(p_mlp, &e);
+    libvlc_exception_clear(&e); // Don't worry if there was an error
 }
 
 /**************************************************************************
@@ -169,7 +200,7 @@ uninstall_playlist_observer( libvlc_media_list_player_t * p_mlp )
 static void
 install_media_player_observer( libvlc_media_list_player_t * p_mlp )
 {
-    libvlc_event_attach( libvlc_media_player_event_manager( p_mlp->p_mi, NULL ),
+    libvlc_event_attach_async( libvlc_media_player_event_manager( p_mlp->p_mi, NULL ),
                          libvlc_MediaPlayerEndReached,
                           media_player_reached_end, p_mlp, NULL );
 }
@@ -182,9 +213,7 @@ static void
 uninstall_media_player_observer( libvlc_media_list_player_t * p_mlp )
 {
     if ( !p_mlp->p_mi )
-    {
         return;
-    }
 
     libvlc_event_detach( libvlc_media_player_event_manager( p_mlp->p_mi, NULL ),
                          libvlc_MediaPlayerEndReached,
@@ -204,12 +233,15 @@ set_current_playing_item( libvlc_media_list_player_t * p_mlp,
     VLC_UNUSED(p_e);
 
     libvlc_media_t * p_md;
-    
-    p_md = libvlc_media_list_item_at_path( p_mlp->p_mlist, path ); 
+
+    p_md = libvlc_media_list_item_at_path( p_mlp->p_mlist, path );
     vlc_mutex_lock( &p_mlp->object_lock );
-    
-    free( p_mlp->current_playing_item_path );
-    p_mlp->current_playing_item_path = path;
+
+    if( p_mlp->current_playing_item_path != path )
+    {
+        free( p_mlp->current_playing_item_path );
+        p_mlp->current_playing_item_path = path;
+    }
 
     if( !p_md )
     {
@@ -256,10 +288,16 @@ libvlc_media_list_player_new( libvlc_instance_t * p_instance,
     (void)p_e;
     libvlc_media_list_player_t * p_mlp;
     p_mlp = malloc(sizeof(libvlc_media_list_player_t));
+    if( !p_mlp )
+        return NULL;
+
+    libvlc_retain( p_instance );
+    p_mlp->p_libvlc_instance = p_instance;
+    p_mlp->i_refcount = 0;
+    vlc_mutex_init( &p_mlp->object_lock );
     p_mlp->current_playing_item_path = NULL;
-    p_mlp->p_mi = NULL;
     p_mlp->p_mlist = NULL;
-    vlc_mutex_init( &p_mlp->object_lock );
+    p_mlp->p_mi = NULL;
     p_mlp->p_event_manager = libvlc_event_manager_new( p_mlp,
                                                        p_instance,
                                                        p_e );
@@ -274,7 +312,32 @@ libvlc_media_list_player_new( libvlc_instance_t * p_instance,
  **************************************************************************/
 void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
 {
-    free(p_mlp);
+    if( !p_mlp )
+        return;
+
+    vlc_mutex_lock( &p_mlp->object_lock );
+
+    p_mlp->i_refcount--;
+    if( p_mlp->i_refcount > 0 )
+    {
+        vlc_mutex_unlock( &p_mlp->object_lock );
+        return;
+    }
+    vlc_mutex_unlock( &p_mlp->object_lock );
+    vlc_mutex_destroy( &p_mlp->object_lock );
+
+    libvlc_event_manager_release( p_mlp->p_event_manager );
+    libvlc_media_player_release( p_mlp->p_mi );
+
+    if( p_mlp->p_mlist )
+    {
+        uninstall_playlist_observer( p_mlp );
+        libvlc_media_list_release( p_mlp->p_mlist );
+    }
+
+    free( p_mlp->current_playing_item_path );
+    libvlc_release( p_mlp->p_libvlc_instance );
+    free( p_mlp );
 }
 
 /**************************************************************************
@@ -311,7 +374,14 @@ void libvlc_media_list_player_set_media_list(
                                      libvlc_exception_t * p_e )
 {
     vlc_mutex_lock( &p_mlp->object_lock );
+
+    if(!p_mlist)
+    {
+        libvlc_exception_raise( p_e, "No media list provided");
+        vlc_mutex_unlock( &p_mlp->object_lock );
+        return;
+    }
+
     if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
     {
         libvlc_media_player_stop( p_mlp->p_mi, p_e );
@@ -379,7 +449,7 @@ libvlc_media_list_player_get_state( libvlc_media_list_player_t * p_mlp,
                                     libvlc_exception_t * p_e )
 {
     if( !p_mlp->p_mi )
-        return libvlc_Stopped;
+        return libvlc_Ended;
     return libvlc_media_player_get_state( p_mlp->p_mi, p_e );
 }
 
@@ -434,7 +504,10 @@ void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
 {
     if ( p_mlp->p_mi )
     {
-        libvlc_media_player_stop( p_mlp->p_mi, p_e );        
+        /* We are not interested in getting media stop event now */
+        uninstall_media_player_observer( p_mlp );
+        libvlc_media_player_stop( p_mlp->p_mi, p_e );
+        install_media_player_observer( p_mlp );
     }
 
     vlc_mutex_lock( &p_mlp->object_lock );
@@ -448,7 +521,7 @@ void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
  **************************************************************************/
 void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
                                     libvlc_exception_t * p_e )
-{    
+{
     libvlc_media_list_path_t path;
 
     if (! p_mlp->p_mlist )
@@ -456,11 +529,16 @@ void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
         libvlc_exception_raise( p_e, "No more element to play" );
         return;
     }
-    
+
     libvlc_media_list_lock( p_mlp->p_mlist );
 
     path = get_next_path( p_mlp );
 
+#ifdef DEBUG_MEDIA_LIST_PLAYER
+    printf("Playing:");
+    libvlc_media_list_path_dump(path);
+#endif
+    
     if( !path )
     {
         libvlc_media_list_unlock( p_mlp->p_mlist );
@@ -470,7 +548,7 @@ void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
     }
 
     set_current_playing_item( p_mlp, path, p_e );
-    
+
     libvlc_media_player_play( p_mlp->p_mi, p_e );
 
     libvlc_media_list_unlock( p_mlp->p_mlist );