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