]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_core.c
src/control/mediacontrol_core.c: libvlc_input now uses milliseconds as
[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
63     libvlc_exception_init( &ex );
64     exception=mediacontrol_exception_init( exception );
65
66     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
67     if( !retval ) 
68         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
69
70     retval->p_instance = libvlc_new( argc, argv, &ex );
71     retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
72     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
73     return retval;  
74 };
75
76 void
77 mediacontrol_exit( mediacontrol_Instance *self )
78 {
79     libvlc_exception_t ex;
80     libvlc_exception_init( &ex );
81
82     libvlc_destroy( self->p_instance, &ex );
83 }
84
85 libvlc_instance_t*
86 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
87 {
88   return self->p_instance;
89 }
90
91 mediacontrol_Instance *
92 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
93                                 mediacontrol_Exception *exception )
94 {
95   mediacontrol_Instance* retval;
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_playlist = retval->p_instance->p_libvlc_int->p_playlist;
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     libvlc_exception_t ex;
118     vlc_int64_t pos;
119     libvlc_input_t * p_input;
120
121     exception = 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     p_input = libvlc_playlist_get_input( self->p_instance, &ex);
129     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
130
131     if(  an_origin != mediacontrol_AbsolutePosition )
132     {
133         libvlc_input_free( p_input );
134         /* Relative or ModuloPosition make no sense */
135         RAISE_NULL( mediacontrol_PositionOriginNotSupported,
136                     "Only absolute position is valid." );
137     }
138
139     /* We are asked for an AbsolutePosition. */
140     pos = libvlc_input_get_time( p_input, &ex );
141
142     if( a_key == mediacontrol_MediaTime )
143     {
144         retval->value = pos;
145     }
146     else
147     {
148         if( ! self->p_playlist->p_input ) 
149         {
150             libvlc_input_free( p_input );
151             RAISE_NULL( mediacontrol_InternalException,
152                         "No input" );
153         }
154         retval->value = mediacontrol_unit_convert( self->p_playlist->p_input,
155                                                    mediacontrol_MediaTime,
156                                                    a_key,
157                                                    pos );
158     }
159     libvlc_input_free( p_input );
160     return retval;
161 }
162
163 /* Sets the media position */
164 void
165 mediacontrol_set_media_position( mediacontrol_Instance *self,
166                                  const mediacontrol_Position * a_position,
167                                  mediacontrol_Exception *exception )
168 {
169     libvlc_input_t * p_input;
170     libvlc_exception_t ex;
171     vlc_int64_t i_pos;
172
173     libvlc_exception_init( &ex );
174     mediacontrol_exception_init( exception );
175
176     p_input = libvlc_playlist_get_input( self->p_instance, &ex);
177     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
178
179     i_pos = mediacontrol_position2microsecond( self->p_playlist->p_input, a_position );
180     libvlc_input_set_time( p_input, i_pos / 1000, &ex );
181     libvlc_input_free( p_input );
182     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
183 }
184
185 /* Starts playing a stream */
186 /*
187  * Known issues: since moving in the playlist using playlist_Next
188  * or playlist_Prev implies starting to play items, the a_position
189  * argument will be only honored for the 1st item in the list.
190  * 
191  * XXX:FIXME split moving in the playlist and playing items two
192  * different actions or make playlist_<Next|Prev> accept a time
193  * value to start to play from.
194  */
195 void
196 mediacontrol_start( mediacontrol_Instance *self,
197                     const mediacontrol_Position * a_position,
198                     mediacontrol_Exception *exception )
199 {
200     playlist_t * p_playlist = self->p_playlist;
201
202     exception = mediacontrol_exception_init( exception );
203     if( ! p_playlist )
204     {
205         RAISE( mediacontrol_PlaylistException, "No available playlist" );
206         return;
207     }
208
209     vlc_mutex_lock( &p_playlist->object_lock );
210     if( p_playlist->i_size )
211     {
212         int i_from;
213         char *psz_from = NULL;
214
215         psz_from = ( char * )malloc( 20 * sizeof( char ) );
216         if( psz_from && p_playlist->status.p_item )
217         {
218             i_from = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
219
220             /* Set start time */
221             snprintf( psz_from, 20, "start-time=%i", i_from );
222             input_ItemAddOption( p_playlist->status.p_item->p_input, psz_from );
223             free( psz_from );
224         }
225
226         vlc_mutex_unlock( &p_playlist->object_lock );
227         playlist_Play( p_playlist );
228     }
229     else
230     {
231         RAISE( mediacontrol_PlaylistException, "Empty playlist." );
232         vlc_mutex_unlock( &p_playlist->object_lock );
233     }
234 }
235
236 void
237 mediacontrol_pause( mediacontrol_Instance *self,
238                     const mediacontrol_Position * a_position,
239                     mediacontrol_Exception *exception )
240 {
241     input_thread_t *p_input = self->p_playlist->p_input;
242
243     /* FIXME: use the a_position parameter */
244     exception=mediacontrol_exception_init( exception );
245     if( p_input != NULL )
246     {
247         var_SetInteger( p_input, "state", PAUSE_S );
248     }
249     else
250     {
251         RAISE( mediacontrol_InternalException, "No input" );
252     }
253 }
254
255 void
256 mediacontrol_resume( mediacontrol_Instance *self,
257                      const mediacontrol_Position * a_position,
258                      mediacontrol_Exception *exception )
259 {
260     input_thread_t *p_input = self->p_playlist->p_input;
261
262     /* FIXME: use the a_position parameter */
263     exception=mediacontrol_exception_init( exception );
264     if( p_input != NULL )
265     {
266         var_SetInteger( p_input, "state", PAUSE_S );
267     }
268     else
269     {
270         RAISE( mediacontrol_InternalException, "No input" );
271     }
272 }
273
274 void
275 mediacontrol_stop( mediacontrol_Instance *self,
276                    const mediacontrol_Position * a_position,
277                    mediacontrol_Exception *exception )
278 {
279     /* FIXME: use the a_position parameter */
280     exception=mediacontrol_exception_init( exception );
281     if( !self->p_playlist )
282     {
283         RAISE( mediacontrol_PlaylistException, "No playlist" );
284     }
285     else
286         playlist_Stop( self->p_playlist );
287 }
288
289 /**************************************************************************
290  * Playlist management
291  **************************************************************************/
292
293 void
294 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
295                                 const char * psz_file,
296                                 mediacontrol_Exception *exception )
297 {
298     libvlc_exception_t ex;
299
300     mediacontrol_exception_init( exception );
301     libvlc_exception_init( &ex );
302
303     libvlc_playlist_add( self->p_instance, psz_file, psz_file, &ex );
304     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
305 }
306
307 void
308 mediacontrol_playlist_next_item( mediacontrol_Instance *self,
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_next( self->p_instance, &ex );
317     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
318 }
319
320 void
321 mediacontrol_playlist_clear( 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_clear( self->p_instance, &ex );
330     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
331 }
332
333 mediacontrol_PlaylistSeq *
334 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
335                                 mediacontrol_Exception *exception )
336 {
337     mediacontrol_PlaylistSeq *retval = NULL;
338     int i_index;
339     playlist_t * p_playlist = self->p_playlist;
340     int i_playlist_size;
341
342     exception=mediacontrol_exception_init( exception );
343     if( !p_playlist )
344     {
345         RAISE( mediacontrol_PlaylistException, "No playlist" );
346         return NULL;
347     }
348
349     vlc_mutex_lock( &p_playlist->object_lock );
350     i_playlist_size = p_playlist->i_size;
351
352     retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
353
354     for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
355     {
356         retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->p_input->psz_uri );
357     }
358     vlc_mutex_unlock( &p_playlist->object_lock );
359
360     return retval;
361 }
362
363 /***************************************************************************
364  * Status feedback
365  ***************************************************************************/
366
367 mediacontrol_StreamInformation *
368 mediacontrol_get_stream_information( mediacontrol_Instance *self,
369                                      mediacontrol_PositionKey a_key,
370                                      mediacontrol_Exception *exception )
371 {
372     mediacontrol_StreamInformation *retval = NULL;
373     input_thread_t *p_input = self->p_playlist->p_input;
374     vlc_value_t val;
375
376     retval = ( mediacontrol_StreamInformation* )
377                             malloc( sizeof( mediacontrol_StreamInformation ) );
378     if( ! retval )
379     {
380         RAISE( mediacontrol_InternalException, "Out of memory" );
381         return NULL;
382     }
383
384     if( ! p_input )
385     {
386         /* No p_input defined */
387         retval->streamstatus = mediacontrol_UndefinedStatus;
388         retval->url          = strdup( "None" );
389         retval->position     = 0;
390         retval->length       = 0;
391     }
392     else
393     {
394         switch( var_GetInteger( p_input, "state" ) )
395         {
396         case PLAYING_S     :
397             retval->streamstatus = mediacontrol_PlayingStatus;
398             break;
399         case PAUSE_S       :
400             retval->streamstatus = mediacontrol_PauseStatus;
401             break;
402         case INIT_S        :
403             retval->streamstatus = mediacontrol_InitStatus;
404             break;
405         case END_S         :
406             retval->streamstatus = mediacontrol_EndStatus;
407             break;
408         default :
409             retval->streamstatus = mediacontrol_UndefinedStatus;
410             break;
411         }
412
413         retval->url = strdup( p_input->input.p_item->psz_uri );
414
415         /* TIME and LENGTH are in microseconds. We want them in ms */
416         var_Get( p_input, "time", &val);
417         retval->position = val.i_time / 1000;
418
419         var_Get( p_input, "length", &val);
420         retval->length = val.i_time / 1000;
421
422         retval->position = mediacontrol_unit_convert( p_input,
423                                          mediacontrol_MediaTime, a_key,
424                                          retval->position );
425         retval->length   = mediacontrol_unit_convert( p_input,
426                                          mediacontrol_MediaTime, a_key,
427                                          retval->length );
428     }
429     return retval;
430 }