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