]> git.sesse.net Git - vlc/blobdiff - src/control/media_player.c
Adding a libvlc_media_player_next_frame
[vlc] / src / control / media_player.c
index 86398134748a9026a9155f5d362c6002bf76d20f..706157e6896460227f62edf4cf7189f5ae8a67f0 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * media_player.c: Libvlc API Media Instance management functions
  *****************************************************************************
- * Copyright (C) 2005 the VideoLAN team
+ * Copyright (C) 2005-2009 the VideoLAN team
  * $Id$
  *
  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include "libvlc_internal.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <assert.h>
 
 #include <vlc/libvlc.h>
+#include <vlc/libvlc_media.h>
+#include <vlc/libvlc_events.h>
+
 #include <vlc_demux.h>
 #include <vlc_input.h>
 #include <vlc_vout.h>
+
 #include "libvlc.h"
-#include <assert.h>
+
+#include "libvlc_internal.h"
+#include "media_internal.h" // libvlc_media_set_state()
+#include "media_player_internal.h"
 
 static int
 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
@@ -46,23 +57,7 @@ input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
                        vlc_value_t oldval, vlc_value_t newval, void *p_data );
 
-static const libvlc_state_t vlc_to_libvlc_state_array[] =
-{
-    [INIT_S]        = libvlc_NothingSpecial,
-    [OPENING_S]     = libvlc_Opening,
-    [PLAYING_S]     = libvlc_Playing,
-    [PAUSE_S]       = libvlc_Paused,
-    [END_S]         = libvlc_Ended,
-    [ERROR_S]       = libvlc_Error,
-};
-
-static inline libvlc_state_t vlc_to_libvlc_state( int vlc_state )
-{
-    if( vlc_state < 0 || vlc_state > 6 )
-        return libvlc_Ended;
-
-    return vlc_to_libvlc_state_array[vlc_state];
-}
+static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
 
 /*
  * Release the associated input thread.
@@ -78,24 +73,20 @@ static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abor
 
     p_input_thread = p_mi->p_input_thread;
 
-    /* No one is tracking this input_thread apart from us. Destroy it. */
-    if( p_mi->b_own_its_input_thread )
-    {
-        var_DelCallback( p_input_thread, "can-seek",
-                         input_seekable_changed, p_mi );
-        var_DelCallback( p_input_thread, "can-pause",
-                         input_pausable_changed, p_mi );
-        var_DelCallback( p_input_thread, "intf-event",
-                         input_event_changed, p_mi );
-
-        /* We owned this one */
-        input_StopThread( p_input_thread, b_input_abort );
-        vlc_thread_join( p_input_thread );
-
-        var_Destroy( p_input_thread, "drawable-hwnd" );
-        var_Destroy( p_input_thread, "drawable-xid" );
-        var_Destroy( p_input_thread, "drawable-agl" );
-    }
+    var_DelCallback( p_input_thread, "can-seek",
+                     input_seekable_changed, p_mi );
+    var_DelCallback( p_input_thread, "can-pause",
+                    input_pausable_changed, p_mi );
+    var_DelCallback( p_input_thread, "intf-event",
+                     input_event_changed, p_mi );
+
+    /* We owned this one */
+    input_Stop( p_input_thread, b_input_abort );
+    vlc_thread_join( p_input_thread );
+
+    var_Destroy( p_input_thread, "drawable-hwnd" );
+    var_Destroy( p_input_thread, "drawable-xid" );
+    var_Destroy( p_input_thread, "drawable-agl" );
 
     vlc_object_release( p_input_thread );
 
@@ -219,7 +210,15 @@ input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
         libvlc_media_set_state( p_mi->p_md, libvlc_state, NULL );
         libvlc_event_send( p_mi->p_event_manager, &event );
     }
-    else if( newval.i_int == INPUT_EVENT_TIMES )
+    else if( newval.i_int == INPUT_EVENT_ABORT )
+    {
+        libvlc_state_t libvlc_state = libvlc_Stopped;
+        event.type = libvlc_MediaPlayerStopped;
+
+        libvlc_media_set_state( p_mi->p_md, libvlc_state, NULL );
+        libvlc_event_send( p_mi->p_event_manager, &event );
+    }
+    else if( newval.i_int == INPUT_EVENT_POSITION )
     {
         if( var_GetInteger( p_input, "state" ) != PLAYING_S )
             return VLC_SUCCESS; /* Don't send the position while stopped */
@@ -236,11 +235,19 @@ input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
                                                var_GetTime( p_input, "time" );
         libvlc_event_send( p_mi->p_event_manager, &event );
     }
+    else if( newval.i_int == INPUT_EVENT_LENGTH )
+    {
+        event.type = libvlc_MediaPlayerLengthChanged;
+        event.u.media_player_length_changed.new_length =
+                                               var_GetTime( p_input, "length" );
+        libvlc_event_send( p_mi->p_event_manager, &event );
+    }
 
     return VLC_SUCCESS;
 
 }
 
+static void libvlc_media_player_destroy( libvlc_media_player_t * );
 
 /**************************************************************************
  * Create a Media Instance object.
@@ -271,17 +278,17 @@ libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
     p_mi = malloc( sizeof(libvlc_media_player_t) );
     if( !p_mi )
     {
-        libvlc_exception_raise( p_e, "Not enough memory" );
+        libvlc_exception_raise( p_e, "not enough memory" );
         return NULL;
     }
     p_mi->p_md = NULL;
     p_mi->drawable.agl = 0;
     p_mi->drawable.xid = 0;
     p_mi->drawable.hwnd = NULL;
+    p_mi->drawable.nsobject = NULL;
     p_mi->p_libvlc_instance = p_libvlc_instance;
     p_mi->p_input_thread = NULL;
     p_mi->i_refcount = 1;
-    p_mi->b_own_its_input_thread = true;
     vlc_mutex_init( &p_mi->object_lock );
     p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
             p_libvlc_instance, p_e );
@@ -317,7 +324,9 @@ libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
             libvlc_MediaPlayerPositionChanged, p_e );
     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
             libvlc_MediaPlayerTimeChanged, p_e );
-     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
+    libvlc_event_manager_register_event_type( p_mi->p_event_manager,
+            libvlc_MediaPlayerLengthChanged, p_e );
+    libvlc_event_manager_register_event_type( p_mi->p_event_manager,
             libvlc_MediaPlayerTitleChanged, p_e );
     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
             libvlc_MediaPlayerSeekableChanged, p_e );
@@ -327,6 +336,7 @@ libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
     /* Snapshot initialization */
     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
            libvlc_MediaPlayerSnapshotTaken, p_e );
+
     /* Attach a var callback to the global object to provide the glue between
         vout_thread that generates the event and media_player that re-emits it
         with its own event manager
@@ -348,8 +358,8 @@ libvlc_media_player_new_from_media(
                                     libvlc_exception_t *p_e )
 {
     libvlc_media_player_t * p_mi;
-    p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
 
+    p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
     if( !p_mi )
         return NULL;
 
@@ -359,80 +369,25 @@ libvlc_media_player_new_from_media(
     return p_mi;
 }
 
-/**************************************************************************
- * Create a new media instance object from an input_thread (Libvlc Internal).
- **************************************************************************/
-libvlc_media_player_t * libvlc_media_player_new_from_input_thread(
-                                   struct libvlc_instance_t *p_libvlc_instance,
-                                   input_thread_t *p_input,
-                                   libvlc_exception_t *p_e )
-{
-    libvlc_media_player_t * p_mi;
-
-    if( !p_input )
-    {
-        libvlc_exception_raise( p_e, "invalid input thread" );
-        return NULL;
-    }
-
-    p_mi = libvlc_media_player_new( p_libvlc_instance, p_e );
-
-    if( !p_mi )
-        return NULL;
-
-    p_mi->p_md = libvlc_media_new_from_input_item(
-                    p_libvlc_instance,
-                    input_GetItem( p_input ), p_e );
-
-    if( !p_mi->p_md )
-    {
-        libvlc_media_player_destroy( p_mi );
-        return NULL;
-    }
-
-    /* will be released in media_player_release() */
-    vlc_object_hold( p_input );
-
-    p_mi->p_input_thread = p_input;
-    p_mi->b_own_its_input_thread = false;
-
-    return p_mi;
-}
-
 /**************************************************************************
  * Destroy a Media Instance object (libvlc internal)
  *
  * Warning: No lock held here, but hey, this is internal. Caller must lock.
  **************************************************************************/
-void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
+static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
 {
-    input_thread_t *p_input_thread;
-    libvlc_exception_t p_e;
+    assert( p_mi );
 
-    libvlc_exception_init( &p_e );
-
-    if( !p_mi )
-        return;
-
-       /* Detach Callback from the main libvlc object */
+    /* Detach Callback from the main libvlc object */
     var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
                      "vout-snapshottaken", SnapshotTakenCallback, p_mi );
 
-    p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
-
-    if( libvlc_exception_raised( &p_e ) )
-    {
-        libvlc_event_manager_release( p_mi->p_event_manager );
-        libvlc_exception_clear( &p_e );
-        free( p_mi );
-        return; /* no need to worry about no input thread */
-    }
-    vlc_mutex_destroy( &p_mi->object_lock );
-
-    vlc_object_release( p_input_thread );
+    /* Realease the input thread */
+    release_input_thread( p_mi, true );
 
+    libvlc_event_manager_release( p_mi->p_event_manager );
     libvlc_media_release( p_mi->p_md );
-
+    vlc_mutex_destroy( &p_mi->object_lock );
     free( p_mi );
 }
 
@@ -443,28 +398,15 @@ void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
  **************************************************************************/
 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
 {
-    if( !p_mi )
-        return;
+    bool destroy;
 
+    assert( p_mi );
     vlc_mutex_lock( &p_mi->object_lock );
-
-    p_mi->i_refcount--;
-
-    if( p_mi->i_refcount > 0 )
-    {
-        vlc_mutex_unlock( &p_mi->object_lock );
-        return;
-    }
+    destroy = !--p_mi->i_refcount;
     vlc_mutex_unlock( &p_mi->object_lock );
-    vlc_mutex_destroy( &p_mi->object_lock );
-
-    release_input_thread( p_mi, true );
-
-    libvlc_event_manager_release( p_mi->p_event_manager );
 
-    libvlc_media_release( p_mi->p_md );
-
-    free( p_mi );
+    if( destroy )
+        libvlc_media_player_destroy( p_mi );
 }
 
 /**************************************************************************
@@ -474,10 +416,11 @@ void libvlc_media_player_release( libvlc_media_player_t *p_mi )
  **************************************************************************/
 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
 {
-    if( !p_mi )
-        return;
+    assert( p_mi );
 
+    vlc_mutex_lock( &p_mi->object_lock );
     p_mi->i_refcount++;
+    vlc_mutex_unlock( &p_mi->object_lock );
 }
 
 /**************************************************************************
@@ -534,12 +477,14 @@ libvlc_media_player_get_media(
                             libvlc_media_player_t *p_mi,
                             libvlc_exception_t *p_e )
 {
+    libvlc_media_t *p_m;
     VLC_UNUSED(p_e);
 
-    if( !p_mi->p_md )
-        return NULL;
-
-    libvlc_media_retain( p_mi->p_md );
+    vlc_mutex_lock( &p_mi->object_lock );
+    p_m = p_mi->p_md;
+    if( p_m )
+        libvlc_media_retain( p_mi->p_md );
+    vlc_mutex_unlock( &p_mi->object_lock );
     return p_mi->p_md;
 }
 
@@ -607,8 +552,8 @@ void libvlc_media_player_play( libvlc_media_player_t *p_mi,
         return;
     }
 
-    p_mi->p_input_thread = input_CreateThread(
-            p_mi->p_libvlc_instance->p_libvlc_int, p_mi->p_md->p_input_item );
+    p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
+                                         p_mi->p_md->p_input_item, NULL, NULL );
 
     if( !p_mi->p_input_thread )
     {
@@ -628,22 +573,22 @@ void libvlc_media_player_play( libvlc_media_player_t *p_mi,
 
     var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
     if( p_mi->drawable.hwnd != NULL )
-    {
-        vlc_value_t val = { .p_address = p_mi->drawable.hwnd };
-        var_Set( p_input_thread, "drawable-hwnd", val );
-    }
+        var_SetAddress( p_input_thread, "drawable-hwnd", p_mi->drawable.hwnd );
 
-       var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
+    var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
     if( p_mi->drawable.nsobject != NULL )
-    {
-        vlc_value_t val = { .p_address = p_mi->drawable.nsobject };
-        var_Set( p_input_thread, "drawable-nsobject", val );
-    }
-       
+        var_SetAddress( p_input_thread, "drawable-nsobject", p_mi->drawable.nsobject );
+
     var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
     var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
     var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
 
+    if( input_Start( p_input_thread ) )
+    {
+        vlc_object_release( p_input_thread );
+        p_mi->p_input_thread = NULL;
+    }
+
     vlc_mutex_unlock( &p_mi->object_lock );
 }
 
@@ -654,13 +599,11 @@ void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
                                   libvlc_exception_t *p_e )
 {
     input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
-
     if( !p_input_thread )
         return;
 
     libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
-
-    if( state == libvlc_Playing )
+    if( state == libvlc_Playing || state == libvlc_Buffering )
     {
         if( libvlc_media_player_can_pause( p_mi, p_e ) )
             input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
@@ -682,10 +625,9 @@ int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
                                      libvlc_exception_t *p_e )
 {
     libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
-    return libvlc_Playing == state;
+    return (libvlc_Playing == state) || (libvlc_Buffering == state);
 }
 
-
 /**************************************************************************
  * Stop playing.
  **************************************************************************/
@@ -694,11 +636,13 @@ void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
 {
     libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
 
-    if( state == libvlc_Playing || state == libvlc_Paused )
+    if( state == libvlc_Playing ||
+        state == libvlc_Paused ||
+        state == libvlc_Buffering )
     {
-        /* Send a stop notification event only if we are in playing or
-         * paused states */
-        libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
+        /* Send a stop notification event only if we are in playing,
+         * buffering or paused states */
+        libvlc_media_set_state( p_mi->p_md, libvlc_Stopped, p_e );
 
         /* Construct and send the event */
         libvlc_event_t event;
@@ -706,40 +650,26 @@ void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
         libvlc_event_send( p_mi->p_event_manager, &event );
     }
 
-    if( p_mi->b_own_its_input_thread )
-    {
-        vlc_mutex_lock( &p_mi->object_lock );
-        release_input_thread( p_mi, true ); /* This will stop the input thread */
-        vlc_mutex_unlock( &p_mi->object_lock );
-    }
-    else
-    {
-        input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
-
-        if( !p_input_thread )
-            return;
-
-        input_StopThread( p_input_thread, true );
-        vlc_object_release( p_input_thread );
-        p_mi->p_input_thread = NULL;
-    }
+    vlc_mutex_lock( &p_mi->object_lock );
+    release_input_thread( p_mi, true ); /* This will stop the input thread */
+    vlc_mutex_unlock( &p_mi->object_lock );
 }
 
 /**************************************************************************
  * set_nsobject
  **************************************************************************/
 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
-                                                                void * drawable,
-                                                                libvlc_exception_t *p_e )
+                                        void * drawable,
+                                        libvlc_exception_t *p_e )
 {
     (void) p_e;
     p_mi->drawable.nsobject = drawable;
 }
 
 /**************************************************************************
- * get_agl
+ * get_nsobject
  **************************************************************************/
-uint32_t libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
+void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
 {
     return p_mi->drawable.nsobject;
 }
@@ -851,16 +781,16 @@ libvlc_time_t libvlc_media_player_get_length(
                              libvlc_exception_t *p_e )
 {
     input_thread_t *p_input_thread;
-    vlc_value_t val;
+    libvlc_time_t i_time;
 
     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
     if( !p_input_thread )
         return -1;
 
-    var_Get( p_input_thread, "length", &val );
+    i_time = var_GetTime( p_input_thread, "length" );
     vlc_object_release( p_input_thread );
 
-    return (val.i_time+500LL)/1000LL;
+    return (i_time+500LL)/1000LL;
 }
 
 libvlc_time_t libvlc_media_player_get_time(
@@ -868,20 +798,20 @@ libvlc_time_t libvlc_media_player_get_time(
                                    libvlc_exception_t *p_e )
 {
     input_thread_t *p_input_thread;
-    vlc_value_t val;
+    libvlc_time_t i_time;
 
     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
     if( !p_input_thread )
         return -1;
 
-    var_Get( p_input_thread , "time", &val );
+    i_time = var_GetTime( p_input_thread , "time" );
     vlc_object_release( p_input_thread );
-    return (val.i_time+500LL)/1000LL;
+    return (i_time+500LL)/1000LL;
 }
 
 void libvlc_media_player_set_time(
                                  libvlc_media_player_t *p_mi,
-                                 libvlc_time_t time,
+                                 libvlc_time_t i_time,
                                  libvlc_exception_t *p_e )
 {
     input_thread_t *p_input_thread;
@@ -890,7 +820,7 @@ void libvlc_media_player_set_time(
     if( !p_input_thread )
         return;
 
-    var_SetTime( p_input_thread, "time", time*1000LL );
+    var_SetTime( p_input_thread, "time", i_time*1000LL );
     vlc_object_release( p_input_thread );
 }
 
@@ -1187,9 +1117,7 @@ libvlc_state_t libvlc_media_player_get_state(
         return state;
     }
 
-    var_Get( p_input_thread, "state", &val );
-    state = vlc_to_libvlc_state(val.i_int);
-
+    state = libvlc_media_get_state( p_mi->p_md, NULL );
     if( state == libvlc_Playing )
     {
         float caching;
@@ -1228,6 +1156,8 @@ libvlc_track_description_t *
                                       libvlc_exception_t *p_e )
 {
     input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
+    libvlc_track_description_t *p_track_description = NULL,
+                               *p_actual, *p_previous;
 
     if( !p_input )
         return NULL;
@@ -1235,19 +1165,16 @@ libvlc_track_description_t *
     vlc_value_t val_list, text_list;
     var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
 
-    if( val_list.p_list->i_count <= 0 ) /* no tracks */
-        return NULL;
+    /* no tracks */
+    if( val_list.p_list->i_count <= 0 )
+        goto end;
 
-    libvlc_track_description_t *p_track_description, *p_actual, *p_previous;
     p_track_description = ( libvlc_track_description_t * )
         malloc( sizeof( libvlc_track_description_t ) );
     if ( !p_track_description )
     {
-        var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
-                    &val_list, &text_list);
-        vlc_object_release( p_input );
-        libvlc_exception_raise( p_e, "no enough memory" );
-        return NULL;
+        libvlc_exception_raise( p_e, "not enough memory" );
+        goto end;
     }
     p_actual = p_track_description;
     p_previous = NULL;
@@ -1260,11 +1187,8 @@ libvlc_track_description_t *
             if ( !p_actual )
             {
                 libvlc_track_description_release( p_track_description );
-                var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
-                            &val_list, &text_list);
-                vlc_object_release( p_input );
-                libvlc_exception_raise( p_e, "no enough memory" );
-                return NULL;
+                libvlc_exception_raise( p_e, "not enough memory" );
+                goto end;
             }
         }
         p_actual->i_id = val_list.p_list->p_values[i].i_int;
@@ -1275,7 +1199,9 @@ libvlc_track_description_t *
         p_previous = p_actual;
         p_actual =  NULL;
     }
-    var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
+
+end:
+    var_FreeList( &val_list, &text_list );
     vlc_object_release( p_input );
 
     return p_track_description;
@@ -1314,3 +1240,12 @@ int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
 
     return val.b_bool;
 }
+
+void    libvlc_media_player_next_frame( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
+{
+    input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
+    if( p_input_thread != NULL )
+        var_TriggerCallback( p_input_thread, "frame-next" );
+    else
+        libvlc_exception_raise( p_e, "Input thread is NULL" );
+}