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)
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 if( PyArg_ParseTuple( args, "O", &py_list ) )
85 i_size = pyoptions_to_args( py_list, &ppsz_args );
91 /* No arguments were given. Clear the exception raised
92 by PyArg_ParseTuple. */
96 self = PyObject_New( vlcInstance, &vlcInstance_Type );
98 Py_BEGIN_ALLOW_THREADS
100 LIBVLC_INSTANCE->p_instance = libvlc_new( i_size, ppsz_args, &ex );
101 free_args( i_size, ppsz_args );
106 return ( PyObject * )self;
110 vlcInstance_dealloc( PyObject *self )
112 libvlc_release( LIBVLC_INSTANCE->p_instance );
113 PyObject_DEL( self );
117 vlcInstance_get_vlc_id( PyObject *self, PyObject *args )
119 return Py_BuildValue( "i", libvlc_get_vlc_id( LIBVLC_INSTANCE->p_instance ) );
122 /* Set loop variable */
124 vlcInstance_playlist_loop( PyObject *self, PyObject *args )
126 libvlc_exception_t ex;
129 if( !PyArg_ParseTuple( args, "i", &i_loop ) )
133 libvlc_playlist_loop( LIBVLC_INSTANCE->p_instance, i_loop, &ex );
136 Py_INCREF( Py_None );
140 /* Playlist play. 2 parameters: i_id, the id to play
141 l_options: a list of options */
143 vlcInstance_playlist_play( PyObject *self, PyObject *args )
145 libvlc_exception_t ex;
147 PyObject *py_options = NULL;
149 char** ppsz_args = NULL;
151 if( !PyArg_ParseTuple( args, "|iO", &i_id, &py_options ) )
156 i_size = pyoptions_to_args( py_options, &ppsz_args );
160 libvlc_playlist_play( LIBVLC_INSTANCE->p_instance, i_id, i_size, ppsz_args, &ex );
161 free_args( i_size, ppsz_args );
164 Py_INCREF( Py_None );
169 vlcInstance_playlist_pause( PyObject *self, PyObject *args )
171 libvlc_exception_t ex;
173 libvlc_playlist_pause( LIBVLC_INSTANCE->p_instance, &ex );
175 Py_INCREF( Py_None );
180 vlcInstance_playlist_isplaying( PyObject *self, PyObject *args )
182 libvlc_exception_t ex;
186 i_ret = libvlc_playlist_isplaying( LIBVLC_INSTANCE->p_instance, &ex );
188 return Py_BuildValue( "i", i_ret );
192 vlcInstance_playlist_items_count( PyObject *self, PyObject *args )
194 libvlc_exception_t ex;
198 i_ret = libvlc_playlist_items_count( LIBVLC_INSTANCE->p_instance, &ex );
200 return Py_BuildValue( "i", i_ret );
204 vlcInstance_playlist_stop( PyObject *self, PyObject *args )
206 libvlc_exception_t ex;
208 libvlc_playlist_stop( LIBVLC_INSTANCE->p_instance, &ex );
210 Py_INCREF( Py_None );
215 vlcInstance_playlist_next( PyObject *self, PyObject *args )
217 libvlc_exception_t ex;
219 libvlc_playlist_next( LIBVLC_INSTANCE->p_instance, &ex );
221 Py_INCREF( Py_None );
226 vlcInstance_playlist_prev( PyObject *self, PyObject *args )
228 libvlc_exception_t ex;
230 libvlc_playlist_prev( LIBVLC_INSTANCE->p_instance, &ex );
232 Py_INCREF( Py_None );
237 vlcInstance_playlist_clear( PyObject *self, PyObject *args )
239 libvlc_exception_t ex;
241 libvlc_playlist_clear( LIBVLC_INSTANCE->p_instance, &ex );
243 Py_INCREF( Py_None );
247 /* Add a playlist item. Main parameter: URI.
248 Optional parameters: name, options */
250 vlcInstance_playlist_add( PyObject *self, PyObject *args)
252 libvlc_exception_t ex;
254 char* psz_uri = NULL;
255 char* psz_name = NULL;
256 PyObject *py_options = NULL;
258 char** ppsz_args = NULL;
260 if( !PyArg_ParseTuple( args, "s|sO", &psz_uri, &psz_name, &py_options ) )
265 /* Set a default name */
266 psz_name = strdup( psz_uri );
271 i_size = pyoptions_to_args( py_options, &ppsz_args );
277 i_ret = libvlc_playlist_add_extended( LIBVLC_INSTANCE->p_instance,
283 free_args( i_size, ppsz_args );
287 i_ret = libvlc_playlist_add( LIBVLC_INSTANCE->p_instance,
294 return Py_BuildValue( "i", i_ret );
298 vlcInstance_playlist_delete_item( PyObject *self, PyObject *args )
300 libvlc_exception_t ex;
304 if( !PyArg_ParseTuple( args, "i", &i_id ) )
308 i_ret = libvlc_playlist_delete_item( LIBVLC_INSTANCE->p_instance, i_id, &ex );
311 return Py_BuildValue( "i", i_ret );
315 vlcInstance_playlist_get_media_player( PyObject *self, PyObject *args )
317 libvlc_exception_t ex;
318 libvlc_media_player_t *p_mi;
319 vlcMediaInstance *p_ret;
322 p_mi = libvlc_playlist_get_media_player( LIBVLC_INSTANCE->p_instance, &ex );
325 p_ret = PyObject_New( vlcMediaInstance, &vlcMediaInstance_Type );
327 Py_INCREF( p_ret ); /* Ah bon ? */
328 return ( PyObject * )p_ret;
332 vlcInstance_video_set_parent( PyObject *self, PyObject *args )
334 libvlc_exception_t ex;
337 if( !PyArg_ParseTuple( args, "i", &i_drawable ) )
341 libvlc_video_set_parent( LIBVLC_INSTANCE->p_instance, (libvlc_drawable_t) i_drawable, &ex );
344 Py_INCREF( Py_None );
349 vlcInstance_video_get_parent( PyObject *self, PyObject *args )
351 libvlc_exception_t ex;
352 libvlc_drawable_t i_ret;
355 i_ret = libvlc_video_get_parent( LIBVLC_INSTANCE->p_instance, &ex );
358 return Py_BuildValue( "L", i_ret );
362 vlcInstance_video_set_size( PyObject *self, PyObject *args )
364 libvlc_exception_t ex;
368 if( !PyArg_ParseTuple( args, "ii", &i_width, &i_height ) )
372 libvlc_video_set_size( LIBVLC_INSTANCE->p_instance, i_width, i_height, &ex );
375 Py_INCREF( Py_None );
380 vlcInstance_audio_toggle_mute( PyObject *self, PyObject *args )
382 libvlc_exception_t ex;
384 libvlc_audio_toggle_mute( LIBVLC_INSTANCE->p_instance, &ex );
386 Py_INCREF( Py_None );
391 vlcInstance_audio_get_mute( PyObject *self, PyObject *args )
393 libvlc_exception_t ex;
397 i_ret = libvlc_audio_get_mute( LIBVLC_INSTANCE->p_instance, &ex );
399 return Py_BuildValue( "i", i_ret );
403 vlcInstance_audio_set_mute( PyObject *self, PyObject *args )
405 libvlc_exception_t ex;
408 if( !PyArg_ParseTuple( args, "i", &i_mute ) )
412 libvlc_audio_set_mute( LIBVLC_INSTANCE->p_instance, i_mute, &ex );
414 Py_INCREF( Py_None );
419 vlcInstance_audio_get_volume( PyObject *self, PyObject *args )
421 libvlc_exception_t ex;
425 i_ret = libvlc_audio_get_volume( LIBVLC_INSTANCE->p_instance, &ex );
427 return Py_BuildValue( "i", i_ret );
431 vlcInstance_audio_set_volume( PyObject *self, PyObject *args )
433 libvlc_exception_t ex;
436 if( !PyArg_ParseTuple( args, "i", &i_volume ) )
440 libvlc_audio_set_volume( LIBVLC_INSTANCE->p_instance, i_volume, &ex );
442 Py_INCREF( Py_None );
447 vlcInstance_audio_get_channel( PyObject *self, PyObject *args )
449 libvlc_exception_t ex;
453 i_ret = libvlc_audio_get_channel( LIBVLC_INSTANCE->p_instance, &ex );
455 return Py_BuildValue( "i", i_ret );
459 vlcInstance_audio_set_channel( PyObject *self, PyObject *args )
461 libvlc_exception_t ex;
464 if( !PyArg_ParseTuple( args, "i", &i_channel ) )
468 libvlc_audio_set_channel( LIBVLC_INSTANCE->p_instance, i_channel, &ex );
470 Py_INCREF( Py_None );
474 /* vlm_add_broadcast : name, input MRL, output MRL
475 Keywords: options, enable, loop */
477 vlcInstance_vlm_add_broadcast( PyObject *self, PyObject *args, PyObject *kwds )
479 libvlc_exception_t ex;
480 static char *kwlist[] = { "name", "input", "output",
481 "options", "enable", "loop", NULL};
482 char* psz_name = NULL;
483 char* psz_input = NULL;
484 char* psz_output = NULL;
485 PyObject* py_options = NULL;
489 char** ppsz_args = NULL;
491 if( !PyArg_ParseTupleAndKeywords( args, kwds, "sss|Oii", kwlist,
493 &psz_input, &psz_output,
494 &py_options, &i_enable, &i_loop ) )
499 i_size = pyoptions_to_args( py_options, &ppsz_args );
503 libvlc_vlm_add_broadcast( LIBVLC_INSTANCE->p_instance,
504 psz_name, psz_input, psz_output,
505 i_size, ppsz_args, i_enable, i_loop, &ex);
506 free_args( i_size, ppsz_args );
508 Py_INCREF( Py_None );
513 vlcInstance_vlm_del_media( PyObject *self, PyObject *args )
515 libvlc_exception_t ex;
518 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
522 libvlc_vlm_del_media( LIBVLC_INSTANCE->p_instance, psz_name, &ex);
524 Py_INCREF( Py_None );
529 vlcInstance_vlm_set_enabled( PyObject *self, PyObject *args )
531 libvlc_exception_t ex;
535 if( !PyArg_ParseTuple( args, "si", &psz_name, &i_enabled ) )
539 libvlc_vlm_set_enabled( LIBVLC_INSTANCE->p_instance, psz_name, i_enabled, &ex);
541 Py_INCREF( Py_None );
546 vlcInstance_vlm_set_output( PyObject *self, PyObject *args )
548 libvlc_exception_t ex;
552 if( !PyArg_ParseTuple( args, "ss", &psz_name, &psz_output ) )
556 libvlc_vlm_set_output( LIBVLC_INSTANCE->p_instance, psz_name, psz_output, &ex);
558 Py_INCREF( Py_None );
563 vlcInstance_vlm_set_input( PyObject *self, PyObject *args )
565 libvlc_exception_t ex;
569 if( !PyArg_ParseTuple( args, "ss", &psz_name, &psz_input ) )
573 libvlc_vlm_set_input( LIBVLC_INSTANCE->p_instance, psz_name, psz_input, &ex);
575 Py_INCREF( Py_None );
580 vlcInstance_vlm_add_input( PyObject *self, PyObject *args )
582 libvlc_exception_t ex;
586 if( !PyArg_ParseTuple( args, "ss", &psz_name, &psz_input ) )
590 libvlc_vlm_add_input( LIBVLC_INSTANCE->p_instance, psz_name, psz_input, &ex);
592 Py_INCREF( Py_None );
597 vlcInstance_vlm_set_loop( PyObject *self, PyObject *args )
599 libvlc_exception_t ex;
603 if( !PyArg_ParseTuple( args, "si", &psz_name, &i_loop ) )
607 libvlc_vlm_set_loop( LIBVLC_INSTANCE->p_instance, psz_name, i_loop, &ex);
609 Py_INCREF( Py_None );
614 vlcInstance_vlm_change_media( PyObject *self, PyObject *args, PyObject *kwds )
616 libvlc_exception_t ex;
617 static char *kwlist[] = { "name", "input", "output",
618 "options", "enable", "loop", NULL};
619 char* psz_name = NULL;
620 char* psz_input = NULL;
621 char* psz_output = NULL;
622 PyObject* py_options = NULL;
626 char** ppsz_args = NULL;
628 if( !PyArg_ParseTupleAndKeywords( args, kwds, "sss|Oii", kwlist,
630 &psz_input, &psz_output,
631 &py_options, &i_enable, &i_loop ) )
636 i_size = pyoptions_to_args( py_options, &ppsz_args );
640 libvlc_vlm_change_media( LIBVLC_INSTANCE->p_instance,
641 psz_name, psz_input, psz_output,
642 i_size, ppsz_args, i_enable, i_loop, &ex);
643 free_args( i_size, ppsz_args );
645 Py_INCREF( Py_None );
650 vlcInstance_vlm_play_media( PyObject *self, PyObject *args )
652 libvlc_exception_t ex;
655 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
659 libvlc_vlm_play_media( LIBVLC_INSTANCE->p_instance, psz_name, &ex);
661 Py_INCREF( Py_None );
666 vlcInstance_vlm_stop_media( PyObject *self, PyObject *args )
668 libvlc_exception_t ex;
671 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
675 libvlc_vlm_stop_media( LIBVLC_INSTANCE->p_instance, psz_name, &ex);
677 Py_INCREF( Py_None );
682 vlcInstance_vlm_pause_media( PyObject *self, PyObject *args )
684 libvlc_exception_t ex;
687 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
691 libvlc_vlm_pause_media( LIBVLC_INSTANCE->p_instance, psz_name, &ex);
693 Py_INCREF( Py_None );
698 vlcInstance_vlm_seek_media( PyObject *self, PyObject *args )
700 libvlc_exception_t ex;
704 if( !PyArg_ParseTuple( args, "sf", &psz_name, &f_percentage ) )
708 libvlc_vlm_seek_media( LIBVLC_INSTANCE->p_instance, psz_name, f_percentage, &ex);
710 Py_INCREF( Py_None );
715 vlcInstance_vlm_show_media( PyObject *self, PyObject *args )
717 libvlc_exception_t ex;
722 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
725 psz_ret = libvlc_vlm_show_media( LIBVLC_INSTANCE->p_instance, psz_name, &ex );
727 o_ret = Py_BuildValue( "s", psz_ret );
733 vlcInstance_media_new( PyObject *self, PyObject *args )
735 libvlc_exception_t ex;
736 libvlc_media_t *p_md;
737 char* psz_mrl = NULL;
738 vlcMediaDescriptor *p_ret;
740 if( !PyArg_ParseTuple( args, "s", &psz_mrl ) )
744 p_md = libvlc_media_new( LIBVLC_INSTANCE->p_instance, psz_mrl, &ex );
747 p_ret = PyObject_New( vlcMediaDescriptor, &vlcMediaDescriptor_Type );
749 Py_INCREF( p_ret ); /* Ah bon ? */
750 return ( PyObject * )p_ret;
754 static PyMethodDef vlcInstance_methods[] =
756 { "get_vlc_id", vlcInstance_get_vlc_id, METH_VARARGS,
757 "get_vlc_id( ) -> int Get the instance id."},
758 { "playlist_loop", vlcInstance_playlist_loop, METH_VARARGS,
759 "playlist_loop(bool) Set loop variable" },
760 { "playlist_play", vlcInstance_playlist_play, METH_VARARGS,
761 "playlist_play(id=int, options=list) Play the given playlist item (-1 for current item) with optional options (a list of strings)" },
762 { "playlist_pause", vlcInstance_playlist_pause, METH_VARARGS,
763 "playlist_pause() Pause the current stream"},
764 { "playlist_isplaying", vlcInstance_playlist_isplaying, METH_VARARGS,
765 "playlist_isplaying() -> int Return True if the playlist if playing"},
766 { "playlist_items_count", vlcInstance_playlist_items_count, METH_VARARGS,
767 "playlist_items_count() -> int Return the number of items in the playlist"},
768 { "playlist_stop", vlcInstance_playlist_stop, METH_VARARGS,
769 "playlist_stop() Stop the current stream"},
770 { "playlist_next", vlcInstance_playlist_next, METH_VARARGS,
771 "playlist_next() Play the next item"},
772 { "playlist_prev", vlcInstance_playlist_prev, METH_VARARGS,
773 "playlist_prev() Play the previous item"},
774 { "playlist_clear", vlcInstance_playlist_clear, METH_VARARGS,
775 "playlist_clear() Clear the playlist"},
776 { "playlist_add", vlcInstance_playlist_add, METH_VARARGS,
777 "playlist_add(mrl=str, name=str, options=list) -> int Add a new item to the playlist. options is a list of strings."},
778 { "playlist_delete_item", vlcInstance_playlist_delete_item, METH_VARARGS,
779 "playlist_delete_item(id=int) Delete the given item"},
780 { "playlist_get_media_player", vlcInstance_playlist_get_media_player, METH_VARARGS,
781 "playlist_get_media_player() -> object Return the current media instance"},
782 { "video_set_parent", vlcInstance_video_set_parent, METH_VARARGS,
783 "video_set_parent(xid=int) Set the parent xid/HWND/CGrafPort"},
784 { "video_get_parent", vlcInstance_video_get_parent, METH_VARARGS,
785 "video_get_parent(xid=int) Get the parent xid/HWND/CGrafPort"},
786 { "video_set_size", vlcInstance_video_set_size, METH_VARARGS,
787 "video_set_size(width=int, height=int) Set the video width and height"},
788 { "audio_toggle_mute", vlcInstance_audio_toggle_mute, METH_VARARGS,
789 "audio_toggle_mute() Toggle the mute state"},
790 { "audio_get_mute", vlcInstance_audio_get_mute, METH_VARARGS,
791 "audio_get_mute() -> int Get the mute state"},
792 { "audio_set_mute", vlcInstance_audio_set_mute, METH_VARARGS,
793 "audio_set_mute(state=int) Set the mute state"},
794 { "audio_get_volume", vlcInstance_audio_get_volume, METH_VARARGS,
795 "audio_get_volume() -> int Get the audio volume"},
796 { "audio_set_volume", vlcInstance_audio_set_volume, METH_VARARGS,
797 "audio_set_volume(volume=int) Set the audio volume"},
798 { "audio_get_channel", vlcInstance_audio_get_channel, METH_VARARGS,
799 "audio_get_channel() -> int Get current audio channel" },
800 { "audio_set_channel", vlcInstance_audio_set_channel, METH_VARARGS,
801 "audio_set_channel(int) Set current audio channel" },
803 { "media_new", vlcInstance_media_new, METH_VARARGS,
804 "media_new(str) -> object Create a media descriptor with the given mrl."},
806 { "vlm_add_broadcast", vlcInstance_vlm_add_broadcast, METH_VARARGS | METH_KEYWORDS,
807 "vlm_add_broadcast(name=str, input=str, output=str, options=list, enable=int, loop=int) Add a new broadcast" },
808 { "vlm_del_media", vlcInstance_vlm_del_media, METH_VARARGS,
809 "vlm_del_media(name=str) Delete a media" },
810 { "vlm_set_enabled", vlcInstance_vlm_set_enabled, METH_VARARGS,
811 "vlm_set_enabled(name=str, enabled=int) Enable/disable a media" },
812 { "vlm_set_output", vlcInstance_vlm_set_output, METH_VARARGS,
813 "vlm_set_output(name=str, output=str) Set the output" },
814 { "vlm_set_input", vlcInstance_vlm_set_input, METH_VARARGS,
815 "vlm_set_input(name=str, output=str) Set the input" },
816 { "vlm_add_input", vlcInstance_vlm_add_input, METH_VARARGS,
817 "vlm_add_input(name=str, output=str) Add a media's input MRL" },
818 { "vlm_set_loop", vlcInstance_vlm_set_loop, METH_VARARGS,
819 "vlm_set_loop(name=str, loop=int) Change the looping value" },
820 { "vlm_change_media", vlcInstance_vlm_change_media, METH_VARARGS | METH_KEYWORDS,
821 "vlm_change_media(name=str, input=str, output=str, options=list, enable=int, loop=int) Change the broadcast parameters" },
822 { "vlm_play_media", vlcInstance_vlm_play_media, METH_VARARGS,
823 "vlm_play_media(name=str) Plays the named broadcast." },
824 { "vlm_stop_media", vlcInstance_vlm_stop_media, METH_VARARGS,
825 "vlm_stop_media(name=str) Stops the named broadcast." },
826 { "vlm_pause_media", vlcInstance_vlm_pause_media, METH_VARARGS,
827 "vlm_pause_media(name=str) Pauses the named broadcast." },
828 { "vlm_seek_media", vlcInstance_vlm_seek_media, METH_VARARGS,
829 "vlm_seek_media(name=str, percentage=float) Seeks in the named broadcast." },
830 { "vlm_show_media", vlcInstance_vlm_show_media, METH_VARARGS,
831 "vlm_show_media(name=str) Return information of the named broadcast." },
833 { NULL, NULL, 0, NULL },
836 static PyTypeObject vlcInstance_Type =
838 PyObject_HEAD_INIT( NULL )
840 "vlc.Instance", /*tp_name*/
841 sizeof( vlcInstance_Type ), /*tp_basicsize*/
843 ( destructor )vlcInstance_dealloc, /*tp_dealloc*/
850 0, /*tp_as_sequence*/
858 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
859 "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 */
862 0, /* tp_richcompare */
863 0, /* tp_weaklistoffset */
866 vlcInstance_methods, /* tp_methods */
871 0, /* tp_descr_get */
872 0, /* tp_descr_set */
873 0, /* tp_dictoffset */
876 vlcInstance_new, /* tp_new */