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