]> git.sesse.net Git - vlc/blob - src/control/core.c
src/control/core.c: correctly set start-time through playlist_ItemAddOption
[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         int i_from;
189
190         vlc_mutex_unlock( &p_playlist->object_lock );
191
192         i_from = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
193
194         if( p_playlist->status.p_item )
195         {
196             char psz_from[20];
197             snprintf( psz_from, 20, "start-time=%i", i_from);
198             playlist_ItemAddOption( p_playlist->status.p_item, psz_from);
199         }
200
201         playlist_Play( p_playlist );
202     }
203     else
204     {
205         RAISE( mediacontrol_PlaylistException, "Empty playlist." );
206         vlc_mutex_unlock( &p_playlist->object_lock );
207         return;
208     }
209
210     return;
211 }
212
213 void
214 mediacontrol_pause( mediacontrol_Instance *self,
215                     const mediacontrol_Position * a_position,
216                     mediacontrol_Exception *exception )
217 {
218     input_thread_t *p_input = self->p_playlist->p_input;
219
220     /* FIXME: use the a_position parameter */
221     exception=mediacontrol_exception_init( exception );
222     if( p_input != NULL )
223     {
224         var_SetInteger( p_input, "state", PAUSE_S );
225     }
226     else
227     {
228         RAISE( mediacontrol_InternalException, "No input" );
229     }
230
231     return;
232 }
233
234 void
235 mediacontrol_resume( mediacontrol_Instance *self,
236                      const mediacontrol_Position * a_position,
237                      mediacontrol_Exception *exception )
238 {
239     input_thread_t *p_input = self->p_playlist->p_input;
240
241     /* FIXME: use the a_position parameter */
242     exception=mediacontrol_exception_init( exception );
243     if( p_input != NULL )
244     {
245         var_SetInteger( p_input, "state", PAUSE_S );
246     }
247     else
248     {
249         RAISE( mediacontrol_InternalException, "No input" );
250     }
251 }
252
253 void
254 mediacontrol_stop( mediacontrol_Instance *self,
255                    const mediacontrol_Position * a_position,
256                    mediacontrol_Exception *exception )
257 {
258     /* FIXME: use the a_position parameter */
259     exception=mediacontrol_exception_init( exception );
260     if( !self->p_playlist )
261     {
262         RAISE( mediacontrol_PlaylistException, "No playlist" );
263         return;
264     }
265
266     playlist_Stop( self->p_playlist );
267 }
268
269 /**************************************************************************
270  * Playlist management
271  **************************************************************************/
272
273 void
274 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
275                                 const char * psz_file,
276                                 mediacontrol_Exception *exception )
277 {
278     exception=mediacontrol_exception_init( exception );
279     if( !self->p_playlist )
280     {
281         RAISE( mediacontrol_InternalException, "No playlist" );
282         return;
283     }
284
285     playlist_Add( self->p_playlist, psz_file, psz_file , PLAYLIST_INSERT,
286                   PLAYLIST_END );
287 }
288
289 void
290 mediacontrol_playlist_clear( mediacontrol_Instance *self,
291                              mediacontrol_Exception *exception )
292 {
293     exception=mediacontrol_exception_init( exception );
294     if( !self->p_playlist )
295     {
296         RAISE( mediacontrol_PlaylistException, "No playlist" );
297         return;
298     }
299
300     playlist_Clear( self->p_playlist );
301
302     return;
303 }
304
305 mediacontrol_PlaylistSeq *
306 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
307                                 mediacontrol_Exception *exception )
308 {
309     mediacontrol_PlaylistSeq *retval;
310     int i_index;
311     playlist_t * p_playlist = self->p_playlist;
312     int i_playlist_size;
313
314     exception=mediacontrol_exception_init( exception );
315     if( !p_playlist )
316     {
317         RAISE( mediacontrol_PlaylistException, "No playlist" );
318         return NULL;
319     }
320
321     vlc_mutex_lock( &p_playlist->object_lock );
322     i_playlist_size = p_playlist->i_size;
323
324     retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
325
326     for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
327     {
328         retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->input.psz_uri );
329     }
330     vlc_mutex_unlock( &p_playlist->object_lock );
331
332     return retval;
333 }
334
335 /***************************************************************************
336  * Status feedback
337  ***************************************************************************/
338
339 mediacontrol_StreamInformation *
340 mediacontrol_get_stream_information( mediacontrol_Instance *self,
341                                      mediacontrol_PositionKey a_key,
342                                      mediacontrol_Exception *exception )
343 {
344     mediacontrol_StreamInformation *retval;
345     input_thread_t *p_input = self->p_playlist->p_input;
346     vlc_value_t val;
347
348     retval = ( mediacontrol_StreamInformation* )malloc( sizeof( mediacontrol_StreamInformation ) );
349     if( ! retval )
350     {
351         RAISE( mediacontrol_InternalException, "Out of memory" );
352         return NULL;
353     }
354
355     if( ! p_input )
356     {
357         /* No p_input defined */
358         retval->streamstatus = mediacontrol_UndefinedStatus;
359         retval->url          = strdup( "None" );
360         retval->position     = 0;
361         retval->length       = 0;
362     }
363     else
364     {
365         switch( var_GetInteger( p_input, "state" ) )
366         {
367         case PLAYING_S     :
368             retval->streamstatus = mediacontrol_PlayingStatus;
369             break;
370         case PAUSE_S       :
371             retval->streamstatus = mediacontrol_PauseStatus;
372             break;
373         case INIT_S        :
374             retval->streamstatus = mediacontrol_InitStatus;
375             break;
376         case END_S         :
377             retval->streamstatus = mediacontrol_EndStatus;
378             break;
379         default :
380             retval->streamstatus = mediacontrol_UndefinedStatus;
381             break;
382         }
383
384         retval->url = strdup( p_input->input.p_item->psz_uri );
385
386         /* TIME and LENGTH are in microseconds. We want them in ms */
387         var_Get( p_input, "time", &val);
388         retval->position = val.i_time / 1000;
389
390         var_Get( p_input, "length", &val);
391         retval->length = val.i_time / 1000;
392
393         retval->position = mediacontrol_unit_convert( p_input,
394                                                       mediacontrol_MediaTime, a_key,
395                                                       retval->position );
396         retval->length   = mediacontrol_unit_convert( p_input,
397                                                       mediacontrol_MediaTime, a_key,
398                                                       retval->length );
399     }
400     return retval;
401 }