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 *****************************************************************************/
29 #include <vlc/mediacontrol.h>
31 #include <vlc/libvlc.h>
32 #include <vlc_interface.h>
33 #include <vlc_playlist.h>
37 #include <vlc_input.h>
39 #include "mediacontrol_internal.h"
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 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, (const char**)argv, &ex );
71 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
72 retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
77 mediacontrol_exit( mediacontrol_Instance *self )
79 libvlc_release( self->p_instance );
83 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
85 return self->p_instance;
88 mediacontrol_Instance *
89 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
90 mediacontrol_Exception *exception )
92 mediacontrol_Instance* retval;
94 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
97 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
99 retval->p_instance = p_instance;
100 retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
104 /**************************************************************************
105 * Playback management
106 **************************************************************************/
107 mediacontrol_Position*
108 mediacontrol_get_media_position( mediacontrol_Instance *self,
109 const mediacontrol_PositionOrigin an_origin,
110 const mediacontrol_PositionKey a_key,
111 mediacontrol_Exception *exception )
113 mediacontrol_Position* retval = NULL;
114 libvlc_exception_t ex;
116 libvlc_media_instance_t * p_mi;
118 mediacontrol_exception_init( exception );
119 libvlc_exception_init( &ex );
121 retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
122 retval->origin = an_origin;
125 p_mi = libvlc_playlist_get_media_instance( self->p_instance, &ex);
126 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
128 if( an_origin != mediacontrol_AbsolutePosition )
130 libvlc_media_instance_release( p_mi );
131 /* Relative or ModuloPosition make no sense */
132 RAISE_NULL( mediacontrol_PositionOriginNotSupported,
133 "Only absolute position is valid." );
136 /* We are asked for an AbsolutePosition. */
137 pos = libvlc_media_instance_get_time( p_mi, &ex );
139 if( a_key == mediacontrol_MediaTime )
145 if( ! self->p_playlist->p_input )
147 libvlc_media_instance_release( p_mi );
148 RAISE_NULL( mediacontrol_InternalException,
151 retval->value = private_mediacontrol_unit_convert( self->p_playlist->p_input,
152 mediacontrol_MediaTime,
156 libvlc_media_instance_release( p_mi );
160 /* Sets the media position */
162 mediacontrol_set_media_position( mediacontrol_Instance *self,
163 const mediacontrol_Position * a_position,
164 mediacontrol_Exception *exception )
166 libvlc_media_instance_t * p_mi;
167 libvlc_exception_t ex;
170 libvlc_exception_init( &ex );
171 mediacontrol_exception_init( exception );
173 p_mi = libvlc_playlist_get_media_instance( self->p_instance, &ex);
174 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
176 i_pos = private_mediacontrol_position2microsecond( self->p_playlist->p_input, a_position );
177 libvlc_media_instance_set_time( p_mi, i_pos / 1000, &ex );
178 libvlc_media_instance_release( p_mi );
179 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
182 /* Starts playing a stream */
184 * Known issues: since moving in the playlist using playlist_Next
185 * or playlist_Prev implies starting to play items, the a_position
186 * argument will be only honored for the 1st item in the list.
188 * XXX:FIXME split moving in the playlist and playing items two
189 * different actions or make playlist_<Next|Prev> accept a time
190 * value to start to play from.
193 mediacontrol_start( mediacontrol_Instance *self,
194 const mediacontrol_Position * a_position,
195 mediacontrol_Exception *exception )
197 playlist_t * p_playlist = self->p_playlist;
199 mediacontrol_exception_init( exception );
202 RAISE( mediacontrol_PlaylistException, "No available playlist" );
206 vlc_mutex_lock( &p_playlist->object_lock );
207 if( p_playlist->items.i_size )
210 char *psz_from = NULL;
212 psz_from = ( char * )malloc( 20 * sizeof( char ) );
213 if( psz_from && p_playlist->status.p_item )
215 i_from = private_mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
218 snprintf( psz_from, 20, "start-time=%i", i_from );
219 input_ItemAddOption( p_playlist->status.p_item->p_input, psz_from );
223 vlc_mutex_unlock( &p_playlist->object_lock );
224 playlist_Play( p_playlist );
228 RAISE( mediacontrol_PlaylistException, "Empty playlist." );
229 vlc_mutex_unlock( &p_playlist->object_lock );
234 mediacontrol_pause( mediacontrol_Instance *self,
235 const mediacontrol_Position * a_position,
236 mediacontrol_Exception *exception )
238 input_thread_t *p_input = self->p_playlist->p_input;
240 /* FIXME: use the a_position parameter */
241 mediacontrol_exception_init( exception );
242 if( p_input != NULL )
244 var_SetInteger( p_input, "state", PAUSE_S );
248 RAISE( mediacontrol_InternalException, "No input" );
253 mediacontrol_resume( mediacontrol_Instance *self,
254 const mediacontrol_Position * a_position,
255 mediacontrol_Exception *exception )
257 input_thread_t *p_input = self->p_playlist->p_input;
259 /* FIXME: use the a_position parameter */
260 mediacontrol_exception_init( exception );
261 if( p_input != NULL )
263 var_SetInteger( p_input, "state", PAUSE_S );
267 RAISE( mediacontrol_InternalException, "No input" );
272 mediacontrol_stop( mediacontrol_Instance *self,
273 const mediacontrol_Position * a_position,
274 mediacontrol_Exception *exception )
276 /* FIXME: use the a_position parameter */
277 mediacontrol_exception_init( exception );
278 if( !self->p_playlist )
280 RAISE( mediacontrol_PlaylistException, "No playlist" );
283 playlist_Stop( self->p_playlist );
286 /**************************************************************************
287 * Playlist management
288 **************************************************************************/
291 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
292 const char * psz_file,
293 mediacontrol_Exception *exception )
295 libvlc_exception_t ex;
297 mediacontrol_exception_init( exception );
298 libvlc_exception_init( &ex );
300 libvlc_playlist_add( self->p_instance, psz_file, psz_file, &ex );
301 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
305 mediacontrol_playlist_next_item( mediacontrol_Instance *self,
306 mediacontrol_Exception *exception )
308 libvlc_exception_t ex;
310 mediacontrol_exception_init( exception );
311 libvlc_exception_init( &ex );
313 libvlc_playlist_next( self->p_instance, &ex );
314 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
318 mediacontrol_playlist_clear( mediacontrol_Instance *self,
319 mediacontrol_Exception *exception )
321 libvlc_exception_t ex;
323 mediacontrol_exception_init( exception );
324 libvlc_exception_init( &ex );
326 libvlc_playlist_clear( self->p_instance, &ex );
327 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
330 mediacontrol_PlaylistSeq *
331 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
332 mediacontrol_Exception *exception )
334 mediacontrol_PlaylistSeq *retval = NULL;
336 playlist_t * p_playlist = self->p_playlist;
337 playlist_item_t *p_root;
340 mediacontrol_exception_init( exception );
343 RAISE( mediacontrol_PlaylistException, "No playlist" );
347 vlc_mutex_lock( &p_playlist->object_lock );
349 p_root = p_playlist->p_root_onelevel->pp_children[0];
350 i_playlist_size = p_root->i_children;
351 retval = private_mediacontrol_PlaylistSeq__alloc( i_playlist_size );
353 for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
355 retval->data[i_index] = strdup( p_root->pp_children[i_index]->p_input->psz_name );
357 vlc_mutex_unlock( &p_playlist->object_lock );
362 /***************************************************************************
364 ***************************************************************************/
366 mediacontrol_StreamInformation *
367 mediacontrol_get_stream_information( mediacontrol_Instance *self,
368 mediacontrol_PositionKey a_key,
369 mediacontrol_Exception *exception )
371 mediacontrol_StreamInformation *retval = NULL;
372 input_thread_t *p_input = self->p_playlist->p_input;
375 retval = ( mediacontrol_StreamInformation* )
376 malloc( sizeof( mediacontrol_StreamInformation ) );
379 RAISE( mediacontrol_InternalException, "Out of memory" );
385 /* No p_input defined */
386 retval->streamstatus = mediacontrol_UndefinedStatus;
387 retval->url = strdup( "None" );
388 retval->position = 0;
393 switch( var_GetInteger( p_input, "state" ) )
396 retval->streamstatus = mediacontrol_PlayingStatus;
399 retval->streamstatus = mediacontrol_PauseStatus;
402 retval->streamstatus = mediacontrol_InitStatus;
405 retval->streamstatus = mediacontrol_EndStatus;
408 retval->streamstatus = mediacontrol_UndefinedStatus;
412 retval->url = input_item_GetURI( input_GetItem( p_input ) );
414 /* TIME and LENGTH are in microseconds. We want them in ms */
415 var_Get( p_input, "time", &val);
416 retval->position = val.i_time / 1000;
418 var_Get( p_input, "length", &val);
419 retval->length = val.i_time / 1000;
421 retval->position = private_mediacontrol_unit_convert( p_input,
422 mediacontrol_MediaTime, a_key,
424 retval->length = private_mediacontrol_unit_convert( p_input,
425 mediacontrol_MediaTime, a_key,