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() */
44 #include <errno.h> /* ENOMEM */
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
54 #ifdef HAVE_SYS_TYPES_H
55 # include <sys/types.h>
58 mediacontrol_Instance* mediacontrol_new( int argc, char** argv, mediacontrol_Exception *exception )
60 mediacontrol_Instance* retval;
61 libvlc_exception_t ex;
63 libvlc_exception_init( &ex );
64 mediacontrol_exception_init( exception );
66 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
68 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
70 retval->p_instance = libvlc_new( argc, (const char**)argv, &ex );
71 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
72 retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
73 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
78 mediacontrol_exit( mediacontrol_Instance *self )
80 libvlc_release( self->p_instance );
84 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
86 return self->p_instance;
89 libvlc_media_player_t*
90 mediacontrol_get_media_player( mediacontrol_Instance *self )
92 return self->p_media_player;
95 mediacontrol_Instance *
96 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
97 mediacontrol_Exception *exception )
99 mediacontrol_Instance* retval;
100 libvlc_exception_t ex;
102 libvlc_exception_init( &ex );
104 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
107 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
109 retval->p_instance = p_instance;
110 retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
111 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
115 /**************************************************************************
116 * Playback management
117 **************************************************************************/
118 mediacontrol_Position*
119 mediacontrol_get_media_position( mediacontrol_Instance *self,
120 const mediacontrol_PositionOrigin an_origin,
121 const mediacontrol_PositionKey a_key,
122 mediacontrol_Exception *exception )
124 mediacontrol_Position* retval = NULL;
125 libvlc_exception_t ex;
128 mediacontrol_exception_init( exception );
129 libvlc_exception_init( &ex );
131 retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
132 retval->origin = an_origin;
135 if( an_origin != mediacontrol_AbsolutePosition )
138 /* Relative or ModuloPosition make no sense */
139 RAISE_NULL( mediacontrol_PositionOriginNotSupported,
140 "Only absolute position is valid." );
143 /* We are asked for an AbsolutePosition. */
144 pos = libvlc_media_player_get_time( self->p_media_player, &ex );
146 if( a_key == mediacontrol_MediaTime )
152 retval->value = private_mediacontrol_unit_convert( self->p_media_player,
153 mediacontrol_MediaTime,
160 /* Sets the media position */
162 mediacontrol_set_media_position( mediacontrol_Instance *self,
163 const mediacontrol_Position * a_position,
164 mediacontrol_Exception *exception )
166 libvlc_exception_t ex;
169 libvlc_exception_init( &ex );
170 mediacontrol_exception_init( exception );
172 i_pos = private_mediacontrol_position2microsecond( self->p_media_player, a_position );
173 libvlc_media_player_set_time( self->p_media_player, i_pos / 1000, &ex );
174 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
177 /* Starts playing a stream */
179 * Known issues: since moving in the playlist using playlist_Next
180 * or playlist_Prev implies starting to play items, the a_position
181 * argument will be only honored for the 1st item in the list.
183 * XXX:FIXME split moving in the playlist and playing items two
184 * different actions or make playlist_<Next|Prev> accept a time
185 * value to start to play from.
188 mediacontrol_start( mediacontrol_Instance *self,
189 const mediacontrol_Position * a_position,
190 mediacontrol_Exception *exception )
192 libvlc_media_t * p_media;
194 libvlc_exception_t ex;
196 mediacontrol_exception_init( exception );
197 libvlc_exception_init( &ex );
199 p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
200 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
204 /* No media was defined. */
205 RAISE( mediacontrol_PlaylistException, "No defined media." );
209 /* A media was defined. Get its mrl to reuse it, but reset the options
210 (because start-time may have been set on the previous invocation */
211 psz_name = libvlc_media_get_mrl( p_media, &ex );
212 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
214 /* Create a new media */
215 p_media = libvlc_media_new( self->p_instance, psz_name, &ex );
216 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
218 if( a_position->value )
221 libvlc_time_t i_from;
223 /* A start position was specified. Add it to media options */
224 psz_from = ( char * )malloc( 20 * sizeof( char ) );
225 i_from = private_mediacontrol_position2microsecond( self->p_media_player, a_position ) / 1000000;
226 snprintf( psz_from, 20, "start-time=%"PRId64, i_from );
227 libvlc_media_add_option( p_media, psz_from, &ex );
228 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
231 libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
232 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
234 libvlc_media_player_play( self->p_media_player, &ex );
235 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
240 mediacontrol_pause( mediacontrol_Instance *self,
241 mediacontrol_Exception *exception )
243 libvlc_exception_t ex;
245 mediacontrol_exception_init( exception );
246 libvlc_exception_init( &ex );
247 libvlc_media_player_pause( self->p_media_player, &ex );
248 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
252 mediacontrol_resume( mediacontrol_Instance *self,
253 mediacontrol_Exception *exception )
255 libvlc_exception_t ex;
257 mediacontrol_exception_init( exception );
258 libvlc_exception_init( &ex );
259 libvlc_media_player_pause( self->p_media_player, &ex );
260 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
264 mediacontrol_stop( mediacontrol_Instance *self,
265 mediacontrol_Exception *exception )
267 libvlc_exception_t ex;
269 mediacontrol_exception_init( exception );
270 libvlc_exception_init( &ex );
271 libvlc_media_player_stop( self->p_media_player, &ex );
272 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
275 /**************************************************************************
277 **************************************************************************/
280 mediacontrol_set_mrl( mediacontrol_Instance *self,
281 const char * psz_file,
282 mediacontrol_Exception *exception )
284 libvlc_media_t * p_media;
285 libvlc_exception_t ex;
287 mediacontrol_exception_init( exception );
288 libvlc_exception_init( &ex );
290 p_media = libvlc_media_new( self->p_instance, psz_file, &ex );
291 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
293 libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
294 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
298 mediacontrol_get_mrl( mediacontrol_Instance *self,
299 mediacontrol_Exception *exception )
301 libvlc_media_t * p_media;
302 libvlc_exception_t ex;
304 mediacontrol_exception_init( exception );
305 libvlc_exception_init( &ex );
307 p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
308 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
318 psz_mrl = libvlc_media_get_mrl( p_media, &ex );
319 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
324 /***************************************************************************
326 ***************************************************************************/
328 mediacontrol_StreamInformation *
329 mediacontrol_get_stream_information( mediacontrol_Instance *self,
330 mediacontrol_PositionKey a_key,
331 mediacontrol_Exception *exception )
334 mediacontrol_StreamInformation *retval = NULL;
335 libvlc_media_t * p_media;
336 libvlc_exception_t ex;
338 libvlc_exception_init( &ex );
340 retval = ( mediacontrol_StreamInformation* )
341 malloc( sizeof( mediacontrol_StreamInformation ) );
344 RAISE( mediacontrol_InternalException, "Out of memory" );
348 p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
349 if( libvlc_exception_raised( &ex ) )
352 RAISE( mediacontrol_InternalException,
353 libvlc_exception_get_message( &ex ) );
354 libvlc_exception_clear( &ex );
360 /* No p_media defined */
361 retval->streamstatus = mediacontrol_UndefinedStatus;
362 retval->url = strdup( "" );
363 retval->position = 0;
368 libvlc_state_t state;
370 state = libvlc_media_player_get_state( self->p_media_player, &ex );
371 if( libvlc_exception_raised( &ex ) )
374 RAISE( mediacontrol_InternalException,
375 libvlc_exception_get_message( &ex ) );
376 libvlc_exception_clear( &ex );
382 case libvlc_NothingSpecial:
383 retval->streamstatus = mediacontrol_UndefinedStatus;
385 case libvlc_Opening :
386 retval->streamstatus = mediacontrol_InitStatus;
388 case libvlc_Buffering:
389 retval->streamstatus = mediacontrol_BufferingStatus;
392 retval->streamstatus = mediacontrol_PlayingStatus;
395 retval->streamstatus = mediacontrol_PauseStatus;
398 retval->streamstatus = mediacontrol_StopStatus;
401 retval->streamstatus = mediacontrol_EndStatus;
404 retval->streamstatus = mediacontrol_ErrorStatus;
407 retval->streamstatus = mediacontrol_UndefinedStatus;
411 retval->url = libvlc_media_get_mrl( p_media, &ex );
413 retval->position = libvlc_media_player_get_time( self->p_media_player, &ex );
414 if( libvlc_exception_raised( &ex ) )
416 libvlc_exception_clear( &ex );
417 retval->position = 0;
420 retval->length = libvlc_media_player_get_length( self->p_media_player, &ex );
421 if( libvlc_exception_raised( &ex ) )
423 libvlc_exception_clear( &ex );