1 /*****************************************************************************
2 * media_player.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005-2009 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_events.h>
34 #include <vlc_demux.h>
35 #include <vlc_input.h>
40 #include "libvlc_internal.h"
41 #include "media_internal.h" // libvlc_media_set_state()
42 #include "media_player_internal.h"
45 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
46 vlc_value_t oldval, vlc_value_t newval,
49 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
50 vlc_value_t oldval, vlc_value_t newval,
53 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
54 vlc_value_t oldval, vlc_value_t newval,
58 snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
59 vlc_value_t oldval, vlc_value_t newval, void *p_data );
61 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
67 #define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
68 static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type_t type)
70 libvlc_event_manager_register_event_type(mp->p_event_manager, type);
73 static inline void lock(libvlc_media_player_t *mp)
75 vlc_mutex_lock(&mp->object_lock);
78 static inline void unlock(libvlc_media_player_t *mp)
80 vlc_mutex_unlock(&mp->object_lock);
84 * Release the associated input thread.
86 * Object lock is NOT held.
88 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
90 input_thread_t * p_input_thread;
92 if( !p_mi || !p_mi->p_input_thread )
95 p_input_thread = p_mi->p_input_thread;
97 var_DelCallback( p_input_thread, "can-seek",
98 input_seekable_changed, p_mi );
99 var_DelCallback( p_input_thread, "can-pause",
100 input_pausable_changed, p_mi );
101 var_DelCallback( p_input_thread, "intf-event",
102 input_event_changed, p_mi );
104 /* We owned this one */
105 input_Stop( p_input_thread, b_input_abort );
107 vlc_thread_join( p_input_thread );
109 assert( p_mi->p_input_resource == NULL );
110 assert( p_input_thread->b_dead );
111 /* Store the input resource for future use. */
112 p_mi->p_input_resource = input_DetachResource( p_input_thread );
114 vlc_object_release( p_input_thread );
116 p_mi->p_input_thread = NULL;
120 * Retrieve the input thread. Be sure to release the object
121 * once you are done with it. (libvlc Internal)
123 * Function will lock the object.
125 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
126 libvlc_exception_t *p_e )
128 input_thread_t *p_input_thread;
130 if( !p_mi ) RAISENULL( "Media Instance is NULL" );
134 if( !p_mi->p_input_thread )
137 RAISENULL( "Input is NULL" );
140 p_input_thread = p_mi->p_input_thread;
141 vlc_object_hold( p_input_thread );
145 return p_input_thread;
149 * Set the internal state of the media_player. (media player Internal)
151 * Function will lock the media_player.
153 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
159 libvlc_media_t *media = p_mi->p_md;
161 libvlc_media_retain(media);
167 // Also set the state of the corresponding media
168 // This is strictly for convenience.
169 libvlc_media_set_state(media, state);
171 libvlc_media_release(media);
176 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
177 vlc_value_t oldval, vlc_value_t newval,
183 libvlc_media_player_t * p_mi = p_userdata;
184 libvlc_event_t event;
186 event.type = libvlc_MediaPlayerSeekableChanged;
187 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
189 libvlc_event_send( p_mi->p_event_manager, &event );
194 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
195 vlc_value_t oldval, vlc_value_t newval,
201 libvlc_media_player_t * p_mi = p_userdata;
202 libvlc_event_t event;
204 event.type = libvlc_MediaPlayerPausableChanged;
205 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
207 libvlc_event_send( p_mi->p_event_manager, &event );
212 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
213 vlc_value_t oldval, vlc_value_t newval,
217 input_thread_t * p_input = (input_thread_t *)p_this;
218 libvlc_media_player_t * p_mi = p_userdata;
219 libvlc_event_t event;
221 assert( !strcmp( psz_cmd, "intf-event" ) );
223 if( newval.i_int == INPUT_EVENT_STATE )
225 libvlc_state_t libvlc_state;
227 switch ( var_GetInteger( p_input, "state" ) )
230 libvlc_state = libvlc_NothingSpecial;
231 event.type = libvlc_MediaPlayerNothingSpecial;
234 libvlc_state = libvlc_Opening;
235 event.type = libvlc_MediaPlayerOpening;
238 libvlc_state = libvlc_Playing;
239 event.type = libvlc_MediaPlayerPlaying;
242 libvlc_state = libvlc_Paused;
243 event.type = libvlc_MediaPlayerPaused;
246 libvlc_state = libvlc_Ended;
247 event.type = libvlc_MediaPlayerEndReached;
250 libvlc_state = libvlc_Error;
251 event.type = libvlc_MediaPlayerEncounteredError;
258 set_state( p_mi, libvlc_state, false );
259 libvlc_event_send( p_mi->p_event_manager, &event );
261 else if( newval.i_int == INPUT_EVENT_ABORT )
263 libvlc_state_t libvlc_state = libvlc_Stopped;
264 event.type = libvlc_MediaPlayerStopped;
266 set_state( p_mi, libvlc_state, false );
267 libvlc_event_send( p_mi->p_event_manager, &event );
269 else if( newval.i_int == INPUT_EVENT_POSITION )
271 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
272 return VLC_SUCCESS; /* Don't send the position while stopped */
275 event.type = libvlc_MediaPlayerPositionChanged;
276 event.u.media_player_position_changed.new_position =
277 var_GetFloat( p_input, "position" );
278 libvlc_event_send( p_mi->p_event_manager, &event );
281 event.type = libvlc_MediaPlayerTimeChanged;
282 event.u.media_player_time_changed.new_time =
283 from_mtime(var_GetTime( p_input, "time" ));
284 libvlc_event_send( p_mi->p_event_manager, &event );
286 else if( newval.i_int == INPUT_EVENT_LENGTH )
288 event.type = libvlc_MediaPlayerLengthChanged;
289 event.u.media_player_length_changed.new_length =
290 from_mtime(var_GetTime( p_input, "length" ));
291 libvlc_event_send( p_mi->p_event_manager, &event );
298 /**************************************************************************
299 * Snapshot Taken Event.
301 * FIXME: This snapshot API interface makes no sense in media_player.
302 *************************************************************************/
303 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
304 vlc_value_t oldval, vlc_value_t newval, void *p_data )
306 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
308 libvlc_media_player_t *mp = p_data;
309 libvlc_event_t event;
310 event.type = libvlc_MediaPlayerSnapshotTaken;
311 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
312 libvlc_event_send(mp->p_event_manager, &event);
318 static void libvlc_media_player_destroy( libvlc_media_player_t * );
321 /**************************************************************************
322 * Create a Media Instance object.
325 * - All items created by _new start with a refcount set to 1.
326 * - Accessor _release decrease the refcount by 1, if after that
327 * operation the refcount is 0, the object is destroyed.
328 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
330 * Object locking strategy:
331 * - No lock held while in constructor.
332 * - When accessing any member variable this lock is held. (XXX who locks?)
333 * - When attempting to destroy the object the lock is also held.
334 **************************************************************************/
335 libvlc_media_player_t *
336 libvlc_media_player_new( libvlc_instance_t *instance, libvlc_exception_t *e )
338 libvlc_media_player_t * mp;
342 mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
343 if (unlikely(mp == NULL))
345 libvlc_printerr("Not enough memory");
348 vlc_object_attach (mp, mp->p_libvlc);
351 var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
353 var_Create (mp, "drawable-hwnd", VLC_VAR_ADDRESS);
356 var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
357 var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
360 /* Drawable input methods */
361 var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
362 var_SetBool (mp, "keyboard-events", true);
363 var_Create (mp, "mouse-events", VLC_VAR_BOOL);
366 mp->state = libvlc_NothingSpecial;
367 mp->p_libvlc_instance = instance;
368 mp->p_input_thread = NULL;
369 mp->p_input_resource = NULL;
371 mp->p_event_manager = libvlc_event_manager_new(mp, instance);
372 if (unlikely(mp->p_event_manager == NULL))
374 vlc_object_release(mp);
377 vlc_mutex_init(&mp->object_lock);
379 register_event(mp, NothingSpecial);
380 register_event(mp, Opening);
381 register_event(mp, Buffering);
382 register_event(mp, Playing);
383 register_event(mp, Paused);
384 register_event(mp, Stopped);
385 register_event(mp, Forward);
386 register_event(mp, Backward);
387 register_event(mp, EndReached);
388 register_event(mp, EncounteredError);
389 register_event(mp, SeekableChanged);
391 register_event(mp, PositionChanged);
392 register_event(mp, TimeChanged);
393 register_event(mp, LengthChanged);
394 register_event(mp, TitleChanged);
395 register_event(mp, PausableChanged);
397 /* Snapshot initialization */
398 register_event(mp, SnapshotTaken);
400 register_event(mp, MediaChanged);
402 /* Attach a var callback to the global object to provide the glue between
403 * vout_thread that generates the event and media_player that re-emits it
404 * with its own event manager
406 * FIXME: It's unclear why we want to put this in public API, and why we
407 * want to expose it in such a limiting and ugly way.
409 var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
414 /**************************************************************************
415 * Create a Media Instance object with a media descriptor.
416 **************************************************************************/
417 libvlc_media_player_t *
418 libvlc_media_player_new_from_media(
419 libvlc_media_t * p_md,
420 libvlc_exception_t *p_e )
422 libvlc_media_player_t * p_mi;
424 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
428 libvlc_media_retain( p_md );
434 /**************************************************************************
435 * Destroy a Media Instance object (libvlc internal)
437 * Warning: No lock held here, but hey, this is internal. Caller must lock.
438 **************************************************************************/
439 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
443 /* Detach Callback from the main libvlc object */
444 var_DelCallback( p_mi->p_libvlc,
445 "snapshot-file", snapshot_was_taken, p_mi );
447 /* If the input thread hasn't been already deleted it means
448 * that the owners didn't stop the thread before releasing it. */
449 assert(!p_mi->p_input_thread);
451 /* Fallback for those who don't use NDEBUG */
452 if (p_mi->p_input_thread)
453 release_input_thread(p_mi, true);
455 if( p_mi->p_input_resource )
457 input_resource_Delete( p_mi->p_input_resource );
458 p_mi->p_input_resource = NULL;
461 libvlc_event_manager_release( p_mi->p_event_manager );
462 libvlc_media_release( p_mi->p_md );
463 vlc_mutex_destroy( &p_mi->object_lock );
464 vlc_object_release( p_mi );
467 /**************************************************************************
468 * Release a Media Instance object.
470 * Function does the locking.
471 **************************************************************************/
472 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
478 destroy = !--p_mi->i_refcount;
482 libvlc_media_player_destroy( p_mi );
485 /**************************************************************************
486 * Retain a Media Instance object.
488 * Caller must hold the lock.
489 **************************************************************************/
490 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
499 /**************************************************************************
500 * Set the Media descriptor associated with the instance.
502 * Enter without lock -- function will lock the object.
503 **************************************************************************/
504 void libvlc_media_player_set_media(
505 libvlc_media_player_t *p_mi,
506 libvlc_media_t *p_md )
510 /* FIXME I am not sure if it is a user request or on die(eof/error)
512 release_input_thread( p_mi,
513 p_mi->p_input_thread &&
514 !p_mi->p_input_thread->b_eof &&
515 !p_mi->p_input_thread->b_error );
517 set_state( p_mi, libvlc_NothingSpecial, true );
519 libvlc_media_release( p_mi->p_md );
525 return; /* It is ok to pass a NULL md */
528 libvlc_media_retain( p_md );
531 /* The policy here is to ignore that we were created using a different
532 * libvlc_instance, because we don't really care */
533 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
537 /* Send an event for the newly available media */
538 libvlc_event_t event;
539 event.type = libvlc_MediaPlayerMediaChanged;
540 event.u.media_player_media_changed.new_media = p_md;
541 libvlc_event_send( p_mi->p_event_manager, &event );
545 /**************************************************************************
546 * Get the Media descriptor associated with the instance.
547 **************************************************************************/
549 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
556 libvlc_media_retain( p_mi->p_md );
561 /**************************************************************************
562 * Get the event Manager.
563 **************************************************************************/
564 libvlc_event_manager_t *
565 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
567 return p_mi->p_event_manager;
570 /**************************************************************************
571 * Tell media player to start playing.
572 **************************************************************************/
573 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
574 libvlc_exception_t *p_e )
576 input_thread_t * p_input_thread;
578 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
580 /* A thread already exists, send it a play message */
581 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
582 vlc_object_release( p_input_thread );
586 /* Ignore previous exception */
587 libvlc_exception_clear( p_e );
594 libvlc_exception_raise( p_e );
595 libvlc_printerr( "No associated media descriptor" );
599 p_mi->p_input_thread = input_Create( p_mi,
600 p_mi->p_md->p_input_item, NULL,
601 p_mi->p_input_resource );
602 if( !p_mi->p_input_thread )
608 p_mi->p_input_resource = NULL;
609 p_input_thread = p_mi->p_input_thread;
611 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
612 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
613 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
615 if( input_Start( p_input_thread ) )
617 vlc_object_release( p_input_thread );
618 p_mi->p_input_thread = NULL;
624 /**************************************************************************
626 **************************************************************************/
627 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
628 libvlc_exception_t *p_e )
630 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
631 if( !p_input_thread )
634 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
635 if( state == libvlc_Playing || state == libvlc_Buffering )
637 if( libvlc_media_player_can_pause( p_mi, p_e ) )
638 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
640 libvlc_media_player_stop( p_mi );
643 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
645 vlc_object_release( p_input_thread );
648 /**************************************************************************
649 * Tells whether the media player is currently playing.
651 * Enter with lock held.
652 **************************************************************************/
653 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
655 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
656 return (libvlc_Playing == state) || (libvlc_Buffering == state);
659 /**************************************************************************
661 **************************************************************************/
662 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
664 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
667 release_input_thread( p_mi, true ); /* This will stop the input thread */
670 /* Force to go to stopped state, in case we were in Ended, or Error
672 if( state != libvlc_Stopped )
674 set_state( p_mi, libvlc_Stopped, false );
676 /* Construct and send the event */
677 libvlc_event_t event;
678 event.type = libvlc_MediaPlayerStopped;
679 libvlc_event_send( p_mi->p_event_manager, &event );
683 /**************************************************************************
685 **************************************************************************/
686 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
689 assert (p_mi != NULL);
691 var_SetAddress (p_mi, "drawable-nsobject", drawable);
693 (void) p_mi; (void)drawable;
697 /**************************************************************************
699 **************************************************************************/
700 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
702 assert (p_mi != NULL);
704 return var_GetAddress (p_mi, "drawable-nsobject");
710 /**************************************************************************
712 **************************************************************************/
713 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
717 var_SetInteger (p_mi, "drawable-agl", drawable);
719 (void) p_mi; (void)drawable;
723 /**************************************************************************
725 **************************************************************************/
726 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
728 assert (p_mi != NULL);
730 return var_GetInteger (p_mi, "drawable-agl");
736 /**************************************************************************
738 **************************************************************************/
739 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
742 assert (p_mi != NULL);
743 var_SetInteger (p_mi, "drawable-xid", drawable);
746 /**************************************************************************
748 **************************************************************************/
749 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
751 return var_GetInteger (p_mi, "drawable-xid");
754 /**************************************************************************
756 **************************************************************************/
757 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
760 assert (p_mi != NULL);
762 var_SetAddress (p_mi, "drawable-hwnd", drawable);
764 (void) p_mi; (void) drawable;
768 /**************************************************************************
770 **************************************************************************/
771 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
773 assert (p_mi != NULL);
775 return var_GetAddress (p_mi, "drawable-hwnd");
781 /**************************************************************************
782 * Getters for stream information
783 **************************************************************************/
784 libvlc_time_t libvlc_media_player_get_length(
785 libvlc_media_player_t *p_mi,
786 libvlc_exception_t *p_e )
788 input_thread_t *p_input_thread;
789 libvlc_time_t i_time;
791 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
792 if( !p_input_thread )
795 i_time = from_mtime(var_GetTime( p_input_thread, "length" ));
796 vlc_object_release( p_input_thread );
801 libvlc_time_t libvlc_media_player_get_time(
802 libvlc_media_player_t *p_mi,
803 libvlc_exception_t *p_e )
805 input_thread_t *p_input_thread;
806 libvlc_time_t i_time;
808 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
809 if( !p_input_thread )
812 i_time = from_mtime(var_GetTime( p_input_thread , "time" ));
813 vlc_object_release( p_input_thread );
817 void libvlc_media_player_set_time(
818 libvlc_media_player_t *p_mi,
819 libvlc_time_t i_time,
820 libvlc_exception_t *p_e )
822 input_thread_t *p_input_thread;
824 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
825 if( !p_input_thread )
828 var_SetTime( p_input_thread, "time", to_mtime(i_time) );
829 vlc_object_release( p_input_thread );
832 void libvlc_media_player_set_position(
833 libvlc_media_player_t *p_mi,
835 libvlc_exception_t *p_e )
837 input_thread_t *p_input_thread;
839 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
840 if( !p_input_thread )
843 var_SetFloat( p_input_thread, "position", position );
844 vlc_object_release( p_input_thread );
847 float libvlc_media_player_get_position(
848 libvlc_media_player_t *p_mi,
849 libvlc_exception_t *p_e )
851 input_thread_t *p_input_thread;
854 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
855 if( !p_input_thread )
858 f_position = var_GetFloat( p_input_thread, "position" );
859 vlc_object_release( p_input_thread );
864 void libvlc_media_player_set_chapter(
865 libvlc_media_player_t *p_mi,
867 libvlc_exception_t *p_e )
869 input_thread_t *p_input_thread;
871 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
872 if( !p_input_thread )
875 var_SetInteger( p_input_thread, "chapter", chapter );
876 vlc_object_release( p_input_thread );
879 int libvlc_media_player_get_chapter(
880 libvlc_media_player_t *p_mi,
881 libvlc_exception_t *p_e )
883 input_thread_t *p_input_thread;
886 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
887 if( !p_input_thread )
890 i_chapter = var_GetInteger( p_input_thread, "chapter" );
891 vlc_object_release( p_input_thread );
896 int libvlc_media_player_get_chapter_count(
897 libvlc_media_player_t *p_mi,
898 libvlc_exception_t *p_e )
900 input_thread_t *p_input_thread;
903 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
904 if( !p_input_thread )
907 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
908 vlc_object_release( p_input_thread );
913 int libvlc_media_player_get_chapter_count_for_title(
914 libvlc_media_player_t *p_mi,
916 libvlc_exception_t *p_e )
918 input_thread_t *p_input_thread;
921 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
922 if( !p_input_thread )
926 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
928 vlc_object_release( p_input_thread );
931 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
932 vlc_object_release( p_input_thread );
938 void libvlc_media_player_set_title(
939 libvlc_media_player_t *p_mi,
941 libvlc_exception_t *p_e )
943 input_thread_t *p_input_thread;
945 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
946 if( !p_input_thread )
949 var_SetInteger( p_input_thread, "title", i_title );
950 vlc_object_release( p_input_thread );
953 libvlc_event_t event;
954 event.type = libvlc_MediaPlayerTitleChanged;
955 event.u.media_player_title_changed.new_title = i_title;
956 libvlc_event_send( p_mi->p_event_manager, &event );
959 int libvlc_media_player_get_title(
960 libvlc_media_player_t *p_mi,
961 libvlc_exception_t *p_e )
963 input_thread_t *p_input_thread;
966 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
967 if( !p_input_thread )
970 i_title = var_GetInteger( p_input_thread, "title" );
971 vlc_object_release( p_input_thread );
976 int libvlc_media_player_get_title_count(
977 libvlc_media_player_t *p_mi,
978 libvlc_exception_t *p_e )
980 input_thread_t *p_input_thread;
983 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
984 if( !p_input_thread )
987 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
988 vlc_object_release( p_input_thread );
993 void libvlc_media_player_next_chapter(
994 libvlc_media_player_t *p_mi,
995 libvlc_exception_t *p_e )
997 input_thread_t *p_input_thread;
999 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1000 if( !p_input_thread )
1003 int i_type = var_Type( p_input_thread, "next-chapter" );
1004 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1005 "next-chapter":"next-title", true );
1007 vlc_object_release( p_input_thread );
1010 void libvlc_media_player_previous_chapter(
1011 libvlc_media_player_t *p_mi,
1012 libvlc_exception_t *p_e )
1014 input_thread_t *p_input_thread;
1016 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1017 if( !p_input_thread )
1020 int i_type = var_Type( p_input_thread, "next-chapter" );
1021 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1022 "prev-chapter":"prev-title", true );
1024 vlc_object_release( p_input_thread );
1027 float libvlc_media_player_get_fps(
1028 libvlc_media_player_t *p_mi,
1029 libvlc_exception_t *p_e)
1031 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1034 if( p_input_thread )
1036 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1038 vlc_object_release( p_input_thread );
1043 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1044 libvlc_exception_t *p_e)
1047 input_thread_t *p_input_thread =
1048 libvlc_get_input_thread ( p_mi, p_e);
1049 if ( !p_input_thread )
1052 b_will_play = !p_input_thread->b_die && !p_input_thread->b_dead;
1053 vlc_object_release( p_input_thread );
1058 void libvlc_media_player_set_rate(
1059 libvlc_media_player_t *p_mi,
1061 libvlc_exception_t *p_e )
1063 input_thread_t *p_input_thread;
1066 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1067 if( !p_input_thread )
1070 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1071 if( (rate < 0.0) && !b_can_rewind )
1073 vlc_object_release( p_input_thread );
1074 libvlc_exception_raise( p_e );
1075 libvlc_printerr( "Invalid playback rate" );
1079 var_SetFloat( p_input_thread, "rate", rate );
1080 vlc_object_release( p_input_thread );
1083 float libvlc_media_player_get_rate(
1084 libvlc_media_player_t *p_mi,
1085 libvlc_exception_t *p_e )
1087 input_thread_t *p_input_thread;
1091 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1092 if( !p_input_thread )
1093 return 0.0; /* rate < 0 indicates rewind */
1095 f_rate = var_GetFloat( p_input_thread, "rate" );
1096 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1097 /* FIXME: why are negative values forbidden ?? (rewinding) */
1098 if( f_rate < 0 && !b_can_rewind )
1100 vlc_object_release( p_input_thread );
1103 vlc_object_release( p_input_thread );
1108 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1111 libvlc_state_t state = p_mi->state;
1116 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1117 libvlc_exception_t *p_e )
1119 input_thread_t *p_input_thread;
1122 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1123 if ( !p_input_thread )
1125 /* We do return the right value, no need to throw an exception */
1126 clear_if_needed(p_e);
1129 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1130 vlc_object_release( p_input_thread );
1135 /* internal function, used by audio, video */
1136 libvlc_track_description_t *
1137 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1138 const char *psz_variable,
1139 libvlc_exception_t *p_e )
1141 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1142 libvlc_track_description_t *p_track_description = NULL,
1143 *p_actual, *p_previous;
1148 vlc_value_t val_list, text_list;
1149 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1152 if( val_list.p_list->i_count <= 0 )
1155 p_track_description = ( libvlc_track_description_t * )
1156 malloc( sizeof( libvlc_track_description_t ) );
1157 if ( !p_track_description )
1159 libvlc_exception_raise( p_e );
1160 libvlc_printerr( "Not enough memory" );
1163 p_actual = p_track_description;
1165 for( int i = 0; i < val_list.p_list->i_count; i++ )
1169 p_actual = ( libvlc_track_description_t * )
1170 malloc( sizeof( libvlc_track_description_t ) );
1173 libvlc_track_description_release( p_track_description );
1174 libvlc_exception_raise( p_e );
1175 libvlc_printerr( "Not enough memory" );
1179 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1180 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1181 p_actual->p_next = NULL;
1183 p_previous->p_next = p_actual;
1184 p_previous = p_actual;
1189 var_FreeList( &val_list, &text_list );
1190 vlc_object_release( p_input );
1192 return p_track_description;
1195 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1197 libvlc_track_description_t *p_actual, *p_before;
1202 free( p_actual->psz_name );
1203 p_before = p_actual;
1204 p_actual = p_before->p_next;
1209 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1210 libvlc_exception_t *p_e )
1212 input_thread_t *p_input_thread;
1215 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1216 if ( !p_input_thread )
1218 /* We do return the right value, no need to throw an exception */
1219 clear_if_needed(p_e);
1222 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1223 vlc_object_release( p_input_thread );
1228 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
1230 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1231 if( p_input_thread != NULL )
1233 var_TriggerCallback( p_input_thread, "frame-next" );
1234 vlc_object_release( p_input_thread );
1238 libvlc_exception_raise( p_e );
1239 libvlc_printerr( "No active input" );