]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_core.c
Remove vlc_int64_t. Was not even correctly defined.
[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_media_player = libvlc_media_player_new( retval->p_instance, &ex );
73     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
74     return retval;
75 }
76
77 void
78 mediacontrol_exit( mediacontrol_Instance *self )
79 {
80     libvlc_release( self->p_instance );
81 }
82
83 libvlc_instance_t*
84 mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
85 {
86     return self->p_instance;
87 }
88
89 libvlc_media_player_t*
90 mediacontrol_get_media_player( mediacontrol_Instance *self )
91 {
92     return self->p_media_player;
93 }
94
95 mediacontrol_Instance *
96 mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
97                 mediacontrol_Exception *exception )
98 {
99     mediacontrol_Instance* retval;
100     libvlc_exception_t ex;
101
102     libvlc_exception_init( &ex );
103
104     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
105     if( ! retval )
106     {
107         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
108     }
109     retval->p_instance = p_instance;
110     retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
111     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
112     return retval;
113 }
114
115 /**************************************************************************
116  * Playback management
117  **************************************************************************/
118 mediacontrol_Position*
119 mediacontrol_get_media_position( mediacontrol_Instance *self,
120                                  const mediacontrol_PositionOrigin an_origin,
121                                  const mediacontrol_PositionKey a_key,
122                                  mediacontrol_Exception *exception )
123 {
124     mediacontrol_Position* retval = NULL;
125     libvlc_exception_t ex;
126     int64_t pos;
127
128     mediacontrol_exception_init( exception );
129     libvlc_exception_init( &ex );
130
131     retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
132     retval->origin = an_origin;
133     retval->key = a_key;
134
135     if(  an_origin != mediacontrol_AbsolutePosition )
136     {
137         /* Relative or ModuloPosition make no sense */
138         RAISE_NULL( mediacontrol_PositionOriginNotSupported,
139                     "Only absolute position is valid." );
140     }
141
142     /* We are asked for an AbsolutePosition. */
143     pos = libvlc_media_player_get_time( self->p_media_player, &ex );
144
145     if( a_key == mediacontrol_MediaTime )
146     {
147         retval->value = pos;
148     }
149     else
150     {
151         retval->value = private_mediacontrol_unit_convert( self->p_media_player,
152                                                            mediacontrol_MediaTime,
153                                                            a_key,
154                                                            pos );
155     }
156     return retval;
157 }
158
159 /* Sets the media position */
160 void
161 mediacontrol_set_media_position( mediacontrol_Instance *self,
162                                  const mediacontrol_Position * a_position,
163                                  mediacontrol_Exception *exception )
164 {
165     libvlc_exception_t ex;
166     int64_t i_pos;
167
168     libvlc_exception_init( &ex );
169     mediacontrol_exception_init( exception );
170
171     i_pos = private_mediacontrol_position2microsecond( self->p_media_player, a_position );
172     libvlc_media_player_set_time( self->p_media_player, i_pos / 1000, &ex );
173     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
174 }
175
176 /* Starts playing a stream */
177 /*
178  * Known issues: since moving in the playlist using playlist_Next
179  * or playlist_Prev implies starting to play items, the a_position
180  * argument will be only honored for the 1st item in the list.
181  *
182  * XXX:FIXME split moving in the playlist and playing items two
183  * different actions or make playlist_<Next|Prev> accept a time
184  * value to start to play from.
185  */
186 void
187 mediacontrol_start( mediacontrol_Instance *self,
188                     const mediacontrol_Position * a_position,
189                     mediacontrol_Exception *exception )
190 {
191     libvlc_media_t * p_media;
192     char * psz_name;
193     libvlc_exception_t ex;
194
195     mediacontrol_exception_init( exception );
196     libvlc_exception_init( &ex );
197
198     p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
199     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
200
201     if ( ! p_media )
202     {
203         /* No media was defined. */
204         RAISE( mediacontrol_PlaylistException, "No defined media." );
205     }
206     else
207     {
208         /* A media was defined. Get its mrl to reuse it, but reset the options
209            (because start-time may have been set on the previous invocation */
210         psz_name = libvlc_media_get_mrl( p_media, &ex );
211         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
212
213         /* Create a new media */
214         p_media = libvlc_media_new( self->p_instance, psz_name, &ex );
215         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
216
217         if( a_position->value )
218         {
219             char * psz_from;
220             libvlc_time_t i_from;
221
222             /* A start position was specified. Add it to media options */
223             psz_from = ( char * )malloc( 20 * sizeof( char ) );
224             i_from = private_mediacontrol_position2microsecond( self->p_media_player, a_position ) / 1000000;
225             snprintf( psz_from, 20, "start-time=%"PRId64, i_from );
226             libvlc_media_add_option( p_media, psz_from, &ex );
227             HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
228         }
229
230         libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
231         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
232
233         libvlc_media_player_play( self->p_media_player, &ex );
234         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
235     }
236 }
237
238 void
239 mediacontrol_pause( mediacontrol_Instance *self,
240                     mediacontrol_Exception *exception )
241 {
242     libvlc_exception_t ex;
243
244     mediacontrol_exception_init( exception );
245     libvlc_exception_init( &ex );
246     libvlc_media_player_pause( self->p_media_player, &ex );
247     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
248 }
249
250 void
251 mediacontrol_resume( mediacontrol_Instance *self,
252                      mediacontrol_Exception *exception )
253 {
254     libvlc_exception_t ex;
255
256     mediacontrol_exception_init( exception );
257     libvlc_exception_init( &ex );
258     libvlc_media_player_pause( self->p_media_player, &ex );
259     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
260 }
261
262 void
263 mediacontrol_stop( mediacontrol_Instance *self,
264                    mediacontrol_Exception *exception )
265 {
266     libvlc_exception_t ex;
267
268     mediacontrol_exception_init( exception );
269     libvlc_exception_init( &ex );
270     libvlc_media_player_stop( self->p_media_player, &ex );
271     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
272 }
273
274 /**************************************************************************
275  * File management
276  **************************************************************************/
277
278 void
279 mediacontrol_set_mrl( mediacontrol_Instance *self,
280                       const char * psz_file,
281                       mediacontrol_Exception *exception )
282 {
283     libvlc_media_t * p_media;
284     libvlc_exception_t ex;
285
286     mediacontrol_exception_init( exception );
287     libvlc_exception_init( &ex );
288
289     p_media = libvlc_media_new( self->p_instance, psz_file, &ex );
290     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
291
292     libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
293     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
294 }
295
296 char *
297 mediacontrol_get_mrl( mediacontrol_Instance *self,
298                       mediacontrol_Exception *exception )
299 {
300     libvlc_media_t * p_media;
301     libvlc_exception_t ex;
302
303     mediacontrol_exception_init( exception );
304     libvlc_exception_init( &ex );
305
306     p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
307     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
308
309     if ( ! p_media )
310     {
311         return strdup( "" );
312     }
313     else
314     {
315         char * psz_mrl;
316
317         psz_mrl = libvlc_media_get_mrl( p_media, &ex );
318         HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
319         return psz_mrl;
320     }
321 }
322
323 /***************************************************************************
324  * Status feedback
325  ***************************************************************************/
326
327 mediacontrol_StreamInformation *
328 mediacontrol_get_stream_information( mediacontrol_Instance *self,
329                                      mediacontrol_PositionKey a_key,
330                                      mediacontrol_Exception *exception )
331 {
332     mediacontrol_StreamInformation *retval = NULL;
333     libvlc_media_t * p_media;
334     libvlc_exception_t ex;
335
336     libvlc_exception_init( &ex );
337
338     retval = ( mediacontrol_StreamInformation* )
339                             malloc( sizeof( mediacontrol_StreamInformation ) );
340     if( ! retval )
341     {
342         RAISE( mediacontrol_InternalException, "Out of memory" );
343         return NULL;
344     }
345
346     p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
347     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
348     if( ! p_media )
349     {
350         /* No p_media defined */
351         retval->streamstatus = mediacontrol_UndefinedStatus;
352         retval->url          = strdup( "" );
353         retval->position     = 0;
354         retval->length       = 0;
355     }
356     else
357     {
358         libvlc_state_t state;
359
360         state = libvlc_media_player_get_state( self->p_media_player, &ex );
361         HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
362         switch( state )
363         {
364         case libvlc_Playing :
365             retval->streamstatus = mediacontrol_PlayingStatus;
366             break;
367         case libvlc_Paused :
368             retval->streamstatus = mediacontrol_PauseStatus;
369             break;
370         case libvlc_Opening :
371         case libvlc_Buffering:
372             retval->streamstatus = mediacontrol_InitStatus;
373             break;
374         case libvlc_Stopped:
375         case libvlc_Ended:
376             retval->streamstatus = mediacontrol_EndStatus;
377             break;
378         default :
379             retval->streamstatus = mediacontrol_UndefinedStatus;
380             break;
381         }
382
383         retval->url = libvlc_media_get_mrl( p_media, &ex );
384
385         /* TIME and LENGTH are in microseconds. We want them in ms */
386         retval->position = libvlc_media_player_get_time( self->p_media_player, &ex );
387
388         retval->length = libvlc_media_player_get_length( self->p_media_player, &ex );
389
390         retval->position = private_mediacontrol_unit_convert( self->p_media_player,
391                                          mediacontrol_MediaTime, a_key,
392                                          retval->position );
393         retval->length   = private_mediacontrol_unit_convert( self->p_media_player,
394                                          mediacontrol_MediaTime, a_key,
395                                          retval->length );
396     }
397     return retval;
398 }