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>
30 #include <vlc_demux.h>
34 #define HAS_SNAPSHOT 1
40 #include <stdlib.h> /* malloc(), free() */
43 #include <errno.h> /* ENOMEM */
50 #ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
53 #ifdef HAVE_SYS_TYPES_H
54 # include <sys/types.h>
57 #define RAISE( c, m ) exception->code = c; \
58 exception->message = strdup(m);
60 mediacontrol_Instance* mediacontrol_new_from_object( int vlc_object_id,
61 mediacontrol_Exception *exception )
63 mediacontrol_Instance* retval = NULL;
65 vlc_object_t *p_object;
67 p_object = ( vlc_object_t* )vlc_current_object( vlc_object_id );
70 RAISE( mediacontrol_InternalException, "Unable to find vlc object" );
74 p_vlc = vlc_object_find( p_object, VLC_OBJECT_ROOT, FIND_PARENT );
77 RAISE( mediacontrol_InternalException, "Unable to initialize VLC" );
80 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
81 retval->p_vlc = p_vlc;
82 retval->vlc_object_id = p_vlc->i_object_id;
84 /* We can keep references on these, which should not change. Is it true ? */
85 retval->p_playlist = vlc_object_find( p_vlc,
86 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
87 retval->p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_ANYWHERE );
89 if( ! retval->p_playlist || ! retval->p_intf )
91 RAISE( mediacontrol_InternalException, "No available interface" );
98 /**************************************************************************
100 **************************************************************************/
101 mediacontrol_Position*
102 mediacontrol_get_media_position( mediacontrol_Instance *self,
103 const mediacontrol_PositionOrigin an_origin,
104 const mediacontrol_PositionKey a_key,
105 mediacontrol_Exception *exception )
107 mediacontrol_Position* retval = NULL;
109 input_thread_t * p_input = self->p_playlist->p_input;
111 exception = mediacontrol_exception_init( exception );
113 retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
114 retval->origin = an_origin;
119 RAISE( mediacontrol_InternalException, "No input thread." );
123 if( an_origin != mediacontrol_AbsolutePosition )
125 /* Relative or ModuloPosition make no sense */
126 RAISE( mediacontrol_PositionOriginNotSupported, "Only absolute position is valid." );
130 /* We are asked for an AbsolutePosition. */
132 var_Get( p_input, "time", &val );
133 /* FIXME: check val.i_time > 0 */
135 retval->value = mediacontrol_unit_convert( p_input,
136 mediacontrol_MediaTime,
142 /* Sets the media position */
144 mediacontrol_set_media_position( mediacontrol_Instance *self,
145 const mediacontrol_Position * a_position,
146 mediacontrol_Exception *exception )
149 input_thread_t * p_input = self->p_playlist->p_input;
151 exception=mediacontrol_exception_init( exception );
154 RAISE( mediacontrol_InternalException, "No input thread." );
156 else if( !var_GetBool( p_input, "seekable" ) )
158 RAISE( mediacontrol_InvalidPosition, "Stream not seekable" );
162 val.i_time = mediacontrol_position2microsecond( p_input, a_position );
163 var_Set( p_input, "time", val );
167 /* Starts playing a stream */
169 * Known issues: since moving in the playlist using playlist_Next
170 * or playlist_Prev implies starting to play items, the a_position
171 * argument will be only honored for the 1st item in the list.
173 * XXX:FIXME split moving in the playlist and playing items two
174 * different actions or make playlist_<Next|Prev> accept a time
175 * value to start to play from.
178 mediacontrol_start( mediacontrol_Instance *self,
179 const mediacontrol_Position * a_position,
180 mediacontrol_Exception *exception )
182 playlist_t * p_playlist = self->p_playlist;
184 exception = mediacontrol_exception_init( exception );
187 RAISE( mediacontrol_PlaylistException, "No available playlist" );
191 vlc_mutex_lock( &p_playlist->object_lock );
192 if( p_playlist->i_size )
196 char *psz_from = NULL;
198 psz_from = ( char * )malloc( 20 * sizeof( char ) );
201 i_from = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
203 i_index = p_playlist->i_index;
206 /* We know that there is at least 1 element, since i_size != 0 */
211 snprintf( psz_from, 20, "start-time=%i", i_from );
212 playlist_ItemAddOption( p_playlist->pp_items[i_index], psz_from );
216 vlc_mutex_unlock( &p_playlist->object_lock );
217 playlist_Play( p_playlist );
221 RAISE( mediacontrol_PlaylistException, "Empty playlist." );
222 vlc_mutex_unlock( &p_playlist->object_lock );
227 mediacontrol_pause( mediacontrol_Instance *self,
228 const mediacontrol_Position * a_position,
229 mediacontrol_Exception *exception )
231 input_thread_t *p_input = self->p_playlist->p_input;
233 /* FIXME: use the a_position parameter */
234 exception=mediacontrol_exception_init( exception );
235 if( p_input != NULL )
237 var_SetInteger( p_input, "state", PAUSE_S );
241 RAISE( mediacontrol_InternalException, "No input" );
246 mediacontrol_resume( mediacontrol_Instance *self,
247 const mediacontrol_Position * a_position,
248 mediacontrol_Exception *exception )
250 input_thread_t *p_input = self->p_playlist->p_input;
252 /* FIXME: use the a_position parameter */
253 exception=mediacontrol_exception_init( exception );
254 if( p_input != NULL )
256 var_SetInteger( p_input, "state", PAUSE_S );
260 RAISE( mediacontrol_InternalException, "No input" );
265 mediacontrol_stop( mediacontrol_Instance *self,
266 const mediacontrol_Position * a_position,
267 mediacontrol_Exception *exception )
269 /* FIXME: use the a_position parameter */
270 exception=mediacontrol_exception_init( exception );
271 if( !self->p_playlist )
273 RAISE( mediacontrol_PlaylistException, "No playlist" );
276 playlist_Stop( self->p_playlist );
279 /**************************************************************************
280 * Playlist management
281 **************************************************************************/
284 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
285 const char * psz_file,
286 mediacontrol_Exception *exception )
288 exception=mediacontrol_exception_init( exception );
289 if( !self->p_playlist )
291 RAISE( mediacontrol_InternalException, "No playlist" );
294 playlist_Add( self->p_playlist, psz_file, psz_file , PLAYLIST_INSERT,
299 mediacontrol_playlist_next_item( mediacontrol_Instance *self,
300 mediacontrol_Exception *exception )
302 exception=mediacontrol_exception_init( exception );
303 if ( !self->p_playlist )
305 RAISE( mediacontrol_InternalException, "No playlist" );
308 playlist_Next( self->p_playlist );
312 mediacontrol_playlist_clear( mediacontrol_Instance *self,
313 mediacontrol_Exception *exception )
315 exception=mediacontrol_exception_init( exception );
316 if( !self->p_playlist )
318 RAISE( mediacontrol_PlaylistException, "No playlist" );
321 playlist_Clear( self->p_playlist );
324 mediacontrol_PlaylistSeq *
325 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
326 mediacontrol_Exception *exception )
328 mediacontrol_PlaylistSeq *retval = NULL;
330 playlist_t * p_playlist = self->p_playlist;
333 exception=mediacontrol_exception_init( exception );
336 RAISE( mediacontrol_PlaylistException, "No playlist" );
340 vlc_mutex_lock( &p_playlist->object_lock );
341 i_playlist_size = p_playlist->i_size;
343 retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
345 for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
347 retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->input.psz_uri );
349 vlc_mutex_unlock( &p_playlist->object_lock );
354 /***************************************************************************
356 ***************************************************************************/
358 mediacontrol_StreamInformation *
359 mediacontrol_get_stream_information( mediacontrol_Instance *self,
360 mediacontrol_PositionKey a_key,
361 mediacontrol_Exception *exception )
363 mediacontrol_StreamInformation *retval = NULL;
364 input_thread_t *p_input = self->p_playlist->p_input;
367 retval = ( mediacontrol_StreamInformation* )malloc( sizeof( mediacontrol_StreamInformation ) );
370 RAISE( mediacontrol_InternalException, "Out of memory" );
376 /* No p_input defined */
377 retval->streamstatus = mediacontrol_UndefinedStatus;
378 retval->url = strdup( "None" );
379 retval->position = 0;
384 switch( var_GetInteger( p_input, "state" ) )
387 retval->streamstatus = mediacontrol_PlayingStatus;
390 retval->streamstatus = mediacontrol_PauseStatus;
393 retval->streamstatus = mediacontrol_InitStatus;
396 retval->streamstatus = mediacontrol_EndStatus;
399 retval->streamstatus = mediacontrol_UndefinedStatus;
403 retval->url = strdup( p_input->input.p_item->psz_uri );
405 /* TIME and LENGTH are in microseconds. We want them in ms */
406 var_Get( p_input, "time", &val);
407 retval->position = val.i_time / 1000;
409 var_Get( p_input, "length", &val);
410 retval->length = val.i_time / 1000;
412 retval->position = mediacontrol_unit_convert( p_input,
413 mediacontrol_MediaTime, a_key,
415 retval->length = mediacontrol_unit_convert( p_input,
416 mediacontrol_MediaTime, a_key,