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