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 *****************************************************************************/
28 #include "mediacontrol_internal.h"
29 #include <vlc/mediacontrol.h>
31 #include <vlc/libvlc.h>
32 #include <vlc_common.h>
33 #include <vlc_interface.h>
34 #include <vlc_playlist.h>
38 #include <vlc_input.h>
41 #include <stdlib.h> /* malloc(), free() */
49 #ifdef HAVE_SYS_TIME_H
50 # include <sys/time.h>
52 #ifdef HAVE_SYS_TYPES_H
53 # include <sys/types.h>
56 mediacontrol_Instance* mediacontrol_new( int argc, char** argv, mediacontrol_Exception *exception )
58 mediacontrol_Instance* retval;
59 libvlc_exception_t ex;
61 libvlc_exception_init( &ex );
62 mediacontrol_exception_init( exception );
64 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
66 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
68 retval->p_instance = libvlc_new( argc, (const char**)argv, &ex );
69 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
70 retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
71 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
76 mediacontrol_exit( mediacontrol_Instance *self )
78 libvlc_release( self->p_instance );
82 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
84 return self->p_instance;
87 libvlc_media_player_t*
88 mediacontrol_get_media_player( mediacontrol_Instance *self )
90 return self->p_media_player;
93 mediacontrol_Instance *
94 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
95 mediacontrol_Exception *exception )
97 mediacontrol_Instance* retval;
98 libvlc_exception_t ex;
100 libvlc_exception_init( &ex );
102 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
105 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
107 retval->p_instance = p_instance;
108 retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
109 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
113 /**************************************************************************
114 * Playback management
115 **************************************************************************/
116 mediacontrol_Position*
117 mediacontrol_get_media_position( mediacontrol_Instance *self,
118 const mediacontrol_PositionOrigin an_origin,
119 const mediacontrol_PositionKey a_key,
120 mediacontrol_Exception *exception )
122 mediacontrol_Position* retval = NULL;
123 libvlc_exception_t ex;
126 mediacontrol_exception_init( exception );
127 libvlc_exception_init( &ex );
129 retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
130 retval->origin = an_origin;
133 if( an_origin != mediacontrol_AbsolutePosition )
136 /* Relative or ModuloPosition make no sense */
137 RAISE_NULL( mediacontrol_PositionOriginNotSupported,
138 "Only absolute position is valid." );
141 /* We are asked for an AbsolutePosition. */
142 pos = libvlc_media_player_get_time( self->p_media_player, &ex );
144 if( a_key == mediacontrol_MediaTime )
150 retval->value = private_mediacontrol_unit_convert( self->p_media_player,
151 mediacontrol_MediaTime,
158 /* Sets the media position */
160 mediacontrol_set_media_position( mediacontrol_Instance *self,
161 const mediacontrol_Position * a_position,
162 mediacontrol_Exception *exception )
164 libvlc_exception_t ex;
167 libvlc_exception_init( &ex );
168 mediacontrol_exception_init( exception );
170 i_pos = private_mediacontrol_position2microsecond( self->p_media_player, a_position );
171 libvlc_media_player_set_time( self->p_media_player, i_pos / 1000, &ex );
172 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
175 /* Starts playing a stream */
177 * Known issues: since moving in the playlist using playlist_Next
178 * or playlist_Prev implies starting to play items, the a_position
179 * argument will be only honored for the 1st item in the list.
181 * XXX:FIXME split moving in the playlist and playing items two
182 * different actions or make playlist_<Next|Prev> accept a time
183 * value to start to play from.
186 mediacontrol_start( mediacontrol_Instance *self,
187 const mediacontrol_Position * a_position,
188 mediacontrol_Exception *exception )
190 libvlc_media_t * p_media;
192 libvlc_exception_t ex;
194 mediacontrol_exception_init( exception );
195 libvlc_exception_init( &ex );
197 p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
198 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
202 /* No media was defined. */
203 RAISE( mediacontrol_PlaylistException, "No defined media." );
207 /* A media was defined. Get its mrl to reuse it, but reset the options
208 (because start-time may have been set on the previous invocation */
209 psz_name = libvlc_media_get_mrl( p_media );
210 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
212 /* Create a new media */
213 p_media = libvlc_media_new( self->p_instance, psz_name, &ex );
214 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
216 if( a_position->value )
219 libvlc_time_t i_from;
221 /* A start position was specified. Add it to media options */
222 psz_from = ( char * )malloc( 20 * sizeof( char ) );
223 i_from = private_mediacontrol_position2microsecond( self->p_media_player, a_position ) / 1000000;
224 snprintf( psz_from, 20, "start-time=%"PRId64, i_from );
225 libvlc_media_add_option( p_media, psz_from );
226 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
229 libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
230 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
232 libvlc_media_player_play( self->p_media_player, &ex );
233 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
238 mediacontrol_pause( mediacontrol_Instance *self,
239 mediacontrol_Exception *exception )
241 libvlc_exception_t ex;
243 mediacontrol_exception_init( exception );
244 libvlc_exception_init( &ex );
245 libvlc_media_player_pause( self->p_media_player, &ex );
246 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
250 mediacontrol_resume( mediacontrol_Instance *self,
251 mediacontrol_Exception *exception )
253 libvlc_exception_t ex;
255 mediacontrol_exception_init( exception );
256 libvlc_exception_init( &ex );
257 libvlc_media_player_pause( self->p_media_player, &ex );
258 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
262 mediacontrol_stop( mediacontrol_Instance *self,
263 mediacontrol_Exception *exception )
265 libvlc_exception_t ex;
267 mediacontrol_exception_init( exception );
268 libvlc_exception_init( &ex );
269 libvlc_media_player_stop( self->p_media_player, &ex );
270 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
273 /**************************************************************************
275 **************************************************************************/
278 mediacontrol_set_mrl( mediacontrol_Instance *self,
279 const char * psz_file,
280 mediacontrol_Exception *exception )
282 libvlc_media_t * p_media;
283 libvlc_exception_t ex;
285 mediacontrol_exception_init( exception );
286 libvlc_exception_init( &ex );
288 p_media = libvlc_media_new( self->p_instance, psz_file, &ex );
289 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
291 libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
292 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
296 mediacontrol_get_mrl( mediacontrol_Instance *self,
297 mediacontrol_Exception *exception )
299 libvlc_media_t * p_media;
300 libvlc_exception_t ex;
302 mediacontrol_exception_init( exception );
303 libvlc_exception_init( &ex );
305 p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
306 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
316 psz_mrl = libvlc_media_get_mrl( p_media );
317 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
322 /***************************************************************************
324 ***************************************************************************/
326 mediacontrol_StreamInformation *
327 mediacontrol_get_stream_information( mediacontrol_Instance *self,
328 mediacontrol_PositionKey a_key,
329 mediacontrol_Exception *exception )
332 mediacontrol_StreamInformation *retval = NULL;
333 libvlc_media_t * p_media;
334 libvlc_exception_t ex;
336 libvlc_exception_init( &ex );
338 retval = ( mediacontrol_StreamInformation* )
339 malloc( sizeof( mediacontrol_StreamInformation ) );
342 RAISE( mediacontrol_InternalException, "Out of memory" );
346 p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
347 if( libvlc_exception_raised( &ex ) )
350 RAISE( mediacontrol_InternalException, libvlc_errmsg( ) );
351 libvlc_exception_clear( &ex );
357 /* No p_media defined */
358 retval->streamstatus = mediacontrol_UndefinedStatus;
359 retval->url = strdup( "" );
360 retval->position = 0;
365 libvlc_state_t state;
367 state = libvlc_media_player_get_state( self->p_media_player, &ex );
368 if( libvlc_exception_raised( &ex ) )
371 RAISE( mediacontrol_InternalException, libvlc_errmsg() );
372 libvlc_exception_clear( &ex );
378 case libvlc_NothingSpecial:
379 retval->streamstatus = mediacontrol_UndefinedStatus;
381 case libvlc_Opening :
382 retval->streamstatus = mediacontrol_InitStatus;
384 case libvlc_Buffering:
385 retval->streamstatus = mediacontrol_BufferingStatus;
388 retval->streamstatus = mediacontrol_PlayingStatus;
391 retval->streamstatus = mediacontrol_PauseStatus;
394 retval->streamstatus = mediacontrol_StopStatus;
397 retval->streamstatus = mediacontrol_EndStatus;
400 retval->streamstatus = mediacontrol_ErrorStatus;
403 retval->streamstatus = mediacontrol_UndefinedStatus;
407 retval->url = libvlc_media_get_mrl( p_media );
409 retval->position = libvlc_media_player_get_time( self->p_media_player, &ex );
410 if( libvlc_exception_raised( &ex ) )
412 libvlc_exception_clear( &ex );
413 retval->position = 0;
416 retval->length = libvlc_media_player_get_length( self->p_media_player, &ex );
417 if( libvlc_exception_raised( &ex ) )
419 libvlc_exception_clear( &ex );