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