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