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() */
50 #ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
53 #ifdef HAVE_SYS_TYPES_H
54 # include <sys/types.h>
57 mediacontrol_Instance* mediacontrol_new( int argc, char** argv, mediacontrol_Exception *exception )
59 mediacontrol_Instance* retval;
60 libvlc_exception_t ex;
62 libvlc_exception_init( &ex );
63 mediacontrol_exception_init( exception );
65 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
67 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
69 retval->p_instance = libvlc_new( argc, (const char**)argv, &ex );
70 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
71 retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
72 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
77 mediacontrol_exit( mediacontrol_Instance *self )
79 libvlc_release( self->p_instance );
83 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
85 return self->p_instance;
88 libvlc_media_player_t*
89 mediacontrol_get_media_player( mediacontrol_Instance *self )
91 return self->p_media_player;
94 mediacontrol_Instance *
95 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
96 mediacontrol_Exception *exception )
98 mediacontrol_Instance* retval;
99 libvlc_exception_t ex;
101 libvlc_exception_init( &ex );
103 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
106 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
108 retval->p_instance = p_instance;
109 retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
110 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
114 /**************************************************************************
115 * Playback management
116 **************************************************************************/
117 mediacontrol_Position*
118 mediacontrol_get_media_position( mediacontrol_Instance *self,
119 const mediacontrol_PositionOrigin an_origin,
120 const mediacontrol_PositionKey a_key,
121 mediacontrol_Exception *exception )
123 mediacontrol_Position* retval = NULL;
124 libvlc_exception_t ex;
127 mediacontrol_exception_init( exception );
128 libvlc_exception_init( &ex );
130 retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
131 retval->origin = an_origin;
134 if( an_origin != mediacontrol_AbsolutePosition )
137 /* Relative or ModuloPosition make no sense */
138 RAISE_NULL( mediacontrol_PositionOriginNotSupported,
139 "Only absolute position is valid." );
142 /* We are asked for an AbsolutePosition. */
143 pos = libvlc_media_player_get_time( self->p_media_player, &ex );
145 if( a_key == mediacontrol_MediaTime )
151 retval->value = private_mediacontrol_unit_convert( self->p_media_player,
152 mediacontrol_MediaTime,
159 /* Sets the media position */
161 mediacontrol_set_media_position( mediacontrol_Instance *self,
162 const mediacontrol_Position * a_position,
163 mediacontrol_Exception *exception )
165 libvlc_exception_t ex;
168 libvlc_exception_init( &ex );
169 mediacontrol_exception_init( exception );
171 i_pos = private_mediacontrol_position2microsecond( self->p_media_player, a_position );
172 libvlc_media_player_set_time( self->p_media_player, i_pos / 1000, &ex );
173 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
176 /* Starts playing a stream */
178 * Known issues: since moving in the playlist using playlist_Next
179 * or playlist_Prev implies starting to play items, the a_position
180 * argument will be only honored for the 1st item in the list.
182 * XXX:FIXME split moving in the playlist and playing items two
183 * different actions or make playlist_<Next|Prev> accept a time
184 * value to start to play from.
187 mediacontrol_start( mediacontrol_Instance *self,
188 const mediacontrol_Position * a_position,
189 mediacontrol_Exception *exception )
191 libvlc_media_t * p_media;
193 libvlc_exception_t ex;
195 mediacontrol_exception_init( exception );
196 libvlc_exception_init( &ex );
198 p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
199 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
203 /* No media was defined. */
204 RAISE( mediacontrol_PlaylistException, "No defined media." );
208 /* A media was defined. Get its mrl to reuse it, but reset the options
209 (because start-time may have been set on the previous invocation */
210 psz_name = libvlc_media_get_mrl( p_media );
211 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
213 /* Create a new media */
214 p_media = libvlc_media_new( self->p_instance, psz_name, &ex );
215 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
217 if( a_position->value )
220 libvlc_time_t i_from;
222 /* A start position was specified. Add it to media options */
223 psz_from = ( char * )malloc( 20 * sizeof( char ) );
224 i_from = private_mediacontrol_position2microsecond( self->p_media_player, a_position ) / 1000000;
225 snprintf( psz_from, 20, "start-time=%"PRId64, i_from );
226 libvlc_media_add_option( p_media, psz_from );
227 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
230 libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
231 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
233 libvlc_media_player_play( self->p_media_player, &ex );
234 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
239 mediacontrol_pause( mediacontrol_Instance *self,
240 mediacontrol_Exception *exception )
242 libvlc_exception_t ex;
244 mediacontrol_exception_init( exception );
245 libvlc_exception_init( &ex );
246 libvlc_media_player_pause( self->p_media_player, &ex );
247 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
251 mediacontrol_resume( mediacontrol_Instance *self,
252 mediacontrol_Exception *exception )
254 libvlc_exception_t ex;
256 mediacontrol_exception_init( exception );
257 libvlc_exception_init( &ex );
258 libvlc_media_player_pause( self->p_media_player, &ex );
259 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
263 mediacontrol_stop( mediacontrol_Instance *self,
264 mediacontrol_Exception *exception )
266 libvlc_exception_t ex;
268 mediacontrol_exception_init( exception );
269 libvlc_exception_init( &ex );
270 libvlc_media_player_stop( self->p_media_player, &ex );
271 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
274 /**************************************************************************
276 **************************************************************************/
279 mediacontrol_set_mrl( mediacontrol_Instance *self,
280 const char * psz_file,
281 mediacontrol_Exception *exception )
283 libvlc_media_t * p_media;
284 libvlc_exception_t ex;
286 mediacontrol_exception_init( exception );
287 libvlc_exception_init( &ex );
289 p_media = libvlc_media_new( self->p_instance, psz_file, &ex );
290 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
292 libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
293 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
297 mediacontrol_get_mrl( mediacontrol_Instance *self,
298 mediacontrol_Exception *exception )
300 libvlc_media_t * p_media;
301 libvlc_exception_t ex;
303 mediacontrol_exception_init( exception );
304 libvlc_exception_init( &ex );
306 p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
307 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
317 psz_mrl = libvlc_media_get_mrl( p_media );
318 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
323 /***************************************************************************
325 ***************************************************************************/
327 mediacontrol_StreamInformation *
328 mediacontrol_get_stream_information( mediacontrol_Instance *self,
329 mediacontrol_PositionKey a_key,
330 mediacontrol_Exception *exception )
333 mediacontrol_StreamInformation *retval = NULL;
334 libvlc_media_t * p_media;
335 libvlc_exception_t ex;
337 libvlc_exception_init( &ex );
339 retval = ( mediacontrol_StreamInformation* )
340 malloc( sizeof( mediacontrol_StreamInformation ) );
343 RAISE( mediacontrol_InternalException, "Out of memory" );
347 p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
348 if( libvlc_exception_raised( &ex ) )
351 RAISE( mediacontrol_InternalException, libvlc_errmsg( ) );
352 libvlc_exception_clear( &ex );
358 /* No p_media defined */
359 retval->streamstatus = mediacontrol_UndefinedStatus;
360 retval->url = strdup( "" );
361 retval->position = 0;
366 libvlc_state_t state;
368 state = libvlc_media_player_get_state( self->p_media_player, &ex );
369 if( libvlc_exception_raised( &ex ) )
372 RAISE( mediacontrol_InternalException, libvlc_errmsg() );
373 libvlc_exception_clear( &ex );
379 case libvlc_NothingSpecial:
380 retval->streamstatus = mediacontrol_UndefinedStatus;
382 case libvlc_Opening :
383 retval->streamstatus = mediacontrol_InitStatus;
385 case libvlc_Buffering:
386 retval->streamstatus = mediacontrol_BufferingStatus;
389 retval->streamstatus = mediacontrol_PlayingStatus;
392 retval->streamstatus = mediacontrol_PauseStatus;
395 retval->streamstatus = mediacontrol_StopStatus;
398 retval->streamstatus = mediacontrol_EndStatus;
401 retval->streamstatus = mediacontrol_ErrorStatus;
404 retval->streamstatus = mediacontrol_UndefinedStatus;
408 retval->url = libvlc_media_get_mrl( p_media );
410 retval->position = libvlc_media_player_get_time( self->p_media_player, &ex );
411 if( libvlc_exception_raised( &ex ) )
413 libvlc_exception_clear( &ex );
414 retval->position = 0;
417 retval->length = libvlc_media_player_get_length( self->p_media_player, &ex );
418 if( libvlc_exception_raised( &ex ) )
420 libvlc_exception_clear( &ex );