]> git.sesse.net Git - vlc/blob - src/control/media_player.c
libvlc: Make sure the media_player has its own state cached.
[vlc] / src / control / media_player.c
1 /*****************************************************************************
2  * media_player.c: Libvlc API Media Instance management functions
3  *****************************************************************************
4  * Copyright (C) 2005-2009 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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <assert.h>
29
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_events.h>
33
34 #include <vlc_demux.h>
35 #include <vlc_input.h>
36 #include <vlc_vout.h>
37
38 #include "libvlc.h"
39
40 #include "libvlc_internal.h"
41 #include "media_internal.h" // libvlc_media_set_state()
42 #include "media_player_internal.h"
43
44 static int
45 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
46                         vlc_value_t oldval, vlc_value_t newval,
47                         void * p_userdata );
48 static int
49 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
50                         vlc_value_t oldval, vlc_value_t newval,
51                         void * p_userdata );
52 static int
53 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
54                      vlc_value_t oldval, vlc_value_t newval,
55                      void * p_userdata );
56
57 static int
58 snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
59                     vlc_value_t oldval, vlc_value_t newval, void *p_data );
60
61 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
62
63 /*
64  * Shortcuts
65  */
66
67 #define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
68 static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type_t type)
69 {
70     libvlc_event_manager_register_event_type(mp->p_event_manager, type, NULL);
71 }
72
73 static inline void lock(libvlc_media_player_t *mp)
74 {
75     vlc_mutex_lock(&mp->object_lock);
76 }
77
78 static inline void unlock(libvlc_media_player_t *mp)
79 {
80     vlc_mutex_unlock(&mp->object_lock);
81 }
82
83 static inline void clear_if_needed(libvlc_exception_t *e)
84 {
85     if (libvlc_exception_raised(e))
86         libvlc_exception_clear(e);
87 }
88
89 /*
90  * Release the associated input thread.
91  *
92  * Object lock is NOT held.
93  */
94 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
95 {
96     input_thread_t * p_input_thread;
97
98     if( !p_mi || !p_mi->p_input_thread )
99         return;
100
101     p_input_thread = p_mi->p_input_thread;
102
103     var_DelCallback( p_input_thread, "can-seek",
104                      input_seekable_changed, p_mi );
105     var_DelCallback( p_input_thread, "can-pause",
106                     input_pausable_changed, p_mi );
107     var_DelCallback( p_input_thread, "intf-event",
108                      input_event_changed, p_mi );
109
110     /* We owned this one */
111     input_Stop( p_input_thread, b_input_abort );
112
113     vlc_thread_join( p_input_thread );
114
115     assert( p_mi->p_input_resource == NULL );
116     assert( p_input_thread->b_dead );
117     /* Store the input resource for future use. */
118     p_mi->p_input_resource = input_DetachResource( p_input_thread );
119
120     var_Destroy( p_input_thread, "drawable-hwnd" );
121     var_Destroy( p_input_thread, "drawable-xid" );
122     var_Destroy( p_input_thread, "drawable-agl" );
123
124     vlc_object_release( p_input_thread );
125
126     p_mi->p_input_thread = NULL;
127 }
128
129 /*
130  * Retrieve the input thread. Be sure to release the object
131  * once you are done with it. (libvlc Internal)
132  *
133  * Function will lock the object.
134  */
135 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
136                                          libvlc_exception_t *p_e )
137 {
138     input_thread_t *p_input_thread;
139
140     if( !p_mi ) RAISENULL( "Media Instance is NULL" );
141
142     lock(p_mi);
143
144     if( !p_mi->p_input_thread )
145     {
146         unlock(p_mi);
147         RAISENULL( "Input is NULL" );
148     }
149
150     p_input_thread = p_mi->p_input_thread;
151     vlc_object_hold( p_input_thread );
152
153     unlock(p_mi);
154
155     return p_input_thread;
156 }
157
158 /*
159  * Set the internal state of the media_player. (media player Internal)
160  *
161  * Function will lock the media_player.
162  */
163 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state )
164 {    
165     lock(p_mi);
166     p_mi->state = state;
167     libvlc_media_t *media = p_mi->p_md;
168     if (media)
169         libvlc_media_retain(media);
170     unlock(p_mi);
171
172
173     if (media) {
174         // Also set the state of the corresponding media
175         // This is strictly for convenience.
176         libvlc_media_set_state(media, state);
177         
178         libvlc_media_release(media);        
179     }
180 }
181
182 static int
183 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
184                         vlc_value_t oldval, vlc_value_t newval,
185                         void * p_userdata )
186 {
187     VLC_UNUSED(oldval);
188     VLC_UNUSED(p_this);
189     VLC_UNUSED(psz_cmd);
190     libvlc_media_player_t * p_mi = p_userdata;
191     libvlc_event_t event;
192
193     event.type = libvlc_MediaPlayerSeekableChanged;
194     event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
195
196     libvlc_event_send( p_mi->p_event_manager, &event );
197     return VLC_SUCCESS;
198 }
199
200 static int
201 input_pausable_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     VLC_UNUSED(oldval);
206     VLC_UNUSED(p_this);
207     VLC_UNUSED(psz_cmd);
208     libvlc_media_player_t * p_mi = p_userdata;
209     libvlc_event_t event;
210
211     event.type = libvlc_MediaPlayerPausableChanged;
212     event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
213
214     libvlc_event_send( p_mi->p_event_manager, &event );
215     return VLC_SUCCESS;
216 }
217
218 static int
219 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
220                      vlc_value_t oldval, vlc_value_t newval,
221                      void * p_userdata )
222 {
223     VLC_UNUSED(oldval);
224     input_thread_t * p_input = (input_thread_t *)p_this;
225     libvlc_media_player_t * p_mi = p_userdata;
226     libvlc_event_t event;
227
228     assert( !strcmp( psz_cmd, "intf-event" ) );
229
230     if( newval.i_int == INPUT_EVENT_STATE )
231     {
232         libvlc_state_t libvlc_state;
233
234         switch ( var_GetInteger( p_input, "state" ) )
235         {
236             case INIT_S:
237                 libvlc_state = libvlc_NothingSpecial;
238                 event.type = libvlc_MediaPlayerNothingSpecial;
239                 break;
240             case OPENING_S:
241                 libvlc_state = libvlc_Opening;
242                 event.type = libvlc_MediaPlayerOpening;
243                 break;
244             case PLAYING_S:
245                 libvlc_state = libvlc_Playing;
246                 event.type = libvlc_MediaPlayerPlaying;
247                 break;
248             case PAUSE_S:
249                 libvlc_state = libvlc_Paused;
250                 event.type = libvlc_MediaPlayerPaused;
251                 break;
252             case END_S:
253                 libvlc_state = libvlc_Ended;
254                 event.type = libvlc_MediaPlayerEndReached;
255                 break;
256             case ERROR_S:
257                 libvlc_state = libvlc_Error;
258                 event.type = libvlc_MediaPlayerEncounteredError;
259                 break;
260
261             default:
262                 return VLC_SUCCESS;
263         }
264         
265         set_state( p_mi, libvlc_state );
266         libvlc_event_send( p_mi->p_event_manager, &event );
267     }
268     else if( newval.i_int == INPUT_EVENT_ABORT )
269     {
270         libvlc_state_t libvlc_state = libvlc_Stopped;
271         event.type = libvlc_MediaPlayerStopped;
272
273         set_state( p_mi, libvlc_state );
274         libvlc_event_send( p_mi->p_event_manager, &event );
275     }
276     else if( newval.i_int == INPUT_EVENT_POSITION )
277     {
278         if( var_GetInteger( p_input, "state" ) != PLAYING_S )
279             return VLC_SUCCESS; /* Don't send the position while stopped */
280
281         /* */
282         event.type = libvlc_MediaPlayerPositionChanged;
283         event.u.media_player_position_changed.new_position =
284                                           var_GetFloat( p_input, "position" );
285         libvlc_event_send( p_mi->p_event_manager, &event );
286
287         /* */
288         event.type = libvlc_MediaPlayerTimeChanged;
289         event.u.media_player_time_changed.new_time =
290                                                var_GetTime( p_input, "time" );
291         libvlc_event_send( p_mi->p_event_manager, &event );
292     }
293     else if( newval.i_int == INPUT_EVENT_LENGTH )
294     {
295         event.type = libvlc_MediaPlayerLengthChanged;
296         event.u.media_player_length_changed.new_length =
297                                                var_GetTime( p_input, "length" );
298         libvlc_event_send( p_mi->p_event_manager, &event );
299     }
300
301     return VLC_SUCCESS;
302
303 }
304
305 /**************************************************************************
306  * Snapshot Taken Event.
307  *
308  * FIXME: This snapshot API interface makes no sense in media_player.
309  *************************************************************************/
310 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
311                               vlc_value_t oldval, vlc_value_t newval, void *p_data )
312 {
313     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
314
315     libvlc_media_player_t *mp = p_data;
316     libvlc_event_t event;
317     event.type = libvlc_MediaPlayerSnapshotTaken;
318     event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
319     libvlc_event_send(mp->p_event_manager, &event);
320
321     return VLC_SUCCESS;
322 }
323
324
325 static void libvlc_media_player_destroy( libvlc_media_player_t * );
326
327
328 /**************************************************************************
329  * Create a Media Instance object.
330  *
331  * Refcount strategy:
332  * - All items created by _new start with a refcount set to 1.
333  * - Accessor _release decrease the refcount by 1, if after that
334  *   operation the refcount is 0, the object is destroyed.
335  * - Accessor _retain increase the refcount by 1 (XXX: to implement)
336  *
337  * Object locking strategy:
338  * - No lock held while in constructor.
339  * - When accessing any member variable this lock is held. (XXX who locks?)
340  * - When attempting to destroy the object the lock is also held.
341  **************************************************************************/
342 libvlc_media_player_t *
343 libvlc_media_player_new( libvlc_instance_t *instance, libvlc_exception_t *e )
344 {
345     libvlc_media_player_t * mp;
346
347     assert(instance);
348
349     mp = malloc(sizeof(libvlc_media_player_t));
350     if (!mp)
351     {
352         libvlc_exception_raise(e);
353         libvlc_printerr("Not enough memory");
354         return NULL;
355     }
356     mp->p_md = NULL;
357     mp->drawable.agl = 0;
358     mp->drawable.xid = 0;
359     mp->drawable.hwnd = NULL;
360     mp->drawable.nsobject = NULL;
361     mp->keyboard_events = mp->mouse_events = 1;
362     mp->state = libvlc_NothingSpecial;
363     mp->p_libvlc_instance = instance;
364     mp->p_input_thread = NULL;
365     mp->p_input_resource = NULL;
366     mp->i_refcount = 1;
367     vlc_mutex_init(&mp->object_lock);
368     mp->p_event_manager = libvlc_event_manager_new(mp, instance, e);
369     if (libvlc_exception_raised(e))
370     {
371         vlc_mutex_destroy(&mp->object_lock);
372         free(mp);
373         return NULL;
374     }
375
376     register_event(mp, NothingSpecial);
377     register_event(mp, Opening);
378     register_event(mp, Buffering);
379     register_event(mp, Playing);
380     register_event(mp, Paused);
381     register_event(mp, Stopped);
382     register_event(mp, Forward);
383     register_event(mp, Backward);
384     register_event(mp, EndReached);
385     register_event(mp, EncounteredError);
386
387     register_event(mp, PositionChanged);
388     register_event(mp, TimeChanged);
389     register_event(mp, LengthChanged);
390     register_event(mp, TitleChanged);
391     register_event(mp, PausableChanged);
392
393     /* Snapshot initialization */
394     register_event(mp, SnapshotTaken);
395
396     /* Attach a var callback to the global object to provide the glue between
397      * vout_thread that generates the event and media_player that re-emits it
398      * with its own event manager
399      *
400      * FIXME: It's unclear why we want to put this in public API, and why we
401      * want to expose it in such a limiting and ugly way.
402      */
403     var_AddCallback(instance->p_libvlc_int, "snapshot-file", snapshot_was_taken, mp);
404
405     return mp;
406 }
407
408 /**************************************************************************
409  * Create a Media Instance object with a media descriptor.
410  **************************************************************************/
411 libvlc_media_player_t *
412 libvlc_media_player_new_from_media(
413                                     libvlc_media_t * p_md,
414                                     libvlc_exception_t *p_e )
415 {
416     libvlc_media_player_t * p_mi;
417
418     p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
419     if( !p_mi )
420         return NULL;
421
422     libvlc_media_retain( p_md );
423     p_mi->p_md = p_md;
424
425     return p_mi;
426 }
427
428 /**************************************************************************
429  * Destroy a Media Instance object (libvlc internal)
430  *
431  * Warning: No lock held here, but hey, this is internal. Caller must lock.
432  **************************************************************************/
433 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
434 {
435     assert( p_mi );
436
437     /* Detach Callback from the main libvlc object */
438     var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
439                      "snapshot-file", snapshot_was_taken, p_mi );
440
441     /* If the input thread hasn't been already deleted it means
442      * that the owners didn't stop the thread before releasing it. */
443     assert(!p_mi->p_input_thread);
444
445     /* Fallback for those who don't use NDEBUG */
446     if (p_mi->p_input_thread)
447         release_input_thread(p_mi, true);
448
449     if( p_mi->p_input_resource )
450     {
451         input_resource_Delete( p_mi->p_input_resource );
452         p_mi->p_input_resource = NULL;    
453     }
454
455     libvlc_event_manager_release( p_mi->p_event_manager );
456     libvlc_media_release( p_mi->p_md );
457     vlc_mutex_destroy( &p_mi->object_lock );
458     free( p_mi );
459 }
460
461 /**************************************************************************
462  * Release a Media Instance object.
463  *
464  * Function does the locking.
465  **************************************************************************/
466 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
467 {
468     bool destroy;
469
470     assert( p_mi );
471     lock(p_mi);
472     destroy = !--p_mi->i_refcount;
473     unlock(p_mi);
474
475     if( destroy )
476         libvlc_media_player_destroy( p_mi );
477 }
478
479 /**************************************************************************
480  * Retain a Media Instance object.
481  *
482  * Caller must hold the lock.
483  **************************************************************************/
484 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
485 {
486     assert( p_mi );
487
488     lock(p_mi);
489     p_mi->i_refcount++;
490     unlock(p_mi);
491 }
492
493 /**************************************************************************
494  * Set the Media descriptor associated with the instance.
495  *
496  * Enter without lock -- function will lock the object.
497  **************************************************************************/
498 void libvlc_media_player_set_media(
499                             libvlc_media_player_t *p_mi,
500                             libvlc_media_t *p_md,
501                             libvlc_exception_t *p_e )
502 {
503     VLC_UNUSED(p_e);
504
505     lock(p_mi);
506
507     /* FIXME I am not sure if it is a user request or on die(eof/error)
508      * request here */
509     release_input_thread( p_mi,
510                           p_mi->p_input_thread &&
511                           !p_mi->p_input_thread->b_eof &&
512                           !p_mi->p_input_thread->b_error );
513     unlock(p_mi);
514
515     set_state( p_mi, libvlc_NothingSpecial );
516
517     lock(p_mi);
518
519     libvlc_media_release( p_mi->p_md );
520
521     if( !p_md )
522     {
523         p_mi->p_md = NULL;
524         unlock(p_mi);
525         return; /* It is ok to pass a NULL md */
526     }
527
528     libvlc_media_retain( p_md );
529     p_mi->p_md = p_md;
530
531     /* The policy here is to ignore that we were created using a different
532      * libvlc_instance, because we don't really care */
533     p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
534
535     unlock(p_mi);
536 }
537
538 /**************************************************************************
539  * Get the Media descriptor associated with the instance.
540  **************************************************************************/
541 libvlc_media_t *
542 libvlc_media_player_get_media(
543                             libvlc_media_player_t *p_mi,
544                             libvlc_exception_t *p_e )
545 {
546     libvlc_media_t *p_m;
547     VLC_UNUSED(p_e);
548
549     lock(p_mi);
550     p_m = p_mi->p_md;
551     if( p_m )
552         libvlc_media_retain( p_mi->p_md );
553     unlock(p_mi);
554     return p_mi->p_md;
555 }
556
557 /**************************************************************************
558  * Get the event Manager.
559  **************************************************************************/
560 libvlc_event_manager_t *
561 libvlc_media_player_event_manager(
562                             libvlc_media_player_t *p_mi,
563                             libvlc_exception_t *p_e )
564 {
565     VLC_UNUSED(p_e);
566
567     return p_mi->p_event_manager;
568 }
569
570 /**************************************************************************
571  * Tell media player to start playing.
572  **************************************************************************/
573 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
574                                  libvlc_exception_t *p_e )
575 {
576     input_thread_t * p_input_thread;
577
578     if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
579     {
580         /* A thread already exists, send it a play message */
581         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
582         vlc_object_release( p_input_thread );
583         return;
584     }
585
586     /* Ignore previous exception */
587     libvlc_exception_clear( p_e );
588
589     lock(p_mi);
590
591     if( !p_mi->p_md )
592     {
593         unlock(p_mi);
594         libvlc_exception_raise( p_e );
595         libvlc_printerr( "No associated media descriptor" );
596         return;
597     }
598
599     p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
600                                          p_mi->p_md->p_input_item, NULL, p_mi->p_input_resource );
601
602     if( !p_mi->p_input_thread )
603     {
604         unlock(p_mi);
605         return;
606     }
607
608     p_mi->p_input_resource = NULL;
609     p_input_thread = p_mi->p_input_thread;
610
611     var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
612     if( p_mi->drawable.agl )
613         var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
614
615     var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
616     if( p_mi->drawable.xid )
617         var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
618
619     var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
620     if( p_mi->drawable.hwnd != NULL )
621         var_SetAddress( p_input_thread, "drawable-hwnd", p_mi->drawable.hwnd );
622
623     var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
624     if( p_mi->drawable.nsobject != NULL )
625         var_SetAddress( p_input_thread, "drawable-nsobject", p_mi->drawable.nsobject );
626
627     var_Create( p_input_thread, "keyboard-events", VLC_VAR_BOOL );
628     var_SetBool( p_input_thread, "keyboard-events", p_mi->keyboard_events );
629     var_Create( p_input_thread, "mouse-events", VLC_VAR_BOOL );
630     var_SetBool( p_input_thread, "mouse-events", p_mi->mouse_events );
631
632     var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
633     var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
634     var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
635
636     if( input_Start( p_input_thread ) )
637     {
638         vlc_object_release( p_input_thread );
639         p_mi->p_input_thread = NULL;
640     }
641
642     unlock(p_mi);
643 }
644
645 /**************************************************************************
646  * Pause.
647  **************************************************************************/
648 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
649                                   libvlc_exception_t *p_e )
650 {
651     input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
652     if( !p_input_thread )
653         return;
654
655     libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
656     if( state == libvlc_Playing || state == libvlc_Buffering )
657     {
658         if( libvlc_media_player_can_pause( p_mi, p_e ) )
659             input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
660         else
661             libvlc_media_player_stop( p_mi, p_e );
662     }
663     else
664         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
665
666     vlc_object_release( p_input_thread );
667 }
668
669 /**************************************************************************
670  * Tells whether the media player is currently playing.
671  *
672  * Enter with lock held.
673  **************************************************************************/
674 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
675                                      libvlc_exception_t *p_e )
676 {
677     libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
678     return (libvlc_Playing == state) || (libvlc_Buffering == state);
679 }
680
681 /**************************************************************************
682  * Stop playing.
683  **************************************************************************/
684 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
685                                  libvlc_exception_t *p_e )
686 {
687     libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
688
689     lock(p_mi);
690     release_input_thread( p_mi, true ); /* This will stop the input thread */
691     unlock(p_mi);
692
693     /* Force to go to stopped state, in case we were in Ended, or Error
694      * state. */
695     if( state != libvlc_Stopped )
696     {
697         set_state( p_mi, libvlc_Stopped );
698
699         /* Construct and send the event */
700         libvlc_event_t event;
701         event.type = libvlc_MediaPlayerStopped;
702         libvlc_event_send( p_mi->p_event_manager, &event );
703     }
704 }
705
706 /**************************************************************************
707  * set_nsobject
708  **************************************************************************/
709 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
710                                         void * drawable,
711                                         libvlc_exception_t *p_e )
712 {
713     (void) p_e;
714     p_mi->drawable.nsobject = drawable;
715 }
716
717 /**************************************************************************
718  * get_nsobject
719  **************************************************************************/
720 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
721 {
722     return p_mi->drawable.nsobject;
723 }
724
725 /**************************************************************************
726  * set_agl
727  **************************************************************************/
728 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
729                                       uint32_t drawable,
730                                       libvlc_exception_t *p_e )
731 {
732     (void) p_e;
733     p_mi->drawable.agl = drawable;
734 }
735
736 /**************************************************************************
737  * get_agl
738  **************************************************************************/
739 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
740 {
741     return p_mi->drawable.agl;
742 }
743
744 /**************************************************************************
745  * set_xwindow
746  **************************************************************************/
747 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
748                                       uint32_t drawable,
749                                       libvlc_exception_t *p_e )
750 {
751     (void) p_e;
752     p_mi->drawable.xid = drawable;
753 }
754
755 /**************************************************************************
756  * get_xwindow
757  **************************************************************************/
758 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
759 {
760     return p_mi->drawable.xid;
761 }
762
763 /**************************************************************************
764  * set_hwnd
765  **************************************************************************/
766 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
767                                    void *drawable,
768                                    libvlc_exception_t *p_e )
769 {
770     (void) p_e;
771     p_mi->drawable.hwnd = drawable;
772 }
773
774 /**************************************************************************
775  * get_hwnd
776  **************************************************************************/
777 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
778 {
779     return p_mi->drawable.hwnd;
780 }
781
782 /**************************************************************************
783  * Getters for stream information
784  **************************************************************************/
785 libvlc_time_t libvlc_media_player_get_length(
786                              libvlc_media_player_t *p_mi,
787                              libvlc_exception_t *p_e )
788 {
789     input_thread_t *p_input_thread;
790     libvlc_time_t i_time;
791
792     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
793     if( !p_input_thread )
794         return -1;
795
796     i_time = var_GetTime( p_input_thread, "length" );
797     vlc_object_release( p_input_thread );
798
799     return (i_time+500LL)/1000LL;
800 }
801
802 libvlc_time_t libvlc_media_player_get_time(
803                                    libvlc_media_player_t *p_mi,
804                                    libvlc_exception_t *p_e )
805 {
806     input_thread_t *p_input_thread;
807     libvlc_time_t i_time;
808
809     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
810     if( !p_input_thread )
811         return -1;
812
813     i_time = var_GetTime( p_input_thread , "time" );
814     vlc_object_release( p_input_thread );
815     return (i_time+500LL)/1000LL;
816 }
817
818 void libvlc_media_player_set_time(
819                                  libvlc_media_player_t *p_mi,
820                                  libvlc_time_t i_time,
821                                  libvlc_exception_t *p_e )
822 {
823     input_thread_t *p_input_thread;
824
825     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
826     if( !p_input_thread )
827         return;
828
829     var_SetTime( p_input_thread, "time", i_time*1000LL );
830     vlc_object_release( p_input_thread );
831 }
832
833 void libvlc_media_player_set_position(
834                                 libvlc_media_player_t *p_mi,
835                                 float position,
836                                 libvlc_exception_t *p_e )
837 {
838     input_thread_t *p_input_thread;
839
840     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
841     if( !p_input_thread )
842         return;
843
844     var_SetFloat( p_input_thread, "position", position );
845     vlc_object_release( p_input_thread );
846 }
847
848 float libvlc_media_player_get_position(
849                                  libvlc_media_player_t *p_mi,
850                                  libvlc_exception_t *p_e )
851 {
852     input_thread_t *p_input_thread;
853     float f_position;
854
855     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
856     if( !p_input_thread )
857         return -1.0;
858
859     f_position = var_GetFloat( p_input_thread, "position" );
860     vlc_object_release( p_input_thread );
861
862     return f_position;
863 }
864
865 void libvlc_media_player_set_chapter(
866                                  libvlc_media_player_t *p_mi,
867                                  int chapter,
868                                  libvlc_exception_t *p_e )
869 {
870     input_thread_t *p_input_thread;
871
872     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
873     if( !p_input_thread )
874         return;
875
876     var_SetInteger( p_input_thread, "chapter", chapter );
877     vlc_object_release( p_input_thread );
878 }
879
880 int libvlc_media_player_get_chapter(
881                                  libvlc_media_player_t *p_mi,
882                                  libvlc_exception_t *p_e )
883 {
884     input_thread_t *p_input_thread;
885     int i_chapter;
886
887     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
888     if( !p_input_thread )
889         return -1;
890
891     i_chapter = var_GetInteger( p_input_thread, "chapter" );
892     vlc_object_release( p_input_thread );
893
894     return i_chapter;
895 }
896
897 int libvlc_media_player_get_chapter_count(
898                                  libvlc_media_player_t *p_mi,
899                                  libvlc_exception_t *p_e )
900 {
901     input_thread_t *p_input_thread;
902     vlc_value_t val;
903
904     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
905     if( !p_input_thread )
906         return -1;
907
908     var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
909     vlc_object_release( p_input_thread );
910
911     return val.i_int;
912 }
913
914 int libvlc_media_player_get_chapter_count_for_title(
915                                  libvlc_media_player_t *p_mi,
916                                  int i_title,
917                                  libvlc_exception_t *p_e )
918 {
919     input_thread_t *p_input_thread;
920     vlc_value_t val;
921
922     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
923     if( !p_input_thread )
924         return -1;
925
926     char *psz_name;
927     if( asprintf( &psz_name,  "title %2i", i_title ) == -1 )
928     {
929         vlc_object_release( p_input_thread );
930         return -1;
931     }
932     var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
933     vlc_object_release( p_input_thread );
934     free( psz_name );
935
936     return val.i_int;
937 }
938
939 void libvlc_media_player_set_title(
940                                  libvlc_media_player_t *p_mi,
941                                  int i_title,
942                                  libvlc_exception_t *p_e )
943 {
944     input_thread_t *p_input_thread;
945
946     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
947     if( !p_input_thread )
948         return;
949
950     var_SetInteger( p_input_thread, "title", i_title );
951     vlc_object_release( p_input_thread );
952
953     //send event
954     libvlc_event_t event;
955     event.type = libvlc_MediaPlayerTitleChanged;
956     event.u.media_player_title_changed.new_title = i_title;
957     libvlc_event_send( p_mi->p_event_manager, &event );
958 }
959
960 int libvlc_media_player_get_title(
961                                  libvlc_media_player_t *p_mi,
962                                  libvlc_exception_t *p_e )
963 {
964     input_thread_t *p_input_thread;
965     int i_title;
966
967     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
968     if( !p_input_thread )
969         return -1;
970
971     i_title = var_GetInteger( p_input_thread, "title" );
972     vlc_object_release( p_input_thread );
973
974     return i_title;
975 }
976
977 int libvlc_media_player_get_title_count(
978                                  libvlc_media_player_t *p_mi,
979                                  libvlc_exception_t *p_e )
980 {
981     input_thread_t *p_input_thread;
982     vlc_value_t val;
983
984     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
985     if( !p_input_thread )
986         return -1;
987
988     var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
989     vlc_object_release( p_input_thread );
990
991     return val.i_int;
992 }
993
994 void libvlc_media_player_next_chapter(
995                                  libvlc_media_player_t *p_mi,
996                                  libvlc_exception_t *p_e )
997 {
998     input_thread_t *p_input_thread;
999
1000     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1001     if( !p_input_thread )
1002         return;
1003
1004     int i_type = var_Type( p_input_thread, "next-chapter" );
1005     var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1006                             "next-chapter":"next-title", true );
1007
1008     vlc_object_release( p_input_thread );
1009 }
1010
1011 void libvlc_media_player_previous_chapter(
1012                                  libvlc_media_player_t *p_mi,
1013                                  libvlc_exception_t *p_e )
1014 {
1015     input_thread_t *p_input_thread;
1016
1017     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1018     if( !p_input_thread )
1019         return;
1020
1021     int i_type = var_Type( p_input_thread, "next-chapter" );
1022     var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1023                             "prev-chapter":"prev-title", true );
1024
1025     vlc_object_release( p_input_thread );
1026 }
1027
1028 float libvlc_media_player_get_fps(
1029                                  libvlc_media_player_t *p_mi,
1030                                  libvlc_exception_t *p_e)
1031 {
1032     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1033     double f_fps = 0.0;
1034
1035     if( p_input_thread )
1036     {
1037         if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1038             f_fps = 0.0;
1039         vlc_object_release( p_input_thread );
1040     }
1041     return f_fps;
1042 }
1043
1044 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1045                                      libvlc_exception_t *p_e)
1046 {
1047     bool b_will_play;
1048     input_thread_t *p_input_thread =
1049                             libvlc_get_input_thread ( p_mi, p_e);
1050     if ( !p_input_thread )
1051         return false;
1052
1053     b_will_play = !p_input_thread->b_die && !p_input_thread->b_dead;
1054     vlc_object_release( p_input_thread );
1055
1056     return b_will_play;
1057 }
1058
1059 void libvlc_media_player_set_rate(
1060                                  libvlc_media_player_t *p_mi,
1061                                  float rate,
1062                                  libvlc_exception_t *p_e )
1063 {
1064     input_thread_t *p_input_thread;
1065     bool b_can_rewind;
1066
1067     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1068     if( !p_input_thread )
1069         return;
1070
1071     b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1072     if( (rate < 0.0) && !b_can_rewind )
1073     {
1074         vlc_object_release( p_input_thread );
1075         libvlc_exception_raise( p_e );
1076         libvlc_printerr( "Invalid playback rate" );
1077         return;
1078     }
1079
1080     var_SetFloat( p_input_thread, "rate", rate );
1081     vlc_object_release( p_input_thread );
1082 }
1083
1084 float libvlc_media_player_get_rate(
1085                                  libvlc_media_player_t *p_mi,
1086                                  libvlc_exception_t *p_e )
1087 {
1088     input_thread_t *p_input_thread;
1089     float f_rate;
1090     bool b_can_rewind;
1091
1092     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1093     if( !p_input_thread )
1094         return 0.0;  /* rate < 0 indicates rewind */
1095
1096     f_rate = var_GetFloat( p_input_thread, "rate" );
1097     b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1098     /* FIXME: why are negative values forbidden ?? (rewinding) */
1099     if( f_rate < 0 && !b_can_rewind )
1100     {
1101         vlc_object_release( p_input_thread );
1102         return 0.0;
1103     }
1104     vlc_object_release( p_input_thread );
1105
1106     return f_rate;
1107 }
1108
1109 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
1110 {
1111     VLC_UNUSED(p_e);
1112     lock(p_mi);
1113     libvlc_state_t state = p_mi->state;
1114     unlock(p_mi);
1115     return state;
1116 }
1117
1118 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1119                                        libvlc_exception_t *p_e )
1120 {
1121     input_thread_t *p_input_thread;
1122     bool b_seekable;
1123
1124     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1125     if ( !p_input_thread )
1126     {
1127         /* We do return the right value, no need to throw an exception */
1128         clear_if_needed(p_e);
1129         return false;
1130     }
1131     b_seekable = var_GetBool( p_input_thread, "can-seek" );
1132     vlc_object_release( p_input_thread );
1133
1134     return b_seekable;
1135 }
1136
1137 /* internal function, used by audio, video */
1138 libvlc_track_description_t *
1139         libvlc_get_track_description( libvlc_media_player_t *p_mi,
1140                                       const char *psz_variable,
1141                                       libvlc_exception_t *p_e )
1142 {
1143     input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1144     libvlc_track_description_t *p_track_description = NULL,
1145                                *p_actual, *p_previous;
1146
1147     if( !p_input )
1148         return NULL;
1149
1150     vlc_value_t val_list, text_list;
1151     var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1152
1153     /* no tracks */
1154     if( val_list.p_list->i_count <= 0 )
1155         goto end;
1156
1157     p_track_description = ( libvlc_track_description_t * )
1158         malloc( sizeof( libvlc_track_description_t ) );
1159     if ( !p_track_description )
1160     {
1161         libvlc_exception_raise( p_e );
1162         libvlc_printerr( "Not enough memory" );
1163         goto end;
1164     }
1165     p_actual = p_track_description;
1166     p_previous = NULL;
1167     for( int i = 0; i < val_list.p_list->i_count; i++ )
1168     {
1169         if( !p_actual )
1170         {
1171             p_actual = ( libvlc_track_description_t * )
1172                 malloc( sizeof( libvlc_track_description_t ) );
1173             if ( !p_actual )
1174             {
1175                 libvlc_track_description_release( p_track_description );
1176                 libvlc_exception_raise( p_e );
1177                 libvlc_printerr( "Not enough memory" );
1178                 goto end;
1179             }
1180         }
1181         p_actual->i_id = val_list.p_list->p_values[i].i_int;
1182         p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1183         p_actual->p_next = NULL;
1184         if( p_previous )
1185             p_previous->p_next = p_actual;
1186         p_previous = p_actual;
1187         p_actual =  NULL;
1188     }
1189
1190 end:
1191     var_FreeList( &val_list, &text_list );
1192     vlc_object_release( p_input );
1193
1194     return p_track_description;
1195 }
1196
1197 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1198 {
1199     libvlc_track_description_t *p_actual, *p_before;
1200     p_actual = p_td;
1201
1202     while ( p_actual )
1203     {
1204         free( p_actual->psz_name );
1205         p_before = p_actual;
1206         p_actual = p_before->p_next;
1207         free( p_before );
1208     }
1209 }
1210
1211 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1212                                      libvlc_exception_t *p_e )
1213 {
1214     input_thread_t *p_input_thread;
1215     bool b_can_pause;
1216
1217     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1218     if ( !p_input_thread )
1219     {
1220         /* We do return the right value, no need to throw an exception */
1221         clear_if_needed(p_e);
1222         return false;
1223     }
1224     b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1225     vlc_object_release( p_input_thread );
1226
1227     return b_can_pause;
1228 }
1229
1230 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
1231 {
1232     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1233     if( p_input_thread != NULL )
1234     {
1235         var_TriggerCallback( p_input_thread, "frame-next" );
1236         vlc_object_release( p_input_thread );
1237     }
1238     else
1239     {
1240         libvlc_exception_raise( p_e );
1241         libvlc_printerr( "No active input" );
1242     }
1243 }