1 /*****************************************************************************
2 * core.c: Core functions : init, playlist, stream management
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
5 * $Id: vlc.c 10786 2005-04-23 23:19:17Z zorglub $
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>
29 #include <vlc_demux.h>
33 #define HAS_SNAPSHOT 1
39 #include <stdlib.h> /* malloc(), free() */
42 #include <errno.h> /* ENOMEM */
49 #ifdef HAVE_SYS_TIME_H
50 # include <sys/time.h>
52 #ifdef HAVE_SYS_TYPES_H
53 # include <sys/types.h>
56 #define RAISE( c, m ) exception->code = c; \
57 exception->message = strdup(m);
60 mediacontrol_Instance* mediacontrol_new_from_object( vlc_object_t* p_object,
61 mediacontrol_Exception *exception )
63 mediacontrol_Instance* retval;
66 p_vlc = vlc_object_find( p_object, VLC_OBJECT_ROOT, FIND_PARENT );
69 RAISE( mediacontrol_InternalException, "Unable to initialize VLC" );
72 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
73 retval->p_vlc = p_vlc;
74 retval->vlc_object_id = p_vlc->i_object_id;
76 /* We can keep references on these, which should not change. Is it true ? */
77 retval->p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
78 retval->p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_ANYWHERE );
80 if( ! retval->p_playlist || ! retval->p_intf )
82 RAISE( mediacontrol_InternalException, "No available interface" );
89 /**************************************************************************
91 **************************************************************************/
92 mediacontrol_Position*
93 mediacontrol_get_media_position( mediacontrol_Instance *self,
94 const mediacontrol_PositionOrigin an_origin,
95 const mediacontrol_PositionKey a_key,
96 mediacontrol_Exception *exception )
98 mediacontrol_Position* retval;
100 input_thread_t * p_input = self->p_playlist->p_input;
102 exception = mediacontrol_exception_init( exception );
104 retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
105 retval->origin = an_origin;
111 RAISE( mediacontrol_InternalException, "No input thread." );
118 if( an_origin == mediacontrol_RelativePosition
119 || an_origin == mediacontrol_ModuloPosition )
121 /* Relative or ModuloPosition make no sense */
126 /* We are asked for an AbsolutePosition. */
128 var_Get( p_input, "time", &val );
129 /* FIXME: check val.i_time > 0 */
131 retval->value = mediacontrol_unit_convert( p_input,
132 mediacontrol_MediaTime,
138 /* Sets the media position */
140 mediacontrol_set_media_position( mediacontrol_Instance *self,
141 const mediacontrol_Position * a_position,
142 mediacontrol_Exception *exception )
145 input_thread_t * p_input = self->p_playlist->p_input;
147 exception=mediacontrol_exception_init( exception );
150 RAISE( mediacontrol_InternalException, "No input thread." );
154 if( !var_GetBool( p_input, "seekable" ) )
156 RAISE( mediacontrol_InvalidPosition, "Stream not seekable" );
160 val.i_time = mediacontrol_position2microsecond( p_input, a_position );
161 var_Set( p_input, "time", val );
165 /* Starts playing a stream */
167 mediacontrol_start( mediacontrol_Instance *self,
168 const mediacontrol_Position * a_position,
169 mediacontrol_Exception *exception )
171 playlist_t * p_playlist = self->p_playlist;
173 exception = mediacontrol_exception_init( exception );
176 RAISE( mediacontrol_PlaylistException, "No available playlist" );
180 vlc_mutex_lock( &p_playlist->object_lock );
181 if( p_playlist->i_size )
185 vlc_mutex_unlock( &p_playlist->object_lock );
188 val.i_int = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
189 var_Set( p_playlist, "start-time", val );
191 playlist_Play( p_playlist );
195 RAISE( mediacontrol_PlaylistException, "Empty playlist." );
196 vlc_mutex_unlock( &p_playlist->object_lock );
204 mediacontrol_pause( mediacontrol_Instance *self,
205 const mediacontrol_Position * a_position,
206 mediacontrol_Exception *exception )
208 input_thread_t *p_input = self->p_playlist->p_input;;
210 /* FIXME: use the a_position parameter */
211 exception=mediacontrol_exception_init( exception );
212 if( p_input != NULL )
214 var_SetInteger( p_input, "state", PAUSE_S );
218 RAISE( mediacontrol_InternalException, "No input" );
225 mediacontrol_resume( mediacontrol_Instance *self,
226 const mediacontrol_Position * a_position,
227 mediacontrol_Exception *exception )
229 input_thread_t *p_input = self->p_playlist->p_input;
231 /* FIXME: use the a_position parameter */
232 exception=mediacontrol_exception_init( exception );
233 if( p_input != NULL )
235 var_SetInteger( p_input, "state", PAUSE_S );
239 RAISE( mediacontrol_InternalException, "No input" );
244 mediacontrol_stop( mediacontrol_Instance *self,
245 const mediacontrol_Position * a_position,
246 mediacontrol_Exception *exception )
248 /* FIXME: use the a_position parameter */
249 exception=mediacontrol_exception_init( exception );
250 if( !self->p_playlist )
252 RAISE( mediacontrol_PlaylistException, "No playlist" );
256 playlist_Stop( self->p_playlist );
259 /**************************************************************************
260 * Playlist management
261 **************************************************************************/
264 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
265 const char * psz_file,
266 mediacontrol_Exception *exception )
268 exception=mediacontrol_exception_init( exception );
269 if( !self->p_playlist )
271 RAISE( mediacontrol_InternalException, "No playlist" );
275 playlist_Add( self->p_playlist, psz_file, psz_file , PLAYLIST_REPLACE, 0 );
279 mediacontrol_playlist_clear( mediacontrol_Instance *self,
280 mediacontrol_Exception *exception )
282 exception=mediacontrol_exception_init( exception );
283 if( !self->p_playlist )
285 RAISE( mediacontrol_PlaylistException, "No playlist" );
289 playlist_Clear( self->p_playlist );
294 mediacontrol_PlaylistSeq *
295 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
296 mediacontrol_Exception *exception )
298 mediacontrol_PlaylistSeq *retval;
300 playlist_t * p_playlist = self->p_playlist;
303 exception=mediacontrol_exception_init( exception );
306 RAISE( mediacontrol_PlaylistException, "No playlist" );
310 vlc_mutex_lock( &p_playlist->object_lock );
311 i_playlist_size = p_playlist->i_size;
313 retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
315 for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
317 retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->input.psz_uri );
319 vlc_mutex_unlock( &p_playlist->object_lock );
324 /***************************************************************************
326 ***************************************************************************/
328 mediacontrol_StreamInformation *
329 mediacontrol_get_stream_information( mediacontrol_Instance *self,
330 mediacontrol_PositionKey a_key,
331 mediacontrol_Exception *exception )
333 mediacontrol_StreamInformation *retval;
334 input_thread_t *p_input = self->p_playlist->p_input;
337 retval = ( mediacontrol_StreamInformation* )malloc( sizeof( mediacontrol_StreamInformation ) );
340 RAISE( mediacontrol_InternalException, "Out of memory" );
346 /* No p_input defined */
347 retval->streamstatus = mediacontrol_UndefinedStatus;
348 retval->url = strdup( "None" );
349 retval->position = 0;
354 switch( var_GetInteger( p_input, "state" ) )
357 retval->streamstatus = mediacontrol_PlayingStatus;
360 retval->streamstatus = mediacontrol_PauseStatus;
363 retval->streamstatus = mediacontrol_InitStatus;
366 retval->streamstatus = mediacontrol_EndStatus;
369 retval->streamstatus = mediacontrol_UndefinedStatus;
373 retval->url = strdup( p_input->input.p_item->psz_uri );
375 /* TIME and LENGTH are in microseconds. We want them in ms */
376 var_Get( p_input, "time", &val);
377 retval->position = val.i_time / 1000;
379 var_Get( p_input, "length", &val);
380 retval->length = val.i_time / 1000;
382 retval->position = mediacontrol_unit_convert( p_input,
383 mediacontrol_MediaTime, a_key,
385 retval->length = mediacontrol_unit_convert( p_input,
386 mediacontrol_MediaTime, a_key,