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