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,
78 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
79 retval->p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_ANYWHERE );
81 if( ! retval->p_playlist || ! retval->p_intf )
83 RAISE( mediacontrol_InternalException, "No available interface" );
90 /**************************************************************************
92 **************************************************************************/
93 mediacontrol_Position*
94 mediacontrol_get_media_position( mediacontrol_Instance *self,
95 const mediacontrol_PositionOrigin an_origin,
96 const mediacontrol_PositionKey a_key,
97 mediacontrol_Exception *exception )
99 mediacontrol_Position* retval;
101 input_thread_t * p_input = self->p_playlist->p_input;
103 exception = mediacontrol_exception_init( exception );
105 retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
106 retval->origin = an_origin;
112 RAISE( mediacontrol_InternalException, "No input thread." );
119 if( an_origin == mediacontrol_RelativePosition
120 || an_origin == mediacontrol_ModuloPosition )
122 /* Relative or ModuloPosition make no sense */
127 /* We are asked for an AbsolutePosition. */
129 var_Get( p_input, "time", &val );
130 /* FIXME: check val.i_time > 0 */
132 retval->value = mediacontrol_unit_convert( p_input,
133 mediacontrol_MediaTime,
139 /* Sets the media position */
141 mediacontrol_set_media_position( mediacontrol_Instance *self,
142 const mediacontrol_Position * a_position,
143 mediacontrol_Exception *exception )
146 input_thread_t * p_input = self->p_playlist->p_input;
148 exception=mediacontrol_exception_init( exception );
151 RAISE( mediacontrol_InternalException, "No input thread." );
155 if( !var_GetBool( p_input, "seekable" ) )
157 RAISE( mediacontrol_InvalidPosition, "Stream not seekable" );
161 val.i_time = mediacontrol_position2microsecond( p_input, a_position );
162 var_Set( p_input, "time", val );
166 /* Starts playing a stream */
168 mediacontrol_start( mediacontrol_Instance *self,
169 const mediacontrol_Position * a_position,
170 mediacontrol_Exception *exception )
172 playlist_t * p_playlist = self->p_playlist;
174 exception = mediacontrol_exception_init( exception );
177 RAISE( mediacontrol_PlaylistException, "No available playlist" );
181 vlc_mutex_lock( &p_playlist->object_lock );
182 if( p_playlist->i_size )
186 vlc_mutex_unlock( &p_playlist->object_lock );
189 val.i_int = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
190 var_Set( p_playlist, "start-time", val );
192 playlist_Play( p_playlist );
196 RAISE( mediacontrol_PlaylistException, "Empty playlist." );
197 vlc_mutex_unlock( &p_playlist->object_lock );
205 mediacontrol_pause( mediacontrol_Instance *self,
206 const mediacontrol_Position * a_position,
207 mediacontrol_Exception *exception )
209 input_thread_t *p_input = self->p_playlist->p_input;;
211 /* FIXME: use the a_position parameter */
212 exception=mediacontrol_exception_init( exception );
213 if( p_input != NULL )
215 var_SetInteger( p_input, "state", PAUSE_S );
219 RAISE( mediacontrol_InternalException, "No input" );
226 mediacontrol_resume( mediacontrol_Instance *self,
227 const mediacontrol_Position * a_position,
228 mediacontrol_Exception *exception )
230 input_thread_t *p_input = self->p_playlist->p_input;
232 /* FIXME: use the a_position parameter */
233 exception=mediacontrol_exception_init( exception );
234 if( p_input != NULL )
236 var_SetInteger( p_input, "state", PAUSE_S );
240 RAISE( mediacontrol_InternalException, "No input" );
245 mediacontrol_stop( mediacontrol_Instance *self,
246 const mediacontrol_Position * a_position,
247 mediacontrol_Exception *exception )
249 /* FIXME: use the a_position parameter */
250 exception=mediacontrol_exception_init( exception );
251 if( !self->p_playlist )
253 RAISE( mediacontrol_PlaylistException, "No playlist" );
257 playlist_Stop( self->p_playlist );
260 /**************************************************************************
261 * Playlist management
262 **************************************************************************/
265 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
266 const char * psz_file,
267 mediacontrol_Exception *exception )
269 exception=mediacontrol_exception_init( exception );
270 if( !self->p_playlist )
272 RAISE( mediacontrol_InternalException, "No playlist" );
276 playlist_Add( self->p_playlist, psz_file, psz_file , PLAYLIST_INSERT,
281 mediacontrol_playlist_clear( mediacontrol_Instance *self,
282 mediacontrol_Exception *exception )
284 exception=mediacontrol_exception_init( exception );
285 if( !self->p_playlist )
287 RAISE( mediacontrol_PlaylistException, "No playlist" );
291 playlist_Clear( self->p_playlist );
296 mediacontrol_PlaylistSeq *
297 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
298 mediacontrol_Exception *exception )
300 mediacontrol_PlaylistSeq *retval;
302 playlist_t * p_playlist = self->p_playlist;
305 exception=mediacontrol_exception_init( exception );
308 RAISE( mediacontrol_PlaylistException, "No playlist" );
312 vlc_mutex_lock( &p_playlist->object_lock );
313 i_playlist_size = p_playlist->i_size;
315 retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
317 for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
319 retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->input.psz_uri );
321 vlc_mutex_unlock( &p_playlist->object_lock );
326 /***************************************************************************
328 ***************************************************************************/
330 mediacontrol_StreamInformation *
331 mediacontrol_get_stream_information( mediacontrol_Instance *self,
332 mediacontrol_PositionKey a_key,
333 mediacontrol_Exception *exception )
335 mediacontrol_StreamInformation *retval;
336 input_thread_t *p_input = self->p_playlist->p_input;
339 retval = ( mediacontrol_StreamInformation* )malloc( sizeof( mediacontrol_StreamInformation ) );
342 RAISE( mediacontrol_InternalException, "Out of memory" );
348 /* No p_input defined */
349 retval->streamstatus = mediacontrol_UndefinedStatus;
350 retval->url = strdup( "None" );
351 retval->position = 0;
356 switch( var_GetInteger( p_input, "state" ) )
359 retval->streamstatus = mediacontrol_PlayingStatus;
362 retval->streamstatus = mediacontrol_PauseStatus;
365 retval->streamstatus = mediacontrol_InitStatus;
368 retval->streamstatus = mediacontrol_EndStatus;
371 retval->streamstatus = mediacontrol_UndefinedStatus;
375 retval->url = strdup( p_input->input.p_item->psz_uri );
377 /* TIME and LENGTH are in microseconds. We want them in ms */
378 var_Get( p_input, "time", &val);
379 retval->position = val.i_time / 1000;
381 var_Get( p_input, "length", &val);
382 retval->length = val.i_time / 1000;
384 retval->position = mediacontrol_unit_convert( p_input,
385 mediacontrol_MediaTime, a_key,
387 retval->length = mediacontrol_unit_convert( p_input,
388 mediacontrol_MediaTime, a_key,