]> git.sesse.net Git - vlc/blob - src/control/media_player.c
Made object created by input_resource attached to a high level object.
[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, "drawable-xid", VLC_VAR_INTEGER);
371 #ifdef WIN32
372     var_Create (mp, "drawable-hwnd", VLC_VAR_ADDRESS);
373 #endif
374 #ifdef __APPLE__
375     var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
376     var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
377 #endif
378
379     var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
380     var_SetBool (mp, "keyboard-events", true);
381     var_Create (mp, "mouse-events", VLC_VAR_BOOL);
382     var_SetBool (mp, "mouse-events", true);
383
384     var_Create (mp, "fullscreen", VLC_VAR_BOOL);
385     var_Create (mp, "autoscale", VLC_VAR_BOOL);
386     var_SetBool (mp, "autoscale", true);
387     var_Create (mp, "scale", VLC_VAR_FLOAT);
388     var_SetFloat (mp, "scale", 1.);
389     var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
390     var_Create (mp, "crop", VLC_VAR_STRING);
391     var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
392     var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
393
394     var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
395
396     var_Create (mp, "marq-marquee", VLC_VAR_STRING);
397     var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
398     var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
399     var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
400     var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
401     var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
402     var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
403     var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
404     var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
405
406     var_Create (mp, "logo-file", VLC_VAR_STRING);
407     var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
408     var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
409     var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
410     var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
411     var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
412     var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
413
414      /* Audio */
415     var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
416     var_Create (mp, "volume-muted", VLC_VAR_BOOL);
417     var_Create (mp, "saved-volume", VLC_VAR_INTEGER);
418     var_Create (mp, "volume-change", VLC_VAR_VOID);
419     var_Create (mp, "find-input-callback", VLC_VAR_ADDRESS);
420     var_SetAddress (mp, "find-input-callback", find_input);
421
422     mp->p_md = NULL;
423     mp->state = libvlc_NothingSpecial;
424     mp->p_libvlc_instance = instance;
425     mp->input.p_thread = NULL;
426     mp->input.p_resource = NULL;
427     vlc_mutex_init (&mp->input.lock);
428     mp->i_refcount = 1;
429     mp->p_event_manager = libvlc_event_manager_new(mp, instance);
430     if (unlikely(mp->p_event_manager == NULL))
431     {
432         vlc_object_release(mp);
433         return NULL;
434     }
435     vlc_mutex_init(&mp->object_lock);
436
437     register_event(mp, NothingSpecial);
438     register_event(mp, Opening);
439     register_event(mp, Buffering);
440     register_event(mp, Playing);
441     register_event(mp, Paused);
442     register_event(mp, Stopped);
443     register_event(mp, Forward);
444     register_event(mp, Backward);
445     register_event(mp, EndReached);
446     register_event(mp, EncounteredError);
447     register_event(mp, SeekableChanged);
448
449     register_event(mp, PositionChanged);
450     register_event(mp, TimeChanged);
451     register_event(mp, LengthChanged);
452     register_event(mp, TitleChanged);
453     register_event(mp, PausableChanged);
454
455     /* Snapshot initialization */
456     register_event(mp, SnapshotTaken);
457
458     register_event(mp, MediaChanged);
459
460     /* Attach a var callback to the global object to provide the glue between
461      * vout_thread that generates the event and media_player that re-emits it
462      * with its own event manager
463      *
464      * FIXME: It's unclear why we want to put this in public API, and why we
465      * want to expose it in such a limiting and ugly way.
466      */
467     var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
468
469     libvlc_retain(instance);
470     return mp;
471 }
472
473 /**************************************************************************
474  * Create a Media Instance object with a media descriptor.
475  **************************************************************************/
476 libvlc_media_player_t *
477 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
478 {
479     libvlc_media_player_t * p_mi;
480
481     p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
482     if( !p_mi )
483         return NULL;
484
485     libvlc_media_retain( p_md );
486     p_mi->p_md = p_md;
487
488     return p_mi;
489 }
490
491 /**************************************************************************
492  * Destroy a Media Instance object (libvlc internal)
493  *
494  * Warning: No lock held here, but hey, this is internal. Caller must lock.
495  **************************************************************************/
496 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
497 {
498     assert( p_mi );
499
500     /* Detach Callback from the main libvlc object */
501     var_DelCallback( p_mi->p_libvlc,
502                      "snapshot-file", snapshot_was_taken, p_mi );
503
504     /* No need for lock_input() because no other threads knows us anymore */
505     if( p_mi->input.p_thread )
506         release_input_thread(p_mi, true);
507     if( p_mi->input.p_resource )
508     {
509         input_resource_Delete( p_mi->input.p_resource );
510         p_mi->input.p_resource = NULL;
511     }
512     vlc_mutex_destroy( &p_mi->input.lock );
513
514     libvlc_event_manager_release( p_mi->p_event_manager );
515     libvlc_media_release( p_mi->p_md );
516     vlc_mutex_destroy( &p_mi->object_lock );
517
518     libvlc_instance_t *instance = p_mi->p_libvlc_instance;
519     vlc_object_release( p_mi );
520     libvlc_release(instance);
521 }
522
523 /**************************************************************************
524  * Release a Media Instance object.
525  *
526  * Function does the locking.
527  **************************************************************************/
528 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
529 {
530     bool destroy;
531
532     assert( p_mi );
533     lock(p_mi);
534     destroy = !--p_mi->i_refcount;
535     unlock(p_mi);
536
537     if( destroy )
538         libvlc_media_player_destroy( p_mi );
539 }
540
541 /**************************************************************************
542  * Retain a Media Instance object.
543  *
544  * Caller must hold the lock.
545  **************************************************************************/
546 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
547 {
548     assert( p_mi );
549
550     lock(p_mi);
551     p_mi->i_refcount++;
552     unlock(p_mi);
553 }
554
555 /**************************************************************************
556  * Set the Media descriptor associated with the instance.
557  *
558  * Enter without lock -- function will lock the object.
559  **************************************************************************/
560 void libvlc_media_player_set_media(
561                             libvlc_media_player_t *p_mi,
562                             libvlc_media_t *p_md )
563 {
564     lock_input(p_mi);
565
566     /* FIXME I am not sure if it is a user request or on die(eof/error)
567      * request here */
568     release_input_thread( p_mi,
569                           p_mi->input.p_thread &&
570                           !p_mi->input.p_thread->b_eof &&
571                           !p_mi->input.p_thread->b_error );
572
573     lock( p_mi );
574     set_state( p_mi, libvlc_NothingSpecial, true );
575     unlock_input( p_mi );
576
577     libvlc_media_release( p_mi->p_md );
578
579     if( !p_md )
580     {
581         p_mi->p_md = NULL;
582         unlock(p_mi);
583         return; /* It is ok to pass a NULL md */
584     }
585
586     libvlc_media_retain( p_md );
587     p_mi->p_md = p_md;
588
589     /* The policy here is to ignore that we were created using a different
590      * libvlc_instance, because we don't really care */
591     p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
592
593     unlock(p_mi);
594
595     /* Send an event for the newly available media */
596     libvlc_event_t event;
597     event.type = libvlc_MediaPlayerMediaChanged;
598     event.u.media_player_media_changed.new_media = p_md;
599     libvlc_event_send( p_mi->p_event_manager, &event );
600
601 }
602
603 /**************************************************************************
604  * Get the Media descriptor associated with the instance.
605  **************************************************************************/
606 libvlc_media_t *
607 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
608 {
609     libvlc_media_t *p_m;
610
611     lock(p_mi);
612     p_m = p_mi->p_md;
613     if( p_m )
614         libvlc_media_retain( p_mi->p_md );
615     unlock(p_mi);
616     return p_mi->p_md;
617 }
618
619 /**************************************************************************
620  * Get the event Manager.
621  **************************************************************************/
622 libvlc_event_manager_t *
623 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
624 {
625     return p_mi->p_event_manager;
626 }
627
628 /**************************************************************************
629  * Tell media player to start playing.
630  **************************************************************************/
631 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
632 {
633     lock_input( p_mi );
634
635     input_thread_t *p_input_thread = p_mi->input.p_thread;
636     if( p_input_thread )
637     {
638         /* A thread already exists, send it a play message */
639         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
640         unlock_input( p_mi );
641         return 0;
642     }
643
644     /* Ignore previous exception */
645     lock(p_mi);
646
647     if( !p_mi->p_md )
648     {
649         unlock(p_mi);
650         unlock_input( p_mi );
651         libvlc_printerr( "No associated media descriptor" );
652         return -1;
653     }
654
655     if( !p_mi->input.p_resource )
656         p_mi->input.p_resource = input_resource_New( VLC_OBJECT( p_mi ) );
657     p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
658                                    p_mi->input.p_resource );
659     unlock(p_mi);
660     if( !p_input_thread )
661     {
662         unlock_input(p_mi);
663         libvlc_printerr( "Not enough memory" );
664         return -1;
665     }
666
667     var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
668     var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
669     var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
670
671     if( input_Start( p_input_thread ) )
672     {
673         unlock_input(p_mi);
674         var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
675         var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
676         var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
677         vlc_object_release( p_input_thread );
678         libvlc_printerr( "Input initialization failure" );
679         return -1;
680     }
681     p_mi->input.p_thread = p_input_thread;
682     unlock_input(p_mi);
683     return 0;
684 }
685
686 /**************************************************************************
687  * Pause.
688  **************************************************************************/
689 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
690 {
691     input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
692     if( !p_input_thread )
693         return;
694
695     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
696     if( state == libvlc_Playing || state == libvlc_Buffering )
697     {
698         if( libvlc_media_player_can_pause( p_mi ) )
699             input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
700         else
701             libvlc_media_player_stop( p_mi );
702     }
703     else
704         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
705
706     vlc_object_release( p_input_thread );
707 }
708
709 /**************************************************************************
710  * Tells whether the media player is currently playing.
711  *
712  * Enter with lock held.
713  **************************************************************************/
714 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
715 {
716     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
717     return (libvlc_Playing == state) || (libvlc_Buffering == state);
718 }
719
720 /**************************************************************************
721  * Stop playing.
722  **************************************************************************/
723 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
724 {
725     libvlc_state_t state = libvlc_media_player_get_state( p_mi );
726
727     lock_input(p_mi);
728     release_input_thread( p_mi, true ); /* This will stop the input thread */
729
730     /* Force to go to stopped state, in case we were in Ended, or Error
731      * state. */
732     if( state != libvlc_Stopped )
733     {
734         set_state( p_mi, libvlc_Stopped, false );
735
736         /* Construct and send the event */
737         libvlc_event_t event;
738         event.type = libvlc_MediaPlayerStopped;
739         libvlc_event_send( p_mi->p_event_manager, &event );
740     }
741     unlock_input(p_mi);
742 }
743
744 /**************************************************************************
745  * set_nsobject
746  **************************************************************************/
747 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
748                                         void * drawable )
749 {
750     assert (p_mi != NULL);
751 #ifdef __APPLE__
752     var_SetAddress (p_mi, "drawable-nsobject", drawable);
753 #else
754     (void) p_mi; (void)drawable;
755 #endif
756 }
757
758 /**************************************************************************
759  * get_nsobject
760  **************************************************************************/
761 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
762 {
763     assert (p_mi != NULL);
764 #ifdef __APPLE__
765     return var_GetAddress (p_mi, "drawable-nsobject");
766 #else
767     return NULL;
768 #endif
769 }
770
771 /**************************************************************************
772  * set_agl
773  **************************************************************************/
774 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
775                                   uint32_t drawable )
776 {
777 #ifdef __APPLE__
778     var_SetInteger (p_mi, "drawable-agl", drawable);
779 #else
780     (void) p_mi; (void)drawable;
781 #endif
782 }
783
784 /**************************************************************************
785  * get_agl
786  **************************************************************************/
787 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
788 {
789     assert (p_mi != NULL);
790 #ifdef __APPLE__
791     return var_GetInteger (p_mi, "drawable-agl");
792 #else
793     return 0;
794 #endif
795 }
796
797 /**************************************************************************
798  * set_xwindow
799  **************************************************************************/
800 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
801                                       uint32_t drawable )
802 {
803     assert (p_mi != NULL);
804     var_SetInteger (p_mi, "drawable-xid", drawable);
805 }
806
807 /**************************************************************************
808  * get_xwindow
809  **************************************************************************/
810 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
811 {
812     return var_GetInteger (p_mi, "drawable-xid");
813 }
814
815 /**************************************************************************
816  * set_hwnd
817  **************************************************************************/
818 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
819                                    void *drawable )
820 {
821     assert (p_mi != NULL);
822 #ifdef WIN32
823     var_SetAddress (p_mi, "drawable-hwnd", drawable);
824 #else
825     (void) p_mi; (void) drawable;
826 #endif
827 }
828
829 /**************************************************************************
830  * get_hwnd
831  **************************************************************************/
832 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
833 {
834     assert (p_mi != NULL);
835 #ifdef WIN32
836     return var_GetAddress (p_mi, "drawable-hwnd");
837 #else
838     return NULL;
839 #endif
840 }
841
842 /**************************************************************************
843  * Getters for stream information
844  **************************************************************************/
845 libvlc_time_t libvlc_media_player_get_length(
846                              libvlc_media_player_t *p_mi )
847 {
848     input_thread_t *p_input_thread;
849     libvlc_time_t i_time;
850
851     p_input_thread = libvlc_get_input_thread ( p_mi );
852     if( !p_input_thread )
853         return -1;
854
855     i_time = from_mtime(var_GetTime( p_input_thread, "length" ));
856     vlc_object_release( p_input_thread );
857
858     return i_time;
859 }
860
861 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
862 {
863     input_thread_t *p_input_thread;
864     libvlc_time_t i_time;
865
866     p_input_thread = libvlc_get_input_thread ( p_mi );
867     if( !p_input_thread )
868         return -1;
869
870     i_time = from_mtime(var_GetTime( p_input_thread , "time" ));
871     vlc_object_release( p_input_thread );
872     return i_time;
873 }
874
875 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
876                                    libvlc_time_t i_time )
877 {
878     input_thread_t *p_input_thread;
879
880     p_input_thread = libvlc_get_input_thread ( p_mi );
881     if( !p_input_thread )
882         return;
883
884     var_SetTime( p_input_thread, "time", to_mtime(i_time) );
885     vlc_object_release( p_input_thread );
886 }
887
888 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
889                                        float position )
890 {
891     input_thread_t *p_input_thread;
892
893     p_input_thread = libvlc_get_input_thread ( p_mi );
894     if( !p_input_thread )
895         return;
896
897     var_SetFloat( p_input_thread, "position", position );
898     vlc_object_release( p_input_thread );
899 }
900
901 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
902 {
903     input_thread_t *p_input_thread;
904     float f_position;
905
906     p_input_thread = libvlc_get_input_thread ( p_mi );
907     if( !p_input_thread )
908         return -1.0;
909
910     f_position = var_GetFloat( p_input_thread, "position" );
911     vlc_object_release( p_input_thread );
912
913     return f_position;
914 }
915
916 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
917                                       int chapter )
918 {
919     input_thread_t *p_input_thread;
920
921     p_input_thread = libvlc_get_input_thread ( p_mi );
922     if( !p_input_thread )
923         return;
924
925     var_SetInteger( p_input_thread, "chapter", chapter );
926     vlc_object_release( p_input_thread );
927 }
928
929 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
930 {
931     input_thread_t *p_input_thread;
932     int i_chapter;
933
934     p_input_thread = libvlc_get_input_thread ( p_mi );
935     if( !p_input_thread )
936         return -1;
937
938     i_chapter = var_GetInteger( p_input_thread, "chapter" );
939     vlc_object_release( p_input_thread );
940
941     return i_chapter;
942 }
943
944 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
945 {
946     input_thread_t *p_input_thread;
947     vlc_value_t val;
948
949     p_input_thread = libvlc_get_input_thread ( p_mi );
950     if( !p_input_thread )
951         return -1;
952
953     var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
954     vlc_object_release( p_input_thread );
955
956     return val.i_int;
957 }
958
959 int libvlc_media_player_get_chapter_count_for_title(
960                                  libvlc_media_player_t *p_mi,
961                                  int i_title )
962 {
963     input_thread_t *p_input_thread;
964     vlc_value_t val;
965
966     p_input_thread = libvlc_get_input_thread ( p_mi );
967     if( !p_input_thread )
968         return -1;
969
970     char *psz_name;
971     if( asprintf( &psz_name,  "title %2i", i_title ) == -1 )
972     {
973         vlc_object_release( p_input_thread );
974         return -1;
975     }
976     var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
977     vlc_object_release( p_input_thread );
978     free( psz_name );
979
980     return val.i_int;
981 }
982
983 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
984                                     int i_title )
985 {
986     input_thread_t *p_input_thread;
987
988     p_input_thread = libvlc_get_input_thread ( p_mi );
989     if( !p_input_thread )
990         return;
991
992     var_SetInteger( p_input_thread, "title", i_title );
993     vlc_object_release( p_input_thread );
994
995     //send event
996     libvlc_event_t event;
997     event.type = libvlc_MediaPlayerTitleChanged;
998     event.u.media_player_title_changed.new_title = i_title;
999     libvlc_event_send( p_mi->p_event_manager, &event );
1000 }
1001
1002 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1003 {
1004     input_thread_t *p_input_thread;
1005     int i_title;
1006
1007     p_input_thread = libvlc_get_input_thread ( p_mi );
1008     if( !p_input_thread )
1009         return -1;
1010
1011     i_title = var_GetInteger( p_input_thread, "title" );
1012     vlc_object_release( p_input_thread );
1013
1014     return i_title;
1015 }
1016
1017 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1018 {
1019     input_thread_t *p_input_thread;
1020     vlc_value_t val;
1021
1022     p_input_thread = libvlc_get_input_thread ( p_mi );
1023     if( !p_input_thread )
1024         return -1;
1025
1026     var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1027     vlc_object_release( p_input_thread );
1028
1029     return val.i_int;
1030 }
1031
1032 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1033 {
1034     input_thread_t *p_input_thread;
1035
1036     p_input_thread = libvlc_get_input_thread ( p_mi );
1037     if( !p_input_thread )
1038         return;
1039
1040     int i_type = var_Type( p_input_thread, "next-chapter" );
1041     var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1042                             "next-chapter":"next-title", true );
1043
1044     vlc_object_release( p_input_thread );
1045 }
1046
1047 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1048 {
1049     input_thread_t *p_input_thread;
1050
1051     p_input_thread = libvlc_get_input_thread ( p_mi );
1052     if( !p_input_thread )
1053         return;
1054
1055     int i_type = var_Type( p_input_thread, "next-chapter" );
1056     var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1057                             "prev-chapter":"prev-title", true );
1058
1059     vlc_object_release( p_input_thread );
1060 }
1061
1062 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1063 {
1064     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1065     double f_fps = 0.0;
1066
1067     if( p_input_thread )
1068     {
1069         if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1070             f_fps = 0.0;
1071         vlc_object_release( p_input_thread );
1072     }
1073     return f_fps;
1074 }
1075
1076 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1077 {
1078     bool b_will_play;
1079     input_thread_t *p_input_thread =
1080                             libvlc_get_input_thread ( p_mi );
1081     if ( !p_input_thread )
1082         return false;
1083
1084     b_will_play = !p_input_thread->b_die && !p_input_thread->b_dead;
1085     vlc_object_release( p_input_thread );
1086
1087     return b_will_play;
1088 }
1089
1090 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1091 {
1092     if (rate < 0.)
1093     {
1094         libvlc_printerr ("Playing backward not supported");
1095         return -1;
1096     }
1097
1098     var_SetFloat (p_mi, "rate", rate);
1099
1100     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1101     if( !p_input_thread )
1102         return 0;
1103     var_SetFloat( p_input_thread, "rate", rate );
1104     vlc_object_release( p_input_thread );
1105     return 0;
1106 }
1107
1108 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1109 {
1110     return var_GetFloat (p_mi, "rate");
1111 }
1112
1113 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1114 {
1115     lock(p_mi);
1116     libvlc_state_t state = p_mi->state;
1117     unlock(p_mi);
1118     return state;
1119 }
1120
1121 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1122 {
1123     input_thread_t *p_input_thread;
1124     bool b_seekable;
1125
1126     p_input_thread = libvlc_get_input_thread ( p_mi );
1127     if ( !p_input_thread )
1128         return false;
1129     b_seekable = var_GetBool( p_input_thread, "can-seek" );
1130     vlc_object_release( p_input_thread );
1131
1132     return b_seekable;
1133 }
1134
1135 /* internal function, used by audio, video */
1136 libvlc_track_description_t *
1137         libvlc_get_track_description( libvlc_media_player_t *p_mi,
1138                                       const char *psz_variable )
1139 {
1140     input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1141     libvlc_track_description_t *p_track_description = NULL,
1142                                *p_actual, *p_previous;
1143
1144     if( !p_input )
1145         return NULL;
1146
1147     vlc_value_t val_list, text_list;
1148     var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1149
1150     /* no tracks */
1151     if( val_list.p_list->i_count <= 0 )
1152         goto end;
1153
1154     p_track_description = ( libvlc_track_description_t * )
1155         malloc( sizeof( libvlc_track_description_t ) );
1156     if ( !p_track_description )
1157     {
1158         libvlc_printerr( "Not enough memory" );
1159         goto end;
1160     }
1161     p_actual = p_track_description;
1162     p_previous = NULL;
1163     for( int i = 0; i < val_list.p_list->i_count; i++ )
1164     {
1165         if( !p_actual )
1166         {
1167             p_actual = ( libvlc_track_description_t * )
1168                 malloc( sizeof( libvlc_track_description_t ) );
1169             if ( !p_actual )
1170             {
1171                 libvlc_track_description_release( p_track_description );
1172                 libvlc_printerr( "Not enough memory" );
1173                 goto end;
1174             }
1175         }
1176         p_actual->i_id = val_list.p_list->p_values[i].i_int;
1177         p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1178         p_actual->p_next = NULL;
1179         if( p_previous )
1180             p_previous->p_next = p_actual;
1181         p_previous = p_actual;
1182         p_actual =  NULL;
1183     }
1184
1185 end:
1186     var_FreeList( &val_list, &text_list );
1187     vlc_object_release( p_input );
1188
1189     return p_track_description;
1190 }
1191
1192 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1193 {
1194     libvlc_track_description_t *p_actual, *p_before;
1195     p_actual = p_td;
1196
1197     while ( p_actual )
1198     {
1199         free( p_actual->psz_name );
1200         p_before = p_actual;
1201         p_actual = p_before->p_next;
1202         free( p_before );
1203     }
1204 }
1205
1206 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1207 {
1208     input_thread_t *p_input_thread;
1209     bool b_can_pause;
1210
1211     p_input_thread = libvlc_get_input_thread ( p_mi );
1212     if ( !p_input_thread )
1213         return false;
1214     b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1215     vlc_object_release( p_input_thread );
1216
1217     return b_can_pause;
1218 }
1219
1220 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1221 {
1222     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1223     if( p_input_thread != NULL )
1224     {
1225         var_TriggerCallback( p_input_thread, "frame-next" );
1226         vlc_object_release( p_input_thread );
1227     }
1228 }