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;
63 libvlc_exception_init( &ex );
64 exception=mediacontrol_exception_init( exception );
66 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
68 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
70 retval->p_instance = libvlc_new( argc, argv, &ex );
71 retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
72 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
77 mediacontrol_exit( mediacontrol_Instance *self )
79 libvlc_exception_t ex;
80 libvlc_exception_init( &ex );
82 libvlc_destroy( self->p_instance, &ex );
86 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
88 return self->p_instance;
91 mediacontrol_Instance *
92 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
93 mediacontrol_Exception *exception )
95 mediacontrol_Instance* retval;
97 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
100 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
102 retval->p_instance = p_instance;
103 retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
107 /**************************************************************************
108 * Playback management
109 **************************************************************************/
110 mediacontrol_Position*
111 mediacontrol_get_media_position( mediacontrol_Instance *self,
112 const mediacontrol_PositionOrigin an_origin,
113 const mediacontrol_PositionKey a_key,
114 mediacontrol_Exception *exception )
116 mediacontrol_Position* retval = NULL;
117 libvlc_exception_t ex;
119 libvlc_input_t * p_input;
121 exception = mediacontrol_exception_init( exception );
122 libvlc_exception_init( &ex );
124 retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
125 retval->origin = an_origin;
128 p_input = libvlc_playlist_get_input( self->p_instance, &ex);
129 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
131 if( an_origin != mediacontrol_AbsolutePosition )
133 libvlc_input_free( p_input );
134 /* Relative or ModuloPosition make no sense */
135 RAISE_NULL( mediacontrol_PositionOriginNotSupported,
136 "Only absolute position is valid." );
139 /* We are asked for an AbsolutePosition. */
140 pos = libvlc_input_get_time( p_input, &ex );
142 if( a_key == mediacontrol_MediaTime )
148 if( ! self->p_playlist->p_input )
150 libvlc_input_free( p_input );
151 RAISE_NULL( mediacontrol_InternalException,
154 retval->value = mediacontrol_unit_convert( self->p_playlist->p_input,
155 mediacontrol_MediaTime,
159 libvlc_input_free( p_input );
163 /* Sets the media position */
165 mediacontrol_set_media_position( mediacontrol_Instance *self,
166 const mediacontrol_Position * a_position,
167 mediacontrol_Exception *exception )
169 libvlc_input_t * p_input;
170 libvlc_exception_t ex;
173 libvlc_exception_init( &ex );
174 mediacontrol_exception_init( exception );
176 p_input = libvlc_playlist_get_input( self->p_instance, &ex);
177 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
179 i_pos = mediacontrol_position2microsecond( self->p_playlist->p_input, a_position );
180 libvlc_input_set_time( p_input, i_pos / 1000, &ex );
181 libvlc_input_free( p_input );
182 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
185 /* Starts playing a stream */
187 * Known issues: since moving in the playlist using playlist_Next
188 * or playlist_Prev implies starting to play items, the a_position
189 * argument will be only honored for the 1st item in the list.
191 * XXX:FIXME split moving in the playlist and playing items two
192 * different actions or make playlist_<Next|Prev> accept a time
193 * value to start to play from.
196 mediacontrol_start( mediacontrol_Instance *self,
197 const mediacontrol_Position * a_position,
198 mediacontrol_Exception *exception )
200 playlist_t * p_playlist = self->p_playlist;
202 exception = mediacontrol_exception_init( exception );
205 RAISE( mediacontrol_PlaylistException, "No available playlist" );
209 vlc_mutex_lock( &p_playlist->object_lock );
210 if( p_playlist->i_size )
213 char *psz_from = NULL;
215 psz_from = ( char * )malloc( 20 * sizeof( char ) );
216 if( psz_from && p_playlist->status.p_item )
218 i_from = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
221 snprintf( psz_from, 20, "start-time=%i", i_from );
222 input_ItemAddOption( p_playlist->status.p_item->p_input, psz_from );
226 vlc_mutex_unlock( &p_playlist->object_lock );
227 playlist_Play( p_playlist );
231 RAISE( mediacontrol_PlaylistException, "Empty playlist." );
232 vlc_mutex_unlock( &p_playlist->object_lock );
237 mediacontrol_pause( mediacontrol_Instance *self,
238 const mediacontrol_Position * a_position,
239 mediacontrol_Exception *exception )
241 input_thread_t *p_input = self->p_playlist->p_input;
243 /* FIXME: use the a_position parameter */
244 exception=mediacontrol_exception_init( exception );
245 if( p_input != NULL )
247 var_SetInteger( p_input, "state", PAUSE_S );
251 RAISE( mediacontrol_InternalException, "No input" );
256 mediacontrol_resume( mediacontrol_Instance *self,
257 const mediacontrol_Position * a_position,
258 mediacontrol_Exception *exception )
260 input_thread_t *p_input = self->p_playlist->p_input;
262 /* FIXME: use the a_position parameter */
263 exception=mediacontrol_exception_init( exception );
264 if( p_input != NULL )
266 var_SetInteger( p_input, "state", PAUSE_S );
270 RAISE( mediacontrol_InternalException, "No input" );
275 mediacontrol_stop( mediacontrol_Instance *self,
276 const mediacontrol_Position * a_position,
277 mediacontrol_Exception *exception )
279 /* FIXME: use the a_position parameter */
280 exception=mediacontrol_exception_init( exception );
281 if( !self->p_playlist )
283 RAISE( mediacontrol_PlaylistException, "No playlist" );
286 playlist_Stop( self->p_playlist );
289 /**************************************************************************
290 * Playlist management
291 **************************************************************************/
294 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
295 const char * psz_file,
296 mediacontrol_Exception *exception )
298 libvlc_exception_t ex;
300 mediacontrol_exception_init( exception );
301 libvlc_exception_init( &ex );
303 libvlc_playlist_add( self->p_instance, psz_file, psz_file, &ex );
304 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
308 mediacontrol_playlist_next_item( mediacontrol_Instance *self,
309 mediacontrol_Exception *exception )
311 libvlc_exception_t ex;
313 mediacontrol_exception_init( exception );
314 libvlc_exception_init( &ex );
316 libvlc_playlist_next( self->p_instance, &ex );
317 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
321 mediacontrol_playlist_clear( 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_clear( self->p_instance, &ex );
330 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
333 mediacontrol_PlaylistSeq *
334 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
335 mediacontrol_Exception *exception )
337 mediacontrol_PlaylistSeq *retval = NULL;
339 playlist_t * p_playlist = self->p_playlist;
342 exception=mediacontrol_exception_init( exception );
345 RAISE( mediacontrol_PlaylistException, "No playlist" );
349 vlc_mutex_lock( &p_playlist->object_lock );
350 i_playlist_size = p_playlist->i_size;
352 retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
354 for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
356 retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->p_input->psz_uri );
358 vlc_mutex_unlock( &p_playlist->object_lock );
363 /***************************************************************************
365 ***************************************************************************/
367 mediacontrol_StreamInformation *
368 mediacontrol_get_stream_information( mediacontrol_Instance *self,
369 mediacontrol_PositionKey a_key,
370 mediacontrol_Exception *exception )
372 mediacontrol_StreamInformation *retval = NULL;
373 input_thread_t *p_input = self->p_playlist->p_input;
376 retval = ( mediacontrol_StreamInformation* )
377 malloc( sizeof( mediacontrol_StreamInformation ) );
380 RAISE( mediacontrol_InternalException, "Out of memory" );
386 /* No p_input defined */
387 retval->streamstatus = mediacontrol_UndefinedStatus;
388 retval->url = strdup( "None" );
389 retval->position = 0;
394 switch( var_GetInteger( p_input, "state" ) )
397 retval->streamstatus = mediacontrol_PlayingStatus;
400 retval->streamstatus = mediacontrol_PauseStatus;
403 retval->streamstatus = mediacontrol_InitStatus;
406 retval->streamstatus = mediacontrol_EndStatus;
409 retval->streamstatus = mediacontrol_UndefinedStatus;
413 retval->url = strdup( p_input->input.p_item->psz_uri );
415 /* TIME and LENGTH are in microseconds. We want them in ms */
416 var_Get( p_input, "time", &val);
417 retval->position = val.i_time / 1000;
419 var_Get( p_input, "length", &val);
420 retval->length = val.i_time / 1000;
422 retval->position = mediacontrol_unit_convert( p_input,
423 mediacontrol_MediaTime, a_key,
425 retval->length = mediacontrol_unit_convert( p_input,
426 mediacontrol_MediaTime, a_key,