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