]> git.sesse.net Git - vlc/blob - src/control/media_instance.c
d0f43196cbb4e4cddcec4a652d5e477d21a364db
[vlc] / src / control / media_instance.c
1 /*****************************************************************************
2  * media_instance.c: Libvlc API Media Instance management functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
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 #include "libvlc_internal.h"
25
26 #include <vlc/libvlc.h>
27 #include <vlc_demux.h>
28 #include <vlc_input.h>
29 #include "libvlc.h"
30
31 static int
32 input_state_changed( vlc_object_t * p_this, char const * psz_cmd,
33                      vlc_value_t oldval, vlc_value_t newval,
34                      void * p_userdata );
35 static int
36 input_position_changed( vlc_object_t * p_this, char const * psz_cmd,
37                      vlc_value_t oldval, vlc_value_t newval,
38                      void * p_userdata );
39 static int
40 input_time_changed( vlc_object_t * p_this, char const * psz_cmd,
41                      vlc_value_t oldval, vlc_value_t newval,
42                      void * p_userdata );
43
44 static const libvlc_state_t vlc_to_libvlc_state_array[] =
45 {
46     [INIT_S]        = libvlc_Opening,
47     [OPENING_S]     = libvlc_Opening,
48     [BUFFERING_S]   = libvlc_Buffering,    
49     [PLAYING_S]     = libvlc_Playing,    
50     [PAUSE_S]       = libvlc_Paused,    
51     [END_S]         = libvlc_Ended,    
52     [ERROR_S]       = libvlc_Error,    
53 };
54 static inline libvlc_state_t vlc_to_libvlc_state( int vlc_state )
55 {
56     if( vlc_state < 0 || vlc_state > 6 )
57         return libvlc_Stopped;
58
59     return vlc_to_libvlc_state_array[vlc_state];
60 }
61
62 /*
63  * Release the associated input thread
64  *
65  * Object lock is NOT held.
66  */
67 static void release_input_thread( libvlc_media_instance_t *p_mi )
68 {
69     input_thread_t *p_input_thread;
70
71     if( !p_mi || p_mi->i_input_id == -1 )
72         return;
73
74     p_input_thread = (input_thread_t*)vlc_object_get( p_mi->i_input_id );
75
76     p_mi->i_input_id = -1;
77
78     if( !p_input_thread )
79         return;
80  
81
82     /* No one is tracking this input_thread appart us. Destroy it */
83     if( p_mi->b_own_its_input_thread )
84     {
85         var_DelCallback( p_input_thread, "state", input_state_changed, p_mi );
86         var_DelCallback( p_input_thread, "seekable", input_state_changed, p_mi );
87         var_DelCallback( p_input_thread, "pausable", input_state_changed, p_mi );
88         var_DelCallback( p_input_thread, "intf-change", input_position_changed, p_mi );
89         var_DelCallback( p_input_thread, "intf-change", input_time_changed, p_mi );
90
91         /* We owned this one */
92         input_StopThread( p_input_thread );
93
94         var_Destroy( p_input_thread, "drawable" );
95     }
96     else
97     {
98         /* XXX: hack the playlist doesn't retain the input thread,
99          * so we did it for the playlist (see _new_from_input_thread),
100          * revert that here. This will be deleted with the playlist API */
101         vlc_object_release( p_input_thread );
102     }
103
104     /* release for previous vlc_object_get */
105     vlc_object_release( p_input_thread );
106 }
107
108 /*
109  * Retrieve the input thread. Be sure to release the object
110  * once you are done with it. (libvlc Internal)
111  *
112  * Object lock is held.
113  */
114 input_thread_t *libvlc_get_input_thread( libvlc_media_instance_t *p_mi,
115                                          libvlc_exception_t *p_e )
116 {
117     input_thread_t *p_input_thread;
118
119     vlc_mutex_lock( &p_mi->object_lock );
120
121     if( !p_mi || p_mi->i_input_id == -1 )
122     {
123         vlc_mutex_unlock( &p_mi->object_lock );
124         RAISENULL( "Input is NULL" );
125     }
126
127     p_input_thread = (input_thread_t*)vlc_object_get( p_mi->i_input_id );
128     if( !p_input_thread )
129     {
130         vlc_mutex_unlock( &p_mi->object_lock );
131         RAISENULL( "Input does not exist" );
132     }
133
134     vlc_mutex_unlock( &p_mi->object_lock );
135     return p_input_thread;
136 }
137
138 /*
139  * input_state_changed (Private) (input var "state" Callback)
140  */
141 static int
142 input_state_changed( vlc_object_t * p_this, char const * psz_cmd,
143                      vlc_value_t oldval, vlc_value_t newval,
144                      void * p_userdata )
145 {
146     VLC_UNUSED(oldval);
147     libvlc_media_instance_t * p_mi = p_userdata;
148     libvlc_event_t event;
149     libvlc_event_type_t type = newval.i_int;
150
151     switch ( type )
152     {
153         case END_S:
154             libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
155             event.type = libvlc_MediaInstanceReachedEnd;
156             break;
157         case PAUSE_S:
158             libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_Playing, NULL);
159             event.type = libvlc_MediaInstancePaused;
160             break;
161         case PLAYING_S:
162             libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_Playing, NULL);
163             event.type = libvlc_MediaInstancePlayed;
164             break;
165         case ERROR_S:
166             libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_Error, NULL);
167             event.type = libvlc_MediaInstanceEncounteredError;
168             break;
169         default:
170             return VLC_SUCCESS;
171     }
172
173     libvlc_event_send( p_mi->p_event_manager, &event );
174     return VLC_SUCCESS;
175 }
176
177 static int
178 input_seakable_changed( vlc_object_t * p_this, char const * psz_cmd,
179                         vlc_value_t oldval, vlc_value_t newval,
180                         void * p_userdata )
181 {
182     VLC_UNUSED(oldval);
183     libvlc_media_instance_t * p_mi = p_userdata;
184     libvlc_event_t event;
185
186     libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
187     event.type = libvlc_MediaInstanceSeekableChanged;
188     event.u.media_instance_seekable_changed.new_seekable = newval.b_bool;
189
190     libvlc_event_send( p_mi->p_event_manager, &event );
191     return VLC_SUCCESS;
192 }
193
194 static int
195 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
196                         vlc_value_t oldval, vlc_value_t newval,
197                         void * p_userdata )
198 {
199     VLC_UNUSED(oldval);
200     libvlc_media_instance_t * p_mi = p_userdata;
201     libvlc_event_t event;
202
203     libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
204     event.type = libvlc_MediaInstancePausableChanged;
205     event.u.media_instance_pausable_changed.new_pausable = newval.b_bool;
206
207     libvlc_event_send( p_mi->p_event_manager, &event );
208     return VLC_SUCCESS;
209 }
210
211 /*
212  * input_position_changed (Private) (input var "intf-change" Callback)
213  */
214 static int
215 input_position_changed( vlc_object_t * p_this, char const * psz_cmd,
216                      vlc_value_t oldval, vlc_value_t newval,
217                      void * p_userdata )
218 {
219     VLC_UNUSED(oldval);
220     libvlc_media_instance_t * p_mi = p_userdata;
221     vlc_value_t val;
222
223     if (!strncmp(psz_cmd, "intf", 4 /* "-change" no need to go further */))
224     {
225         input_thread_t * p_input = (input_thread_t *)p_this;
226
227         var_Get( p_input, "state", &val );
228         if( val.i_int != PLAYING_S )
229             return VLC_SUCCESS; /* Don't send the position while stopped */
230
231         var_Get( p_input, "position", &val );
232     }
233     else
234         val.i_time = newval.i_time;
235
236     libvlc_event_t event;
237     event.type = libvlc_MediaInstancePositionChanged;
238     event.u.media_instance_position_changed.new_position = val.f_float;
239
240     libvlc_event_send( p_mi->p_event_manager, &event );
241     return VLC_SUCCESS;
242 }
243
244 /*
245  * input_time_changed (Private) (input var "intf-change" Callback)
246  */
247 static int
248 input_time_changed( vlc_object_t * p_this, char const * psz_cmd,
249                      vlc_value_t oldval, vlc_value_t newval,
250                      void * p_userdata )
251 {
252     VLC_UNUSED(oldval);
253     libvlc_media_instance_t * p_mi = p_userdata;
254     vlc_value_t val;
255
256     if (!strncmp(psz_cmd, "intf", 4 /* "-change" no need to go further */))
257     {
258         input_thread_t * p_input = (input_thread_t *)p_this;
259     
260         var_Get( p_input, "state", &val );
261         if( val.i_int != PLAYING_S )
262             return VLC_SUCCESS; /* Don't send the position while stopped */
263
264         var_Get( p_input, "time", &val );
265     }
266     else
267         val.i_time = newval.i_time;
268
269     libvlc_event_t event;
270     event.type = libvlc_MediaInstanceTimeChanged;
271     event.u.media_instance_time_changed.new_time = val.i_time;
272     libvlc_event_send( p_mi->p_event_manager, &event );
273     return VLC_SUCCESS;
274 }
275
276 /**************************************************************************
277  * Create a Media Instance object
278  **************************************************************************/
279 libvlc_media_instance_t *
280 libvlc_media_instance_new( libvlc_instance_t * p_libvlc_instance,
281                            libvlc_exception_t * p_e )
282 {
283     libvlc_media_instance_t * p_mi;
284
285     if( !p_libvlc_instance )
286     {
287         libvlc_exception_raise( p_e, "invalid libvlc instance" );
288         return NULL;
289     }
290
291     p_mi = malloc( sizeof(libvlc_media_instance_t) );
292     p_mi->p_md = NULL;
293     p_mi->drawable = 0;
294     p_mi->p_libvlc_instance = p_libvlc_instance;
295     p_mi->i_input_id = -1;
296     /* refcount strategy:
297      * - All items created by _new start with a refcount set to 1
298      * - Accessor _release decrease the refcount by 1, if after that
299      *   operation the refcount is 0, the object is destroyed.
300      * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
301     p_mi->i_refcount = 1;
302     p_mi->b_own_its_input_thread = VLC_TRUE;
303     /* object_lock strategy:
304      * - No lock held in constructor
305      * - Lock when accessing all variable this lock is held
306      * - Lock when attempting to destroy the object the lock is also held */
307     vlc_mutex_init( p_mi->p_libvlc_instance->p_libvlc_int,
308                     &p_mi->object_lock );
309     p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
310             p_libvlc_instance, p_e );
311     if( libvlc_exception_raised( p_e ) )
312     {
313         free( p_mi );
314         return NULL;
315     }
316  
317     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
318             libvlc_MediaInstanceReachedEnd, p_e );
319     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
320             libvlc_MediaInstanceEncounteredError, p_e );
321     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
322             libvlc_MediaInstancePaused, p_e );
323     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
324             libvlc_MediaInstancePlayed, p_e );
325     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
326             libvlc_MediaInstancePositionChanged, p_e );
327     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
328             libvlc_MediaInstanceTimeChanged, p_e );
329     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
330             libvlc_MediaInstanceSeekableChanged, p_e );
331     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
332             libvlc_MediaInstancePausableChanged, p_e );
333
334     return p_mi;
335 }
336
337 /**************************************************************************
338  * Create a Media Instance object with a media descriptor
339  **************************************************************************/
340 libvlc_media_instance_t *
341 libvlc_media_instance_new_from_media_descriptor(
342                                     libvlc_media_descriptor_t * p_md,
343                                     libvlc_exception_t *p_e )
344 {
345     libvlc_media_instance_t * p_mi;
346     p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
347
348     if( !p_mi )
349         return NULL;
350
351     libvlc_media_descriptor_retain( p_md );
352     p_mi->p_md = p_md;
353
354     return p_mi;
355 }
356
357 /**************************************************************************
358  * Create a new media instance object from an input_thread (Libvlc Internal)
359  **************************************************************************/
360 libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
361                                    struct libvlc_instance_t *p_libvlc_instance,
362                                    input_thread_t *p_input,
363                                    libvlc_exception_t *p_e )
364 {
365     libvlc_media_instance_t * p_mi;
366
367     if( !p_input )
368     {
369         libvlc_exception_raise( p_e, "invalid input thread" );
370         return NULL;
371     }
372
373     p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
374
375     if( !p_mi )
376         return NULL;
377
378     p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
379                     p_libvlc_instance,
380                     input_GetItem( p_input ), p_e );
381
382     if( !p_mi->p_md )
383     {
384         libvlc_media_instance_destroy( p_mi );
385         return NULL;
386     }
387
388     p_mi->i_input_id = p_input->i_object_id;
389     p_mi->b_own_its_input_thread = VLC_FALSE;
390
391     /* will be released in media_instance_release() */
392     vlc_object_yield( p_input );
393
394     /* XXX: Hack as the playlist doesn't yield the input thread we retain
395      * the input for the playlist. (see corresponding hack in _release) */
396     vlc_object_yield( p_input );
397
398     return p_mi;
399 }
400
401 /**************************************************************************
402  * Destroy a Media Instance object (libvlc internal)
403  *
404  * Warning: No lock held here, but hey, this is internal.
405  **************************************************************************/
406 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
407 {
408     input_thread_t *p_input_thread;
409     libvlc_exception_t p_e;
410
411     libvlc_exception_init( &p_e );
412
413     if( !p_mi )
414         return;
415
416     p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
417
418     if( libvlc_exception_raised( &p_e ) )
419     {
420         libvlc_event_manager_release( p_mi->p_event_manager );
421         libvlc_exception_clear( &p_e );
422         free( p_mi );
423         return; /* no need to worry about no input thread */
424     }
425     vlc_mutex_destroy( &p_mi->object_lock );
426
427     input_DestroyThread( p_input_thread );
428
429     libvlc_media_descriptor_release( p_mi->p_md );
430
431     free( p_mi );
432 }
433
434 /**************************************************************************
435  * Release a Media Instance object
436  **************************************************************************/
437 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
438 {
439     if( !p_mi )
440         return;
441
442     vlc_mutex_lock( &p_mi->object_lock );
443  
444     p_mi->i_refcount--;
445
446     if( p_mi->i_refcount > 0 )
447     {
448         vlc_mutex_unlock( &p_mi->object_lock );
449         return;
450     }
451     vlc_mutex_unlock( &p_mi->object_lock );
452     vlc_mutex_destroy( &p_mi->object_lock );
453
454     release_input_thread( p_mi );
455
456     libvlc_event_manager_release( p_mi->p_event_manager );
457  
458     libvlc_media_descriptor_release( p_mi->p_md );
459
460     free( p_mi );
461 }
462
463 /**************************************************************************
464  * Retain a Media Instance object
465  **************************************************************************/
466 void libvlc_media_instance_retain( libvlc_media_instance_t *p_mi )
467 {
468     if( !p_mi )
469         return;
470
471     p_mi->i_refcount++;
472 }
473
474 /**************************************************************************
475  * Set the Media descriptor associated with the instance
476  **************************************************************************/
477 void libvlc_media_instance_set_media_descriptor(
478                             libvlc_media_instance_t *p_mi,
479                             libvlc_media_descriptor_t *p_md,
480                             libvlc_exception_t *p_e )
481 {
482     (void)p_e;
483
484     if( !p_mi )
485         return;
486
487     vlc_mutex_lock( &p_mi->object_lock );
488
489     release_input_thread( p_mi );
490
491     if( p_mi->p_md )
492         libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL );
493
494     libvlc_media_descriptor_release( p_mi->p_md );
495
496     if( !p_md )
497     {
498         p_mi->p_md = NULL;
499         vlc_mutex_unlock( &p_mi->object_lock );
500         return; /* It is ok to pass a NULL md */
501     }
502
503     libvlc_media_descriptor_retain( p_md );
504     p_mi->p_md = p_md;
505  
506     /* The policy here is to ignore that we were created using a different
507      * libvlc_instance, because we don't really care */
508     p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
509
510     vlc_mutex_unlock( &p_mi->object_lock );
511 }
512
513 /**************************************************************************
514  * Get the Media descriptor associated with the instance
515  **************************************************************************/
516 libvlc_media_descriptor_t *
517 libvlc_media_instance_get_media_descriptor(
518                             libvlc_media_instance_t *p_mi,
519                             libvlc_exception_t *p_e )
520 {
521     (void)p_e;
522
523     if( !p_mi->p_md )
524         return NULL;
525
526     libvlc_media_descriptor_retain( p_mi->p_md );
527     return p_mi->p_md;
528 }
529
530 /**************************************************************************
531  * Get the event Manager
532  **************************************************************************/
533 libvlc_event_manager_t *
534 libvlc_media_instance_event_manager(
535                             libvlc_media_instance_t *p_mi,
536                             libvlc_exception_t *p_e )
537 {
538     (void)p_e;
539
540     return p_mi->p_event_manager;
541 }
542
543 /**************************************************************************
544  * Play
545  **************************************************************************/
546 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
547                                  libvlc_exception_t *p_e )
548 {
549     input_thread_t * p_input_thread;
550
551     if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
552     {
553         /* A thread alread exists, send it a play message */
554         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
555         vlc_object_release( p_input_thread );
556         return;
557     }
558
559     /* Ignore previous exception */
560     libvlc_exception_clear( p_e );
561
562     vlc_mutex_lock( &p_mi->object_lock );
563  
564     if( !p_mi->p_md )
565     {
566         libvlc_exception_raise( p_e, "no associated media descriptor" );
567         vlc_mutex_unlock( &p_mi->object_lock );
568         return;
569     }
570
571     p_mi->i_input_id = input_Read( p_mi->p_libvlc_instance->p_libvlc_int,
572                                    p_mi->p_md->p_input_item, VLC_FALSE );
573
574     p_input_thread = (input_thread_t*)vlc_object_get( p_mi->i_input_id );
575
576     if( !p_input_thread )
577     {
578         return;
579         vlc_mutex_unlock( &p_mi->object_lock );
580     }
581
582     if( p_mi->drawable )
583     {
584         vlc_value_t val;
585         val.i_int = p_mi->drawable;
586         var_Create( p_input_thread, "drawable", VLC_VAR_DOINHERIT );
587         var_Set( p_input_thread, "drawable", val );
588     }
589     var_AddCallback( p_input_thread, "state", input_state_changed, p_mi );
590     var_AddCallback( p_input_thread, "seekable", input_seekable_changed, p_mi );
591     var_AddCallback( p_input_thread, "pausable", input_pausable_changed, p_mi );
592     var_AddCallback( p_input_thread, "intf-change", input_position_changed, p_mi );
593     var_AddCallback( p_input_thread, "intf-change", input_time_changed, p_mi );
594
595     vlc_object_release( p_input_thread );
596     vlc_mutex_unlock( &p_mi->object_lock );
597 }
598
599 /**************************************************************************
600  * Pause
601  **************************************************************************/
602 void libvlc_media_instance_pause( libvlc_media_instance_t *p_mi,
603                                   libvlc_exception_t *p_e )
604 {
605     input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
606
607     if( !p_input_thread )
608         return;
609
610     int state = var_GetInteger( p_input_thread, "state" );
611
612     if( state == PLAYING_S )
613     {
614         if( libvlc_media_instance_can_pause( p_mi, p_e ) )
615             input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
616         else
617             libvlc_media_instance_stop( p_mi, p_e );
618     }
619     else
620         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
621
622     vlc_object_release( p_input_thread );
623 }
624
625 /**************************************************************************
626  * Stop
627  **************************************************************************/
628 void libvlc_media_instance_stop( libvlc_media_instance_t *p_mi,
629                                  libvlc_exception_t *p_e )
630 {
631     if( p_mi->b_own_its_input_thread )
632         release_input_thread( p_mi ); /* This will stop the input thread */
633     else
634     {
635         input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
636
637         if( !p_input_thread )
638             return;
639
640         input_StopThread( p_input_thread );
641         vlc_object_release( p_input_thread );
642     }
643 }
644
645 /**************************************************************************
646  * Set Drawable
647  **************************************************************************/
648 void libvlc_media_instance_set_drawable( libvlc_media_instance_t *p_mi,
649                                          libvlc_drawable_t drawable,
650                                          libvlc_exception_t *p_e )
651 {
652     (void)p_e;
653     p_mi->drawable = drawable;
654 }
655
656 /**************************************************************************
657  * Get Drawable
658  **************************************************************************/
659 libvlc_drawable_t
660 libvlc_media_instance_get_drawable ( libvlc_media_instance_t *p_mi, libvlc_exception_t *p_e )
661 {
662     (void)p_e;
663     return p_mi->drawable;
664 }
665
666 /**************************************************************************
667  * Getters for stream information
668  **************************************************************************/
669 libvlc_time_t libvlc_media_instance_get_length(
670                              libvlc_media_instance_t *p_mi,
671                              libvlc_exception_t *p_e )
672 {
673     input_thread_t *p_input_thread;
674     vlc_value_t val;
675
676     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
677     if( !p_input_thread )
678         return -1;
679
680     var_Get( p_input_thread, "length", &val );
681     vlc_object_release( p_input_thread );
682
683     return (val.i_time+500LL)/1000LL;
684 }
685
686 libvlc_time_t libvlc_media_instance_get_time(
687                                    libvlc_media_instance_t *p_mi,
688                                    libvlc_exception_t *p_e )
689 {
690     input_thread_t *p_input_thread;
691     vlc_value_t val;
692
693     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
694     if( !p_input_thread )
695         return -1;
696
697     var_Get( p_input_thread , "time", &val );
698     vlc_object_release( p_input_thread );
699     return (val.i_time+500LL)/1000LL;
700 }
701
702 void libvlc_media_instance_set_time(
703                                  libvlc_media_instance_t *p_mi,
704                                  libvlc_time_t time,
705                                  libvlc_exception_t *p_e )
706 {
707     input_thread_t *p_input_thread;
708     vlc_value_t value;
709
710     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
711     if( !p_input_thread )
712         return;
713
714     value.i_time = time*1000LL;
715     var_Set( p_input_thread, "time", value );
716     vlc_object_release( p_input_thread );
717 }
718
719 void libvlc_media_instance_set_position(
720                                 libvlc_media_instance_t *p_mi,
721                                 float position,
722                                 libvlc_exception_t *p_e )
723 {
724     input_thread_t *p_input_thread;
725     vlc_value_t val;
726     val.f_float = position;
727
728     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
729     if( !p_input_thread )
730         return;
731
732     var_Set( p_input_thread, "position", val );
733     vlc_object_release( p_input_thread );
734 }
735
736 float libvlc_media_instance_get_position(
737                                  libvlc_media_instance_t *p_mi,
738                                  libvlc_exception_t *p_e )
739 {
740     input_thread_t *p_input_thread;
741     vlc_value_t val;
742
743     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
744     if( !p_input_thread )
745         return -1.0;
746
747     var_Get( p_input_thread, "position", &val );
748     vlc_object_release( p_input_thread );
749
750     return val.f_float;
751 }
752
753 void libvlc_media_instance_set_chapter(
754                                  libvlc_media_instance_t *p_mi,
755                                  int chapter,
756                                  libvlc_exception_t *p_e )
757 {
758     input_thread_t *p_input_thread;
759     vlc_value_t val;
760     val.i_int = chapter;
761
762     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
763     if( !p_input_thread )
764         return;
765
766     var_Set( p_input_thread, "chapter", val );
767     vlc_object_release( p_input_thread );
768 }
769
770 int libvlc_media_instance_get_chapter(
771                                  libvlc_media_instance_t *p_mi,
772                                  libvlc_exception_t *p_e )
773 {
774     input_thread_t *p_input_thread;
775     vlc_value_t val;
776
777     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
778     if( !p_input_thread )
779         return -1.0;
780
781     var_Get( p_input_thread, "chapter", &val );
782     vlc_object_release( p_input_thread );
783
784     return val.i_int;
785 }
786
787 int libvlc_media_instance_get_chapter_count(
788                                  libvlc_media_instance_t *p_mi,
789                                  libvlc_exception_t *p_e )
790 {
791     input_thread_t *p_input_thread;
792     vlc_value_t val;
793
794     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
795     if( !p_input_thread )
796         return -1.0;
797
798     var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
799     vlc_object_release( p_input_thread );
800
801     return val.i_int;
802 }
803
804 float libvlc_media_instance_get_fps(
805                                  libvlc_media_instance_t *p_mi,
806                                  libvlc_exception_t *p_e)
807 {
808     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
809     double f_fps = 0.0;
810
811     if( p_input_thread )
812     {
813         if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
814             f_fps = 0.0;
815         vlc_object_release( p_input_thread );
816     }
817     return f_fps;
818 }
819
820 vlc_bool_t libvlc_media_instance_will_play(
821                                  libvlc_media_instance_t *p_mi,
822                                  libvlc_exception_t *p_e)
823 {
824     input_thread_t *p_input_thread =
825                             libvlc_get_input_thread ( p_mi, p_e);
826     if ( !p_input_thread )
827         return VLC_FALSE;
828
829     if ( !p_input_thread->b_die && !p_input_thread->b_dead )
830     {
831         vlc_object_release( p_input_thread );
832         return VLC_TRUE;
833     }
834     vlc_object_release( p_input_thread );
835     return VLC_FALSE;
836 }
837
838 void libvlc_media_instance_set_rate(
839                                  libvlc_media_instance_t *p_mi,
840                                  float rate,
841                                  libvlc_exception_t *p_e )
842 {
843     input_thread_t *p_input_thread;
844     vlc_value_t val;
845
846     if( rate <= 0 )
847         RAISEVOID( "Rate value is invalid" );
848
849     val.i_int = 1000.0f/rate;
850
851     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
852     if ( !p_input_thread )
853         return;
854
855     var_Set( p_input_thread, "rate", val );
856     vlc_object_release( p_input_thread );
857 }
858
859 float libvlc_media_instance_get_rate(
860                                  libvlc_media_instance_t *p_mi,
861                                  libvlc_exception_t *p_e )
862 {
863     input_thread_t *p_input_thread;
864     vlc_value_t val;
865
866     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
867     if ( !p_input_thread )
868         return -1.0;
869
870     var_Get( p_input_thread, "rate", &val );
871     vlc_object_release( p_input_thread );
872
873     return (float)1000.0f/val.i_int;
874 }
875
876 libvlc_state_t libvlc_media_instance_get_state(
877                                  libvlc_media_instance_t *p_mi,
878                                  libvlc_exception_t *p_e )
879 {
880     input_thread_t *p_input_thread;
881     vlc_value_t val;
882
883     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
884     if ( !p_input_thread )
885     {
886         /* We do return the right value, no need to throw an exception */
887         if( libvlc_exception_raised( p_e ) )
888             libvlc_exception_clear( p_e );
889         return libvlc_Stopped;
890     }
891
892     var_Get( p_input_thread, "state", &val );
893     vlc_object_release( p_input_thread );
894
895     return vlc_to_libvlc_state(val.i_int);
896 }
897
898 vlc_bool_t libvlc_media_instance_is_seekable(
899                                  libvlc_media_instance_t *p_mi,
900                                  libvlc_exception_t *p_e )
901 {
902     input_thread_t *p_input_thread;
903     vlc_value_t val;
904
905     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
906     if ( !p_input_thread )
907     {
908         /* We do return the right value, no need to throw an exception */
909         if( libvlc_exception_raised( p_e ) )
910             libvlc_exception_clear( p_e );
911         return VLC_FALSE;
912     }
913     var_Get( p_input_thread, "seekable", &val );
914     vlc_object_release( p_input_thread );
915
916     return val.b_bool;
917 }
918
919 vlc_bool_t libvlc_media_instance_can_pause(
920                                  libvlc_media_instance_t *p_mi,
921                                  libvlc_exception_t *p_e )
922 {
923     input_thread_t *p_input_thread;
924     vlc_value_t val;
925
926     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
927     if ( !p_input_thread )
928     {
929         /* We do return the right value, no need to throw an exception */
930         if( libvlc_exception_raised( p_e ) )
931             libvlc_exception_clear( p_e );
932         return VLC_FALSE;
933     }
934     var_Get( p_input_thread, "can-pause", &val );
935     vlc_object_release( p_input_thread );
936
937     return val.b_bool;
938 }