]> git.sesse.net Git - vlc/blob - src/control/media_instance.c
afcdfc2f8187947f65243b0cec11fab0fb067c0c
[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_seekable_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_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
41                         vlc_value_t oldval, vlc_value_t newval,
42                         void * p_userdata );
43 static int
44 input_position_changed( vlc_object_t * p_this, char const * psz_cmd,
45                      vlc_value_t oldval, vlc_value_t newval,
46                      void * p_userdata );
47 static int
48 input_time_changed( vlc_object_t * p_this, char const * psz_cmd,
49                      vlc_value_t oldval, vlc_value_t newval,
50                      void * p_userdata );
51
52 static const libvlc_state_t vlc_to_libvlc_state_array[] =
53 {
54     [INIT_S]        = libvlc_Opening,
55     [OPENING_S]     = libvlc_Opening,
56     [BUFFERING_S]   = libvlc_Buffering,    
57     [PLAYING_S]     = libvlc_Playing,    
58     [PAUSE_S]       = libvlc_Paused,    
59     [END_S]         = libvlc_Ended,    
60     [ERROR_S]       = libvlc_Error,    
61 };
62 static inline libvlc_state_t vlc_to_libvlc_state( int vlc_state )
63 {
64     if( vlc_state < 0 || vlc_state > 6 )
65         return libvlc_Stopped;
66
67     return vlc_to_libvlc_state_array[vlc_state];
68 }
69
70 /*
71  * Release the associated input thread
72  *
73  * Object lock is NOT held.
74  */
75 static void release_input_thread( libvlc_media_instance_t *p_mi )
76 {
77     input_thread_t * p_input_thread;
78
79     if( !p_mi || !p_mi->p_input_thread )
80         return;
81
82     p_input_thread = p_mi->p_input_thread;
83
84     /* No one is tracking this input_thread appart us. Destroy it */
85     if( p_mi->b_own_its_input_thread )
86     {
87         var_DelCallback( p_input_thread, "state", input_state_changed, p_mi );
88         var_DelCallback( p_input_thread, "seekable", input_seekable_changed, p_mi );
89         var_DelCallback( p_input_thread, "pausable", input_pausable_changed, p_mi );
90         var_DelCallback( p_input_thread, "intf-change", input_position_changed, p_mi );
91         var_DelCallback( p_input_thread, "intf-change", input_time_changed, p_mi );
92
93         /* We owned this one */
94         input_StopThread( p_input_thread );
95         input_DestroyThread( p_input_thread );
96
97         var_Destroy( p_input_thread, "drawable" );
98     }
99     else
100         vlc_object_release( p_input_thread );
101
102     p_mi->p_input_thread = NULL;
103 }
104
105 /*
106  * Retrieve the input thread. Be sure to release the object
107  * once you are done with it. (libvlc Internal)
108  *
109  * Object lock is held.
110  */
111 input_thread_t *libvlc_get_input_thread( libvlc_media_instance_t *p_mi,
112                                          libvlc_exception_t *p_e )
113 {
114     input_thread_t *p_input_thread;
115
116     if( !p_mi ) RAISENULL( "Media Instance is NULL" );
117     
118     vlc_mutex_lock( &p_mi->object_lock );
119
120     if( !p_mi->p_input_thread )
121     {
122         vlc_mutex_unlock( &p_mi->object_lock );
123         RAISENULL( "Input is NULL" );
124     }
125
126     p_input_thread = p_mi->p_input_thread;
127     vlc_object_yield( p_input_thread );
128
129     vlc_mutex_unlock( &p_mi->object_lock );
130
131     return p_input_thread;
132 }
133
134 /*
135  * input_state_changed (Private) (input var "state" Callback)
136  */
137 static int
138 input_state_changed( vlc_object_t * p_this, char const * psz_cmd,
139                      vlc_value_t oldval, vlc_value_t newval,
140                      void * p_userdata )
141 {
142     VLC_UNUSED(oldval);
143     VLC_UNUSED(p_this);
144     VLC_UNUSED(psz_cmd);
145     libvlc_media_instance_t * p_mi = p_userdata;
146     libvlc_event_t event;
147     libvlc_event_type_t type = newval.i_int;
148
149     switch ( type )
150     {
151         case END_S:
152             libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
153             event.type = libvlc_MediaInstanceReachedEnd;
154             break;
155         case PAUSE_S:
156             libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_Playing, NULL);
157             event.type = libvlc_MediaInstancePaused;
158             break;
159         case PLAYING_S:
160             libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_Playing, NULL);
161             event.type = libvlc_MediaInstancePlayed;
162             break;
163         case ERROR_S:
164             libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_Error, NULL);
165             event.type = libvlc_MediaInstanceEncounteredError;
166             break;
167         default:
168             return VLC_SUCCESS;
169     }
170
171     libvlc_event_send( p_mi->p_event_manager, &event );
172     return VLC_SUCCESS;
173 }
174
175 static int
176 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
177                         vlc_value_t oldval, vlc_value_t newval,
178                         void * p_userdata )
179 {
180     VLC_UNUSED(oldval);
181     VLC_UNUSED(p_this);
182     VLC_UNUSED(psz_cmd);
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     VLC_UNUSED(p_this);
201     VLC_UNUSED(psz_cmd);
202     libvlc_media_instance_t * p_mi = p_userdata;
203     libvlc_event_t event;
204
205     libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
206     event.type = libvlc_MediaInstancePausableChanged;
207     event.u.media_instance_pausable_changed.new_pausable = newval.b_bool;
208
209     libvlc_event_send( p_mi->p_event_manager, &event );
210     return VLC_SUCCESS;
211 }
212
213 /*
214  * input_position_changed (Private) (input var "intf-change" Callback)
215  */
216 static int
217 input_position_changed( vlc_object_t * p_this, char const * psz_cmd,
218                      vlc_value_t oldval, vlc_value_t newval,
219                      void * p_userdata )
220 {
221     VLC_UNUSED(oldval);
222     libvlc_media_instance_t * p_mi = p_userdata;
223     vlc_value_t val;
224
225     if (!strncmp(psz_cmd, "intf", 4 /* "-change" no need to go further */))
226     {
227         input_thread_t * p_input = (input_thread_t *)p_this;
228
229         var_Get( p_input, "state", &val );
230         if( val.i_int != PLAYING_S )
231             return VLC_SUCCESS; /* Don't send the position while stopped */
232
233         var_Get( p_input, "position", &val );
234     }
235     else
236         val.i_time = newval.i_time;
237
238     libvlc_event_t event;
239     event.type = libvlc_MediaInstancePositionChanged;
240     event.u.media_instance_position_changed.new_position = val.f_float;
241
242     libvlc_event_send( p_mi->p_event_manager, &event );
243     return VLC_SUCCESS;
244 }
245
246 /*
247  * input_time_changed (Private) (input var "intf-change" Callback)
248  */
249 static int
250 input_time_changed( vlc_object_t * p_this, char const * psz_cmd,
251                      vlc_value_t oldval, vlc_value_t newval,
252                      void * p_userdata )
253 {
254     VLC_UNUSED(oldval);
255     libvlc_media_instance_t * p_mi = p_userdata;
256     vlc_value_t val;
257
258     if (!strncmp(psz_cmd, "intf", 4 /* "-change" no need to go further */))
259     {
260         input_thread_t * p_input = (input_thread_t *)p_this;
261     
262         var_Get( p_input, "state", &val );
263         if( val.i_int != PLAYING_S )
264             return VLC_SUCCESS; /* Don't send the position while stopped */
265
266         var_Get( p_input, "time", &val );
267     }
268     else
269         val.i_time = newval.i_time;
270
271     libvlc_event_t event;
272     event.type = libvlc_MediaInstanceTimeChanged;
273     event.u.media_instance_time_changed.new_time = val.i_time;
274     libvlc_event_send( p_mi->p_event_manager, &event );
275     return VLC_SUCCESS;
276 }
277
278 /**************************************************************************
279  * Create a Media Instance object
280  **************************************************************************/
281 libvlc_media_instance_t *
282 libvlc_media_instance_new( libvlc_instance_t * p_libvlc_instance,
283                            libvlc_exception_t * p_e )
284 {
285     libvlc_media_instance_t * p_mi;
286
287     if( !p_libvlc_instance )
288     {
289         libvlc_exception_raise( p_e, "invalid libvlc instance" );
290         return NULL;
291     }
292
293     p_mi = malloc( sizeof(libvlc_media_instance_t) );
294     p_mi->p_md = NULL;
295     p_mi->drawable = 0;
296     p_mi->p_libvlc_instance = p_libvlc_instance;
297     p_mi->p_input_thread = NULL;
298     /* refcount strategy:
299      * - All items created by _new start with a refcount set to 1
300      * - Accessor _release decrease the refcount by 1, if after that
301      *   operation the refcount is 0, the object is destroyed.
302      * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
303     p_mi->i_refcount = 1;
304     p_mi->b_own_its_input_thread = VLC_TRUE;
305     /* object_lock strategy:
306      * - No lock held in constructor
307      * - Lock when accessing all variable this lock is held
308      * - Lock when attempting to destroy the object the lock is also held */
309     vlc_mutex_init( p_mi->p_libvlc_instance->p_libvlc_int,
310                     &p_mi->object_lock );
311     p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
312             p_libvlc_instance, p_e );
313     if( libvlc_exception_raised( p_e ) )
314     {
315         free( p_mi );
316         return NULL;
317     }
318  
319     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
320             libvlc_MediaInstanceReachedEnd, p_e );
321     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
322             libvlc_MediaInstanceEncounteredError, p_e );
323     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
324             libvlc_MediaInstancePaused, p_e );
325     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
326             libvlc_MediaInstancePlayed, p_e );
327     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
328             libvlc_MediaInstancePositionChanged, p_e );
329     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
330             libvlc_MediaInstanceTimeChanged, p_e );
331     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
332             libvlc_MediaInstanceSeekableChanged, p_e );
333     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
334             libvlc_MediaInstancePausableChanged, p_e );
335
336     return p_mi;
337 }
338
339 /**************************************************************************
340  * Create a Media Instance object with a media descriptor
341  **************************************************************************/
342 libvlc_media_instance_t *
343 libvlc_media_instance_new_from_media_descriptor(
344                                     libvlc_media_descriptor_t * p_md,
345                                     libvlc_exception_t *p_e )
346 {
347     libvlc_media_instance_t * p_mi;
348     p_mi = libvlc_media_instance_new( p_md->p_libvlc_instance, p_e );
349
350     if( !p_mi )
351         return NULL;
352
353     libvlc_media_descriptor_retain( p_md );
354     p_mi->p_md = p_md;
355
356     return p_mi;
357 }
358
359 /**************************************************************************
360  * Create a new media instance object from an input_thread (Libvlc Internal)
361  **************************************************************************/
362 libvlc_media_instance_t * libvlc_media_instance_new_from_input_thread(
363                                    struct libvlc_instance_t *p_libvlc_instance,
364                                    input_thread_t *p_input,
365                                    libvlc_exception_t *p_e )
366 {
367     libvlc_media_instance_t * p_mi;
368
369     if( !p_input )
370     {
371         libvlc_exception_raise( p_e, "invalid input thread" );
372         return NULL;
373     }
374
375     p_mi = libvlc_media_instance_new( p_libvlc_instance, p_e );
376
377     if( !p_mi )
378         return NULL;
379
380     p_mi->p_md = libvlc_media_descriptor_new_from_input_item(
381                     p_libvlc_instance,
382                     input_GetItem( p_input ), p_e );
383
384     if( !p_mi->p_md )
385     {
386         libvlc_media_instance_destroy( p_mi );
387         return NULL;
388     }
389
390     /* will be released in media_instance_release() */
391     vlc_object_yield( p_input );
392
393     p_mi->p_input_thread = p_input;
394     p_mi->b_own_its_input_thread = VLC_FALSE;
395
396     return p_mi;
397 }
398
399 /**************************************************************************
400  * Destroy a Media Instance object (libvlc internal)
401  *
402  * Warning: No lock held here, but hey, this is internal.
403  **************************************************************************/
404 void libvlc_media_instance_destroy( libvlc_media_instance_t *p_mi )
405 {
406     input_thread_t *p_input_thread;
407     libvlc_exception_t p_e;
408
409     libvlc_exception_init( &p_e );
410
411     if( !p_mi )
412         return;
413
414     p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
415
416     if( libvlc_exception_raised( &p_e ) )
417     {
418         libvlc_event_manager_release( p_mi->p_event_manager );
419         libvlc_exception_clear( &p_e );
420         free( p_mi );
421         return; /* no need to worry about no input thread */
422     }
423     vlc_mutex_destroy( &p_mi->object_lock );
424
425     input_DestroyThread( p_input_thread );
426
427     libvlc_media_descriptor_release( p_mi->p_md );
428
429     free( p_mi );
430 }
431
432 /**************************************************************************
433  * Release a Media Instance object
434  **************************************************************************/
435 void libvlc_media_instance_release( libvlc_media_instance_t *p_mi )
436 {
437     if( !p_mi )
438         return;
439
440     vlc_mutex_lock( &p_mi->object_lock );
441  
442     p_mi->i_refcount--;
443
444     if( p_mi->i_refcount > 0 )
445     {
446         vlc_mutex_unlock( &p_mi->object_lock );
447         return;
448     }
449     vlc_mutex_unlock( &p_mi->object_lock );
450     vlc_mutex_destroy( &p_mi->object_lock );
451
452     release_input_thread( p_mi );
453
454     libvlc_event_manager_release( p_mi->p_event_manager );
455  
456     libvlc_media_descriptor_release( p_mi->p_md );
457
458     free( p_mi );
459 }
460
461 /**************************************************************************
462  * Retain a Media Instance object
463  **************************************************************************/
464 void libvlc_media_instance_retain( libvlc_media_instance_t *p_mi )
465 {
466     if( !p_mi )
467         return;
468
469     p_mi->i_refcount++;
470 }
471
472 /**************************************************************************
473  * Set the Media descriptor associated with the instance
474  **************************************************************************/
475 void libvlc_media_instance_set_media_descriptor(
476                             libvlc_media_instance_t *p_mi,
477                             libvlc_media_descriptor_t *p_md,
478                             libvlc_exception_t *p_e )
479 {
480     (void)p_e;
481
482     if( !p_mi )
483         return;
484
485     vlc_mutex_lock( &p_mi->object_lock );
486
487     release_input_thread( p_mi );
488
489     if( p_mi->p_md )
490         libvlc_media_descriptor_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL );
491
492     libvlc_media_descriptor_release( p_mi->p_md );
493
494     if( !p_md )
495     {
496         p_mi->p_md = NULL;
497         vlc_mutex_unlock( &p_mi->object_lock );
498         return; /* It is ok to pass a NULL md */
499     }
500
501     libvlc_media_descriptor_retain( p_md );
502     p_mi->p_md = p_md;
503  
504     /* The policy here is to ignore that we were created using a different
505      * libvlc_instance, because we don't really care */
506     p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
507
508     vlc_mutex_unlock( &p_mi->object_lock );
509 }
510
511 /**************************************************************************
512  * Get the Media descriptor associated with the instance
513  **************************************************************************/
514 libvlc_media_descriptor_t *
515 libvlc_media_instance_get_media_descriptor(
516                             libvlc_media_instance_t *p_mi,
517                             libvlc_exception_t *p_e )
518 {
519     (void)p_e;
520
521     if( !p_mi->p_md )
522         return NULL;
523
524     libvlc_media_descriptor_retain( p_mi->p_md );
525     return p_mi->p_md;
526 }
527
528 /**************************************************************************
529  * Get the event Manager
530  **************************************************************************/
531 libvlc_event_manager_t *
532 libvlc_media_instance_event_manager(
533                             libvlc_media_instance_t *p_mi,
534                             libvlc_exception_t *p_e )
535 {
536     (void)p_e;
537
538     return p_mi->p_event_manager;
539 }
540
541 /**************************************************************************
542  * Play
543  **************************************************************************/
544 void libvlc_media_instance_play( libvlc_media_instance_t *p_mi,
545                                  libvlc_exception_t *p_e )
546 {
547     input_thread_t * p_input_thread;
548
549     if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
550     {
551         /* A thread already exists, send it a play message */
552         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
553         vlc_object_release( p_input_thread );
554         return;
555     }
556
557     /* Ignore previous exception */
558     libvlc_exception_clear( p_e );
559
560     vlc_mutex_lock( &p_mi->object_lock );
561  
562     if( !p_mi->p_md )
563     {
564         libvlc_exception_raise( p_e, "no associated media descriptor" );
565         vlc_mutex_unlock( &p_mi->object_lock );
566         return;
567     }
568
569     int i_input_id = input_Read( p_mi->p_libvlc_instance->p_libvlc_int,
570                       p_mi->p_md->p_input_item, VLC_FALSE );
571
572     /* Released in input_release */
573     p_mi->p_input_thread = (input_thread_t*)vlc_object_get( i_input_id );
574
575     if( !p_mi->p_input_thread )
576     {
577         return;
578         vlc_mutex_unlock( &p_mi->object_lock );
579     }
580
581     p_input_thread = p_mi->p_input_thread;
582
583     if( p_mi->drawable )
584     {
585         vlc_value_t val;
586         val.i_int = p_mi->drawable;
587         var_Create( p_input_thread, "drawable", VLC_VAR_DOINHERIT );
588         var_Set( p_input_thread, "drawable", val );
589     }
590     var_AddCallback( p_input_thread, "state", input_state_changed, p_mi );
591     var_AddCallback( p_input_thread, "seekable", input_seekable_changed, p_mi );
592     var_AddCallback( p_input_thread, "pausable", input_pausable_changed, p_mi );
593     var_AddCallback( p_input_thread, "intf-change", input_position_changed, p_mi );
594     var_AddCallback( p_input_thread, "intf-change", input_time_changed, p_mi );
595
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 int libvlc_media_instance_will_play( libvlc_media_instance_t *p_mi,
821                                      libvlc_exception_t *p_e)
822 {
823     input_thread_t *p_input_thread =
824                             libvlc_get_input_thread ( p_mi, p_e);
825     if ( !p_input_thread )
826         return VLC_FALSE;
827
828     if ( !p_input_thread->b_die && !p_input_thread->b_dead )
829     {
830         vlc_object_release( p_input_thread );
831         return VLC_TRUE;
832     }
833     vlc_object_release( p_input_thread );
834     return VLC_FALSE;
835 }
836
837 void libvlc_media_instance_set_rate(
838                                  libvlc_media_instance_t *p_mi,
839                                  float rate,
840                                  libvlc_exception_t *p_e )
841 {
842     input_thread_t *p_input_thread;
843     vlc_value_t val;
844
845     if( rate <= 0 )
846         RAISEVOID( "Rate value is invalid" );
847
848     val.i_int = 1000.0f/rate;
849
850     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
851     if ( !p_input_thread )
852         return;
853
854     var_Set( p_input_thread, "rate", val );
855     vlc_object_release( p_input_thread );
856 }
857
858 float libvlc_media_instance_get_rate(
859                                  libvlc_media_instance_t *p_mi,
860                                  libvlc_exception_t *p_e )
861 {
862     input_thread_t *p_input_thread;
863     vlc_value_t val;
864
865     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
866     if ( !p_input_thread )
867         return -1.0;
868
869     var_Get( p_input_thread, "rate", &val );
870     vlc_object_release( p_input_thread );
871
872     return (float)1000.0f/val.i_int;
873 }
874
875 libvlc_state_t libvlc_media_instance_get_state(
876                                  libvlc_media_instance_t *p_mi,
877                                  libvlc_exception_t *p_e )
878 {
879     input_thread_t *p_input_thread;
880     vlc_value_t val;
881
882     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
883     if ( !p_input_thread )
884     {
885         /* We do return the right value, no need to throw an exception */
886         if( libvlc_exception_raised( p_e ) )
887             libvlc_exception_clear( p_e );
888         return libvlc_Stopped;
889     }
890
891     var_Get( p_input_thread, "state", &val );
892     vlc_object_release( p_input_thread );
893
894     return vlc_to_libvlc_state(val.i_int);
895 }
896
897 int libvlc_media_instance_is_seekable( libvlc_media_instance_t *p_mi,
898                                        libvlc_exception_t *p_e )
899 {
900     input_thread_t *p_input_thread;
901     vlc_value_t val;
902
903     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
904     if ( !p_input_thread )
905     {
906         /* We do return the right value, no need to throw an exception */
907         if( libvlc_exception_raised( p_e ) )
908             libvlc_exception_clear( p_e );
909         return VLC_FALSE;
910     }
911     var_Get( p_input_thread, "seekable", &val );
912     vlc_object_release( p_input_thread );
913
914     return val.b_bool;
915 }
916
917 int libvlc_media_instance_can_pause( libvlc_media_instance_t *p_mi,
918                                      libvlc_exception_t *p_e )
919 {
920     input_thread_t *p_input_thread;
921     vlc_value_t val;
922
923     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
924     if ( !p_input_thread )
925     {
926         /* We do return the right value, no need to throw an exception */
927         if( libvlc_exception_raised( p_e ) )
928             libvlc_exception_clear( p_e );
929         return VLC_FALSE;
930     }
931     var_Get( p_input_thread, "can-pause", &val );
932     vlc_object_release( p_input_thread );
933
934     return val.b_bool;
935 }