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 #include <sys/types.h>
51 mediacontrol_Instance* mediacontrol_new( int argc, char** argv, mediacontrol_Exception *exception )
53 mediacontrol_Instance* retval;
54 libvlc_exception_t ex;
56 libvlc_exception_init( &ex );
57 mediacontrol_exception_init( exception );
59 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
61 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
63 retval->p_instance = libvlc_new( argc, (const char**)argv, &ex );
64 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
65 retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
66 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
71 mediacontrol_exit( mediacontrol_Instance *self )
73 libvlc_release( self->p_instance );
77 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
79 return self->p_instance;
82 libvlc_media_player_t*
83 mediacontrol_get_media_player( mediacontrol_Instance *self )
85 return self->p_media_player;
88 mediacontrol_Instance *
89 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
90 mediacontrol_Exception *exception )
92 mediacontrol_Instance* retval;
93 libvlc_exception_t ex;
95 libvlc_exception_init( &ex );
97 retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
100 RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
102 retval->p_instance = p_instance;
103 retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
104 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
108 /**************************************************************************
109 * Playback management
110 **************************************************************************/
111 mediacontrol_Position*
112 mediacontrol_get_media_position( mediacontrol_Instance *self,
113 const mediacontrol_PositionOrigin an_origin,
114 const mediacontrol_PositionKey a_key,
115 mediacontrol_Exception *exception )
117 mediacontrol_Position* retval = NULL;
118 libvlc_exception_t ex;
121 mediacontrol_exception_init( exception );
122 libvlc_exception_init( &ex );
124 retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
125 retval->origin = an_origin;
128 if( an_origin != mediacontrol_AbsolutePosition )
131 /* Relative or ModuloPosition make no sense */
132 RAISE_NULL( mediacontrol_PositionOriginNotSupported,
133 "Only absolute position is valid." );
136 /* We are asked for an AbsolutePosition. */
137 pos = libvlc_media_player_get_time( self->p_media_player, &ex );
139 if( a_key == mediacontrol_MediaTime )
145 retval->value = private_mediacontrol_unit_convert( self->p_media_player,
146 mediacontrol_MediaTime,
153 /* Sets the media position */
155 mediacontrol_set_media_position( mediacontrol_Instance *self,
156 const mediacontrol_Position * a_position,
157 mediacontrol_Exception *exception )
159 libvlc_exception_t ex;
162 libvlc_exception_init( &ex );
163 mediacontrol_exception_init( exception );
165 i_pos = private_mediacontrol_position2microsecond( self->p_media_player, a_position );
166 libvlc_media_player_set_time( self->p_media_player, i_pos / 1000, &ex );
167 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
170 /* Starts playing a stream */
172 * Known issues: since moving in the playlist using playlist_Next
173 * or playlist_Prev implies starting to play items, the a_position
174 * argument will be only honored for the 1st item in the list.
176 * XXX:FIXME split moving in the playlist and playing items two
177 * different actions or make playlist_<Next|Prev> accept a time
178 * value to start to play from.
181 mediacontrol_start( mediacontrol_Instance *self,
182 const mediacontrol_Position * a_position,
183 mediacontrol_Exception *exception )
185 libvlc_media_t * p_media;
187 libvlc_exception_t ex;
189 mediacontrol_exception_init( exception );
190 libvlc_exception_init( &ex );
192 p_media = libvlc_media_player_get_media( self->p_media_player );
196 /* No media was defined. */
197 RAISE( mediacontrol_PlaylistException, "No defined media." );
201 /* A media was defined. Get its mrl to reuse it, but reset the options
202 (because start-time may have been set on the previous invocation */
203 psz_name = libvlc_media_get_mrl( p_media );
204 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
206 /* Create a new media */
207 p_media = libvlc_media_new( self->p_instance, psz_name, &ex );
208 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
210 if( a_position->value )
213 libvlc_time_t i_from;
215 /* A start position was specified. Add it to media options */
216 psz_from = ( char * )malloc( 20 * sizeof( char ) );
217 i_from = private_mediacontrol_position2microsecond( self->p_media_player, a_position ) / 1000000;
218 snprintf( psz_from, 20, "start-time=%"PRId64, i_from );
219 libvlc_media_add_option( p_media, psz_from );
220 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
223 libvlc_media_player_set_media( self->p_media_player, p_media );
225 libvlc_media_player_play( self->p_media_player, &ex );
226 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
231 mediacontrol_pause( mediacontrol_Instance *self,
232 mediacontrol_Exception *exception )
234 libvlc_exception_t ex;
236 mediacontrol_exception_init( exception );
237 libvlc_exception_init( &ex );
238 libvlc_media_player_pause( self->p_media_player, &ex );
239 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
243 mediacontrol_resume( mediacontrol_Instance *self,
244 mediacontrol_Exception *exception )
246 libvlc_exception_t ex;
248 mediacontrol_exception_init( exception );
249 libvlc_exception_init( &ex );
250 libvlc_media_player_pause( self->p_media_player, &ex );
251 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
255 mediacontrol_stop( mediacontrol_Instance *self,
256 mediacontrol_Exception *exception )
258 libvlc_exception_t ex;
260 mediacontrol_exception_init( exception );
261 libvlc_exception_init( &ex );
262 libvlc_media_player_stop( self->p_media_player );
265 /**************************************************************************
267 **************************************************************************/
270 mediacontrol_set_mrl( mediacontrol_Instance *self,
271 const char * psz_file,
272 mediacontrol_Exception *exception )
274 libvlc_media_t * p_media;
275 libvlc_exception_t ex;
277 mediacontrol_exception_init( exception );
278 libvlc_exception_init( &ex );
280 p_media = libvlc_media_new( self->p_instance, psz_file, &ex );
281 HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
283 libvlc_media_player_set_media( self->p_media_player, p_media );
287 mediacontrol_get_mrl( mediacontrol_Instance *self,
288 mediacontrol_Exception *exception )
290 libvlc_media_t * p_media;
291 libvlc_exception_t ex;
293 mediacontrol_exception_init( exception );
294 libvlc_exception_init( &ex );
296 p_media = libvlc_media_player_get_media( self->p_media_player );
306 psz_mrl = libvlc_media_get_mrl( p_media );
307 HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
312 /***************************************************************************
314 ***************************************************************************/
316 mediacontrol_StreamInformation *
317 mediacontrol_get_stream_information( mediacontrol_Instance *self,
318 mediacontrol_PositionKey a_key,
319 mediacontrol_Exception *exception )
322 mediacontrol_StreamInformation *retval = NULL;
323 libvlc_media_t * p_media;
324 libvlc_exception_t ex;
326 libvlc_exception_init( &ex );
328 retval = ( mediacontrol_StreamInformation* )
329 malloc( sizeof( mediacontrol_StreamInformation ) );
332 RAISE( mediacontrol_InternalException, "Out of memory" );
336 p_media = libvlc_media_player_get_media( self->p_media_player );
339 /* No p_media defined */
340 retval->streamstatus = mediacontrol_UndefinedStatus;
341 retval->url = strdup( "" );
342 retval->position = 0;
347 libvlc_state_t state;
349 state = libvlc_media_player_get_state( self->p_media_player );
352 case libvlc_NothingSpecial:
353 retval->streamstatus = mediacontrol_UndefinedStatus;
355 case libvlc_Opening :
356 retval->streamstatus = mediacontrol_InitStatus;
358 case libvlc_Buffering:
359 retval->streamstatus = mediacontrol_BufferingStatus;
362 retval->streamstatus = mediacontrol_PlayingStatus;
365 retval->streamstatus = mediacontrol_PauseStatus;
368 retval->streamstatus = mediacontrol_StopStatus;
371 retval->streamstatus = mediacontrol_EndStatus;
374 retval->streamstatus = mediacontrol_ErrorStatus;
377 retval->streamstatus = mediacontrol_UndefinedStatus;
381 retval->url = libvlc_media_get_mrl( p_media );
383 retval->position = libvlc_media_player_get_time( self->p_media_player, &ex );
384 if( libvlc_exception_raised( &ex ) )
386 libvlc_exception_clear( &ex );
387 retval->position = 0;
390 retval->length = libvlc_media_player_get_length( self->p_media_player, &ex );
391 if( libvlc_exception_raised( &ex ) )
393 libvlc_exception_clear( &ex );