1 /*****************************************************************************
2 * vlc_instance.c: vlc.Instance binding
3 *****************************************************************************
4 * Copyright (C) 2006,2007,2008,2009 the VideoLAN team
7 * Authors: Olivier Aubert <olivier.aubert at liris.cnrs.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 *****************************************************************************/
25 /* Helper functions */
27 pyoptions_to_args(PyObject *py_options, char*** pppsz_args)
32 Py_INCREF( py_options );
33 if( ! PySequence_Check( py_options ) )
35 PyErr_SetString( PyExc_TypeError, "Parameter must be a sequence." );
38 i_size = PySequence_Size( py_options );
40 char **ppsz_args = *pppsz_args = malloc( ( i_size + 1 ) * sizeof( char * ) );
44 PyErr_SetString( PyExc_MemoryError, "Out of memory" );
48 for ( i_index = 0; i_index < i_size; i_index++ )
51 strdup( PyString_AsString( PyObject_Str(
52 PySequence_GetItem( py_options,
55 ppsz_args[i_size] = NULL;
56 Py_DECREF( py_options );
61 free_args(int i_size, char** ppsz_args)
65 for ( i_index = 0; i_index < i_size; i_index++ )
66 free( ppsz_args[i_index] );
70 /*****************************************************************************
71 * Instance object implementation
72 *****************************************************************************/
75 vlcInstance_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
78 libvlc_exception_t ex;
79 PyObject* py_list = NULL;
80 char** ppsz_args = NULL;
83 fprintf(stderr, "Instantiating\n");
84 if( PyArg_ParseTuple( args, "|O", &py_list ) )
86 i_size = pyoptions_to_args( py_list, &ppsz_args );
92 /* No arguments were given. Clear the exception raised
93 by PyArg_ParseTuple. */
97 self = PyObject_New( vlcInstance, &vlcInstance_Type );
99 Py_BEGIN_ALLOW_THREADS
101 LIBVLC_INSTANCE(self) = libvlc_new( i_size, ppsz_args, &ex );
102 free_args( i_size, ppsz_args );
107 return ( PyObject * )self;
111 vlcInstance_dealloc( PyObject *self )
113 libvlc_release( LIBVLC_INSTANCE(self) );
114 PyObject_DEL( self );
118 vlcInstance_get_vlc_id( PyObject *self, PyObject *args )
120 return Py_BuildValue( "i", libvlc_get_vlc_id( LIBVLC_INSTANCE(self) ) );
124 vlcInstance_new_media_player( PyObject *self, PyObject *args )
126 libvlc_exception_t ex;
127 libvlc_media_player_t *p_mp;
128 vlcMediaPlayer *p_ret;
131 p_mp = libvlc_media_player_new( LIBVLC_INSTANCE(self), &ex );
134 p_ret = PyObject_New( vlcMediaPlayer, &vlcMediaPlayer_Type );
136 Py_INCREF( p_ret ); /* Ah bon ? */
137 return ( PyObject * )p_ret;
141 vlcInstance_audio_toggle_mute( PyObject *self, PyObject *args )
143 libvlc_exception_t ex;
145 libvlc_audio_toggle_mute( LIBVLC_INSTANCE(self), &ex );
147 Py_INCREF( Py_None );
152 vlcInstance_audio_get_mute( PyObject *self, PyObject *args )
154 libvlc_exception_t ex;
158 i_ret = libvlc_audio_get_mute( LIBVLC_INSTANCE(self), &ex );
160 return Py_BuildValue( "i", i_ret );
164 vlcInstance_audio_set_mute( PyObject *self, PyObject *args )
166 libvlc_exception_t ex;
169 if( !PyArg_ParseTuple( args, "i", &i_mute ) )
173 libvlc_audio_set_mute( LIBVLC_INSTANCE(self), i_mute, &ex );
175 Py_INCREF( Py_None );
180 vlcInstance_audio_get_volume( PyObject *self, PyObject *args )
182 libvlc_exception_t ex;
186 i_ret = libvlc_audio_get_volume( LIBVLC_INSTANCE(self), &ex );
188 return Py_BuildValue( "i", i_ret );
192 vlcInstance_audio_set_volume( PyObject *self, PyObject *args )
194 libvlc_exception_t ex;
197 if( !PyArg_ParseTuple( args, "i", &i_volume ) )
201 libvlc_audio_set_volume( LIBVLC_INSTANCE(self), i_volume, &ex );
203 Py_INCREF( Py_None );
208 vlcInstance_audio_get_channel( PyObject *self, PyObject *args )
210 libvlc_exception_t ex;
214 i_ret = libvlc_audio_get_channel( LIBVLC_INSTANCE(self), &ex );
216 return Py_BuildValue( "i", i_ret );
220 vlcInstance_audio_set_channel( PyObject *self, PyObject *args )
222 libvlc_exception_t ex;
225 if( !PyArg_ParseTuple( args, "i", &i_channel ) )
229 libvlc_audio_set_channel( LIBVLC_INSTANCE(self), i_channel, &ex );
231 Py_INCREF( Py_None );
235 /* vlm_add_broadcast : name, input MRL, output MRL
236 Keywords: options, enable, loop */
238 vlcInstance_vlm_add_broadcast( PyObject *self, PyObject *args, PyObject *kwds )
240 libvlc_exception_t ex;
241 static char *kwlist[] = { "name", "input", "output",
242 "options", "enable", "loop", NULL};
243 char* psz_name = NULL;
244 char* psz_input = NULL;
245 char* psz_output = NULL;
246 PyObject* py_options = NULL;
250 char** ppsz_args = NULL;
252 if( !PyArg_ParseTupleAndKeywords( args, kwds, "sss|Oii", kwlist,
254 &psz_input, &psz_output,
255 &py_options, &i_enable, &i_loop ) )
260 i_size = pyoptions_to_args( py_options, &ppsz_args );
264 libvlc_vlm_add_broadcast( LIBVLC_INSTANCE(self),
265 psz_name, psz_input, psz_output,
266 i_size, ppsz_args, i_enable, i_loop, &ex);
267 free_args( i_size, ppsz_args );
269 Py_INCREF( Py_None );
274 vlcInstance_vlm_del_media( PyObject *self, PyObject *args )
276 libvlc_exception_t ex;
279 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
283 libvlc_vlm_del_media( LIBVLC_INSTANCE(self), psz_name, &ex);
285 Py_INCREF( Py_None );
290 vlcInstance_vlm_set_enabled( PyObject *self, PyObject *args )
292 libvlc_exception_t ex;
296 if( !PyArg_ParseTuple( args, "si", &psz_name, &i_enabled ) )
300 libvlc_vlm_set_enabled( LIBVLC_INSTANCE(self), psz_name, i_enabled, &ex);
302 Py_INCREF( Py_None );
307 vlcInstance_vlm_set_output( PyObject *self, PyObject *args )
309 libvlc_exception_t ex;
313 if( !PyArg_ParseTuple( args, "ss", &psz_name, &psz_output ) )
317 libvlc_vlm_set_output( LIBVLC_INSTANCE(self), psz_name, psz_output, &ex);
319 Py_INCREF( Py_None );
324 vlcInstance_vlm_set_input( PyObject *self, PyObject *args )
326 libvlc_exception_t ex;
330 if( !PyArg_ParseTuple( args, "ss", &psz_name, &psz_input ) )
334 libvlc_vlm_set_input( LIBVLC_INSTANCE(self), psz_name, psz_input, &ex);
336 Py_INCREF( Py_None );
341 vlcInstance_vlm_add_input( PyObject *self, PyObject *args )
343 libvlc_exception_t ex;
347 if( !PyArg_ParseTuple( args, "ss", &psz_name, &psz_input ) )
351 libvlc_vlm_add_input( LIBVLC_INSTANCE(self), psz_name, psz_input, &ex);
353 Py_INCREF( Py_None );
358 vlcInstance_vlm_set_loop( PyObject *self, PyObject *args )
360 libvlc_exception_t ex;
364 if( !PyArg_ParseTuple( args, "si", &psz_name, &i_loop ) )
368 libvlc_vlm_set_loop( LIBVLC_INSTANCE(self), psz_name, i_loop, &ex);
370 Py_INCREF( Py_None );
375 vlcInstance_vlm_change_media( PyObject *self, PyObject *args, PyObject *kwds )
377 libvlc_exception_t ex;
378 static char *kwlist[] = { "name", "input", "output",
379 "options", "enable", "loop", NULL};
380 char* psz_name = NULL;
381 char* psz_input = NULL;
382 char* psz_output = NULL;
383 PyObject* py_options = NULL;
387 char** ppsz_args = NULL;
389 if( !PyArg_ParseTupleAndKeywords( args, kwds, "sss|Oii", kwlist,
391 &psz_input, &psz_output,
392 &py_options, &i_enable, &i_loop ) )
397 i_size = pyoptions_to_args( py_options, &ppsz_args );
401 libvlc_vlm_change_media( LIBVLC_INSTANCE(self),
402 psz_name, psz_input, psz_output,
403 i_size, ppsz_args, i_enable, i_loop, &ex);
404 free_args( i_size, ppsz_args );
406 Py_INCREF( Py_None );
411 vlcInstance_vlm_play_media( PyObject *self, PyObject *args )
413 libvlc_exception_t ex;
416 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
420 libvlc_vlm_play_media( LIBVLC_INSTANCE(self), psz_name, &ex);
422 Py_INCREF( Py_None );
427 vlcInstance_vlm_stop_media( PyObject *self, PyObject *args )
429 libvlc_exception_t ex;
432 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
436 libvlc_vlm_stop_media( LIBVLC_INSTANCE(self), psz_name, &ex);
438 Py_INCREF( Py_None );
443 vlcInstance_vlm_pause_media( PyObject *self, PyObject *args )
445 libvlc_exception_t ex;
448 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
452 libvlc_vlm_pause_media( LIBVLC_INSTANCE(self), psz_name, &ex);
454 Py_INCREF( Py_None );
459 vlcInstance_vlm_seek_media( PyObject *self, PyObject *args )
461 libvlc_exception_t ex;
465 if( !PyArg_ParseTuple( args, "sf", &psz_name, &f_percentage ) )
469 libvlc_vlm_seek_media( LIBVLC_INSTANCE(self), psz_name, f_percentage, &ex);
471 Py_INCREF( Py_None );
476 vlcInstance_vlm_show_media( PyObject *self, PyObject *args )
478 libvlc_exception_t ex;
483 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
486 psz_ret = libvlc_vlm_show_media( LIBVLC_INSTANCE(self), psz_name, &ex );
488 o_ret = Py_BuildValue( "s", psz_ret );
494 vlcInstance_media_new( PyObject *self, PyObject *args )
496 libvlc_exception_t ex;
497 libvlc_media_t *p_media;
498 char* psz_mrl = NULL;
501 if( !PyArg_ParseTuple( args, "s", &psz_mrl ) )
505 p_media = libvlc_media_new( LIBVLC_INSTANCE(self), psz_mrl, &ex );
508 p_ret = PyObject_New( vlcMedia, &vlcMedia_Type );
509 p_ret->p_media = p_media;
510 Py_INCREF( p_ret ); /* Ah bon ? */
511 return ( PyObject * )p_ret;
515 static PyMethodDef vlcInstance_methods[] =
517 { "get_vlc_id", vlcInstance_get_vlc_id, METH_NOARGS,
518 "get_vlc_id( ) -> int Get the instance id."},
519 { "audio_toggle_mute", vlcInstance_audio_toggle_mute, METH_NOARGS,
520 "audio_toggle_mute() Toggle the mute state"},
521 { "audio_get_mute", vlcInstance_audio_get_mute, METH_NOARGS,
522 "audio_get_mute() -> int Get the mute state"},
523 { "audio_set_mute", vlcInstance_audio_set_mute, METH_VARARGS,
524 "audio_set_mute(state=int) Set the mute state"},
525 { "audio_get_volume", vlcInstance_audio_get_volume, METH_NOARGS,
526 "audio_get_volume() -> int Get the audio volume"},
527 { "audio_set_volume", vlcInstance_audio_set_volume, METH_VARARGS,
528 "audio_set_volume(volume=int) Set the audio volume"},
529 { "audio_get_channel", vlcInstance_audio_get_channel, METH_NOARGS,
530 "audio_get_channel() -> int Get current audio channel" },
531 { "audio_set_channel", vlcInstance_audio_set_channel, METH_VARARGS,
532 "audio_set_channel(int) Set current audio channel" },
534 { "media_new", vlcInstance_media_new, METH_VARARGS,
535 "media_new(str) -> object Create a media object with the given mrl."},
537 { "mediaplayer_new", vlcInstance_new_media_player, METH_NOARGS,
538 "mediaplayer_new() -> object Create a media player."},
540 { "vlm_add_broadcast", vlcInstance_vlm_add_broadcast, METH_VARARGS | METH_KEYWORDS,
541 "vlm_add_broadcast(name=str, input=str, output=str, options=list, enable=int, loop=int) Add a new broadcast" },
542 { "vlm_del_media", vlcInstance_vlm_del_media, METH_VARARGS,
543 "vlm_del_media(name=str) Delete a media" },
544 { "vlm_set_enabled", vlcInstance_vlm_set_enabled, METH_VARARGS,
545 "vlm_set_enabled(name=str, enabled=int) Enable/disable a media" },
546 { "vlm_set_output", vlcInstance_vlm_set_output, METH_VARARGS,
547 "vlm_set_output(name=str, output=str) Set the output" },
548 { "vlm_set_input", vlcInstance_vlm_set_input, METH_VARARGS,
549 "vlm_set_input(name=str, output=str) Set the input" },
550 { "vlm_add_input", vlcInstance_vlm_add_input, METH_VARARGS,
551 "vlm_add_input(name=str, output=str) Add a media's input MRL" },
552 { "vlm_set_loop", vlcInstance_vlm_set_loop, METH_VARARGS,
553 "vlm_set_loop(name=str, loop=int) Change the looping value" },
554 { "vlm_change_media", vlcInstance_vlm_change_media, METH_VARARGS | METH_KEYWORDS,
555 "vlm_change_media(name=str, input=str, output=str, options=list, enable=int, loop=int) Change the broadcast parameters" },
556 { "vlm_play_media", vlcInstance_vlm_play_media, METH_VARARGS,
557 "vlm_play_media(name=str) Plays the named broadcast." },
558 { "vlm_stop_media", vlcInstance_vlm_stop_media, METH_VARARGS,
559 "vlm_stop_media(name=str) Stops the named broadcast." },
560 { "vlm_pause_media", vlcInstance_vlm_pause_media, METH_VARARGS,
561 "vlm_pause_media(name=str) Pauses the named broadcast." },
562 { "vlm_seek_media", vlcInstance_vlm_seek_media, METH_VARARGS,
563 "vlm_seek_media(name=str, percentage=float) Seeks in the named broadcast." },
564 { "vlm_show_media", vlcInstance_vlm_show_media, METH_VARARGS,
565 "vlm_show_media(name=str) Return information of the named broadcast." },
567 { NULL, NULL, 0, NULL },
570 static PyTypeObject vlcInstance_Type =
572 PyObject_HEAD_INIT( NULL )
574 "vlc.Instance", /*tp_name*/
575 sizeof( vlcInstance_Type ), /*tp_basicsize*/
577 ( destructor )vlcInstance_dealloc, /*tp_dealloc*/
584 0, /*tp_as_sequence*/
592 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
593 "VLC Instance(args)", /* tp_doc */
596 0, /* tp_richcompare */
597 0, /* tp_weaklistoffset */
600 vlcInstance_methods, /* tp_methods */
605 0, /* tp_descr_get */
606 0, /* tp_descr_set */
607 0, /* tp_dictoffset */
610 vlcInstance_new, /* tp_new */