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, NULL);
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);
83 static inline void clear_if_needed(libvlc_exception_t *e)
85 if (libvlc_exception_raised(e))
86 libvlc_exception_clear(e);
90 * Release the associated input thread.
92 * Object lock is NOT held.
94 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
96 input_thread_t * p_input_thread;
98 if( !p_mi || !p_mi->p_input_thread )
101 p_input_thread = p_mi->p_input_thread;
103 var_DelCallback( p_input_thread, "can-seek",
104 input_seekable_changed, p_mi );
105 var_DelCallback( p_input_thread, "can-pause",
106 input_pausable_changed, p_mi );
107 var_DelCallback( p_input_thread, "intf-event",
108 input_event_changed, p_mi );
110 /* We owned this one */
111 input_Stop( p_input_thread, b_input_abort );
113 vlc_thread_join( p_input_thread );
115 assert( p_mi->p_input_resource == NULL );
116 assert( p_input_thread->b_dead );
117 /* Store the input resource for future use. */
118 p_mi->p_input_resource = input_DetachResource( p_input_thread );
120 var_Destroy( p_input_thread, "drawable-hwnd" );
121 var_Destroy( p_input_thread, "drawable-xid" );
122 var_Destroy( p_input_thread, "drawable-agl" );
124 vlc_object_release( p_input_thread );
126 p_mi->p_input_thread = NULL;
130 * Retrieve the input thread. Be sure to release the object
131 * once you are done with it. (libvlc Internal)
133 * Function will lock the object.
135 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
136 libvlc_exception_t *p_e )
138 input_thread_t *p_input_thread;
140 if( !p_mi ) RAISENULL( "Media Instance is NULL" );
144 if( !p_mi->p_input_thread )
147 RAISENULL( "Input is NULL" );
150 p_input_thread = p_mi->p_input_thread;
151 vlc_object_hold( p_input_thread );
155 return p_input_thread;
159 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
160 vlc_value_t oldval, vlc_value_t newval,
166 libvlc_media_player_t * p_mi = p_userdata;
167 libvlc_event_t event;
169 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial );
170 event.type = libvlc_MediaPlayerSeekableChanged;
171 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
173 libvlc_event_send( p_mi->p_event_manager, &event );
178 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
179 vlc_value_t oldval, vlc_value_t newval,
185 libvlc_media_player_t * p_mi = p_userdata;
186 libvlc_event_t event;
188 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial );
189 event.type = libvlc_MediaPlayerPausableChanged;
190 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
192 libvlc_event_send( p_mi->p_event_manager, &event );
197 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
198 vlc_value_t oldval, vlc_value_t newval,
202 input_thread_t * p_input = (input_thread_t *)p_this;
203 libvlc_media_player_t * p_mi = p_userdata;
204 libvlc_event_t event;
206 assert( !strcmp( psz_cmd, "intf-event" ) );
208 if( newval.i_int == INPUT_EVENT_STATE )
210 libvlc_state_t libvlc_state;
212 switch ( var_GetInteger( p_input, "state" ) )
215 libvlc_state = libvlc_NothingSpecial;
216 event.type = libvlc_MediaPlayerNothingSpecial;
219 libvlc_state = libvlc_Opening;
220 event.type = libvlc_MediaPlayerOpening;
223 libvlc_state = libvlc_Playing;
224 event.type = libvlc_MediaPlayerPlaying;
227 libvlc_state = libvlc_Paused;
228 event.type = libvlc_MediaPlayerPaused;
231 libvlc_state = libvlc_Ended;
232 event.type = libvlc_MediaPlayerEndReached;
235 libvlc_state = libvlc_Error;
236 event.type = libvlc_MediaPlayerEncounteredError;
243 libvlc_media_set_state( p_mi->p_md, libvlc_state );
244 libvlc_event_send( p_mi->p_event_manager, &event );
246 else if( newval.i_int == INPUT_EVENT_ABORT )
248 libvlc_state_t libvlc_state = libvlc_Stopped;
249 event.type = libvlc_MediaPlayerStopped;
251 libvlc_media_set_state( p_mi->p_md, libvlc_state );
252 libvlc_event_send( p_mi->p_event_manager, &event );
254 else if( newval.i_int == INPUT_EVENT_POSITION )
256 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
257 return VLC_SUCCESS; /* Don't send the position while stopped */
260 event.type = libvlc_MediaPlayerPositionChanged;
261 event.u.media_player_position_changed.new_position =
262 var_GetFloat( p_input, "position" );
263 libvlc_event_send( p_mi->p_event_manager, &event );
266 event.type = libvlc_MediaPlayerTimeChanged;
267 event.u.media_player_time_changed.new_time =
268 var_GetTime( p_input, "time" );
269 libvlc_event_send( p_mi->p_event_manager, &event );
271 else if( newval.i_int == INPUT_EVENT_LENGTH )
273 event.type = libvlc_MediaPlayerLengthChanged;
274 event.u.media_player_length_changed.new_length =
275 var_GetTime( p_input, "length" );
276 libvlc_event_send( p_mi->p_event_manager, &event );
283 /**************************************************************************
284 * Snapshot Taken Event.
286 * FIXME: This snapshot API interface makes no sense in media_player.
287 *************************************************************************/
288 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
289 vlc_value_t oldval, vlc_value_t newval, void *p_data )
291 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
293 libvlc_media_player_t *mp = p_data;
294 libvlc_event_t event;
295 event.type = libvlc_MediaPlayerSnapshotTaken;
296 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
297 libvlc_event_send(mp->p_event_manager, &event);
303 static void libvlc_media_player_destroy( libvlc_media_player_t * );
306 /**************************************************************************
307 * Create a Media Instance object.
310 * - All items created by _new start with a refcount set to 1.
311 * - Accessor _release decrease the refcount by 1, if after that
312 * operation the refcount is 0, the object is destroyed.
313 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
315 * Object locking strategy:
316 * - No lock held while in constructor.
317 * - When accessing any member variable this lock is held. (XXX who locks?)
318 * - When attempting to destroy the object the lock is also held.
319 **************************************************************************/
320 libvlc_media_player_t *
321 libvlc_media_player_new( libvlc_instance_t *instance, libvlc_exception_t *e )
323 libvlc_media_player_t * mp;
327 mp = malloc(sizeof(libvlc_media_player_t));
330 libvlc_exception_raise(e);
331 libvlc_printerr("Not enough memory");
335 mp->drawable.agl = 0;
336 mp->drawable.xid = 0;
337 mp->drawable.hwnd = NULL;
338 mp->drawable.nsobject = NULL;
339 mp->p_libvlc_instance = instance;
340 mp->p_input_thread = NULL;
341 mp->p_input_resource = NULL;
343 vlc_mutex_init(&mp->object_lock);
344 mp->p_event_manager = libvlc_event_manager_new(mp, instance, e);
345 if (libvlc_exception_raised(e))
347 vlc_mutex_destroy(&mp->object_lock);
352 register_event(mp, NothingSpecial);
353 register_event(mp, Opening);
354 register_event(mp, Buffering);
355 register_event(mp, Playing);
356 register_event(mp, Paused);
357 register_event(mp, Stopped);
358 register_event(mp, Forward);
359 register_event(mp, Backward);
360 register_event(mp, EndReached);
361 register_event(mp, EncounteredError);
363 register_event(mp, PositionChanged);
364 register_event(mp, TimeChanged);
365 register_event(mp, LengthChanged);
366 register_event(mp, TitleChanged);
367 register_event(mp, PausableChanged);
369 /* Snapshot initialization */
370 register_event(mp, SnapshotTaken);
372 /* Attach a var callback to the global object to provide the glue between
373 * vout_thread that generates the event and media_player that re-emits it
374 * with its own event manager
376 * FIXME: It's unclear why we want to put this in public API, and why we
377 * want to expose it in such a limiting and ugly way.
379 var_AddCallback(instance->p_libvlc_int, "snapshot-file", snapshot_was_taken, mp);
384 /**************************************************************************
385 * Create a Media Instance object with a media descriptor.
386 **************************************************************************/
387 libvlc_media_player_t *
388 libvlc_media_player_new_from_media(
389 libvlc_media_t * p_md,
390 libvlc_exception_t *p_e )
392 libvlc_media_player_t * p_mi;
394 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
398 libvlc_media_retain( p_md );
404 /**************************************************************************
405 * Destroy a Media Instance object (libvlc internal)
407 * Warning: No lock held here, but hey, this is internal. Caller must lock.
408 **************************************************************************/
409 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
413 /* Detach Callback from the main libvlc object */
414 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
415 "snapshot-file", snapshot_was_taken, p_mi );
417 /* Release the input thread */
418 release_input_thread( p_mi, true );
420 if( p_mi->p_input_resource )
422 input_resource_Delete( p_mi->p_input_resource );
423 p_mi->p_input_resource = NULL;
426 libvlc_event_manager_release( p_mi->p_event_manager );
427 libvlc_media_release( p_mi->p_md );
428 vlc_mutex_destroy( &p_mi->object_lock );
432 /**************************************************************************
433 * Release a Media Instance object.
435 * Function does the locking.
436 **************************************************************************/
437 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
443 destroy = !--p_mi->i_refcount;
447 libvlc_media_player_destroy( p_mi );
450 /**************************************************************************
451 * Retain a Media Instance object.
453 * Caller must hold the lock.
454 **************************************************************************/
455 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
464 /**************************************************************************
465 * Set the Media descriptor associated with the instance.
467 * Enter without lock -- function will lock the object.
468 **************************************************************************/
469 void libvlc_media_player_set_media(
470 libvlc_media_player_t *p_mi,
471 libvlc_media_t *p_md,
472 libvlc_exception_t *p_e )
481 /* FIXME I am not sure if it is a user request or on die(eof/error)
483 release_input_thread( p_mi,
484 p_mi->p_input_thread &&
485 !p_mi->p_input_thread->b_eof &&
486 !p_mi->p_input_thread->b_error );
489 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial );
491 libvlc_media_release( p_mi->p_md );
497 return; /* It is ok to pass a NULL md */
500 libvlc_media_retain( p_md );
503 /* The policy here is to ignore that we were created using a different
504 * libvlc_instance, because we don't really care */
505 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
510 /**************************************************************************
511 * Get the Media descriptor associated with the instance.
512 **************************************************************************/
514 libvlc_media_player_get_media(
515 libvlc_media_player_t *p_mi,
516 libvlc_exception_t *p_e )
524 libvlc_media_retain( p_mi->p_md );
529 /**************************************************************************
530 * Get the event Manager.
531 **************************************************************************/
532 libvlc_event_manager_t *
533 libvlc_media_player_event_manager(
534 libvlc_media_player_t *p_mi,
535 libvlc_exception_t *p_e )
539 return p_mi->p_event_manager;
542 /**************************************************************************
543 * Tell media player to start playing.
544 **************************************************************************/
545 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
546 libvlc_exception_t *p_e )
548 input_thread_t * p_input_thread;
550 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
552 /* A thread already exists, send it a play message */
553 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
554 vlc_object_release( p_input_thread );
558 /* Ignore previous exception */
559 libvlc_exception_clear( p_e );
566 libvlc_exception_raise( p_e );
567 libvlc_printerr( "No associated media descriptor" );
571 p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
572 p_mi->p_md->p_input_item, NULL, p_mi->p_input_resource );
574 if( !p_mi->p_input_thread )
580 p_mi->p_input_resource = NULL;
581 p_input_thread = p_mi->p_input_thread;
583 var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
584 if( p_mi->drawable.agl )
585 var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
587 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
588 if( p_mi->drawable.xid )
589 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
591 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
592 if( p_mi->drawable.hwnd != NULL )
593 var_SetAddress( p_input_thread, "drawable-hwnd", p_mi->drawable.hwnd );
595 var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
596 if( p_mi->drawable.nsobject != NULL )
597 var_SetAddress( p_input_thread, "drawable-nsobject", p_mi->drawable.nsobject );
599 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
600 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
601 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
603 if( input_Start( p_input_thread ) )
605 vlc_object_release( p_input_thread );
606 p_mi->p_input_thread = NULL;
612 /**************************************************************************
614 **************************************************************************/
615 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
616 libvlc_exception_t *p_e )
618 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
619 if( !p_input_thread )
622 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
623 if( state == libvlc_Playing || state == libvlc_Buffering )
625 if( libvlc_media_player_can_pause( p_mi, p_e ) )
626 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
628 libvlc_media_player_stop( p_mi, p_e );
631 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
633 vlc_object_release( p_input_thread );
636 /**************************************************************************
637 * Tells whether the media player is currently playing.
639 * Enter with lock held.
640 **************************************************************************/
641 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
642 libvlc_exception_t *p_e )
644 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
645 return (libvlc_Playing == state) || (libvlc_Buffering == state);
648 /**************************************************************************
650 **************************************************************************/
651 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
652 libvlc_exception_t *p_e )
654 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
656 if( state == libvlc_Playing ||
657 state == libvlc_Paused ||
658 state == libvlc_Buffering )
660 /* Send a stop notification event only if we are in playing,
661 * buffering or paused states */
662 libvlc_media_set_state( p_mi->p_md, libvlc_Stopped );
664 /* Construct and send the event */
665 libvlc_event_t event;
666 event.type = libvlc_MediaPlayerStopped;
667 libvlc_event_send( p_mi->p_event_manager, &event );
671 release_input_thread( p_mi, true ); /* This will stop the input thread */
675 /**************************************************************************
677 **************************************************************************/
678 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
680 libvlc_exception_t *p_e )
683 p_mi->drawable.nsobject = drawable;
686 /**************************************************************************
688 **************************************************************************/
689 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
691 return p_mi->drawable.nsobject;
694 /**************************************************************************
696 **************************************************************************/
697 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
699 libvlc_exception_t *p_e )
702 p_mi->drawable.agl = drawable;
705 /**************************************************************************
707 **************************************************************************/
708 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
710 return p_mi->drawable.agl;
713 /**************************************************************************
715 **************************************************************************/
716 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
718 libvlc_exception_t *p_e )
721 p_mi->drawable.xid = drawable;
724 /**************************************************************************
726 **************************************************************************/
727 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
729 return p_mi->drawable.xid;
732 /**************************************************************************
734 **************************************************************************/
735 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
737 libvlc_exception_t *p_e )
740 p_mi->drawable.hwnd = drawable;
743 /**************************************************************************
745 **************************************************************************/
746 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
748 return p_mi->drawable.hwnd;
751 /**************************************************************************
752 * Getters for stream information
753 **************************************************************************/
754 libvlc_time_t libvlc_media_player_get_length(
755 libvlc_media_player_t *p_mi,
756 libvlc_exception_t *p_e )
758 input_thread_t *p_input_thread;
759 libvlc_time_t i_time;
761 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
762 if( !p_input_thread )
765 i_time = var_GetTime( p_input_thread, "length" );
766 vlc_object_release( p_input_thread );
768 return (i_time+500LL)/1000LL;
771 libvlc_time_t libvlc_media_player_get_time(
772 libvlc_media_player_t *p_mi,
773 libvlc_exception_t *p_e )
775 input_thread_t *p_input_thread;
776 libvlc_time_t i_time;
778 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
779 if( !p_input_thread )
782 i_time = var_GetTime( p_input_thread , "time" );
783 vlc_object_release( p_input_thread );
784 return (i_time+500LL)/1000LL;
787 void libvlc_media_player_set_time(
788 libvlc_media_player_t *p_mi,
789 libvlc_time_t i_time,
790 libvlc_exception_t *p_e )
792 input_thread_t *p_input_thread;
794 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
795 if( !p_input_thread )
798 var_SetTime( p_input_thread, "time", i_time*1000LL );
799 vlc_object_release( p_input_thread );
802 void libvlc_media_player_set_position(
803 libvlc_media_player_t *p_mi,
805 libvlc_exception_t *p_e )
807 input_thread_t *p_input_thread;
809 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
810 if( !p_input_thread )
813 var_SetFloat( p_input_thread, "position", position );
814 vlc_object_release( p_input_thread );
817 float libvlc_media_player_get_position(
818 libvlc_media_player_t *p_mi,
819 libvlc_exception_t *p_e )
821 input_thread_t *p_input_thread;
824 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
825 if( !p_input_thread )
828 f_position = var_GetFloat( p_input_thread, "position" );
829 vlc_object_release( p_input_thread );
834 void libvlc_media_player_set_chapter(
835 libvlc_media_player_t *p_mi,
837 libvlc_exception_t *p_e )
839 input_thread_t *p_input_thread;
841 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
842 if( !p_input_thread )
845 var_SetInteger( p_input_thread, "chapter", chapter );
846 vlc_object_release( p_input_thread );
849 int libvlc_media_player_get_chapter(
850 libvlc_media_player_t *p_mi,
851 libvlc_exception_t *p_e )
853 input_thread_t *p_input_thread;
856 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
857 if( !p_input_thread )
860 i_chapter = var_GetInteger( p_input_thread, "chapter" );
861 vlc_object_release( p_input_thread );
866 int libvlc_media_player_get_chapter_count(
867 libvlc_media_player_t *p_mi,
868 libvlc_exception_t *p_e )
870 input_thread_t *p_input_thread;
873 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
874 if( !p_input_thread )
877 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
878 vlc_object_release( p_input_thread );
883 int libvlc_media_player_get_chapter_count_for_title(
884 libvlc_media_player_t *p_mi,
886 libvlc_exception_t *p_e )
888 input_thread_t *p_input_thread;
891 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
892 if( !p_input_thread )
896 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
898 vlc_object_release( p_input_thread );
901 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
902 vlc_object_release( p_input_thread );
908 void libvlc_media_player_set_title(
909 libvlc_media_player_t *p_mi,
911 libvlc_exception_t *p_e )
913 input_thread_t *p_input_thread;
915 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
916 if( !p_input_thread )
919 var_SetInteger( p_input_thread, "title", i_title );
920 vlc_object_release( p_input_thread );
923 libvlc_event_t event;
924 event.type = libvlc_MediaPlayerTitleChanged;
925 event.u.media_player_title_changed.new_title = i_title;
926 libvlc_event_send( p_mi->p_event_manager, &event );
929 int libvlc_media_player_get_title(
930 libvlc_media_player_t *p_mi,
931 libvlc_exception_t *p_e )
933 input_thread_t *p_input_thread;
936 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
937 if( !p_input_thread )
940 i_title = var_GetInteger( p_input_thread, "title" );
941 vlc_object_release( p_input_thread );
946 int libvlc_media_player_get_title_count(
947 libvlc_media_player_t *p_mi,
948 libvlc_exception_t *p_e )
950 input_thread_t *p_input_thread;
953 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
954 if( !p_input_thread )
957 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
958 vlc_object_release( p_input_thread );
963 void libvlc_media_player_next_chapter(
964 libvlc_media_player_t *p_mi,
965 libvlc_exception_t *p_e )
967 input_thread_t *p_input_thread;
969 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
970 if( !p_input_thread )
973 int i_type = var_Type( p_input_thread, "next-chapter" );
974 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
975 "next-chapter":"next-title", true );
977 vlc_object_release( p_input_thread );
980 void libvlc_media_player_previous_chapter(
981 libvlc_media_player_t *p_mi,
982 libvlc_exception_t *p_e )
984 input_thread_t *p_input_thread;
986 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
987 if( !p_input_thread )
990 int i_type = var_Type( p_input_thread, "next-chapter" );
991 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
992 "prev-chapter":"prev-title", true );
994 vlc_object_release( p_input_thread );
997 float libvlc_media_player_get_fps(
998 libvlc_media_player_t *p_mi,
999 libvlc_exception_t *p_e)
1001 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1004 if( p_input_thread )
1006 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1008 vlc_object_release( p_input_thread );
1013 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1014 libvlc_exception_t *p_e)
1016 input_thread_t *p_input_thread =
1017 libvlc_get_input_thread ( p_mi, p_e);
1018 if ( !p_input_thread )
1021 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1023 vlc_object_release( p_input_thread );
1026 vlc_object_release( p_input_thread );
1030 void libvlc_media_player_set_rate(
1031 libvlc_media_player_t *p_mi,
1033 libvlc_exception_t *p_e )
1035 input_thread_t *p_input_thread;
1038 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1039 if( !p_input_thread )
1042 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1043 if( (rate < 0.0) && !b_can_rewind )
1045 vlc_object_release( p_input_thread );
1046 libvlc_exception_raise( p_e );
1047 libvlc_printerr( "Invalid playback rate" );
1051 var_SetInteger( p_input_thread, "rate", 1000.0f/rate );
1052 vlc_object_release( p_input_thread );
1055 float libvlc_media_player_get_rate(
1056 libvlc_media_player_t *p_mi,
1057 libvlc_exception_t *p_e )
1059 input_thread_t *p_input_thread;
1063 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1064 if( !p_input_thread )
1065 return 0.0; /* rate < 0 indicates rewind */
1067 i_rate = var_GetInteger( p_input_thread, "rate" );
1068 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1069 if( i_rate < 0 && !b_can_rewind )
1071 vlc_object_release( p_input_thread );
1074 vlc_object_release( p_input_thread );
1076 return (float)1000.0f/i_rate;
1079 libvlc_state_t libvlc_media_player_get_state(
1080 libvlc_media_player_t *p_mi,
1081 libvlc_exception_t *p_e )
1083 input_thread_t *p_input_thread;
1084 libvlc_state_t state = libvlc_Ended;
1086 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1087 if( !p_input_thread )
1089 /* We do return the right value, no need to throw an exception */
1090 clear_if_needed(p_e);
1094 state = libvlc_media_get_state( p_mi->p_md );
1095 if( state == libvlc_Playing )
1098 caching = var_GetFloat( p_input_thread, "cache" );
1099 if( caching > 0.0 && caching < 1.0 )
1100 state = libvlc_Buffering;
1102 vlc_object_release( p_input_thread );
1106 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1107 libvlc_exception_t *p_e )
1109 input_thread_t *p_input_thread;
1112 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1113 if ( !p_input_thread )
1115 /* We do return the right value, no need to throw an exception */
1116 clear_if_needed(p_e);
1119 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1120 vlc_object_release( p_input_thread );
1125 /* internal function, used by audio, video */
1126 libvlc_track_description_t *
1127 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1128 const char *psz_variable,
1129 libvlc_exception_t *p_e )
1131 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1132 libvlc_track_description_t *p_track_description = NULL,
1133 *p_actual, *p_previous;
1138 vlc_value_t val_list, text_list;
1139 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1142 if( val_list.p_list->i_count <= 0 )
1145 p_track_description = ( libvlc_track_description_t * )
1146 malloc( sizeof( libvlc_track_description_t ) );
1147 if ( !p_track_description )
1149 libvlc_exception_raise( p_e );
1150 libvlc_printerr( "Not enough memory" );
1153 p_actual = p_track_description;
1155 for( int i = 0; i < val_list.p_list->i_count; i++ )
1159 p_actual = ( libvlc_track_description_t * )
1160 malloc( sizeof( libvlc_track_description_t ) );
1163 libvlc_track_description_release( p_track_description );
1164 libvlc_exception_raise( p_e );
1165 libvlc_printerr( "Not enough memory" );
1169 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1170 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1171 p_actual->p_next = NULL;
1173 p_previous->p_next = p_actual;
1174 p_previous = p_actual;
1179 var_FreeList( &val_list, &text_list );
1180 vlc_object_release( p_input );
1182 return p_track_description;
1185 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1187 libvlc_track_description_t *p_actual, *p_before;
1192 free( p_actual->psz_name );
1193 p_before = p_actual;
1194 p_actual = p_before->p_next;
1199 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1200 libvlc_exception_t *p_e )
1202 input_thread_t *p_input_thread;
1205 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1206 if ( !p_input_thread )
1208 /* We do return the right value, no need to throw an exception */
1209 clear_if_needed(p_e);
1212 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1213 vlc_object_release( p_input_thread );
1218 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
1220 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1221 if( p_input_thread != NULL )
1223 var_TriggerCallback( p_input_thread, "frame-next" );
1224 vlc_object_release( p_input_thread );
1228 libvlc_exception_raise( p_e );
1229 libvlc_printerr( "No active input" );