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_new_media_player( PyObject
*self
, PyObject
*args
)
120 libvlc_exception_t ex
;
121 libvlc_media_player_t
*p_mp
;
122 vlcMediaPlayer
*p_ret
;
125 p_mp
= libvlc_media_player_new( LIBVLC_INSTANCE(self
), &ex
);
128 p_ret
= PyObject_New( vlcMediaPlayer
, &vlcMediaPlayer_Type
);
130 Py_INCREF( p_ret
); /* Ah bon ? */
131 return ( PyObject
* )p_ret
;
135 vlcInstance_audio_toggle_mute( PyObject
*self
, PyObject
*args
)
137 libvlc_exception_t ex
;
139 libvlc_audio_toggle_mute( LIBVLC_INSTANCE(self
), &ex
);
141 Py_INCREF( Py_None
);
146 vlcInstance_audio_get_mute( PyObject
*self
, PyObject
*args
)
148 libvlc_exception_t ex
;
152 i_ret
= libvlc_audio_get_mute( LIBVLC_INSTANCE(self
), &ex
);
154 return Py_BuildValue( "i", i_ret
);
158 vlcInstance_audio_set_mute( PyObject
*self
, PyObject
*args
)
160 libvlc_exception_t ex
;
163 if( !PyArg_ParseTuple( args
, "i", &i_mute
) )
167 libvlc_audio_set_mute( LIBVLC_INSTANCE(self
), i_mute
, &ex
);
169 Py_INCREF( Py_None
);
174 vlcInstance_audio_get_volume( PyObject
*self
, PyObject
*args
)
176 libvlc_exception_t ex
;
180 i_ret
= libvlc_audio_get_volume( LIBVLC_INSTANCE(self
), &ex
);
182 return Py_BuildValue( "i", i_ret
);
186 vlcInstance_audio_set_volume( PyObject
*self
, PyObject
*args
)
188 libvlc_exception_t ex
;
191 if( !PyArg_ParseTuple( args
, "i", &i_volume
) )
195 libvlc_audio_set_volume( LIBVLC_INSTANCE(self
), i_volume
, &ex
);
197 Py_INCREF( Py_None
);
202 vlcInstance_audio_get_channel( PyObject
*self
, PyObject
*args
)
204 libvlc_exception_t ex
;
208 i_ret
= libvlc_audio_get_channel( LIBVLC_INSTANCE(self
), &ex
);
210 return Py_BuildValue( "i", i_ret
);
214 vlcInstance_audio_set_channel( PyObject
*self
, PyObject
*args
)
216 libvlc_exception_t ex
;
219 if( !PyArg_ParseTuple( args
, "i", &i_channel
) )
223 libvlc_audio_set_channel( LIBVLC_INSTANCE(self
), i_channel
, &ex
);
225 Py_INCREF( Py_None
);
229 /* vlm_add_broadcast : name, input MRL, output MRL
230 Keywords: options, enable, loop */
232 vlcInstance_vlm_add_broadcast( PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
234 libvlc_exception_t ex
;
235 static char *kwlist
[] = { "name", "input", "output",
236 "options", "enable", "loop", NULL
};
237 char* psz_name
= NULL
;
238 char* psz_input
= NULL
;
239 char* psz_output
= NULL
;
240 PyObject
* py_options
= NULL
;
244 char** ppsz_args
= NULL
;
246 if( !PyArg_ParseTupleAndKeywords( args
, kwds
, "sss|Oii", kwlist
,
248 &psz_input
, &psz_output
,
249 &py_options
, &i_enable
, &i_loop
) )
254 i_size
= pyoptions_to_args( py_options
, &ppsz_args
);
258 libvlc_vlm_add_broadcast( LIBVLC_INSTANCE(self
),
259 psz_name
, psz_input
, psz_output
,
260 i_size
, ppsz_args
, i_enable
, i_loop
, &ex
);
261 free_args( i_size
, ppsz_args
);
263 Py_INCREF( Py_None
);
268 vlcInstance_vlm_del_media( PyObject
*self
, PyObject
*args
)
270 libvlc_exception_t ex
;
273 if( !PyArg_ParseTuple( args
, "s", &psz_name
) )
277 libvlc_vlm_del_media( LIBVLC_INSTANCE(self
), psz_name
, &ex
);
279 Py_INCREF( Py_None
);
284 vlcInstance_vlm_set_enabled( PyObject
*self
, PyObject
*args
)
286 libvlc_exception_t ex
;
290 if( !PyArg_ParseTuple( args
, "si", &psz_name
, &i_enabled
) )
294 libvlc_vlm_set_enabled( LIBVLC_INSTANCE(self
), psz_name
, i_enabled
, &ex
);
296 Py_INCREF( Py_None
);
301 vlcInstance_vlm_set_output( PyObject
*self
, PyObject
*args
)
303 libvlc_exception_t ex
;
307 if( !PyArg_ParseTuple( args
, "ss", &psz_name
, &psz_output
) )
311 libvlc_vlm_set_output( LIBVLC_INSTANCE(self
), psz_name
, psz_output
, &ex
);
313 Py_INCREF( Py_None
);
318 vlcInstance_vlm_set_input( PyObject
*self
, PyObject
*args
)
320 libvlc_exception_t ex
;
324 if( !PyArg_ParseTuple( args
, "ss", &psz_name
, &psz_input
) )
328 libvlc_vlm_set_input( LIBVLC_INSTANCE(self
), psz_name
, psz_input
, &ex
);
330 Py_INCREF( Py_None
);
335 vlcInstance_vlm_add_input( PyObject
*self
, PyObject
*args
)
337 libvlc_exception_t ex
;
341 if( !PyArg_ParseTuple( args
, "ss", &psz_name
, &psz_input
) )
345 libvlc_vlm_add_input( LIBVLC_INSTANCE(self
), psz_name
, psz_input
, &ex
);
347 Py_INCREF( Py_None
);
352 vlcInstance_vlm_set_loop( PyObject
*self
, PyObject
*args
)
354 libvlc_exception_t ex
;
358 if( !PyArg_ParseTuple( args
, "si", &psz_name
, &i_loop
) )
362 libvlc_vlm_set_loop( LIBVLC_INSTANCE(self
), psz_name
, i_loop
, &ex
);
364 Py_INCREF( Py_None
);
369 vlcInstance_vlm_change_media( PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
371 libvlc_exception_t ex
;
372 static char *kwlist
[] = { "name", "input", "output",
373 "options", "enable", "loop", NULL
};
374 char* psz_name
= NULL
;
375 char* psz_input
= NULL
;
376 char* psz_output
= NULL
;
377 PyObject
* py_options
= NULL
;
381 char** ppsz_args
= NULL
;
383 if( !PyArg_ParseTupleAndKeywords( args
, kwds
, "sss|Oii", kwlist
,
385 &psz_input
, &psz_output
,
386 &py_options
, &i_enable
, &i_loop
) )
391 i_size
= pyoptions_to_args( py_options
, &ppsz_args
);
395 libvlc_vlm_change_media( LIBVLC_INSTANCE(self
),
396 psz_name
, psz_input
, psz_output
,
397 i_size
, ppsz_args
, i_enable
, i_loop
, &ex
);
398 free_args( i_size
, ppsz_args
);
400 Py_INCREF( Py_None
);
405 vlcInstance_vlm_play_media( PyObject
*self
, PyObject
*args
)
407 libvlc_exception_t ex
;
410 if( !PyArg_ParseTuple( args
, "s", &psz_name
) )
414 libvlc_vlm_play_media( LIBVLC_INSTANCE(self
), psz_name
, &ex
);
416 Py_INCREF( Py_None
);
421 vlcInstance_vlm_stop_media( PyObject
*self
, PyObject
*args
)
423 libvlc_exception_t ex
;
426 if( !PyArg_ParseTuple( args
, "s", &psz_name
) )
430 libvlc_vlm_stop_media( LIBVLC_INSTANCE(self
), psz_name
, &ex
);
432 Py_INCREF( Py_None
);
437 vlcInstance_vlm_pause_media( PyObject
*self
, PyObject
*args
)
439 libvlc_exception_t ex
;
442 if( !PyArg_ParseTuple( args
, "s", &psz_name
) )
446 libvlc_vlm_pause_media( LIBVLC_INSTANCE(self
), psz_name
, &ex
);
448 Py_INCREF( Py_None
);
453 vlcInstance_vlm_seek_media( PyObject
*self
, PyObject
*args
)
455 libvlc_exception_t ex
;
459 if( !PyArg_ParseTuple( args
, "sf", &psz_name
, &f_percentage
) )
463 libvlc_vlm_seek_media( LIBVLC_INSTANCE(self
), psz_name
, f_percentage
, &ex
);
465 Py_INCREF( Py_None
);
470 vlcInstance_vlm_show_media( PyObject
*self
, PyObject
*args
)
472 libvlc_exception_t ex
;
477 if( !PyArg_ParseTuple( args
, "s", &psz_name
) )
480 psz_ret
= libvlc_vlm_show_media( LIBVLC_INSTANCE(self
), psz_name
, &ex
);
482 o_ret
= Py_BuildValue( "s", psz_ret
);
488 vlcInstance_media_new( PyObject
*self
, PyObject
*args
)
490 libvlc_exception_t ex
;
491 libvlc_media_t
*p_media
;
492 char* psz_mrl
= NULL
;
495 if( !PyArg_ParseTuple( args
, "s", &psz_mrl
) )
499 p_media
= libvlc_media_new( LIBVLC_INSTANCE(self
), psz_mrl
, &ex
);
502 p_ret
= PyObject_New( vlcMedia
, &vlcMedia_Type
);
503 p_ret
->p_media
= p_media
;
504 Py_INCREF( p_ret
); /* Ah bon ? */
505 return ( PyObject
* )p_ret
;
509 static PyMethodDef vlcInstance_methods
[] =
511 { "get_vlc_id", vlcInstance_get_vlc_id
, METH_NOARGS
,
512 "get_vlc_id( ) -> int Get the instance id."},
513 { "audio_toggle_mute", vlcInstance_audio_toggle_mute
, METH_NOARGS
,
514 "audio_toggle_mute() Toggle the mute state"},
515 { "audio_get_mute", vlcInstance_audio_get_mute
, METH_NOARGS
,
516 "audio_get_mute() -> int Get the mute state"},
517 { "audio_set_mute", vlcInstance_audio_set_mute
, METH_VARARGS
,
518 "audio_set_mute(state=int) Set the mute state"},
519 { "audio_get_volume", vlcInstance_audio_get_volume
, METH_NOARGS
,
520 "audio_get_volume() -> int Get the audio volume"},
521 { "audio_set_volume", vlcInstance_audio_set_volume
, METH_VARARGS
,
522 "audio_set_volume(volume=int) Set the audio volume"},
523 { "audio_get_channel", vlcInstance_audio_get_channel
, METH_NOARGS
,
524 "audio_get_channel() -> int Get current audio channel" },
525 { "audio_set_channel", vlcInstance_audio_set_channel
, METH_VARARGS
,
526 "audio_set_channel(int) Set current audio channel" },
528 { "media_new", vlcInstance_media_new
, METH_VARARGS
,
529 "media_new(str) -> object Create a media object with the given mrl."},
531 { "mediaplayer_new", vlcInstance_new_media_player
, METH_NOARGS
,
532 "mediaplayer_new() -> object Create a media player."},
534 { "vlm_add_broadcast", vlcInstance_vlm_add_broadcast
, METH_VARARGS
| METH_KEYWORDS
,
535 "vlm_add_broadcast(name=str, input=str, output=str, options=list, enable=int, loop=int) Add a new broadcast" },
536 { "vlm_del_media", vlcInstance_vlm_del_media
, METH_VARARGS
,
537 "vlm_del_media(name=str) Delete a media" },
538 { "vlm_set_enabled", vlcInstance_vlm_set_enabled
, METH_VARARGS
,
539 "vlm_set_enabled(name=str, enabled=int) Enable/disable a media" },
540 { "vlm_set_output", vlcInstance_vlm_set_output
, METH_VARARGS
,
541 "vlm_set_output(name=str, output=str) Set the output" },
542 { "vlm_set_input", vlcInstance_vlm_set_input
, METH_VARARGS
,
543 "vlm_set_input(name=str, output=str) Set the input" },
544 { "vlm_add_input", vlcInstance_vlm_add_input
, METH_VARARGS
,
545 "vlm_add_input(name=str, output=str) Add a media's input MRL" },
546 { "vlm_set_loop", vlcInstance_vlm_set_loop
, METH_VARARGS
,
547 "vlm_set_loop(name=str, loop=int) Change the looping value" },
548 { "vlm_change_media", vlcInstance_vlm_change_media
, METH_VARARGS
| METH_KEYWORDS
,
549 "vlm_change_media(name=str, input=str, output=str, options=list, enable=int, loop=int) Change the broadcast parameters" },
550 { "vlm_play_media", vlcInstance_vlm_play_media
, METH_VARARGS
,
551 "vlm_play_media(name=str) Plays the named broadcast." },
552 { "vlm_stop_media", vlcInstance_vlm_stop_media
, METH_VARARGS
,
553 "vlm_stop_media(name=str) Stops the named broadcast." },
554 { "vlm_pause_media", vlcInstance_vlm_pause_media
, METH_VARARGS
,
555 "vlm_pause_media(name=str) Pauses the named broadcast." },
556 { "vlm_seek_media", vlcInstance_vlm_seek_media
, METH_VARARGS
,
557 "vlm_seek_media(name=str, percentage=float) Seeks in the named broadcast." },
558 { "vlm_show_media", vlcInstance_vlm_show_media
, METH_VARARGS
,
559 "vlm_show_media(name=str) Return information of the named broadcast." },
561 { NULL
, NULL
, 0, NULL
},
564 static PyTypeObject vlcInstance_Type
=
566 PyObject_HEAD_INIT( NULL
)
568 "vlc.Instance", /*tp_name*/
569 sizeof( vlcInstance_Type
), /*tp_basicsize*/
571 ( destructor
)vlcInstance_dealloc
, /*tp_dealloc*/
578 0, /*tp_as_sequence*/
586 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
587 "VLC Instance(args)", /* tp_doc */
590 0, /* tp_richcompare */
591 0, /* tp_weaklistoffset */
594 vlcInstance_methods
, /* tp_methods */
599 0, /* tp_descr_get */
600 0, /* tp_descr_set */
601 0, /* tp_dictoffset */
604 vlcInstance_new
, /* tp_new */