1 /*****************************************************************************
2 * media_player.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005-2009 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_events.h>
34 #include <vlc_demux.h>
35 #include <vlc_input.h>
40 #include "libvlc_internal.h"
41 #include "media_internal.h" // libvlc_media_set_state()
42 #include "media_player_internal.h"
45 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
46 vlc_value_t oldval, vlc_value_t newval,
49 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
50 vlc_value_t oldval, vlc_value_t newval,
53 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
54 vlc_value_t oldval, vlc_value_t newval,
58 snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
59 vlc_value_t oldval, vlc_value_t newval, void *p_data );
61 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
67 #define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
68 static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type_t type)
70 libvlc_event_manager_register_event_type(mp->p_event_manager, type);
74 * The input lock protects the input and input resource pointer.
75 * It MUST NOT be used from callbacks.
77 * The object lock protects the reset, namely the media and the player state.
78 * It can, and usually needs to be taken from callbacks.
79 * The object lock can be acquired under the input lock... and consequently
80 * the opposite order is STRICTLY PROHIBITED.
82 static inline void lock(libvlc_media_player_t *mp)
84 vlc_mutex_lock(&mp->object_lock);
87 static inline void unlock(libvlc_media_player_t *mp)
89 vlc_mutex_unlock(&mp->object_lock);
92 static inline void lock_input(libvlc_media_player_t *mp)
94 vlc_mutex_lock(&mp->input.lock);
97 static inline void unlock_input(libvlc_media_player_t *mp)
99 vlc_mutex_unlock(&mp->input.lock);
103 * Release the associated input thread.
105 * Object lock is NOT held.
106 * Input lock is held or instance is being destroyed.
108 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
112 input_thread_t *p_input_thread = p_mi->input.p_thread;
113 if( !p_input_thread )
116 var_DelCallback( p_input_thread, "can-seek",
117 input_seekable_changed, p_mi );
118 var_DelCallback( p_input_thread, "can-pause",
119 input_pausable_changed, p_mi );
120 var_DelCallback( p_input_thread, "intf-event",
121 input_event_changed, p_mi );
123 /* We owned this one */
124 input_Stop( p_input_thread, b_input_abort );
126 vlc_thread_join( p_input_thread );
128 assert( p_input_thread->b_dead );
130 p_mi->input.p_thread = NULL;
131 vlc_object_release( p_input_thread );
135 * Retrieve the input thread. Be sure to release the object
136 * once you are done with it. (libvlc Internal)
138 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
140 input_thread_t *p_input_thread;
145 p_input_thread = p_mi->input.p_thread;
147 vlc_object_hold( p_input_thread );
149 libvlc_printerr( "No active input" );
152 return p_input_thread;
156 * Set the internal state of the media_player. (media player Internal)
158 * Function will lock the media_player.
160 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
167 libvlc_media_t *media = p_mi->p_md;
169 libvlc_media_retain(media);
176 // Also set the state of the corresponding media
177 // This is strictly for convenience.
178 libvlc_media_set_state(media, state);
180 libvlc_media_release(media);
185 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
186 vlc_value_t oldval, vlc_value_t newval,
192 libvlc_media_player_t * p_mi = p_userdata;
193 libvlc_event_t event;
195 event.type = libvlc_MediaPlayerSeekableChanged;
196 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
198 libvlc_event_send( p_mi->p_event_manager, &event );
203 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
204 vlc_value_t oldval, vlc_value_t newval,
210 libvlc_media_player_t * p_mi = p_userdata;
211 libvlc_event_t event;
213 event.type = libvlc_MediaPlayerPausableChanged;
214 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
216 libvlc_event_send( p_mi->p_event_manager, &event );
221 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
222 vlc_value_t oldval, vlc_value_t newval,
226 input_thread_t * p_input = (input_thread_t *)p_this;
227 libvlc_media_player_t * p_mi = p_userdata;
228 libvlc_event_t event;
230 assert( !strcmp( psz_cmd, "intf-event" ) );
232 if( newval.i_int == INPUT_EVENT_STATE )
234 libvlc_state_t libvlc_state;
236 switch ( var_GetInteger( p_input, "state" ) )
239 libvlc_state = libvlc_NothingSpecial;
240 event.type = libvlc_MediaPlayerNothingSpecial;
243 libvlc_state = libvlc_Opening;
244 event.type = libvlc_MediaPlayerOpening;
247 libvlc_state = libvlc_Playing;
248 event.type = libvlc_MediaPlayerPlaying;
251 libvlc_state = libvlc_Paused;
252 event.type = libvlc_MediaPlayerPaused;
255 libvlc_state = libvlc_Ended;
256 event.type = libvlc_MediaPlayerEndReached;
259 libvlc_state = libvlc_Error;
260 event.type = libvlc_MediaPlayerEncounteredError;
267 set_state( p_mi, libvlc_state, false );
268 libvlc_event_send( p_mi->p_event_manager, &event );
270 else if( newval.i_int == INPUT_EVENT_ABORT )
272 libvlc_state_t libvlc_state = libvlc_Stopped;
273 event.type = libvlc_MediaPlayerStopped;
275 set_state( p_mi, libvlc_state, false );
276 libvlc_event_send( p_mi->p_event_manager, &event );
278 else if( newval.i_int == INPUT_EVENT_POSITION )
280 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
281 return VLC_SUCCESS; /* Don't send the position while stopped */
284 event.type = libvlc_MediaPlayerPositionChanged;
285 event.u.media_player_position_changed.new_position =
286 var_GetFloat( p_input, "position" );
287 libvlc_event_send( p_mi->p_event_manager, &event );
290 event.type = libvlc_MediaPlayerTimeChanged;
291 event.u.media_player_time_changed.new_time =
292 from_mtime(var_GetTime( p_input, "time" ));
293 libvlc_event_send( p_mi->p_event_manager, &event );
295 else if( newval.i_int == INPUT_EVENT_LENGTH )
297 event.type = libvlc_MediaPlayerLengthChanged;
298 event.u.media_player_length_changed.new_length =
299 from_mtime(var_GetTime( p_input, "length" ));
300 libvlc_event_send( p_mi->p_event_manager, &event );
307 /**************************************************************************
308 * Snapshot Taken Event.
310 * FIXME: This snapshot API interface makes no sense in media_player.
311 *************************************************************************/
312 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
313 vlc_value_t oldval, vlc_value_t newval, void *p_data )
315 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
317 libvlc_media_player_t *mp = p_data;
318 libvlc_event_t event;
319 event.type = libvlc_MediaPlayerSnapshotTaken;
320 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
321 libvlc_event_send(mp->p_event_manager, &event);
326 static input_thread_t *find_input (vlc_object_t *obj)
328 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
330 return libvlc_get_input_thread (mp);
334 static void libvlc_media_player_destroy( libvlc_media_player_t * );
337 /**************************************************************************
338 * Create a Media Instance object.
341 * - All items created by _new start with a refcount set to 1.
342 * - Accessor _release decrease the refcount by 1, if after that
343 * operation the refcount is 0, the object is destroyed.
344 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
346 * Object locking strategy:
347 * - No lock held while in constructor.
348 * - When accessing any member variable this lock is held. (XXX who locks?)
349 * - When attempting to destroy the object the lock is also held.
350 **************************************************************************/
351 libvlc_media_player_t *
352 libvlc_media_player_new( libvlc_instance_t *instance )
354 libvlc_media_player_t * mp;
358 mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
359 if (unlikely(mp == NULL))
361 libvlc_printerr("Not enough memory");
364 vlc_object_attach (mp, mp->p_libvlc);
367 var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
370 var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
371 var_Create (mp, "window", VLC_VAR_STRING);
372 var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
374 var_Create (mp, "drawable-hwnd", VLC_VAR_ADDRESS);
377 var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
378 var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
381 var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
382 var_SetBool (mp, "keyboard-events", true);
383 var_Create (mp, "mouse-events", VLC_VAR_BOOL);
384 var_SetBool (mp, "mouse-events", true);
386 var_Create (mp, "fullscreen", VLC_VAR_BOOL);
387 var_Create (mp, "autoscale", VLC_VAR_BOOL);
388 var_SetBool (mp, "autoscale", true);
389 var_Create (mp, "scale", VLC_VAR_FLOAT);
390 var_SetFloat (mp, "scale", 1.);
391 var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
392 var_Create (mp, "crop", VLC_VAR_STRING);
393 var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
394 var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
396 var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
398 var_Create (mp, "marq-marquee", VLC_VAR_STRING);
399 var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
400 var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
401 var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
402 var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
403 var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
404 var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
405 var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
406 var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
408 var_Create (mp, "logo-file", VLC_VAR_STRING);
409 var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
410 var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
411 var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
412 var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
413 var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
414 var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
416 var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
417 var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
418 var_Create (mp, "hue", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
419 var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
420 var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
423 var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
424 var_Create (mp, "volume-muted", VLC_VAR_BOOL);
425 var_Create (mp, "saved-volume", VLC_VAR_INTEGER);
426 var_Create (mp, "volume-change", VLC_VAR_VOID);
427 var_Create (mp, "find-input-callback", VLC_VAR_ADDRESS);
428 var_SetAddress (mp, "find-input-callback", find_input);
431 mp->state = libvlc_NothingSpecial;
432 mp->p_libvlc_instance = instance;
433 mp->input.p_thread = NULL;
434 mp->input.p_resource = NULL;
435 vlc_mutex_init (&mp->input.lock);
437 mp->p_event_manager = libvlc_event_manager_new(mp, instance);
438 if (unlikely(mp->p_event_manager == NULL))
440 vlc_object_release(mp);
443 vlc_mutex_init(&mp->object_lock);
445 register_event(mp, NothingSpecial);
446 register_event(mp, Opening);
447 register_event(mp, Buffering);
448 register_event(mp, Playing);
449 register_event(mp, Paused);
450 register_event(mp, Stopped);
451 register_event(mp, Forward);
452 register_event(mp, Backward);
453 register_event(mp, EndReached);
454 register_event(mp, EncounteredError);
455 register_event(mp, SeekableChanged);
457 register_event(mp, PositionChanged);
458 register_event(mp, TimeChanged);
459 register_event(mp, LengthChanged);
460 register_event(mp, TitleChanged);
461 register_event(mp, PausableChanged);
463 /* Snapshot initialization */
464 register_event(mp, SnapshotTaken);
466 register_event(mp, MediaChanged);
468 /* Attach a var callback to the global object to provide the glue between
469 * vout_thread that generates the event and media_player that re-emits it
470 * with its own event manager
472 * FIXME: It's unclear why we want to put this in public API, and why we
473 * want to expose it in such a limiting and ugly way.
475 var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
477 libvlc_retain(instance);
481 /**************************************************************************
482 * Create a Media Instance object with a media descriptor.
483 **************************************************************************/
484 libvlc_media_player_t *
485 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
487 libvlc_media_player_t * p_mi;
489 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
493 libvlc_media_retain( p_md );
499 /**************************************************************************
500 * Destroy a Media Instance object (libvlc internal)
502 * Warning: No lock held here, but hey, this is internal. Caller must lock.
503 **************************************************************************/
504 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
508 /* Detach Callback from the main libvlc object */
509 var_DelCallback( p_mi->p_libvlc,
510 "snapshot-file", snapshot_was_taken, p_mi );
512 /* No need for lock_input() because no other threads knows us anymore */
513 if( p_mi->input.p_thread )
514 release_input_thread(p_mi, true);
515 if( p_mi->input.p_resource )
517 input_resource_Terminate( p_mi->input.p_resource );
518 input_resource_Release( p_mi->input.p_resource );
519 p_mi->input.p_resource = NULL;
521 vlc_mutex_destroy( &p_mi->input.lock );
523 libvlc_event_manager_release( p_mi->p_event_manager );
524 libvlc_media_release( p_mi->p_md );
525 vlc_mutex_destroy( &p_mi->object_lock );
527 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
528 vlc_object_release( p_mi );
529 libvlc_release(instance);
532 /**************************************************************************
533 * Release a Media Instance object.
535 * Function does the locking.
536 **************************************************************************/
537 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
543 destroy = !--p_mi->i_refcount;
547 libvlc_media_player_destroy( p_mi );
550 /**************************************************************************
551 * Retain a Media Instance object.
553 * Caller must hold the lock.
554 **************************************************************************/
555 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
564 /**************************************************************************
565 * Set the Media descriptor associated with the instance.
567 * Enter without lock -- function will lock the object.
568 **************************************************************************/
569 void libvlc_media_player_set_media(
570 libvlc_media_player_t *p_mi,
571 libvlc_media_t *p_md )
575 /* FIXME I am not sure if it is a user request or on die(eof/error)
577 release_input_thread( p_mi,
578 p_mi->input.p_thread &&
579 !p_mi->input.p_thread->b_eof &&
580 !p_mi->input.p_thread->b_error );
583 set_state( p_mi, libvlc_NothingSpecial, true );
584 unlock_input( p_mi );
586 libvlc_media_release( p_mi->p_md );
592 return; /* It is ok to pass a NULL md */
595 libvlc_media_retain( p_md );
598 /* The policy here is to ignore that we were created using a different
599 * libvlc_instance, because we don't really care */
600 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
604 /* Send an event for the newly available media */
605 libvlc_event_t event;
606 event.type = libvlc_MediaPlayerMediaChanged;
607 event.u.media_player_media_changed.new_media = p_md;
608 libvlc_event_send( p_mi->p_event_manager, &event );
612 /**************************************************************************
613 * Get the Media descriptor associated with the instance.
614 **************************************************************************/
616 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
623 libvlc_media_retain( p_mi->p_md );
628 /**************************************************************************
629 * Get the event Manager.
630 **************************************************************************/
631 libvlc_event_manager_t *
632 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
634 return p_mi->p_event_manager;
637 /**************************************************************************
638 * Tell media player to start playing.
639 **************************************************************************/
640 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
644 input_thread_t *p_input_thread = p_mi->input.p_thread;
647 /* A thread already exists, send it a play message */
648 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
649 unlock_input( p_mi );
653 /* Ignore previous exception */
659 unlock_input( p_mi );
660 libvlc_printerr( "No associated media descriptor" );
664 if( !p_mi->input.p_resource )
665 p_mi->input.p_resource = input_resource_New( VLC_OBJECT( p_mi ) );
666 p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
667 p_mi->input.p_resource );
669 if( !p_input_thread )
672 libvlc_printerr( "Not enough memory" );
676 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
677 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
678 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
680 if( input_Start( p_input_thread ) )
683 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
684 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
685 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
686 vlc_object_release( p_input_thread );
687 libvlc_printerr( "Input initialization failure" );
690 p_mi->input.p_thread = p_input_thread;
695 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
697 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
698 if( !p_input_thread )
701 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
702 if( state == libvlc_Playing || state == libvlc_Buffering )
706 if( libvlc_media_player_can_pause( p_mi ) )
707 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
709 libvlc_media_player_stop( p_mi );
715 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
718 vlc_object_release( p_input_thread );
721 /**************************************************************************
723 **************************************************************************/
724 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
726 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
727 bool playing = (state == libvlc_Playing || state == libvlc_Buffering);
729 libvlc_media_player_set_pause( p_mi, playing );
732 /**************************************************************************
733 * Tells whether the media player is currently playing.
735 * Enter with lock held.
736 **************************************************************************/
737 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
739 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
740 return (libvlc_Playing == state) || (libvlc_Buffering == state);
743 /**************************************************************************
745 **************************************************************************/
746 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
748 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
751 release_input_thread( p_mi, true ); /* This will stop the input thread */
753 /* Force to go to stopped state, in case we were in Ended, or Error
755 if( state != libvlc_Stopped )
757 set_state( p_mi, libvlc_Stopped, false );
759 /* Construct and send the event */
760 libvlc_event_t event;
761 event.type = libvlc_MediaPlayerStopped;
762 libvlc_event_send( p_mi->p_event_manager, &event );
765 if( p_mi->input.p_resource != NULL )
766 input_resource_TerminateVout( p_mi->input.p_resource );
770 /**************************************************************************
772 **************************************************************************/
773 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
776 assert (p_mi != NULL);
778 var_SetAddress (p_mi, "drawable-nsobject", drawable);
780 (void) p_mi; (void)drawable;
784 /**************************************************************************
786 **************************************************************************/
787 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
789 assert (p_mi != NULL);
791 return var_GetAddress (p_mi, "drawable-nsobject");
797 /**************************************************************************
799 **************************************************************************/
800 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
804 var_SetInteger (p_mi, "drawable-agl", drawable);
806 (void) p_mi; (void)drawable;
810 /**************************************************************************
812 **************************************************************************/
813 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
815 assert (p_mi != NULL);
817 return var_GetInteger (p_mi, "drawable-agl");
823 /**************************************************************************
825 **************************************************************************/
826 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
829 assert (p_mi != NULL);
831 var_SetString (p_mi, "vout", drawable ? "xid" : "any");
832 var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "any");
833 var_SetInteger (p_mi, "drawable-xid", drawable);
836 /**************************************************************************
838 **************************************************************************/
839 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
841 return var_GetInteger (p_mi, "drawable-xid");
844 /**************************************************************************
846 **************************************************************************/
847 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
850 assert (p_mi != NULL);
852 var_SetString (p_mi, "window",
853 (drawable != NULL) ? "embed-hwnd,any" : "");
854 var_SetAddress (p_mi, "drawable-hwnd", drawable);
856 (void) p_mi; (void) drawable;
860 /**************************************************************************
862 **************************************************************************/
863 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
865 assert (p_mi != NULL);
867 return var_GetAddress (p_mi, "drawable-hwnd");
873 /**************************************************************************
874 * Getters for stream information
875 **************************************************************************/
876 libvlc_time_t libvlc_media_player_get_length(
877 libvlc_media_player_t *p_mi )
879 input_thread_t *p_input_thread;
880 libvlc_time_t i_time;
882 p_input_thread = libvlc_get_input_thread ( p_mi );
883 if( !p_input_thread )
886 i_time = from_mtime(var_GetTime( p_input_thread, "length" ));
887 vlc_object_release( p_input_thread );
892 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
894 input_thread_t *p_input_thread;
895 libvlc_time_t i_time;
897 p_input_thread = libvlc_get_input_thread ( p_mi );
898 if( !p_input_thread )
901 i_time = from_mtime(var_GetTime( p_input_thread , "time" ));
902 vlc_object_release( p_input_thread );
906 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
907 libvlc_time_t i_time )
909 input_thread_t *p_input_thread;
911 p_input_thread = libvlc_get_input_thread ( p_mi );
912 if( !p_input_thread )
915 var_SetTime( p_input_thread, "time", to_mtime(i_time) );
916 vlc_object_release( p_input_thread );
919 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
922 input_thread_t *p_input_thread;
924 p_input_thread = libvlc_get_input_thread ( p_mi );
925 if( !p_input_thread )
928 var_SetFloat( p_input_thread, "position", position );
929 vlc_object_release( p_input_thread );
932 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
934 input_thread_t *p_input_thread;
937 p_input_thread = libvlc_get_input_thread ( p_mi );
938 if( !p_input_thread )
941 f_position = var_GetFloat( p_input_thread, "position" );
942 vlc_object_release( p_input_thread );
947 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
950 input_thread_t *p_input_thread;
952 p_input_thread = libvlc_get_input_thread ( p_mi );
953 if( !p_input_thread )
956 var_SetInteger( p_input_thread, "chapter", chapter );
957 vlc_object_release( p_input_thread );
960 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
962 input_thread_t *p_input_thread;
965 p_input_thread = libvlc_get_input_thread ( p_mi );
966 if( !p_input_thread )
969 i_chapter = var_GetInteger( p_input_thread, "chapter" );
970 vlc_object_release( p_input_thread );
975 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
977 input_thread_t *p_input_thread;
980 p_input_thread = libvlc_get_input_thread ( p_mi );
981 if( !p_input_thread )
984 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
985 vlc_object_release( p_input_thread );
990 int libvlc_media_player_get_chapter_count_for_title(
991 libvlc_media_player_t *p_mi,
994 input_thread_t *p_input_thread;
997 p_input_thread = libvlc_get_input_thread ( p_mi );
998 if( !p_input_thread )
1002 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
1004 vlc_object_release( p_input_thread );
1007 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1008 vlc_object_release( p_input_thread );
1014 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1017 input_thread_t *p_input_thread;
1019 p_input_thread = libvlc_get_input_thread ( p_mi );
1020 if( !p_input_thread )
1023 var_SetInteger( p_input_thread, "title", i_title );
1024 vlc_object_release( p_input_thread );
1027 libvlc_event_t event;
1028 event.type = libvlc_MediaPlayerTitleChanged;
1029 event.u.media_player_title_changed.new_title = i_title;
1030 libvlc_event_send( p_mi->p_event_manager, &event );
1033 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1035 input_thread_t *p_input_thread;
1038 p_input_thread = libvlc_get_input_thread ( p_mi );
1039 if( !p_input_thread )
1042 i_title = var_GetInteger( p_input_thread, "title" );
1043 vlc_object_release( p_input_thread );
1048 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1050 input_thread_t *p_input_thread;
1053 p_input_thread = libvlc_get_input_thread ( p_mi );
1054 if( !p_input_thread )
1057 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1058 vlc_object_release( p_input_thread );
1063 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1065 input_thread_t *p_input_thread;
1067 p_input_thread = libvlc_get_input_thread ( p_mi );
1068 if( !p_input_thread )
1071 int i_type = var_Type( p_input_thread, "next-chapter" );
1072 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1073 "next-chapter":"next-title", true );
1075 vlc_object_release( p_input_thread );
1078 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1080 input_thread_t *p_input_thread;
1082 p_input_thread = libvlc_get_input_thread ( p_mi );
1083 if( !p_input_thread )
1086 int i_type = var_Type( p_input_thread, "next-chapter" );
1087 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1088 "prev-chapter":"prev-title", true );
1090 vlc_object_release( p_input_thread );
1093 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1095 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1098 if( p_input_thread )
1100 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1102 vlc_object_release( p_input_thread );
1107 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1110 input_thread_t *p_input_thread =
1111 libvlc_get_input_thread ( p_mi );
1112 if ( !p_input_thread )
1115 b_will_play = !p_input_thread->b_die && !p_input_thread->b_dead;
1116 vlc_object_release( p_input_thread );
1121 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1125 libvlc_printerr ("Playing backward not supported");
1129 var_SetFloat (p_mi, "rate", rate);
1131 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1132 if( !p_input_thread )
1134 var_SetFloat( p_input_thread, "rate", rate );
1135 vlc_object_release( p_input_thread );
1139 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1141 return var_GetFloat (p_mi, "rate");
1144 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1147 libvlc_state_t state = p_mi->state;
1152 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1154 input_thread_t *p_input_thread;
1157 p_input_thread = libvlc_get_input_thread ( p_mi );
1158 if ( !p_input_thread )
1160 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1161 vlc_object_release( p_input_thread );
1166 /* internal function, used by audio, video */
1167 libvlc_track_description_t *
1168 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1169 const char *psz_variable )
1171 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1172 libvlc_track_description_t *p_track_description = NULL,
1173 *p_actual, *p_previous;
1178 vlc_value_t val_list, text_list;
1179 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1182 if( val_list.p_list->i_count <= 0 )
1185 p_track_description = ( libvlc_track_description_t * )
1186 malloc( sizeof( libvlc_track_description_t ) );
1187 if ( !p_track_description )
1189 libvlc_printerr( "Not enough memory" );
1192 p_actual = p_track_description;
1194 for( int i = 0; i < val_list.p_list->i_count; i++ )
1198 p_actual = ( libvlc_track_description_t * )
1199 malloc( sizeof( libvlc_track_description_t ) );
1202 libvlc_track_description_release( p_track_description );
1203 libvlc_printerr( "Not enough memory" );
1207 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1208 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1209 p_actual->p_next = NULL;
1211 p_previous->p_next = p_actual;
1212 p_previous = p_actual;
1217 var_FreeList( &val_list, &text_list );
1218 vlc_object_release( p_input );
1220 return p_track_description;
1223 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1225 libvlc_track_description_t *p_actual, *p_before;
1230 free( p_actual->psz_name );
1231 p_before = p_actual;
1232 p_actual = p_before->p_next;
1237 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1239 input_thread_t *p_input_thread;
1242 p_input_thread = libvlc_get_input_thread ( p_mi );
1243 if ( !p_input_thread )
1245 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1246 vlc_object_release( p_input_thread );
1251 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1253 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1254 if( p_input_thread != NULL )
1256 var_TriggerCallback( p_input_thread, "frame-next" );
1257 vlc_object_release( p_input_thread );