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>
34 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
35 vlc_value_t oldval, vlc_value_t newval,
38 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
39 vlc_value_t oldval, vlc_value_t newval,
42 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
43 vlc_value_t oldval, vlc_value_t newval,
46 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
47 vlc_value_t oldval, vlc_value_t newval, void *p_data );
49 static const libvlc_state_t vlc_to_libvlc_state_array[] =
51 [INIT_S] = libvlc_NothingSpecial,
52 [OPENING_S] = libvlc_Opening,
53 [PLAYING_S] = libvlc_Playing,
54 [PAUSE_S] = libvlc_Paused,
55 [END_S] = libvlc_Ended,
56 [ERROR_S] = libvlc_Error,
59 static inline libvlc_state_t vlc_to_libvlc_state( int vlc_state )
61 if( vlc_state < 0 || vlc_state > 6 )
64 return vlc_to_libvlc_state_array[vlc_state];
68 * Release the associated input thread
70 * Object lock is NOT held.
72 static void release_input_thread( libvlc_media_player_t *p_mi )
74 input_thread_t * p_input_thread;
76 if( !p_mi || !p_mi->p_input_thread )
79 p_input_thread = p_mi->p_input_thread;
81 /* No one is tracking this input_thread appart us. Destroy it */
82 if( p_mi->b_own_its_input_thread )
84 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
85 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
86 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
88 /* We owned this one */
89 input_StopThread( p_input_thread );
90 vlc_thread_join( p_input_thread );
92 var_Destroy( p_input_thread, "drawable-hwnd" );
93 var_Destroy( p_input_thread, "drawable-xid" );
96 vlc_object_release( p_input_thread );
98 p_mi->p_input_thread = NULL;
102 * Retrieve the input thread. Be sure to release the object
103 * once you are done with it. (libvlc Internal)
105 * Object lock is held.
107 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
108 libvlc_exception_t *p_e )
110 input_thread_t *p_input_thread;
112 if( !p_mi ) RAISENULL( "Media Instance is NULL" );
114 vlc_mutex_lock( &p_mi->object_lock );
116 if( !p_mi->p_input_thread )
118 vlc_mutex_unlock( &p_mi->object_lock );
119 RAISENULL( "Input is NULL" );
122 p_input_thread = p_mi->p_input_thread;
123 vlc_object_hold( p_input_thread );
125 vlc_mutex_unlock( &p_mi->object_lock );
127 return p_input_thread;
131 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
132 vlc_value_t oldval, vlc_value_t newval,
138 libvlc_media_player_t * p_mi = p_userdata;
139 libvlc_event_t event;
141 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
142 event.type = libvlc_MediaPlayerSeekableChanged;
143 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
145 libvlc_event_send( p_mi->p_event_manager, &event );
150 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
151 vlc_value_t oldval, vlc_value_t newval,
157 libvlc_media_player_t * p_mi = p_userdata;
158 libvlc_event_t event;
160 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
161 event.type = libvlc_MediaPlayerPausableChanged;
162 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
164 libvlc_event_send( p_mi->p_event_manager, &event );
169 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
170 vlc_value_t oldval, vlc_value_t newval,
174 input_thread_t * p_input = (input_thread_t *)p_this;
175 libvlc_media_player_t * p_mi = p_userdata;
176 libvlc_event_t event;
178 assert( !strcmp( psz_cmd, "intf-event" ) );
180 if( newval.i_int == INPUT_EVENT_STATE )
182 libvlc_state_t libvlc_state;
184 switch ( var_GetInteger( p_input, "state" ) )
187 libvlc_state = libvlc_NothingSpecial;
188 event.type = libvlc_MediaPlayerNothingSpecial;
191 libvlc_state = libvlc_Opening;
192 event.type = libvlc_MediaPlayerOpening;
195 libvlc_state = libvlc_Playing;
196 event.type = libvlc_MediaPlayerPlaying;
199 libvlc_state = libvlc_Paused;
200 event.type = libvlc_MediaPlayerPaused;
203 libvlc_state = libvlc_Ended;
204 event.type = libvlc_MediaPlayerEndReached;
207 libvlc_state = libvlc_Error;
208 event.type = libvlc_MediaPlayerEncounteredError;
215 libvlc_media_set_state( p_mi->p_md, libvlc_state, NULL);
216 libvlc_event_send( p_mi->p_event_manager, &event );
218 else if( newval.i_int == INPUT_EVENT_TIMES )
220 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
221 return VLC_SUCCESS; /* Don't send the position while stopped */
224 event.type = libvlc_MediaPlayerPositionChanged;
225 event.u.media_player_position_changed.new_position = var_GetFloat( p_input, "position" );;
226 libvlc_event_send( p_mi->p_event_manager, &event );
229 event.type = libvlc_MediaPlayerTimeChanged;
230 event.u.media_player_time_changed.new_time = var_GetTime( p_input, "time" );
231 libvlc_event_send( p_mi->p_event_manager, &event );
239 /**************************************************************************
240 * Create a Media Instance object
241 **************************************************************************/
242 libvlc_media_player_t *
243 libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
244 libvlc_exception_t * p_e )
246 libvlc_media_player_t * p_mi;
248 if( !p_libvlc_instance )
250 libvlc_exception_raise( p_e, "invalid libvlc instance" );
254 p_mi = malloc( sizeof(libvlc_media_player_t) );
257 libvlc_exception_raise( p_e, "Not enough memory" );
262 p_mi->drawable.xid = 0;
264 p_mi->drawable.hwnd = NULL;
266 p_mi->p_libvlc_instance = p_libvlc_instance;
267 p_mi->p_input_thread = NULL;
268 /* refcount strategy:
269 * - All items created by _new start with a refcount set to 1
270 * - Accessor _release decrease the refcount by 1, if after that
271 * operation the refcount is 0, the object is destroyed.
272 * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
273 p_mi->i_refcount = 1;
274 p_mi->b_own_its_input_thread = true;
275 /* object_lock strategy:
276 * - No lock held in constructor
277 * - Lock when accessing all variable this lock is held
278 * - Lock when attempting to destroy the object the lock is also held */
279 vlc_mutex_init( &p_mi->object_lock );
280 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
281 p_libvlc_instance, p_e );
282 if( libvlc_exception_raised( p_e ) )
288 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
289 libvlc_MediaPlayerNothingSpecial, p_e );
290 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
291 libvlc_MediaPlayerOpening, p_e );
292 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
293 libvlc_MediaPlayerBuffering, p_e );
294 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
295 libvlc_MediaPlayerPlaying, p_e );
296 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
297 libvlc_MediaPlayerPaused, p_e );
298 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
299 libvlc_MediaPlayerStopped, p_e );
300 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
301 libvlc_MediaPlayerForward, p_e );
302 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
303 libvlc_MediaPlayerBackward, p_e );
304 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
305 libvlc_MediaPlayerEndReached, p_e );
306 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
307 libvlc_MediaPlayerEncounteredError, p_e );
309 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
310 libvlc_MediaPlayerPositionChanged, p_e );
311 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
312 libvlc_MediaPlayerTimeChanged, p_e );
313 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
314 libvlc_MediaPlayerTitleChanged, p_e );
315 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
316 libvlc_MediaPlayerSeekableChanged, p_e );
317 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
318 libvlc_MediaPlayerPausableChanged, p_e );
320 /* Snapshot initialization */
321 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
322 libvlc_MediaPlayerSnapshotTaken, p_e );
323 /* Attach a var callback to the global object to provide the glue between
324 vout_thread that generates the event and media_player that re-emits it
325 with its own event manager
327 var_Create( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
328 var_AddCallback( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", SnapshotTakenCallback, p_mi );
333 /**************************************************************************
334 * Create a Media Instance object with a media descriptor
335 **************************************************************************/
336 libvlc_media_player_t *
337 libvlc_media_player_new_from_media(
338 libvlc_media_t * p_md,
339 libvlc_exception_t *p_e )
341 libvlc_media_player_t * p_mi;
342 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
347 libvlc_media_retain( p_md );
353 /**************************************************************************
354 * Create a new media instance object from an input_thread (Libvlc Internal)
355 **************************************************************************/
356 libvlc_media_player_t * libvlc_media_player_new_from_input_thread(
357 struct libvlc_instance_t *p_libvlc_instance,
358 input_thread_t *p_input,
359 libvlc_exception_t *p_e )
361 libvlc_media_player_t * p_mi;
365 libvlc_exception_raise( p_e, "invalid input thread" );
369 p_mi = libvlc_media_player_new( p_libvlc_instance, p_e );
374 p_mi->p_md = libvlc_media_new_from_input_item(
376 input_GetItem( p_input ), p_e );
380 libvlc_media_player_destroy( p_mi );
384 /* will be released in media_player_release() */
385 vlc_object_hold( p_input );
387 p_mi->p_input_thread = p_input;
388 p_mi->b_own_its_input_thread = false;
393 /**************************************************************************
394 * Destroy a Media Instance object (libvlc internal)
396 * Warning: No lock held here, but hey, this is internal.
397 **************************************************************************/
398 void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
400 input_thread_t *p_input_thread;
401 libvlc_exception_t p_e;
403 libvlc_exception_init( &p_e );
408 /* Detach Callback from the main libvlc object */
409 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", SnapshotTakenCallback, p_mi );
411 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
413 if( libvlc_exception_raised( &p_e ) )
415 libvlc_event_manager_release( p_mi->p_event_manager );
416 libvlc_exception_clear( &p_e );
418 return; /* no need to worry about no input thread */
420 vlc_mutex_destroy( &p_mi->object_lock );
422 vlc_object_release( p_input_thread );
424 libvlc_media_release( p_mi->p_md );
429 /**************************************************************************
430 * Release a Media Instance object
431 **************************************************************************/
432 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
437 vlc_mutex_lock( &p_mi->object_lock );
441 if( p_mi->i_refcount > 0 )
443 vlc_mutex_unlock( &p_mi->object_lock );
446 vlc_mutex_unlock( &p_mi->object_lock );
447 vlc_mutex_destroy( &p_mi->object_lock );
449 release_input_thread( p_mi );
451 libvlc_event_manager_release( p_mi->p_event_manager );
453 libvlc_media_release( p_mi->p_md );
458 /**************************************************************************
459 * Retain a Media Instance object
460 **************************************************************************/
461 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
469 /**************************************************************************
470 * Set the Media descriptor associated with the instance
471 **************************************************************************/
472 void libvlc_media_player_set_media(
473 libvlc_media_player_t *p_mi,
474 libvlc_media_t *p_md,
475 libvlc_exception_t *p_e )
482 vlc_mutex_lock( &p_mi->object_lock );
484 release_input_thread( p_mi );
487 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
489 libvlc_media_release( p_mi->p_md );
494 vlc_mutex_unlock( &p_mi->object_lock );
495 return; /* It is ok to pass a NULL md */
498 libvlc_media_retain( p_md );
501 /* The policy here is to ignore that we were created using a different
502 * libvlc_instance, because we don't really care */
503 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
505 vlc_mutex_unlock( &p_mi->object_lock );
508 /**************************************************************************
509 * Get the Media descriptor associated with the instance
510 **************************************************************************/
512 libvlc_media_player_get_media(
513 libvlc_media_player_t *p_mi,
514 libvlc_exception_t *p_e )
521 libvlc_media_retain( p_mi->p_md );
525 /**************************************************************************
526 * Get the event Manager
527 **************************************************************************/
528 libvlc_event_manager_t *
529 libvlc_media_player_event_manager(
530 libvlc_media_player_t *p_mi,
531 libvlc_exception_t *p_e )
535 return p_mi->p_event_manager;
538 /**************************************************************************
539 * Trigger a snapshot Taken Event
540 *************************************************************************/
541 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
542 vlc_value_t oldval, vlc_value_t newval, void *p_data )
544 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
547 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
548 libvlc_event_t event ;
549 event.type = libvlc_MediaPlayerSnapshotTaken ;
550 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
551 /* Snapshot psz data is a vlc_variable owned by libvlc object .
552 Its memmory management is taken care by the obj*/
553 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p (%s)",
554 event.u.media_player_snapshot_taken.psz_filename ,event.u.media_player_snapshot_taken.psz_filename );
555 libvlc_event_send( p_mi->p_event_manager, &event );
560 /**************************************************************************
562 **************************************************************************/
563 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
564 libvlc_exception_t *p_e )
566 input_thread_t * p_input_thread;
568 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
570 /* A thread already exists, send it a play message */
571 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
572 vlc_object_release( p_input_thread );
576 /* Ignore previous exception */
577 libvlc_exception_clear( p_e );
579 vlc_mutex_lock( &p_mi->object_lock );
583 libvlc_exception_raise( p_e, "no associated media descriptor" );
584 vlc_mutex_unlock( &p_mi->object_lock );
588 p_mi->p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
589 p_mi->p_md->p_input_item );
592 if( !p_mi->p_input_thread )
594 vlc_mutex_unlock( &p_mi->object_lock );
598 p_input_thread = p_mi->p_input_thread;
600 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
601 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
604 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
606 var_SetInteger( p_input_thread, "drawable-hwnd", p_mi->drawable.hwnd );
609 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
610 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
611 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
613 vlc_mutex_unlock( &p_mi->object_lock );
616 /**************************************************************************
618 **************************************************************************/
619 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
620 libvlc_exception_t *p_e )
622 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
624 if( !p_input_thread )
627 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
629 if( state == libvlc_Playing )
631 if( libvlc_media_player_can_pause( p_mi, p_e ) )
632 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
634 libvlc_media_player_stop( p_mi, p_e );
637 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
639 vlc_object_release( p_input_thread );
642 /**************************************************************************
644 **************************************************************************/
645 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
646 libvlc_exception_t *p_e )
648 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
650 if( !p_input_thread )
653 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
655 vlc_object_release( p_input_thread );
657 if( state == libvlc_Playing )
665 /**************************************************************************
667 **************************************************************************/
668 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
669 libvlc_exception_t *p_e )
671 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
673 if( state == libvlc_Playing || state == libvlc_Paused )
675 /* Send a stop notification event only of we are in playing or paused states */
676 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
678 /* Construct and send the event */
679 libvlc_event_t event;
680 event.type = libvlc_MediaPlayerStopped;
681 libvlc_event_send( p_mi->p_event_manager, &event );
684 if( p_mi->b_own_its_input_thread )
686 vlc_mutex_lock( &p_mi->object_lock );
687 release_input_thread( p_mi ); /* This will stop the input thread */
688 vlc_mutex_unlock( &p_mi->object_lock );
692 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
694 if( !p_input_thread )
697 input_StopThread( p_input_thread );
698 vlc_object_release( p_input_thread );
702 /**************************************************************************
704 **************************************************************************/
705 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
706 libvlc_drawable_t drawable,
707 libvlc_exception_t *p_e )
709 input_thread_t *p_input_thread;
710 vout_thread_t *p_vout = NULL;
712 p_mi->drawable.xid = drawable;
714 /* Allow on the fly drawable changing. This is tricky has this may
715 * not be supported by every vout. We though can't disable it
716 * because of some creepy drawable type that are not flexible enough
717 * (Win32 HWND for instance) */
718 p_input_thread = libvlc_get_input_thread( p_mi, p_e );
719 if( !p_input_thread ) {
720 /* No input, nothing more to do, we are fine */
721 libvlc_exception_clear( p_e );
725 p_vout = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
728 vout_Control( p_vout , VOUT_REPARENT, drawable);
729 vlc_object_release( p_vout );
731 vlc_object_release( p_input_thread );
734 /**************************************************************************
736 **************************************************************************/
738 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
741 return p_mi->drawable.xid;
744 /**************************************************************************
745 * Getters for stream information
746 **************************************************************************/
747 libvlc_time_t libvlc_media_player_get_length(
748 libvlc_media_player_t *p_mi,
749 libvlc_exception_t *p_e )
751 input_thread_t *p_input_thread;
754 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
755 if( !p_input_thread )
758 var_Get( p_input_thread, "length", &val );
759 vlc_object_release( p_input_thread );
761 return (val.i_time+500LL)/1000LL;
764 libvlc_time_t libvlc_media_player_get_time(
765 libvlc_media_player_t *p_mi,
766 libvlc_exception_t *p_e )
768 input_thread_t *p_input_thread;
771 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
772 if( !p_input_thread )
775 var_Get( p_input_thread , "time", &val );
776 vlc_object_release( p_input_thread );
777 return (val.i_time+500LL)/1000LL;
780 void libvlc_media_player_set_time(
781 libvlc_media_player_t *p_mi,
783 libvlc_exception_t *p_e )
785 input_thread_t *p_input_thread;
788 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
789 if( !p_input_thread )
792 value.i_time = time*1000LL;
793 var_Set( p_input_thread, "time", value );
794 vlc_object_release( p_input_thread );
797 void libvlc_media_player_set_position(
798 libvlc_media_player_t *p_mi,
800 libvlc_exception_t *p_e )
802 input_thread_t *p_input_thread;
804 val.f_float = position;
806 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
807 if( !p_input_thread )
810 var_Set( p_input_thread, "position", val );
811 vlc_object_release( p_input_thread );
814 float libvlc_media_player_get_position(
815 libvlc_media_player_t *p_mi,
816 libvlc_exception_t *p_e )
818 input_thread_t *p_input_thread;
821 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
822 if( !p_input_thread )
825 var_Get( p_input_thread, "position", &val );
826 vlc_object_release( p_input_thread );
831 void libvlc_media_player_set_chapter(
832 libvlc_media_player_t *p_mi,
834 libvlc_exception_t *p_e )
836 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_Set( p_input_thread, "chapter", val );
845 vlc_object_release( p_input_thread );
848 int libvlc_media_player_get_chapter(
849 libvlc_media_player_t *p_mi,
850 libvlc_exception_t *p_e )
852 input_thread_t *p_input_thread;
855 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
856 if( !p_input_thread )
859 var_Get( p_input_thread, "chapter", &val );
860 vlc_object_release( p_input_thread );
865 int libvlc_media_player_get_chapter_count(
866 libvlc_media_player_t *p_mi,
867 libvlc_exception_t *p_e )
869 input_thread_t *p_input_thread;
872 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
873 if( !p_input_thread )
876 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
877 vlc_object_release( p_input_thread );
882 int libvlc_media_player_get_chapter_count_for_title(
883 libvlc_media_player_t *p_mi,
885 libvlc_exception_t *p_e )
887 input_thread_t *p_input_thread;
890 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
891 if( !p_input_thread )
895 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
897 vlc_object_release( p_input_thread );
900 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
901 vlc_object_release( p_input_thread );
907 void libvlc_media_player_set_title(
908 libvlc_media_player_t *p_mi,
910 libvlc_exception_t *p_e )
912 input_thread_t *p_input_thread;
916 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
917 if( !p_input_thread )
920 var_Set( p_input_thread, "title", val );
921 vlc_object_release( p_input_thread );
924 libvlc_event_t event;
925 event.type = libvlc_MediaPlayerTitleChanged;
926 event.u.media_player_title_changed.new_title = i_title;
927 libvlc_event_send( p_mi->p_event_manager, &event );
930 int libvlc_media_player_get_title(
931 libvlc_media_player_t *p_mi,
932 libvlc_exception_t *p_e )
934 input_thread_t *p_input_thread;
937 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
938 if( !p_input_thread )
941 var_Get( p_input_thread, "title", &val );
942 vlc_object_release( p_input_thread );
947 int libvlc_media_player_get_title_count(
948 libvlc_media_player_t *p_mi,
949 libvlc_exception_t *p_e )
951 input_thread_t *p_input_thread;
954 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
955 if( !p_input_thread )
958 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
959 vlc_object_release( p_input_thread );
964 void libvlc_media_player_next_chapter(
965 libvlc_media_player_t *p_mi,
966 libvlc_exception_t *p_e )
968 input_thread_t *p_input_thread;
970 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
971 if( !p_input_thread )
974 int i_type = var_Type( p_input_thread, "next-chapter" );
977 var_Set( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
978 "next-chapter":"next-title", val );
980 vlc_object_release( p_input_thread );
983 void libvlc_media_player_previous_chapter(
984 libvlc_media_player_t *p_mi,
985 libvlc_exception_t *p_e )
987 input_thread_t *p_input_thread;
989 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
990 if( !p_input_thread )
993 int i_type = var_Type( p_input_thread, "next-chapter" );
996 var_Set( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
997 "prev-chapter":"prev-title", val );
999 vlc_object_release( p_input_thread );
1002 float libvlc_media_player_get_fps(
1003 libvlc_media_player_t *p_mi,
1004 libvlc_exception_t *p_e)
1006 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1009 if( p_input_thread )
1011 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1013 vlc_object_release( p_input_thread );
1018 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1019 libvlc_exception_t *p_e)
1021 input_thread_t *p_input_thread =
1022 libvlc_get_input_thread ( p_mi, p_e);
1023 if ( !p_input_thread )
1026 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1028 vlc_object_release( p_input_thread );
1031 vlc_object_release( p_input_thread );
1035 void libvlc_media_player_set_rate(
1036 libvlc_media_player_t *p_mi,
1038 libvlc_exception_t *p_e )
1040 input_thread_t *p_input_thread;
1045 RAISEVOID( "Rate value is invalid" );
1047 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1048 if( !p_input_thread )
1051 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1052 if( (rate < 0) && !b_can_rewind )
1054 vlc_object_release( p_input_thread );
1055 libvlc_exception_raise( p_e, "Rate value is invalid" );
1059 val.i_int = 1000.0f/rate;
1060 var_Set( p_input_thread, "rate", val );
1061 vlc_object_release( p_input_thread );
1064 float libvlc_media_player_get_rate(
1065 libvlc_media_player_t *p_mi,
1066 libvlc_exception_t *p_e )
1068 input_thread_t *p_input_thread;
1072 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1073 if( !p_input_thread )
1074 return 0.0; /* rate < 0 indicates rewind */
1076 var_Get( p_input_thread, "rate", &val );
1077 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1078 if( (val.i_int < 0) && !b_can_rewind )
1080 libvlc_exception_raise( p_e, "invalid rate" );
1083 vlc_object_release( p_input_thread );
1085 return (float)1000.0f/val.i_int;
1088 libvlc_state_t libvlc_media_player_get_state(
1089 libvlc_media_player_t *p_mi,
1090 libvlc_exception_t *p_e )
1092 input_thread_t *p_input_thread;
1095 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1096 if ( !p_input_thread )
1098 /* We do return the right value, no need to throw an exception */
1099 if( libvlc_exception_raised( p_e ) )
1100 libvlc_exception_clear( p_e );
1101 return libvlc_Ended;
1104 var_Get( p_input_thread, "state", &val );
1105 vlc_object_release( p_input_thread );
1107 return vlc_to_libvlc_state(val.i_int);
1110 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1111 libvlc_exception_t *p_e )
1113 input_thread_t *p_input_thread;
1116 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1117 if ( !p_input_thread )
1119 /* We do return the right value, no need to throw an exception */
1120 if( libvlc_exception_raised( p_e ) )
1121 libvlc_exception_clear( p_e );
1124 var_Get( p_input_thread, "can-seek", &val );
1125 vlc_object_release( p_input_thread );
1130 /* internal function, used by audio, video */
1131 libvlc_track_description_t *
1132 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1133 const char *psz_variable,
1134 libvlc_exception_t *p_e )
1136 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1141 vlc_value_t val_list, text_list;
1142 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1144 if( val_list.p_list->i_count <= 0 ) /* no tracks */
1147 libvlc_track_description_t *p_track_description, *p_actual, *p_previous;
1148 p_track_description = ( libvlc_track_description_t * )
1149 malloc( sizeof( libvlc_track_description_t ) );
1150 if ( !p_track_description )
1152 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1153 vlc_object_release( p_input );
1154 libvlc_exception_raise( p_e, "no enough memory" );
1157 p_actual = p_track_description;
1159 for( int i = 0; i < val_list.p_list->i_count; i++ )
1163 p_actual = ( libvlc_track_description_t * )
1164 malloc( sizeof( libvlc_track_description_t ) );
1167 libvlc_track_description_release( p_track_description );
1168 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1169 vlc_object_release( p_input );
1170 libvlc_exception_raise( p_e, "no enough memory" );
1174 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1175 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1176 p_actual->p_next = NULL;
1178 p_previous->p_next = p_actual;
1179 p_previous = p_actual;
1182 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1183 vlc_object_release( p_input );
1185 return p_track_description;
1188 void libvlc_track_description_release( libvlc_track_description_t *p_track_description )
1190 libvlc_track_description_t *p_actual, *p_before;
1191 p_actual = p_track_description;
1195 free( p_actual->psz_name );
1196 p_before = p_actual;
1197 p_actual = p_before->p_next;
1202 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1203 libvlc_exception_t *p_e )
1205 input_thread_t *p_input_thread;
1208 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1209 if ( !p_input_thread )
1211 /* We do return the right value, no need to throw an exception */
1212 if( libvlc_exception_raised( p_e ) )
1213 libvlc_exception_clear( p_e );
1216 var_Get( p_input_thread, "can-pause", &val );
1217 vlc_object_release( p_input_thread );