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