1 /*****************************************************************************
2 * vlc_mediacontrol.c: vlc.MediaControl 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 /*****************************************************************************
26 * VLC MediaControl object implementation
27 *****************************************************************************/
29 /* The MediaControl constructor takes either an existing vlc.Instance or a
32 MediaControl_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
35 mediacontrol_Exception *exception = NULL;
36 PyObject* py_param = NULL;
37 char** ppsz_args = NULL;
38 libvlc_instance_t* p_instance = NULL;
39 Py_ssize_t i_size = 0;
41 self = PyObject_New( MediaControl, &MediaControl_Type );
43 if( PyArg_ParseTuple( args, "O", &py_param ) )
45 Py_INCREF( py_param );
46 if( PyObject_TypeCheck( py_param, &vlcInstance_Type ) == 1 )
48 p_instance = ((vlcInstance*)py_param)->p_instance;
54 if( ! PySequence_Check( py_param ) )
56 PyErr_SetString( PyExc_TypeError, "Parameter must be a vlc.Instance or a sequence of strings." );
57 Py_DECREF( py_param );
60 i_size = PySequence_Size( py_param );
61 ppsz_args = malloc( ( i_size + 1 ) * sizeof( char * ) );
64 PyErr_SetString( PyExc_MemoryError, "Out of memory" );
65 Py_DECREF( py_param );
69 for ( i_index = 0; i_index < i_size; i_index++ )
72 strdup( PyString_AsString( PyObject_Str(
73 PySequence_GetItem( py_param,
76 ppsz_args[i_size] = NULL;
78 Py_DECREF( py_param );
82 /* No arguments were given. Clear the exception raised
83 by PyArg_ParseTuple. */
87 Py_BEGIN_ALLOW_THREADS
91 self->mc = mediacontrol_new_from_instance( p_instance, exception );
95 self->mc = mediacontrol_new( i_size, ppsz_args, exception );
101 return ( PyObject * )self;
105 MediaControl_dealloc( PyObject *self )
111 MediaControl_get_vlc_instance( PyObject *self, PyObject *args )
115 p_ret = PyObject_New( vlcInstance, &vlcInstance_Type );
118 p_ret->p_instance = mediacontrol_get_libvlc_instance( SELF->mc );
119 return ( PyObject * )p_ret;
123 * Return the current position in the stream. The returned value can
124 be relative or absolute ( according to PositionOrigin ) and the unit
125 is set by PositionKey
128 MediaControl_get_media_position( PyObject *self, PyObject *args )
130 mediacontrol_Position* pos;
131 mediacontrol_Exception* exception = NULL;
135 mediacontrol_PositionOrigin origin;
136 mediacontrol_PositionKey key;
138 if( !PyArg_ParseTuple( args, "OO", &py_origin, &py_key ) )
141 origin = positionOrigin_py_to_c( py_origin );
142 key = positionKey_py_to_c( py_key );
144 Py_BEGIN_ALLOW_THREADS
146 pos = mediacontrol_get_media_position( SELF->mc, origin, key, exception );
150 py_retval = ( PyObject* )position_c_to_py( pos );
155 /** Set the media position */
157 MediaControl_set_media_position( PyObject *self, PyObject *args )
159 mediacontrol_Exception* exception = NULL;
160 mediacontrol_Position *a_position;
163 if( !PyArg_ParseTuple( args, "O", &py_pos ) )
166 a_position = position_py_to_c( py_pos );
169 PyErr_SetString( PyExc_MemoryError, "Out of memory" );
173 Py_BEGIN_ALLOW_THREADS
175 mediacontrol_set_media_position( SELF->mc, a_position, exception );
180 Py_INCREF( Py_None );
185 MediaControl_start( PyObject *self, PyObject *args )
187 mediacontrol_Position *a_position;
188 mediacontrol_Exception *exception = NULL;
191 if( !PyArg_ParseTuple( args, "O", &py_pos ) )
193 /* No argument. Use a default 0 value. */
197 a_position = position_py_to_c( py_pos );
201 Py_BEGIN_ALLOW_THREADS
203 mediacontrol_start( SELF->mc, a_position, exception );
208 Py_INCREF( Py_None );
213 MediaControl_pause( PyObject *self, PyObject *args )
215 mediacontrol_Position *a_position;
216 mediacontrol_Exception *exception = NULL;
219 if( !PyArg_ParseTuple( args, "O", &py_pos ) )
221 /* No argument. Use a default 0 value. */
225 a_position = position_py_to_c( py_pos );
229 Py_BEGIN_ALLOW_THREADS
231 mediacontrol_pause( SELF->mc, a_position, exception );
236 Py_INCREF( Py_None );
241 MediaControl_resume( PyObject *self, PyObject *args )
243 mediacontrol_Position *a_position;
244 mediacontrol_Exception *exception = NULL;
247 if( !PyArg_ParseTuple( args, "O", &py_pos ) )
249 /* No argument. Use a default 0 value. */
253 a_position = position_py_to_c( py_pos );
257 Py_BEGIN_ALLOW_THREADS
259 mediacontrol_start( SELF->mc, a_position, exception );
264 Py_INCREF( Py_None );
269 MediaControl_stop( PyObject *self, PyObject *args )
271 mediacontrol_Position *a_position;
272 mediacontrol_Exception *exception = NULL;
275 if( !PyArg_ParseTuple( args, "O", &py_pos ) )
277 /* No argument. Use a default 0 value. */
281 a_position = position_py_to_c( py_pos );
285 Py_BEGIN_ALLOW_THREADS
287 mediacontrol_stop( SELF->mc, a_position, exception );
292 Py_INCREF( Py_None );
297 MediaControl_exit( PyObject *self, PyObject *args )
299 mediacontrol_exit( SELF->mc );
300 Py_INCREF( Py_None );
305 MediaControl_playlist_add_item( PyObject *self, PyObject *args )
308 mediacontrol_Exception *exception = NULL;
310 if( !PyArg_ParseTuple( args, "s", &psz_file ) )
313 Py_BEGIN_ALLOW_THREADS
315 mediacontrol_playlist_add_item( SELF->mc, psz_file, exception );
319 Py_INCREF( Py_None );
324 MediaControl_playlist_clear( PyObject *self, PyObject *args )
326 mediacontrol_Exception *exception = NULL;
328 Py_BEGIN_ALLOW_THREADS
330 mediacontrol_playlist_clear( SELF->mc, exception );
334 Py_INCREF( Py_None );
339 MediaControl_playlist_get_list( PyObject *self, PyObject *args )
342 mediacontrol_Exception *exception = NULL;
343 mediacontrol_PlaylistSeq* pl;
345 Py_ssize_t i_playlist_size;
347 Py_BEGIN_ALLOW_THREADS
349 pl = mediacontrol_playlist_get_list( SELF->mc, exception );
353 i_playlist_size = pl->size;
355 py_retval = PyList_New( i_playlist_size );
357 for ( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
359 PyList_SetItem( py_retval, i_index,
360 Py_BuildValue( "s", pl->data[i_index] ) );
362 mediacontrol_PlaylistSeq__free( pl );
369 MediaControl_snapshot( PyObject *self, PyObject *args )
371 mediacontrol_RGBPicture *p_retval = NULL;
372 mediacontrol_Exception* exception = NULL;
373 mediacontrol_Position *a_position = NULL;
374 PyObject *py_pos = NULL;
375 PyObject *py_obj = NULL;
377 if( !PyArg_ParseTuple( args, "O", &py_pos ) )
380 a_position = position_py_to_c( py_pos );
382 Py_BEGIN_ALLOW_THREADS
384 p_retval = mediacontrol_snapshot( SELF->mc, a_position, exception );
391 Py_INCREF( Py_None );
395 /* FIXME: create a real RGBPicture object */
396 py_obj = PyDict_New();
398 PyDict_SetItemString( py_obj, "width",
399 Py_BuildValue( "i", p_retval->width ) );
400 PyDict_SetItemString( py_obj, "height",
401 Py_BuildValue( "i", p_retval->height ) );
402 PyDict_SetItemString( py_obj, "type",
403 Py_BuildValue( "i", p_retval->type ) );
404 PyDict_SetItemString( py_obj, "data",
405 Py_BuildValue( "s#", p_retval->data, p_retval->size ) );
406 PyDict_SetItemString( py_obj, "date",
407 Py_BuildValue( "L", p_retval->date ) );
409 mediacontrol_RGBPicture__free( p_retval );
415 MediaControl_display_text( PyObject *self, PyObject *args )
417 mediacontrol_Exception* exception = NULL;
418 PyObject *py_begin, *py_end;
420 mediacontrol_Position * begin;
421 mediacontrol_Position * end;
423 if( !PyArg_ParseTuple( args, "sOO", &message, &py_begin, &py_end ) )
426 begin = position_py_to_c( py_begin );
427 end = position_py_to_c( py_end );
429 Py_BEGIN_ALLOW_THREADS
431 mediacontrol_display_text( SELF->mc, message, begin, end, exception );
438 Py_INCREF( Py_None );
443 MediaControl_get_stream_information( PyObject *self, PyObject *args )
445 mediacontrol_StreamInformation *retval = NULL;
446 mediacontrol_Exception* exception = NULL;
449 Py_BEGIN_ALLOW_THREADS
451 retval = mediacontrol_get_stream_information(
452 SELF->mc, mediacontrol_MediaTime, exception );
456 py_obj = PyDict_New( );
458 /* FIXME: create a real StreamInformation object */
459 PyDict_SetItemString( py_obj, "status",
460 Py_BuildValue( "i", retval->streamstatus ) );
461 PyDict_SetItemString( py_obj, "url",
462 Py_BuildValue( "s", retval->url ) );
463 PyDict_SetItemString( py_obj, "position",
464 Py_BuildValue( "L", retval->position ) );
465 PyDict_SetItemString( py_obj, "length",
466 Py_BuildValue( "L", retval->length ) );
468 mediacontrol_StreamInformation__free( retval );
474 MediaControl_sound_set_volume( PyObject *self, PyObject *args )
476 mediacontrol_Exception* exception = NULL;
477 unsigned short volume;
479 if( !PyArg_ParseTuple( args, "H", &volume ) )
482 Py_BEGIN_ALLOW_THREADS
484 mediacontrol_sound_set_volume( SELF->mc, volume, exception );
488 Py_INCREF( Py_None );
493 MediaControl_sound_get_volume( PyObject *self, PyObject *args )
495 mediacontrol_Exception* exception = NULL;
497 unsigned short volume;
499 Py_BEGIN_ALLOW_THREADS
501 volume = mediacontrol_sound_get_volume( SELF->mc, exception );
505 py_retval = Py_BuildValue( "H", volume );
510 MediaControl_set_rate( PyObject *self, PyObject *args )
512 mediacontrol_Exception* exception = NULL;
515 if( !PyArg_ParseTuple( args, "i", &rate ) )
518 Py_BEGIN_ALLOW_THREADS
520 mediacontrol_set_rate( SELF->mc, rate, exception );
524 Py_INCREF( Py_None );
529 MediaControl_get_rate( PyObject *self, PyObject *args )
531 mediacontrol_Exception* exception = NULL;
535 Py_BEGIN_ALLOW_THREADS
537 rate = mediacontrol_get_rate( SELF->mc, exception );
541 py_retval = Py_BuildValue( "i", rate );
546 MediaControl_set_fullscreen( PyObject *self, PyObject *args )
548 mediacontrol_Exception* exception = NULL;
551 if( !PyArg_ParseTuple( args, "i", &fs ) )
554 Py_BEGIN_ALLOW_THREADS
556 mediacontrol_set_fullscreen( SELF->mc, fs, exception );
560 Py_INCREF( Py_None );
565 MediaControl_get_fullscreen( PyObject *self, PyObject *args )
567 mediacontrol_Exception* exception = NULL;
571 Py_BEGIN_ALLOW_THREADS
573 fs = mediacontrol_get_fullscreen( SELF->mc, exception );
577 py_retval = Py_BuildValue( "i", fs );
582 MediaControl_set_visual( PyObject *self, PyObject *args )
584 mediacontrol_Exception* exception = NULL;
587 if( !PyArg_ParseTuple( args, "i", &visual ) )
590 Py_BEGIN_ALLOW_THREADS
592 mediacontrol_set_visual( SELF->mc, visual, exception );
596 Py_INCREF( Py_None );
600 static PyMethodDef MediaControl_methods[] =
602 { "get_vlc_instance", MediaControl_get_vlc_instance, METH_VARARGS,
603 "get_vlc_instance( ) -> Instance Get matching vlc.Instance." },
604 { "get_media_position", MediaControl_get_media_position, METH_VARARGS,
605 "get_media_position( origin, key ) -> Position Get current media position." },
606 { "set_media_position", MediaControl_set_media_position, METH_VARARGS,
607 "set_media_position( Position ) Set media position" },
608 { "start", MediaControl_start, METH_VARARGS,
609 "start( Position ) Start the player." },
610 { "pause", MediaControl_pause, METH_VARARGS,
611 "pause( Position ) Pause the player." },
612 { "resume", MediaControl_resume, METH_VARARGS,
613 "resume( Position ) Resume the player" },
614 { "stop", MediaControl_stop, METH_VARARGS,
615 "stop( Position ) Stop the player" },
616 { "exit", MediaControl_exit, METH_VARARGS,
617 "exit( ) Exit the player" },
618 { "playlist_add_item", MediaControl_playlist_add_item, METH_VARARGS,
619 "playlist_add_item( str ) Add an item to the playlist" },
620 { "playlist_get_list", MediaControl_playlist_get_list, METH_VARARGS,
621 "playlist_get_list( ) -> list Get the contents of the playlist" },
622 { "playlist_clear", MediaControl_playlist_clear, METH_VARARGS,
623 "clear( ) Clear the playlist." },
624 { "snapshot", MediaControl_snapshot, METH_VARARGS,
625 "snapshot( Position ) -> dict Take a snapshot" },
626 { "display_text", MediaControl_display_text, METH_VARARGS,
627 "display_text( str, Position, Position ) Display a text on the video" },
628 { "get_stream_information", MediaControl_get_stream_information,
630 "get_stream_information( ) -> dict Get information about the stream"},
631 { "sound_get_volume", MediaControl_sound_get_volume, METH_VARARGS,
632 "sound_get_volume( ) -> int Get the volume" },
633 { "sound_set_volume", MediaControl_sound_set_volume, METH_VARARGS,
634 "sound_set_volume( int ) Set the volume" },
635 { "set_visual", MediaControl_set_visual, METH_VARARGS,
636 "set_visual( int ) Set the embedding window visual ID" },
637 { "get_rate", MediaControl_get_rate, METH_VARARGS,
638 "get_rate( ) -> int Get the rate" },
639 { "set_rate", MediaControl_set_rate, METH_VARARGS,
640 "set_rate( int ) Set the rate" },
641 { "get_fullscreen", MediaControl_get_fullscreen, METH_VARARGS,
642 "get_fullscreen( ) -> int Get the fullscreen status" },
643 { "set_fullscreen", MediaControl_set_fullscreen, METH_VARARGS,
644 "set_fullscreen( int ) Set the fullscreen status" },
645 { NULL, NULL, 0, NULL },
648 static PyTypeObject MediaControl_Type =
650 PyObject_HEAD_INIT( NULL )
652 "vlc.MediaControl", /*tp_name*/
653 sizeof( MediaControl_Type ), /*tp_basicsize*/
655 ( destructor )MediaControl_dealloc, /*tp_dealloc*/
662 0, /*tp_as_sequence*/
670 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
671 "Control of a VLC instance.\n\nvlc.MediaControl(args): initialisation with a list of VLC parameters.\nvlc.MediaControl(instance): initialisation with an existing vlc.Instance", /* tp_doc */
674 0, /* tp_richcompare */
675 0, /* tp_weaklistoffset */
678 MediaControl_methods, /* tp_methods */
683 0, /* tp_descr_get */
684 0, /* tp_descr_set */
685 0, /* tp_dictoffset */
688 MediaControl_new, /* tp_new */