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