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 /* Store the input resource for future use. */
131 assert( p_mi->input.p_resource == NULL );
132 p_mi->input.p_resource = input_DetachResource( p_input_thread );
134 p_mi->input.p_thread = NULL;
135 vlc_object_release( p_input_thread );
139 * Retrieve the input thread. Be sure to release the object
140 * once you are done with it. (libvlc Internal)
142 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
144 input_thread_t *p_input_thread;
149 p_input_thread = p_mi->input.p_thread;
151 vlc_object_hold( p_input_thread );
153 libvlc_printerr( "No active input" );
156 return p_input_thread;
160 * Set the internal state of the media_player. (media player Internal)
162 * Function will lock the media_player.
164 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
171 libvlc_media_t *media = p_mi->p_md;
173 libvlc_media_retain(media);
180 // Also set the state of the corresponding media
181 // This is strictly for convenience.
182 libvlc_media_set_state(media, state);
184 libvlc_media_release(media);
189 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
190 vlc_value_t oldval, vlc_value_t newval,
196 libvlc_media_player_t * p_mi = p_userdata;
197 libvlc_event_t event;
199 event.type = libvlc_MediaPlayerSeekableChanged;
200 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
202 libvlc_event_send( p_mi->p_event_manager, &event );
207 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
208 vlc_value_t oldval, vlc_value_t newval,
214 libvlc_media_player_t * p_mi = p_userdata;
215 libvlc_event_t event;
217 event.type = libvlc_MediaPlayerPausableChanged;
218 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
220 libvlc_event_send( p_mi->p_event_manager, &event );
225 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
226 vlc_value_t oldval, vlc_value_t newval,
230 input_thread_t * p_input = (input_thread_t *)p_this;
231 libvlc_media_player_t * p_mi = p_userdata;
232 libvlc_event_t event;
234 assert( !strcmp( psz_cmd, "intf-event" ) );
236 if( newval.i_int == INPUT_EVENT_STATE )
238 libvlc_state_t libvlc_state;
240 switch ( var_GetInteger( p_input, "state" ) )
243 libvlc_state = libvlc_NothingSpecial;
244 event.type = libvlc_MediaPlayerNothingSpecial;
247 libvlc_state = libvlc_Opening;
248 event.type = libvlc_MediaPlayerOpening;
251 libvlc_state = libvlc_Playing;
252 event.type = libvlc_MediaPlayerPlaying;
255 libvlc_state = libvlc_Paused;
256 event.type = libvlc_MediaPlayerPaused;
259 libvlc_state = libvlc_Ended;
260 event.type = libvlc_MediaPlayerEndReached;
263 libvlc_state = libvlc_Error;
264 event.type = libvlc_MediaPlayerEncounteredError;
271 set_state( p_mi, libvlc_state, false );
272 libvlc_event_send( p_mi->p_event_manager, &event );
274 else if( newval.i_int == INPUT_EVENT_ABORT )
276 libvlc_state_t libvlc_state = libvlc_Stopped;
277 event.type = libvlc_MediaPlayerStopped;
279 set_state( p_mi, libvlc_state, false );
280 libvlc_event_send( p_mi->p_event_manager, &event );
282 else if( newval.i_int == INPUT_EVENT_POSITION )
284 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
285 return VLC_SUCCESS; /* Don't send the position while stopped */
288 event.type = libvlc_MediaPlayerPositionChanged;
289 event.u.media_player_position_changed.new_position =
290 var_GetFloat( p_input, "position" );
291 libvlc_event_send( p_mi->p_event_manager, &event );
294 event.type = libvlc_MediaPlayerTimeChanged;
295 event.u.media_player_time_changed.new_time =
296 from_mtime(var_GetTime( p_input, "time" ));
297 libvlc_event_send( p_mi->p_event_manager, &event );
299 else if( newval.i_int == INPUT_EVENT_LENGTH )
301 event.type = libvlc_MediaPlayerLengthChanged;
302 event.u.media_player_length_changed.new_length =
303 from_mtime(var_GetTime( p_input, "length" ));
304 libvlc_event_send( p_mi->p_event_manager, &event );
311 /**************************************************************************
312 * Snapshot Taken Event.
314 * FIXME: This snapshot API interface makes no sense in media_player.
315 *************************************************************************/
316 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
317 vlc_value_t oldval, vlc_value_t newval, void *p_data )
319 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
321 libvlc_media_player_t *mp = p_data;
322 libvlc_event_t event;
323 event.type = libvlc_MediaPlayerSnapshotTaken;
324 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
325 libvlc_event_send(mp->p_event_manager, &event);
330 static input_thread_t *find_input (vlc_object_t *obj)
332 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
334 return libvlc_get_input_thread (mp);
338 static void libvlc_media_player_destroy( libvlc_media_player_t * );
341 /**************************************************************************
342 * Create a Media Instance object.
345 * - All items created by _new start with a refcount set to 1.
346 * - Accessor _release decrease the refcount by 1, if after that
347 * operation the refcount is 0, the object is destroyed.
348 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
350 * Object locking strategy:
351 * - No lock held while in constructor.
352 * - When accessing any member variable this lock is held. (XXX who locks?)
353 * - When attempting to destroy the object the lock is also held.
354 **************************************************************************/
355 libvlc_media_player_t *
356 libvlc_media_player_new( libvlc_instance_t *instance )
358 libvlc_media_player_t * mp;
362 mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
363 if (unlikely(mp == NULL))
365 libvlc_printerr("Not enough memory");
368 vlc_object_attach (mp, mp->p_libvlc);
371 var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
374 var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
376 var_Create (mp, "drawable-hwnd", VLC_VAR_ADDRESS);
379 var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
380 var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
383 var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
384 var_SetBool (mp, "keyboard-events", true);
385 var_Create (mp, "mouse-events", VLC_VAR_BOOL);
386 var_SetBool (mp, "mouse-events", true);
388 var_Create (mp, "fullscreen", VLC_VAR_BOOL);
389 var_Create (mp, "autoscale", VLC_VAR_BOOL);
390 var_SetBool (mp, "autoscale", true);
391 var_Create (mp, "scale", VLC_VAR_FLOAT);
392 var_SetFloat (mp, "scale", 1.);
393 var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
394 var_Create (mp, "crop", VLC_VAR_STRING);
395 var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
396 var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
398 var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
400 var_Create (mp, "marq-marquee", VLC_VAR_STRING);
401 var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
402 var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
403 var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
404 var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
405 var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
406 var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
407 var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
408 var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
410 var_Create (mp, "logo-file", VLC_VAR_STRING);
411 var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
412 var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
413 var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
414 var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
415 var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
416 var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
419 var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
420 var_Create (mp, "volume-muted", VLC_VAR_BOOL);
421 var_Create (mp, "saved-volume", VLC_VAR_INTEGER);
422 var_Create (mp, "volume-change", VLC_VAR_VOID);
423 var_Create (mp, "find-input-callback", VLC_VAR_ADDRESS);
424 var_SetAddress (mp, "find-input-callback", find_input);
427 mp->state = libvlc_NothingSpecial;
428 mp->p_libvlc_instance = instance;
429 mp->input.p_thread = NULL;
430 mp->input.p_resource = NULL;
431 vlc_mutex_init (&mp->input.lock);
433 mp->p_event_manager = libvlc_event_manager_new(mp, instance);
434 if (unlikely(mp->p_event_manager == NULL))
436 vlc_object_release(mp);
439 vlc_mutex_init(&mp->object_lock);
441 register_event(mp, NothingSpecial);
442 register_event(mp, Opening);
443 register_event(mp, Buffering);
444 register_event(mp, Playing);
445 register_event(mp, Paused);
446 register_event(mp, Stopped);
447 register_event(mp, Forward);
448 register_event(mp, Backward);
449 register_event(mp, EndReached);
450 register_event(mp, EncounteredError);
451 register_event(mp, SeekableChanged);
453 register_event(mp, PositionChanged);
454 register_event(mp, TimeChanged);
455 register_event(mp, LengthChanged);
456 register_event(mp, TitleChanged);
457 register_event(mp, PausableChanged);
459 /* Snapshot initialization */
460 register_event(mp, SnapshotTaken);
462 register_event(mp, MediaChanged);
464 /* Attach a var callback to the global object to provide the glue between
465 * vout_thread that generates the event and media_player that re-emits it
466 * with its own event manager
468 * FIXME: It's unclear why we want to put this in public API, and why we
469 * want to expose it in such a limiting and ugly way.
471 var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
473 libvlc_retain(instance);
477 /**************************************************************************
478 * Create a Media Instance object with a media descriptor.
479 **************************************************************************/
480 libvlc_media_player_t *
481 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
483 libvlc_media_player_t * p_mi;
485 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
489 libvlc_media_retain( p_md );
495 /**************************************************************************
496 * Destroy a Media Instance object (libvlc internal)
498 * Warning: No lock held here, but hey, this is internal. Caller must lock.
499 **************************************************************************/
500 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
504 /* Detach Callback from the main libvlc object */
505 var_DelCallback( p_mi->p_libvlc,
506 "snapshot-file", snapshot_was_taken, p_mi );
508 /* No need for lock_input() because no other threads knows us anymore */
509 if( p_mi->input.p_thread )
510 release_input_thread(p_mi, true);
511 if( p_mi->input.p_resource )
513 input_resource_Delete( p_mi->input.p_resource );
514 p_mi->input.p_resource = NULL;
516 vlc_mutex_destroy( &p_mi->input.lock );
518 libvlc_event_manager_release( p_mi->p_event_manager );
519 libvlc_media_release( p_mi->p_md );
520 vlc_mutex_destroy( &p_mi->object_lock );
522 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
523 vlc_object_release( p_mi );
524 libvlc_release(instance);
527 /**************************************************************************
528 * Release a Media Instance object.
530 * Function does the locking.
531 **************************************************************************/
532 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
538 destroy = !--p_mi->i_refcount;
542 libvlc_media_player_destroy( p_mi );
545 /**************************************************************************
546 * Retain a Media Instance object.
548 * Caller must hold the lock.
549 **************************************************************************/
550 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
559 /**************************************************************************
560 * Set the Media descriptor associated with the instance.
562 * Enter without lock -- function will lock the object.
563 **************************************************************************/
564 void libvlc_media_player_set_media(
565 libvlc_media_player_t *p_mi,
566 libvlc_media_t *p_md )
570 /* FIXME I am not sure if it is a user request or on die(eof/error)
572 release_input_thread( p_mi,
573 p_mi->input.p_thread &&
574 !p_mi->input.p_thread->b_eof &&
575 !p_mi->input.p_thread->b_error );
578 set_state( p_mi, libvlc_NothingSpecial, true );
579 unlock_input( p_mi );
581 libvlc_media_release( p_mi->p_md );
587 return; /* It is ok to pass a NULL md */
590 libvlc_media_retain( p_md );
593 /* The policy here is to ignore that we were created using a different
594 * libvlc_instance, because we don't really care */
595 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
599 /* Send an event for the newly available media */
600 libvlc_event_t event;
601 event.type = libvlc_MediaPlayerMediaChanged;
602 event.u.media_player_media_changed.new_media = p_md;
603 libvlc_event_send( p_mi->p_event_manager, &event );
607 /**************************************************************************
608 * Get the Media descriptor associated with the instance.
609 **************************************************************************/
611 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
618 libvlc_media_retain( p_mi->p_md );
623 /**************************************************************************
624 * Get the event Manager.
625 **************************************************************************/
626 libvlc_event_manager_t *
627 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
629 return p_mi->p_event_manager;
632 /**************************************************************************
633 * Tell media player to start playing.
634 **************************************************************************/
635 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
639 input_thread_t *p_input_thread = p_mi->input.p_thread;
642 /* A thread already exists, send it a play message */
643 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
644 unlock_input( p_mi );
648 /* Ignore previous exception */
654 unlock_input( p_mi );
655 libvlc_printerr( "No associated media descriptor" );
659 p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
660 p_mi->input.p_resource );
662 if( !p_input_thread )
665 libvlc_printerr( "Not enough memory" );
669 p_mi->input.p_resource = NULL;
671 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
672 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
673 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
675 if( input_Start( p_input_thread ) )
678 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
679 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
680 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
681 vlc_object_release( p_input_thread );
682 libvlc_printerr( "Input initialization failure" );
685 p_mi->input.p_thread = p_input_thread;
690 /**************************************************************************
692 **************************************************************************/
693 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
695 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
696 if( !p_input_thread )
699 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
700 if( state == libvlc_Playing || state == libvlc_Buffering )
702 if( libvlc_media_player_can_pause( p_mi ) )
703 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
705 libvlc_media_player_stop( p_mi );
708 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
710 vlc_object_release( p_input_thread );
713 /**************************************************************************
714 * Tells whether the media player is currently playing.
716 * Enter with lock held.
717 **************************************************************************/
718 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
720 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
721 return (libvlc_Playing == state) || (libvlc_Buffering == state);
724 /**************************************************************************
726 **************************************************************************/
727 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
729 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
732 release_input_thread( p_mi, true ); /* This will stop the input thread */
734 /* Force to go to stopped state, in case we were in Ended, or Error
736 if( state != libvlc_Stopped )
738 set_state( p_mi, libvlc_Stopped, false );
740 /* Construct and send the event */
741 libvlc_event_t event;
742 event.type = libvlc_MediaPlayerStopped;
743 libvlc_event_send( p_mi->p_event_manager, &event );
748 /**************************************************************************
750 **************************************************************************/
751 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
754 assert (p_mi != NULL);
756 var_SetAddress (p_mi, "drawable-nsobject", drawable);
758 (void) p_mi; (void)drawable;
762 /**************************************************************************
764 **************************************************************************/
765 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
767 assert (p_mi != NULL);
769 return var_GetAddress (p_mi, "drawable-nsobject");
775 /**************************************************************************
777 **************************************************************************/
778 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
782 var_SetInteger (p_mi, "drawable-agl", drawable);
784 (void) p_mi; (void)drawable;
788 /**************************************************************************
790 **************************************************************************/
791 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
793 assert (p_mi != NULL);
795 return var_GetInteger (p_mi, "drawable-agl");
801 /**************************************************************************
803 **************************************************************************/
804 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
807 assert (p_mi != NULL);
808 var_SetInteger (p_mi, "drawable-xid", drawable);
811 /**************************************************************************
813 **************************************************************************/
814 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
816 return var_GetInteger (p_mi, "drawable-xid");
819 /**************************************************************************
821 **************************************************************************/
822 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
825 assert (p_mi != NULL);
827 var_SetAddress (p_mi, "drawable-hwnd", drawable);
829 (void) p_mi; (void) drawable;
833 /**************************************************************************
835 **************************************************************************/
836 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
838 assert (p_mi != NULL);
840 return var_GetAddress (p_mi, "drawable-hwnd");
846 /**************************************************************************
847 * Getters for stream information
848 **************************************************************************/
849 libvlc_time_t libvlc_media_player_get_length(
850 libvlc_media_player_t *p_mi )
852 input_thread_t *p_input_thread;
853 libvlc_time_t i_time;
855 p_input_thread = libvlc_get_input_thread ( p_mi );
856 if( !p_input_thread )
859 i_time = from_mtime(var_GetTime( p_input_thread, "length" ));
860 vlc_object_release( p_input_thread );
865 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
867 input_thread_t *p_input_thread;
868 libvlc_time_t i_time;
870 p_input_thread = libvlc_get_input_thread ( p_mi );
871 if( !p_input_thread )
874 i_time = from_mtime(var_GetTime( p_input_thread , "time" ));
875 vlc_object_release( p_input_thread );
879 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
880 libvlc_time_t i_time )
882 input_thread_t *p_input_thread;
884 p_input_thread = libvlc_get_input_thread ( p_mi );
885 if( !p_input_thread )
888 var_SetTime( p_input_thread, "time", to_mtime(i_time) );
889 vlc_object_release( p_input_thread );
892 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
895 input_thread_t *p_input_thread;
897 p_input_thread = libvlc_get_input_thread ( p_mi );
898 if( !p_input_thread )
901 var_SetFloat( p_input_thread, "position", position );
902 vlc_object_release( p_input_thread );
905 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
907 input_thread_t *p_input_thread;
910 p_input_thread = libvlc_get_input_thread ( p_mi );
911 if( !p_input_thread )
914 f_position = var_GetFloat( p_input_thread, "position" );
915 vlc_object_release( p_input_thread );
920 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
923 input_thread_t *p_input_thread;
925 p_input_thread = libvlc_get_input_thread ( p_mi );
926 if( !p_input_thread )
929 var_SetInteger( p_input_thread, "chapter", chapter );
930 vlc_object_release( p_input_thread );
933 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
935 input_thread_t *p_input_thread;
938 p_input_thread = libvlc_get_input_thread ( p_mi );
939 if( !p_input_thread )
942 i_chapter = var_GetInteger( p_input_thread, "chapter" );
943 vlc_object_release( p_input_thread );
948 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
950 input_thread_t *p_input_thread;
953 p_input_thread = libvlc_get_input_thread ( p_mi );
954 if( !p_input_thread )
957 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
958 vlc_object_release( p_input_thread );
963 int libvlc_media_player_get_chapter_count_for_title(
964 libvlc_media_player_t *p_mi,
967 input_thread_t *p_input_thread;
970 p_input_thread = libvlc_get_input_thread ( p_mi );
971 if( !p_input_thread )
975 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
977 vlc_object_release( p_input_thread );
980 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
981 vlc_object_release( p_input_thread );
987 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
990 input_thread_t *p_input_thread;
992 p_input_thread = libvlc_get_input_thread ( p_mi );
993 if( !p_input_thread )
996 var_SetInteger( p_input_thread, "title", i_title );
997 vlc_object_release( p_input_thread );
1000 libvlc_event_t event;
1001 event.type = libvlc_MediaPlayerTitleChanged;
1002 event.u.media_player_title_changed.new_title = i_title;
1003 libvlc_event_send( p_mi->p_event_manager, &event );
1006 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1008 input_thread_t *p_input_thread;
1011 p_input_thread = libvlc_get_input_thread ( p_mi );
1012 if( !p_input_thread )
1015 i_title = var_GetInteger( p_input_thread, "title" );
1016 vlc_object_release( p_input_thread );
1021 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1023 input_thread_t *p_input_thread;
1026 p_input_thread = libvlc_get_input_thread ( p_mi );
1027 if( !p_input_thread )
1030 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1031 vlc_object_release( p_input_thread );
1036 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1038 input_thread_t *p_input_thread;
1040 p_input_thread = libvlc_get_input_thread ( p_mi );
1041 if( !p_input_thread )
1044 int i_type = var_Type( p_input_thread, "next-chapter" );
1045 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1046 "next-chapter":"next-title", true );
1048 vlc_object_release( p_input_thread );
1051 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1053 input_thread_t *p_input_thread;
1055 p_input_thread = libvlc_get_input_thread ( p_mi );
1056 if( !p_input_thread )
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 );
1063 vlc_object_release( p_input_thread );
1066 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1068 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1071 if( p_input_thread )
1073 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1075 vlc_object_release( p_input_thread );
1080 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1083 input_thread_t *p_input_thread =
1084 libvlc_get_input_thread ( p_mi );
1085 if ( !p_input_thread )
1088 b_will_play = !p_input_thread->b_die && !p_input_thread->b_dead;
1089 vlc_object_release( p_input_thread );
1094 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1098 libvlc_printerr ("Playing backward not supported");
1102 var_SetFloat (p_mi, "rate", rate);
1104 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1105 if( !p_input_thread )
1107 var_SetFloat( p_input_thread, "rate", rate );
1108 vlc_object_release( p_input_thread );
1112 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1114 return var_GetFloat (p_mi, "rate");
1117 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1120 libvlc_state_t state = p_mi->state;
1125 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1127 input_thread_t *p_input_thread;
1130 p_input_thread = libvlc_get_input_thread ( p_mi );
1131 if ( !p_input_thread )
1133 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1134 vlc_object_release( p_input_thread );
1139 /* internal function, used by audio, video */
1140 libvlc_track_description_t *
1141 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1142 const char *psz_variable )
1144 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1145 libvlc_track_description_t *p_track_description = NULL,
1146 *p_actual, *p_previous;
1151 vlc_value_t val_list, text_list;
1152 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1155 if( val_list.p_list->i_count <= 0 )
1158 p_track_description = ( libvlc_track_description_t * )
1159 malloc( sizeof( libvlc_track_description_t ) );
1160 if ( !p_track_description )
1162 libvlc_printerr( "Not enough memory" );
1165 p_actual = p_track_description;
1167 for( int i = 0; i < val_list.p_list->i_count; i++ )
1171 p_actual = ( libvlc_track_description_t * )
1172 malloc( sizeof( libvlc_track_description_t ) );
1175 libvlc_track_description_release( p_track_description );
1176 libvlc_printerr( "Not enough memory" );
1180 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1181 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1182 p_actual->p_next = NULL;
1184 p_previous->p_next = p_actual;
1185 p_previous = p_actual;
1190 var_FreeList( &val_list, &text_list );
1191 vlc_object_release( p_input );
1193 return p_track_description;
1196 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1198 libvlc_track_description_t *p_actual, *p_before;
1203 free( p_actual->psz_name );
1204 p_before = p_actual;
1205 p_actual = p_before->p_next;
1210 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1212 input_thread_t *p_input_thread;
1215 p_input_thread = libvlc_get_input_thread ( p_mi );
1216 if ( !p_input_thread )
1218 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1219 vlc_object_release( p_input_thread );
1224 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1226 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1227 if( p_input_thread != NULL )
1229 var_TriggerCallback( p_input_thread, "frame-next" );
1230 vlc_object_release( p_input_thread );