]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_core.c
cast to (const char**)
[vlc] / src / control / mediacontrol_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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc/vlc.h>
29 #include <vlc/mediacontrol.h>
30
31 #include <vlc/libvlc.h>
32 #include <vlc_interface.h>
33 #include <vlc_playlist.h>
34
35 #include <vlc_vout.h>
36 #include <vlc_aout.h>
37 #include <vlc_input.h>
38 #include <vlc_osd.h>
39 #include "mediacontrol_internal.h"
40
41 #include <stdlib.h>                                      /* malloc(), free() */
42 #include <string.h>
43
44 #include <errno.h>                                                 /* ENOMEM */
45 #include <stdio.h>
46 #include <ctype.h>
47
48 #ifdef HAVE_UNISTD_H
49 #    include <unistd.h>
50 #endif
51 #ifdef HAVE_SYS_TIME_H
52 #    include <sys/time.h>
53 #endif
54 #ifdef HAVE_SYS_TYPES_H
55 #    include <sys/types.h>
56 #endif
57
58 mediacontrol_Instance* mediacontrol_new( int argc, char** argv, mediacontrol_Exception *exception )
59 {
60     mediacontrol_Instance* retval;
61     libvlc_exception_t ex;
62
63     libvlc_exception_init( &ex );
64     mediacontrol_exception_init( exception );
65
66     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
67     if( !retval )
68         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
69
70     retval->p_instance = libvlc_new( argc, (const char**)argv, &ex );
71     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
72     retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
73     return retval;
74 }
75
76 void
77 mediacontrol_exit( mediacontrol_Instance *self )
78 {
79     libvlc_exception_t ex;
80     libvlc_exception_init( &ex );
81
82     libvlc_release( self->p_instance, &ex );
83 }
84
85 libvlc_instance_t*
86 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
87 {
88   return self->p_instance;
89 }
90
91 mediacontrol_Instance *
92 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
93                 mediacontrol_Exception *exception )
94 {
95   mediacontrol_Instance* retval;
96
97   retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
98   if( ! retval )
99   {
100       RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
101   }
102   retval->p_instance = p_instance;
103   retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
104   return retval;
105 }
106
107 /**************************************************************************
108  * Playback management
109  **************************************************************************/
110 mediacontrol_Position*
111 mediacontrol_get_media_position( mediacontrol_Instance *self,
112                                  const mediacontrol_PositionOrigin an_origin,
113                                  const mediacontrol_PositionKey a_key,
114                                  mediacontrol_Exception *exception )
115 {
116     mediacontrol_Position* retval = NULL;
117     libvlc_exception_t ex;
118     vlc_int64_t pos;
119     libvlc_media_instance_t * p_mi;
120
121     mediacontrol_exception_init( exception );
122     libvlc_exception_init( &ex );
123
124     retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
125     retval->origin = an_origin;
126     retval->key = a_key;
127
128     p_mi = libvlc_playlist_get_media_instance( self->p_instance, &ex);
129     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
130
131     if(  an_origin != mediacontrol_AbsolutePosition )
132     {
133         libvlc_media_instance_release( p_mi );
134         /* Relative or ModuloPosition make no sense */
135         RAISE_NULL( mediacontrol_PositionOriginNotSupported,
136                     "Only absolute position is valid." );
137     }
138
139     /* We are asked for an AbsolutePosition. */
140     pos = libvlc_media_instance_get_time( p_mi, &ex );
141
142     if( a_key == mediacontrol_MediaTime )
143     {
144         retval->value = pos;
145     }
146     else
147     {
148         if( ! self->p_playlist->p_input )
149         {
150             libvlc_media_instance_release( p_mi );
151             RAISE_NULL( mediacontrol_InternalException,
152                         "No input" );
153         }
154         retval->value = private_mediacontrol_unit_convert( self->p_playlist->p_input,
155                                                    mediacontrol_MediaTime,
156                                                    a_key,
157                                                    pos );
158     }
159     libvlc_media_instance_release( p_mi );
160     return retval;
161 }
162
163 /* Sets the media position */
164 void
165 mediacontrol_set_media_position( mediacontrol_Instance *self,
166                                  const mediacontrol_Position * a_position,
167                                  mediacontrol_Exception *exception )
168 {
169     libvlc_media_instance_t * p_mi;
170     libvlc_exception_t ex;
171     vlc_int64_t i_pos;
172
173     libvlc_exception_init( &ex );
174     mediacontrol_exception_init( exception );
175
176     p_mi = libvlc_playlist_get_media_instance( self->p_instance, &ex);
177     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
178
179     i_pos = private_mediacontrol_position2microsecond( self->p_playlist->p_input, a_position );
180     libvlc_media_instance_set_time( p_mi, i_pos / 1000, &ex );
181     libvlc_media_instance_release( p_mi );
182     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
183 }
184
185 /* Starts playing a stream */
186 /*
187  * Known issues: since moving in the playlist using playlist_Next
188  * or playlist_Prev implies starting to play items, the a_position
189  * argument will be only honored for the 1st item in the list.
190  *
191  * XXX:FIXME split moving in the playlist and playing items two
192  * different actions or make playlist_<Next|Prev> accept a time
193  * value to start to play from.
194  */
195 void
196 mediacontrol_start( mediacontrol_Instance *self,
197                     const mediacontrol_Position * a_position,
198                     mediacontrol_Exception *exception )
199 {
200     playlist_t * p_playlist = self->p_playlist;
201
202     mediacontrol_exception_init( exception );
203     if( ! p_playlist )
204     {
205         RAISE( mediacontrol_PlaylistException, "No available playlist" );
206         return;
207     }
208
209     vlc_mutex_lock( &p_playlist->object_lock );
210     if( p_playlist->items.i_size )
211     {
212         int i_from;
213         char *psz_from = NULL;
214
215         psz_from = ( char * )malloc( 20 * sizeof( char ) );
216         if( psz_from && p_playlist->status.p_item )
217         {
218             i_from = private_mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
219
220             /* Set start time */
221             snprintf( psz_from, 20, "start-time=%i", i_from );
222             input_ItemAddOption( p_playlist->status.p_item->p_input, psz_from );
223             free( psz_from );
224         }
225
226         vlc_mutex_unlock( &p_playlist->object_lock );
227         playlist_Play( p_playlist );
228     }
229     else
230     {
231         RAISE( mediacontrol_PlaylistException, "Empty playlist." );
232         vlc_mutex_unlock( &p_playlist->object_lock );
233     }
234 }
235
236 void
237 mediacontrol_pause( mediacontrol_Instance *self,
238                     const mediacontrol_Position * a_position,
239                     mediacontrol_Exception *exception )
240 {
241     input_thread_t *p_input = self->p_playlist->p_input;
242
243     /* FIXME: use the a_position parameter */
244     mediacontrol_exception_init( exception );
245     if( p_input != NULL )
246     {
247         var_SetInteger( p_input, "state", PAUSE_S );
248     }
249     else
250     {
251         RAISE( mediacontrol_InternalException, "No input" );
252     }
253 }
254
255 void
256 mediacontrol_resume( mediacontrol_Instance *self,
257                      const mediacontrol_Position * a_position,
258                      mediacontrol_Exception *exception )
259 {
260     input_thread_t *p_input = self->p_playlist->p_input;
261
262     /* FIXME: use the a_position parameter */
263     mediacontrol_exception_init( exception );
264     if( p_input != NULL )
265     {
266         var_SetInteger( p_input, "state", PAUSE_S );
267     }
268     else
269     {
270         RAISE( mediacontrol_InternalException, "No input" );
271     }
272 }
273
274 void
275 mediacontrol_stop( mediacontrol_Instance *self,
276                    const mediacontrol_Position * a_position,
277                    mediacontrol_Exception *exception )
278 {
279     /* FIXME: use the a_position parameter */
280     mediacontrol_exception_init( exception );
281     if( !self->p_playlist )
282     {
283         RAISE( mediacontrol_PlaylistException, "No playlist" );
284     }
285     else
286         playlist_Stop( self->p_playlist );
287 }
288
289 /**************************************************************************
290  * Playlist management
291  **************************************************************************/
292
293 void
294 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
295                                 const char * psz_file,
296                                 mediacontrol_Exception *exception )
297 {
298     libvlc_exception_t ex;
299
300     mediacontrol_exception_init( exception );
301     libvlc_exception_init( &ex );
302
303     libvlc_playlist_add( self->p_instance, psz_file, psz_file, &ex );
304     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
305 }
306
307 void
308 mediacontrol_playlist_next_item( mediacontrol_Instance *self,
309                                  mediacontrol_Exception *exception )
310 {
311     libvlc_exception_t ex;
312
313     mediacontrol_exception_init( exception );
314     libvlc_exception_init( &ex );
315
316     libvlc_playlist_next( self->p_instance, &ex );
317     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
318 }
319
320 void
321 mediacontrol_playlist_clear( mediacontrol_Instance *self,
322                              mediacontrol_Exception *exception )
323 {
324     libvlc_exception_t ex;
325
326     mediacontrol_exception_init( exception );
327     libvlc_exception_init( &ex );
328
329     libvlc_playlist_clear( self->p_instance, &ex );
330     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
331 }
332
333 mediacontrol_PlaylistSeq *
334 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
335                                 mediacontrol_Exception *exception )
336 {
337     mediacontrol_PlaylistSeq *retval = NULL;
338     int i_index;
339     playlist_t * p_playlist = self->p_playlist;
340     playlist_item_t *p_root;
341     int i_playlist_size;
342
343     mediacontrol_exception_init( exception );
344     if( !p_playlist )
345     {
346         RAISE( mediacontrol_PlaylistException, "No playlist" );
347         return NULL;
348     }
349
350     vlc_mutex_lock( &p_playlist->object_lock );
351
352     p_root = p_playlist->p_root_onelevel->pp_children[0];
353     i_playlist_size = p_root->i_children;
354     retval = private_mediacontrol_PlaylistSeq__alloc( i_playlist_size );
355
356     for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
357     {
358         retval->data[i_index] = strdup( p_root->pp_children[i_index]->p_input->psz_name );
359     }
360     vlc_mutex_unlock( &p_playlist->object_lock );
361
362     return retval;
363 }
364
365 /***************************************************************************
366  * Status feedback
367  ***************************************************************************/
368
369 mediacontrol_StreamInformation *
370 mediacontrol_get_stream_information( mediacontrol_Instance *self,
371                                      mediacontrol_PositionKey a_key,
372                                      mediacontrol_Exception *exception )
373 {
374     mediacontrol_StreamInformation *retval = NULL;
375     input_thread_t *p_input = self->p_playlist->p_input;
376     vlc_value_t val;
377
378     retval = ( mediacontrol_StreamInformation* )
379                             malloc( sizeof( mediacontrol_StreamInformation ) );
380     if( ! retval )
381     {
382         RAISE( mediacontrol_InternalException, "Out of memory" );
383         return NULL;
384     }
385
386     if( ! p_input )
387     {
388         /* No p_input defined */
389         retval->streamstatus = mediacontrol_UndefinedStatus;
390         retval->url          = strdup( "None" );
391         retval->position     = 0;
392         retval->length       = 0;
393     }
394     else
395     {
396         switch( var_GetInteger( p_input, "state" ) )
397         {
398         case PLAYING_S     :
399             retval->streamstatus = mediacontrol_PlayingStatus;
400             break;
401         case PAUSE_S       :
402             retval->streamstatus = mediacontrol_PauseStatus;
403             break;
404         case INIT_S        :
405             retval->streamstatus = mediacontrol_InitStatus;
406             break;
407         case END_S         :
408             retval->streamstatus = mediacontrol_EndStatus;
409             break;
410         default :
411             retval->streamstatus = mediacontrol_UndefinedStatus;
412             break;
413         }
414
415         retval->url = input_item_GetURI( input_GetItem( p_input ) );
416
417         /* TIME and LENGTH are in microseconds. We want them in ms */
418         var_Get( p_input, "time", &val);
419         retval->position = val.i_time / 1000;
420
421         var_Get( p_input, "length", &val);
422         retval->length = val.i_time / 1000;
423
424         retval->position = private_mediacontrol_unit_convert( p_input,
425                                          mediacontrol_MediaTime, a_key,
426                                          retval->position );
427         retval->length   = private_mediacontrol_unit_convert( p_input,
428                                          mediacontrol_MediaTime, a_key,
429                                          retval->length );
430     }
431     return retval;
432 }