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 fprintf (stderr, "Instanciating mediacontrol\n");
44 if( PyArg_ParseTuple( args, "O", &py_param ) )
46 if( PyObject_TypeCheck( py_param, &vlcInstance_Type ) == 1 )
48 p_instance = ((vlcInstance*)py_param)->p_instance;
54 Py_INCREF( py_param );
55 if( ! PySequence_Check( py_param ) )
57 PyErr_SetString( PyExc_TypeError, "Parameter must be a vlc.Instance or a sequence of strings." );
58 Py_DECREF( py_param );
61 i_size = PySequence_Size( py_param );
62 ppsz_args = malloc( ( i_size + 1 ) * sizeof( char * ) );
65 PyErr_SetString( PyExc_MemoryError, "Out of memory" );
66 Py_DECREF( py_param );
70 for ( i_index = 0; i_index < i_size; i_index++ )
73 strdup( PyString_AsString( PyObject_Str(
74 PySequence_GetItem( py_param,
77 ppsz_args[i_size] = NULL;
78 Py_DECREF( py_param );
83 /* No arguments were given. Clear the exception raised
84 by PyArg_ParseTuple. */
88 Py_BEGIN_ALLOW_THREADS
92 self->mc = mediacontrol_new_from_instance( p_instance, exception );
93 Py_INCREF( py_param );
94 self->vlc_instance = ( vlcInstance* ) py_param;
98 self->mc = mediacontrol_new( i_size, ppsz_args, exception );
99 self->vlc_instance = PyObject_New( vlcInstance, &vlcInstance_Type );
100 self->vlc_instance->p_instance = mediacontrol_get_libvlc_instance( SELF->mc );
106 return ( PyObject * )self;
110 MediaControl_dealloc( PyObject *self )
112 fprintf(stderr, "MC dealloc\n");
113 Py_DECREF( SELF->vlc_instance );
114 PyObject_DEL( self );
118 MediaControl_get_vlc_instance( PyObject *self, PyObject *args )
122 p_ret = SELF->vlc_instance;
124 return ( PyObject * )p_ret;
128 MediaControl_get_mediaplayer( PyObject *self, PyObject *args )
130 vlcMediaPlayer *p_ret;
132 p_ret = PyObject_New( vlcMediaPlayer, &vlcMediaPlayer_Type );
133 p_ret->p_mp = mediacontrol_get_media_player( SELF->mc );
135 return ( PyObject * )p_ret;
139 * Return the current position in the stream. The returned value can
140 be relative or absolute ( according to PositionOrigin ) and the unit
141 is set by PositionKey
144 MediaControl_get_media_position( PyObject *self, PyObject *args )
146 mediacontrol_Position* pos;
147 mediacontrol_Exception* exception = NULL;
151 mediacontrol_PositionOrigin origin;
152 mediacontrol_PositionKey key;
154 if( !PyArg_ParseTuple( args, "OO", &py_origin, &py_key ) )
157 origin = positionOrigin_py_to_c( py_origin );
158 key = positionKey_py_to_c( py_key );
160 Py_BEGIN_ALLOW_THREADS
162 pos = mediacontrol_get_media_position( SELF->mc, origin, key, exception );
166 py_retval = ( PyObject* )position_c_to_py( pos );
171 /** Set the media position */
173 MediaControl_set_media_position( PyObject *self, PyObject *args )
175 mediacontrol_Exception* exception = NULL;
176 mediacontrol_Position *a_position;
179 if( !PyArg_ParseTuple( args, "O", &py_pos ) )
182 a_position = position_py_to_c( py_pos );
185 PyErr_SetString( PyExc_MemoryError, "Out of memory" );
189 Py_BEGIN_ALLOW_THREADS
191 mediacontrol_set_media_position( SELF->mc, a_position, exception );
196 Py_INCREF( Py_None );
201 MediaControl_start( PyObject *self, PyObject *args )
203 mediacontrol_Position *a_position;
204 mediacontrol_Exception *exception = NULL;
207 if( !PyArg_ParseTuple( args, "O", &py_pos ) )
209 /* No argument. Use a default 0 value. */
213 a_position = position_py_to_c( py_pos );
217 Py_BEGIN_ALLOW_THREADS
219 mediacontrol_start( SELF->mc, a_position, exception );
224 Py_INCREF( Py_None );
229 MediaControl_pause( PyObject *self, PyObject *args )
231 mediacontrol_Exception *exception = NULL;
233 Py_BEGIN_ALLOW_THREADS
235 mediacontrol_pause( SELF->mc, exception );
239 Py_INCREF( Py_None );
244 MediaControl_resume( PyObject *self, PyObject *args )
246 mediacontrol_Exception *exception = NULL;
248 Py_BEGIN_ALLOW_THREADS
250 mediacontrol_resume( SELF->mc, exception );
254 Py_INCREF( Py_None );
259 MediaControl_stop( PyObject *self, PyObject *args )
261 mediacontrol_Exception *exception = NULL;
263 Py_BEGIN_ALLOW_THREADS
265 mediacontrol_stop( SELF->mc, exception );
269 Py_INCREF( Py_None );
274 MediaControl_exit( PyObject *self, PyObject *args )
276 mediacontrol_exit( SELF->mc );
277 Py_INCREF( Py_None );
282 MediaControl_set_mrl( PyObject *self, PyObject *args )
285 mediacontrol_Exception *exception = NULL;
287 if( !PyArg_ParseTuple( args, "s", &psz_file ) )
290 Py_BEGIN_ALLOW_THREADS
292 mediacontrol_set_mrl( SELF->mc, psz_file, exception );
296 Py_INCREF( Py_None );
301 MediaControl_get_mrl( PyObject *self, PyObject *args )
305 mediacontrol_Exception *exception = NULL;
307 Py_BEGIN_ALLOW_THREADS
309 psz_file = mediacontrol_get_mrl( SELF->mc, exception );
313 py_retval = Py_BuildValue( "s", psz_file );
319 MediaControl_snapshot( PyObject *self, PyObject *args )
321 mediacontrol_RGBPicture *p_retval = NULL;
322 mediacontrol_Exception* exception = NULL;
323 mediacontrol_Position *a_position = NULL;
324 PyObject *py_pos = NULL;
325 PyObject *py_obj = NULL;
327 if( !PyArg_ParseTuple( args, "O", &py_pos ) )
330 a_position = position_py_to_c( py_pos );
332 Py_BEGIN_ALLOW_THREADS
334 p_retval = mediacontrol_snapshot( SELF->mc, a_position, exception );
341 Py_INCREF( Py_None );
345 /* FIXME: create a real RGBPicture object */
346 py_obj = PyDict_New();
348 PyDict_SetItemString( py_obj, "width",
349 Py_BuildValue( "i", p_retval->width ) );
350 PyDict_SetItemString( py_obj, "height",
351 Py_BuildValue( "i", p_retval->height ) );
352 PyDict_SetItemString( py_obj, "type",
353 Py_BuildValue( "i", p_retval->type ) );
354 PyDict_SetItemString( py_obj, "data",
355 Py_BuildValue( "s#", p_retval->data, p_retval->size ) );
356 PyDict_SetItemString( py_obj, "date",
357 Py_BuildValue( "L", p_retval->date ) );
359 mediacontrol_RGBPicture__free( p_retval );
365 MediaControl_display_text( PyObject *self, PyObject *args )
367 mediacontrol_Exception* exception = NULL;
368 PyObject *py_begin, *py_end;
370 mediacontrol_Position * begin;
371 mediacontrol_Position * end;
373 if( !PyArg_ParseTuple( args, "sOO", &message, &py_begin, &py_end ) )
376 begin = position_py_to_c( py_begin );
377 end = position_py_to_c( py_end );
379 Py_BEGIN_ALLOW_THREADS
381 mediacontrol_display_text( SELF->mc, message, begin, end, exception );
388 Py_INCREF( Py_None );
393 MediaControl_get_stream_information( PyObject *self, PyObject *args )
395 mediacontrol_StreamInformation *retval = NULL;
396 mediacontrol_Exception* exception = NULL;
399 Py_BEGIN_ALLOW_THREADS
401 retval = mediacontrol_get_stream_information(
402 SELF->mc, mediacontrol_MediaTime, exception );
406 py_obj = PyDict_New( );
408 /* FIXME: create a real StreamInformation object */
409 PyDict_SetItemString( py_obj, "status",
410 Py_BuildValue( "i", retval->streamstatus ) );
411 PyDict_SetItemString( py_obj, "url",
412 Py_BuildValue( "s", retval->url ) );
413 PyDict_SetItemString( py_obj, "position",
414 Py_BuildValue( "L", retval->position ) );
415 PyDict_SetItemString( py_obj, "length",
416 Py_BuildValue( "L", retval->length ) );
418 mediacontrol_StreamInformation__free( retval );
424 MediaControl_sound_set_volume( PyObject *self, PyObject *args )
426 mediacontrol_Exception* exception = NULL;
427 unsigned short volume;
429 if( !PyArg_ParseTuple( args, "H", &volume ) )
432 Py_BEGIN_ALLOW_THREADS
434 mediacontrol_sound_set_volume( SELF->mc, volume, exception );
438 Py_INCREF( Py_None );
443 MediaControl_sound_get_volume( PyObject *self, PyObject *args )
445 mediacontrol_Exception* exception = NULL;
447 unsigned short volume;
449 Py_BEGIN_ALLOW_THREADS
451 volume = mediacontrol_sound_get_volume( SELF->mc, exception );
455 py_retval = Py_BuildValue( "H", volume );
460 MediaControl_set_rate( PyObject *self, PyObject *args )
462 mediacontrol_Exception* exception = NULL;
465 if( !PyArg_ParseTuple( args, "i", &rate ) )
468 Py_BEGIN_ALLOW_THREADS
470 mediacontrol_set_rate( SELF->mc, rate, exception );
474 Py_INCREF( Py_None );
479 MediaControl_get_rate( PyObject *self, PyObject *args )
481 mediacontrol_Exception* exception = NULL;
485 Py_BEGIN_ALLOW_THREADS
487 rate = mediacontrol_get_rate( SELF->mc, exception );
491 py_retval = Py_BuildValue( "i", rate );
496 MediaControl_set_fullscreen( PyObject *self, PyObject *args )
498 mediacontrol_Exception* exception = NULL;
501 if( !PyArg_ParseTuple( args, "i", &fs ) )
504 Py_BEGIN_ALLOW_THREADS
506 mediacontrol_set_fullscreen( SELF->mc, fs, exception );
510 Py_INCREF( Py_None );
515 MediaControl_get_fullscreen( PyObject *self, PyObject *args )
517 mediacontrol_Exception* exception = NULL;
521 Py_BEGIN_ALLOW_THREADS
523 fs = mediacontrol_get_fullscreen( SELF->mc, exception );
527 py_retval = Py_BuildValue( "i", fs );
532 MediaControl_set_visual( PyObject *self, PyObject *args )
534 mediacontrol_Exception* exception = NULL;
537 if( !PyArg_ParseTuple( args, "i", &visual ) )
540 Py_BEGIN_ALLOW_THREADS
542 mediacontrol_set_visual( SELF->mc, visual, exception );
546 Py_INCREF( Py_None );
550 static PyMethodDef MediaControl_methods[] =
552 { "get_vlc_instance", MediaControl_get_vlc_instance, METH_VARARGS,
553 "get_vlc_instance( ) -> Instance Get embedded vlc.Instance." },
554 { "get_mediaplayer", MediaControl_get_mediaplayer, METH_VARARGS,
555 "get_mediaplayer( ) -> MediaPlayer Get embedded vlc.MediaPlayer." },
556 { "get_media_position", MediaControl_get_media_position, METH_VARARGS,
557 "get_media_position( origin, key ) -> Position Get current media position." },
558 { "set_media_position", MediaControl_set_media_position, METH_VARARGS,
559 "set_media_position( Position ) Set media position" },
560 { "start", MediaControl_start, METH_VARARGS,
561 "start( Position ) Start the player." },
562 { "pause", MediaControl_pause, METH_VARARGS,
563 "pause( Position ) Pause the player." },
564 { "resume", MediaControl_resume, METH_VARARGS,
565 "resume( Position ) Resume the player" },
566 { "stop", MediaControl_stop, METH_VARARGS,
567 "stop( Position ) Stop the player" },
568 { "exit", MediaControl_exit, METH_VARARGS,
569 "exit( ) Exit the player" },
570 { "set_mrl", MediaControl_set_mrl, METH_VARARGS,
571 "set_mrl( str ) Set the file to be played" },
572 { "get_mrl", MediaControl_get_mrl, METH_VARARGS,
573 "get_mrl( ) -> str Get the played file" },
574 { "snapshot", MediaControl_snapshot, METH_VARARGS,
575 "snapshot( Position ) -> dict Take a snapshot" },
576 { "display_text", MediaControl_display_text, METH_VARARGS,
577 "display_text( str, Position, Position ) Display a text on the video" },
578 { "get_stream_information", MediaControl_get_stream_information,
580 "get_stream_information( ) -> dict Get information about the stream"},
581 { "sound_get_volume", MediaControl_sound_get_volume, METH_VARARGS,
582 "sound_get_volume( ) -> int Get the volume" },
583 { "sound_set_volume", MediaControl_sound_set_volume, METH_VARARGS,
584 "sound_set_volume( int ) Set the volume" },
585 { "set_visual", MediaControl_set_visual, METH_VARARGS,
586 "set_visual( int ) Set the embedding window visual ID" },
587 { "get_rate", MediaControl_get_rate, METH_VARARGS,
588 "get_rate( ) -> int Get the rate" },
589 { "set_rate", MediaControl_set_rate, METH_VARARGS,
590 "set_rate( int ) Set the rate" },
591 { "get_fullscreen", MediaControl_get_fullscreen, METH_VARARGS,
592 "get_fullscreen( ) -> int Get the fullscreen status" },
593 { "set_fullscreen", MediaControl_set_fullscreen, METH_VARARGS,
594 "set_fullscreen( int ) Set the fullscreen status" },
595 { NULL, NULL, 0, NULL },
598 static PyTypeObject MediaControl_Type =
600 PyObject_HEAD_INIT( NULL )
602 "vlc.MediaControl", /*tp_name*/
603 sizeof( MediaControl_Type ), /*tp_basicsize*/
605 ( destructor )MediaControl_dealloc, /*tp_dealloc*/
612 0, /*tp_as_sequence*/
620 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
621 "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 */
624 0, /* tp_richcompare */
625 0, /* tp_weaklistoffset */
628 MediaControl_methods, /* tp_methods */
633 0, /* tp_descr_get */
634 0, /* tp_descr_set */
635 0, /* tp_dictoffset */
638 MediaControl_new, /* tp_new */