]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_core.c
good
[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 #include <sys/types.h>
50
51 mediacontrol_Instance* mediacontrol_new( int argc, char** argv, mediacontrol_Exception *exception )
52 {
53     mediacontrol_Instance* retval;
54     libvlc_exception_t ex;
55
56     libvlc_exception_init( &ex );
57     mediacontrol_exception_init( exception );
58
59     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
60     if( !retval )
61         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
62
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 );
66     if( !retval->p_media_player )
67         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
68     return retval;
69 }
70
71 void
72 mediacontrol_exit( mediacontrol_Instance *self )
73 {
74     libvlc_release( self->p_instance );
75 }
76
77 libvlc_instance_t*
78 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
79 {
80     return self->p_instance;
81 }
82
83 libvlc_media_player_t*
84 mediacontrol_get_media_player( mediacontrol_Instance *self )
85 {
86     return self->p_media_player;
87 }
88
89 mediacontrol_Instance *
90 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
91                 mediacontrol_Exception *exception )
92 {
93     mediacontrol_Instance* retval;
94
95     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
96     if( ! retval )
97     {
98         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
99     }
100     retval->p_instance = p_instance;
101     retval->p_media_player = libvlc_media_player_new( retval->p_instance );
102     if( ! retval->p_media_player )
103          RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
104     return retval;
105 }
106
107 /**************************************************************************
108  * Playback management
109  **************************************************************************/
110 mediacontrol_Position*
111 mediacontrol_get_media_position( mediacontrol_Instance *self,
112                                  const mediacontrol_PositionOrigin an_origin,
113                                  const mediacontrol_PositionKey a_key,
114                                  mediacontrol_Exception *exception )
115 {
116     mediacontrol_Position* retval = NULL;
117     libvlc_exception_t ex;
118     int64_t pos;
119
120     mediacontrol_exception_init( exception );
121     libvlc_exception_init( &ex );
122
123     retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
124     retval->origin = an_origin;
125     retval->key = a_key;
126
127     if(  an_origin != mediacontrol_AbsolutePosition )
128     {
129         free( retval );
130         /* Relative or ModuloPosition make no sense */
131         RAISE_NULL( mediacontrol_PositionOriginNotSupported,
132                     "Only absolute position is valid." );
133     }
134
135     /* We are asked for an AbsolutePosition. */
136     pos = libvlc_media_player_get_time( self->p_media_player, &ex );
137
138     if( a_key == mediacontrol_MediaTime )
139     {
140         retval->value = pos;
141     }
142     else
143     {
144         retval->value = private_mediacontrol_unit_convert( self->p_media_player,
145                                                            mediacontrol_MediaTime,
146                                                            a_key,
147                                                            pos );
148     }
149     return retval;
150 }
151
152 /* Sets the media position */
153 void
154 mediacontrol_set_media_position( mediacontrol_Instance *self,
155                                  const mediacontrol_Position * a_position,
156                                  mediacontrol_Exception *exception )
157 {
158     libvlc_exception_t ex;
159     int64_t i_pos;
160
161     libvlc_exception_init( &ex );
162     mediacontrol_exception_init( exception );
163
164     i_pos = private_mediacontrol_position2microsecond( self->p_media_player, a_position );
165     libvlc_media_player_set_time( self->p_media_player, i_pos / 1000, &ex );
166     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
167 }
168
169 /* Starts playing a stream */
170 /*
171  * Known issues: since moving in the playlist using playlist_Next
172  * or playlist_Prev implies starting to play items, the a_position
173  * argument will be only honored for the 1st item in the list.
174  *
175  * XXX:FIXME split moving in the playlist and playing items two
176  * different actions or make playlist_<Next|Prev> accept a time
177  * value to start to play from.
178  */
179 void
180 mediacontrol_start( mediacontrol_Instance *self,
181                     const mediacontrol_Position * a_position,
182                     mediacontrol_Exception *exception )
183 {
184     libvlc_media_t * p_media;
185     char * psz_name;
186     libvlc_exception_t ex;
187
188     mediacontrol_exception_init( exception );
189     libvlc_exception_init( &ex );
190
191     p_media = libvlc_media_player_get_media( self->p_media_player );
192
193     if ( ! p_media )
194     {
195         /* No media was defined. */
196         RAISE( mediacontrol_PlaylistException, "No defined media." );
197     }
198     else
199     {
200         /* A media was defined. Get its mrl to reuse it, but reset the options
201            (because start-time may have been set on the previous invocation */
202         psz_name = libvlc_media_get_mrl( p_media );
203         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
204
205         /* Create a new media */
206         p_media = libvlc_media_new( self->p_instance, psz_name, &ex );
207         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
208
209         if( a_position->value )
210         {
211             char * psz_from;
212             libvlc_time_t i_from;
213
214             /* A start position was specified. Add it to media options */
215             psz_from = ( char * )malloc( 20 * sizeof( char ) );
216             i_from = private_mediacontrol_position2microsecond( self->p_media_player, a_position ) / 1000000;
217             snprintf( psz_from, 20, "start-time=%"PRId64, i_from );
218             libvlc_media_add_option( p_media, psz_from );
219             HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
220         }
221
222         libvlc_media_player_set_media( self->p_media_player, p_media );
223
224         libvlc_media_player_play( self->p_media_player, &ex );
225         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
226     }
227 }
228
229 void
230 mediacontrol_pause( mediacontrol_Instance *self,
231                     mediacontrol_Exception *exception )
232 {
233     libvlc_exception_t ex;
234
235     mediacontrol_exception_init( exception );
236     libvlc_exception_init( &ex );
237     libvlc_media_player_pause( self->p_media_player, &ex );
238     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
239 }
240
241 void
242 mediacontrol_resume( mediacontrol_Instance *self,
243                      mediacontrol_Exception *exception )
244 {
245     libvlc_exception_t ex;
246
247     mediacontrol_exception_init( exception );
248     libvlc_exception_init( &ex );
249     libvlc_media_player_pause( self->p_media_player, &ex );
250     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
251 }
252
253 void
254 mediacontrol_stop( mediacontrol_Instance *self,
255                    mediacontrol_Exception *exception )
256 {
257     libvlc_exception_t ex;
258
259     mediacontrol_exception_init( exception );
260     libvlc_exception_init( &ex );
261     libvlc_media_player_stop( self->p_media_player );
262 }
263
264 /**************************************************************************
265  * File management
266  **************************************************************************/
267
268 void
269 mediacontrol_set_mrl( mediacontrol_Instance *self,
270                       const char * psz_file,
271                       mediacontrol_Exception *exception )
272 {
273     libvlc_media_t * p_media;
274     libvlc_exception_t ex;
275
276     mediacontrol_exception_init( exception );
277     libvlc_exception_init( &ex );
278
279     p_media = libvlc_media_new( self->p_instance, psz_file, &ex );
280     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
281
282     libvlc_media_player_set_media( self->p_media_player, p_media );
283 }
284
285 char *
286 mediacontrol_get_mrl( mediacontrol_Instance *self,
287                       mediacontrol_Exception *exception )
288 {
289     libvlc_media_t * p_media;
290     libvlc_exception_t ex;
291
292     mediacontrol_exception_init( exception );
293     libvlc_exception_init( &ex );
294
295     p_media = libvlc_media_player_get_media( self->p_media_player );
296
297     if ( ! p_media )
298     {
299         return strdup( "" );
300     }
301     else
302     {
303         char * psz_mrl;
304
305         psz_mrl = libvlc_media_get_mrl( p_media );
306         HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
307         return psz_mrl;
308     }
309 }
310
311 /***************************************************************************
312  * Status feedback
313  ***************************************************************************/
314
315 mediacontrol_StreamInformation *
316 mediacontrol_get_stream_information( mediacontrol_Instance *self,
317                                      mediacontrol_PositionKey a_key,
318                                      mediacontrol_Exception *exception )
319 {
320     (void)a_key;
321     mediacontrol_StreamInformation *retval = NULL;
322     libvlc_media_t * p_media;
323     libvlc_exception_t ex;
324
325     libvlc_exception_init( &ex );
326
327     retval = ( mediacontrol_StreamInformation* )
328                             malloc( sizeof( mediacontrol_StreamInformation ) );
329     if( ! retval )
330     {
331         RAISE( mediacontrol_InternalException, "Out of memory" );
332         return NULL;
333     }
334
335     p_media = libvlc_media_player_get_media( self->p_media_player );
336     if( ! p_media )
337     {
338         /* No p_media defined */
339         retval->streamstatus = mediacontrol_UndefinedStatus;
340         retval->url          = strdup( "" );
341         retval->position     = 0;
342         retval->length       = 0;
343     }
344     else
345     {
346         libvlc_state_t state;
347
348         state = libvlc_media_player_get_state( self->p_media_player );
349         switch( state )
350         {
351         case libvlc_NothingSpecial:
352             retval->streamstatus = mediacontrol_UndefinedStatus;
353             break;
354         case libvlc_Opening :
355             retval->streamstatus = mediacontrol_InitStatus;
356             break;
357         case libvlc_Buffering:
358             retval->streamstatus = mediacontrol_BufferingStatus;
359             break;
360         case libvlc_Playing:
361             retval->streamstatus = mediacontrol_PlayingStatus;
362             break;
363         case libvlc_Paused:
364             retval->streamstatus = mediacontrol_PauseStatus;
365             break;
366         case libvlc_Stopped:
367             retval->streamstatus = mediacontrol_StopStatus;
368             break;
369         case libvlc_Ended:
370             retval->streamstatus = mediacontrol_EndStatus;
371             break;
372         case libvlc_Error:
373             retval->streamstatus = mediacontrol_ErrorStatus;
374             break;
375         default :
376             retval->streamstatus = mediacontrol_UndefinedStatus;
377             break;
378         }
379
380         retval->url = libvlc_media_get_mrl( p_media );
381
382         retval->position = libvlc_media_player_get_time( self->p_media_player, &ex );
383         if( libvlc_exception_raised( &ex ) )
384         {
385             libvlc_exception_clear( &ex );
386             retval->position = 0;
387         }
388
389         retval->length = libvlc_media_player_get_length( self->p_media_player, &ex );
390         if( libvlc_exception_raised( &ex ) )
391         {
392             libvlc_exception_clear( &ex );
393             retval->length = 0;
394         }
395
396     }
397     return retval;
398 }