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