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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #include <vlc_control.h>
25 #include <vlc/control.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 vlc_t * vlc_current_object( int );
62 mediacontrol_Instance* mediacontrol_new_from_object( int vlc_object_id,
63 mediacontrol_Exception *exception )
65 mediacontrol_Instance* retval;
67 vlc_object_t *p_object;
69 p_object = ( vlc_object_t* )vlc_current_object( vlc_object_id );
72 RAISE( mediacontrol_InternalException, "Unable to find vlc object" );
76 p_vlc = vlc_object_find( p_object, VLC_OBJECT_ROOT, FIND_PARENT );
79 RAISE( mediacontrol_InternalException, "Unable to initialize VLC" );
82 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
83 retval->p_vlc = p_vlc;
84 retval->vlc_object_id = p_vlc->i_object_id;
86 /* We can keep references on these, which should not change. Is it true ? */
87 retval->p_playlist = vlc_object_find( p_vlc,
88 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
89 retval->p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_ANYWHERE );
91 if( ! retval->p_playlist || ! retval->p_intf )
93 RAISE( mediacontrol_InternalException, "No available interface" );
100 /**************************************************************************
101 * Playback management
102 **************************************************************************/
103 mediacontrol_Position*
104 mediacontrol_get_media_position( mediacontrol_Instance *self,
105 const mediacontrol_PositionOrigin an_origin,
106 const mediacontrol_PositionKey a_key,
107 mediacontrol_Exception *exception )
109 mediacontrol_Position* retval;
111 input_thread_t * p_input = self->p_playlist->p_input;
113 exception = mediacontrol_exception_init( exception );
115 retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
116 retval->origin = an_origin;
121 RAISE( mediacontrol_InternalException, "No input thread." );
125 if( an_origin != mediacontrol_AbsolutePosition )
127 /* Relative or ModuloPosition make no sense */
128 RAISE( mediacontrol_PositionOriginNotSupported, "Only absolute position is valid." );
132 /* We are asked for an AbsolutePosition. */
134 var_Get( p_input, "time", &val );
135 /* FIXME: check val.i_time > 0 */
137 retval->value = mediacontrol_unit_convert( p_input,
138 mediacontrol_MediaTime,
144 /* Sets the media position */
146 mediacontrol_set_media_position( mediacontrol_Instance *self,
147 const mediacontrol_Position * a_position,
148 mediacontrol_Exception *exception )
151 input_thread_t * p_input = self->p_playlist->p_input;
153 exception=mediacontrol_exception_init( exception );
156 RAISE( mediacontrol_InternalException, "No input thread." );
160 if( !var_GetBool( p_input, "seekable" ) )
162 RAISE( mediacontrol_InvalidPosition, "Stream not seekable" );
166 val.i_time = mediacontrol_position2microsecond( p_input, a_position );
167 var_Set( p_input, "time", val );
171 /* Starts playing a stream */
173 * Known issues: since moving in the playlist using playlist_Next
174 * or playlist_Prev implies starting to play items, the a_position
175 * argument will be only honored for the 1st item in the list.
177 * XXX:FIXME split moving in the playlist and playing items two
178 * different actions or make playlist_<Next|Prev> accept a time
179 * value to start to play from.
182 mediacontrol_start( mediacontrol_Instance *self,
183 const mediacontrol_Position * a_position,
184 mediacontrol_Exception *exception )
186 playlist_t * p_playlist = self->p_playlist;
188 exception = mediacontrol_exception_init( exception );
191 RAISE( mediacontrol_PlaylistException, "No available playlist" );
195 vlc_mutex_lock( &p_playlist->object_lock );
196 if( p_playlist->i_size )
200 char * psz_from = ( char * )malloc( 20 * sizeof( char ) );
202 i_from = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
204 i_index = p_playlist->i_index;
207 /* We know that there is at least 1 element, since i_size != 0 */
212 snprintf( psz_from, 20, "start-time=%i", i_from );
213 playlist_ItemAddOption( p_playlist->pp_items[i_index], psz_from );
216 vlc_mutex_unlock( &p_playlist->object_lock );
218 playlist_Play( p_playlist );
222 RAISE( mediacontrol_PlaylistException, "Empty playlist." );
223 vlc_mutex_unlock( &p_playlist->object_lock );
230 mediacontrol_pause( mediacontrol_Instance *self,
231 const mediacontrol_Position * a_position,
232 mediacontrol_Exception *exception )
234 input_thread_t *p_input = self->p_playlist->p_input;
236 /* FIXME: use the a_position parameter */
237 exception=mediacontrol_exception_init( exception );
238 if( p_input != NULL )
240 var_SetInteger( p_input, "state", PAUSE_S );
244 RAISE( mediacontrol_InternalException, "No input" );
251 mediacontrol_resume( mediacontrol_Instance *self,
252 const mediacontrol_Position * a_position,
253 mediacontrol_Exception *exception )
255 input_thread_t *p_input = self->p_playlist->p_input;
257 /* FIXME: use the a_position parameter */
258 exception=mediacontrol_exception_init( exception );
259 if( p_input != NULL )
261 var_SetInteger( p_input, "state", PAUSE_S );
265 RAISE( mediacontrol_InternalException, "No input" );
270 mediacontrol_stop( mediacontrol_Instance *self,
271 const mediacontrol_Position * a_position,
272 mediacontrol_Exception *exception )
274 /* FIXME: use the a_position parameter */
275 exception=mediacontrol_exception_init( exception );
276 if( !self->p_playlist )
278 RAISE( mediacontrol_PlaylistException, "No playlist" );
282 playlist_Stop( self->p_playlist );
285 /**************************************************************************
286 * Playlist management
287 **************************************************************************/
290 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
291 const char * psz_file,
292 mediacontrol_Exception *exception )
294 exception=mediacontrol_exception_init( exception );
295 if( !self->p_playlist )
297 RAISE( mediacontrol_InternalException, "No playlist" );
301 playlist_Add( self->p_playlist, psz_file, psz_file , PLAYLIST_INSERT,
306 mediacontrol_playlist_next_item( mediacontrol_Instance *self,
307 mediacontrol_Exception *exception )
309 exception=mediacontrol_exception_init( exception );
310 if ( !self->p_playlist )
312 RAISE( mediacontrol_InternalException, "No playlist" );
316 playlist_Next( self->p_playlist );
322 mediacontrol_playlist_clear( mediacontrol_Instance *self,
323 mediacontrol_Exception *exception )
325 exception=mediacontrol_exception_init( exception );
326 if( !self->p_playlist )
328 RAISE( mediacontrol_PlaylistException, "No playlist" );
332 playlist_Clear( self->p_playlist );
337 mediacontrol_PlaylistSeq *
338 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
339 mediacontrol_Exception *exception )
341 mediacontrol_PlaylistSeq *retval;
343 playlist_t * p_playlist = self->p_playlist;
346 exception=mediacontrol_exception_init( exception );
349 RAISE( mediacontrol_PlaylistException, "No playlist" );
353 vlc_mutex_lock( &p_playlist->object_lock );
354 i_playlist_size = p_playlist->i_size;
356 retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
358 for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
360 retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->input.psz_uri );
362 vlc_mutex_unlock( &p_playlist->object_lock );
367 /***************************************************************************
369 ***************************************************************************/
371 mediacontrol_StreamInformation *
372 mediacontrol_get_stream_information( mediacontrol_Instance *self,
373 mediacontrol_PositionKey a_key,
374 mediacontrol_Exception *exception )
376 mediacontrol_StreamInformation *retval;
377 input_thread_t *p_input = self->p_playlist->p_input;
380 retval = ( mediacontrol_StreamInformation* )malloc( sizeof( mediacontrol_StreamInformation ) );
383 RAISE( mediacontrol_InternalException, "Out of memory" );
389 /* No p_input defined */
390 retval->streamstatus = mediacontrol_UndefinedStatus;
391 retval->url = strdup( "None" );
392 retval->position = 0;
397 switch( var_GetInteger( p_input, "state" ) )
400 retval->streamstatus = mediacontrol_PlayingStatus;
403 retval->streamstatus = mediacontrol_PauseStatus;
406 retval->streamstatus = mediacontrol_InitStatus;
409 retval->streamstatus = mediacontrol_EndStatus;
412 retval->streamstatus = mediacontrol_UndefinedStatus;
416 retval->url = strdup( p_input->input.p_item->psz_uri );
418 /* TIME and LENGTH are in microseconds. We want them in ms */
419 var_Get( p_input, "time", &val);
420 retval->position = val.i_time / 1000;
422 var_Get( p_input, "length", &val);
423 retval->length = val.i_time / 1000;
425 retval->position = mediacontrol_unit_convert( p_input,
426 mediacontrol_MediaTime, a_key,
428 retval->length = mediacontrol_unit_convert( p_input,
429 mediacontrol_MediaTime, a_key,