]> git.sesse.net Git - vlc/blobdiff - bindings/python/vlc_mediacontrol.c
Clone video filter : fix potential memleak.
[vlc] / bindings / python / vlc_mediacontrol.c
index 910384c7d7e3693069659ba2104252fade4c5f4a..b5b51ff98959b9d93442b1ad3e93a8a262f64594 100644 (file)
@@ -2,7 +2,7 @@
  * vlc_mediacontrol.c: vlc.MediaControl binding
  *****************************************************************************
  * Copyright (C) 2006 the VideoLAN team
- * $Id$
+ * $Id$
  *
  * Authors: Olivier Aubert <oaubert at bat710.univ-lyon1.fr>
  *
@@ -36,21 +36,22 @@ MediaControl_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
     PyObject* py_param = NULL;
     char** ppsz_args = NULL;
     libvlc_instance_t* p_instance = NULL;
-    int i_size = 0;
-    
+    Py_ssize_t i_size = 0;
+
     self = PyObject_New( MediaControl, &MediaControl_Type );
 
+    fprintf (stderr, "Instanciating mediacontrol\n");
     if( PyArg_ParseTuple( args, "O", &py_param ) )
     {
-        Py_INCREF( py_param );
         if( PyObject_TypeCheck( py_param, &vlcInstance_Type ) == 1 )
         {
             p_instance = ((vlcInstance*)py_param)->p_instance;
         }
         else
         {
-            int i_index;
+            Py_ssize_t i_index;
 
+            Py_INCREF( py_param );
             if( ! PySequence_Check( py_param ) )
             {
                 PyErr_SetString( PyExc_TypeError, "Parameter must be a vlc.Instance or a sequence of strings." );
@@ -74,8 +75,8 @@ MediaControl_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
                                                                        i_index ) ) ) );
             }
             ppsz_args[i_size] = NULL;
+            Py_DECREF( py_param );
         }
-        Py_DECREF( py_param );
     }
     else
     {
@@ -89,10 +90,14 @@ MediaControl_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
     if( p_instance )
     {
         self->mc = mediacontrol_new_from_instance( p_instance, exception );
+        Py_INCREF( py_param );
+        self->vlc_instance = ( vlcInstance* ) py_param;
     }
     else
     {
         self->mc = mediacontrol_new( i_size, ppsz_args, exception );
+        self->vlc_instance = PyObject_New( vlcInstance, &vlcInstance_Type );
+        self->vlc_instance->p_instance = mediacontrol_get_libvlc_instance( SELF->mc );
     }
     MC_EXCEPT;
     Py_END_ALLOW_THREADS
@@ -104,7 +109,9 @@ MediaControl_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
 static void
 MediaControl_dealloc( PyObject *self )
 {
-    PyMem_DEL( self );
+    fprintf(stderr, "MC dealloc\n");
+    Py_DECREF( SELF->vlc_instance );
+    PyObject_DEL( self );
 }
 
 static PyObject *
@@ -112,10 +119,19 @@ MediaControl_get_vlc_instance( PyObject *self, PyObject *args )
 {
     vlcInstance *p_ret;
 
-    p_ret = PyObject_New( vlcInstance, &vlcInstance_Type );
-    if( !p_ret )
-        return NULL;
-    p_ret->p_instance = mediacontrol_get_libvlc_instance( SELF->mc );
+    p_ret = SELF->vlc_instance;
+    Py_INCREF( p_ret );
+    return ( PyObject * )p_ret;
+}
+
+static PyObject *
+MediaControl_get_mediaplayer( PyObject *self, PyObject *args )
+{
+    vlcMediaPlayer *p_ret;
+
+    p_ret = PyObject_New( vlcMediaPlayer, &vlcMediaPlayer_Type );
+    p_ret->p_mp = mediacontrol_get_media_player( SELF->mc );
+    Py_INCREF( p_ret );
     return ( PyObject * )p_ret;
 }
 
@@ -212,24 +228,11 @@ MediaControl_start( PyObject *self, PyObject *args )
 static PyObject *
 MediaControl_pause( PyObject *self, PyObject *args )
 {
-    mediacontrol_Position *a_position;
     mediacontrol_Exception *exception = NULL;
-    PyObject *py_pos;
-
-    if( !PyArg_ParseTuple( args, "O", &py_pos ) )
-    {
-        /* No argument. Use a default 0 value. */
-        PyErr_Clear( );
-        py_pos = NULL;
-    }
-    a_position = position_py_to_c( py_pos );
-    if( !a_position )
-        return NULL;
 
     Py_BEGIN_ALLOW_THREADS
     MC_TRY;
-    mediacontrol_pause( SELF->mc, a_position, exception );
-    free( a_position );
+    mediacontrol_pause( SELF->mc, exception );
     Py_END_ALLOW_THREADS
     MC_EXCEPT;
 
@@ -240,24 +243,11 @@ MediaControl_pause( PyObject *self, PyObject *args )
 static PyObject *
 MediaControl_resume( PyObject *self, PyObject *args )
 {
-    mediacontrol_Position *a_position;
     mediacontrol_Exception *exception = NULL;
-    PyObject *py_pos;
-
-    if( !PyArg_ParseTuple( args, "O", &py_pos ) )
-    {
-        /* No argument. Use a default 0 value. */
-        PyErr_Clear( );
-        py_pos = NULL;
-    }
-    a_position = position_py_to_c( py_pos );
-    if( !a_position )
-        return NULL;
 
     Py_BEGIN_ALLOW_THREADS
     MC_TRY;
-    mediacontrol_start( SELF->mc, a_position, exception );
-    free( a_position );
+    mediacontrol_resume( SELF->mc, exception );
     Py_END_ALLOW_THREADS
     MC_EXCEPT;
 
@@ -268,24 +258,11 @@ MediaControl_resume( PyObject *self, PyObject *args )
 static PyObject *
 MediaControl_stop( PyObject *self, PyObject *args )
 {
-    mediacontrol_Position *a_position;
     mediacontrol_Exception *exception = NULL;
-    PyObject *py_pos;
-
-    if( !PyArg_ParseTuple( args, "O", &py_pos ) )
-    {
-        /* No argument. Use a default 0 value. */
-        PyErr_Clear( );
-        py_pos = NULL;
-    }
-    a_position = position_py_to_c( py_pos );
-    if( !a_position )
-        return NULL;
 
     Py_BEGIN_ALLOW_THREADS
     MC_TRY;
-    mediacontrol_stop( SELF->mc, a_position, exception );
-    free( a_position );
+    mediacontrol_stop( SELF->mc, exception );
     Py_END_ALLOW_THREADS
     MC_EXCEPT;
 
@@ -302,7 +279,7 @@ MediaControl_exit( PyObject *self, PyObject *args )
 }
 
 static PyObject *
-MediaControl_playlist_add_item( PyObject *self, PyObject *args )
+MediaControl_set_mrl( PyObject *self, PyObject *args )
 {
     char *psz_file;
     mediacontrol_Exception *exception = NULL;
@@ -312,7 +289,7 @@ MediaControl_playlist_add_item( PyObject *self, PyObject *args )
 
     Py_BEGIN_ALLOW_THREADS
     MC_TRY;
-    mediacontrol_playlist_add_item( SELF->mc, psz_file, exception );
+    mediacontrol_set_mrl( SELF->mc, psz_file, exception );
     Py_END_ALLOW_THREADS
     MC_EXCEPT;
 
@@ -321,50 +298,23 @@ MediaControl_playlist_add_item( PyObject *self, PyObject *args )
 }
 
 static PyObject *
-MediaControl_playlist_clear( PyObject *self, PyObject *args )
-{
-    mediacontrol_Exception *exception = NULL;
-
-    Py_BEGIN_ALLOW_THREADS
-    MC_TRY;
-    mediacontrol_playlist_clear( SELF->mc, exception );
-    Py_END_ALLOW_THREADS
-    MC_EXCEPT;
-
-    Py_INCREF( Py_None );
-    return Py_None;
-}
-
-static PyObject *
-MediaControl_playlist_get_list( PyObject *self, PyObject *args )
+MediaControl_get_mrl( PyObject *self, PyObject *args )
 {
     PyObject *py_retval;
+    char* psz_file;
     mediacontrol_Exception *exception = NULL;
-    mediacontrol_PlaylistSeq* pl;
-    int i_index;
-    int i_playlist_size;
 
     Py_BEGIN_ALLOW_THREADS
     MC_TRY;
-    pl = mediacontrol_playlist_get_list( SELF->mc, exception );
+    psz_file = mediacontrol_get_mrl( SELF->mc, exception );
     Py_END_ALLOW_THREADS
     MC_EXCEPT;
 
-    i_playlist_size = pl->size;
-
-    py_retval = PyList_New( i_playlist_size );
-
-    for ( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
-    {
-        PyList_SetItem( py_retval, i_index,
-                        Py_BuildValue( "s", pl->data[i_index] ) );
-    }
-    mediacontrol_PlaylistSeq__free( pl );
-
+    py_retval = Py_BuildValue( "s", psz_file );
+    free( psz_file );
     return py_retval;
 }
 
-
 static PyObject *
 MediaControl_snapshot( PyObject *self, PyObject *args )
 {
@@ -406,6 +356,8 @@ MediaControl_snapshot( PyObject *self, PyObject *args )
     PyDict_SetItemString( py_obj, "date",
                           Py_BuildValue( "L", p_retval->date ) );
 
+    mediacontrol_RGBPicture__free( p_retval );
+
     return py_obj;
 }
 
@@ -463,8 +415,7 @@ MediaControl_get_stream_information( PyObject *self, PyObject *args )
     PyDict_SetItemString( py_obj, "length",
                   Py_BuildValue( "L", retval->length ) );
 
-    free( retval->url );
-    free( retval );
+    mediacontrol_StreamInformation__free( retval );
 
     return py_obj;
 }
@@ -599,7 +550,9 @@ MediaControl_set_visual( PyObject *self, PyObject *args )
 static PyMethodDef MediaControl_methods[] =
 {
     { "get_vlc_instance", MediaControl_get_vlc_instance, METH_VARARGS,
-      "get_vlc_instance( ) -> Instance    Get matching vlc.Instance." },
+      "get_vlc_instance( ) -> Instance    Get embedded vlc.Instance." },
+    { "get_mediaplayer", MediaControl_get_mediaplayer, METH_VARARGS,
+      "get_mediaplayer( ) -> MediaPlayer    Get embedded vlc.MediaPlayer." },
     { "get_media_position", MediaControl_get_media_position, METH_VARARGS,
       "get_media_position( origin, key ) -> Position    Get current media position." },
     { "set_media_position", MediaControl_set_media_position, METH_VARARGS,
@@ -614,12 +567,10 @@ static PyMethodDef MediaControl_methods[] =
       "stop( Position )              Stop the player" },
     { "exit", MediaControl_exit, METH_VARARGS,
       "exit( )                     Exit the player" },
-    { "playlist_add_item", MediaControl_playlist_add_item, METH_VARARGS,
-      "playlist_add_item( str )               Add an item to the playlist" },
-    { "playlist_get_list", MediaControl_playlist_get_list, METH_VARARGS,
-      "playlist_get_list( ) -> list       Get the contents of the playlist" },
-    { "playlist_clear", MediaControl_playlist_clear, METH_VARARGS,
-      "clear( )         Clear the playlist." },
+    { "set_mrl", MediaControl_set_mrl, METH_VARARGS,
+      "set_mrl( str )               Set the file to be played" },
+    { "get_mrl", MediaControl_get_mrl, METH_VARARGS,
+      "get_mrl( ) -> str       Get the played file" },
     { "snapshot", MediaControl_snapshot, METH_VARARGS,
       "snapshot( Position ) -> dict        Take a snapshot" },
     { "display_text", MediaControl_display_text, METH_VARARGS,
@@ -667,7 +618,7 @@ static PyTypeObject MediaControl_Type =
     0,                         /*tp_setattro*/
     0,                         /*tp_as_buffer*/
     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
-    "Control of a VLC instance.",  /* tp_doc */
+    "Control of a VLC instance.\n\nvlc.MediaControl(args): initialisation with a list of VLC parameters.\nvlc.MediaControl(instance): initialisation with an existing vlc.Instance",  /* tp_doc */
     0,                     /* tp_traverse */
     0,                     /* tp_clear */
     0,                     /* tp_richcompare */