a072bd83d163a65eedf876a9a1b1a895534b9d47
[vlc.git] / 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 #include "mediacontrol_internal.h"
25 #include <vlc/mediacontrol.h>
26
27 #include <vlc/libvlc.h>
28 #include <vlc_interface.h>
29 #include <vlc_playlist.h>
30
31 #include <vlc_vout.h>
32 #include <vlc_aout.h>
33 #include <vlc_input.h>
34 #include <vlc_osd.h>
35
36 #include <stdlib.h>                                      /* malloc(), free() */
37 #include <string.h>
38
39 #include <errno.h>                                                 /* ENOMEM */
40 #include <stdio.h>
41 #include <ctype.h>
42
43 #ifdef HAVE_UNISTD_H
44 #    include <unistd.h>
45 #endif
46 #ifdef HAVE_SYS_TIME_H
47 #    include <sys/time.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     char** ppsz_argv = NULL;
58     int i_index;
59
60     libvlc_exception_init( &ex );
61     exception=mediacontrol_exception_init( exception );
62
63     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
64     if( !retval ) 
65         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
66
67     /* Prepend a dummy argv[0] so that users of the API do not have to  
68        do it themselves, and can simply provide the args list. */ 
69     ppsz_argv = malloc( ( argc + 2 ) * sizeof( char * ) ) ; 
70     if( ! ppsz_argv )  
71         RAISE_NULL( mediacontrol_InternalException, "Out of memory" ); 
72                  
73     ppsz_argv[0] = strdup("vlc"); 
74     for ( i_index = 0; i_index < argc; i_index++ ) 
75         ppsz_argv[i_index + 1] = argv[i_index]; 
76     ppsz_argv[argc + 1] = NULL; 
77                      
78     retval->p_instance = libvlc_new( argc + 1, ppsz_argv, &ex );
79     retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
80     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
81     return retval;  
82 };
83
84 void
85 mediacontrol_exit( mediacontrol_Instance *self )
86 {
87     libvlc_exception_t ex;
88     libvlc_exception_init( &ex );
89
90     libvlc_destroy( self->p_instance, &ex );
91 }
92
93 libvlc_instance_t*
94 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
95 {
96   return self->p_instance;
97 }
98
99 mediacontrol_Instance *
100 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
101                                 mediacontrol_Exception *exception )
102 {
103   mediacontrol_Instance* retval;
104
105   retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
106   if( ! retval )
107   { 
108       RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
109   }
110   retval->p_instance = p_instance;
111   retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
112   return retval;  
113 }
114
115 /**************************************************************************
116  * Playback management
117  **************************************************************************/
118 mediacontrol_Position*
119 mediacontrol_get_media_position( mediacontrol_Instance *self,
120                                  const mediacontrol_PositionOrigin an_origin,
121                                  const mediacontrol_PositionKey a_key,
122                                  mediacontrol_Exception *exception )
123 {
124     mediacontrol_Position* retval = NULL;
125     libvlc_exception_t ex;
126     vlc_int64_t pos;
127     libvlc_input_t * p_input;
128
129     exception = mediacontrol_exception_init( exception );
130     libvlc_exception_init( &ex );
131
132     retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
133     retval->origin = an_origin;
134     retval->key = a_key;
135
136     p_input = libvlc_playlist_get_input( self->p_instance, &ex);
137     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
138
139     if(  an_origin != mediacontrol_AbsolutePosition )
140     {
141         libvlc_input_free( p_input );
142         /* Relative or ModuloPosition make no sense */
143         RAISE_NULL( mediacontrol_PositionOriginNotSupported,
144                     "Only absolute position is valid." );
145     }
146
147     /* We are asked for an AbsolutePosition. */
148     pos = libvlc_input_get_time( p_input, &ex );
149
150     if( a_key == mediacontrol_MediaTime )
151     {
152         retval->value = pos;
153     }
154     else
155     {
156         if( ! self->p_playlist->p_input )
157         {
158             libvlc_input_free( p_input );
159             RAISE_NULL( mediacontrol_InternalException,
160                         "No input" );
161         }
162         retval->value = mediacontrol_unit_convert( self->p_playlist->p_input,
163                                                    mediacontrol_MediaTime,
164                                                    a_key,
165                                                    pos );
166     }
167     libvlc_input_free( p_input );
168     return retval;
169 }
170
171 /* Sets the media position */
172 void
173 mediacontrol_set_media_position( mediacontrol_Instance *self,
174                                  const mediacontrol_Position * a_position,
175                                  mediacontrol_Exception *exception )
176 {
177     libvlc_input_t * p_input;
178     libvlc_exception_t ex;
179     vlc_int64_t i_pos;
180
181     libvlc_exception_init( &ex );
182     mediacontrol_exception_init( exception );
183
184     p_input = libvlc_playlist_get_input( self->p_instance, &ex);
185     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
186
187     i_pos = mediacontrol_position2microsecond( self->p_playlist->p_input, a_position );
188     libvlc_input_set_time( p_input, i_pos / 1000, &ex );
189     libvlc_input_free( p_input );
190     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
191 }
192
193 /* Starts playing a stream */
194 /*
195  * Known issues: since moving in the playlist using playlist_Next
196  * or playlist_Prev implies starting to play items, the a_position
197  * argument will be only honored for the 1st item in the list.
198  * 
199  * XXX:FIXME split moving in the playlist and playing items two
200  * different actions or make playlist_<Next|Prev> accept a time
201  * value to start to play from.
202  */
203 void
204 mediacontrol_start( mediacontrol_Instance *self,
205                     const mediacontrol_Position * a_position,
206                     mediacontrol_Exception *exception )
207 {
208     playlist_t * p_playlist = self->p_playlist;
209
210     exception = mediacontrol_exception_init( exception );
211     if( ! p_playlist )
212     {
213         RAISE( mediacontrol_PlaylistException, "No available playlist" );
214         return;
215     }
216
217     vlc_mutex_lock( &p_playlist->object_lock );
218     if( p_playlist->items.i_size )
219     {
220         int i_from;
221         char *psz_from = NULL;
222
223         psz_from = ( char * )malloc( 20 * sizeof( char ) );
224         if( psz_from && p_playlist->status.p_item )
225         {
226             i_from = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
227
228             /* Set start time */
229             snprintf( psz_from, 20, "start-time=%i", i_from );
230             input_ItemAddOption( p_playlist->status.p_item->p_input, psz_from );
231             free( psz_from );
232         }
233
234         vlc_mutex_unlock( &p_playlist->object_lock );
235         playlist_Play( p_playlist );
236     }
237     else
238     {
239         RAISE( mediacontrol_PlaylistException, "Empty playlist." );
240         vlc_mutex_unlock( &p_playlist->object_lock );
241     }
242 }
243
244 void
245 mediacontrol_pause( mediacontrol_Instance *self,
246                     const mediacontrol_Position * a_position,
247                     mediacontrol_Exception *exception )
248 {
249     input_thread_t *p_input = self->p_playlist->p_input;
250
251     /* FIXME: use the a_position parameter */
252     exception=mediacontrol_exception_init( exception );
253     if( p_input != NULL )
254     {
255         var_SetInteger( p_input, "state", PAUSE_S );
256     }
257     else
258     {
259         RAISE( mediacontrol_InternalException, "No input" );
260     }
261 }
262
263 void
264 mediacontrol_resume( mediacontrol_Instance *self,
265                      const mediacontrol_Position * a_position,
266                      mediacontrol_Exception *exception )
267 {
268     input_thread_t *p_input = self->p_playlist->p_input;
269
270     /* FIXME: use the a_position parameter */
271     exception=mediacontrol_exception_init( exception );
272     if( p_input != NULL )
273     {
274         var_SetInteger( p_input, "state", PAUSE_S );
275     }
276     else
277     {
278         RAISE( mediacontrol_InternalException, "No input" );
279     }
280 }
281
282 void
283 mediacontrol_stop( mediacontrol_Instance *self,
284                    const mediacontrol_Position * a_position,
285                    mediacontrol_Exception *exception )
286 {
287     /* FIXME: use the a_position parameter */
288     exception=mediacontrol_exception_init( exception );
289     if( !self->p_playlist )
290     {
291         RAISE( mediacontrol_PlaylistException, "No playlist" );
292     }
293     else
294         playlist_Stop( self->p_playlist );
295 }
296
297 /**************************************************************************
298  * Playlist management
299  **************************************************************************/
300
301 void
302 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
303                                 const char * psz_file,
304                                 mediacontrol_Exception *exception )
305 {
306     libvlc_exception_t ex;
307
308     mediacontrol_exception_init( exception );
309     libvlc_exception_init( &ex );
310
311     libvlc_playlist_add( self->p_instance, psz_file, psz_file, &ex );
312     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
313 }
314
315 void
316 mediacontrol_playlist_next_item( mediacontrol_Instance *self,
317                                  mediacontrol_Exception *exception )
318 {
319     libvlc_exception_t ex;
320
321     mediacontrol_exception_init( exception );
322     libvlc_exception_init( &ex );
323
324     libvlc_playlist_next( self->p_instance, &ex );
325     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
326 }
327
328 void
329 mediacontrol_playlist_clear( mediacontrol_Instance *self,
330                              mediacontrol_Exception *exception )
331 {
332     libvlc_exception_t ex;
333
334     mediacontrol_exception_init( exception );
335     libvlc_exception_init( &ex );
336
337     libvlc_playlist_clear( self->p_instance, &ex );
338     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
339 }
340
341 mediacontrol_PlaylistSeq *
342 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
343                                 mediacontrol_Exception *exception )
344 {
345     mediacontrol_PlaylistSeq *retval = NULL;
346     int i_index;
347     playlist_t * p_playlist = self->p_playlist;
348     int i_playlist_size;
349
350     exception=mediacontrol_exception_init( exception );
351     if( !p_playlist )
352     {
353         RAISE( mediacontrol_PlaylistException, "No playlist" );
354         return NULL;
355     }
356
357     vlc_mutex_lock( &p_playlist->object_lock );
358     i_playlist_size = p_playlist->items.i_size;
359
360     retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
361
362     for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
363     {
364         retval->data[i_index] = strdup( ARRAY_VAL(p_playlist->items, i_index)->p_input->psz_uri );
365     }
366     vlc_mutex_unlock( &p_playlist->object_lock );
367
368     return retval;
369 }
370
371 /***************************************************************************
372  * Status feedback
373  ***************************************************************************/
374
375 mediacontrol_StreamInformation *
376 mediacontrol_get_stream_information( mediacontrol_Instance *self,
377                                      mediacontrol_PositionKey a_key,
378                                      mediacontrol_Exception *exception )
379 {
380     mediacontrol_StreamInformation *retval = NULL;
381     input_thread_t *p_input = self->p_playlist->p_input;
382     vlc_value_t val;
383
384     retval = ( mediacontrol_StreamInformation* )
385                             malloc( sizeof( mediacontrol_StreamInformation ) );
386     if( ! retval )
387     {
388         RAISE( mediacontrol_InternalException, "Out of memory" );
389         return NULL;
390     }
391
392     if( ! p_input )
393     {
394         /* No p_input defined */
395         retval->streamstatus = mediacontrol_UndefinedStatus;
396         retval->url          = strdup( "None" );
397         retval->position     = 0;
398         retval->length       = 0;
399     }
400     else
401     {
402         switch( var_GetInteger( p_input, "state" ) )
403         {
404         case PLAYING_S     :
405             retval->streamstatus = mediacontrol_PlayingStatus;
406             break;
407         case PAUSE_S       :
408             retval->streamstatus = mediacontrol_PauseStatus;
409             break;
410         case INIT_S        :
411             retval->streamstatus = mediacontrol_InitStatus;
412             break;
413         case END_S         :
414             retval->streamstatus = mediacontrol_EndStatus;
415             break;
416         default :
417             retval->streamstatus = mediacontrol_UndefinedStatus;
418             break;
419         }
420
421         retval->url = strdup( input_GetItem(p_input)->psz_uri );
422
423         /* TIME and LENGTH are in microseconds. We want them in ms */
424         var_Get( p_input, "time", &val);
425         retval->position = val.i_time / 1000;
426
427         var_Get( p_input, "length", &val);
428         retval->length = val.i_time / 1000;
429
430         retval->position = mediacontrol_unit_convert( p_input,
431                                          mediacontrol_MediaTime, a_key,
432                                          retval->position );
433         retval->length   = mediacontrol_unit_convert( p_input,
434                                          mediacontrol_MediaTime, a_key,
435                                          retval->length );
436     }
437     return retval;
438 }