Commit 1ef06cd3 authored by Pierre d'Herbemont's avatar Pierre d'Herbemont

libvlc: Add some shortcuts in media_player.c and do some cleanup.

No functional changes.
parent a9dfcaad
...@@ -54,11 +54,38 @@ input_event_changed( vlc_object_t * p_this, char const * psz_cmd, ...@@ -54,11 +54,38 @@ input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
vlc_value_t oldval, vlc_value_t newval, vlc_value_t oldval, vlc_value_t newval,
void * p_userdata ); void * p_userdata );
static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd, 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 ); vlc_value_t oldval, vlc_value_t newval, void *p_data );
static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi ); static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
/*
* Shortcuts
*/
#define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type_t type)
{
libvlc_event_manager_register_event_type(mp->p_event_manager, type, NULL);
}
static inline void lock(libvlc_media_player_t *mp)
{
vlc_mutex_lock(&mp->object_lock);
}
static inline void unlock(libvlc_media_player_t *mp)
{
vlc_mutex_unlock(&mp->object_lock);
}
static inline void clear_if_needed(libvlc_exception_t *e)
{
if (libvlc_exception_raised(e))
libvlc_exception_clear(e);
}
/* /*
* Release the associated input thread. * Release the associated input thread.
* *
...@@ -112,18 +139,18 @@ input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi, ...@@ -112,18 +139,18 @@ input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
if( !p_mi ) RAISENULL( "Media Instance is NULL" ); if( !p_mi ) RAISENULL( "Media Instance is NULL" );
vlc_mutex_lock( &p_mi->object_lock ); lock(p_mi);
if( !p_mi->p_input_thread ) if( !p_mi->p_input_thread )
{ {
vlc_mutex_unlock( &p_mi->object_lock ); unlock(p_mi);
RAISENULL( "Input is NULL" ); RAISENULL( "Input is NULL" );
} }
p_input_thread = p_mi->p_input_thread; p_input_thread = p_mi->p_input_thread;
vlc_object_hold( p_input_thread ); vlc_object_hold( p_input_thread );
vlc_mutex_unlock( &p_mi->object_lock ); unlock(p_mi);
return p_input_thread; return p_input_thread;
} }
...@@ -253,8 +280,29 @@ input_event_changed( vlc_object_t * p_this, char const * psz_cmd, ...@@ -253,8 +280,29 @@ input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
} }
/**************************************************************************
* Snapshot Taken Event.
*
* FIXME: This snapshot API interface makes no sense in media_player.
*************************************************************************/
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 )
{
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_MediaPlayerSnapshotTaken;
event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
libvlc_event_send(mp->p_event_manager, &event);
return VLC_SUCCESS;
}
static void libvlc_media_player_destroy( libvlc_media_player_t * ); static void libvlc_media_player_destroy( libvlc_media_player_t * );
/************************************************************************** /**************************************************************************
* Create a Media Instance object. * Create a Media Instance object.
* *
...@@ -270,85 +318,67 @@ static void libvlc_media_player_destroy( libvlc_media_player_t * ); ...@@ -270,85 +318,67 @@ static void libvlc_media_player_destroy( libvlc_media_player_t * );
* - When attempting to destroy the object the lock is also held. * - When attempting to destroy the object the lock is also held.
**************************************************************************/ **************************************************************************/
libvlc_media_player_t * libvlc_media_player_t *
libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance, libvlc_media_player_new( libvlc_instance_t *instance, libvlc_exception_t *e )
libvlc_exception_t * p_e )
{ {
libvlc_media_player_t * p_mi; libvlc_media_player_t * mp;
assert( p_libvlc_instance ); assert(instance);
p_mi = malloc( sizeof(libvlc_media_player_t) ); mp = malloc(sizeof(libvlc_media_player_t));
if( !p_mi ) if (!mp)
{ {
libvlc_exception_raise( p_e ); libvlc_exception_raise(e);
libvlc_printerr( "Not enough memory" ); libvlc_printerr("Not enough memory");
return NULL; return NULL;
} }
p_mi->p_md = NULL; mp->p_md = NULL;
p_mi->drawable.agl = 0; mp->drawable.agl = 0;
p_mi->drawable.xid = 0; mp->drawable.xid = 0;
p_mi->drawable.hwnd = NULL; mp->drawable.hwnd = NULL;
p_mi->drawable.nsobject = NULL; mp->drawable.nsobject = NULL;
p_mi->p_libvlc_instance = p_libvlc_instance; mp->p_libvlc_instance = instance;
p_mi->p_input_thread = NULL; mp->p_input_thread = NULL;
p_mi->p_input_resource = NULL; mp->p_input_resource = NULL;
p_mi->i_refcount = 1; mp->i_refcount = 1;
vlc_mutex_init( &p_mi->object_lock ); vlc_mutex_init(&mp->object_lock);
p_mi->p_event_manager = libvlc_event_manager_new( p_mi, mp->p_event_manager = libvlc_event_manager_new(mp, instance, e);
p_libvlc_instance, p_e ); if (libvlc_exception_raised(e))
if( libvlc_exception_raised( p_e ) )
{ {
vlc_mutex_destroy( &p_mi->object_lock ); vlc_mutex_destroy(&mp->object_lock);
free( p_mi ); free(mp);
return NULL; return NULL;
} }
libvlc_event_manager_register_event_type( p_mi->p_event_manager, register_event(mp, NothingSpecial);
libvlc_MediaPlayerNothingSpecial, p_e ); register_event(mp, Opening);
libvlc_event_manager_register_event_type( p_mi->p_event_manager, register_event(mp, Buffering);
libvlc_MediaPlayerOpening, p_e ); register_event(mp, Playing);
libvlc_event_manager_register_event_type( p_mi->p_event_manager, register_event(mp, Paused);
libvlc_MediaPlayerBuffering, p_e ); register_event(mp, Stopped);
libvlc_event_manager_register_event_type( p_mi->p_event_manager, register_event(mp, Forward);
libvlc_MediaPlayerPlaying, p_e ); register_event(mp, Backward);
libvlc_event_manager_register_event_type( p_mi->p_event_manager, register_event(mp, EndReached);
libvlc_MediaPlayerPaused, p_e ); register_event(mp, EncounteredError);
libvlc_event_manager_register_event_type( p_mi->p_event_manager,
libvlc_MediaPlayerStopped, p_e ); register_event(mp, PositionChanged);
libvlc_event_manager_register_event_type( p_mi->p_event_manager, register_event(mp, TimeChanged);
libvlc_MediaPlayerForward, p_e ); register_event(mp, LengthChanged);
libvlc_event_manager_register_event_type( p_mi->p_event_manager, register_event(mp, TitleChanged);
libvlc_MediaPlayerBackward, p_e ); register_event(mp, PausableChanged);
libvlc_event_manager_register_event_type( p_mi->p_event_manager,
libvlc_MediaPlayerEndReached, p_e );
libvlc_event_manager_register_event_type( p_mi->p_event_manager,
libvlc_MediaPlayerEncounteredError, p_e );
libvlc_event_manager_register_event_type( p_mi->p_event_manager,
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_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 );
libvlc_event_manager_register_event_type( p_mi->p_event_manager,
libvlc_MediaPlayerPausableChanged, p_e );
/* Snapshot initialization */ /* Snapshot initialization */
libvlc_event_manager_register_event_type( p_mi->p_event_manager, register_event(mp, SnapshotTaken);
libvlc_MediaPlayerSnapshotTaken, p_e );
/* Attach a var callback to the global object to provide the glue between /* 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 * vout_thread that generates the event and media_player that re-emits it
with its own event manager * with its own event manager
*
* FIXME: It's unclear why we want to put this in public API, and why we
* want to expose it in such a limiting and ugly way.
*/ */
var_AddCallback( p_libvlc_instance->p_libvlc_int, "snapshot-file", var_AddCallback(instance->p_libvlc_int, "snapshot-file", snapshot_was_taken, mp);
SnapshotTakenCallback, p_mi );
return p_mi; return mp;
} }
/************************************************************************** /**************************************************************************
...@@ -382,7 +412,7 @@ static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi ) ...@@ -382,7 +412,7 @@ static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
/* Detach Callback from the main libvlc object */ /* Detach Callback from the main libvlc object */
var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int, var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
"snapshot-file", SnapshotTakenCallback, p_mi ); "snapshot-file", snapshot_was_taken, p_mi );
/* Release the input thread */ /* Release the input thread */
release_input_thread( p_mi, true ); release_input_thread( p_mi, true );
...@@ -409,9 +439,9 @@ void libvlc_media_player_release( libvlc_media_player_t *p_mi ) ...@@ -409,9 +439,9 @@ void libvlc_media_player_release( libvlc_media_player_t *p_mi )
bool destroy; bool destroy;
assert( p_mi ); assert( p_mi );
vlc_mutex_lock( &p_mi->object_lock ); lock(p_mi);
destroy = !--p_mi->i_refcount; destroy = !--p_mi->i_refcount;
vlc_mutex_unlock( &p_mi->object_lock ); unlock(p_mi);
if( destroy ) if( destroy )
libvlc_media_player_destroy( p_mi ); libvlc_media_player_destroy( p_mi );
...@@ -426,9 +456,9 @@ void libvlc_media_player_retain( libvlc_media_player_t *p_mi ) ...@@ -426,9 +456,9 @@ void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
{ {
assert( p_mi ); assert( p_mi );
vlc_mutex_lock( &p_mi->object_lock ); lock(p_mi);
p_mi->i_refcount++; p_mi->i_refcount++;
vlc_mutex_unlock( &p_mi->object_lock ); unlock(p_mi);
} }
/************************************************************************** /**************************************************************************
...@@ -446,7 +476,7 @@ void libvlc_media_player_set_media( ...@@ -446,7 +476,7 @@ void libvlc_media_player_set_media(
if( !p_mi ) if( !p_mi )
return; return;
vlc_mutex_lock( &p_mi->object_lock ); lock(p_mi);
/* FIXME I am not sure if it is a user request or on die(eof/error) /* FIXME I am not sure if it is a user request or on die(eof/error)
* request here */ * request here */
...@@ -463,7 +493,7 @@ void libvlc_media_player_set_media( ...@@ -463,7 +493,7 @@ void libvlc_media_player_set_media(
if( !p_md ) if( !p_md )
{ {
p_mi->p_md = NULL; p_mi->p_md = NULL;
vlc_mutex_unlock( &p_mi->object_lock ); unlock(p_mi);
return; /* It is ok to pass a NULL md */ return; /* It is ok to pass a NULL md */
} }
...@@ -474,7 +504,7 @@ void libvlc_media_player_set_media( ...@@ -474,7 +504,7 @@ void libvlc_media_player_set_media(
* libvlc_instance, because we don't really care */ * libvlc_instance, because we don't really care */
p_mi->p_libvlc_instance = p_md->p_libvlc_instance; p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
vlc_mutex_unlock( &p_mi->object_lock ); unlock(p_mi);
} }
/************************************************************************** /**************************************************************************
...@@ -488,11 +518,11 @@ libvlc_media_player_get_media( ...@@ -488,11 +518,11 @@ libvlc_media_player_get_media(
libvlc_media_t *p_m; libvlc_media_t *p_m;
VLC_UNUSED(p_e); VLC_UNUSED(p_e);
vlc_mutex_lock( &p_mi->object_lock ); lock(p_mi);
p_m = p_mi->p_md; p_m = p_mi->p_md;
if( p_m ) if( p_m )
libvlc_media_retain( p_mi->p_md ); libvlc_media_retain( p_mi->p_md );
vlc_mutex_unlock( &p_mi->object_lock ); unlock(p_mi);
return p_mi->p_md; return p_mi->p_md;
} }
...@@ -509,29 +539,6 @@ libvlc_media_player_event_manager( ...@@ -509,29 +539,6 @@ libvlc_media_player_event_manager(
return p_mi->p_event_manager; return p_mi->p_event_manager;
} }
/**************************************************************************
* Trigger a snapshot Taken Event.
*************************************************************************/
static int SnapshotTakenCallback( 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* p_mi = (libvlc_media_player_t*) p_data ;
libvlc_event_t event ;
event.type = libvlc_MediaPlayerSnapshotTaken ;
event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
/* Snapshot psz data is a vlc_variable owned by libvlc object .
Its memmory management is taken care by the obj*/
msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p"
" (%s)", event.u.media_player_snapshot_taken.psz_filename,
event.u.media_player_snapshot_taken.psz_filename );
libvlc_event_send( p_mi->p_event_manager, &event );
return VLC_SUCCESS;
}
/************************************************************************** /**************************************************************************
* Tell media player to start playing. * Tell media player to start playing.
**************************************************************************/ **************************************************************************/
...@@ -551,11 +558,11 @@ void libvlc_media_player_play( libvlc_media_player_t *p_mi, ...@@ -551,11 +558,11 @@ void libvlc_media_player_play( libvlc_media_player_t *p_mi,
/* Ignore previous exception */ /* Ignore previous exception */
libvlc_exception_clear( p_e ); libvlc_exception_clear( p_e );
vlc_mutex_lock( &p_mi->object_lock ); lock(p_mi);
if( !p_mi->p_md ) if( !p_mi->p_md )
{ {
vlc_mutex_unlock( &p_mi->object_lock ); unlock(p_mi);
libvlc_exception_raise( p_e ); libvlc_exception_raise( p_e );
libvlc_printerr( "No associated media descriptor" ); libvlc_printerr( "No associated media descriptor" );
return; return;
...@@ -566,7 +573,7 @@ void libvlc_media_player_play( libvlc_media_player_t *p_mi, ...@@ -566,7 +573,7 @@ void libvlc_media_player_play( libvlc_media_player_t *p_mi,
if( !p_mi->p_input_thread ) if( !p_mi->p_input_thread )
{ {
vlc_mutex_unlock( &p_mi->object_lock ); unlock(p_mi);
return; return;
} }
...@@ -599,7 +606,7 @@ void libvlc_media_player_play( libvlc_media_player_t *p_mi, ...@@ -599,7 +606,7 @@ void libvlc_media_player_play( libvlc_media_player_t *p_mi,
p_mi->p_input_thread = NULL; p_mi->p_input_thread = NULL;
} }
vlc_mutex_unlock( &p_mi->object_lock ); unlock(p_mi);
} }
/************************************************************************** /**************************************************************************
...@@ -660,9 +667,9 @@ void libvlc_media_player_stop( libvlc_media_player_t *p_mi, ...@@ -660,9 +667,9 @@ void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
libvlc_event_send( p_mi->p_event_manager, &event ); libvlc_event_send( p_mi->p_event_manager, &event );
} }
vlc_mutex_lock( &p_mi->object_lock ); lock(p_mi);
release_input_thread( p_mi, true ); /* This will stop the input thread */ release_input_thread( p_mi, true ); /* This will stop the input thread */
vlc_mutex_unlock( &p_mi->object_lock ); unlock(p_mi);
} }
/************************************************************************** /**************************************************************************
...@@ -1080,8 +1087,7 @@ libvlc_state_t libvlc_media_player_get_state( ...@@ -1080,8 +1087,7 @@ libvlc_state_t libvlc_media_player_get_state(
if( !p_input_thread ) if( !p_input_thread )
{ {
/* We do return the right value, no need to throw an exception */ /* We do return the right value, no need to throw an exception */
if( libvlc_exception_raised( p_e ) ) clear_if_needed(p_e);
libvlc_exception_clear( p_e );
return state; return state;
} }
...@@ -1107,8 +1113,7 @@ int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi, ...@@ -1107,8 +1113,7 @@ int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
if ( !p_input_thread ) if ( !p_input_thread )
{ {
/* We do return the right value, no need to throw an exception */ /* We do return the right value, no need to throw an exception */
if( libvlc_exception_raised( p_e ) ) clear_if_needed(p_e);
libvlc_exception_clear( p_e );
return false; return false;
} }
b_seekable = var_GetBool( p_input_thread, "can-seek" ); b_seekable = var_GetBool( p_input_thread, "can-seek" );
...@@ -1201,8 +1206,7 @@ int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi, ...@@ -1201,8 +1206,7 @@ int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
if ( !p_input_thread ) if ( !p_input_thread )
{ {
/* We do return the right value, no need to throw an exception */ /* We do return the right value, no need to throw an exception */
if( libvlc_exception_raised( p_e ) ) clear_if_needed(p_e);
libvlc_exception_clear( p_e );
return false; return false;
} }
b_can_pause = var_GetBool( p_input_thread, "can-pause" ); b_can_pause = var_GetBool( p_input_thread, "can-pause" );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment