]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_core.c
709019440c1d8c540298774120f7d67086b4f329
[vlc] / src / control / mediacontrol_core.c
1 /*****************************************************************************
2  * core.c: Core functions : init, playlist, stream management
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "mediacontrol_internal.h"
29 #include <vlc/mediacontrol.h>
30
31 #include <vlc/libvlc.h>
32 #include <vlc_common.h>
33 #include <vlc_interface.h>
34 #include <vlc_playlist.h>
35
36 #include <vlc_vout.h>
37 #include <vlc_aout.h>
38 #include <vlc_input.h>
39 #include <vlc_osd.h>
40
41 #include <stdlib.h>                                      /* malloc(), free() */
42 #include <string.h>
43
44 #include <stdio.h>
45
46 #ifdef HAVE_UNISTD_H
47 #    include <unistd.h>
48 #endif
49 #ifdef HAVE_SYS_TIME_H
50 #    include <sys/time.h>
51 #endif
52 #ifdef HAVE_SYS_TYPES_H
53 #    include <sys/types.h>
54 #endif
55
56 mediacontrol_Instance* mediacontrol_new( int argc, char** argv, mediacontrol_Exception *exception )
57 {
58     mediacontrol_Instance* retval;
59     libvlc_exception_t ex;
60
61     libvlc_exception_init( &ex );
62     mediacontrol_exception_init( exception );
63
64     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
65     if( !retval )
66         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
67
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 );
72     return retval;
73 }
74
75 void
76 mediacontrol_exit( mediacontrol_Instance *self )
77 {
78     libvlc_release( self->p_instance );
79 }
80
81 libvlc_instance_t*
82 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
83 {
84     return self->p_instance;
85 }
86
87 libvlc_media_player_t*
88 mediacontrol_get_media_player( mediacontrol_Instance *self )
89 {
90     return self->p_media_player;
91 }
92
93 mediacontrol_Instance *
94 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
95                 mediacontrol_Exception *exception )
96 {
97     mediacontrol_Instance* retval;
98     libvlc_exception_t ex;
99
100     libvlc_exception_init( &ex );
101
102     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
103     if( ! retval )
104     {
105         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
106     }
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 );
110     return retval;
111 }
112
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 )
121 {
122     mediacontrol_Position* retval = NULL;
123     libvlc_exception_t ex;
124     int64_t pos;
125
126     mediacontrol_exception_init( exception );
127     libvlc_exception_init( &ex );
128
129     retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
130     retval->origin = an_origin;
131     retval->key = a_key;
132
133     if(  an_origin != mediacontrol_AbsolutePosition )
134     {
135         free( retval );
136         /* Relative or ModuloPosition make no sense */
137         RAISE_NULL( mediacontrol_PositionOriginNotSupported,
138                     "Only absolute position is valid." );
139     }
140
141     /* We are asked for an AbsolutePosition. */
142     pos = libvlc_media_player_get_time( self->p_media_player, &ex );
143
144     if( a_key == mediacontrol_MediaTime )
145     {
146         retval->value = pos;
147     }
148     else
149     {
150         retval->value = private_mediacontrol_unit_convert( self->p_media_player,
151                                                            mediacontrol_MediaTime,
152                                                            a_key,
153                                                            pos );
154     }
155     return retval;
156 }
157
158 /* Sets the media position */
159 void
160 mediacontrol_set_media_position( mediacontrol_Instance *self,
161                                  const mediacontrol_Position * a_position,
162                                  mediacontrol_Exception *exception )
163 {
164     libvlc_exception_t ex;
165     int64_t i_pos;
166
167     libvlc_exception_init( &ex );
168     mediacontrol_exception_init( exception );
169
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 );
173 }
174
175 /* Starts playing a stream */
176 /*
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.
180  *
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.
184  */
185 void
186 mediacontrol_start( mediacontrol_Instance *self,
187                     const mediacontrol_Position * a_position,
188                     mediacontrol_Exception *exception )
189 {
190     libvlc_media_t * p_media;
191     char * psz_name;
192     libvlc_exception_t ex;
193
194     mediacontrol_exception_init( exception );
195     libvlc_exception_init( &ex );
196
197     p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
198     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
199
200     if ( ! p_media )
201     {
202         /* No media was defined. */
203         RAISE( mediacontrol_PlaylistException, "No defined media." );
204     }
205     else
206     {
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 );
211
212         /* Create a new media */
213         p_media = libvlc_media_new( self->p_instance, psz_name, &ex );
214         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
215
216         if( a_position->value )
217         {
218             char * psz_from;
219             libvlc_time_t i_from;
220
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 );
227         }
228
229         libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
230         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
231
232         libvlc_media_player_play( self->p_media_player, &ex );
233         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
234     }
235 }
236
237 void
238 mediacontrol_pause( mediacontrol_Instance *self,
239                     mediacontrol_Exception *exception )
240 {
241     libvlc_exception_t ex;
242
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 );
247 }
248
249 void
250 mediacontrol_resume( mediacontrol_Instance *self,
251                      mediacontrol_Exception *exception )
252 {
253     libvlc_exception_t ex;
254
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 );
259 }
260
261 void
262 mediacontrol_stop( mediacontrol_Instance *self,
263                    mediacontrol_Exception *exception )
264 {
265     libvlc_exception_t ex;
266
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 );
271 }
272
273 /**************************************************************************
274  * File management
275  **************************************************************************/
276
277 void
278 mediacontrol_set_mrl( mediacontrol_Instance *self,
279                       const char * psz_file,
280                       mediacontrol_Exception *exception )
281 {
282     libvlc_media_t * p_media;
283     libvlc_exception_t ex;
284
285     mediacontrol_exception_init( exception );
286     libvlc_exception_init( &ex );
287
288     p_media = libvlc_media_new( self->p_instance, psz_file, &ex );
289     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
290
291     libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
292     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
293 }
294
295 char *
296 mediacontrol_get_mrl( mediacontrol_Instance *self,
297                       mediacontrol_Exception *exception )
298 {
299     libvlc_media_t * p_media;
300     libvlc_exception_t ex;
301
302     mediacontrol_exception_init( exception );
303     libvlc_exception_init( &ex );
304
305     p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
306     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
307
308     if ( ! p_media )
309     {
310         return strdup( "" );
311     }
312     else
313     {
314         char * psz_mrl;
315
316         psz_mrl = libvlc_media_get_mrl( p_media );
317         HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
318         return psz_mrl;
319     }
320 }
321
322 /***************************************************************************
323  * Status feedback
324  ***************************************************************************/
325
326 mediacontrol_StreamInformation *
327 mediacontrol_get_stream_information( mediacontrol_Instance *self,
328                                      mediacontrol_PositionKey a_key,
329                                      mediacontrol_Exception *exception )
330 {
331     (void)a_key;
332     mediacontrol_StreamInformation *retval = NULL;
333     libvlc_media_t * p_media;
334     libvlc_exception_t ex;
335
336     libvlc_exception_init( &ex );
337
338     retval = ( mediacontrol_StreamInformation* )
339                             malloc( sizeof( mediacontrol_StreamInformation ) );
340     if( ! retval )
341     {
342         RAISE( mediacontrol_InternalException, "Out of memory" );
343         return NULL;
344     }
345
346     p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
347     if( libvlc_exception_raised( &ex ) )
348     {
349         free( retval );
350         RAISE( mediacontrol_InternalException, libvlc_errmsg( ) );
351         libvlc_exception_clear( &ex );
352         return NULL;
353     }
354
355     if( ! p_media )
356     {
357         /* No p_media defined */
358         retval->streamstatus = mediacontrol_UndefinedStatus;
359         retval->url          = strdup( "" );
360         retval->position     = 0;
361         retval->length       = 0;
362     }
363     else
364     {
365         libvlc_state_t state;
366
367         state = libvlc_media_player_get_state( self->p_media_player, &ex );
368         if( libvlc_exception_raised( &ex ) )
369         {
370             free( retval );
371             RAISE( mediacontrol_InternalException, libvlc_errmsg() );
372             libvlc_exception_clear( &ex );
373             return NULL;
374         }
375
376         switch( state )
377         {
378         case libvlc_NothingSpecial:
379             retval->streamstatus = mediacontrol_UndefinedStatus;
380             break;
381         case libvlc_Opening :
382             retval->streamstatus = mediacontrol_InitStatus;
383             break;
384         case libvlc_Buffering:
385             retval->streamstatus = mediacontrol_BufferingStatus;
386             break;
387         case libvlc_Playing:
388             retval->streamstatus = mediacontrol_PlayingStatus;
389             break;
390         case libvlc_Paused:
391             retval->streamstatus = mediacontrol_PauseStatus;
392             break;
393         case libvlc_Stopped:
394             retval->streamstatus = mediacontrol_StopStatus;
395             break;
396         case libvlc_Ended:
397             retval->streamstatus = mediacontrol_EndStatus;
398             break;
399         case libvlc_Error:
400             retval->streamstatus = mediacontrol_ErrorStatus;
401             break;
402         default :
403             retval->streamstatus = mediacontrol_UndefinedStatus;
404             break;
405         }
406
407         retval->url = libvlc_media_get_mrl( p_media );
408
409         retval->position = libvlc_media_player_get_time( self->p_media_player, &ex );
410         if( libvlc_exception_raised( &ex ) )
411         {
412             libvlc_exception_clear( &ex );
413             retval->position = 0;
414         }
415
416         retval->length = libvlc_media_player_get_length( self->p_media_player, &ex );
417         if( libvlc_exception_raised( &ex ) )
418         {
419             libvlc_exception_clear( &ex );
420             retval->length = 0;
421         }
422
423     }
424     return retval;
425 }