]> git.sesse.net Git - vlc/blobdiff - src/control/media_player.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / control / media_player.c
index c5af723a633e0b21d80f078b9fca66e525a260be..3789ddf90a03eda5d89525329010f704a6908c77 100644 (file)
@@ -34,6 +34,7 @@
 #include <vlc_demux.h>
 #include <vlc_input.h>
 #include <vlc_vout.h>
+#include <vlc_keys.h>
 
 #include "libvlc.h"
 
 #include "media_internal.h" // libvlc_media_set_state()
 #include "media_player_internal.h"
 
+/*
+ * mapping of libvlc_navigate_mode_t to vlc_key_t
+ */
+static const vlc_key_t libvlc_navigate_to_action[] =
+  {
+    ACTIONID_NAV_ACTIVATE,
+    ACTIONID_NAV_UP,
+    ACTIONID_NAV_DOWN,
+    ACTIONID_NAV_LEFT,
+    ACTIONID_NAV_RIGHT
+  };
+
+static const uint32_t libvlc_navigate_to_action_size =                 \
+  sizeof( libvlc_navigate_to_action ) / sizeof( libvlc_navigate_to_action[0] );
+
+
 static int
 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
                         vlc_value_t oldval, vlc_value_t newval,
@@ -70,6 +87,15 @@ static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type
     libvlc_event_manager_register_event_type(mp->p_event_manager, type);
 }
 
+/*
+ * The input lock protects the input and input resource pointer.
+ * It MUST NOT be used from callbacks.
+ *
+ * The object lock protects the reset, namely the media and the player state.
+ * It can, and usually needs to be taken from callbacks.
+ * The object lock can be acquired under the input lock... and consequently
+ * the opposite order is STRICTLY PROHIBITED.
+ */
 static inline void lock(libvlc_media_player_t *mp)
 {
     vlc_mutex_lock(&mp->object_lock);
@@ -80,20 +106,30 @@ static inline void unlock(libvlc_media_player_t *mp)
     vlc_mutex_unlock(&mp->object_lock);
 }
 
+static inline void lock_input(libvlc_media_player_t *mp)
+{
+    vlc_mutex_lock(&mp->input.lock);
+}
+
+static inline void unlock_input(libvlc_media_player_t *mp)
+{
+    vlc_mutex_unlock(&mp->input.lock);
+}
+
 /*
  * Release the associated input thread.
  *
  * Object lock is NOT held.
+ * Input lock is held or instance is being destroyed.
  */
 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
 {
-    input_thread_t * p_input_thread;
+    assert( p_mi );
 
-    if( !p_mi || !p_mi->p_input_thread )
+    input_thread_t *p_input_thread = p_mi->input.p_thread;
+    if( !p_input_thread )
         return;
 
-    p_input_thread = p_mi->p_input_thread;
-
     var_DelCallback( p_input_thread, "can-seek",
                      input_seekable_changed, p_mi );
     var_DelCallback( p_input_thread, "can-pause",
@@ -106,21 +142,15 @@ static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abor
 
     vlc_thread_join( p_input_thread );
 
-    assert( p_mi->p_input_resource == NULL );
     assert( p_input_thread->b_dead );
-    /* Store the input resource for future use. */
-    p_mi->p_input_resource = input_DetachResource( p_input_thread );
 
+    p_mi->input.p_thread = NULL;
     vlc_object_release( p_input_thread );
-
-    p_mi->p_input_thread = NULL;
 }
 
 /*
  * Retrieve the input thread. Be sure to release the object
  * once you are done with it. (libvlc Internal)
- *
- * Function will lock the object.
  */
 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
 {
@@ -128,13 +158,13 @@ input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
 
     assert( p_mi );
 
-    lock(p_mi);
-    p_input_thread = p_mi->p_input_thread;
+    lock_input(p_mi);
+    p_input_thread = p_mi->input.p_thread;
     if( p_input_thread )
         vlc_object_hold( p_input_thread );
     else
         libvlc_printerr( "No active input" );
-    unlock(p_mi);
+    unlock_input(p_mi);
 
     return p_input_thread;
 }
@@ -150,14 +180,16 @@ static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
     if(!b_locked)
         lock(p_mi);
     p_mi->state = state;
+
     libvlc_media_t *media = p_mi->p_md;
     if (media)
         libvlc_media_retain(media);
+
     if(!b_locked)
         unlock(p_mi);
 
-
-    if (media) {
+    if (media)
+    {
         // Also set the state of the corresponding media
         // This is strictly for convenience.
         libvlc_media_set_state(media, state);
@@ -284,9 +316,15 @@ input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
            from_mtime(var_GetTime( p_input, "length" ));
         libvlc_event_send( p_mi->p_event_manager, &event );
     }
+    else if( newval.i_int == INPUT_EVENT_CACHE )
+    {
+        event.type = libvlc_MediaPlayerBuffering;
+        event.u.media_player_buffering.new_cache = (int)(100 *
+            var_GetFloat( p_input, "cache" ));
+        libvlc_event_send( p_mi->p_event_manager, &event );
+    }
 
     return VLC_SUCCESS;
-
 }
 
 /**************************************************************************
@@ -308,7 +346,14 @@ static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
     return VLC_SUCCESS;
 }
 
+static input_thread_t *find_input (vlc_object_t *obj)
+{
+    libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
 
+    return libvlc_get_input_thread (mp);
+}
+
+/* */
 static void libvlc_media_player_destroy( libvlc_media_player_t * );
 
 
@@ -341,7 +386,20 @@ libvlc_media_player_new( libvlc_instance_t *instance )
     }
     vlc_object_attach (mp, mp->p_libvlc);
 
+    /* Input */
+    var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
+
     /* Video */
+    var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
+    var_Create (mp, "window", VLC_VAR_STRING);
+    var_Create (mp, "vmem-lock", VLC_VAR_ADDRESS);
+    var_Create (mp, "vmem-unlock", VLC_VAR_ADDRESS);
+    var_Create (mp, "vmem-display", VLC_VAR_ADDRESS);
+    var_Create (mp, "vmem-data", VLC_VAR_ADDRESS);
+    var_Create (mp, "vmem-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
+    var_Create (mp, "vmem-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "vmem-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "vmem-pitch", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
     var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
 #ifdef WIN32
     var_Create (mp, "drawable-hwnd", VLC_VAR_ADDRESS);
@@ -365,16 +423,47 @@ libvlc_media_player_new( libvlc_instance_t *instance )
     var_Create (mp, "crop", VLC_VAR_STRING);
     var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
     var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
+
     var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
 
+    var_Create (mp, "marq-marquee", VLC_VAR_STRING);
+    var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+
+    var_Create (mp, "logo-file", VLC_VAR_STRING);
+    var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+
+    var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
+    var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
+    var_Create (mp, "hue", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
+    var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
+
      /* Audio */
     var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
+    var_Create (mp, "volume-muted", VLC_VAR_BOOL);
+    var_Create (mp, "saved-volume", VLC_VAR_INTEGER);
+    var_Create (mp, "volume-change", VLC_VAR_VOID);
+    var_Create (mp, "find-input-callback", VLC_VAR_ADDRESS);
+    var_SetAddress (mp, "find-input-callback", find_input);
 
     mp->p_md = NULL;
     mp->state = libvlc_NothingSpecial;
     mp->p_libvlc_instance = instance;
-    mp->p_input_thread = NULL;
-    mp->p_input_resource = NULL;
+    mp->input.p_thread = NULL;
+    mp->input.p_resource = NULL;
+    vlc_mutex_init (&mp->input.lock);
     mp->i_refcount = 1;
     mp->p_event_manager = libvlc_event_manager_new(mp, instance);
     if (unlikely(mp->p_event_manager == NULL))
@@ -451,19 +540,16 @@ static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
     var_DelCallback( p_mi->p_libvlc,
                      "snapshot-file", snapshot_was_taken, p_mi );
 
-    /* If the input thread hasn't been already deleted it means
-     * that the owners didn't stop the thread before releasing it. */
-    assert(!p_mi->p_input_thread);
-
-    /* Fallback for those who don't use NDEBUG */
-    if (p_mi->p_input_thread)
+    /* No need for lock_input() because no other threads knows us anymore */
+    if( p_mi->input.p_thread )
         release_input_thread(p_mi, true);
-
-    if( p_mi->p_input_resource )
+    if( p_mi->input.p_resource )
     {
-        input_resource_Delete( p_mi->p_input_resource );
-        p_mi->p_input_resource = NULL;
+        input_resource_Terminate( p_mi->input.p_resource );
+        input_resource_Release( p_mi->input.p_resource );
+        p_mi->input.p_resource = NULL;
     }
+    vlc_mutex_destroy( &p_mi->input.lock );
 
     libvlc_event_manager_release( p_mi->p_event_manager );
     libvlc_media_release( p_mi->p_md );
@@ -515,16 +601,18 @@ void libvlc_media_player_set_media(
                             libvlc_media_player_t *p_mi,
                             libvlc_media_t *p_md )
 {
-    lock(p_mi);
+    lock_input(p_mi);
 
     /* FIXME I am not sure if it is a user request or on die(eof/error)
      * request here */
     release_input_thread( p_mi,
-                          p_mi->p_input_thread &&
-                          !p_mi->p_input_thread->b_eof &&
-                          !p_mi->p_input_thread->b_error );
+                          p_mi->input.p_thread &&
+                          !p_mi->input.p_thread->b_eof &&
+                          !p_mi->input.p_thread->b_error );
 
+    lock( p_mi );
     set_state( p_mi, libvlc_NothingSpecial, true );
+    unlock_input( p_mi );
 
     libvlc_media_release( p_mi->p_md );
 
@@ -582,13 +670,14 @@ libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
  **************************************************************************/
 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
 {
-    input_thread_t * p_input_thread;
+    lock_input( p_mi );
 
-    if( (p_input_thread = libvlc_get_input_thread( p_mi )) )
+    input_thread_t *p_input_thread = p_mi->input.p_thread;
+    if( p_input_thread )
     {
         /* A thread already exists, send it a play message */
         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
-        vlc_object_release( p_input_thread );
+        unlock_input( p_mi );
         return 0;
     }
 
@@ -598,41 +687,43 @@ int libvlc_media_player_play( libvlc_media_player_t *p_mi )
     if( !p_mi->p_md )
     {
         unlock(p_mi);
+        unlock_input( p_mi );
         libvlc_printerr( "No associated media descriptor" );
         return -1;
     }
 
-    p_mi->p_input_thread = input_Create( p_mi,
-                                         p_mi->p_md->p_input_item, NULL,
-                                         p_mi->p_input_resource );
-    if( !p_mi->p_input_thread )
+    if( !p_mi->input.p_resource )
+        p_mi->input.p_resource = input_resource_New( VLC_OBJECT( p_mi ) );
+    p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
+                                   p_mi->input.p_resource );
+    unlock(p_mi);
+    if( !p_input_thread )
     {
-        unlock(p_mi);
+        unlock_input(p_mi);
         libvlc_printerr( "Not enough memory" );
         return -1;
     }
 
-    p_mi->p_input_resource = NULL;
-    p_input_thread = p_mi->p_input_thread;
-
     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 ) )
     {
+        unlock_input(p_mi);
+        var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
+        var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
+        var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
         vlc_object_release( p_input_thread );
-        p_mi->p_input_thread = NULL;
+        libvlc_printerr( "Input initialization failure" );
+        return -1;
     }
-
-    unlock(p_mi);
+    p_mi->input.p_thread = p_input_thread;
+    unlock_input(p_mi);
     return 0;
 }
 
-/**************************************************************************
- * Pause.
- **************************************************************************/
-void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
+void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
 {
     input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
     if( !p_input_thread )
@@ -641,17 +732,34 @@ void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
     if( state == libvlc_Playing || state == libvlc_Buffering )
     {
-        if( libvlc_media_player_can_pause( p_mi ) )
-            input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
-        else
-            libvlc_media_player_stop( p_mi );
+        if( paused )
+        {
+            if( libvlc_media_player_can_pause( p_mi ) )
+                input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
+            else
+                libvlc_media_player_stop( p_mi );
+        }
     }
     else
-        input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
+    {
+        if( !paused )
+            input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
+    }
 
     vlc_object_release( p_input_thread );
 }
 
+/**************************************************************************
+ * Toggle pause.
+ **************************************************************************/
+void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
+{
+    libvlc_state_t state = libvlc_media_player_get_state( p_mi );
+    bool playing = (state == libvlc_Playing || state == libvlc_Buffering);
+
+    libvlc_media_player_set_pause( p_mi, playing );
+}
+
 /**************************************************************************
  * Tells whether the media player is currently playing.
  *
@@ -670,9 +778,8 @@ void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
 {
     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
 
-    lock(p_mi);
+    lock_input(p_mi);
     release_input_thread( p_mi, true ); /* This will stop the input thread */
-    unlock(p_mi);
 
     /* Force to go to stopped state, in case we were in Ended, or Error
      * state. */
@@ -685,6 +792,33 @@ void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
         event.type = libvlc_MediaPlayerStopped;
         libvlc_event_send( p_mi->p_event_manager, &event );
     }
+
+    if( p_mi->input.p_resource != NULL )
+        input_resource_TerminateVout( p_mi->input.p_resource );
+    unlock_input(p_mi);
+}
+
+
+void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
+    void *(*lock_cb) (void *, void **),
+    void (*unlock_cb) (void *, void *, void *const *),
+    void (*display_cb) (void *, void *),
+    void *opaque )
+{
+    var_SetAddress( mp, "vmem-lock", lock_cb );
+    var_SetAddress( mp, "vmem-unlock", unlock_cb );
+    var_SetAddress( mp, "vmem-display", display_cb );
+    var_SetAddress( mp, "vmem-data", opaque );
+    var_SetString( mp, "vout", "vmem" );
+}
+
+void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
+                              unsigned width, unsigned height, unsigned pitch )
+{
+    var_SetString( mp, "vmem-chroma", chroma );
+    var_SetInteger( mp, "vmem-width", width );
+    var_SetInteger( mp, "vmem-height", height );
+    var_SetInteger( mp, "vmem-pitch", pitch );
 }
 
 /**************************************************************************
@@ -747,6 +881,9 @@ void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
                                       uint32_t drawable )
 {
     assert (p_mi != NULL);
+
+    var_SetString (p_mi, "vout", drawable ? "xid" : "any");
+    var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "any");
     var_SetInteger (p_mi, "drawable-xid", drawable);
 }
 
@@ -766,6 +903,8 @@ void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
 {
     assert (p_mi != NULL);
 #ifdef WIN32
+    var_SetString (p_mi, "window",
+                   (drawable != NULL) ? "embed-hwnd,any" : "");
     var_SetAddress (p_mi, "drawable-hwnd", drawable);
 #else
     (void) p_mi; (void) drawable;
@@ -1035,21 +1174,17 @@ int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
 
 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
 {
-    input_thread_t *p_input_thread;
-    bool b_can_rewind;
-
-    p_input_thread = libvlc_get_input_thread ( p_mi );
-    if( !p_input_thread )
-        return -1;
-
-    b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
-    if( (rate < 0.0) && !b_can_rewind )
+    if (rate < 0.)
     {
-        vlc_object_release( p_input_thread );
-        libvlc_printerr( "Invalid playback rate" );
+        libvlc_printerr ("Playing backward not supported");
         return -1;
     }
 
+    var_SetFloat (p_mi, "rate", rate);
+
+    input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
+    if( !p_input_thread )
+        return 0;
     var_SetFloat( p_input_thread, "rate", rate );
     vlc_object_release( p_input_thread );
     return 0;
@@ -1057,25 +1192,7 @@ int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
 
 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
 {
-    input_thread_t *p_input_thread;
-    float f_rate;
-    bool b_can_rewind;
-
-    p_input_thread = libvlc_get_input_thread ( p_mi );
-    if( !p_input_thread )
-        return 0.0;  /* rate < 0 indicates rewind */
-
-    f_rate = var_GetFloat( p_input_thread, "rate" );
-    b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
-    /* FIXME: why are negative values forbidden ?? (rewinding) */
-    if( f_rate < 0 && !b_can_rewind )
-    {
-        vlc_object_release( p_input_thread );
-        return 0.0;
-    }
-    vlc_object_release( p_input_thread );
-
-    return f_rate;
+    return var_GetFloat (p_mi, "rate");
 }
 
 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
@@ -1100,6 +1217,24 @@ int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
     return b_seekable;
 }
 
+void libvlc_media_player_navigate( libvlc_media_player_t* p_mi,
+                                  unsigned navigate )
+{
+    input_thread_t *p_input_thread;
+
+    if ( navigate > libvlc_navigate_to_action_size)
+      return;
+
+    p_input_thread = libvlc_get_input_thread ( p_mi );
+    if ( !p_input_thread )
+      return;
+
+    var_SetInteger( p_mi->p_libvlc_instance->p_libvlc_int,
+                   "key-action", libvlc_navigate_to_action[navigate] );
+
+    vlc_object_release( p_input_thread );
+}
+
 /* internal function, used by audio, video */
 libvlc_track_description_t *
         libvlc_get_track_description( libvlc_media_player_t *p_mi,