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);
73 static inline void lock(libvlc_media_player_t *mp)
75 vlc_mutex_lock(&mp->object_lock);
78 static inline void unlock(libvlc_media_player_t *mp)
80 vlc_mutex_unlock(&mp->object_lock);
84 * Release the associated input thread.
86 * Object lock is NOT held.
88 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
90 input_thread_t * p_input_thread;
92 if( !p_mi || !p_mi->p_input_thread )
95 p_input_thread = p_mi->p_input_thread;
97 var_DelCallback( p_input_thread, "can-seek",
98 input_seekable_changed, p_mi );
99 var_DelCallback( p_input_thread, "can-pause",
100 input_pausable_changed, p_mi );
101 var_DelCallback( p_input_thread, "intf-event",
102 input_event_changed, p_mi );
104 /* We owned this one */
105 input_Stop( p_input_thread, b_input_abort );
107 vlc_thread_join( p_input_thread );
109 assert( p_mi->p_input_resource == NULL );
110 assert( p_input_thread->b_dead );
111 /* Store the input resource for future use. */
112 p_mi->p_input_resource = input_DetachResource( p_input_thread );
114 vlc_object_release( p_input_thread );
116 p_mi->p_input_thread = NULL;
120 * Retrieve the input thread. Be sure to release the object
121 * once you are done with it. (libvlc Internal)
123 * Function will lock the object.
125 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
127 input_thread_t *p_input_thread;
132 p_input_thread = p_mi->p_input_thread;
134 vlc_object_hold( p_input_thread );
136 libvlc_printerr( "No active input" );
139 return p_input_thread;
143 * Set the internal state of the media_player. (media player Internal)
145 * Function will lock the media_player.
147 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
153 libvlc_media_t *media = p_mi->p_md;
155 libvlc_media_retain(media);
161 // Also set the state of the corresponding media
162 // This is strictly for convenience.
163 libvlc_media_set_state(media, state);
165 libvlc_media_release(media);
170 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
171 vlc_value_t oldval, vlc_value_t newval,
177 libvlc_media_player_t * p_mi = p_userdata;
178 libvlc_event_t event;
180 event.type = libvlc_MediaPlayerSeekableChanged;
181 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
183 libvlc_event_send( p_mi->p_event_manager, &event );
188 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
189 vlc_value_t oldval, vlc_value_t newval,
195 libvlc_media_player_t * p_mi = p_userdata;
196 libvlc_event_t event;
198 event.type = libvlc_MediaPlayerPausableChanged;
199 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
201 libvlc_event_send( p_mi->p_event_manager, &event );
206 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
207 vlc_value_t oldval, vlc_value_t newval,
211 input_thread_t * p_input = (input_thread_t *)p_this;
212 libvlc_media_player_t * p_mi = p_userdata;
213 libvlc_event_t event;
215 assert( !strcmp( psz_cmd, "intf-event" ) );
217 if( newval.i_int == INPUT_EVENT_STATE )
219 libvlc_state_t libvlc_state;
221 switch ( var_GetInteger( p_input, "state" ) )
224 libvlc_state = libvlc_NothingSpecial;
225 event.type = libvlc_MediaPlayerNothingSpecial;
228 libvlc_state = libvlc_Opening;
229 event.type = libvlc_MediaPlayerOpening;
232 libvlc_state = libvlc_Playing;
233 event.type = libvlc_MediaPlayerPlaying;
236 libvlc_state = libvlc_Paused;
237 event.type = libvlc_MediaPlayerPaused;
240 libvlc_state = libvlc_Ended;
241 event.type = libvlc_MediaPlayerEndReached;
244 libvlc_state = libvlc_Error;
245 event.type = libvlc_MediaPlayerEncounteredError;
252 set_state( p_mi, libvlc_state, false );
253 libvlc_event_send( p_mi->p_event_manager, &event );
255 else if( newval.i_int == INPUT_EVENT_ABORT )
257 libvlc_state_t libvlc_state = libvlc_Stopped;
258 event.type = libvlc_MediaPlayerStopped;
260 set_state( p_mi, libvlc_state, false );
261 libvlc_event_send( p_mi->p_event_manager, &event );
263 else if( newval.i_int == INPUT_EVENT_POSITION )
265 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
266 return VLC_SUCCESS; /* Don't send the position while stopped */
269 event.type = libvlc_MediaPlayerPositionChanged;
270 event.u.media_player_position_changed.new_position =
271 var_GetFloat( p_input, "position" );
272 libvlc_event_send( p_mi->p_event_manager, &event );
275 event.type = libvlc_MediaPlayerTimeChanged;
276 event.u.media_player_time_changed.new_time =
277 from_mtime(var_GetTime( p_input, "time" ));
278 libvlc_event_send( p_mi->p_event_manager, &event );
280 else if( newval.i_int == INPUT_EVENT_LENGTH )
282 event.type = libvlc_MediaPlayerLengthChanged;
283 event.u.media_player_length_changed.new_length =
284 from_mtime(var_GetTime( p_input, "length" ));
285 libvlc_event_send( p_mi->p_event_manager, &event );
292 /**************************************************************************
293 * Snapshot Taken Event.
295 * FIXME: This snapshot API interface makes no sense in media_player.
296 *************************************************************************/
297 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
298 vlc_value_t oldval, vlc_value_t newval, void *p_data )
300 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
302 libvlc_media_player_t *mp = p_data;
303 libvlc_event_t event;
304 event.type = libvlc_MediaPlayerSnapshotTaken;
305 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
306 libvlc_event_send(mp->p_event_manager, &event);
312 static void libvlc_media_player_destroy( libvlc_media_player_t * );
315 /**************************************************************************
316 * Create a Media Instance object.
319 * - All items created by _new start with a refcount set to 1.
320 * - Accessor _release decrease the refcount by 1, if after that
321 * operation the refcount is 0, the object is destroyed.
322 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
324 * Object locking strategy:
325 * - No lock held while in constructor.
326 * - When accessing any member variable this lock is held. (XXX who locks?)
327 * - When attempting to destroy the object the lock is also held.
328 **************************************************************************/
329 libvlc_media_player_t *
330 libvlc_media_player_new( libvlc_instance_t *instance )
332 libvlc_media_player_t * mp;
336 mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
337 if (unlikely(mp == NULL))
339 libvlc_printerr("Not enough memory");
342 vlc_object_attach (mp, mp->p_libvlc);
345 var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
347 var_Create (mp, "drawable-hwnd", VLC_VAR_ADDRESS);
350 var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
351 var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
354 var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
355 var_SetBool (mp, "keyboard-events", true);
356 var_Create (mp, "mouse-events", VLC_VAR_BOOL);
357 var_SetBool (mp, "mouse-events", true);
359 var_Create (mp, "fullscreen", VLC_VAR_BOOL);
360 var_Create (mp, "autoscale", VLC_VAR_BOOL);
361 var_SetBool (mp, "autoscale", true);
362 var_Create (mp, "scale", VLC_VAR_FLOAT);
363 var_SetFloat (mp, "scale", 1.);
364 var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
365 var_Create (mp, "crop", VLC_VAR_STRING);
366 var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
367 var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
369 var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
371 var_Create (mp, "marq-marquee", VLC_VAR_STRING);
372 var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
373 var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
374 var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
375 var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
376 var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
377 var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
378 var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
379 var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
381 var_Create (mp, "logo-file", VLC_VAR_STRING);
382 var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
383 var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
384 var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
385 var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
386 var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
387 var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
390 var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
393 mp->state = libvlc_NothingSpecial;
394 mp->p_libvlc_instance = instance;
395 mp->p_input_thread = NULL;
396 mp->p_input_resource = NULL;
398 mp->p_event_manager = libvlc_event_manager_new(mp, instance);
399 if (unlikely(mp->p_event_manager == NULL))
401 vlc_object_release(mp);
404 vlc_mutex_init(&mp->object_lock);
406 register_event(mp, NothingSpecial);
407 register_event(mp, Opening);
408 register_event(mp, Buffering);
409 register_event(mp, Playing);
410 register_event(mp, Paused);
411 register_event(mp, Stopped);
412 register_event(mp, Forward);
413 register_event(mp, Backward);
414 register_event(mp, EndReached);
415 register_event(mp, EncounteredError);
416 register_event(mp, SeekableChanged);
418 register_event(mp, PositionChanged);
419 register_event(mp, TimeChanged);
420 register_event(mp, LengthChanged);
421 register_event(mp, TitleChanged);
422 register_event(mp, PausableChanged);
424 /* Snapshot initialization */
425 register_event(mp, SnapshotTaken);
427 register_event(mp, MediaChanged);
429 /* Attach a var callback to the global object to provide the glue between
430 * vout_thread that generates the event and media_player that re-emits it
431 * with its own event manager
433 * FIXME: It's unclear why we want to put this in public API, and why we
434 * want to expose it in such a limiting and ugly way.
436 var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
438 libvlc_retain(instance);
442 /**************************************************************************
443 * Create a Media Instance object with a media descriptor.
444 **************************************************************************/
445 libvlc_media_player_t *
446 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
448 libvlc_media_player_t * p_mi;
450 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
454 libvlc_media_retain( p_md );
460 /**************************************************************************
461 * Destroy a Media Instance object (libvlc internal)
463 * Warning: No lock held here, but hey, this is internal. Caller must lock.
464 **************************************************************************/
465 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
469 /* Detach Callback from the main libvlc object */
470 var_DelCallback( p_mi->p_libvlc,
471 "snapshot-file", snapshot_was_taken, p_mi );
473 /* If the input thread hasn't been already deleted it means
474 * that the owners didn't stop the thread before releasing it. */
475 assert(!p_mi->p_input_thread);
477 /* Fallback for those who don't use NDEBUG */
478 if (p_mi->p_input_thread)
479 release_input_thread(p_mi, true);
481 if( p_mi->p_input_resource )
483 input_resource_Delete( p_mi->p_input_resource );
484 p_mi->p_input_resource = NULL;
487 libvlc_event_manager_release( p_mi->p_event_manager );
488 libvlc_media_release( p_mi->p_md );
489 vlc_mutex_destroy( &p_mi->object_lock );
491 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
492 vlc_object_release( p_mi );
493 libvlc_release(instance);
496 /**************************************************************************
497 * Release a Media Instance object.
499 * Function does the locking.
500 **************************************************************************/
501 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
507 destroy = !--p_mi->i_refcount;
511 libvlc_media_player_destroy( p_mi );
514 /**************************************************************************
515 * Retain a Media Instance object.
517 * Caller must hold the lock.
518 **************************************************************************/
519 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
528 /**************************************************************************
529 * Set the Media descriptor associated with the instance.
531 * Enter without lock -- function will lock the object.
532 **************************************************************************/
533 void libvlc_media_player_set_media(
534 libvlc_media_player_t *p_mi,
535 libvlc_media_t *p_md )
539 /* FIXME I am not sure if it is a user request or on die(eof/error)
541 release_input_thread( p_mi,
542 p_mi->p_input_thread &&
543 !p_mi->p_input_thread->b_eof &&
544 !p_mi->p_input_thread->b_error );
546 set_state( p_mi, libvlc_NothingSpecial, true );
548 libvlc_media_release( p_mi->p_md );
554 return; /* It is ok to pass a NULL md */
557 libvlc_media_retain( p_md );
560 /* The policy here is to ignore that we were created using a different
561 * libvlc_instance, because we don't really care */
562 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
566 /* Send an event for the newly available media */
567 libvlc_event_t event;
568 event.type = libvlc_MediaPlayerMediaChanged;
569 event.u.media_player_media_changed.new_media = p_md;
570 libvlc_event_send( p_mi->p_event_manager, &event );
574 /**************************************************************************
575 * Get the Media descriptor associated with the instance.
576 **************************************************************************/
578 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
585 libvlc_media_retain( p_mi->p_md );
590 /**************************************************************************
591 * Get the event Manager.
592 **************************************************************************/
593 libvlc_event_manager_t *
594 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
596 return p_mi->p_event_manager;
599 /**************************************************************************
600 * Tell media player to start playing.
601 **************************************************************************/
602 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
604 input_thread_t * p_input_thread;
606 if( (p_input_thread = libvlc_get_input_thread( p_mi )) )
608 /* A thread already exists, send it a play message */
609 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
610 vlc_object_release( p_input_thread );
614 /* Ignore previous exception */
620 libvlc_printerr( "No associated media descriptor" );
624 p_mi->p_input_thread = input_Create( p_mi,
625 p_mi->p_md->p_input_item, NULL,
626 p_mi->p_input_resource );
627 if( !p_mi->p_input_thread )
630 libvlc_printerr( "Not enough memory" );
634 p_mi->p_input_resource = NULL;
635 p_input_thread = p_mi->p_input_thread;
637 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
638 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
639 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
641 if( input_Start( p_input_thread ) )
643 vlc_object_release( p_input_thread );
644 p_mi->p_input_thread = NULL;
651 /**************************************************************************
653 **************************************************************************/
654 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
656 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
657 if( !p_input_thread )
660 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
661 if( state == libvlc_Playing || state == libvlc_Buffering )
663 if( libvlc_media_player_can_pause( p_mi ) )
664 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
666 libvlc_media_player_stop( p_mi );
669 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
671 vlc_object_release( p_input_thread );
674 /**************************************************************************
675 * Tells whether the media player is currently playing.
677 * Enter with lock held.
678 **************************************************************************/
679 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
681 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
682 return (libvlc_Playing == state) || (libvlc_Buffering == state);
685 /**************************************************************************
687 **************************************************************************/
688 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
690 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
693 release_input_thread( p_mi, true ); /* This will stop the input thread */
696 /* Force to go to stopped state, in case we were in Ended, or Error
698 if( state != libvlc_Stopped )
700 set_state( p_mi, libvlc_Stopped, false );
702 /* Construct and send the event */
703 libvlc_event_t event;
704 event.type = libvlc_MediaPlayerStopped;
705 libvlc_event_send( p_mi->p_event_manager, &event );
709 /**************************************************************************
711 **************************************************************************/
712 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
715 assert (p_mi != NULL);
717 var_SetAddress (p_mi, "drawable-nsobject", drawable);
719 (void) p_mi; (void)drawable;
723 /**************************************************************************
725 **************************************************************************/
726 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
728 assert (p_mi != NULL);
730 return var_GetAddress (p_mi, "drawable-nsobject");
736 /**************************************************************************
738 **************************************************************************/
739 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
743 var_SetInteger (p_mi, "drawable-agl", drawable);
745 (void) p_mi; (void)drawable;
749 /**************************************************************************
751 **************************************************************************/
752 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
754 assert (p_mi != NULL);
756 return var_GetInteger (p_mi, "drawable-agl");
762 /**************************************************************************
764 **************************************************************************/
765 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
768 assert (p_mi != NULL);
769 var_SetInteger (p_mi, "drawable-xid", drawable);
772 /**************************************************************************
774 **************************************************************************/
775 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
777 return var_GetInteger (p_mi, "drawable-xid");
780 /**************************************************************************
782 **************************************************************************/
783 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
786 assert (p_mi != NULL);
788 var_SetAddress (p_mi, "drawable-hwnd", drawable);
790 (void) p_mi; (void) drawable;
794 /**************************************************************************
796 **************************************************************************/
797 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
799 assert (p_mi != NULL);
801 return var_GetAddress (p_mi, "drawable-hwnd");
807 /**************************************************************************
808 * Getters for stream information
809 **************************************************************************/
810 libvlc_time_t libvlc_media_player_get_length(
811 libvlc_media_player_t *p_mi )
813 input_thread_t *p_input_thread;
814 libvlc_time_t i_time;
816 p_input_thread = libvlc_get_input_thread ( p_mi );
817 if( !p_input_thread )
820 i_time = from_mtime(var_GetTime( p_input_thread, "length" ));
821 vlc_object_release( p_input_thread );
826 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
828 input_thread_t *p_input_thread;
829 libvlc_time_t i_time;
831 p_input_thread = libvlc_get_input_thread ( p_mi );
832 if( !p_input_thread )
835 i_time = from_mtime(var_GetTime( p_input_thread , "time" ));
836 vlc_object_release( p_input_thread );
840 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
841 libvlc_time_t i_time )
843 input_thread_t *p_input_thread;
845 p_input_thread = libvlc_get_input_thread ( p_mi );
846 if( !p_input_thread )
849 var_SetTime( p_input_thread, "time", to_mtime(i_time) );
850 vlc_object_release( p_input_thread );
853 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
856 input_thread_t *p_input_thread;
858 p_input_thread = libvlc_get_input_thread ( p_mi );
859 if( !p_input_thread )
862 var_SetFloat( p_input_thread, "position", position );
863 vlc_object_release( p_input_thread );
866 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
868 input_thread_t *p_input_thread;
871 p_input_thread = libvlc_get_input_thread ( p_mi );
872 if( !p_input_thread )
875 f_position = var_GetFloat( p_input_thread, "position" );
876 vlc_object_release( p_input_thread );
881 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
884 input_thread_t *p_input_thread;
886 p_input_thread = libvlc_get_input_thread ( p_mi );
887 if( !p_input_thread )
890 var_SetInteger( p_input_thread, "chapter", chapter );
891 vlc_object_release( p_input_thread );
894 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
896 input_thread_t *p_input_thread;
899 p_input_thread = libvlc_get_input_thread ( p_mi );
900 if( !p_input_thread )
903 i_chapter = var_GetInteger( p_input_thread, "chapter" );
904 vlc_object_release( p_input_thread );
909 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
911 input_thread_t *p_input_thread;
914 p_input_thread = libvlc_get_input_thread ( p_mi );
915 if( !p_input_thread )
918 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
919 vlc_object_release( p_input_thread );
924 int libvlc_media_player_get_chapter_count_for_title(
925 libvlc_media_player_t *p_mi,
928 input_thread_t *p_input_thread;
931 p_input_thread = libvlc_get_input_thread ( p_mi );
932 if( !p_input_thread )
936 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
938 vlc_object_release( p_input_thread );
941 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
942 vlc_object_release( p_input_thread );
948 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
951 input_thread_t *p_input_thread;
953 p_input_thread = libvlc_get_input_thread ( p_mi );
954 if( !p_input_thread )
957 var_SetInteger( p_input_thread, "title", i_title );
958 vlc_object_release( p_input_thread );
961 libvlc_event_t event;
962 event.type = libvlc_MediaPlayerTitleChanged;
963 event.u.media_player_title_changed.new_title = i_title;
964 libvlc_event_send( p_mi->p_event_manager, &event );
967 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
969 input_thread_t *p_input_thread;
972 p_input_thread = libvlc_get_input_thread ( p_mi );
973 if( !p_input_thread )
976 i_title = var_GetInteger( p_input_thread, "title" );
977 vlc_object_release( p_input_thread );
982 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
984 input_thread_t *p_input_thread;
987 p_input_thread = libvlc_get_input_thread ( p_mi );
988 if( !p_input_thread )
991 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
992 vlc_object_release( p_input_thread );
997 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
999 input_thread_t *p_input_thread;
1001 p_input_thread = libvlc_get_input_thread ( p_mi );
1002 if( !p_input_thread )
1005 int i_type = var_Type( p_input_thread, "next-chapter" );
1006 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1007 "next-chapter":"next-title", true );
1009 vlc_object_release( p_input_thread );
1012 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1014 input_thread_t *p_input_thread;
1016 p_input_thread = libvlc_get_input_thread ( p_mi );
1017 if( !p_input_thread )
1020 int i_type = var_Type( p_input_thread, "next-chapter" );
1021 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1022 "prev-chapter":"prev-title", true );
1024 vlc_object_release( p_input_thread );
1027 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1029 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1032 if( p_input_thread )
1034 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1036 vlc_object_release( p_input_thread );
1041 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1044 input_thread_t *p_input_thread =
1045 libvlc_get_input_thread ( p_mi );
1046 if ( !p_input_thread )
1049 b_will_play = !p_input_thread->b_die && !p_input_thread->b_dead;
1050 vlc_object_release( p_input_thread );
1055 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1057 input_thread_t *p_input_thread;
1060 p_input_thread = libvlc_get_input_thread ( p_mi );
1061 if( !p_input_thread )
1064 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1065 if( (rate < 0.0) && !b_can_rewind )
1067 vlc_object_release( p_input_thread );
1068 libvlc_printerr( "Invalid playback rate" );
1072 var_SetFloat( p_input_thread, "rate", rate );
1073 vlc_object_release( p_input_thread );
1077 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1079 input_thread_t *p_input_thread;
1083 p_input_thread = libvlc_get_input_thread ( p_mi );
1084 if( !p_input_thread )
1085 return 0.0; /* rate < 0 indicates rewind */
1087 f_rate = var_GetFloat( p_input_thread, "rate" );
1088 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1089 /* FIXME: why are negative values forbidden ?? (rewinding) */
1090 if( f_rate < 0 && !b_can_rewind )
1092 vlc_object_release( p_input_thread );
1095 vlc_object_release( p_input_thread );
1100 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1103 libvlc_state_t state = p_mi->state;
1108 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1110 input_thread_t *p_input_thread;
1113 p_input_thread = libvlc_get_input_thread ( p_mi );
1114 if ( !p_input_thread )
1116 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1117 vlc_object_release( p_input_thread );
1122 /* internal function, used by audio, video */
1123 libvlc_track_description_t *
1124 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1125 const char *psz_variable )
1127 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1128 libvlc_track_description_t *p_track_description = NULL,
1129 *p_actual, *p_previous;
1134 vlc_value_t val_list, text_list;
1135 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1138 if( val_list.p_list->i_count <= 0 )
1141 p_track_description = ( libvlc_track_description_t * )
1142 malloc( sizeof( libvlc_track_description_t ) );
1143 if ( !p_track_description )
1145 libvlc_printerr( "Not enough memory" );
1148 p_actual = p_track_description;
1150 for( int i = 0; i < val_list.p_list->i_count; i++ )
1154 p_actual = ( libvlc_track_description_t * )
1155 malloc( sizeof( libvlc_track_description_t ) );
1158 libvlc_track_description_release( p_track_description );
1159 libvlc_printerr( "Not enough memory" );
1163 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1164 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1165 p_actual->p_next = NULL;
1167 p_previous->p_next = p_actual;
1168 p_previous = p_actual;
1173 var_FreeList( &val_list, &text_list );
1174 vlc_object_release( p_input );
1176 return p_track_description;
1179 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1181 libvlc_track_description_t *p_actual, *p_before;
1186 free( p_actual->psz_name );
1187 p_before = p_actual;
1188 p_actual = p_before->p_next;
1193 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1195 input_thread_t *p_input_thread;
1198 p_input_thread = libvlc_get_input_thread ( p_mi );
1199 if ( !p_input_thread )
1201 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1202 vlc_object_release( p_input_thread );
1207 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1209 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1210 if( p_input_thread != NULL )
1212 var_TriggerCallback( p_input_thread, "frame-next" );
1213 vlc_object_release( p_input_thread );