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 * Set the internal state of the media_player. (media player Internal)
161 * Function will lock the media_player.
163 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state )
167 libvlc_media_t *media = p_mi->p_md;
169 libvlc_media_retain(media);
174 // Also set the state of the corresponding media
175 // This is strictly for convenience.
176 libvlc_media_set_state(media, state);
178 libvlc_media_release(media);
183 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
184 vlc_value_t oldval, vlc_value_t newval,
190 libvlc_media_player_t * p_mi = p_userdata;
191 libvlc_event_t event;
193 event.type = libvlc_MediaPlayerSeekableChanged;
194 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
196 libvlc_event_send( p_mi->p_event_manager, &event );
201 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
202 vlc_value_t oldval, vlc_value_t newval,
208 libvlc_media_player_t * p_mi = p_userdata;
209 libvlc_event_t event;
211 event.type = libvlc_MediaPlayerPausableChanged;
212 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
214 libvlc_event_send( p_mi->p_event_manager, &event );
219 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
220 vlc_value_t oldval, vlc_value_t newval,
224 input_thread_t * p_input = (input_thread_t *)p_this;
225 libvlc_media_player_t * p_mi = p_userdata;
226 libvlc_event_t event;
228 assert( !strcmp( psz_cmd, "intf-event" ) );
230 if( newval.i_int == INPUT_EVENT_STATE )
232 libvlc_state_t libvlc_state;
234 switch ( var_GetInteger( p_input, "state" ) )
237 libvlc_state = libvlc_NothingSpecial;
238 event.type = libvlc_MediaPlayerNothingSpecial;
241 libvlc_state = libvlc_Opening;
242 event.type = libvlc_MediaPlayerOpening;
245 libvlc_state = libvlc_Playing;
246 event.type = libvlc_MediaPlayerPlaying;
249 libvlc_state = libvlc_Paused;
250 event.type = libvlc_MediaPlayerPaused;
253 libvlc_state = libvlc_Ended;
254 event.type = libvlc_MediaPlayerEndReached;
257 libvlc_state = libvlc_Error;
258 event.type = libvlc_MediaPlayerEncounteredError;
265 set_state( p_mi, libvlc_state );
266 libvlc_event_send( p_mi->p_event_manager, &event );
268 else if( newval.i_int == INPUT_EVENT_ABORT )
270 libvlc_state_t libvlc_state = libvlc_Stopped;
271 event.type = libvlc_MediaPlayerStopped;
273 set_state( p_mi, libvlc_state );
274 libvlc_event_send( p_mi->p_event_manager, &event );
276 else if( newval.i_int == INPUT_EVENT_POSITION )
278 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
279 return VLC_SUCCESS; /* Don't send the position while stopped */
282 event.type = libvlc_MediaPlayerPositionChanged;
283 event.u.media_player_position_changed.new_position =
284 var_GetFloat( p_input, "position" );
285 libvlc_event_send( p_mi->p_event_manager, &event );
288 event.type = libvlc_MediaPlayerTimeChanged;
289 event.u.media_player_time_changed.new_time =
290 var_GetTime( p_input, "time" );
291 libvlc_event_send( p_mi->p_event_manager, &event );
293 else if( newval.i_int == INPUT_EVENT_LENGTH )
295 event.type = libvlc_MediaPlayerLengthChanged;
296 event.u.media_player_length_changed.new_length =
297 var_GetTime( p_input, "length" );
298 libvlc_event_send( p_mi->p_event_manager, &event );
305 /**************************************************************************
306 * Snapshot Taken Event.
308 * FIXME: This snapshot API interface makes no sense in media_player.
309 *************************************************************************/
310 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
311 vlc_value_t oldval, vlc_value_t newval, void *p_data )
313 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
315 libvlc_media_player_t *mp = p_data;
316 libvlc_event_t event;
317 event.type = libvlc_MediaPlayerSnapshotTaken;
318 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
319 libvlc_event_send(mp->p_event_manager, &event);
325 static void libvlc_media_player_destroy( libvlc_media_player_t * );
328 /**************************************************************************
329 * Create a Media Instance object.
332 * - All items created by _new start with a refcount set to 1.
333 * - Accessor _release decrease the refcount by 1, if after that
334 * operation the refcount is 0, the object is destroyed.
335 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
337 * Object locking strategy:
338 * - No lock held while in constructor.
339 * - When accessing any member variable this lock is held. (XXX who locks?)
340 * - When attempting to destroy the object the lock is also held.
341 **************************************************************************/
342 libvlc_media_player_t *
343 libvlc_media_player_new( libvlc_instance_t *instance, libvlc_exception_t *e )
345 libvlc_media_player_t * mp;
349 mp = malloc(sizeof(libvlc_media_player_t));
352 libvlc_exception_raise(e);
353 libvlc_printerr("Not enough memory");
357 mp->drawable.agl = 0;
358 mp->drawable.xid = 0;
359 mp->drawable.hwnd = NULL;
360 mp->drawable.nsobject = NULL;
361 mp->keyboard_events = mp->mouse_events = 1;
362 mp->state = libvlc_NothingSpecial;
363 mp->p_libvlc_instance = instance;
364 mp->p_input_thread = NULL;
365 mp->p_input_resource = NULL;
367 vlc_mutex_init(&mp->object_lock);
368 mp->p_event_manager = libvlc_event_manager_new(mp, instance, e);
369 if (libvlc_exception_raised(e))
371 vlc_mutex_destroy(&mp->object_lock);
376 register_event(mp, NothingSpecial);
377 register_event(mp, Opening);
378 register_event(mp, Buffering);
379 register_event(mp, Playing);
380 register_event(mp, Paused);
381 register_event(mp, Stopped);
382 register_event(mp, Forward);
383 register_event(mp, Backward);
384 register_event(mp, EndReached);
385 register_event(mp, EncounteredError);
387 register_event(mp, PositionChanged);
388 register_event(mp, TimeChanged);
389 register_event(mp, LengthChanged);
390 register_event(mp, TitleChanged);
391 register_event(mp, PausableChanged);
393 /* Snapshot initialization */
394 register_event(mp, SnapshotTaken);
396 register_event(mp, MediaChanged);
398 /* Attach a var callback to the global object to provide the glue between
399 * vout_thread that generates the event and media_player that re-emits it
400 * with its own event manager
402 * FIXME: It's unclear why we want to put this in public API, and why we
403 * want to expose it in such a limiting and ugly way.
405 var_AddCallback(instance->p_libvlc_int, "snapshot-file", snapshot_was_taken, mp);
410 /**************************************************************************
411 * Create a Media Instance object with a media descriptor.
412 **************************************************************************/
413 libvlc_media_player_t *
414 libvlc_media_player_new_from_media(
415 libvlc_media_t * p_md,
416 libvlc_exception_t *p_e )
418 libvlc_media_player_t * p_mi;
420 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
424 libvlc_media_retain( p_md );
430 /**************************************************************************
431 * Destroy a Media Instance object (libvlc internal)
433 * Warning: No lock held here, but hey, this is internal. Caller must lock.
434 **************************************************************************/
435 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
439 /* Detach Callback from the main libvlc object */
440 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
441 "snapshot-file", snapshot_was_taken, p_mi );
443 /* If the input thread hasn't been already deleted it means
444 * that the owners didn't stop the thread before releasing it. */
445 assert(!p_mi->p_input_thread);
447 /* Fallback for those who don't use NDEBUG */
448 if (p_mi->p_input_thread)
449 release_input_thread(p_mi, true);
451 if( p_mi->p_input_resource )
453 input_resource_Delete( p_mi->p_input_resource );
454 p_mi->p_input_resource = NULL;
457 libvlc_event_manager_release( p_mi->p_event_manager );
458 libvlc_media_release( p_mi->p_md );
459 vlc_mutex_destroy( &p_mi->object_lock );
463 /**************************************************************************
464 * Release a Media Instance object.
466 * Function does the locking.
467 **************************************************************************/
468 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
474 destroy = !--p_mi->i_refcount;
478 libvlc_media_player_destroy( p_mi );
481 /**************************************************************************
482 * Retain a Media Instance object.
484 * Caller must hold the lock.
485 **************************************************************************/
486 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
495 /**************************************************************************
496 * Set the Media descriptor associated with the instance.
498 * Enter without lock -- function will lock the object.
499 **************************************************************************/
500 void libvlc_media_player_set_media(
501 libvlc_media_player_t *p_mi,
502 libvlc_media_t *p_md,
503 libvlc_exception_t *p_e )
509 /* FIXME I am not sure if it is a user request or on die(eof/error)
511 release_input_thread( p_mi,
512 p_mi->p_input_thread &&
513 !p_mi->p_input_thread->b_eof &&
514 !p_mi->p_input_thread->b_error );
517 set_state( p_mi, libvlc_NothingSpecial );
521 libvlc_media_release( p_mi->p_md );
527 return; /* It is ok to pass a NULL md */
530 libvlc_media_retain( p_md );
533 /* The policy here is to ignore that we were created using a different
534 * libvlc_instance, because we don't really care */
535 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
539 /* Send an event for the newly available media */
540 libvlc_event_t event;
541 event.type = libvlc_MediaPlayerMediaChanged;
542 event.u.media_player_media_changed.new_media = p_md;
543 libvlc_event_send( p_mi->p_event_manager, &event );
547 /**************************************************************************
548 * Get the Media descriptor associated with the instance.
549 **************************************************************************/
551 libvlc_media_player_get_media(
552 libvlc_media_player_t *p_mi,
553 libvlc_exception_t *p_e )
561 libvlc_media_retain( p_mi->p_md );
566 /**************************************************************************
567 * Get the event Manager.
568 **************************************************************************/
569 libvlc_event_manager_t *
570 libvlc_media_player_event_manager(
571 libvlc_media_player_t *p_mi,
572 libvlc_exception_t *p_e )
576 return p_mi->p_event_manager;
579 /**************************************************************************
580 * Tell media player to start playing.
581 **************************************************************************/
582 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
583 libvlc_exception_t *p_e )
585 input_thread_t * p_input_thread;
587 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
589 /* A thread already exists, send it a play message */
590 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
591 vlc_object_release( p_input_thread );
595 /* Ignore previous exception */
596 libvlc_exception_clear( p_e );
603 libvlc_exception_raise( p_e );
604 libvlc_printerr( "No associated media descriptor" );
608 p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
609 p_mi->p_md->p_input_item, NULL, p_mi->p_input_resource );
611 if( !p_mi->p_input_thread )
617 p_mi->p_input_resource = NULL;
618 p_input_thread = p_mi->p_input_thread;
620 var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
621 if( p_mi->drawable.agl )
622 var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
624 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
625 if( p_mi->drawable.xid )
626 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
628 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
629 if( p_mi->drawable.hwnd != NULL )
630 var_SetAddress( p_input_thread, "drawable-hwnd", p_mi->drawable.hwnd );
632 var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
633 if( p_mi->drawable.nsobject != NULL )
634 var_SetAddress( p_input_thread, "drawable-nsobject", p_mi->drawable.nsobject );
636 var_Create( p_input_thread, "keyboard-events", VLC_VAR_BOOL );
637 var_SetBool( p_input_thread, "keyboard-events", p_mi->keyboard_events );
638 var_Create( p_input_thread, "mouse-events", VLC_VAR_BOOL );
639 var_SetBool( p_input_thread, "mouse-events", p_mi->mouse_events );
641 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
642 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
643 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
645 if( input_Start( p_input_thread ) )
647 vlc_object_release( p_input_thread );
648 p_mi->p_input_thread = NULL;
654 /**************************************************************************
656 **************************************************************************/
657 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
658 libvlc_exception_t *p_e )
660 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
661 if( !p_input_thread )
664 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
665 if( state == libvlc_Playing || state == libvlc_Buffering )
667 if( libvlc_media_player_can_pause( p_mi, p_e ) )
668 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
670 libvlc_media_player_stop( p_mi, p_e );
673 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
675 vlc_object_release( p_input_thread );
678 /**************************************************************************
679 * Tells whether the media player is currently playing.
681 * Enter with lock held.
682 **************************************************************************/
683 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
684 libvlc_exception_t *p_e )
686 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
687 return (libvlc_Playing == state) || (libvlc_Buffering == state);
690 /**************************************************************************
692 **************************************************************************/
693 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
694 libvlc_exception_t *p_e )
696 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
699 release_input_thread( p_mi, true ); /* This will stop the input thread */
702 /* Force to go to stopped state, in case we were in Ended, or Error
704 if( state != libvlc_Stopped )
706 set_state( p_mi, libvlc_Stopped );
708 /* Construct and send the event */
709 libvlc_event_t event;
710 event.type = libvlc_MediaPlayerStopped;
711 libvlc_event_send( p_mi->p_event_manager, &event );
715 /**************************************************************************
717 **************************************************************************/
718 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
720 libvlc_exception_t *p_e )
723 p_mi->drawable.nsobject = drawable;
726 /**************************************************************************
728 **************************************************************************/
729 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
731 return p_mi->drawable.nsobject;
734 /**************************************************************************
736 **************************************************************************/
737 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
739 libvlc_exception_t *p_e )
742 p_mi->drawable.agl = drawable;
745 /**************************************************************************
747 **************************************************************************/
748 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
750 return p_mi->drawable.agl;
753 /**************************************************************************
755 **************************************************************************/
756 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
758 libvlc_exception_t *p_e )
761 p_mi->drawable.xid = drawable;
764 /**************************************************************************
766 **************************************************************************/
767 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
769 return p_mi->drawable.xid;
772 /**************************************************************************
774 **************************************************************************/
775 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
777 libvlc_exception_t *p_e )
780 p_mi->drawable.hwnd = drawable;
783 /**************************************************************************
785 **************************************************************************/
786 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
788 return p_mi->drawable.hwnd;
791 /**************************************************************************
792 * Getters for stream information
793 **************************************************************************/
794 libvlc_time_t libvlc_media_player_get_length(
795 libvlc_media_player_t *p_mi,
796 libvlc_exception_t *p_e )
798 input_thread_t *p_input_thread;
799 libvlc_time_t i_time;
801 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
802 if( !p_input_thread )
805 i_time = var_GetTime( p_input_thread, "length" );
806 vlc_object_release( p_input_thread );
808 return (i_time+500LL)/1000LL;
811 libvlc_time_t libvlc_media_player_get_time(
812 libvlc_media_player_t *p_mi,
813 libvlc_exception_t *p_e )
815 input_thread_t *p_input_thread;
816 libvlc_time_t i_time;
818 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
819 if( !p_input_thread )
822 i_time = var_GetTime( p_input_thread , "time" );
823 vlc_object_release( p_input_thread );
824 return (i_time+500LL)/1000LL;
827 void libvlc_media_player_set_time(
828 libvlc_media_player_t *p_mi,
829 libvlc_time_t i_time,
830 libvlc_exception_t *p_e )
832 input_thread_t *p_input_thread;
834 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
835 if( !p_input_thread )
838 var_SetTime( p_input_thread, "time", i_time*1000LL );
839 vlc_object_release( p_input_thread );
842 void libvlc_media_player_set_position(
843 libvlc_media_player_t *p_mi,
845 libvlc_exception_t *p_e )
847 input_thread_t *p_input_thread;
849 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
850 if( !p_input_thread )
853 var_SetFloat( p_input_thread, "position", position );
854 vlc_object_release( p_input_thread );
857 float libvlc_media_player_get_position(
858 libvlc_media_player_t *p_mi,
859 libvlc_exception_t *p_e )
861 input_thread_t *p_input_thread;
864 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
865 if( !p_input_thread )
868 f_position = var_GetFloat( p_input_thread, "position" );
869 vlc_object_release( p_input_thread );
874 void libvlc_media_player_set_chapter(
875 libvlc_media_player_t *p_mi,
877 libvlc_exception_t *p_e )
879 input_thread_t *p_input_thread;
881 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
882 if( !p_input_thread )
885 var_SetInteger( p_input_thread, "chapter", chapter );
886 vlc_object_release( p_input_thread );
889 int libvlc_media_player_get_chapter(
890 libvlc_media_player_t *p_mi,
891 libvlc_exception_t *p_e )
893 input_thread_t *p_input_thread;
896 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
897 if( !p_input_thread )
900 i_chapter = var_GetInteger( p_input_thread, "chapter" );
901 vlc_object_release( p_input_thread );
906 int libvlc_media_player_get_chapter_count(
907 libvlc_media_player_t *p_mi,
908 libvlc_exception_t *p_e )
910 input_thread_t *p_input_thread;
913 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
914 if( !p_input_thread )
917 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
918 vlc_object_release( p_input_thread );
923 int libvlc_media_player_get_chapter_count_for_title(
924 libvlc_media_player_t *p_mi,
926 libvlc_exception_t *p_e )
928 input_thread_t *p_input_thread;
931 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
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(
949 libvlc_media_player_t *p_mi,
951 libvlc_exception_t *p_e )
953 input_thread_t *p_input_thread;
955 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
956 if( !p_input_thread )
959 var_SetInteger( p_input_thread, "title", i_title );
960 vlc_object_release( p_input_thread );
963 libvlc_event_t event;
964 event.type = libvlc_MediaPlayerTitleChanged;
965 event.u.media_player_title_changed.new_title = i_title;
966 libvlc_event_send( p_mi->p_event_manager, &event );
969 int libvlc_media_player_get_title(
970 libvlc_media_player_t *p_mi,
971 libvlc_exception_t *p_e )
973 input_thread_t *p_input_thread;
976 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
977 if( !p_input_thread )
980 i_title = var_GetInteger( p_input_thread, "title" );
981 vlc_object_release( p_input_thread );
986 int libvlc_media_player_get_title_count(
987 libvlc_media_player_t *p_mi,
988 libvlc_exception_t *p_e )
990 input_thread_t *p_input_thread;
993 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
994 if( !p_input_thread )
997 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
998 vlc_object_release( p_input_thread );
1003 void libvlc_media_player_next_chapter(
1004 libvlc_media_player_t *p_mi,
1005 libvlc_exception_t *p_e )
1007 input_thread_t *p_input_thread;
1009 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1010 if( !p_input_thread )
1013 int i_type = var_Type( p_input_thread, "next-chapter" );
1014 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1015 "next-chapter":"next-title", true );
1017 vlc_object_release( p_input_thread );
1020 void libvlc_media_player_previous_chapter(
1021 libvlc_media_player_t *p_mi,
1022 libvlc_exception_t *p_e )
1024 input_thread_t *p_input_thread;
1026 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1027 if( !p_input_thread )
1030 int i_type = var_Type( p_input_thread, "next-chapter" );
1031 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1032 "prev-chapter":"prev-title", true );
1034 vlc_object_release( p_input_thread );
1037 float libvlc_media_player_get_fps(
1038 libvlc_media_player_t *p_mi,
1039 libvlc_exception_t *p_e)
1041 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1044 if( p_input_thread )
1046 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1048 vlc_object_release( p_input_thread );
1053 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1054 libvlc_exception_t *p_e)
1057 input_thread_t *p_input_thread =
1058 libvlc_get_input_thread ( p_mi, p_e);
1059 if ( !p_input_thread )
1062 b_will_play = !p_input_thread->b_die && !p_input_thread->b_dead;
1063 vlc_object_release( p_input_thread );
1068 void libvlc_media_player_set_rate(
1069 libvlc_media_player_t *p_mi,
1071 libvlc_exception_t *p_e )
1073 input_thread_t *p_input_thread;
1076 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1077 if( !p_input_thread )
1080 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1081 if( (rate < 0.0) && !b_can_rewind )
1083 vlc_object_release( p_input_thread );
1084 libvlc_exception_raise( p_e );
1085 libvlc_printerr( "Invalid playback rate" );
1089 var_SetFloat( p_input_thread, "rate", rate );
1090 vlc_object_release( p_input_thread );
1093 float libvlc_media_player_get_rate(
1094 libvlc_media_player_t *p_mi,
1095 libvlc_exception_t *p_e )
1097 input_thread_t *p_input_thread;
1101 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1102 if( !p_input_thread )
1103 return 0.0; /* rate < 0 indicates rewind */
1105 f_rate = var_GetFloat( p_input_thread, "rate" );
1106 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1107 /* FIXME: why are negative values forbidden ?? (rewinding) */
1108 if( f_rate < 0 && !b_can_rewind )
1110 vlc_object_release( p_input_thread );
1113 vlc_object_release( p_input_thread );
1118 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
1122 libvlc_state_t state = p_mi->state;
1127 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1128 libvlc_exception_t *p_e )
1130 input_thread_t *p_input_thread;
1133 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1134 if ( !p_input_thread )
1136 /* We do return the right value, no need to throw an exception */
1137 clear_if_needed(p_e);
1140 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1141 vlc_object_release( p_input_thread );
1146 /* internal function, used by audio, video */
1147 libvlc_track_description_t *
1148 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1149 const char *psz_variable,
1150 libvlc_exception_t *p_e )
1152 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1153 libvlc_track_description_t *p_track_description = NULL,
1154 *p_actual, *p_previous;
1159 vlc_value_t val_list, text_list;
1160 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1163 if( val_list.p_list->i_count <= 0 )
1166 p_track_description = ( libvlc_track_description_t * )
1167 malloc( sizeof( libvlc_track_description_t ) );
1168 if ( !p_track_description )
1170 libvlc_exception_raise( p_e );
1171 libvlc_printerr( "Not enough memory" );
1174 p_actual = p_track_description;
1176 for( int i = 0; i < val_list.p_list->i_count; i++ )
1180 p_actual = ( libvlc_track_description_t * )
1181 malloc( sizeof( libvlc_track_description_t ) );
1184 libvlc_track_description_release( p_track_description );
1185 libvlc_exception_raise( p_e );
1186 libvlc_printerr( "Not enough memory" );
1190 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1191 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1192 p_actual->p_next = NULL;
1194 p_previous->p_next = p_actual;
1195 p_previous = p_actual;
1200 var_FreeList( &val_list, &text_list );
1201 vlc_object_release( p_input );
1203 return p_track_description;
1206 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1208 libvlc_track_description_t *p_actual, *p_before;
1213 free( p_actual->psz_name );
1214 p_before = p_actual;
1215 p_actual = p_before->p_next;
1220 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1221 libvlc_exception_t *p_e )
1223 input_thread_t *p_input_thread;
1226 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1227 if ( !p_input_thread )
1229 /* We do return the right value, no need to throw an exception */
1230 clear_if_needed(p_e);
1233 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1234 vlc_object_release( p_input_thread );
1239 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
1241 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1242 if( p_input_thread != NULL )
1244 var_TriggerCallback( p_input_thread, "frame-next" );
1245 vlc_object_release( p_input_thread );
1249 libvlc_exception_raise( p_e );
1250 libvlc_printerr( "No active input" );