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