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