1 /*****************************************************************************
2 * core.c: Core functions : init, playlist, stream management
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Olivier Aubert <olivier.aubert@liris.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 *****************************************************************************/
24 #include <mediacontrol_internal.h>
25 #include <vlc/mediacontrol.h>
27 #include <vlc/libvlc.h>
31 #include <vlc_demux.h>
35 #define HAS_SNAPSHOT 1
41 #include <stdlib.h> /* malloc(), free() */
44 #include <errno.h> /* ENOMEM */
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
54 #ifdef HAVE_SYS_TYPES_H
55 # include <sys/types.h>
58 mediacontrol_Instance* mediacontrol_new( int argc, char** argv, mediacontrol_Exception *exception )
60 mediacontrol_Instance* retval;
61 libvlc_exception_t ex;
62 char** ppsz_argv = NULL;
65 libvlc_exception_init( &ex );
66 exception=mediacontrol_exception_init( exception );
68 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
70 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
72 /* Prepend a dummy argv[0] so that users of the API do not have to
73 do it themselves, and can simply provide the args list. */
74 ppsz_argv = malloc( ( argc + 2 ) * sizeof( char * ) ) ;
76 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
78 ppsz_argv[0] = strdup("vlc");
79 for ( i_index = 0; i_index < argc; i_index++ )
80 ppsz_argv[i_index + 1] = argv[i_index];
81 ppsz_argv[argc + 1] = NULL;
83 retval->p_instance = libvlc_new( argc + 1, ppsz_argv, &ex );
84 retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
85 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
90 mediacontrol_exit( mediacontrol_Instance *self )
92 libvlc_exception_t ex;
93 libvlc_exception_init( &ex );
95 libvlc_destroy( self->p_instance, &ex );
99 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
101 return self->p_instance;
104 mediacontrol_Instance *
105 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
106 mediacontrol_Exception *exception )
108 mediacontrol_Instance* retval;
110 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
113 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
115 retval->p_instance = p_instance;
116 retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
120 /**************************************************************************
121 * Playback management
122 **************************************************************************/
123 mediacontrol_Position*
124 mediacontrol_get_media_position( mediacontrol_Instance *self,
125 const mediacontrol_PositionOrigin an_origin,
126 const mediacontrol_PositionKey a_key,
127 mediacontrol_Exception *exception )
129 mediacontrol_Position* retval = NULL;
130 libvlc_exception_t ex;
132 libvlc_input_t * p_input;
134 exception = mediacontrol_exception_init( exception );
135 libvlc_exception_init( &ex );
137 retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
138 retval->origin = an_origin;
141 p_input = libvlc_playlist_get_input( self->p_instance, &ex);
142 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
144 if( an_origin != mediacontrol_AbsolutePosition )
146 libvlc_input_free( p_input );
147 /* Relative or ModuloPosition make no sense */
148 RAISE_NULL( mediacontrol_PositionOriginNotSupported,
149 "Only absolute position is valid." );
152 /* We are asked for an AbsolutePosition. */
153 pos = libvlc_input_get_time( p_input, &ex );
155 if( a_key == mediacontrol_MediaTime )
161 if( ! self->p_playlist->p_input )
163 libvlc_input_free( p_input );
164 RAISE_NULL( mediacontrol_InternalException,
167 retval->value = mediacontrol_unit_convert( self->p_playlist->p_input,
168 mediacontrol_MediaTime,
172 libvlc_input_free( p_input );
176 /* Sets the media position */
178 mediacontrol_set_media_position( mediacontrol_Instance *self,
179 const mediacontrol_Position * a_position,
180 mediacontrol_Exception *exception )
182 libvlc_input_t * p_input;
183 libvlc_exception_t ex;
186 libvlc_exception_init( &ex );
187 mediacontrol_exception_init( exception );
189 p_input = libvlc_playlist_get_input( self->p_instance, &ex);
190 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
192 i_pos = mediacontrol_position2microsecond( self->p_playlist->p_input, a_position );
193 libvlc_input_set_time( p_input, i_pos / 1000, &ex );
194 libvlc_input_free( p_input );
195 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
198 /* Starts playing a stream */
200 * Known issues: since moving in the playlist using playlist_Next
201 * or playlist_Prev implies starting to play items, the a_position
202 * argument will be only honored for the 1st item in the list.
204 * XXX:FIXME split moving in the playlist and playing items two
205 * different actions or make playlist_<Next|Prev> accept a time
206 * value to start to play from.
209 mediacontrol_start( mediacontrol_Instance *self,
210 const mediacontrol_Position * a_position,
211 mediacontrol_Exception *exception )
213 playlist_t * p_playlist = self->p_playlist;
215 exception = mediacontrol_exception_init( exception );
218 RAISE( mediacontrol_PlaylistException, "No available playlist" );
222 vlc_mutex_lock( &p_playlist->object_lock );
223 if( p_playlist->i_size )
226 char *psz_from = NULL;
228 psz_from = ( char * )malloc( 20 * sizeof( char ) );
229 if( psz_from && p_playlist->status.p_item )
231 i_from = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
234 snprintf( psz_from, 20, "start-time=%i", i_from );
235 input_ItemAddOption( p_playlist->status.p_item->p_input, psz_from );
239 vlc_mutex_unlock( &p_playlist->object_lock );
240 playlist_Play( p_playlist );
244 RAISE( mediacontrol_PlaylistException, "Empty playlist." );
245 vlc_mutex_unlock( &p_playlist->object_lock );
250 mediacontrol_pause( mediacontrol_Instance *self,
251 const mediacontrol_Position * a_position,
252 mediacontrol_Exception *exception )
254 input_thread_t *p_input = self->p_playlist->p_input;
256 /* FIXME: use the a_position parameter */
257 exception=mediacontrol_exception_init( exception );
258 if( p_input != NULL )
260 var_SetInteger( p_input, "state", PAUSE_S );
264 RAISE( mediacontrol_InternalException, "No input" );
269 mediacontrol_resume( mediacontrol_Instance *self,
270 const mediacontrol_Position * a_position,
271 mediacontrol_Exception *exception )
273 input_thread_t *p_input = self->p_playlist->p_input;
275 /* FIXME: use the a_position parameter */
276 exception=mediacontrol_exception_init( exception );
277 if( p_input != NULL )
279 var_SetInteger( p_input, "state", PAUSE_S );
283 RAISE( mediacontrol_InternalException, "No input" );
288 mediacontrol_stop( mediacontrol_Instance *self,
289 const mediacontrol_Position * a_position,
290 mediacontrol_Exception *exception )
292 /* FIXME: use the a_position parameter */
293 exception=mediacontrol_exception_init( exception );
294 if( !self->p_playlist )
296 RAISE( mediacontrol_PlaylistException, "No playlist" );
299 playlist_Stop( self->p_playlist );
302 /**************************************************************************
303 * Playlist management
304 **************************************************************************/
307 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
308 const char * psz_file,
309 mediacontrol_Exception *exception )
311 libvlc_exception_t ex;
313 mediacontrol_exception_init( exception );
314 libvlc_exception_init( &ex );
316 libvlc_playlist_add( self->p_instance, psz_file, psz_file, &ex );
317 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
321 mediacontrol_playlist_next_item( mediacontrol_Instance *self,
322 mediacontrol_Exception *exception )
324 libvlc_exception_t ex;
326 mediacontrol_exception_init( exception );
327 libvlc_exception_init( &ex );
329 libvlc_playlist_next( self->p_instance, &ex );
330 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
334 mediacontrol_playlist_clear( mediacontrol_Instance *self,
335 mediacontrol_Exception *exception )
337 libvlc_exception_t ex;
339 mediacontrol_exception_init( exception );
340 libvlc_exception_init( &ex );
342 libvlc_playlist_clear( self->p_instance, &ex );
343 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
346 mediacontrol_PlaylistSeq *
347 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
348 mediacontrol_Exception *exception )
350 mediacontrol_PlaylistSeq *retval = NULL;
352 playlist_t * p_playlist = self->p_playlist;
355 exception=mediacontrol_exception_init( exception );
358 RAISE( mediacontrol_PlaylistException, "No playlist" );
362 vlc_mutex_lock( &p_playlist->object_lock );
363 i_playlist_size = p_playlist->i_size;
365 retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
367 for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
369 retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->p_input->psz_uri );
371 vlc_mutex_unlock( &p_playlist->object_lock );
376 /***************************************************************************
378 ***************************************************************************/
380 mediacontrol_StreamInformation *
381 mediacontrol_get_stream_information( mediacontrol_Instance *self,
382 mediacontrol_PositionKey a_key,
383 mediacontrol_Exception *exception )
385 mediacontrol_StreamInformation *retval = NULL;
386 input_thread_t *p_input = self->p_playlist->p_input;
389 retval = ( mediacontrol_StreamInformation* )
390 malloc( sizeof( mediacontrol_StreamInformation ) );
393 RAISE( mediacontrol_InternalException, "Out of memory" );
399 /* No p_input defined */
400 retval->streamstatus = mediacontrol_UndefinedStatus;
401 retval->url = strdup( "None" );
402 retval->position = 0;
407 switch( var_GetInteger( p_input, "state" ) )
410 retval->streamstatus = mediacontrol_PlayingStatus;
413 retval->streamstatus = mediacontrol_PauseStatus;
416 retval->streamstatus = mediacontrol_InitStatus;
419 retval->streamstatus = mediacontrol_EndStatus;
422 retval->streamstatus = mediacontrol_UndefinedStatus;
426 retval->url = strdup( p_input->input.p_item->psz_uri );
428 /* TIME and LENGTH are in microseconds. We want them in ms */
429 var_Get( p_input, "time", &val);
430 retval->position = val.i_time / 1000;
432 var_Get( p_input, "length", &val);
433 retval->length = val.i_time / 1000;
435 retval->position = mediacontrol_unit_convert( p_input,
436 mediacontrol_MediaTime, a_key,
438 retval->length = mediacontrol_unit_convert( p_input,
439 mediacontrol_MediaTime, a_key,