1 /*****************************************************************************
2 * vlc_instance.c: vlc.Instance binding
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 *****************************************************************************/
25 /* Helper functions */
27 pyoptions_to_args(PyObject *py_options, char*** pppsz_args)
31 char** ppsz_args = *pppsz_args;
35 Py_INCREF( py_options );
36 if( ! PySequence_Check( py_options ) )
38 PyErr_SetString( PyExc_TypeError, "Parameter must be a sequence." );
41 i_size = PySequence_Size( py_options );
42 ppsz_args = malloc( ( i_size + 1 ) * sizeof( char * ) );
45 PyErr_SetString( PyExc_MemoryError, "Out of memory" );
49 for ( i_index = 0; i_index < i_size; i_index++ )
52 strdup( PyString_AsString( PyObject_Str(
53 PySequence_GetItem( py_options,
56 ppsz_args[i_size] = NULL;
57 Py_DECREF( py_options );
62 free_args(int i_size, char** ppsz_args)
66 for ( i_index = 0; i_index < i_size; i_index++ )
67 if( ppsz_args[i_index] )
68 free(ppsz_args[i_index]);
72 /*****************************************************************************
73 * Instance object implementation
74 *****************************************************************************/
77 vlcInstance_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
80 libvlc_exception_t ex;
81 PyObject* py_list = NULL;
82 char** ppsz_args = NULL;
85 self = PyObject_New( vlcInstance, &vlcInstance_Type );
88 if( PyArg_ParseTuple( args, "O", &py_list ) )
90 i_size = pyoptions_to_args( py_list, &ppsz_args );
96 /* No arguments were given. Clear the exception raised
97 by PyArg_ParseTuple. */
101 Py_BEGIN_ALLOW_THREADS
103 LIBVLC_INSTANCE->p_instance = libvlc_new( i_size, ppsz_args, &ex );
104 free_args( i_size, ppsz_args );
109 return ( PyObject * )self;
113 vlcInstance_dealloc( PyObject *self )
115 libvlc_exception_t ex;
116 libvlc_destroy( LIBVLC_INSTANCE->p_instance, &ex );
121 vlcInstance_get_vlc_id( PyObject *self, PyObject *args )
123 return Py_BuildValue( "i", libvlc_get_vlc_id( LIBVLC_INSTANCE->p_instance ) );
126 /* Set loop variable */
128 vlcInstance_playlist_loop( PyObject *self, PyObject *args )
130 libvlc_exception_t ex;
133 if( !PyArg_ParseTuple( args, "i", &i_loop ) )
137 libvlc_playlist_loop( LIBVLC_INSTANCE->p_instance, i_loop, &ex );
140 Py_INCREF( Py_None );
144 /* Playlist play. 2 parameters: i_id, the id to play
145 l_options: a list of options */
147 vlcInstance_playlist_play( PyObject *self, PyObject *args )
149 libvlc_exception_t ex;
151 PyObject *py_options = NULL;
153 char** ppsz_args = NULL;
155 if( !PyArg_ParseTuple( args, "|iO", &i_id, &py_options ) )
160 i_size = pyoptions_to_args( py_options, &ppsz_args );
164 libvlc_playlist_play( LIBVLC_INSTANCE->p_instance, i_id, i_size, ppsz_args, &ex );
165 free_args( i_size, ppsz_args );
168 Py_INCREF( Py_None );
173 vlcInstance_playlist_pause( PyObject *self, PyObject *args )
175 libvlc_exception_t ex;
177 libvlc_playlist_pause( LIBVLC_INSTANCE->p_instance, &ex );
179 Py_INCREF( Py_None );
184 vlcInstance_playlist_isplaying( PyObject *self, PyObject *args )
186 libvlc_exception_t ex;
190 i_ret = libvlc_playlist_isplaying( LIBVLC_INSTANCE->p_instance, &ex );
192 return Py_BuildValue( "i", i_ret );
196 vlcInstance_playlist_items_count( PyObject *self, PyObject *args )
198 libvlc_exception_t ex;
202 i_ret = libvlc_playlist_items_count( LIBVLC_INSTANCE->p_instance, &ex );
204 return Py_BuildValue( "i", i_ret );
208 vlcInstance_playlist_stop( PyObject *self, PyObject *args )
210 libvlc_exception_t ex;
212 libvlc_playlist_stop( LIBVLC_INSTANCE->p_instance, &ex );
214 Py_INCREF( Py_None );
219 vlcInstance_playlist_next( PyObject *self, PyObject *args )
221 libvlc_exception_t ex;
223 libvlc_playlist_next( LIBVLC_INSTANCE->p_instance, &ex );
225 Py_INCREF( Py_None );
230 vlcInstance_playlist_prev( PyObject *self, PyObject *args )
232 libvlc_exception_t ex;
234 libvlc_playlist_prev( LIBVLC_INSTANCE->p_instance, &ex );
236 Py_INCREF( Py_None );
241 vlcInstance_playlist_clear( PyObject *self, PyObject *args )
243 libvlc_exception_t ex;
245 libvlc_playlist_clear( LIBVLC_INSTANCE->p_instance, &ex );
247 Py_INCREF( Py_None );
251 /* Add a playlist item. Main parameter: URI.
252 Optional parameters: name, options */
254 vlcInstance_playlist_add( PyObject *self, PyObject *args)
256 libvlc_exception_t ex;
258 char* psz_uri = NULL;
259 char* psz_name = NULL;
260 PyObject *py_options = NULL;
262 char** ppsz_args = NULL;
264 if( !PyArg_ParseTuple( args, "s|sO", &psz_uri, &psz_name, &py_options ) )
269 /* Set a default name */
270 psz_name = strdup( psz_uri );
275 i_size = pyoptions_to_args( py_options, &ppsz_args );
281 i_ret = libvlc_playlist_add_extended( LIBVLC_INSTANCE->p_instance,
287 free_args( i_size, ppsz_args );
291 i_ret = libvlc_playlist_add( LIBVLC_INSTANCE->p_instance,
298 return Py_BuildValue( "i", i_ret );
302 vlcInstance_playlist_delete_item( PyObject *self, PyObject *args )
304 libvlc_exception_t ex;
308 if( !PyArg_ParseTuple( args, "i", &i_id ) )
312 i_ret = libvlc_playlist_delete_item( LIBVLC_INSTANCE->p_instance, i_id, &ex );
315 return Py_BuildValue( "i", i_ret );
319 vlcInstance_playlist_get_input( PyObject *self, PyObject *args )
321 libvlc_exception_t ex;
322 libvlc_input_t *p_input;
326 p_input = libvlc_playlist_get_input( LIBVLC_INSTANCE->p_instance, &ex );
329 p_ret = PyObject_New( vlcInput, &vlcInput_Type );
330 p_ret->p_input = p_input;
331 Py_INCREF( p_ret ); /* Ah bon ? */
332 return ( PyObject * )p_ret;
336 vlcInstance_video_set_parent( PyObject *self, PyObject *args )
338 libvlc_exception_t ex;
341 if( !PyArg_ParseTuple( args, "i", &i_drawable ) )
345 libvlc_video_set_parent( LIBVLC_INSTANCE->p_instance, (libvlc_drawable_t) i_drawable, &ex );
348 Py_INCREF( Py_None );
353 vlcInstance_video_set_size( PyObject *self, PyObject *args )
355 libvlc_exception_t ex;
359 if( !PyArg_ParseTuple( args, "ii", &i_width, &i_height ) )
363 libvlc_video_set_size( LIBVLC_INSTANCE->p_instance, i_width, i_height, &ex );
366 Py_INCREF( Py_None );
371 vlcInstance_audio_toggle_mute( PyObject *self, PyObject *args )
373 libvlc_exception_t ex;
375 libvlc_audio_toggle_mute( LIBVLC_INSTANCE->p_instance, &ex );
377 Py_INCREF( Py_None );
382 vlcInstance_audio_get_mute( PyObject *self, PyObject *args )
384 libvlc_exception_t ex;
388 i_ret = libvlc_audio_get_mute( LIBVLC_INSTANCE->p_instance, &ex );
390 return Py_BuildValue( "i", i_ret );
394 vlcInstance_audio_set_mute( PyObject *self, PyObject *args )
396 libvlc_exception_t ex;
399 if( !PyArg_ParseTuple( args, "i", &i_mute ) )
403 libvlc_audio_set_mute( LIBVLC_INSTANCE->p_instance, i_mute, &ex );
405 Py_INCREF( Py_None );
410 vlcInstance_audio_get_volume( PyObject *self, PyObject *args )
412 libvlc_exception_t ex;
416 i_ret = libvlc_audio_get_volume( LIBVLC_INSTANCE->p_instance, &ex );
418 return Py_BuildValue( "i", i_ret );
422 vlcInstance_audio_set_volume( PyObject *self, PyObject *args )
424 libvlc_exception_t ex;
427 if( !PyArg_ParseTuple( args, "i", &i_volume ) )
431 libvlc_audio_set_mute( LIBVLC_INSTANCE->p_instance, i_volume, &ex );
433 Py_INCREF( Py_None );
438 vlcInstance_audio_get_channel( PyObject *self, PyObject *args )
440 libvlc_exception_t ex;
444 i_ret = libvlc_audio_get_channel( LIBVLC_INSTANCE->p_instance, &ex );
446 return Py_BuildValue( "i", i_ret );
450 vlcInstance_audio_set_channel( PyObject *self, PyObject *args )
452 libvlc_exception_t ex;
455 if( !PyArg_ParseTuple( args, "i", &i_channel ) )
459 libvlc_audio_set_channel( LIBVLC_INSTANCE->p_instance, i_channel, &ex );
461 Py_INCREF( Py_None );
465 /* vlm_add_broadcast : name, input MRL, output MRL
466 Keywords: options, enable, loop */
468 vlcInstance_vlm_add_broadcast( PyObject *self, PyObject *args, PyObject *kwds )
470 libvlc_exception_t ex;
471 static char *kwlist[] = { "name", "input", "output",
472 "options", "enable", "loop", NULL};
473 char* psz_name = NULL;
474 char* psz_input = NULL;
475 char* psz_output = NULL;
476 PyObject* py_options = NULL;
480 char** ppsz_args = NULL;
482 if( !PyArg_ParseTupleAndKeywords( args, kwds, "sss|Oii", kwlist,
484 &psz_input, &psz_output,
485 &py_options, &i_enable, &i_loop ) )
490 i_size = pyoptions_to_args( py_options, &ppsz_args );
494 libvlc_vlm_add_broadcast( LIBVLC_INSTANCE->p_instance,
495 psz_name, psz_input, psz_output,
496 i_size, ppsz_args, i_enable, i_loop, &ex);
497 free_args( i_size, ppsz_args );
499 Py_INCREF( Py_None );
504 vlcInstance_vlm_del_media( PyObject *self, PyObject *args )
506 libvlc_exception_t ex;
509 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
513 libvlc_vlm_del_media( LIBVLC_INSTANCE->p_instance, psz_name, &ex);
515 Py_INCREF( Py_None );
520 vlcInstance_vlm_set_enabled( PyObject *self, PyObject *args )
522 libvlc_exception_t ex;
526 if( !PyArg_ParseTuple( args, "si", &psz_name, &i_enabled ) )
530 libvlc_vlm_set_enabled( LIBVLC_INSTANCE->p_instance, psz_name, i_enabled, &ex);
532 Py_INCREF( Py_None );
537 vlcInstance_vlm_set_output( PyObject *self, PyObject *args )
539 libvlc_exception_t ex;
543 if( !PyArg_ParseTuple( args, "ss", &psz_name, &psz_output ) )
547 libvlc_vlm_set_output( LIBVLC_INSTANCE->p_instance, psz_name, psz_output, &ex);
549 Py_INCREF( Py_None );
554 vlcInstance_vlm_set_input( PyObject *self, PyObject *args )
556 libvlc_exception_t ex;
560 if( !PyArg_ParseTuple( args, "ss", &psz_name, &psz_input ) )
564 libvlc_vlm_set_input( LIBVLC_INSTANCE->p_instance, psz_name, psz_input, &ex);
566 Py_INCREF( Py_None );
571 vlcInstance_vlm_set_loop( PyObject *self, PyObject *args )
573 libvlc_exception_t ex;
577 if( !PyArg_ParseTuple( args, "si", &psz_name, &i_loop ) )
581 libvlc_vlm_set_loop( LIBVLC_INSTANCE->p_instance, psz_name, i_loop, &ex);
583 Py_INCREF( Py_None );
588 vlcInstance_vlm_change_media( PyObject *self, PyObject *args, PyObject *kwds )
590 libvlc_exception_t ex;
591 static char *kwlist[] = { "name", "input", "output",
592 "options", "enable", "loop", NULL};
593 char* psz_name = NULL;
594 char* psz_input = NULL;
595 char* psz_output = NULL;
596 PyObject* py_options = NULL;
600 char** ppsz_args = NULL;
602 if( !PyArg_ParseTupleAndKeywords( args, kwds, "sss|Oii", kwlist,
604 &psz_input, &psz_output,
605 &py_options, &i_enable, &i_loop ) )
610 i_size = pyoptions_to_args( py_options, &ppsz_args );
614 libvlc_vlm_change_media( LIBVLC_INSTANCE->p_instance,
615 psz_name, psz_input, psz_output,
616 i_size, ppsz_args, i_enable, i_loop, &ex);
617 free_args( i_size, ppsz_args );
619 Py_INCREF( Py_None );
624 vlcInstance_vlm_play_media( PyObject *self, PyObject *args )
626 libvlc_exception_t ex;
629 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
633 libvlc_vlm_play_media( LIBVLC_INSTANCE->p_instance, psz_name, &ex);
635 Py_INCREF( Py_None );
640 vlcInstance_vlm_stop_media( PyObject *self, PyObject *args )
642 libvlc_exception_t ex;
645 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
649 libvlc_vlm_stop_media( LIBVLC_INSTANCE->p_instance, psz_name, &ex);
651 Py_INCREF( Py_None );
656 vlcInstance_vlm_pause_media( PyObject *self, PyObject *args )
658 libvlc_exception_t ex;
661 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
665 libvlc_vlm_pause_media( LIBVLC_INSTANCE->p_instance, psz_name, &ex);
667 Py_INCREF( Py_None );
672 vlcInstance_vlm_seek_media( PyObject *self, PyObject *args )
674 libvlc_exception_t ex;
678 if( !PyArg_ParseTuple( args, "sf", &psz_name, &f_percentage ) )
682 libvlc_vlm_seek_media( LIBVLC_INSTANCE->p_instance, psz_name, f_percentage, &ex);
684 Py_INCREF( Py_None );
689 vlcInstance_vlm_show_media( PyObject *self, PyObject *args )
691 libvlc_exception_t ex;
696 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
699 psz_ret = libvlc_vlm_show_media( LIBVLC_INSTANCE->p_instance, psz_name, &ex );
701 o_ret = Py_BuildValue( "s", psz_ret );
707 static PyMethodDef vlcInstance_methods[] =
709 { "get_vlc_id", vlcInstance_get_vlc_id, METH_VARARGS,
710 "get_vlc_id( ) -> int Get the instance id."},
711 { "playlist_loop", vlcInstance_playlist_loop, METH_VARARGS,
712 "playlist_loop(bool) Set loop variable" },
713 { "playlist_play", vlcInstance_playlist_play, METH_VARARGS,
714 "playlist_play(id=int, options=list) Play the given playlist item (-1 for current item) with optional options (a list of strings)" },
715 { "playlist_pause", vlcInstance_playlist_pause, METH_VARARGS,
716 "playlist_pause() Pause the current stream"},
717 { "playlist_isplaying", vlcInstance_playlist_isplaying, METH_VARARGS,
718 "playlist_isplaying() -> int Return True if the playlist if playing"},
719 { "playlist_items_count", vlcInstance_playlist_items_count, METH_VARARGS,
720 "playlist_items_count() -> int Return the number of items in the playlist"},
721 { "playlist_stop", vlcInstance_playlist_stop, METH_VARARGS,
722 "playlist_stop() Stop the current stream"},
723 { "playlist_next", vlcInstance_playlist_next, METH_VARARGS,
724 "playlist_next() Play the next item"},
725 { "playlist_prev", vlcInstance_playlist_prev, METH_VARARGS,
726 "playlist_prev() Play the previous item"},
727 { "playlist_clear", vlcInstance_playlist_clear, METH_VARARGS,
728 "playlist_clear() Clear the playlist"},
729 { "playlist_add", vlcInstance_playlist_add, METH_VARARGS,
730 "playlist_add(mrl=str, name=str, options=list) -> int Add a new item to the playlist. options is a list of strings."},
731 { "playlist_delete_item", vlcInstance_playlist_delete_item, METH_VARARGS,
732 "playlist_delete_item(id=int) Delete the given item"},
733 { "playlist_get_input", vlcInstance_playlist_get_input, METH_VARARGS,
734 "playlist_get_input() -> object Return the current input"},
735 { "video_set_parent", vlcInstance_video_set_parent, METH_VARARGS,
736 "video_set_parent(xid=int) Set the parent xid or HWND"},
737 { "video_set_size", vlcInstance_video_set_size, METH_VARARGS,
738 "video_set_size(width=int, height=int) Set the video width and height"},
739 { "audio_toggle_mute", vlcInstance_audio_toggle_mute, METH_VARARGS,
740 "audio_toggle_mute() Toggle the mute state"},
741 { "audio_get_mute", vlcInstance_audio_get_mute, METH_VARARGS,
742 "audio_get_mute() -> int Get the mute state"},
743 { "audio_set_mute", vlcInstance_audio_set_mute, METH_VARARGS,
744 "audio_set_mute(state=int) Set the mute state"},
745 { "audio_get_volume", vlcInstance_audio_get_volume, METH_VARARGS,
746 "audio_get_volume() -> int Get the audio volume"},
747 { "audio_set_volume", vlcInstance_audio_set_volume, METH_VARARGS,
748 "audio_set_volume(volume=int) Set the audio volume"},
749 { "audio_get_channel", vlcInstance_audio_get_channel, METH_VARARGS,
750 "audio_get_channel() -> int Get current audio channel" },
751 { "audio_set_channel", vlcInstance_audio_set_channel, METH_VARARGS,
752 "audio_set_channel(int) Set current audio channel" },
753 { "vlm_add_broadcast", vlcInstance_vlm_add_broadcast, METH_VARARGS | METH_KEYWORDS,
754 "vlm_add_broadcast(name=str, input=str, output=str, options=list, enable=int, loop=int) Add a new broadcast" },
755 { "vlm_del_media", vlcInstance_vlm_del_media, METH_VARARGS,
756 "vlm_del_media(name=str) Delete a media" },
757 { "vlm_set_enabled", vlcInstance_vlm_set_enabled, METH_VARARGS,
758 "vlm_set_enabled(name=str, enabled=int) Enable/disable a media" },
759 { "vlm_set_output", vlcInstance_vlm_set_output, METH_VARARGS,
760 "vlm_set_output(name=str, output=str) Set the output" },
761 { "vlm_set_input", vlcInstance_vlm_set_input, METH_VARARGS,
762 "vlm_set_input(name=str, output=str) Set the input" },
763 { "vlm_set_loop", vlcInstance_vlm_set_loop, METH_VARARGS,
764 "vlm_set_loop(name=str, loop=int) Change the looping value" },
765 { "vlm_change_media", vlcInstance_vlm_change_media, METH_VARARGS | METH_KEYWORDS,
766 "vlm_change_media(name=str, input=str, output=str, options=list, enable=int, loop=int) Change the broadcast parameters" },
767 { "vlm_play_media", vlcInstance_vlm_play_media, METH_VARARGS,
768 "vlm_play_media(name=str) Plays the named broadcast." },
769 { "vlm_stop_media", vlcInstance_vlm_stop_media, METH_VARARGS,
770 "vlm_stop_media(name=str) Stops the named broadcast." },
771 { "vlm_pause_media", vlcInstance_vlm_pause_media, METH_VARARGS,
772 "vlm_pause_media(name=str) Pauses the named broadcast." },
773 { "vlm_seek_media", vlcInstance_vlm_seek_media, METH_VARARGS,
774 "vlm_seek_media(name=str, percentage=float) Seeks in the named broadcast." },
775 { "vlm_show_media", vlcInstance_vlm_show_media, METH_VARARGS,
776 "vlm_show_media(name=str) Return information of the named broadcast." },
778 { NULL, NULL, 0, NULL },
781 static PyTypeObject vlcInstance_Type =
783 PyObject_HEAD_INIT( NULL )
785 "vlc.Instance", /*tp_name*/
786 sizeof( vlcInstance_Type ), /*tp_basicsize*/
788 ( destructor )vlcInstance_dealloc, /*tp_dealloc*/
795 0, /*tp_as_sequence*/
803 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
804 "VLC Instance(args)\n\nNote: if args is specified, the first arg is interpreted as an executable name to get the directory of the VLC plugins.", /* tp_doc */
807 0, /* tp_richcompare */
808 0, /* tp_weaklistoffset */
811 vlcInstance_methods, /* tp_methods */
816 0, /* tp_descr_get */
817 0, /* tp_descr_set */
818 0, /* tp_dictoffset */
821 vlcInstance_new, /* tp_new */