1 /*****************************************************************************
2 * media_player.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005-2009 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
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, bool b_input_abort )
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 var_DelCallback( p_input_thread, "can-seek",
82 input_seekable_changed, p_mi );
83 var_DelCallback( p_input_thread, "can-pause",
84 input_pausable_changed, p_mi );
85 var_DelCallback( p_input_thread, "intf-event",
86 input_event_changed, p_mi );
88 /* We owned this one */
89 input_Stop( p_input_thread, b_input_abort );
90 vlc_thread_join( p_input_thread );
92 var_Destroy( p_input_thread, "drawable-hwnd" );
93 var_Destroy( p_input_thread, "drawable-xid" );
94 var_Destroy( p_input_thread, "drawable-agl" );
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 * Function will lock the object.
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_POSITION )
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 =
226 var_GetFloat( p_input, "position" );
227 libvlc_event_send( p_mi->p_event_manager, &event );
230 event.type = libvlc_MediaPlayerTimeChanged;
231 event.u.media_player_time_changed.new_time =
232 var_GetTime( p_input, "time" );
233 libvlc_event_send( p_mi->p_event_manager, &event );
240 static void libvlc_media_player_destroy( libvlc_media_player_t * );
242 /**************************************************************************
243 * Create a Media Instance object.
246 * - All items created by _new start with a refcount set to 1.
247 * - Accessor _release decrease the refcount by 1, if after that
248 * operation the refcount is 0, the object is destroyed.
249 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
251 * Object locking strategy:
252 * - No lock held while in constructor.
253 * - When accessing any member variable this lock is held. (XXX who locks?)
254 * - When attempting to destroy the object the lock is also held.
255 **************************************************************************/
256 libvlc_media_player_t *
257 libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
258 libvlc_exception_t * p_e )
260 libvlc_media_player_t * p_mi;
262 if( !p_libvlc_instance )
264 libvlc_exception_raise( p_e, "invalid libvlc instance" );
268 p_mi = malloc( sizeof(libvlc_media_player_t) );
271 libvlc_exception_raise( p_e, "Not enough memory" );
275 p_mi->drawable.agl = 0;
276 p_mi->drawable.xid = 0;
277 p_mi->drawable.hwnd = NULL;
278 p_mi->p_libvlc_instance = p_libvlc_instance;
279 p_mi->p_input_thread = NULL;
280 p_mi->i_refcount = 1;
281 vlc_mutex_init( &p_mi->object_lock );
282 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
283 p_libvlc_instance, p_e );
284 if( libvlc_exception_raised( p_e ) )
286 vlc_mutex_destroy( &p_mi->object_lock );
291 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
292 libvlc_MediaPlayerNothingSpecial, p_e );
293 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
294 libvlc_MediaPlayerOpening, p_e );
295 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
296 libvlc_MediaPlayerBuffering, p_e );
297 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
298 libvlc_MediaPlayerPlaying, p_e );
299 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
300 libvlc_MediaPlayerPaused, p_e );
301 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
302 libvlc_MediaPlayerStopped, p_e );
303 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
304 libvlc_MediaPlayerForward, p_e );
305 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
306 libvlc_MediaPlayerBackward, p_e );
307 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
308 libvlc_MediaPlayerEndReached, p_e );
309 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
310 libvlc_MediaPlayerEncounteredError, p_e );
312 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
313 libvlc_MediaPlayerPositionChanged, p_e );
314 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
315 libvlc_MediaPlayerTimeChanged, p_e );
316 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
317 libvlc_MediaPlayerTitleChanged, p_e );
318 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
319 libvlc_MediaPlayerSeekableChanged, p_e );
320 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
321 libvlc_MediaPlayerPausableChanged, p_e );
323 /* Snapshot initialization */
324 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
325 libvlc_MediaPlayerSnapshotTaken, p_e );
327 /* Attach a var callback to the global object to provide the glue between
328 vout_thread that generates the event and media_player that re-emits it
329 with its own event manager
331 var_Create( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
332 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
333 var_AddCallback( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
334 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;
349 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
353 libvlc_media_retain( p_md );
359 /**************************************************************************
360 * Destroy a Media Instance object (libvlc internal)
362 * Warning: No lock held here, but hey, this is internal. Caller must lock.
363 **************************************************************************/
364 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
366 input_thread_t *p_input_thread;
367 libvlc_exception_t p_e;
371 /* Detach Callback from the main libvlc object */
372 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
373 "vout-snapshottaken", SnapshotTakenCallback, p_mi );
375 libvlc_exception_init( &p_e );
376 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
378 if( libvlc_exception_raised( &p_e ) )
379 /* no need to worry about no input thread */
380 libvlc_exception_clear( &p_e );
382 release_input_thread( p_mi, true );
384 libvlc_event_manager_release( p_mi->p_event_manager );
386 libvlc_media_release( p_mi->p_md );
387 vlc_mutex_destroy( &p_mi->object_lock );
391 /**************************************************************************
392 * Release a Media Instance object.
394 * Function does the locking.
395 **************************************************************************/
396 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
401 vlc_mutex_lock( &p_mi->object_lock );
402 destroy = !--p_mi->i_refcount;
403 vlc_mutex_unlock( &p_mi->object_lock );
406 libvlc_media_player_destroy( p_mi );
409 /**************************************************************************
410 * Retain a Media Instance object.
412 * Caller must hold the lock.
413 **************************************************************************/
414 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
418 vlc_mutex_lock( &p_mi->object_lock );
420 vlc_mutex_unlock( &p_mi->object_lock );
423 /**************************************************************************
424 * Set the Media descriptor associated with the instance.
426 * Enter without lock -- function will lock the object.
427 **************************************************************************/
428 void libvlc_media_player_set_media(
429 libvlc_media_player_t *p_mi,
430 libvlc_media_t *p_md,
431 libvlc_exception_t *p_e )
438 vlc_mutex_lock( &p_mi->object_lock );
440 /* FIXME I am not sure if it is a user request or on die(eof/error)
442 release_input_thread( p_mi,
443 p_mi->p_input_thread &&
444 !p_mi->p_input_thread->b_eof &&
445 !p_mi->p_input_thread->b_error );
448 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
450 libvlc_media_release( p_mi->p_md );
455 vlc_mutex_unlock( &p_mi->object_lock );
456 return; /* It is ok to pass a NULL md */
459 libvlc_media_retain( p_md );
462 /* The policy here is to ignore that we were created using a different
463 * libvlc_instance, because we don't really care */
464 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
466 vlc_mutex_unlock( &p_mi->object_lock );
469 /**************************************************************************
470 * Get the Media descriptor associated with the instance.
471 **************************************************************************/
473 libvlc_media_player_get_media(
474 libvlc_media_player_t *p_mi,
475 libvlc_exception_t *p_e )
482 libvlc_media_retain( p_mi->p_md );
486 /**************************************************************************
487 * Get the event Manager.
488 **************************************************************************/
489 libvlc_event_manager_t *
490 libvlc_media_player_event_manager(
491 libvlc_media_player_t *p_mi,
492 libvlc_exception_t *p_e )
496 return p_mi->p_event_manager;
499 /**************************************************************************
500 * Trigger a snapshot Taken Event.
501 *************************************************************************/
502 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
503 vlc_value_t oldval, vlc_value_t newval, void *p_data )
505 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
508 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
509 libvlc_event_t event ;
510 event.type = libvlc_MediaPlayerSnapshotTaken ;
511 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
512 /* Snapshot psz data is a vlc_variable owned by libvlc object .
513 Its memmory management is taken care by the obj*/
514 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p"
515 " (%s)", event.u.media_player_snapshot_taken.psz_filename,
516 event.u.media_player_snapshot_taken.psz_filename );
517 libvlc_event_send( p_mi->p_event_manager, &event );
522 /**************************************************************************
523 * Tell media player to start playing.
524 **************************************************************************/
525 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
526 libvlc_exception_t *p_e )
528 input_thread_t * p_input_thread;
530 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
532 /* A thread already exists, send it a play message */
533 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
534 vlc_object_release( p_input_thread );
538 /* Ignore previous exception */
539 libvlc_exception_clear( p_e );
541 vlc_mutex_lock( &p_mi->object_lock );
545 libvlc_exception_raise( p_e, "no associated media descriptor" );
546 vlc_mutex_unlock( &p_mi->object_lock );
550 p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
551 p_mi->p_md->p_input_item, NULL, NULL );
553 if( !p_mi->p_input_thread )
555 vlc_mutex_unlock( &p_mi->object_lock );
559 p_input_thread = p_mi->p_input_thread;
561 var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
562 if( p_mi->drawable.agl )
563 var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
565 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
566 if( p_mi->drawable.xid )
567 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
569 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
570 if( p_mi->drawable.hwnd != NULL )
572 vlc_value_t val = { .p_address = p_mi->drawable.hwnd };
573 var_Set( p_input_thread, "drawable-hwnd", val );
576 var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
577 if( p_mi->drawable.nsobject != NULL )
579 vlc_value_t val = { .p_address = p_mi->drawable.nsobject };
580 var_Set( p_input_thread, "drawable-nsobject", val );
583 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
584 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
585 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
587 if( input_Start( p_input_thread ) )
589 vlc_object_release( p_input_thread );
590 p_mi->p_input_thread = NULL;
593 vlc_mutex_unlock( &p_mi->object_lock );
596 /**************************************************************************
598 **************************************************************************/
599 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
600 libvlc_exception_t *p_e )
602 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
603 if( !p_input_thread )
606 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
607 if( state == libvlc_Playing || state == libvlc_Buffering )
609 if( libvlc_media_player_can_pause( p_mi, p_e ) )
610 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
612 libvlc_media_player_stop( p_mi, p_e );
615 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
617 vlc_object_release( p_input_thread );
620 /**************************************************************************
621 * Tells whether the media player is currently playing.
623 * Enter with lock held.
624 **************************************************************************/
625 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
626 libvlc_exception_t *p_e )
628 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
629 return (libvlc_Playing == state) || (libvlc_Buffering == state);
632 /**************************************************************************
634 **************************************************************************/
635 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
636 libvlc_exception_t *p_e )
638 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
640 if( state == libvlc_Playing ||
641 state == libvlc_Paused ||
642 state == libvlc_Buffering )
644 /* Send a stop notification event only if we are in playing,
645 * buffering or paused states */
646 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
648 /* Construct and send the event */
649 libvlc_event_t event;
650 event.type = libvlc_MediaPlayerStopped;
651 libvlc_event_send( p_mi->p_event_manager, &event );
654 vlc_mutex_lock( &p_mi->object_lock );
655 release_input_thread( p_mi, true ); /* This will stop the input thread */
656 vlc_mutex_unlock( &p_mi->object_lock );
659 /**************************************************************************
661 **************************************************************************/
662 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
664 libvlc_exception_t *p_e )
667 p_mi->drawable.nsobject = drawable;
670 /**************************************************************************
672 **************************************************************************/
673 uint32_t libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
675 return p_mi->drawable.nsobject;
678 /**************************************************************************
680 **************************************************************************/
681 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
683 libvlc_exception_t *p_e )
686 p_mi->drawable.agl = drawable;
689 /**************************************************************************
691 **************************************************************************/
692 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
694 return p_mi->drawable.agl;
697 /**************************************************************************
699 **************************************************************************/
700 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
702 libvlc_exception_t *p_e )
705 p_mi->drawable.xid = drawable;
708 /**************************************************************************
710 **************************************************************************/
711 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
713 return p_mi->drawable.xid;
716 /**************************************************************************
718 **************************************************************************/
719 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
721 libvlc_exception_t *p_e )
724 p_mi->drawable.hwnd = drawable;
727 /**************************************************************************
729 **************************************************************************/
730 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
732 return p_mi->drawable.hwnd;
735 /**************************************************************************
737 **************************************************************************/
738 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
739 libvlc_drawable_t drawable,
740 libvlc_exception_t *p_e )
743 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
744 p_mi->drawable.hwnd = (HWND)drawable;
746 libvlc_exception_raise(p_e, "Operation not supported");
747 #elif defined(__APPLE__)
748 p_mi->drawable.agl = drawable;
751 p_mi->drawable.xid = drawable;
756 /**************************************************************************
758 **************************************************************************/
760 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi,
761 libvlc_exception_t *p_e )
766 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
767 return (libvlc_drawable_t)p_mi->drawable.hwnd;
770 #elif defined(__APPLE__)
771 return p_mi->drawable.agl;
773 return p_mi->drawable.xid;
777 /**************************************************************************
778 * Getters for stream information
779 **************************************************************************/
780 libvlc_time_t libvlc_media_player_get_length(
781 libvlc_media_player_t *p_mi,
782 libvlc_exception_t *p_e )
784 input_thread_t *p_input_thread;
787 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
788 if( !p_input_thread )
791 var_Get( p_input_thread, "length", &val );
792 vlc_object_release( p_input_thread );
794 return (val.i_time+500LL)/1000LL;
797 libvlc_time_t libvlc_media_player_get_time(
798 libvlc_media_player_t *p_mi,
799 libvlc_exception_t *p_e )
801 input_thread_t *p_input_thread;
804 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
805 if( !p_input_thread )
808 var_Get( p_input_thread , "time", &val );
809 vlc_object_release( p_input_thread );
810 return (val.i_time+500LL)/1000LL;
813 void libvlc_media_player_set_time(
814 libvlc_media_player_t *p_mi,
816 libvlc_exception_t *p_e )
818 input_thread_t *p_input_thread;
820 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
821 if( !p_input_thread )
824 var_SetTime( p_input_thread, "time", time*1000LL );
825 vlc_object_release( p_input_thread );
828 void libvlc_media_player_set_position(
829 libvlc_media_player_t *p_mi,
831 libvlc_exception_t *p_e )
833 input_thread_t *p_input_thread;
835 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
836 if( !p_input_thread )
839 var_SetFloat( p_input_thread, "position", position );
840 vlc_object_release( p_input_thread );
843 float libvlc_media_player_get_position(
844 libvlc_media_player_t *p_mi,
845 libvlc_exception_t *p_e )
847 input_thread_t *p_input_thread;
850 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
851 if( !p_input_thread )
854 var_Get( p_input_thread, "position", &val );
855 vlc_object_release( p_input_thread );
860 void libvlc_media_player_set_chapter(
861 libvlc_media_player_t *p_mi,
863 libvlc_exception_t *p_e )
865 input_thread_t *p_input_thread;
867 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
868 if( !p_input_thread )
871 var_SetInteger( p_input_thread, "chapter", chapter );
872 vlc_object_release( p_input_thread );
875 int libvlc_media_player_get_chapter(
876 libvlc_media_player_t *p_mi,
877 libvlc_exception_t *p_e )
879 input_thread_t *p_input_thread;
882 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
883 if( !p_input_thread )
886 var_Get( p_input_thread, "chapter", &val );
887 vlc_object_release( p_input_thread );
892 int libvlc_media_player_get_chapter_count(
893 libvlc_media_player_t *p_mi,
894 libvlc_exception_t *p_e )
896 input_thread_t *p_input_thread;
899 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
900 if( !p_input_thread )
903 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
904 vlc_object_release( p_input_thread );
909 int libvlc_media_player_get_chapter_count_for_title(
910 libvlc_media_player_t *p_mi,
912 libvlc_exception_t *p_e )
914 input_thread_t *p_input_thread;
917 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
918 if( !p_input_thread )
922 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
924 vlc_object_release( p_input_thread );
927 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
928 vlc_object_release( p_input_thread );
934 void libvlc_media_player_set_title(
935 libvlc_media_player_t *p_mi,
937 libvlc_exception_t *p_e )
939 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_SetInteger( p_input_thread, "title", i_title );
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" );
1000 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1001 "next-chapter":"next-title", true );
1003 vlc_object_release( p_input_thread );
1006 void libvlc_media_player_previous_chapter(
1007 libvlc_media_player_t *p_mi,
1008 libvlc_exception_t *p_e )
1010 input_thread_t *p_input_thread;
1012 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1013 if( !p_input_thread )
1016 int i_type = var_Type( p_input_thread, "next-chapter" );
1017 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1018 "prev-chapter":"prev-title", true );
1020 vlc_object_release( p_input_thread );
1023 float libvlc_media_player_get_fps(
1024 libvlc_media_player_t *p_mi,
1025 libvlc_exception_t *p_e)
1027 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1030 if( p_input_thread )
1032 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1034 vlc_object_release( p_input_thread );
1039 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1040 libvlc_exception_t *p_e)
1042 input_thread_t *p_input_thread =
1043 libvlc_get_input_thread ( p_mi, p_e);
1044 if ( !p_input_thread )
1047 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1049 vlc_object_release( p_input_thread );
1052 vlc_object_release( p_input_thread );
1056 void libvlc_media_player_set_rate(
1057 libvlc_media_player_t *p_mi,
1059 libvlc_exception_t *p_e )
1061 input_thread_t *p_input_thread;
1064 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1065 if( !p_input_thread )
1068 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1069 if( (rate < 0.0) && !b_can_rewind )
1071 vlc_object_release( p_input_thread );
1072 libvlc_exception_raise( p_e, "Rate value is invalid" );
1076 var_SetInteger( p_input_thread, "rate", 1000.0f/rate );
1077 vlc_object_release( p_input_thread );
1080 float libvlc_media_player_get_rate(
1081 libvlc_media_player_t *p_mi,
1082 libvlc_exception_t *p_e )
1084 input_thread_t *p_input_thread;
1088 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1089 if( !p_input_thread )
1090 return 0.0; /* rate < 0 indicates rewind */
1092 var_Get( p_input_thread, "rate", &val );
1093 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1094 if( (val.i_int < 0) && !b_can_rewind )
1096 libvlc_exception_raise( p_e, "invalid rate" );
1099 vlc_object_release( p_input_thread );
1101 return (float)1000.0f/val.i_int;
1104 libvlc_state_t libvlc_media_player_get_state(
1105 libvlc_media_player_t *p_mi,
1106 libvlc_exception_t *p_e )
1108 input_thread_t *p_input_thread;
1109 libvlc_state_t state = libvlc_Ended;
1112 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1113 if( !p_input_thread )
1115 /* We do return the right value, no need to throw an exception */
1116 if( libvlc_exception_raised( p_e ) )
1117 libvlc_exception_clear( p_e );
1121 var_Get( p_input_thread, "state", &val );
1122 state = vlc_to_libvlc_state(val.i_int);
1124 if( state == libvlc_Playing )
1127 caching = var_GetFloat( p_input_thread, "cache" );
1128 if( caching > 0.0 && caching < 1.0 )
1129 state = libvlc_Buffering;
1131 vlc_object_release( p_input_thread );
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,
1178 &val_list, &text_list);
1179 vlc_object_release( p_input );
1180 libvlc_exception_raise( p_e, "no enough memory" );
1183 p_actual = p_track_description;
1185 for( int i = 0; i < val_list.p_list->i_count; i++ )
1189 p_actual = ( libvlc_track_description_t * )
1190 malloc( sizeof( libvlc_track_description_t ) );
1193 libvlc_track_description_release( p_track_description );
1194 var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1195 &val_list, &text_list);
1196 vlc_object_release( p_input );
1197 libvlc_exception_raise( p_e, "no enough memory" );
1201 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1202 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1203 p_actual->p_next = NULL;
1205 p_previous->p_next = p_actual;
1206 p_previous = p_actual;
1209 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1210 vlc_object_release( p_input );
1212 return p_track_description;
1215 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1217 libvlc_track_description_t *p_actual, *p_before;
1222 free( p_actual->psz_name );
1223 p_before = p_actual;
1224 p_actual = p_before->p_next;
1229 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1230 libvlc_exception_t *p_e )
1232 input_thread_t *p_input_thread;
1235 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1236 if ( !p_input_thread )
1238 /* We do return the right value, no need to throw an exception */
1239 if( libvlc_exception_raised( p_e ) )
1240 libvlc_exception_clear( p_e );
1243 var_Get( p_input_thread, "can-pause", &val );
1244 vlc_object_release( p_input_thread );