]> git.sesse.net Git - vlc/blob - src/control/core.c
src/control/core.c, include/vlc/control.h: Applied patch from Filippo Carone,
[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
60 mediacontrol_Instance* mediacontrol_new_from_object( int vlc_object_id,
61                                                      mediacontrol_Exception *exception )
62 {
63     mediacontrol_Instance* retval;
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 available interface" );
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;
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         /*
120            RAISE( mediacontrol_InternalException, "No input thread." );
121            return( NULL );
122         */
123         retval->value = 0;
124         return retval;
125     }
126
127     if(  an_origin == mediacontrol_RelativePosition
128          || an_origin == mediacontrol_ModuloPosition )
129     {
130         /* Relative or ModuloPosition make no sense */
131         retval->value = 0;
132         return retval;
133     }
134
135     /* We are asked for an AbsolutePosition. */
136     val.i_time = 0;
137     var_Get( p_input, "time", &val );
138     /* FIXME: check val.i_time > 0 */
139
140     retval->value = mediacontrol_unit_convert( p_input,
141                                                mediacontrol_MediaTime,
142                                                a_key,
143                                                val.i_time / 1000 );
144     return retval;
145 }
146
147 /* Sets the media position */
148 void
149 mediacontrol_set_media_position( mediacontrol_Instance *self,
150                                  const mediacontrol_Position * a_position,
151                                  mediacontrol_Exception *exception )
152 {
153     vlc_value_t val;
154     input_thread_t * p_input = self->p_playlist->p_input;
155
156     exception=mediacontrol_exception_init( exception );
157     if( ! p_input )
158     {
159         RAISE( mediacontrol_InternalException, "No input thread." );
160         return;
161     }
162
163     if( !var_GetBool( p_input, "seekable" ) )
164     {
165         RAISE( mediacontrol_InvalidPosition, "Stream not seekable" );
166         return;
167     }
168
169     val.i_time = mediacontrol_position2microsecond( p_input, a_position );
170     var_Set( p_input, "time", val );
171     return;
172 }
173
174 /* Starts playing a stream */
175 void
176 mediacontrol_start( mediacontrol_Instance *self,
177                     const mediacontrol_Position * a_position,
178                     mediacontrol_Exception *exception )
179 {
180     playlist_t * p_playlist = self->p_playlist;
181
182     exception = mediacontrol_exception_init( exception );
183     if( ! p_playlist )
184     {
185         RAISE( mediacontrol_PlaylistException, "No available playlist" );
186         return;
187     }
188
189     vlc_mutex_lock( &p_playlist->object_lock );
190     if( p_playlist->i_size )
191     {
192         vlc_value_t val;
193
194         vlc_mutex_unlock( &p_playlist->object_lock );
195
196         /* Set start time */
197         val.i_int = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
198         var_Set( p_playlist, "start-time", val );
199
200         playlist_Play( p_playlist );
201     }
202     else
203     {
204         RAISE( mediacontrol_PlaylistException, "Empty playlist." );
205         vlc_mutex_unlock( &p_playlist->object_lock );
206         return;
207     }
208
209     return;
210 }
211
212 void
213 mediacontrol_pause( mediacontrol_Instance *self,
214                     const mediacontrol_Position * a_position,
215                     mediacontrol_Exception *exception )
216 {
217     input_thread_t *p_input = self->p_playlist->p_input;;
218
219     /* FIXME: use the a_position parameter */
220     exception=mediacontrol_exception_init( exception );
221     if( p_input != NULL )
222     {
223         var_SetInteger( p_input, "state", PAUSE_S );
224     }
225     else
226     {
227         RAISE( mediacontrol_InternalException, "No input" );
228     }
229
230     return;
231 }
232
233 void
234 mediacontrol_resume( mediacontrol_Instance *self,
235                      const mediacontrol_Position * a_position,
236                      mediacontrol_Exception *exception )
237 {
238     input_thread_t *p_input = self->p_playlist->p_input;
239
240     /* FIXME: use the a_position parameter */
241     exception=mediacontrol_exception_init( exception );
242     if( p_input != NULL )
243     {
244         var_SetInteger( p_input, "state", PAUSE_S );
245     }
246     else
247     {
248         RAISE( mediacontrol_InternalException, "No input" );
249     }
250 }
251
252 void
253 mediacontrol_stop( mediacontrol_Instance *self,
254                    const mediacontrol_Position * a_position,
255                    mediacontrol_Exception *exception )
256 {
257     /* FIXME: use the a_position parameter */
258     exception=mediacontrol_exception_init( exception );
259     if( !self->p_playlist )
260     {
261         RAISE( mediacontrol_PlaylistException, "No playlist" );
262         return;
263     }
264
265     playlist_Stop( self->p_playlist );
266 }
267
268 /**************************************************************************
269  * Playlist management
270  **************************************************************************/
271
272 void
273 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
274                                 const char * psz_file,
275                                 mediacontrol_Exception *exception )
276 {
277     exception=mediacontrol_exception_init( exception );
278     if( !self->p_playlist )
279     {
280         RAISE( mediacontrol_InternalException, "No playlist" );
281         return;
282     }
283
284     playlist_Add( self->p_playlist, psz_file, psz_file , PLAYLIST_INSERT,
285                   PLAYLIST_END );
286 }
287
288 void
289 mediacontrol_playlist_clear( mediacontrol_Instance *self,
290                              mediacontrol_Exception *exception )
291 {
292     exception=mediacontrol_exception_init( exception );
293     if( !self->p_playlist )
294     {
295         RAISE( mediacontrol_PlaylistException, "No playlist" );
296         return;
297     }
298
299     playlist_Clear( self->p_playlist );
300
301     return;
302 }
303
304 mediacontrol_PlaylistSeq *
305 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
306                                 mediacontrol_Exception *exception )
307 {
308     mediacontrol_PlaylistSeq *retval;
309     int i_index;
310     playlist_t * p_playlist = self->p_playlist;
311     int i_playlist_size;
312
313     exception=mediacontrol_exception_init( exception );
314     if( !p_playlist )
315     {
316         RAISE( mediacontrol_PlaylistException, "No playlist" );
317         return NULL;
318     }
319
320     vlc_mutex_lock( &p_playlist->object_lock );
321     i_playlist_size = p_playlist->i_size;
322
323     retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
324
325     for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
326     {
327         retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->input.psz_uri );
328     }
329     vlc_mutex_unlock( &p_playlist->object_lock );
330
331     return retval;
332 }
333
334 /***************************************************************************
335  * Status feedback
336  ***************************************************************************/
337
338 mediacontrol_StreamInformation *
339 mediacontrol_get_stream_information( mediacontrol_Instance *self,
340                                      mediacontrol_PositionKey a_key,
341                                      mediacontrol_Exception *exception )
342 {
343     mediacontrol_StreamInformation *retval;
344     input_thread_t *p_input = self->p_playlist->p_input;
345     vlc_value_t val;
346
347     retval = ( mediacontrol_StreamInformation* )malloc( sizeof( mediacontrol_StreamInformation ) );
348     if( ! retval )
349     {
350         RAISE( mediacontrol_InternalException, "Out of memory" );
351         return NULL;
352     }
353
354     if( ! p_input )
355     {
356         /* No p_input defined */
357         retval->streamstatus = mediacontrol_UndefinedStatus;
358         retval->url          = strdup( "None" );
359         retval->position     = 0;
360         retval->length       = 0;
361     }
362     else
363     {
364         switch( var_GetInteger( p_input, "state" ) )
365         {
366         case PLAYING_S     :
367             retval->streamstatus = mediacontrol_PlayingStatus;
368             break;
369         case PAUSE_S       :
370             retval->streamstatus = mediacontrol_PauseStatus;
371             break;
372         case INIT_S        :
373             retval->streamstatus = mediacontrol_InitStatus;
374             break;
375         case END_S         :
376             retval->streamstatus = mediacontrol_EndStatus;
377             break;
378         default :
379             retval->streamstatus = mediacontrol_UndefinedStatus;
380             break;
381         }
382
383         retval->url = strdup( p_input->input.p_item->psz_uri );
384
385         /* TIME and LENGTH are in microseconds. We want them in ms */
386         var_Get( p_input, "time", &val);
387         retval->position = val.i_time / 1000;
388
389         var_Get( p_input, "length", &val);
390         retval->length = val.i_time / 1000;
391
392         retval->position = mediacontrol_unit_convert( p_input,
393                                                       mediacontrol_MediaTime, a_key,
394                                                       retval->position );
395         retval->length   = mediacontrol_unit_convert( p_input,
396                                                       mediacontrol_MediaTime, a_key,
397                                                       retval->length );
398     }
399     return retval;
400 }