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" );
261 p_mi->drawable.xid = 0;
262 p_mi->drawable.hwnd = NULL;
263 p_mi->p_libvlc_instance = p_libvlc_instance;
264 p_mi->p_input_thread = NULL;
265 /* refcount strategy:
266 * - All items created by _new start with a refcount set to 1
267 * - Accessor _release decrease the refcount by 1, if after that
268 * operation the refcount is 0, the object is destroyed.
269 * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
270 p_mi->i_refcount = 1;
271 p_mi->b_own_its_input_thread = true;
272 /* object_lock strategy:
273 * - No lock held in constructor
274 * - Lock when accessing all variable this lock is held
275 * - Lock when attempting to destroy the object the lock is also held */
276 vlc_mutex_init( &p_mi->object_lock );
277 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
278 p_libvlc_instance, p_e );
279 if( libvlc_exception_raised( p_e ) )
285 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
286 libvlc_MediaPlayerNothingSpecial, p_e );
287 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
288 libvlc_MediaPlayerOpening, p_e );
289 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
290 libvlc_MediaPlayerBuffering, p_e );
291 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
292 libvlc_MediaPlayerPlaying, p_e );
293 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
294 libvlc_MediaPlayerPaused, p_e );
295 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
296 libvlc_MediaPlayerStopped, p_e );
297 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
298 libvlc_MediaPlayerForward, p_e );
299 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
300 libvlc_MediaPlayerBackward, p_e );
301 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
302 libvlc_MediaPlayerEndReached, p_e );
303 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
304 libvlc_MediaPlayerEncounteredError, p_e );
306 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
307 libvlc_MediaPlayerPositionChanged, p_e );
308 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
309 libvlc_MediaPlayerTimeChanged, p_e );
310 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
311 libvlc_MediaPlayerTitleChanged, p_e );
312 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
313 libvlc_MediaPlayerSeekableChanged, p_e );
314 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
315 libvlc_MediaPlayerPausableChanged, p_e );
317 /* Snapshot initialization */
318 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
319 libvlc_MediaPlayerSnapshotTaken, p_e );
320 /* Attach a var callback to the global object to provide the glue between
321 vout_thread that generates the event and media_player that re-emits it
322 with its own event manager
324 var_Create( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
325 var_AddCallback( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", SnapshotTakenCallback, p_mi );
330 /**************************************************************************
331 * Create a Media Instance object with a media descriptor
332 **************************************************************************/
333 libvlc_media_player_t *
334 libvlc_media_player_new_from_media(
335 libvlc_media_t * p_md,
336 libvlc_exception_t *p_e )
338 libvlc_media_player_t * p_mi;
339 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
344 libvlc_media_retain( p_md );
350 /**************************************************************************
351 * Create a new media instance object from an input_thread (Libvlc Internal)
352 **************************************************************************/
353 libvlc_media_player_t * libvlc_media_player_new_from_input_thread(
354 struct libvlc_instance_t *p_libvlc_instance,
355 input_thread_t *p_input,
356 libvlc_exception_t *p_e )
358 libvlc_media_player_t * p_mi;
362 libvlc_exception_raise( p_e, "invalid input thread" );
366 p_mi = libvlc_media_player_new( p_libvlc_instance, p_e );
371 p_mi->p_md = libvlc_media_new_from_input_item(
373 input_GetItem( p_input ), p_e );
377 libvlc_media_player_destroy( p_mi );
381 /* will be released in media_player_release() */
382 vlc_object_hold( p_input );
384 p_mi->p_input_thread = p_input;
385 p_mi->b_own_its_input_thread = false;
390 /**************************************************************************
391 * Destroy a Media Instance object (libvlc internal)
393 * Warning: No lock held here, but hey, this is internal.
394 **************************************************************************/
395 void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
397 input_thread_t *p_input_thread;
398 libvlc_exception_t p_e;
400 libvlc_exception_init( &p_e );
405 /* Detach Callback from the main libvlc object */
406 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", SnapshotTakenCallback, p_mi );
408 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
410 if( libvlc_exception_raised( &p_e ) )
412 libvlc_event_manager_release( p_mi->p_event_manager );
413 libvlc_exception_clear( &p_e );
415 return; /* no need to worry about no input thread */
417 vlc_mutex_destroy( &p_mi->object_lock );
419 vlc_object_release( p_input_thread );
421 libvlc_media_release( p_mi->p_md );
426 /**************************************************************************
427 * Release a Media Instance object
428 **************************************************************************/
429 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
434 vlc_mutex_lock( &p_mi->object_lock );
438 if( p_mi->i_refcount > 0 )
440 vlc_mutex_unlock( &p_mi->object_lock );
443 vlc_mutex_unlock( &p_mi->object_lock );
444 vlc_mutex_destroy( &p_mi->object_lock );
446 release_input_thread( p_mi );
448 libvlc_event_manager_release( p_mi->p_event_manager );
450 libvlc_media_release( p_mi->p_md );
455 /**************************************************************************
456 * Retain a Media Instance object
457 **************************************************************************/
458 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
466 /**************************************************************************
467 * Set the Media descriptor associated with the instance
468 **************************************************************************/
469 void libvlc_media_player_set_media(
470 libvlc_media_player_t *p_mi,
471 libvlc_media_t *p_md,
472 libvlc_exception_t *p_e )
479 vlc_mutex_lock( &p_mi->object_lock );
481 release_input_thread( p_mi );
484 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
486 libvlc_media_release( p_mi->p_md );
491 vlc_mutex_unlock( &p_mi->object_lock );
492 return; /* It is ok to pass a NULL md */
495 libvlc_media_retain( p_md );
498 /* The policy here is to ignore that we were created using a different
499 * libvlc_instance, because we don't really care */
500 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
502 vlc_mutex_unlock( &p_mi->object_lock );
505 /**************************************************************************
506 * Get the Media descriptor associated with the instance
507 **************************************************************************/
509 libvlc_media_player_get_media(
510 libvlc_media_player_t *p_mi,
511 libvlc_exception_t *p_e )
518 libvlc_media_retain( p_mi->p_md );
522 /**************************************************************************
523 * Get the event Manager
524 **************************************************************************/
525 libvlc_event_manager_t *
526 libvlc_media_player_event_manager(
527 libvlc_media_player_t *p_mi,
528 libvlc_exception_t *p_e )
532 return p_mi->p_event_manager;
535 /**************************************************************************
536 * Trigger a snapshot Taken Event
537 *************************************************************************/
538 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
539 vlc_value_t oldval, vlc_value_t newval, void *p_data )
541 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
544 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
545 libvlc_event_t event ;
546 event.type = libvlc_MediaPlayerSnapshotTaken ;
547 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
548 /* Snapshot psz data is a vlc_variable owned by libvlc object .
549 Its memmory management is taken care by the obj*/
550 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p (%s)",
551 event.u.media_player_snapshot_taken.psz_filename ,event.u.media_player_snapshot_taken.psz_filename );
552 libvlc_event_send( p_mi->p_event_manager, &event );
557 /**************************************************************************
559 **************************************************************************/
560 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
561 libvlc_exception_t *p_e )
563 input_thread_t * p_input_thread;
565 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
567 /* A thread already exists, send it a play message */
568 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
569 vlc_object_release( p_input_thread );
573 /* Ignore previous exception */
574 libvlc_exception_clear( p_e );
576 vlc_mutex_lock( &p_mi->object_lock );
580 libvlc_exception_raise( p_e, "no associated media descriptor" );
581 vlc_mutex_unlock( &p_mi->object_lock );
585 p_mi->p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
586 p_mi->p_md->p_input_item );
589 if( !p_mi->p_input_thread )
591 vlc_mutex_unlock( &p_mi->object_lock );
595 p_input_thread = p_mi->p_input_thread;
597 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
598 if( p_mi->drawable.xid )
599 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
601 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
602 if( p_mi->drawable.hwnd != NULL )
604 vlc_value_t val = { .p_address = p_mi->drawable.hwnd };
605 var_Set( p_input_thread, "drawable-hwnd", val );
608 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
609 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
610 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
612 vlc_mutex_unlock( &p_mi->object_lock );
615 /**************************************************************************
617 **************************************************************************/
618 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
619 libvlc_exception_t *p_e )
621 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
623 if( !p_input_thread )
626 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
628 if( state == libvlc_Playing )
630 if( libvlc_media_player_can_pause( p_mi, p_e ) )
631 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
633 libvlc_media_player_stop( p_mi, p_e );
636 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
638 vlc_object_release( p_input_thread );
641 /**************************************************************************
643 **************************************************************************/
644 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
645 libvlc_exception_t *p_e )
647 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
649 if( !p_input_thread )
652 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
654 vlc_object_release( p_input_thread );
656 if( state == libvlc_Playing )
664 /**************************************************************************
666 **************************************************************************/
667 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
668 libvlc_exception_t *p_e )
670 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
672 if( state == libvlc_Playing || state == libvlc_Paused )
674 /* Send a stop notification event only of we are in playing or paused states */
675 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
677 /* Construct and send the event */
678 libvlc_event_t event;
679 event.type = libvlc_MediaPlayerStopped;
680 libvlc_event_send( p_mi->p_event_manager, &event );
683 if( p_mi->b_own_its_input_thread )
685 vlc_mutex_lock( &p_mi->object_lock );
686 release_input_thread( p_mi ); /* This will stop the input thread */
687 vlc_mutex_unlock( &p_mi->object_lock );
691 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
693 if( !p_input_thread )
696 input_StopThread( p_input_thread );
697 vlc_object_release( p_input_thread );
701 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
703 libvlc_exception_t *p_e )
706 p_mi->drawable.xid = drawable;
709 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
711 return p_mi->drawable.xid;
714 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
716 libvlc_exception_t *p_e )
719 p_mi->drawable.hwnd = drawable;
722 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
724 return p_mi->drawable.hwnd;
727 /**************************************************************************
729 **************************************************************************/
730 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
731 libvlc_drawable_t drawable,
732 libvlc_exception_t *p_e )
734 input_thread_t *p_input_thread;
735 vout_thread_t *p_vout = NULL;
737 p_mi->drawable.xid = drawable;
739 /* Allow on the fly drawable changing. This is tricky has this may
740 * not be supported by every vout. We though can't disable it
741 * because of some creepy drawable type that are not flexible enough
742 * (Win32 HWND for instance) */
743 p_input_thread = libvlc_get_input_thread( p_mi, p_e );
744 if( !p_input_thread ) {
745 /* No input, nothing more to do, we are fine */
746 libvlc_exception_clear( p_e );
750 p_vout = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
753 vout_Control( p_vout , VOUT_REPARENT, drawable);
754 vlc_object_release( p_vout );
756 vlc_object_release( p_input_thread );
759 /**************************************************************************
761 **************************************************************************/
763 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
766 return p_mi->drawable.xid;
769 /**************************************************************************
770 * Getters for stream information
771 **************************************************************************/
772 libvlc_time_t libvlc_media_player_get_length(
773 libvlc_media_player_t *p_mi,
774 libvlc_exception_t *p_e )
776 input_thread_t *p_input_thread;
779 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
780 if( !p_input_thread )
783 var_Get( p_input_thread, "length", &val );
784 vlc_object_release( p_input_thread );
786 return (val.i_time+500LL)/1000LL;
789 libvlc_time_t libvlc_media_player_get_time(
790 libvlc_media_player_t *p_mi,
791 libvlc_exception_t *p_e )
793 input_thread_t *p_input_thread;
796 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
797 if( !p_input_thread )
800 var_Get( p_input_thread , "time", &val );
801 vlc_object_release( p_input_thread );
802 return (val.i_time+500LL)/1000LL;
805 void libvlc_media_player_set_time(
806 libvlc_media_player_t *p_mi,
808 libvlc_exception_t *p_e )
810 input_thread_t *p_input_thread;
813 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
814 if( !p_input_thread )
817 value.i_time = time*1000LL;
818 var_Set( p_input_thread, "time", value );
819 vlc_object_release( p_input_thread );
822 void libvlc_media_player_set_position(
823 libvlc_media_player_t *p_mi,
825 libvlc_exception_t *p_e )
827 input_thread_t *p_input_thread;
829 val.f_float = position;
831 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
832 if( !p_input_thread )
835 var_Set( p_input_thread, "position", val );
836 vlc_object_release( p_input_thread );
839 float libvlc_media_player_get_position(
840 libvlc_media_player_t *p_mi,
841 libvlc_exception_t *p_e )
843 input_thread_t *p_input_thread;
846 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
847 if( !p_input_thread )
850 var_Get( p_input_thread, "position", &val );
851 vlc_object_release( p_input_thread );
856 void libvlc_media_player_set_chapter(
857 libvlc_media_player_t *p_mi,
859 libvlc_exception_t *p_e )
861 input_thread_t *p_input_thread;
865 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
866 if( !p_input_thread )
869 var_Set( p_input_thread, "chapter", val );
870 vlc_object_release( p_input_thread );
873 int libvlc_media_player_get_chapter(
874 libvlc_media_player_t *p_mi,
875 libvlc_exception_t *p_e )
877 input_thread_t *p_input_thread;
880 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
881 if( !p_input_thread )
884 var_Get( p_input_thread, "chapter", &val );
885 vlc_object_release( p_input_thread );
890 int libvlc_media_player_get_chapter_count(
891 libvlc_media_player_t *p_mi,
892 libvlc_exception_t *p_e )
894 input_thread_t *p_input_thread;
897 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
898 if( !p_input_thread )
901 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
902 vlc_object_release( p_input_thread );
907 int libvlc_media_player_get_chapter_count_for_title(
908 libvlc_media_player_t *p_mi,
910 libvlc_exception_t *p_e )
912 input_thread_t *p_input_thread;
915 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
916 if( !p_input_thread )
920 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
922 vlc_object_release( p_input_thread );
925 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
926 vlc_object_release( p_input_thread );
932 void libvlc_media_player_set_title(
933 libvlc_media_player_t *p_mi,
935 libvlc_exception_t *p_e )
937 input_thread_t *p_input_thread;
941 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
942 if( !p_input_thread )
945 var_Set( p_input_thread, "title", val );
946 vlc_object_release( p_input_thread );
949 libvlc_event_t event;
950 event.type = libvlc_MediaPlayerTitleChanged;
951 event.u.media_player_title_changed.new_title = i_title;
952 libvlc_event_send( p_mi->p_event_manager, &event );
955 int libvlc_media_player_get_title(
956 libvlc_media_player_t *p_mi,
957 libvlc_exception_t *p_e )
959 input_thread_t *p_input_thread;
962 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
963 if( !p_input_thread )
966 var_Get( p_input_thread, "title", &val );
967 vlc_object_release( p_input_thread );
972 int libvlc_media_player_get_title_count(
973 libvlc_media_player_t *p_mi,
974 libvlc_exception_t *p_e )
976 input_thread_t *p_input_thread;
979 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
980 if( !p_input_thread )
983 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
984 vlc_object_release( p_input_thread );
989 void libvlc_media_player_next_chapter(
990 libvlc_media_player_t *p_mi,
991 libvlc_exception_t *p_e )
993 input_thread_t *p_input_thread;
995 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
996 if( !p_input_thread )
999 int i_type = var_Type( p_input_thread, "next-chapter" );
1002 var_Set( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1003 "next-chapter":"next-title", val );
1005 vlc_object_release( p_input_thread );
1008 void libvlc_media_player_previous_chapter(
1009 libvlc_media_player_t *p_mi,
1010 libvlc_exception_t *p_e )
1012 input_thread_t *p_input_thread;
1014 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1015 if( !p_input_thread )
1018 int i_type = var_Type( p_input_thread, "next-chapter" );
1021 var_Set( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1022 "prev-chapter":"prev-title", val );
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)
1046 input_thread_t *p_input_thread =
1047 libvlc_get_input_thread ( p_mi, p_e);
1048 if ( !p_input_thread )
1051 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1053 vlc_object_release( p_input_thread );
1056 vlc_object_release( p_input_thread );
1060 void libvlc_media_player_set_rate(
1061 libvlc_media_player_t *p_mi,
1063 libvlc_exception_t *p_e )
1065 input_thread_t *p_input_thread;
1070 RAISEVOID( "Rate value is invalid" );
1072 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1073 if( !p_input_thread )
1076 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1077 if( (rate < 0) && !b_can_rewind )
1079 vlc_object_release( p_input_thread );
1080 libvlc_exception_raise( p_e, "Rate value is invalid" );
1084 val.i_int = 1000.0f/rate;
1085 var_Set( p_input_thread, "rate", val );
1086 vlc_object_release( p_input_thread );
1089 float libvlc_media_player_get_rate(
1090 libvlc_media_player_t *p_mi,
1091 libvlc_exception_t *p_e )
1093 input_thread_t *p_input_thread;
1097 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1098 if( !p_input_thread )
1099 return 0.0; /* rate < 0 indicates rewind */
1101 var_Get( p_input_thread, "rate", &val );
1102 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1103 if( (val.i_int < 0) && !b_can_rewind )
1105 libvlc_exception_raise( p_e, "invalid rate" );
1108 vlc_object_release( p_input_thread );
1110 return (float)1000.0f/val.i_int;
1113 libvlc_state_t libvlc_media_player_get_state(
1114 libvlc_media_player_t *p_mi,
1115 libvlc_exception_t *p_e )
1117 input_thread_t *p_input_thread;
1120 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1121 if ( !p_input_thread )
1123 /* We do return the right value, no need to throw an exception */
1124 if( libvlc_exception_raised( p_e ) )
1125 libvlc_exception_clear( p_e );
1126 return libvlc_Ended;
1129 var_Get( p_input_thread, "state", &val );
1130 vlc_object_release( p_input_thread );
1132 return vlc_to_libvlc_state(val.i_int);
1135 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1136 libvlc_exception_t *p_e )
1138 input_thread_t *p_input_thread;
1141 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1142 if ( !p_input_thread )
1144 /* We do return the right value, no need to throw an exception */
1145 if( libvlc_exception_raised( p_e ) )
1146 libvlc_exception_clear( p_e );
1149 var_Get( p_input_thread, "can-seek", &val );
1150 vlc_object_release( p_input_thread );
1155 /* internal function, used by audio, video */
1156 libvlc_track_description_t *
1157 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1158 const char *psz_variable,
1159 libvlc_exception_t *p_e )
1161 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1166 vlc_value_t val_list, text_list;
1167 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1169 if( val_list.p_list->i_count <= 0 ) /* no tracks */
1172 libvlc_track_description_t *p_track_description, *p_actual, *p_previous;
1173 p_track_description = ( libvlc_track_description_t * )
1174 malloc( sizeof( libvlc_track_description_t ) );
1175 if ( !p_track_description )
1177 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1178 vlc_object_release( p_input );
1179 libvlc_exception_raise( p_e, "no enough memory" );
1182 p_actual = p_track_description;
1184 for( int i = 0; i < val_list.p_list->i_count; i++ )
1188 p_actual = ( libvlc_track_description_t * )
1189 malloc( sizeof( libvlc_track_description_t ) );
1192 libvlc_track_description_release( p_track_description );
1193 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1194 vlc_object_release( p_input );
1195 libvlc_exception_raise( p_e, "no enough memory" );
1199 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1200 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1201 p_actual->p_next = NULL;
1203 p_previous->p_next = p_actual;
1204 p_previous = p_actual;
1207 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1208 vlc_object_release( p_input );
1210 return p_track_description;
1213 void libvlc_track_description_release( libvlc_track_description_t *p_track_description )
1215 libvlc_track_description_t *p_actual, *p_before;
1216 p_actual = p_track_description;
1220 free( p_actual->psz_name );
1221 p_before = p_actual;
1222 p_actual = p_before->p_next;
1227 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1228 libvlc_exception_t *p_e )
1230 input_thread_t *p_input_thread;
1233 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1234 if ( !p_input_thread )
1236 /* We do return the right value, no need to throw an exception */
1237 if( libvlc_exception_raised( p_e ) )
1238 libvlc_exception_clear( p_e );
1241 var_Get( p_input_thread, "can-pause", &val );
1242 vlc_object_release( p_input_thread );