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