]> git.sesse.net Git - vlc/blobdiff - src/control/mediacontrol_core.c
Replace libvlc_exception_get_message with libvlc_errmsg
[vlc] / src / control / mediacontrol_core.c
index db2907cb75af5ee74386e538925124a01757968d..9281ac06eb95e6292ca4703e0824c4ec572cdb59 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include "mediacontrol_internal.h"
 #include <vlc/mediacontrol.h>
 
 #include <vlc/libvlc.h>
+#include <vlc_common.h>
 #include <vlc_interface.h>
 #include <vlc_playlist.h>
 
@@ -54,62 +59,57 @@ mediacontrol_Instance* mediacontrol_new( int argc, char** argv, mediacontrol_Exc
 {
     mediacontrol_Instance* retval;
     libvlc_exception_t ex;
-    char** ppsz_argv = NULL;
-    int i_index;
 
     libvlc_exception_init( &ex );
     mediacontrol_exception_init( exception );
 
     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
-    if( !retval ) 
+    if( !retval )
         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
 
-    /* Prepend a dummy argv[0] so that users of the API do not have to  
-       do it themselves, and can simply provide the args list. */ 
-    ppsz_argv = malloc( ( argc + 2 ) * sizeof( char * ) ) ; 
-    if( ! ppsz_argv )  
-        RAISE_NULL( mediacontrol_InternalException, "Out of memory" ); 
-                
-    ppsz_argv[0] = strdup("vlc"); 
-    for ( i_index = 0; i_index < argc; i_index++ ) 
-        ppsz_argv[i_index + 1] = argv[i_index]; 
-    ppsz_argv[argc + 1] = NULL; 
-                    
-    retval->p_instance = libvlc_new( argc + 1, ppsz_argv, &ex );
-    retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
+    retval->p_instance = libvlc_new( argc, (const char**)argv, &ex );
     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
-    return retval;  
-};
+    retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
+    HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
+    return retval;
+}
 
 void
 mediacontrol_exit( mediacontrol_Instance *self )
 {
-    libvlc_exception_t ex;
-    libvlc_exception_init( &ex );
-
-    libvlc_destroy( self->p_instance, &ex );
+    libvlc_release( self->p_instance );
 }
 
 libvlc_instance_t*
 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
 {
-  return self->p_instance;
+    return self->p_instance;
+}
+
+libvlc_media_player_t*
+mediacontrol_get_media_player( mediacontrol_Instance *self )
+{
+    return self->p_media_player;
 }
 
 mediacontrol_Instance *
 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
-                               mediacontrol_Exception *exception )
+                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;  
+    mediacontrol_Instance* retval;
+    libvlc_exception_t ex;
+
+    libvlc_exception_init( &ex );
+
+    retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
+    if( ! retval )
+    {
+        RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
+    }
+    retval->p_instance = p_instance;
+    retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
+    HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
+    return retval;
 }
 
 /**************************************************************************
@@ -123,8 +123,7 @@ mediacontrol_get_media_position( mediacontrol_Instance *self,
 {
     mediacontrol_Position* retval = NULL;
     libvlc_exception_t ex;
-    vlc_int64_t pos;
-    libvlc_media_instance_t * p_mi;
+    int64_t pos;
 
     mediacontrol_exception_init( exception );
     libvlc_exception_init( &ex );
@@ -133,19 +132,16 @@ mediacontrol_get_media_position( mediacontrol_Instance *self,
     retval->origin = an_origin;
     retval->key = a_key;
 
-    p_mi = libvlc_playlist_get_media_instance( self->p_instance, &ex);
-    HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
-
     if(  an_origin != mediacontrol_AbsolutePosition )
     {
-        libvlc_media_instance_destroy_and_detach( p_mi );
+        free( retval );
         /* Relative or ModuloPosition make no sense */
         RAISE_NULL( mediacontrol_PositionOriginNotSupported,
                     "Only absolute position is valid." );
     }
 
     /* We are asked for an AbsolutePosition. */
-    pos = libvlc_media_instance_get_time( p_mi, &ex );
+    pos = libvlc_media_player_get_time( self->p_media_player, &ex );
 
     if( a_key == mediacontrol_MediaTime )
     {
@@ -153,18 +149,11 @@ mediacontrol_get_media_position( mediacontrol_Instance *self,
     }
     else
     {
-        if( ! self->p_playlist->p_input )
-        {
-            libvlc_media_instance_destroy_and_detach( p_mi );
-            RAISE_NULL( mediacontrol_InternalException,
-                        "No input" );
-        }
-        retval->value = mediacontrol_unit_convert( self->p_playlist->p_input,
-                                                   mediacontrol_MediaTime,
-                                                   a_key,
-                                                   pos );
+        retval->value = private_mediacontrol_unit_convert( self->p_media_player,
+                                                           mediacontrol_MediaTime,
+                                                           a_key,
+                                                           pos );
     }
-    libvlc_media_instance_destroy_and_detach( p_mi );
     return retval;
 }
 
@@ -174,19 +163,14 @@ mediacontrol_set_media_position( mediacontrol_Instance *self,
                                  const mediacontrol_Position * a_position,
                                  mediacontrol_Exception *exception )
 {
-    libvlc_media_instance_t * p_mi;
     libvlc_exception_t ex;
-    vlc_int64_t i_pos;
+    int64_t i_pos;
 
     libvlc_exception_init( &ex );
     mediacontrol_exception_init( exception );
 
-    p_mi = libvlc_playlist_get_media_instance( self->p_instance, &ex);
-    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
-
-    i_pos = mediacontrol_position2microsecond( self->p_playlist->p_input, a_position );
-    libvlc_media_instance_set_time( p_mi, i_pos / 1000, &ex );
-    libvlc_media_instance_destroy_and_detach( p_mi );
+    i_pos = private_mediacontrol_position2microsecond( self->p_media_player, a_position );
+    libvlc_media_player_set_time( self->p_media_player, i_pos / 1000, &ex );
     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
 }
 
@@ -195,7 +179,7 @@ mediacontrol_set_media_position( mediacontrol_Instance *self,
  * Known issues: since moving in the playlist using playlist_Next
  * or playlist_Prev implies starting to play items, the a_position
  * argument will be only honored for the 1st item in the list.
- * 
+ *
  * XXX:FIXME split moving in the playlist and playing items two
  * different actions or make playlist_<Next|Prev> accept a time
  * value to start to play from.
@@ -205,167 +189,136 @@ mediacontrol_start( mediacontrol_Instance *self,
                     const mediacontrol_Position * a_position,
                     mediacontrol_Exception *exception )
 {
-    playlist_t * p_playlist = self->p_playlist;
+    libvlc_media_t * p_media;
+    char * psz_name;
+    libvlc_exception_t ex;
 
     mediacontrol_exception_init( exception );
-    if( ! p_playlist )
+    libvlc_exception_init( &ex );
+
+    p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
+    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
+
+    if ( ! p_media )
     {
-        RAISE( mediacontrol_PlaylistException, "No available playlist" );
-        return;
+        /* No media was defined. */
+        RAISE( mediacontrol_PlaylistException, "No defined media." );
     }
-
-    vlc_mutex_lock( &p_playlist->object_lock );
-    if( p_playlist->items.i_size )
+    else
     {
-        int i_from;
-        char *psz_from = NULL;
+        /* A media was defined. Get its mrl to reuse it, but reset the options
+           (because start-time may have been set on the previous invocation */
+        psz_name = libvlc_media_get_mrl( p_media, &ex );
+        HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
 
-        psz_from = ( char * )malloc( 20 * sizeof( char ) );
-        if( psz_from && p_playlist->status.p_item )
-        {
-            i_from = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
+        /* Create a new media */
+        p_media = libvlc_media_new( self->p_instance, psz_name, &ex );
+        HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
 
-            /* Set start time */
-            snprintf( psz_from, 20, "start-time=%i", i_from );
-            input_ItemAddOption( p_playlist->status.p_item->p_input, psz_from );
-            free( psz_from );
+        if( a_position->value )
+        {
+            char * psz_from;
+            libvlc_time_t i_from;
+
+            /* A start position was specified. Add it to media options */
+            psz_from = ( char * )malloc( 20 * sizeof( char ) );
+            i_from = private_mediacontrol_position2microsecond( self->p_media_player, a_position ) / 1000000;
+            snprintf( psz_from, 20, "start-time=%"PRId64, i_from );
+            libvlc_media_add_option( p_media, psz_from, &ex );
+            HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
         }
 
-        vlc_mutex_unlock( &p_playlist->object_lock );
-        playlist_Play( p_playlist );
-    }
-    else
-    {
-        RAISE( mediacontrol_PlaylistException, "Empty playlist." );
-        vlc_mutex_unlock( &p_playlist->object_lock );
+        libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
+        HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
+
+        libvlc_media_player_play( self->p_media_player, &ex );
+        HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
     }
 }
 
 void
 mediacontrol_pause( mediacontrol_Instance *self,
-                    const mediacontrol_Position * a_position,
                     mediacontrol_Exception *exception )
 {
-    input_thread_t *p_input = self->p_playlist->p_input;
+    libvlc_exception_t ex;
 
-    /* FIXME: use the a_position parameter */
     mediacontrol_exception_init( exception );
-    if( p_input != NULL )
-    {
-        var_SetInteger( p_input, "state", PAUSE_S );
-    }
-    else
-    {
-        RAISE( mediacontrol_InternalException, "No input" );
-    }
+    libvlc_exception_init( &ex );
+    libvlc_media_player_pause( self->p_media_player, &ex );
+    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
 }
 
 void
 mediacontrol_resume( mediacontrol_Instance *self,
-                     const mediacontrol_Position * a_position,
                      mediacontrol_Exception *exception )
 {
-    input_thread_t *p_input = self->p_playlist->p_input;
+    libvlc_exception_t ex;
 
-    /* FIXME: use the a_position parameter */
     mediacontrol_exception_init( exception );
-    if( p_input != NULL )
-    {
-        var_SetInteger( p_input, "state", PAUSE_S );
-    }
-    else
-    {
-        RAISE( mediacontrol_InternalException, "No input" );
-    }
+    libvlc_exception_init( &ex );
+    libvlc_media_player_pause( self->p_media_player, &ex );
+    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
 }
 
 void
 mediacontrol_stop( mediacontrol_Instance *self,
-                   const mediacontrol_Position * a_position,
                    mediacontrol_Exception *exception )
 {
-    /* FIXME: use the a_position parameter */
+    libvlc_exception_t ex;
+
     mediacontrol_exception_init( exception );
-    if( !self->p_playlist )
-    {
-        RAISE( mediacontrol_PlaylistException, "No playlist" );
-    }
-    else
-        playlist_Stop( self->p_playlist );
+    libvlc_exception_init( &ex );
+    libvlc_media_player_stop( self->p_media_player, &ex );
+    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
 }
 
 /**************************************************************************
- * Playlist management
+ * File management
  **************************************************************************/
 
 void
-mediacontrol_playlist_add_item( mediacontrol_Instance *self,
-                                const char * psz_file,
-                                mediacontrol_Exception *exception )
+mediacontrol_set_mrl( mediacontrol_Instance *self,
+                      const char * psz_file,
+                      mediacontrol_Exception *exception )
 {
+    libvlc_media_t * p_media;
     libvlc_exception_t ex;
 
     mediacontrol_exception_init( exception );
     libvlc_exception_init( &ex );
 
-    libvlc_playlist_add( self->p_instance, psz_file, psz_file, &ex );
+    p_media = libvlc_media_new( self->p_instance, psz_file, &ex );
     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
-}
 
-void
-mediacontrol_playlist_next_item( mediacontrol_Instance *self,
-                                 mediacontrol_Exception *exception )
-{
-    libvlc_exception_t ex;
-
-    mediacontrol_exception_init( exception );
-    libvlc_exception_init( &ex );
-
-    libvlc_playlist_next( self->p_instance, &ex );
+    libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
 }
 
-void
-mediacontrol_playlist_clear( mediacontrol_Instance *self,
-                             mediacontrol_Exception *exception )
+char *
+mediacontrol_get_mrl( mediacontrol_Instance *self,
+                      mediacontrol_Exception *exception )
 {
+    libvlc_media_t * p_media;
     libvlc_exception_t ex;
 
     mediacontrol_exception_init( exception );
     libvlc_exception_init( &ex );
 
-    libvlc_playlist_clear( self->p_instance, &ex );
-    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
-}
-
-mediacontrol_PlaylistSeq *
-mediacontrol_playlist_get_list( mediacontrol_Instance *self,
-                                mediacontrol_Exception *exception )
-{
-    mediacontrol_PlaylistSeq *retval = NULL;
-    int i_index;
-    playlist_t * p_playlist = self->p_playlist;
-    int i_playlist_size;
+    p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
+    HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
 
-    mediacontrol_exception_init( exception );
-    if( !p_playlist )
+    if ( ! p_media )
     {
-        RAISE( mediacontrol_PlaylistException, "No playlist" );
-        return NULL;
+        return strdup( "" );
     }
-
-    vlc_mutex_lock( &p_playlist->object_lock );
-    i_playlist_size = p_playlist->current.i_size;
-
-    retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
-
-    for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
+    else
     {
-        retval->data[i_index] = strdup( ARRAY_VAL(p_playlist->current, i_index)->p_input->psz_uri );
-    }
-    vlc_mutex_unlock( &p_playlist->object_lock );
+        char * psz_mrl;
 
-    return retval;
+        psz_mrl = libvlc_media_get_mrl( p_media, &ex );
+        HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
+        return psz_mrl;
+    }
 }
 
 /***************************************************************************
@@ -377,9 +330,12 @@ mediacontrol_get_stream_information( mediacontrol_Instance *self,
                                      mediacontrol_PositionKey a_key,
                                      mediacontrol_Exception *exception )
 {
+    (void)a_key;
     mediacontrol_StreamInformation *retval = NULL;
-    input_thread_t *p_input = self->p_playlist->p_input;
-    vlc_value_t val;
+    libvlc_media_t * p_media;
+    libvlc_exception_t ex;
+
+    libvlc_exception_init( &ex );
 
     retval = ( mediacontrol_StreamInformation* )
                             malloc( sizeof( mediacontrol_StreamInformation ) );
@@ -389,50 +345,83 @@ mediacontrol_get_stream_information( mediacontrol_Instance *self,
         return NULL;
     }
 
-    if( ! p_input )
+    p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
+    if( libvlc_exception_raised( &ex ) )
     {
-        /* No p_input defined */
+        free( retval );
+        RAISE( mediacontrol_InternalException, libvlc_errmsg( ) );
+        libvlc_exception_clear( &ex );
+        return NULL;
+    }
+
+    if( ! p_media )
+    {
+        /* No p_media defined */
         retval->streamstatus = mediacontrol_UndefinedStatus;
-        retval->url          = strdup( "None" );
+        retval->url          = strdup( "" );
         retval->position     = 0;
         retval->length       = 0;
     }
     else
     {
-        switch( var_GetInteger( p_input, "state" ) )
+        libvlc_state_t state;
+
+        state = libvlc_media_player_get_state( self->p_media_player, &ex );
+        if( libvlc_exception_raised( &ex ) )
+        {
+            free( retval );
+            RAISE( mediacontrol_InternalException, libvlc_errmsg() );
+            libvlc_exception_clear( &ex );
+            return NULL;
+        }
+
+        switch( state )
         {
-        case PLAYING_S     :
+        case libvlc_NothingSpecial:
+            retval->streamstatus = mediacontrol_UndefinedStatus;
+            break;
+        case libvlc_Opening :
+            retval->streamstatus = mediacontrol_InitStatus;
+            break;
+        case libvlc_Buffering:
+            retval->streamstatus = mediacontrol_BufferingStatus;
+            break;
+        case libvlc_Playing:
             retval->streamstatus = mediacontrol_PlayingStatus;
             break;
-        case PAUSE_S       :
+        case libvlc_Paused:
             retval->streamstatus = mediacontrol_PauseStatus;
             break;
-        case INIT_S        :
-            retval->streamstatus = mediacontrol_InitStatus;
+        case libvlc_Stopped:
+            retval->streamstatus = mediacontrol_StopStatus;
             break;
-        case END_S         :
+        case libvlc_Ended:
             retval->streamstatus = mediacontrol_EndStatus;
             break;
+        case libvlc_Error:
+            retval->streamstatus = mediacontrol_ErrorStatus;
+            break;
         default :
             retval->streamstatus = mediacontrol_UndefinedStatus;
             break;
         }
 
-        retval->url = strdup( input_GetItem(p_input)->psz_uri );
-
-        /* TIME and LENGTH are in microseconds. We want them in ms */
-        var_Get( p_input, "time", &val);
-        retval->position = val.i_time / 1000;
+        retval->url = libvlc_media_get_mrl( p_media, &ex );
+       
+        retval->position = libvlc_media_player_get_time( self->p_media_player, &ex );
+        if( libvlc_exception_raised( &ex ) )
+        {
+            libvlc_exception_clear( &ex );
+            retval->position = 0;
+        }
 
-        var_Get( p_input, "length", &val);
-        retval->length = val.i_time / 1000;
+        retval->length = libvlc_media_player_get_length( self->p_media_player, &ex );
+        if( libvlc_exception_raised( &ex ) )
+        {
+            libvlc_exception_clear( &ex );
+            retval->length = 0;
+        }
 
-        retval->position = mediacontrol_unit_convert( p_input,
-                                         mediacontrol_MediaTime, a_key,
-                                         retval->position );
-        retval->length   = mediacontrol_unit_convert( p_input,
-                                         mediacontrol_MediaTime, a_key,
-                                         retval->length );
     }
     return retval;
 }