1 /*****************************************************************************
2 * vlc_internal.c: vlcinternal python binding module
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Olivier Aubert <oaubert at bat710.univ-lyon1.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "vlc_internal.h"
26 /**************************************************************************
28 **************************************************************************/
30 #ifndef vlcinternalMODINIT_FUNC /* declarations for DLL import/export */
31 #define vlcinternalMODINIT_FUNC void
34 static PyMethodDef vlcinternal_methods[] = {
35 { NULL } /* Sentinel */
38 vlcinternalMODINIT_FUNC
39 initvlcinternal( void )
43 p_module = Py_InitModule3( "vlcinternal", vlcinternal_methods,
44 "VLC media player internal module" );
49 if( PyType_Ready( &vlcObject_Type ) < 0 )
53 Py_INCREF( &vlcObject_Type );
54 PyModule_AddObject( p_module, "Object",
55 ( PyObject * )&vlcObject_Type );
59 /* Make libpostproc happy... */
60 void * fast_memcpy( void * to, const void * from, size_t len )
62 return memcpy( to, from, len );
65 /*****************************************************************************
66 * VLCObject implementation
67 *****************************************************************************/
70 *vlcObject_new( PyTypeObject *p_type, PyObject *p_args, PyObject *p_kwds )
73 vlc_object_t *p_object;
76 self = PyObject_New( vlcObject, &vlcObject_Type );
78 if( !PyArg_ParseTuple( p_args, "i", &i_id ) )
81 /* Maybe we were already initialized */
82 p_object = ( vlc_object_t* )vlc_current_object( i_id );
86 /* Try to initialize */
90 PyErr_SetString( PyExc_StandardError, "Unable to create a VLC instance." );
93 p_object = ( vlc_object_t* )vlc_current_object( i_id );
98 PyErr_SetString( PyExc_StandardError, "Unable to get object." );
102 self->p_object = p_object;
103 self->b_released = 0;
105 Py_INCREF( self ); /* Ah bon ? */
106 return ( PyObject * )self;
110 vlcObject_release( PyObject *self, PyObject *p_args )
112 if( VLCSELF->b_released == 0 )
114 vlc_object_release( VLCSELF->p_object );
115 VLCSELF->b_released = 1;
117 Py_INCREF( Py_None );
122 vlcObject_dealloc( PyObject *self )
124 vlcObject_release( self, NULL );
125 PyObject_DEL( self );
129 vlcObject_find_object( PyObject *self, PyObject *args )
136 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
140 ( aout, decoder, input, httpd, intf, playlist, root, vlc, vout )
142 if( !strncmp( psz_name, "aout", 4 ) )
144 i_object_type = VLC_OBJECT_AOUT;
146 else if (! strncmp( psz_name, "decoder", 7 ) )
148 i_object_type = VLC_OBJECT_DECODER;
150 else if (! strncmp( psz_name, "httpd", 5 ) )
152 i_object_type = VLC_OBJECT_HTTPD;
154 else if (! strncmp( psz_name, "intf", 4 ) )
156 i_object_type = VLC_OBJECT_INTF;
158 else if (! strncmp( psz_name, "input", 5 ) )
160 i_object_type = VLC_OBJECT_INPUT;
162 else if (! strncmp( psz_name, "playlist", 8 ) )
164 i_object_type = VLC_OBJECT_PLAYLIST;
166 else if (! strncmp( psz_name, "libvlc", 6 ) )
168 i_object_type = VLC_OBJECT_LIBVLC;
170 else if (! strncmp( psz_name, "vout", 4 ) )
172 i_object_type = VLC_OBJECT_VOUT;
174 else if (! strncmp( psz_name, "sout", 4 ) )
176 i_object_type = VLC_OBJECT_SOUT;
178 else if (! strncmp( psz_name, "global", 6 ) )
180 i_object_type = VLC_OBJECT_GLOBAL;
182 else if (! strncmp( psz_name, "packetizer", 10 ) )
184 i_object_type = VLC_OBJECT_PACKETIZER;
186 else if (! strncmp( psz_name, "encoder", 7 ) )
188 i_object_type = VLC_OBJECT_ENCODER;
190 else if (! strncmp( psz_name, "vlm", 3 ) )
192 i_object_type = VLC_OBJECT_VLM;
194 else if (! strncmp( psz_name, "announce", 8 ) )
196 i_object_type = VLC_OBJECT_ANNOUNCE;
198 else if (! strncmp( psz_name, "demux", 5 ) )
200 i_object_type = VLC_OBJECT_DEMUX;
202 else if (! strncmp( psz_name, "access", 6 ) )
204 i_object_type = VLC_OBJECT_ACCESS;
206 else if (! strncmp( psz_name, "stream", 6 ) )
208 i_object_type = VLC_OBJECT_STREAM;
210 else if (! strncmp( psz_name, "filter", 6 ) )
212 i_object_type = VLC_OBJECT_FILTER;
214 else if (! strncmp( psz_name, "vod", 3 ) )
216 i_object_type = VLC_OBJECT_VOD;
218 else if (! strncmp( psz_name, "xml", 3 ) )
220 i_object_type = VLC_OBJECT_XML;
222 else if (! strncmp( psz_name, "osdmenu", 7 ) )
224 i_object_type = VLC_OBJECT_OSDMENU;
226 else if (! strncmp( psz_name, "stats", 5 ) )
228 i_object_type = VLC_OBJECT_STATS;
230 else if (! strncmp( psz_name, "metaengine", 10 ) )
232 i_object_type = VLC_OBJECT_META_ENGINE;
236 /* FIXME: raise an exception ? */
237 Py_INCREF( Py_None );
241 p_obj = vlc_object_find( VLCSELF->p_object, i_object_type, FIND_ANYWHERE );
245 Py_INCREF( Py_None );
249 p_retval = PyObject_New( vlcObject, &vlcObject_Type );
251 p_retval->p_object = p_obj;
253 return ( PyObject * )p_retval;
257 vlcObject_find_name( PyObject *self, PyObject *args )
264 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
267 p_obj = vlc_object_find_name( VLCSELF->p_object, psz_name, FIND_ANYWHERE );
271 Py_INCREF( Py_None );
275 p_retval = PyObject_New( vlcObject, &vlcObject_Type );
277 p_retval->p_object = p_obj;
279 return ( PyObject * )p_retval;
283 vlcObject_info( PyObject *self, PyObject *args )
288 p_obj = VLCSELF->p_object;
290 /* Return information about the object as a dict. */
291 p_retval = PyDict_New();
293 PyDict_SetItemString( p_retval, "object-id",
294 Py_BuildValue( "l", p_obj->i_object_id ) );
295 PyDict_SetItemString( p_retval, "object-type",
296 Py_BuildValue( "s", p_obj->psz_object_type ) );
297 PyDict_SetItemString( p_retval, "object-name",
298 Py_BuildValue( "s", p_obj->psz_object_name ) );
299 PyDict_SetItemString( p_retval, "thread",
300 PyBool_FromLong( p_obj->b_thread ) );
301 PyDict_SetItemString( p_retval, "thread-id",
302 PyLong_FromLongLong( p_obj->thread_id ) );
303 PyDict_SetItemString( p_retval, "refcount",
304 PyInt_FromLong( p_obj->i_refcount ) );
310 vlcObject_find_id( PyObject *self, PyObject *args )
313 vlc_object_t* p_object;
316 if( !PyArg_ParseTuple( args, "i", &i_id ) )
319 p_object = ( vlc_object_t* )vlc_current_object( i_id );
323 Py_INCREF( Py_None );
327 p_retval = PyObject_NEW( vlcObject, &vlcObject_Type );
329 p_retval->p_object = p_object;
331 return ( PyObject * )p_retval;
334 /* Do a var_Get call on the object. Parameter: the variable name. */
336 vlcObject_var_get( PyObject *self, PyObject *args )
343 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
346 if( var_Get( VLCSELF->p_object, psz_name, &value ) != VLC_SUCCESS )
348 PyErr_SetString( PyExc_StandardError,
349 "Error: variable does not exist.\n" );
353 i_type = var_Type ( VLCSELF->p_object, psz_name );
358 p_retval = PyString_FromString( "A void variable" );
361 p_retval = PyBool_FromLong( value.b_bool );
363 case VLC_VAR_INTEGER :
364 p_retval = PyInt_FromLong( ( long )value.i_int );
366 case VLC_VAR_HOTKEY :
367 p_retval = PyString_FromFormat( "A hotkey variable ( %d )", value.i_int );
370 case VLC_VAR_STRING :
371 case VLC_VAR_DIRECTORY :
372 case VLC_VAR_VARIABLE :
373 p_retval = PyString_FromString( value.psz_string );
375 case VLC_VAR_MODULE :
376 p_retval = ( PyObject* )PyObject_New( vlcObject, &vlcObject_Type );
377 ( ( vlcObject* )p_retval )->p_object = value.p_object;
380 p_retval = PyFloat_FromDouble( ( double )value.f_float );
383 p_retval = PyLong_FromLongLong( value.i_time );
385 case VLC_VAR_ADDRESS :
386 p_retval = PyString_FromString( "A VLC address ( not handled yet )" );
389 p_retval = PyString_FromString( "A VLC list ( not handled yet )" );
392 p_retval = PyString_FromString( "A mutex" );
398 Py_INCREF( p_retval );
403 vlcObject_var_type( PyObject *self, PyObject *args )
409 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
412 i_type = var_Type( VLCSELF->p_object, psz_name );
417 p_retval = PyString_FromString( "Void" );
420 p_retval = PyString_FromString( "Boolean" );
422 case VLC_VAR_INTEGER :
423 p_retval = PyString_FromString( "Integer" );
425 case VLC_VAR_HOTKEY :
426 p_retval = PyString_FromString( "Hotkey" );
429 p_retval = PyString_FromString( "File" );
431 case VLC_VAR_STRING :
432 p_retval = PyString_FromString( "String" );
434 case VLC_VAR_DIRECTORY :
435 p_retval = PyString_FromString( "Directory" );
437 case VLC_VAR_VARIABLE :
438 p_retval = PyString_FromString( "Variable" );
440 case VLC_VAR_MODULE :
441 p_retval = PyString_FromString( "Module" );
444 p_retval = PyString_FromString( "Float" );
447 p_retval = PyString_FromString( "Time" );
449 case VLC_VAR_ADDRESS :
450 p_retval = PyString_FromString( "Address" );
453 p_retval = PyString_FromString( "List" );
456 p_retval = PyString_FromString( "Mutex" );
459 p_retval = PyString_FromString( "Unknown" );
464 /* Do a var_Set call on the object. Parameter: the variable name. */
466 vlcObject_var_set( PyObject *self, PyObject *args )
474 if( !PyArg_ParseTuple( args, "sO", &psz_name, &py_value ) )
477 p_obj = VLCSELF->p_object;
478 i_type = var_Type( p_obj, psz_name );
485 value.b_bool = PyInt_AsLong( py_value );
487 case VLC_VAR_INTEGER :
488 case VLC_VAR_HOTKEY :
489 value.i_int = PyInt_AsLong( py_value );
492 case VLC_VAR_STRING :
493 case VLC_VAR_DIRECTORY :
494 case VLC_VAR_VARIABLE :
495 value.psz_string = strdup( PyString_AsString( py_value ) );
497 case VLC_VAR_MODULE :
498 /* FIXME: we should check the PyObject type and get its p_object */
499 value.p_object = ( ( vlcObject* )p_obj )->p_object;
502 value.f_float = PyFloat_AsDouble( py_value );
505 value.i_time = PyLong_AsLongLong( py_value );
507 case VLC_VAR_ADDRESS :
508 value.p_address = ( char* )PyLong_AsVoidPtr( py_value );
518 var_Set( p_obj, psz_name, value );
520 Py_INCREF( Py_None );
525 vlcObject_var_list( PyObject *self, PyObject *args )
531 i_size = VLCSELF->p_object->i_vars;
532 p_retval = PyTuple_New( i_size );
534 for ( i_index = 0 ; i_index < i_size ; i_index++ )
536 PyTuple_SetItem( p_retval, i_index,
537 Py_BuildValue( "s", VLCSELF->p_object->p_vars[i_index].psz_name ) );
543 /* Do a config_Get call on the object. Parameter: the variable name. */
545 vlcObject_config_get( PyObject *self, PyObject *args )
550 module_config_t *p_config;
552 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
555 p_config = config_FindConfig( VLCSELF->p_object, psz_name );
559 PyErr_SetString( PyExc_StandardError,
560 "Error: config variable does not exist.\n" );
564 switch ( p_config->i_type )
566 case CONFIG_ITEM_BOOL :
567 p_retval = PyBool_FromLong( p_config->value.i );
569 case CONFIG_ITEM_INTEGER :
570 p_retval = PyInt_FromLong( ( long )p_config->value.i );
572 case CONFIG_ITEM_KEY :
573 p_retval = PyString_FromFormat( "A hotkey variable ( %d )", p_config->value.i );
575 case CONFIG_ITEM_FILE :
576 case CONFIG_ITEM_STRING :
577 case CONFIG_ITEM_DIRECTORY :
578 case CONFIG_ITEM_MODULE :
579 vlc_mutex_lock( p_config->p_lock );
580 if( p_config->value.psz )
581 p_retval = PyString_FromString( p_config->value.psz );
583 p_retval = PyString_FromString( "" );
584 vlc_mutex_unlock( p_config->p_lock );
586 p_retval = ( PyObject* )PyObject_New( vlcObject, &vlcObject_Type );
587 ( ( vlcObject* )p_retval )->p_object = value.p_object;
589 case CONFIG_ITEM_FLOAT :
590 p_retval = PyFloat_FromDouble( ( double )p_config->value.f );
594 Py_INCREF( p_retval );
600 /* Do a config_put* call on the object. Parameter: the variable name. */
602 vlcObject_config_set( PyObject *self, PyObject *args )
607 module_config_t *p_config;
610 if( !PyArg_ParseTuple( args, "sO", &psz_name, &py_value ) )
613 p_obj = VLCSELF->p_object;
614 p_config = config_FindConfig( p_obj, psz_name );
618 PyErr_SetString( PyExc_StandardError,
619 "Error: option does not exist.\n" );
623 switch ( p_config->i_type )
625 case CONFIG_ITEM_BOOL :
626 case CONFIG_ITEM_INTEGER :
627 case CONFIG_ITEM_KEY :
628 config_PutInt( p_obj, psz_name, PyInt_AsLong( py_value ) );
630 case CONFIG_ITEM_FILE :
631 case CONFIG_ITEM_STRING :
632 case CONFIG_ITEM_DIRECTORY :
633 case CONFIG_ITEM_MODULE :
634 config_PutPsz( p_obj, psz_name, PyString_AsString( py_value ) );
636 case CONFIG_ITEM_FLOAT :
637 config_PutFloat( p_obj, psz_name, PyFloat_AsDouble( py_value ) );
640 Py_INCREF( Py_None );
645 vlcObject_children( PyObject *self, PyObject *args )
651 i_size = VLCSELF->p_object->i_children;
652 p_retval = PyTuple_New( i_size );
654 for ( i_index = 0 ; i_index < i_size ; i_index++ )
656 PyTuple_SetItem( p_retval, i_index,
658 VLCSELF->p_object->pp_children[i_index]->i_object_id ) );
666 static PyMethodDef vlcObject_methods[] =
668 { "get", vlcObject_var_get, METH_VARARGS,
669 "get( str ) -> value Get a variable value."},
670 { "set", vlcObject_var_set, METH_VARARGS,
671 "set( str, value ) Set a variable value" },
672 { "config_get", vlcObject_config_get, METH_VARARGS,
673 "config_get( str ) -> value Get a configuration option." },
674 { "config_set", vlcObject_config_set, METH_VARARGS,
675 "config_set( str, value ) Set a configuration option" },
676 { "type", vlcObject_var_type, METH_VARARGS,
677 "type( str ) -> str Get a variable type" },
678 { "list", vlcObject_var_list, METH_NOARGS,
679 "list( ) List the available variables" },
680 { "children", vlcObject_children, METH_NOARGS,
681 "children( ) List the children ids" },
682 { "find_object", vlcObject_find_object, METH_VARARGS,
683 "find_object( str ) -> Object Find the object of a given type.\n\nAvailable types are : aout, decoder, input, httpd, intf, playlist, root, vlc, vout"},
684 { "find_id", vlcObject_find_id, METH_VARARGS,
685 "find_id( int ) -> Object Find an object by id" },
686 { "find_name", vlcObject_find_name, METH_VARARGS,
687 "find_name( str ) -> Object Find an object by name" },
688 { "info", vlcObject_info, METH_NOARGS,
689 "info( ) -> dict Return information about the object" },
690 { "release", vlcObject_release, METH_NOARGS,
691 "release( ) -> Release the VLC Object" },
692 { NULL, NULL, 0, NULL },
695 static PyTypeObject vlcObject_Type =
697 PyObject_HEAD_INIT( NULL )
699 "vlc.Object", /*tp_name*/
700 sizeof( vlcObject_Type ), /*tp_basicsize*/
702 ( destructor )vlcObject_dealloc, /*tp_dealloc*/
709 0, /*tp_as_sequence*/
717 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
718 "Expose VLC object infrastructure.\n\nConstructor: vlc.Object(object_id)", /* tp_doc */
721 0, /* tp_richcompare */
722 0, /* tp_weaklistoffset */
725 vlcObject_methods, /* tp_methods */
730 0, /* tp_descr_get */
731 0, /* tp_descr_set */
732 0, /* tp_dictoffset */
735 vlcObject_new, /* tp_new */