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