Commit 673593dc authored by Olivier Aubert's avatar Olivier Aubert

mediacontrol API: (mostly) use the new libvlc API

parent 12697ee7
...@@ -29,13 +29,13 @@ extern "C" { ...@@ -29,13 +29,13 @@ extern "C" {
# endif # endif
#include <vlc/vlc.h> #include <vlc/vlc.h>
#include "vlc/mediacontrol_structures.h" #include <vlc/mediacontrol_structures.h>
#include <libvlc_internal.h>
#include <vlc/libvlc.h>
struct mediacontrol_Instance { struct mediacontrol_Instance {
vlc_object_t *p_vlc; struct libvlc_instance_t * p_instance;
playlist_t *p_playlist; playlist_t *p_playlist;
intf_thread_t *p_intf;
int vlc_object_id;
}; };
vlc_int64_t mediacontrol_unit_convert( input_thread_t *p_input, vlc_int64_t mediacontrol_unit_convert( input_thread_t *p_input,
...@@ -46,6 +46,28 @@ vlc_int64_t mediacontrol_position2microsecond( ...@@ -46,6 +46,28 @@ vlc_int64_t mediacontrol_position2microsecond(
input_thread_t *p_input, input_thread_t *p_input,
const mediacontrol_Position *pos ); const mediacontrol_Position *pos );
#define RAISE( c, m ) exception->code = c; \
exception->message = strdup(m);
#define RAISE_NULL( c, m ) RAISE( c, m ); return NULL;
#define RAISE_VOID( c, m ) RAISE( c, m ); return;
#define HANDLE_LIBVLC_EXCEPTION_VOID( e ) if( libvlc_exception_raised( e ) ) { \
RAISE( mediacontrol_InternalException, libvlc_exception_get_message( e )); \
libvlc_exception_clear( e ); \
return; }
#define HANDLE_LIBVLC_EXCEPTION_NULL( e ) if( libvlc_exception_raised( e ) ) { \
RAISE( mediacontrol_InternalException, libvlc_exception_get_message( e )); \
libvlc_exception_clear( e ); \
return NULL; }
#define HANDLE_LIBVLC_EXCEPTION_ZERO( e ) if( libvlc_exception_raised( e ) ) { \
RAISE( mediacontrol_InternalException, libvlc_exception_get_message( e )); \
libvlc_exception_clear( e ); \
return 0; }
# ifdef __cplusplus # ifdef __cplusplus
} }
# endif # endif
......
...@@ -114,10 +114,14 @@ void mediacontrol_exception_free(mediacontrol_Exception *exception); ...@@ -114,10 +114,14 @@ void mediacontrol_exception_free(mediacontrol_Exception *exception);
mediacontrol_Instance * mediacontrol_Instance *
mediacontrol_new( char **args, mediacontrol_Exception *exception ); mediacontrol_new( char **args, mediacontrol_Exception *exception );
/* Bridge with the libvlc API */
mediacontrol_Instance * mediacontrol_Instance *
mediacontrol_new_from_object( int vlc_object_id, mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
mediacontrol_Exception *exception ); mediacontrol_Exception *exception );
libvlc_instance_t*
mediacontrol_get_libvlc_instance( mediacontrol_Instance* self );
mediacontrol_Position * mediacontrol_get_media_position( mediacontrol_Position * mediacontrol_get_media_position(
mediacontrol_Instance *self, mediacontrol_Instance *self,
const mediacontrol_PositionOrigin an_origin, const mediacontrol_PositionOrigin an_origin,
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <mediacontrol_internal.h> #include <mediacontrol_internal.h>
#include <vlc/mediacontrol.h> #include <vlc/mediacontrol.h>
#include <vlc/libvlc.h>
#include <vlc/intf.h> #include <vlc/intf.h>
#include <vlc/vout.h> #include <vlc/vout.h>
...@@ -245,17 +246,16 @@ unsigned short ...@@ -245,17 +246,16 @@ unsigned short
mediacontrol_sound_get_volume( mediacontrol_Instance *self, mediacontrol_sound_get_volume( mediacontrol_Instance *self,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
short retval; libvlc_exception_t ex;
audio_volume_t i_volume; int i_ret = 0;
if( !self->p_intf ) mediacontrol_exception_init( exception );
{ libvlc_exception_init( &ex );
RAISE( mediacontrol_InternalException, "No interface module" );
return 0; i_ret = libvlc_audio_get_volume( self->p_instance, &ex );
} HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
aout_VolumeGet( self->p_intf, &i_volume ); /* FIXME: Normalize in [0..100] */
retval = i_volume; return (unsigned short)i_ret;
return retval;
} }
void void
...@@ -263,46 +263,49 @@ mediacontrol_sound_set_volume( mediacontrol_Instance *self, ...@@ -263,46 +263,49 @@ mediacontrol_sound_set_volume( mediacontrol_Instance *self,
const unsigned short volume, const unsigned short volume,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
if( !self->p_intf ) /* FIXME: Normalize in [0..100] */
{ libvlc_exception_t ex;
RAISE( mediacontrol_InternalException, "No interface module" );
} mediacontrol_exception_init( exception );
else aout_VolumeSet( self->p_intf,( audio_volume_t )volume ); libvlc_exception_init( &ex );
libvlc_audio_set_volume( self->p_instance, volume, &ex );
HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
} }
vlc_bool_t mediacontrol_set_visual( mediacontrol_Instance *self, vlc_bool_t mediacontrol_set_visual( mediacontrol_Instance *self,
WINDOWHANDLE visual_id, WINDOWHANDLE visual_id,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
vlc_value_t value; libvlc_exception_t ex;
int ret;
if( !self->p_vlc ) mediacontrol_exception_init( exception );
{ libvlc_exception_init( &ex );
RAISE( mediacontrol_InternalException, "No vlc reference" );
return VLC_FALSE;
}
value.i_int=visual_id;
ret = var_Set(self->p_vlc, "drawable", value);
return (ret == VLC_SUCCESS); libvlc_video_set_parent( self->p_instance, visual_id, &ex );
HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
return VLC_TRUE;
} }
int int
mediacontrol_get_rate( mediacontrol_Instance *self, mediacontrol_get_rate( mediacontrol_Instance *self,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
int retval; libvlc_exception_t ex;
input_thread_t *p_input = NULL; libvlc_input_t* p_input;
int i_ret;
p_input = self->p_playlist->p_input; mediacontrol_exception_init( exception );
if( ! p_input ) libvlc_exception_init( &ex );
{
RAISE( mediacontrol_InternalException, "No input" ); p_input = libvlc_playlist_get_input( self->p_instance, &ex );
return 0; HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
}
retval = var_GetInteger(p_input, "rate") / 10; i_ret = libvlc_input_get_rate( p_input, &ex );
return retval; libvlc_input_free( p_input );
HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
return i_ret / 10;
} }
void void
...@@ -310,42 +313,39 @@ mediacontrol_set_rate( mediacontrol_Instance *self, ...@@ -310,42 +313,39 @@ mediacontrol_set_rate( mediacontrol_Instance *self,
const int rate, const int rate,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
input_thread_t *p_input = NULL; libvlc_exception_t ex;
libvlc_input_t* p_input;
p_input = self->p_playlist->p_input; mediacontrol_exception_init( exception );
if( ! p_input ) libvlc_exception_init( &ex );
{
RAISE( mediacontrol_InternalException, "No input" ); p_input = libvlc_playlist_get_input( self->p_instance, &ex );
return; HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
}
var_SetInteger(p_input, "rate", rate * 10); libvlc_input_set_rate( p_input, rate * 10, &ex );
return; libvlc_input_free( p_input );
HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
} }
int int
mediacontrol_get_fullscreen( mediacontrol_Instance *self, mediacontrol_get_fullscreen( mediacontrol_Instance *self,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
vout_thread_t *p_vout = NULL; libvlc_exception_t ex;
vlc_value_t val; libvlc_input_t* p_input;
int i_ret; int i_ret;
p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD ); mediacontrol_exception_init( exception );
if( ! p_vout ) libvlc_exception_init( &ex );
{
RAISE( mediacontrol_InternalException, "no video output" );
return 0;
}
i_ret = var_Get( p_vout, "fullscreen", &val ); p_input = libvlc_playlist_get_input( self->p_instance, &ex );
vlc_object_release( p_vout ); HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
if( i_ret ) i_ret = libvlc_get_fullscreen( p_input, &ex );
{ libvlc_input_free( p_input );
RAISE( mediacontrol_InternalException, "Unexpected error while looking up fullscreen value" ); HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
return 0;
} return i_ret;
return val.b_bool == VLC_TRUE ? 1 : 0;
} }
void void
...@@ -353,27 +353,16 @@ mediacontrol_set_fullscreen( mediacontrol_Instance *self, ...@@ -353,27 +353,16 @@ mediacontrol_set_fullscreen( mediacontrol_Instance *self,
const int b_fullscreen, const int b_fullscreen,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
vout_thread_t *p_vout = NULL; libvlc_exception_t ex;
vlc_value_t val; libvlc_input_t* p_input;
int i_ret;
p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD ); mediacontrol_exception_init( exception );
if( ! p_vout ) libvlc_exception_init( &ex );
{
RAISE( mediacontrol_InternalException, "no video output" );
return;
}
if( b_fullscreen ) val.b_bool = VLC_TRUE; p_input = libvlc_playlist_get_input( self->p_instance, &ex );
else val.b_bool = VLC_FALSE; HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
i_ret = var_Set( p_vout, "fullscreen", val ); libvlc_set_fullscreen( p_input, b_fullscreen, &ex );
vlc_object_release( p_vout ); libvlc_input_free( p_input );
HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
if( i_ret )
{
RAISE( mediacontrol_InternalException, "Unexpected error while setting fullscreen value" );
return;
}
return;
} }
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <mediacontrol_internal.h> #include <mediacontrol_internal.h>
#include <vlc/mediacontrol.h> #include <vlc/mediacontrol.h>
#include <vlc/libvlc.h>
#include <vlc/intf.h> #include <vlc/intf.h>
#include <vlc/vout.h> #include <vlc/vout.h>
#include <vlc/aout.h> #include <vlc/aout.h>
...@@ -54,46 +55,84 @@ ...@@ -54,46 +55,84 @@
# include <sys/types.h> # include <sys/types.h>
#endif #endif
#define RAISE( c, m ) exception->code = c; \ mediacontrol_Instance* mediacontrol_new( char** args, mediacontrol_Exception *exception )
exception->message = strdup(m);
mediacontrol_Instance* mediacontrol_new_from_object( int vlc_object_id,
mediacontrol_Exception *exception )
{ {
mediacontrol_Instance* retval = NULL; mediacontrol_Instance* retval;
vlc_object_t *p_vlc; libvlc_exception_t ex;
vlc_object_t *p_object; char **ppsz_argv;
int i_count = 0;
int i_index;
char **p_tmp;
libvlc_exception_init( &ex );
exception=mediacontrol_exception_init( exception );
p_object = ( vlc_object_t* )vlc_current_object( vlc_object_id ); /* Copy args array */
if( ! p_object ) if( args )
{ {
RAISE( mediacontrol_InternalException, "unable to find vlc object" ); for ( p_tmp = args ; *p_tmp != NULL ; p_tmp++ )
return NULL; i_count++;
} }
p_vlc = vlc_object_find( p_object, VLC_OBJECT_ROOT, FIND_PARENT ); ppsz_argv = malloc( ( i_count + 2 ) * sizeof( char * ) ) ;
if( ! p_vlc ) if( ! ppsz_argv )
{ {
RAISE( mediacontrol_InternalException, "unable to initialize VLC" ); RAISE_NULL( mediacontrol_InternalException, "out of memory" );
return NULL; }
ppsz_argv[0] = "vlc";
for ( i_index = 0; i_index < i_count; i_index++ )
{
ppsz_argv[i_index + 1] = strdup( args[i_index] );
if( ! ppsz_argv[i_index + 1] )
{
RAISE_NULL( mediacontrol_InternalException, "out of memory" );
}
} }
retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
retval->p_vlc = p_vlc;
retval->vlc_object_id = p_vlc->i_object_id;
/* We can keep references on these, which should not change. Is it true ? */ ppsz_argv[i_count + 2] = NULL;
retval->p_playlist = vlc_object_find( p_vlc,
VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
retval->p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_ANYWHERE );
if( ! retval->p_playlist || ! retval->p_intf ) retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
if( ! retval )
{ {
RAISE( mediacontrol_InternalException, "no interface available" ); RAISE_NULL( mediacontrol_InternalException, "out of memory" );
return NULL;
} }
retval->p_instance = libvlc_new( i_count + 1, ppsz_argv, &ex );
retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
return retval; return retval;
}; };
void
mediacontrol_exit( mediacontrol_Instance *self )
{
libvlc_exception_t ex;
libvlc_exception_init( &ex );
libvlc_destroy( self->p_instance, &ex );
}
libvlc_instance_t*
mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
{
return self->p_instance;
}
mediacontrol_Instance *
mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
mediacontrol_Exception *exception )
{
mediacontrol_Instance* retval;
retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
if( ! retval )
{
RAISE_NULL( mediacontrol_InternalException, "out of memory" );
}
retval->p_instance = p_instance;
retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
return retval;
}
/************************************************************************** /**************************************************************************
* Playback management * Playback management
...@@ -105,38 +144,49 @@ mediacontrol_get_media_position( mediacontrol_Instance *self, ...@@ -105,38 +144,49 @@ mediacontrol_get_media_position( mediacontrol_Instance *self,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
mediacontrol_Position* retval = NULL; mediacontrol_Position* retval = NULL;
vlc_value_t val; libvlc_exception_t ex;
input_thread_t * p_input = self->p_playlist->p_input; vlc_int64_t pos;
libvlc_input_t * p_input;
exception = mediacontrol_exception_init( exception ); exception = mediacontrol_exception_init( exception );
libvlc_exception_init( &ex );
retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) ); retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
retval->origin = an_origin; retval->origin = an_origin;
retval->key = a_key; retval->key = a_key;
if( ! p_input ) p_input = libvlc_playlist_get_input( self->p_instance, &ex);
{ HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
RAISE( mediacontrol_InternalException, "No input thread." );
return NULL;
}
if( an_origin != mediacontrol_AbsolutePosition ) if( an_origin != mediacontrol_AbsolutePosition )
{ {
libvlc_input_free( p_input );
/* Relative or ModuloPosition make no sense */ /* Relative or ModuloPosition make no sense */
RAISE( mediacontrol_PositionOriginNotSupported, RAISE_NULL( mediacontrol_PositionOriginNotSupported,
"Only absolute position is valid." ); "Only absolute position is valid." );
return NULL;
} }
/* We are asked for an AbsolutePosition. */ /* We are asked for an AbsolutePosition. */
val.i_time = 0; pos = libvlc_input_get_time( p_input, &ex );
var_Get( p_input, "time", &val );
/* FIXME: check val.i_time > 0 */
retval->value = mediacontrol_unit_convert( p_input, if( a_key == mediacontrol_MediaTime )
{
retval->value = pos / 1000;
}
else
{
if( ! self->p_playlist->p_input )
{
libvlc_input_free( p_input );
RAISE_NULL( mediacontrol_InternalException,
"No input" );
}
retval->value = mediacontrol_unit_convert( self->p_playlist->p_input,
mediacontrol_MediaTime, mediacontrol_MediaTime,
a_key, a_key,
val.i_time / 1000 ); pos / 1000 );
}
libvlc_input_free( p_input );
return retval; return retval;
} }
...@@ -146,23 +196,20 @@ mediacontrol_set_media_position( mediacontrol_Instance *self, ...@@ -146,23 +196,20 @@ mediacontrol_set_media_position( mediacontrol_Instance *self,
const mediacontrol_Position * a_position, const mediacontrol_Position * a_position,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
vlc_value_t val; libvlc_input_t * p_input;
input_thread_t * p_input = self->p_playlist->p_input; libvlc_exception_t ex;
vlc_int64_t i_pos;
exception=mediacontrol_exception_init( exception ); libvlc_exception_init( &ex );
if( ! p_input ) mediacontrol_exception_init( exception );
{
RAISE( mediacontrol_InternalException, "No input thread." ); p_input = libvlc_playlist_get_input( self->p_instance, &ex);
} HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
else if( !var_GetBool( p_input, "seekable" ) )
{ i_pos = mediacontrol_position2microsecond( self->p_playlist->p_input, a_position );
RAISE( mediacontrol_InvalidPosition, "Stream not seekable" ); libvlc_input_set_time( p_input, i_pos, &ex );
} libvlc_input_free( p_input );
else HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
{
val.i_time = mediacontrol_position2microsecond( p_input, a_position );
var_Set( p_input, "time", val );
}
} }
/* Starts playing a stream */ /* Starts playing a stream */
...@@ -278,40 +325,39 @@ mediacontrol_playlist_add_item( mediacontrol_Instance *self, ...@@ -278,40 +325,39 @@ mediacontrol_playlist_add_item( mediacontrol_Instance *self,
const char * psz_file, const char * psz_file,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
exception=mediacontrol_exception_init( exception ); libvlc_exception_t ex;
if( !self->p_playlist )
{ mediacontrol_exception_init( exception );
RAISE( mediacontrol_InternalException, "No playlist" ); libvlc_exception_init( &ex );
}
else libvlc_playlist_add( self->p_instance, psz_file, psz_file, &ex );
playlist_PlaylistAdd( self->p_playlist, psz_file, psz_file , PLAYLIST_INSERT, HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
PLAYLIST_END );
} }
void void
mediacontrol_playlist_next_item( mediacontrol_Instance *self, mediacontrol_playlist_next_item( mediacontrol_Instance *self,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
exception=mediacontrol_exception_init( exception );