1 /*****************************************************************************
2 * media_player.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005 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 *****************************************************************************/
24 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27 #include <vlc_demux.h>
28 #include <vlc_input.h>
33 input_state_changed( const vlc_event_t * event, void * p_userdata );
36 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
37 vlc_value_t oldval, vlc_value_t newval,
40 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
41 vlc_value_t oldval, vlc_value_t newval,
44 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
45 vlc_value_t oldval, vlc_value_t newval,
48 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
49 vlc_value_t oldval, vlc_value_t newval, void *p_data );
51 static const libvlc_state_t vlc_to_libvlc_state_array[] =
53 [INIT_S] = libvlc_NothingSpecial,
54 [OPENING_S] = libvlc_Opening,
55 [PLAYING_S] = libvlc_Playing,
56 [PAUSE_S] = libvlc_Paused,
57 [END_S] = libvlc_Ended,
58 [ERROR_S] = libvlc_Error,
61 static inline libvlc_state_t vlc_to_libvlc_state( int vlc_state )
63 if( vlc_state < 0 || vlc_state > 6 )
66 return vlc_to_libvlc_state_array[vlc_state];
70 * Release the associated input thread
72 * Object lock is NOT held.
74 static void release_input_thread( libvlc_media_player_t *p_mi )
76 input_thread_t * p_input_thread;
78 if( !p_mi || !p_mi->p_input_thread )
81 p_input_thread = p_mi->p_input_thread;
83 /* No one is tracking this input_thread appart us. Destroy it */
84 if( p_mi->b_own_its_input_thread )
86 vlc_event_manager_t * p_em = input_GetEventManager( p_input_thread );
87 vlc_event_detach( p_em, vlc_InputStateChanged, input_state_changed, p_mi );
88 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
89 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
90 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
92 /* We owned this one */
93 input_StopThread( p_input_thread );
94 vlc_thread_join( p_input_thread );
96 var_Destroy( p_input_thread, "drawable" );
99 vlc_object_release( p_input_thread );
101 p_mi->p_input_thread = NULL;
105 * Retrieve the input thread. Be sure to release the object
106 * once you are done with it. (libvlc Internal)
108 * Object lock is held.
110 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
111 libvlc_exception_t *p_e )
113 input_thread_t *p_input_thread;
115 if( !p_mi ) RAISENULL( "Media Instance is NULL" );
117 vlc_mutex_lock( &p_mi->object_lock );
119 if( !p_mi->p_input_thread )
121 vlc_mutex_unlock( &p_mi->object_lock );
122 RAISENULL( "Input is NULL" );
125 p_input_thread = p_mi->p_input_thread;
126 vlc_object_hold( p_input_thread );
128 vlc_mutex_unlock( &p_mi->object_lock );
130 return p_input_thread;
134 * input_state_changed (Private) (vlc_InputStateChanged callback)
137 input_state_changed( const vlc_event_t * event, void * p_userdata )
139 libvlc_media_player_t * p_mi = p_userdata;
140 libvlc_event_t forwarded_event;
141 libvlc_event_type_t type = event->u.input_state_changed.new_state;
146 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
147 forwarded_event.type = libvlc_MediaPlayerNothingSpecial;
150 libvlc_media_set_state( p_mi->p_md, libvlc_Opening, NULL);
151 forwarded_event.type = libvlc_MediaPlayerOpening;
154 libvlc_media_set_state( p_mi->p_md, libvlc_Playing, NULL);
155 forwarded_event.type = libvlc_MediaPlayerPlaying;
158 libvlc_media_set_state( p_mi->p_md, libvlc_Paused, NULL);
159 forwarded_event.type = libvlc_MediaPlayerPaused;
162 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, NULL);
163 forwarded_event.type = libvlc_MediaPlayerEndReached;
166 libvlc_media_set_state( p_mi->p_md, libvlc_Error, NULL);
167 forwarded_event.type = libvlc_MediaPlayerEncounteredError;
174 libvlc_event_send( p_mi->p_event_manager, &forwarded_event );
179 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
180 vlc_value_t oldval, vlc_value_t newval,
186 libvlc_media_player_t * p_mi = p_userdata;
187 libvlc_event_t event;
189 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
190 event.type = libvlc_MediaPlayerSeekableChanged;
191 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
193 libvlc_event_send( p_mi->p_event_manager, &event );
198 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
199 vlc_value_t oldval, vlc_value_t newval,
205 libvlc_media_player_t * p_mi = p_userdata;
206 libvlc_event_t event;
208 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
209 event.type = libvlc_MediaPlayerPausableChanged;
210 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
212 libvlc_event_send( p_mi->p_event_manager, &event );
217 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
218 vlc_value_t oldval, vlc_value_t newval,
222 input_thread_t * p_input = (input_thread_t *)p_this;
223 libvlc_media_player_t * p_mi = p_userdata;
224 libvlc_event_t event;
226 if( newval.i_int != INPUT_EVENT_TIMES )
229 assert( !strcmp(psz_cmd, "intf-event" ) );
231 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
232 return VLC_SUCCESS; /* Don't send the position while stopped */
235 event.type = libvlc_MediaPlayerPositionChanged;
236 event.u.media_player_position_changed.new_position = var_GetFloat( p_input, "position" );;
237 libvlc_event_send( p_mi->p_event_manager, &event );
240 event.type = libvlc_MediaPlayerTimeChanged;
241 event.u.media_player_time_changed.new_time = var_GetTime( p_input, "time" );
242 libvlc_event_send( p_mi->p_event_manager, &event );
249 /**************************************************************************
250 * Create a Media Instance object
251 **************************************************************************/
252 libvlc_media_player_t *
253 libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
254 libvlc_exception_t * p_e )
256 libvlc_media_player_t * p_mi;
258 if( !p_libvlc_instance )
260 libvlc_exception_raise( p_e, "invalid libvlc instance" );
264 p_mi = malloc( sizeof(libvlc_media_player_t) );
267 libvlc_exception_raise( p_e, "Not enough memory" );
272 p_mi->p_libvlc_instance = p_libvlc_instance;
273 p_mi->p_input_thread = NULL;
274 /* refcount strategy:
275 * - All items created by _new start with a refcount set to 1
276 * - Accessor _release decrease the refcount by 1, if after that
277 * operation the refcount is 0, the object is destroyed.
278 * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
279 p_mi->i_refcount = 1;
280 p_mi->b_own_its_input_thread = true;
281 /* object_lock strategy:
282 * - No lock held in constructor
283 * - Lock when accessing all variable this lock is held
284 * - Lock when attempting to destroy the object the lock is also held */
285 vlc_mutex_init( &p_mi->object_lock );
286 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
287 p_libvlc_instance, p_e );
288 if( libvlc_exception_raised( p_e ) )
294 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
295 libvlc_MediaPlayerNothingSpecial, p_e );
296 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
297 libvlc_MediaPlayerOpening, p_e );
298 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
299 libvlc_MediaPlayerBuffering, p_e );
300 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
301 libvlc_MediaPlayerPlaying, p_e );
302 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
303 libvlc_MediaPlayerPaused, p_e );
304 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
305 libvlc_MediaPlayerStopped, p_e );
306 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
307 libvlc_MediaPlayerForward, p_e );
308 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
309 libvlc_MediaPlayerBackward, p_e );
310 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
311 libvlc_MediaPlayerEndReached, p_e );
312 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
313 libvlc_MediaPlayerEncounteredError, p_e );
315 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
316 libvlc_MediaPlayerPositionChanged, p_e );
317 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
318 libvlc_MediaPlayerTimeChanged, p_e );
319 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
320 libvlc_MediaPlayerTitleChanged, p_e );
321 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
322 libvlc_MediaPlayerSeekableChanged, p_e );
323 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
324 libvlc_MediaPlayerPausableChanged, p_e );
326 /* Snapshot initialization */
327 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
328 libvlc_MediaPlayerSnapshotTaken, p_e );
329 /* Attach a var callback to the global object to provide the glue between
330 vout_thread that generates the event and media_player that re-emits it
331 with its own event manager
333 var_Create( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
334 var_AddCallback( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", SnapshotTakenCallback, p_mi );
339 /**************************************************************************
340 * Create a Media Instance object with a media descriptor
341 **************************************************************************/
342 libvlc_media_player_t *
343 libvlc_media_player_new_from_media(
344 libvlc_media_t * p_md,
345 libvlc_exception_t *p_e )
347 libvlc_media_player_t * p_mi;
348 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
353 libvlc_media_retain( p_md );
359 /**************************************************************************
360 * Create a new media instance object from an input_thread (Libvlc Internal)
361 **************************************************************************/
362 libvlc_media_player_t * libvlc_media_player_new_from_input_thread(
363 struct libvlc_instance_t *p_libvlc_instance,
364 input_thread_t *p_input,
365 libvlc_exception_t *p_e )
367 libvlc_media_player_t * p_mi;
371 libvlc_exception_raise( p_e, "invalid input thread" );
375 p_mi = libvlc_media_player_new( p_libvlc_instance, p_e );
380 p_mi->p_md = libvlc_media_new_from_input_item(
382 input_GetItem( p_input ), p_e );
386 libvlc_media_player_destroy( p_mi );
390 /* will be released in media_player_release() */
391 vlc_object_hold( p_input );
393 p_mi->p_input_thread = p_input;
394 p_mi->b_own_its_input_thread = false;
399 /**************************************************************************
400 * Destroy a Media Instance object (libvlc internal)
402 * Warning: No lock held here, but hey, this is internal.
403 **************************************************************************/
404 void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
406 input_thread_t *p_input_thread;
407 libvlc_exception_t p_e;
409 libvlc_exception_init( &p_e );
414 /* Detach Callback from the main libvlc object */
415 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", SnapshotTakenCallback, p_mi );
417 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
419 if( libvlc_exception_raised( &p_e ) )
421 libvlc_event_manager_release( p_mi->p_event_manager );
422 libvlc_exception_clear( &p_e );
424 return; /* no need to worry about no input thread */
426 vlc_mutex_destroy( &p_mi->object_lock );
428 vlc_object_release( p_input_thread );
430 libvlc_media_release( p_mi->p_md );
435 /**************************************************************************
436 * Release a Media Instance object
437 **************************************************************************/
438 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
443 vlc_mutex_lock( &p_mi->object_lock );
447 if( p_mi->i_refcount > 0 )
449 vlc_mutex_unlock( &p_mi->object_lock );
452 vlc_mutex_unlock( &p_mi->object_lock );
453 vlc_mutex_destroy( &p_mi->object_lock );
455 release_input_thread( p_mi );
457 libvlc_event_manager_release( p_mi->p_event_manager );
459 libvlc_media_release( p_mi->p_md );
464 /**************************************************************************
465 * Retain a Media Instance object
466 **************************************************************************/
467 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
475 /**************************************************************************
476 * Set the Media descriptor associated with the instance
477 **************************************************************************/
478 void libvlc_media_player_set_media(
479 libvlc_media_player_t *p_mi,
480 libvlc_media_t *p_md,
481 libvlc_exception_t *p_e )
488 vlc_mutex_lock( &p_mi->object_lock );
490 release_input_thread( p_mi );
493 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
495 libvlc_media_release( p_mi->p_md );
500 vlc_mutex_unlock( &p_mi->object_lock );
501 return; /* It is ok to pass a NULL md */
504 libvlc_media_retain( p_md );
507 /* The policy here is to ignore that we were created using a different
508 * libvlc_instance, because we don't really care */
509 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
511 vlc_mutex_unlock( &p_mi->object_lock );
514 /**************************************************************************
515 * Get the Media descriptor associated with the instance
516 **************************************************************************/
518 libvlc_media_player_get_media(
519 libvlc_media_player_t *p_mi,
520 libvlc_exception_t *p_e )
527 libvlc_media_retain( p_mi->p_md );
531 /**************************************************************************
532 * Get the event Manager
533 **************************************************************************/
534 libvlc_event_manager_t *
535 libvlc_media_player_event_manager(
536 libvlc_media_player_t *p_mi,
537 libvlc_exception_t *p_e )
541 return p_mi->p_event_manager;
544 /**************************************************************************
545 * Trigger a snapshot Taken Event
546 *************************************************************************/
547 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
548 vlc_value_t oldval, vlc_value_t newval, void *p_data )
550 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
553 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
554 libvlc_event_t event ;
555 event.type = libvlc_MediaPlayerSnapshotTaken ;
556 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
557 /* Snapshot psz data is a vlc_variable owned by libvlc object .
558 Its memmory management is taken care by the obj*/
559 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%x (%s)",
560 event.u.media_player_snapshot_taken.psz_filename ,event.u.media_player_snapshot_taken.psz_filename );
561 libvlc_event_send( p_mi->p_event_manager, &event );
566 /**************************************************************************
568 **************************************************************************/
569 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
570 libvlc_exception_t *p_e )
572 input_thread_t * p_input_thread;
574 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
576 /* A thread already exists, send it a play message */
577 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
578 vlc_object_release( p_input_thread );
582 /* Ignore previous exception */
583 libvlc_exception_clear( p_e );
585 vlc_mutex_lock( &p_mi->object_lock );
589 libvlc_exception_raise( p_e, "no associated media descriptor" );
590 vlc_mutex_unlock( &p_mi->object_lock );
594 p_mi->p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
595 p_mi->p_md->p_input_item );
598 if( !p_mi->p_input_thread )
600 vlc_mutex_unlock( &p_mi->object_lock );
604 p_input_thread = p_mi->p_input_thread;
609 val.i_int = p_mi->drawable;
610 var_Create( p_input_thread, "drawable", VLC_VAR_DOINHERIT );
611 var_Set( p_input_thread, "drawable", val );
614 vlc_event_manager_t * p_em = input_GetEventManager( p_input_thread );
615 vlc_event_attach( p_em, vlc_InputStateChanged, input_state_changed, p_mi );
617 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
618 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
619 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
621 vlc_mutex_unlock( &p_mi->object_lock );
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 );
632 if( !p_input_thread )
635 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
637 if( state == libvlc_Playing )
639 if( libvlc_media_player_can_pause( p_mi, p_e ) )
640 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
642 libvlc_media_player_stop( p_mi, p_e );
645 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
647 vlc_object_release( p_input_thread );
650 /**************************************************************************
652 **************************************************************************/
653 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
654 libvlc_exception_t *p_e )
656 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
658 if( state == libvlc_Playing || state == libvlc_Paused )
660 /* Send a stop notification event only of we are in playing or paused states */
661 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
663 /* Construct and send the event */
664 libvlc_event_t event;
665 event.type = libvlc_MediaPlayerEndReached;
666 libvlc_event_send( p_mi->p_event_manager, &event );
669 if( p_mi->b_own_its_input_thread )
671 vlc_mutex_lock( &p_mi->object_lock );
672 release_input_thread( p_mi ); /* This will stop the input thread */
673 vlc_mutex_unlock( &p_mi->object_lock );
677 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
679 if( !p_input_thread )
682 input_StopThread( p_input_thread );
683 vlc_object_release( p_input_thread );
687 /**************************************************************************
689 **************************************************************************/
690 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
691 libvlc_drawable_t drawable,
692 libvlc_exception_t *p_e )
694 input_thread_t *p_input_thread;
695 vout_thread_t *p_vout = NULL;
697 p_mi->drawable = drawable;
699 /* Allow on the fly drawable changing. This is tricky has this may
700 * not be supported by every vout. We though can't disable it
701 * because of some creepy drawable type that are not flexible enough
702 * (Win32 HWND for instance) */
703 p_input_thread = libvlc_get_input_thread( p_mi, p_e );
704 if( !p_input_thread ) {
705 /* No input, nothing more to do, we are fine */
706 libvlc_exception_clear( p_e );
710 p_vout = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
713 vout_Control( p_vout , VOUT_REPARENT, drawable);
714 vlc_object_release( p_vout );
716 vlc_object_release( p_input_thread );
719 /**************************************************************************
721 **************************************************************************/
723 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
726 return p_mi->drawable;
729 /**************************************************************************
730 * Getters for stream information
731 **************************************************************************/
732 libvlc_time_t libvlc_media_player_get_length(
733 libvlc_media_player_t *p_mi,
734 libvlc_exception_t *p_e )
736 input_thread_t *p_input_thread;
739 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
740 if( !p_input_thread )
743 var_Get( p_input_thread, "length", &val );
744 vlc_object_release( p_input_thread );
746 return (val.i_time+500LL)/1000LL;
749 libvlc_time_t libvlc_media_player_get_time(
750 libvlc_media_player_t *p_mi,
751 libvlc_exception_t *p_e )
753 input_thread_t *p_input_thread;
756 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
757 if( !p_input_thread )
760 var_Get( p_input_thread , "time", &val );
761 vlc_object_release( p_input_thread );
762 return (val.i_time+500LL)/1000LL;
765 void libvlc_media_player_set_time(
766 libvlc_media_player_t *p_mi,
768 libvlc_exception_t *p_e )
770 input_thread_t *p_input_thread;
773 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
774 if( !p_input_thread )
777 value.i_time = time*1000LL;
778 var_Set( p_input_thread, "time", value );
779 vlc_object_release( p_input_thread );
782 void libvlc_media_player_set_position(
783 libvlc_media_player_t *p_mi,
785 libvlc_exception_t *p_e )
787 input_thread_t *p_input_thread;
789 val.f_float = position;
791 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
792 if( !p_input_thread )
795 var_Set( p_input_thread, "position", val );
796 vlc_object_release( p_input_thread );
799 float libvlc_media_player_get_position(
800 libvlc_media_player_t *p_mi,
801 libvlc_exception_t *p_e )
803 input_thread_t *p_input_thread;
806 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
807 if( !p_input_thread )
810 var_Get( p_input_thread, "position", &val );
811 vlc_object_release( p_input_thread );
816 void libvlc_media_player_set_chapter(
817 libvlc_media_player_t *p_mi,
819 libvlc_exception_t *p_e )
821 input_thread_t *p_input_thread;
825 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
826 if( !p_input_thread )
829 var_Set( p_input_thread, "chapter", val );
830 vlc_object_release( p_input_thread );
833 int libvlc_media_player_get_chapter(
834 libvlc_media_player_t *p_mi,
835 libvlc_exception_t *p_e )
837 input_thread_t *p_input_thread;
840 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
841 if( !p_input_thread )
844 var_Get( p_input_thread, "chapter", &val );
845 vlc_object_release( p_input_thread );
850 int libvlc_media_player_get_chapter_count(
851 libvlc_media_player_t *p_mi,
852 libvlc_exception_t *p_e )
854 input_thread_t *p_input_thread;
857 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
858 if( !p_input_thread )
861 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
862 vlc_object_release( p_input_thread );
867 int libvlc_media_player_get_chapter_count_for_title(
868 libvlc_media_player_t *p_mi,
870 libvlc_exception_t *p_e )
872 input_thread_t *p_input_thread;
875 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
876 if( !p_input_thread )
880 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
882 vlc_object_release( p_input_thread );
885 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
886 vlc_object_release( p_input_thread );
892 void libvlc_media_player_set_title(
893 libvlc_media_player_t *p_mi,
895 libvlc_exception_t *p_e )
897 input_thread_t *p_input_thread;
901 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
902 if( !p_input_thread )
905 var_Set( p_input_thread, "title", val );
906 vlc_object_release( p_input_thread );
909 libvlc_event_t event;
910 event.type = libvlc_MediaPlayerTitleChanged;
911 event.u.media_player_title_changed.new_title = i_title;
912 libvlc_event_send( p_mi->p_event_manager, &event );
915 int libvlc_media_player_get_title(
916 libvlc_media_player_t *p_mi,
917 libvlc_exception_t *p_e )
919 input_thread_t *p_input_thread;
922 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
923 if( !p_input_thread )
926 var_Get( p_input_thread, "title", &val );
927 vlc_object_release( p_input_thread );
932 int libvlc_media_player_get_title_count(
933 libvlc_media_player_t *p_mi,
934 libvlc_exception_t *p_e )
936 input_thread_t *p_input_thread;
939 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
940 if( !p_input_thread )
943 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
944 vlc_object_release( p_input_thread );
949 void libvlc_media_player_next_chapter(
950 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 int i_type = var_Type( p_input_thread, "next-chapter" );
962 var_Set( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
963 "next-chapter":"next-title", val );
965 vlc_object_release( p_input_thread );
968 void libvlc_media_player_previous_chapter(
969 libvlc_media_player_t *p_mi,
970 libvlc_exception_t *p_e )
972 input_thread_t *p_input_thread;
974 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
975 if( !p_input_thread )
978 int i_type = var_Type( p_input_thread, "next-chapter" );
981 var_Set( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
982 "prev-chapter":"prev-title", val );
984 vlc_object_release( p_input_thread );
987 float libvlc_media_player_get_fps(
988 libvlc_media_player_t *p_mi,
989 libvlc_exception_t *p_e)
991 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
996 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
998 vlc_object_release( p_input_thread );
1003 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1004 libvlc_exception_t *p_e)
1006 input_thread_t *p_input_thread =
1007 libvlc_get_input_thread ( p_mi, p_e);
1008 if ( !p_input_thread )
1011 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1013 vlc_object_release( p_input_thread );
1016 vlc_object_release( p_input_thread );
1020 void libvlc_media_player_set_rate(
1021 libvlc_media_player_t *p_mi,
1023 libvlc_exception_t *p_e )
1025 input_thread_t *p_input_thread;
1030 RAISEVOID( "Rate value is invalid" );
1032 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1033 if( !p_input_thread )
1036 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1037 if( (rate < 0) && !b_can_rewind )
1039 vlc_object_release( p_input_thread );
1040 libvlc_exception_raise( p_e, "Rate value is invalid" );
1044 val.i_int = 1000.0f/rate;
1045 var_Set( p_input_thread, "rate", val );
1046 vlc_object_release( p_input_thread );
1049 float libvlc_media_player_get_rate(
1050 libvlc_media_player_t *p_mi,
1051 libvlc_exception_t *p_e )
1053 input_thread_t *p_input_thread;
1057 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1058 if( !p_input_thread )
1059 return 0.0; /* rate < 0 indicates rewind */
1061 var_Get( p_input_thread, "rate", &val );
1062 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1063 if( (val.i_int < 0) && !b_can_rewind )
1065 libvlc_exception_raise( p_e, "invalid rate" );
1068 vlc_object_release( p_input_thread );
1070 return (float)1000.0f/val.i_int;
1073 libvlc_state_t libvlc_media_player_get_state(
1074 libvlc_media_player_t *p_mi,
1075 libvlc_exception_t *p_e )
1077 input_thread_t *p_input_thread;
1080 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1081 if ( !p_input_thread )
1083 /* We do return the right value, no need to throw an exception */
1084 if( libvlc_exception_raised( p_e ) )
1085 libvlc_exception_clear( p_e );
1086 return libvlc_Ended;
1089 var_Get( p_input_thread, "state", &val );
1090 vlc_object_release( p_input_thread );
1092 return vlc_to_libvlc_state(val.i_int);
1095 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1096 libvlc_exception_t *p_e )
1098 input_thread_t *p_input_thread;
1101 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1102 if ( !p_input_thread )
1104 /* We do return the right value, no need to throw an exception */
1105 if( libvlc_exception_raised( p_e ) )
1106 libvlc_exception_clear( p_e );
1109 var_Get( p_input_thread, "can-seek", &val );
1110 vlc_object_release( p_input_thread );
1115 /* internal function, used by audio, video */
1116 libvlc_track_description_t *
1117 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1118 const char *psz_variable,
1119 libvlc_exception_t *p_e )
1121 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1126 vlc_value_t val_list, text_list;
1127 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1129 if( val_list.p_list->i_count <= 0 ) /* no tracks */
1132 libvlc_track_description_t *p_track_description, *p_actual, *p_previous;
1133 p_track_description = ( libvlc_track_description_t * )
1134 malloc( sizeof( libvlc_track_description_t ) );
1135 if ( !p_track_description )
1137 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1138 vlc_object_release( p_input );
1139 libvlc_exception_raise( p_e, "no enough memory" );
1142 p_actual = p_track_description;
1144 for( int i = 0; i < val_list.p_list->i_count; i++ )
1148 p_actual = ( libvlc_track_description_t * )
1149 malloc( sizeof( libvlc_track_description_t ) );
1152 libvlc_track_description_release( p_track_description );
1153 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1154 vlc_object_release( p_input );
1155 libvlc_exception_raise( p_e, "no enough memory" );
1159 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1160 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1161 p_actual->p_next = NULL;
1163 p_previous->p_next = p_actual;
1164 p_previous = p_actual;
1167 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1168 vlc_object_release( p_input );
1170 return p_track_description;
1173 void libvlc_track_description_release( libvlc_track_description_t *p_track_description )
1175 libvlc_track_description_t *p_actual, *p_before;
1176 p_actual = p_track_description;
1180 free( p_actual->psz_name );
1181 p_before = p_actual;
1182 p_actual = p_before->p_next;
1187 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1188 libvlc_exception_t *p_e )
1190 input_thread_t *p_input_thread;
1193 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1194 if ( !p_input_thread )
1196 /* We do return the right value, no need to throw an exception */
1197 if( libvlc_exception_raised( p_e ) )
1198 libvlc_exception_clear( p_e );
1201 var_Get( p_input_thread, "can-pause", &val );
1202 vlc_object_release( p_input_thread );