]> git.sesse.net Git - vlc/blob - src/control/media_player.c
6dbb8c6a6a3df7f0be14628326d45dfce133e15a
[vlc] / src / control / media_player.c
1 /*****************************************************************************
2  * media_player.c: Libvlc API Media Instance management functions
3  *****************************************************************************
4  * Copyright (C) 2005 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_StopThread( 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 /**************************************************************************
246  * Create a Media Instance object.
247  *
248  * Refcount strategy:
249  * - All items created by _new start with a refcount set to 1.
250  * - Accessor _release decrease the refcount by 1, if after that
251  *   operation the refcount is 0, the object is destroyed.
252  * - Accessor _retain increase the refcount by 1 (XXX: to implement)
253  *
254  * Object locking strategy:
255  * - No lock held while in constructor.
256  * - When accessing any member variable this lock is held. (XXX who locks?)
257  * - When attempting to destroy the object the lock is also held.
258  **************************************************************************/
259 libvlc_media_player_t *
260 libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
261                            libvlc_exception_t * p_e )
262 {
263     libvlc_media_player_t * p_mi;
264
265     if( !p_libvlc_instance )
266     {
267         libvlc_exception_raise( p_e, "invalid libvlc instance" );
268         return NULL;
269     }
270
271     p_mi = malloc( sizeof(libvlc_media_player_t) );
272     if( !p_mi )
273     {
274         libvlc_exception_raise( p_e, "Not enough memory" );
275         return NULL;
276     }
277     p_mi->p_md = NULL;
278     p_mi->drawable.agl = 0;
279     p_mi->drawable.xid = 0;
280     p_mi->drawable.hwnd = 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     /* 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     p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
352
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
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
387     if( !p_mi->p_md )
388     {
389         libvlc_media_player_destroy( p_mi );
390         return NULL;
391     }
392
393     /* will be released in media_player_release() */
394     vlc_object_hold( p_input );
395
396     p_mi->p_input_thread = p_input;
397     p_mi->b_own_its_input_thread = false;
398
399     return p_mi;
400 }
401
402 /**************************************************************************
403  * Destroy a Media Instance object (libvlc internal)
404  *
405  * Warning: No lock held here, but hey, this is internal. Caller must lock.
406  **************************************************************************/
407 void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
408 {
409     input_thread_t *p_input_thread;
410     libvlc_exception_t p_e;
411
412     libvlc_exception_init( &p_e );
413
414     if( !p_mi )
415         return;
416
417         /* Detach Callback from the main libvlc object */
418     var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
419                      "vout-snapshottaken", SnapshotTakenCallback, p_mi );
420
421     p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
422
423     if( libvlc_exception_raised( &p_e ) )
424     {
425         libvlc_event_manager_release( p_mi->p_event_manager );
426         libvlc_exception_clear( &p_e );
427         free( p_mi );
428         return; /* no need to worry about no input thread */
429     }
430     vlc_mutex_destroy( &p_mi->object_lock );
431
432     vlc_object_release( p_input_thread );
433
434     libvlc_media_release( p_mi->p_md );
435
436     free( p_mi );
437 }
438
439 /**************************************************************************
440  * Release a Media Instance object.
441  *
442  * Function does the locking.
443  **************************************************************************/
444 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
445 {
446     if( !p_mi )
447         return;
448
449     vlc_mutex_lock( &p_mi->object_lock );
450
451     p_mi->i_refcount--;
452
453     if( p_mi->i_refcount > 0 )
454     {
455         vlc_mutex_unlock( &p_mi->object_lock );
456         return;
457     }
458     vlc_mutex_unlock( &p_mi->object_lock );
459     vlc_mutex_destroy( &p_mi->object_lock );
460
461     release_input_thread( p_mi, true );
462
463     libvlc_event_manager_release( p_mi->p_event_manager );
464
465     libvlc_media_release( p_mi->p_md );
466
467     free( p_mi );
468 }
469
470 /**************************************************************************
471  * Retain a Media Instance object.
472  *
473  * Caller must hold the lock.
474  **************************************************************************/
475 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
476 {
477     if( !p_mi )
478         return;
479
480     p_mi->i_refcount++;
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_CreateThread(
611             p_mi->p_libvlc_instance->p_libvlc_int, p_mi->p_md->p_input_item );
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_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
637     var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
638     var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
639
640     vlc_mutex_unlock( &p_mi->object_lock );
641 }
642
643 /**************************************************************************
644  * Pause.
645  **************************************************************************/
646 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
647                                   libvlc_exception_t *p_e )
648 {
649     input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
650
651     if( !p_input_thread )
652         return;
653
654     libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
655
656     if( state == libvlc_Playing )
657     {
658         if( libvlc_media_player_can_pause( p_mi, p_e ) )
659             input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
660         else
661             libvlc_media_player_stop( p_mi, p_e );
662     }
663     else
664         input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
665
666     vlc_object_release( p_input_thread );
667 }
668
669 /**************************************************************************
670  * Tells whether the media player is currently playing.
671  *
672  * Enter with lock held.
673  **************************************************************************/
674 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
675                                      libvlc_exception_t *p_e )
676 {
677     libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
678     return libvlc_Playing == state;
679 }
680
681
682 /**************************************************************************
683  * Stop playing.
684  **************************************************************************/
685 void libvlc_media_player_stop( 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
690     if( state == libvlc_Playing || state == libvlc_Paused )
691     {
692         /* Send a stop notification event only if we are in playing or
693          * paused states */
694         libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
695
696         /* Construct and send the event */
697         libvlc_event_t event;
698         event.type = libvlc_MediaPlayerStopped;
699         libvlc_event_send( p_mi->p_event_manager, &event );
700     }
701
702     if( p_mi->b_own_its_input_thread )
703     {
704         vlc_mutex_lock( &p_mi->object_lock );
705         release_input_thread( p_mi, true ); /* This will stop the input thread */
706         vlc_mutex_unlock( &p_mi->object_lock );
707     }
708     else
709     {
710         input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
711
712         if( !p_input_thread )
713             return;
714
715         input_StopThread( p_input_thread, true );
716         vlc_object_release( p_input_thread );
717         p_mi->p_input_thread = NULL;
718     }
719 }
720
721 /**************************************************************************
722  * set_agl
723  **************************************************************************/
724 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
725                                       uint32_t drawable,
726                                       libvlc_exception_t *p_e )
727 {
728     (void) p_e;
729     p_mi->drawable.agl = drawable;
730 }
731
732 /**************************************************************************
733  * get_agl
734  **************************************************************************/
735 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
736 {
737     return p_mi->drawable.agl;
738 }
739
740 /**************************************************************************
741  * set_xwindow
742  **************************************************************************/
743 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
744                                       uint32_t drawable,
745                                       libvlc_exception_t *p_e )
746 {
747     (void) p_e;
748     p_mi->drawable.xid = drawable;
749 }
750
751 /**************************************************************************
752  * get_xwindow
753  **************************************************************************/
754 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
755 {
756     return p_mi->drawable.xid;
757 }
758
759 /**************************************************************************
760  * set_hwnd
761  **************************************************************************/
762 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
763                                    void *drawable,
764                                    libvlc_exception_t *p_e )
765 {
766     (void) p_e;
767     p_mi->drawable.hwnd = drawable;
768 }
769
770 /**************************************************************************
771  * get_hwnd
772  **************************************************************************/
773 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
774 {
775     return p_mi->drawable.hwnd;
776 }
777
778 /**************************************************************************
779  * Set Drawable
780  **************************************************************************/
781 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
782                                        libvlc_drawable_t drawable,
783                                        libvlc_exception_t *p_e )
784 {
785 #ifdef WIN32
786     if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
787         p_mi->drawable.hwnd = (HWND)drawable;
788     else
789         libvlc_exception_raise(p_e, "Operation not supported");
790 #elif defined(__APPLE__)
791     p_mi->drawable.agl = drawable;
792     (void) p_e;
793 #else
794     p_mi->drawable.xid = drawable;
795     (void) p_e;
796 #endif
797 }
798
799 /**************************************************************************
800  * Get Drawable
801  **************************************************************************/
802 libvlc_drawable_t
803 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi,
804                                    libvlc_exception_t *p_e )
805 {
806     VLC_UNUSED(p_e);
807
808 #ifdef WIN32
809     if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
810         return (libvlc_drawable_t)p_mi->drawable.hwnd;
811     else
812         return 0;
813 #elif defined(__APPLE__)
814     return p_mi->drawable.agl;
815 #else
816     return p_mi->drawable.xid;
817 #endif
818 }
819
820 /**************************************************************************
821  * Getters for stream information
822  **************************************************************************/
823 libvlc_time_t libvlc_media_player_get_length(
824                              libvlc_media_player_t *p_mi,
825                              libvlc_exception_t *p_e )
826 {
827     input_thread_t *p_input_thread;
828     vlc_value_t val;
829
830     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
831     if( !p_input_thread )
832         return -1;
833
834     var_Get( p_input_thread, "length", &val );
835     vlc_object_release( p_input_thread );
836
837     return (val.i_time+500LL)/1000LL;
838 }
839
840 libvlc_time_t libvlc_media_player_get_time(
841                                    libvlc_media_player_t *p_mi,
842                                    libvlc_exception_t *p_e )
843 {
844     input_thread_t *p_input_thread;
845     vlc_value_t val;
846
847     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
848     if( !p_input_thread )
849         return -1;
850
851     var_Get( p_input_thread , "time", &val );
852     vlc_object_release( p_input_thread );
853     return (val.i_time+500LL)/1000LL;
854 }
855
856 void libvlc_media_player_set_time(
857                                  libvlc_media_player_t *p_mi,
858                                  libvlc_time_t time,
859                                  libvlc_exception_t *p_e )
860 {
861     input_thread_t *p_input_thread;
862
863     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
864     if( !p_input_thread )
865         return;
866
867     var_SetTime( p_input_thread, "time", time*1000LL );
868     vlc_object_release( p_input_thread );
869 }
870
871 void libvlc_media_player_set_position(
872                                 libvlc_media_player_t *p_mi,
873                                 float position,
874                                 libvlc_exception_t *p_e )
875 {
876     input_thread_t *p_input_thread;
877
878     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
879     if( !p_input_thread )
880         return;
881
882     var_SetFloat( p_input_thread, "position", position );
883     vlc_object_release( p_input_thread );
884 }
885
886 float libvlc_media_player_get_position(
887                                  libvlc_media_player_t *p_mi,
888                                  libvlc_exception_t *p_e )
889 {
890     input_thread_t *p_input_thread;
891     vlc_value_t val;
892
893     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
894     if( !p_input_thread )
895         return -1.0;
896
897     var_Get( p_input_thread, "position", &val );
898     vlc_object_release( p_input_thread );
899
900     return val.f_float;
901 }
902
903 void libvlc_media_player_set_chapter(
904                                  libvlc_media_player_t *p_mi,
905                                  int chapter,
906                                  libvlc_exception_t *p_e )
907 {
908     input_thread_t *p_input_thread;
909
910     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
911     if( !p_input_thread )
912         return;
913
914     var_SetInteger( p_input_thread, "chapter", chapter );
915     vlc_object_release( p_input_thread );
916 }
917
918 int libvlc_media_player_get_chapter(
919                                  libvlc_media_player_t *p_mi,
920                                  libvlc_exception_t *p_e )
921 {
922     input_thread_t *p_input_thread;
923     vlc_value_t val;
924
925     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
926     if( !p_input_thread )
927         return -1;
928
929     var_Get( p_input_thread, "chapter", &val );
930     vlc_object_release( p_input_thread );
931
932     return val.i_int;
933 }
934
935 int libvlc_media_player_get_chapter_count(
936                                  libvlc_media_player_t *p_mi,
937                                  libvlc_exception_t *p_e )
938 {
939     input_thread_t *p_input_thread;
940     vlc_value_t val;
941
942     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
943     if( !p_input_thread )
944         return -1;
945
946     var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
947     vlc_object_release( p_input_thread );
948
949     return val.i_int;
950 }
951
952 int libvlc_media_player_get_chapter_count_for_title(
953                                  libvlc_media_player_t *p_mi,
954                                  int i_title,
955                                  libvlc_exception_t *p_e )
956 {
957     input_thread_t *p_input_thread;
958     vlc_value_t val;
959
960     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
961     if( !p_input_thread )
962         return -1;
963
964     char *psz_name;
965     if( asprintf( &psz_name,  "title %2i", i_title ) == -1 )
966     {
967         vlc_object_release( p_input_thread );
968         return -1;
969     }
970     var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
971     vlc_object_release( p_input_thread );
972     free( psz_name );
973
974     return val.i_int;
975 }
976
977 void libvlc_media_player_set_title(
978                                  libvlc_media_player_t *p_mi,
979                                  int i_title,
980                                  libvlc_exception_t *p_e )
981 {
982     input_thread_t *p_input_thread;
983
984     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
985     if( !p_input_thread )
986         return;
987
988     var_SetInteger( p_input_thread, "title", i_title );
989     vlc_object_release( p_input_thread );
990
991     //send event
992     libvlc_event_t event;
993     event.type = libvlc_MediaPlayerTitleChanged;
994     event.u.media_player_title_changed.new_title = i_title;
995     libvlc_event_send( p_mi->p_event_manager, &event );
996 }
997
998 int libvlc_media_player_get_title(
999                                  libvlc_media_player_t *p_mi,
1000                                  libvlc_exception_t *p_e )
1001 {
1002     input_thread_t *p_input_thread;
1003     vlc_value_t val;
1004
1005     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1006     if( !p_input_thread )
1007         return -1;
1008
1009     var_Get( p_input_thread, "title", &val );
1010     vlc_object_release( p_input_thread );
1011
1012     return val.i_int;
1013 }
1014
1015 int libvlc_media_player_get_title_count(
1016                                  libvlc_media_player_t *p_mi,
1017                                  libvlc_exception_t *p_e )
1018 {
1019     input_thread_t *p_input_thread;
1020     vlc_value_t val;
1021
1022     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1023     if( !p_input_thread )
1024         return -1;
1025
1026     var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1027     vlc_object_release( p_input_thread );
1028
1029     return val.i_int;
1030 }
1031
1032 void libvlc_media_player_next_chapter(
1033                                  libvlc_media_player_t *p_mi,
1034                                  libvlc_exception_t *p_e )
1035 {
1036     input_thread_t *p_input_thread;
1037
1038     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1039     if( !p_input_thread )
1040         return;
1041
1042     int i_type = var_Type( p_input_thread, "next-chapter" );
1043     var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1044                             "next-chapter":"next-title", true );
1045
1046     vlc_object_release( p_input_thread );
1047 }
1048
1049 void libvlc_media_player_previous_chapter(
1050                                  libvlc_media_player_t *p_mi,
1051                                  libvlc_exception_t *p_e )
1052 {
1053     input_thread_t *p_input_thread;
1054
1055     p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1056     if( !p_input_thread )
1057         return;
1058
1059     int i_type = var_Type( p_input_thread, "next-chapter" );
1060     var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1061                             "prev-chapter":"prev-title", true );
1062
1063     vlc_object_release( p_input_thread );
1064 }
1065
1066 float libvlc_media_player_get_fps(
1067                                  libvlc_media_player_t *p_mi,
1068                                  libvlc_exception_t *p_e)
1069 {
1070     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1071     double f_fps = 0.0;
1072
1073     if( p_input_thread )
1074     {
1075         if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1076             f_fps = 0.0;
1077         vlc_object_release( p_input_thread );
1078     }
1079     return f_fps;
1080 }
1081
1082 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1083                                      libvlc_exception_t *p_e)
1084 {
1085     input_thread_t *p_input_thread =
1086                             libvlc_get_input_thread ( p_mi, p_e);
1087     if ( !p_input_thread )
1088         return false;
1089
1090     if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1091     {
1092         vlc_object_release( p_input_thread );
1093         return true;
1094     }
1095     vlc_object_release( p_input_thread );
1096     return false;
1097 }
1098
1099 void libvlc_media_player_set_rate(
1100                                  libvlc_media_player_t *p_mi,
1101                                  float rate,
1102                                  libvlc_exception_t *p_e )
1103 {
1104     input_thread_t *p_input_thread;
1105     bool b_can_rewind;
1106
1107     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1108     if( !p_input_thread )
1109         return;
1110
1111     b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1112     if( (rate < 0.0) && !b_can_rewind )
1113     {
1114         vlc_object_release( p_input_thread );
1115         libvlc_exception_raise( p_e, "Rate value is invalid" );
1116         return;
1117     }
1118
1119     var_SetInteger( p_input_thread, "rate", 1000.0f/rate );
1120     vlc_object_release( p_input_thread );
1121 }
1122
1123 float libvlc_media_player_get_rate(
1124                                  libvlc_media_player_t *p_mi,
1125                                  libvlc_exception_t *p_e )
1126 {
1127     input_thread_t *p_input_thread;
1128     vlc_value_t val;
1129     bool b_can_rewind;
1130
1131     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1132     if( !p_input_thread )
1133         return 0.0;  /* rate < 0 indicates rewind */
1134
1135     var_Get( p_input_thread, "rate", &val );
1136     b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1137     if( (val.i_int < 0) && !b_can_rewind )
1138     {
1139         libvlc_exception_raise( p_e, "invalid rate" );
1140         return 0.0;
1141     }
1142     vlc_object_release( p_input_thread );
1143
1144     return (float)1000.0f/val.i_int;
1145 }
1146
1147 libvlc_state_t libvlc_media_player_get_state(
1148                                  libvlc_media_player_t *p_mi,
1149                                  libvlc_exception_t *p_e )
1150 {
1151     input_thread_t *p_input_thread;
1152     libvlc_state_t state = libvlc_Ended;
1153     vlc_value_t val;
1154
1155     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1156     if( !p_input_thread )
1157     {
1158         /* We do return the right value, no need to throw an exception */
1159         if( libvlc_exception_raised( p_e ) )
1160             libvlc_exception_clear( p_e );
1161         return state;
1162     }
1163
1164     var_Get( p_input_thread, "state", &val );
1165     state = vlc_to_libvlc_state(val.i_int);
1166
1167     if( state == libvlc_Playing )
1168     {
1169         float caching;
1170         caching = var_GetFloat( p_input_thread, "cache" );
1171         if( caching > 0.0 && caching < 1.0 )
1172             state = libvlc_Buffering;
1173     }
1174     vlc_object_release( p_input_thread );
1175     return state;
1176 }
1177
1178 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1179                                        libvlc_exception_t *p_e )
1180 {
1181     input_thread_t *p_input_thread;
1182     vlc_value_t val;
1183
1184     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1185     if ( !p_input_thread )
1186     {
1187         /* We do return the right value, no need to throw an exception */
1188         if( libvlc_exception_raised( p_e ) )
1189             libvlc_exception_clear( p_e );
1190         return false;
1191     }
1192     var_Get( p_input_thread, "can-seek", &val );
1193     vlc_object_release( p_input_thread );
1194
1195     return val.b_bool;
1196 }
1197
1198 /* internal function, used by audio, video */
1199 libvlc_track_description_t *
1200         libvlc_get_track_description( libvlc_media_player_t *p_mi,
1201                                       const char *psz_variable,
1202                                       libvlc_exception_t *p_e )
1203 {
1204     input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1205
1206     if( !p_input )
1207         return NULL;
1208
1209     vlc_value_t val_list, text_list;
1210     var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1211
1212     if( val_list.p_list->i_count <= 0 ) /* no tracks */
1213         return NULL;
1214
1215     libvlc_track_description_t *p_track_description, *p_actual, *p_previous;
1216     p_track_description = ( libvlc_track_description_t * )
1217         malloc( sizeof( libvlc_track_description_t ) );
1218     if ( !p_track_description )
1219     {
1220         var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1221                     &val_list, &text_list);
1222         vlc_object_release( p_input );
1223         libvlc_exception_raise( p_e, "no enough memory" );
1224         return NULL;
1225     }
1226     p_actual = p_track_description;
1227     p_previous = NULL;
1228     for( int i = 0; i < val_list.p_list->i_count; i++ )
1229     {
1230         if( !p_actual )
1231         {
1232             p_actual = ( libvlc_track_description_t * )
1233                 malloc( sizeof( libvlc_track_description_t ) );
1234             if ( !p_actual )
1235             {
1236                 libvlc_track_description_release( p_track_description );
1237                 var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1238                             &val_list, &text_list);
1239                 vlc_object_release( p_input );
1240                 libvlc_exception_raise( p_e, "no enough memory" );
1241                 return NULL;
1242             }
1243         }
1244         p_actual->i_id = val_list.p_list->p_values[i].i_int;
1245         p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1246         p_actual->p_next = NULL;
1247         if( p_previous )
1248             p_previous->p_next = p_actual;
1249         p_previous = p_actual;
1250         p_actual =  NULL;
1251     }
1252     var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1253     vlc_object_release( p_input );
1254
1255     return p_track_description;
1256 }
1257
1258 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1259 {
1260     libvlc_track_description_t *p_actual, *p_before;
1261     p_actual = p_td;
1262
1263     while ( p_actual )
1264     {
1265         free( p_actual->psz_name );
1266         p_before = p_actual;
1267         p_actual = p_before->p_next;
1268         free( p_before );
1269     }
1270 }
1271
1272 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1273                                      libvlc_exception_t *p_e )
1274 {
1275     input_thread_t *p_input_thread;
1276     vlc_value_t val;
1277
1278     p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1279     if ( !p_input_thread )
1280     {
1281         /* We do return the right value, no need to throw an exception */
1282         if( libvlc_exception_raised( p_e ) )
1283             libvlc_exception_clear( p_e );
1284         return false;
1285     }
1286     var_Get( p_input_thread, "can-pause", &val );
1287     vlc_object_release( p_input_thread );
1288
1289     return val.b_bool;
1290 }