]> git.sesse.net Git - vlc/blobdiff - src/control/core.c
Replace libvlc_exception_get_message with libvlc_errmsg
[vlc] / src / control / core.c
index 59789ffa3225a3748cae0ec42ef4666003b09d1d..343efdd4650263c9c7bbf2794072e26f39644f5f 100644 (file)
@@ -1,10 +1,10 @@
 /*****************************************************************************
- * core.c: Core functions : init, playlist, stream management
+ * core.c: Core libvlc new API functions : initialization, exceptions handling
  *****************************************************************************
  * Copyright (C) 2005 the VideoLAN team
  * $Id$
  *
- * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
+ * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <vlc/control.h>
+#include "libvlc_internal.h"
+#include <vlc/libvlc.h>
 
-#include <vlc/intf.h>
-#include <vlc/vout.h>
-#include <vlc/aout.h>
-#include <vlc_demux.h>
+#include <vlc_interface.h>
+#include <vlc_vlm.h>
 
-#include <vlc_osd.h>
+#include <stdarg.h>
+#include <limits.h>
+#include <assert.h>
 
-#define HAS_SNAPSHOT 1
+static const char nomemstr[] = "Insufficient memory";
 
-#ifdef HAS_SNAPSHOT
-#include <snapshot.h>
-#endif
-
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>
-
-#include <errno.h>                                                 /* ENOMEM */
-#include <stdio.h>
-#include <ctype.h>
+/*************************************************************************
+ * Exceptions handling
+ *************************************************************************/
+void libvlc_exception_init( libvlc_exception_t *p_exception )
+{
+    p_exception->b_raised = 0;
+}
 
-#ifdef HAVE_UNISTD_H
-#    include <unistd.h>
-#endif
-#ifdef HAVE_SYS_TIME_H
-#    include <sys/time.h>
-#endif
-#ifdef HAVE_SYS_TYPES_H
-#    include <sys/types.h>
-#endif
+void libvlc_exception_clear( libvlc_exception_t *p_exception )
+{
+    if( NULL == p_exception )
+        return;
+    p_exception->b_raised = 0;
+    libvlc_clearerr ();
+}
 
-#define RAISE( c, m )  exception->code = c; \
-                       exception->message = strdup(m);
+int libvlc_exception_raised( const libvlc_exception_t *p_exception )
+{
+    return (NULL != p_exception) && p_exception->b_raised;
+}
 
-vlc_t * vlc_current_object( int );
+static void libvlc_exception_not_handled( const char *psz )
+{
+    fprintf( stderr, "*** LibVLC Exception not handled: %s\nSet a breakpoint in '%s' to debug.\n",
+             psz, __func__ );
+    abort();
+}
 
-mediacontrol_Instance* mediacontrol_new_from_object( int vlc_object_id,
-                                                     mediacontrol_Exception *exception )
+void libvlc_exception_raise( libvlc_exception_t *p_exception,
+                             const char *psz_format, ... )
 {
-    mediacontrol_Instance* retval;
-    vlc_object_t *p_vlc;
-    vlc_object_t *p_object;
+    va_list args;
 
-    p_object = ( vlc_object_t* )vlc_current_object( vlc_object_id );
-    if( ! p_object )
+    /* Make sure that there is no unnoticed previous exception */
+    if( p_exception && p_exception->b_raised )
     {
-        RAISE( mediacontrol_InternalException, "Unable to find vlc object" );
-        return NULL;
+        libvlc_exception_not_handled( libvlc_errmsg() );
+        libvlc_exception_clear( p_exception );
     }
 
-    p_vlc = vlc_object_find( p_object, VLC_OBJECT_ROOT, FIND_PARENT );
-    if( ! p_vlc )
-    {
-        RAISE( mediacontrol_InternalException, "Unable to initialize VLC" );
-        return NULL;
+    /* Unformat-ize the message */
+    va_start( args, psz_format );
+    libvlc_vprinterr( psz_format, args );
+    va_end( args );
+
+    /* Does caller care about exceptions ? */
+    if( p_exception == NULL ) {
+        /* Print something, so that lazy third-parties can easily
+         * notice that something may have gone unnoticedly wrong */
+        libvlc_exception_not_handled( libvlc_errmsg() );
+        return;
     }
-    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 ? */
-    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 );
+    p_exception->b_raised = 1;
+}
 
-    if( ! retval->p_playlist || ! retval->p_intf )
-    {
-        RAISE( mediacontrol_InternalException, "No available interface" );
-        return NULL;
-    }
-    return retval;
-};
-
-
-/**************************************************************************
- * Playback management
- **************************************************************************/
-mediacontrol_Position*
-mediacontrol_get_media_position( mediacontrol_Instance *self,
-                                 const mediacontrol_PositionOrigin an_origin,
-                                 const mediacontrol_PositionKey a_key,
-                                 mediacontrol_Exception *exception )
+libvlc_instance_t * libvlc_new( int argc, const char *const *argv,
+                                libvlc_exception_t *p_e )
 {
-    mediacontrol_Position* retval;
-    vlc_value_t val;
-    input_thread_t * p_input = self->p_playlist->p_input;
-
-    exception = mediacontrol_exception_init( exception );
+    libvlc_instance_t *p_new;
+    int i_ret;
 
-    retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
-    retval->origin = an_origin;
-    retval->key = a_key;
+    libvlc_init_threads ();
 
-    if( ! p_input )
+    libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
+    if( !p_libvlc_int )
     {
-        RAISE( mediacontrol_InternalException, "No input thread." );
-        return NULL;
+        libvlc_deinit_threads ();
+        RAISENULL( "VLC initialization failed" );
     }
 
-    if(  an_origin != mediacontrol_AbsolutePosition )
+    p_new = malloc( sizeof( libvlc_instance_t ) );
+    if( !p_new )
     {
-        /* Relative or ModuloPosition make no sense */
-        RAISE( mediacontrol_PositionOriginNotSupported, "Only absolute position is valid." );
-        return NULL;
+        libvlc_deinit_threads ();
+        RAISENULL( "Out of memory" );
     }
 
-    /* We are asked for an AbsolutePosition. */
-    val.i_time = 0;
-    var_Get( p_input, "time", &val );
-    /* FIXME: check val.i_time > 0 */
+    const char *my_argv[argc + 2];
 
-    retval->value = mediacontrol_unit_convert( p_input,
-                                               mediacontrol_MediaTime,
-                                               a_key,
-                                               val.i_time / 1000 );
-    return retval;
-}
+    my_argv[0] = "libvlc"; /* dummy arg0, skipped by getopt() et al */
+    for( int i = 0; i < argc; i++ )
+         my_argv[i + 1] = argv[i];
+    my_argv[argc + 1] = NULL; /* C calling conventions require a NULL */
 
-/* Sets the media position */
-void
-mediacontrol_set_media_position( mediacontrol_Instance *self,
-                                 const mediacontrol_Position * a_position,
-                                 mediacontrol_Exception *exception )
-{
-    vlc_value_t val;
-    input_thread_t * p_input = self->p_playlist->p_input;
+    /** \todo Look for interface settings. If we don't have any, add -I dummy */
+    /* Because we probably don't want a GUI by default */
 
-    exception=mediacontrol_exception_init( exception );
-    if( ! p_input )
+    i_ret = libvlc_InternalInit( p_libvlc_int, argc + 1, my_argv );
+    if( i_ret )
     {
-        RAISE( mediacontrol_InternalException, "No input thread." );
-        return;
-    }
+        libvlc_InternalDestroy( p_libvlc_int );
+        free( p_new );
+        libvlc_deinit_threads ();
 
-    if( !var_GetBool( p_input, "seekable" ) )
-    {
-        RAISE( mediacontrol_InvalidPosition, "Stream not seekable" );
-        return;
+        if( i_ret == VLC_EEXITSUCCESS )
+            return NULL;
+        else
+            RAISENULL( "VLC initialization failed" );
     }
 
-    val.i_time = mediacontrol_position2microsecond( p_input, a_position );
-    var_Set( p_input, "time", val );
-    return;
+    p_new->p_libvlc_int = p_libvlc_int;
+    p_new->libvlc_vlm.p_vlm = NULL;
+    p_new->libvlc_vlm.p_event_manager = NULL;
+    p_new->libvlc_vlm.pf_release = NULL;
+    p_new->b_playlist_locked = 0;
+    p_new->ref_count = 1;
+    p_new->verbosity = 1;
+    p_new->p_callback_list = NULL;
+    vlc_mutex_init(&p_new->instance_lock);
+    vlc_mutex_init(&p_new->event_callback_lock);
+
+    return p_new;
 }
 
-/* Starts playing a stream */
-void
-mediacontrol_start( mediacontrol_Instance *self,
-                    const mediacontrol_Position * a_position,
-                    mediacontrol_Exception *exception )
+void libvlc_retain( libvlc_instance_t *p_instance )
 {
-    playlist_t * p_playlist = self->p_playlist;
+    assert( p_instance != NULL );
+    assert( p_instance->ref_count < UINT_MAX );
 
-    exception = mediacontrol_exception_init( exception );
-    if( ! p_playlist )
-    {
-        RAISE( mediacontrol_PlaylistException, "No available playlist" );
-        return;
-    }
-
-    vlc_mutex_lock( &p_playlist->object_lock );
-    if( p_playlist->i_size )
-    {
-        int i_from;
-
-        vlc_mutex_unlock( &p_playlist->object_lock );
+    vlc_mutex_lock( &p_instance->instance_lock );
+    p_instance->ref_count++;
+    vlc_mutex_unlock( &p_instance->instance_lock );
+}
 
-        i_from = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
+void libvlc_release( libvlc_instance_t *p_instance )
+{
+    vlc_mutex_t *lock = &p_instance->instance_lock;
+    int refs;
 
-       if( p_playlist->status.p_item )
-        {
-            char psz_from[20];
-            snprintf( psz_from, 20, "start-time=%i", i_from);
-            playlist_ItemAddOption( p_playlist->status.p_item, psz_from);
-        }
+    vlc_mutex_lock( lock );
+    assert( p_instance->ref_count > 0 );
+    refs = --p_instance->ref_count;
+    vlc_mutex_unlock( lock );
 
-        playlist_Play( p_playlist );
-    }
-    else
+    if( refs == 0 )
     {
-        RAISE( mediacontrol_PlaylistException, "Empty playlist." );
-        vlc_mutex_unlock( &p_playlist->object_lock );
-        return;
+        vlc_mutex_destroy( lock );
+        vlc_mutex_destroy( &p_instance->event_callback_lock );
+        if( p_instance->libvlc_vlm.pf_release )
+            p_instance->libvlc_vlm.pf_release( p_instance );
+        libvlc_InternalCleanup( p_instance->p_libvlc_int );
+        libvlc_InternalDestroy( p_instance->p_libvlc_int );
+        free( p_instance );
+        libvlc_deinit_threads ();
     }
-
-    return;
 }
 
-void
-mediacontrol_pause( mediacontrol_Instance *self,
-                    const mediacontrol_Position * a_position,
-                    mediacontrol_Exception *exception )
+int libvlc_add_intf( libvlc_instance_t *p_i, const char *name,
+                      libvlc_exception_t *p_e )
 {
-    input_thread_t *p_input = self->p_playlist->p_input;
-
-    /* FIXME: use the a_position parameter */
-    exception=mediacontrol_exception_init( exception );
-    if( p_input != NULL )
-    {
-        var_SetInteger( p_input, "state", PAUSE_S );
-    }
-    else
+    if( libvlc_InternalAddIntf( p_i->p_libvlc_int, name ) )
     {
-        RAISE( mediacontrol_InternalException, "No input" );
+        libvlc_exception_raise( p_e, "Interface initialization failed" );
+        return -1;
     }
-
-    return;
+    return 0;
 }
 
-void
-mediacontrol_resume( mediacontrol_Instance *self,
-                     const mediacontrol_Position * a_position,
-                     mediacontrol_Exception *exception )
+void libvlc_wait( libvlc_instance_t *p_i )
 {
-    input_thread_t *p_input = self->p_playlist->p_input;
-
-    /* FIXME: use the a_position parameter */
-    exception=mediacontrol_exception_init( exception );
-    if( p_input != NULL )
-    {
-        var_SetInteger( p_input, "state", PAUSE_S );
-    }
-    else
-    {
-        RAISE( mediacontrol_InternalException, "No input" );
-    }
+    libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
+    libvlc_InternalWait( p_libvlc );
 }
 
-void
-mediacontrol_stop( mediacontrol_Instance *self,
-                   const mediacontrol_Position * a_position,
-                   mediacontrol_Exception *exception )
+const char * libvlc_get_version(void)
 {
-    /* FIXME: use the a_position parameter */
-    exception=mediacontrol_exception_init( exception );
-    if( !self->p_playlist )
-    {
-        RAISE( mediacontrol_PlaylistException, "No playlist" );
-        return;
-    }
-
-    playlist_Stop( self->p_playlist );
+    return VLC_Version();
 }
 
-/**************************************************************************
- * Playlist management
- **************************************************************************/
-
-void
-mediacontrol_playlist_add_item( mediacontrol_Instance *self,
-                                const char * psz_file,
-                                mediacontrol_Exception *exception )
+const char * libvlc_get_compiler(void)
 {
-    exception=mediacontrol_exception_init( exception );
-    if( !self->p_playlist )
-    {
-        RAISE( mediacontrol_InternalException, "No playlist" );
-        return;
-    }
-
-    playlist_Add( self->p_playlist, psz_file, psz_file , PLAYLIST_INSERT,
-                  PLAYLIST_END );
+    return VLC_Compiler();
 }
 
-void
-mediacontrol_playlist_clear( mediacontrol_Instance *self,
-                             mediacontrol_Exception *exception )
+const char * libvlc_get_changeset(void)
 {
-    exception=mediacontrol_exception_init( exception );
-    if( !self->p_playlist )
-    {
-        RAISE( mediacontrol_PlaylistException, "No playlist" );
-        return;
-    }
-
-    playlist_Clear( self->p_playlist );
-
-    return;
+    extern const char psz_vlc_changeset[];
+    return psz_vlc_changeset;
 }
 
-mediacontrol_PlaylistSeq *
-mediacontrol_playlist_get_list( mediacontrol_Instance *self,
-                                mediacontrol_Exception *exception )
+/* export internal libvlc_instance for ugly hacks with libvlccore */
+vlc_object_t *libvlc_get_vlc_instance( libvlc_instance_t* p_instance )
 {
-    mediacontrol_PlaylistSeq *retval;
-    int i_index;
-    playlist_t * p_playlist = self->p_playlist;
-    int i_playlist_size;
-
-    exception=mediacontrol_exception_init( exception );
-    if( !p_playlist )
-    {
-        RAISE( mediacontrol_PlaylistException, "No playlist" );
-        return NULL;
-    }
-
-    vlc_mutex_lock( &p_playlist->object_lock );
-    i_playlist_size = p_playlist->i_size;
-
-    retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
-
-    for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
-    {
-        retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->input.psz_uri );
-    }
-    vlc_mutex_unlock( &p_playlist->object_lock );
-
-    return retval;
+    vlc_object_hold( p_instance->p_libvlc_int ) ;
+    return (vlc_object_t*) p_instance->p_libvlc_int ;
 }
 
-/***************************************************************************
- * Status feedback
- ***************************************************************************/
-
-mediacontrol_StreamInformation *
-mediacontrol_get_stream_information( mediacontrol_Instance *self,
-                                     mediacontrol_PositionKey a_key,
-                                     mediacontrol_Exception *exception )
+void libvlc_free( void *ptr )
 {
-    mediacontrol_StreamInformation *retval;
-    input_thread_t *p_input = self->p_playlist->p_input;
-    vlc_value_t val;
-
-    retval = ( mediacontrol_StreamInformation* )malloc( sizeof( mediacontrol_StreamInformation ) );
-    if( ! retval )
-    {
-        RAISE( mediacontrol_InternalException, "Out of memory" );
-        return NULL;
-    }
-
-    if( ! p_input )
-    {
-        /* No p_input defined */
-        retval->streamstatus = mediacontrol_UndefinedStatus;
-        retval->url          = strdup( "None" );
-        retval->position     = 0;
-        retval->length       = 0;
-    }
-    else
-    {
-        switch( var_GetInteger( p_input, "state" ) )
-        {
-        case PLAYING_S     :
-            retval->streamstatus = mediacontrol_PlayingStatus;
-            break;
-        case PAUSE_S       :
-            retval->streamstatus = mediacontrol_PauseStatus;
-            break;
-        case INIT_S        :
-            retval->streamstatus = mediacontrol_InitStatus;
-            break;
-        case END_S         :
-            retval->streamstatus = mediacontrol_EndStatus;
-            break;
-        default :
-            retval->streamstatus = mediacontrol_UndefinedStatus;
-            break;
-        }
-
-        retval->url = strdup( p_input->input.p_item->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;
-
-        var_Get( p_input, "length", &val);
-        retval->length = val.i_time / 1000;
-
-        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;
+    free( ptr );
 }