X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fcontrol%2Fmedia_player.c;h=cf54fcd90541816804af116ba1e7de18a01ca5a8;hb=b27990c5c570519c44101464ea9871e4325a527b;hp=ba2c1890f764faafe5a6f88bcbef86a463791d24;hpb=114b922fa7bbe06f4acfcc3d7af9c47ef534b1a2;p=vlc diff --git a/src/control/media_player.c b/src/control/media_player.c index ba2c1890f7..cf54fcd905 100644 --- a/src/control/media_player.c +++ b/src/control/media_player.c @@ -58,21 +58,6 @@ static int snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd, vlc_value_t oldval, vlc_value_t newval, void *p_data ); -/* Mouse events */ -static int -mouse_moved( vlc_object_t *p_this, char const *psz_cmd, - vlc_value_t oldval, vlc_value_t newval, void *p_data ); -static int -mouse_button( vlc_object_t *p_this, char const *psz_cmd, - vlc_value_t oldval, vlc_value_t newval, void *p_data ); -static int -mouse_clicked( vlc_object_t *p_this, char const *psz_cmd, - vlc_value_t oldval, vlc_value_t newval, void *p_data ); -static int -mouse_object( vlc_object_t *p_this, char const *psz_cmd, - vlc_value_t oldval, vlc_value_t newval, void *p_data ); - -/* */ static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi ); /* @@ -85,6 +70,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); @@ -95,20 +89,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", @@ -121,21 +125,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 ) { @@ -143,61 +141,17 @@ 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; } -/* - * Get vout thread from current input - * - * Object lock is NOT held. - */ -static vout_thread_t *get_vout_thread( libvlc_media_player_t *p_mi ) -{ - vout_thread_t *p_vout_thread; - - p_vout_thread = input_GetVout( p_mi->p_input_thread ); - if( p_vout_thread ) - { - var_AddCallback( p_vout_thread, "mouse-button-down", mouse_button, p_mi ); - var_AddCallback( p_vout_thread, "mouse-moved", mouse_moved, p_mi ); - var_AddCallback( p_vout_thread, "mouse-clicked", mouse_clicked, p_mi ); - var_AddCallback( p_vout_thread, "mouse-object", mouse_object, p_mi ); - } - - return p_vout_thread; -} - -/* - * Release the associated vout thread. - * - * Object lock is NOT held. - */ -static void release_vout_thread( libvlc_media_player_t *p_mi ) -{ - vout_thread_t *p_vout_thread; - - if( !p_mi || !p_mi->p_vout_thread ) - return; - - p_vout_thread = p_mi->p_vout_thread; - - var_DelCallback( p_vout_thread, "mouse-button-down", mouse_button, p_mi ); - var_DelCallback( p_vout_thread, "mouse-moved", mouse_moved, p_mi ); - var_DelCallback( p_vout_thread, "mouse-clicked", mouse_clicked, p_mi ); - var_DelCallback( p_vout_thread, "mouse-object", mouse_object, p_mi ); - - vlc_object_release( p_vout_thread ); - p_mi->p_vout_thread = NULL; -} - /* * Set the internal state of the media_player. (media player Internal) * @@ -345,16 +299,6 @@ 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_VOUT ) - { - lock( p_mi ); - /* release old vout */ - if( p_mi->p_vout_thread ) - release_vout_thread( p_mi ); - /* remember new vout */ - p_mi->p_vout_thread = get_vout_thread( p_mi ); - unlock( p_mi ); - } return VLC_SUCCESS; @@ -379,75 +323,11 @@ static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd, return VLC_SUCCESS; } -/************************************************************************** - * Mouse Events. - *************************************************************************/ - -static int -mouse_moved( vlc_object_t *p_this, char const *psz_cmd, - vlc_value_t oldval, vlc_value_t newval, void *p_data ) +static input_thread_t *find_input (vlc_object_t *obj) { - VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this); VLC_UNUSED(newval); + libvlc_media_player_t *mp = (libvlc_media_player_t *)obj; - libvlc_media_player_t *mp = p_data; - libvlc_event_t event; - event.type = libvlc_MediaPlayerMouseMoved; - event.u.media_player_mouse_moved.x = var_GetInteger( mp->p_vout_thread, "mouse-x" ); - event.u.media_player_mouse_moved.y = var_GetInteger( mp->p_vout_thread, "mouse-y" ); - libvlc_event_send(mp->p_event_manager, &event); - - return VLC_SUCCESS; -} - -static int -mouse_button( vlc_object_t *p_this, char const *psz_cmd, - vlc_value_t oldval, vlc_value_t newval, void *p_data ) -{ - VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this); - - libvlc_media_player_t *mp = p_data; - libvlc_event_t event; - int pressed = newval.i_int; - - event.type = libvlc_MediaPlayerMouseButton; - event.u.media_player_mouse_button.mb_left = (pressed & (1<p_event_manager, &event); - - return VLC_SUCCESS; -} - -static int -mouse_clicked( vlc_object_t *p_this, char const *psz_cmd, - vlc_value_t oldval, vlc_value_t newval, void *p_data ) -{ - VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this); - - libvlc_media_player_t *mp = p_data; - libvlc_event_t event; - event.type = libvlc_MediaPlayerMouseClick; - event.u.media_player_mouse_clicked.clicked = newval.b_bool ? 1 : 0; - libvlc_event_send(mp->p_event_manager, &event); - - return VLC_SUCCESS; -} - -static int -mouse_object( vlc_object_t *p_this, char const *psz_cmd, - vlc_value_t oldval, vlc_value_t newval, void *p_data ) -{ - VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this); - - libvlc_media_player_t *mp = p_data; - libvlc_event_t event; - event.type = libvlc_MediaPlayerMouseObject; - event.u.media_player_mouse_object.moved = (newval.b_bool ? 1 : 0); - libvlc_event_send(mp->p_event_manager, &event); - - return VLC_SUCCESS; + return libvlc_get_input_thread (mp); } /* */ @@ -483,7 +363,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-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT); var_Create (mp, "drawable-xid", VLC_VAR_INTEGER); #ifdef WIN32 var_Create (mp, "drawable-hwnd", VLC_VAR_ADDRESS); @@ -528,15 +421,26 @@ libvlc_media_player_new( libvlc_instance_t *instance ) 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->p_vout_thread = 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)) @@ -569,12 +473,6 @@ libvlc_media_player_new( libvlc_instance_t *instance ) register_event(mp, MediaChanged); - /* mouse events */ - register_event(mp, MouseMoved); - register_event(mp, MouseButton); - register_event(mp, MouseClick); - register_event(mp, MouseObject); - /* 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 @@ -619,22 +517,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_vout_thread ) - release_vout_thread( p_mi ); - - 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 ); @@ -686,16 +578,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 ); @@ -753,13 +647,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; } @@ -769,41 +664,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 ) @@ -812,17 +709,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. * @@ -841,9 +755,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. */ @@ -856,6 +769,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 ); } /************************************************************************** @@ -918,6 +858,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); } @@ -937,6 +880,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; @@ -1206,21 +1151,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; @@ -1228,25 +1169,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 )