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 )
422 /**************************************************************************
423 * Set the Media descriptor associated with the instance.
425 * Enter without lock -- function will lock the object.
426 **************************************************************************/
427 void libvlc_media_player_set_media(
428 libvlc_media_player_t *p_mi,
429 libvlc_media_t *p_md,
430 libvlc_exception_t *p_e )
437 vlc_mutex_lock( &p_mi->object_lock );
439 /* FIXME I am not sure if it is a user request or on die(eof/error)
441 release_input_thread( p_mi,
442 p_mi->p_input_thread &&
443 !p_mi->p_input_thread->b_eof &&
444 !p_mi->p_input_thread->b_error );
447 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
449 libvlc_media_release( p_mi->p_md );
454 vlc_mutex_unlock( &p_mi->object_lock );
455 return; /* It is ok to pass a NULL md */
458 libvlc_media_retain( p_md );
461 /* The policy here is to ignore that we were created using a different
462 * libvlc_instance, because we don't really care */
463 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
465 vlc_mutex_unlock( &p_mi->object_lock );
468 /**************************************************************************
469 * Get the Media descriptor associated with the instance.
470 **************************************************************************/
472 libvlc_media_player_get_media(
473 libvlc_media_player_t *p_mi,
474 libvlc_exception_t *p_e )
481 libvlc_media_retain( p_mi->p_md );
485 /**************************************************************************
486 * Get the event Manager.
487 **************************************************************************/
488 libvlc_event_manager_t *
489 libvlc_media_player_event_manager(
490 libvlc_media_player_t *p_mi,
491 libvlc_exception_t *p_e )
495 return p_mi->p_event_manager;
498 /**************************************************************************
499 * Trigger a snapshot Taken Event.
500 *************************************************************************/
501 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
502 vlc_value_t oldval, vlc_value_t newval, void *p_data )
504 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
507 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
508 libvlc_event_t event ;
509 event.type = libvlc_MediaPlayerSnapshotTaken ;
510 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
511 /* Snapshot psz data is a vlc_variable owned by libvlc object .
512 Its memmory management is taken care by the obj*/
513 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p"
514 " (%s)", event.u.media_player_snapshot_taken.psz_filename,
515 event.u.media_player_snapshot_taken.psz_filename );
516 libvlc_event_send( p_mi->p_event_manager, &event );
521 /**************************************************************************
522 * Tell media player to start playing.
523 **************************************************************************/
524 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
525 libvlc_exception_t *p_e )
527 input_thread_t * p_input_thread;
529 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
531 /* A thread already exists, send it a play message */
532 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
533 vlc_object_release( p_input_thread );
537 /* Ignore previous exception */
538 libvlc_exception_clear( p_e );
540 vlc_mutex_lock( &p_mi->object_lock );
544 libvlc_exception_raise( p_e, "no associated media descriptor" );
545 vlc_mutex_unlock( &p_mi->object_lock );
549 p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
550 p_mi->p_md->p_input_item, NULL, NULL );
552 if( !p_mi->p_input_thread )
554 vlc_mutex_unlock( &p_mi->object_lock );
558 p_input_thread = p_mi->p_input_thread;
560 var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
561 if( p_mi->drawable.agl )
562 var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
564 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
565 if( p_mi->drawable.xid )
566 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
568 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
569 if( p_mi->drawable.hwnd != NULL )
571 vlc_value_t val = { .p_address = p_mi->drawable.hwnd };
572 var_Set( p_input_thread, "drawable-hwnd", val );
575 var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
576 if( p_mi->drawable.nsobject != NULL )
578 vlc_value_t val = { .p_address = p_mi->drawable.nsobject };
579 var_Set( p_input_thread, "drawable-nsobject", val );
582 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
583 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
584 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
586 if( input_Start( p_input_thread ) )
588 vlc_object_release( p_input_thread );
589 p_mi->p_input_thread = NULL;
592 vlc_mutex_unlock( &p_mi->object_lock );
595 /**************************************************************************
597 **************************************************************************/
598 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
599 libvlc_exception_t *p_e )
601 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
602 if( !p_input_thread )
605 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
606 if( state == libvlc_Playing || state == libvlc_Buffering )
608 if( libvlc_media_player_can_pause( p_mi, p_e ) )
609 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
611 libvlc_media_player_stop( p_mi, p_e );
614 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
616 vlc_object_release( p_input_thread );
619 /**************************************************************************
620 * Tells whether the media player is currently playing.
622 * Enter with lock held.
623 **************************************************************************/
624 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
625 libvlc_exception_t *p_e )
627 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
628 return (libvlc_Playing == state) || (libvlc_Buffering == state);
631 /**************************************************************************
633 **************************************************************************/
634 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
635 libvlc_exception_t *p_e )
637 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
639 if( state == libvlc_Playing ||
640 state == libvlc_Paused ||
641 state == libvlc_Buffering )
643 /* Send a stop notification event only if we are in playing,
644 * buffering or paused states */
645 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
647 /* Construct and send the event */
648 libvlc_event_t event;
649 event.type = libvlc_MediaPlayerStopped;
650 libvlc_event_send( p_mi->p_event_manager, &event );
653 vlc_mutex_lock( &p_mi->object_lock );
654 release_input_thread( p_mi, true ); /* This will stop the input thread */
655 vlc_mutex_unlock( &p_mi->object_lock );
658 /**************************************************************************
660 **************************************************************************/
661 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
663 libvlc_exception_t *p_e )
666 p_mi->drawable.nsobject = drawable;
669 /**************************************************************************
671 **************************************************************************/
672 uint32_t libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
674 return p_mi->drawable.nsobject;
677 /**************************************************************************
679 **************************************************************************/
680 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
682 libvlc_exception_t *p_e )
685 p_mi->drawable.agl = drawable;
688 /**************************************************************************
690 **************************************************************************/
691 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
693 return p_mi->drawable.agl;
696 /**************************************************************************
698 **************************************************************************/
699 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
701 libvlc_exception_t *p_e )
704 p_mi->drawable.xid = drawable;
707 /**************************************************************************
709 **************************************************************************/
710 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
712 return p_mi->drawable.xid;
715 /**************************************************************************
717 **************************************************************************/
718 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
720 libvlc_exception_t *p_e )
723 p_mi->drawable.hwnd = drawable;
726 /**************************************************************************
728 **************************************************************************/
729 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
731 return p_mi->drawable.hwnd;
734 /**************************************************************************
736 **************************************************************************/
737 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
738 libvlc_drawable_t drawable,
739 libvlc_exception_t *p_e )
742 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
743 p_mi->drawable.hwnd = (HWND)drawable;
745 libvlc_exception_raise(p_e, "Operation not supported");
746 #elif defined(__APPLE__)
747 p_mi->drawable.agl = drawable;
750 p_mi->drawable.xid = drawable;
755 /**************************************************************************
757 **************************************************************************/
759 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi,
760 libvlc_exception_t *p_e )
765 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
766 return (libvlc_drawable_t)p_mi->drawable.hwnd;
769 #elif defined(__APPLE__)
770 return p_mi->drawable.agl;
772 return p_mi->drawable.xid;
776 /**************************************************************************
777 * Getters for stream information
778 **************************************************************************/
779 libvlc_time_t libvlc_media_player_get_length(
780 libvlc_media_player_t *p_mi,
781 libvlc_exception_t *p_e )
783 input_thread_t *p_input_thread;
786 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
787 if( !p_input_thread )
790 var_Get( p_input_thread, "length", &val );
791 vlc_object_release( p_input_thread );
793 return (val.i_time+500LL)/1000LL;
796 libvlc_time_t libvlc_media_player_get_time(
797 libvlc_media_player_t *p_mi,
798 libvlc_exception_t *p_e )
800 input_thread_t *p_input_thread;
803 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
804 if( !p_input_thread )
807 var_Get( p_input_thread , "time", &val );
808 vlc_object_release( p_input_thread );
809 return (val.i_time+500LL)/1000LL;
812 void libvlc_media_player_set_time(
813 libvlc_media_player_t *p_mi,
815 libvlc_exception_t *p_e )
817 input_thread_t *p_input_thread;
819 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
820 if( !p_input_thread )
823 var_SetTime( p_input_thread, "time", time*1000LL );
824 vlc_object_release( p_input_thread );
827 void libvlc_media_player_set_position(
828 libvlc_media_player_t *p_mi,
830 libvlc_exception_t *p_e )
832 input_thread_t *p_input_thread;
834 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
835 if( !p_input_thread )
838 var_SetFloat( p_input_thread, "position", position );
839 vlc_object_release( p_input_thread );
842 float libvlc_media_player_get_position(
843 libvlc_media_player_t *p_mi,
844 libvlc_exception_t *p_e )
846 input_thread_t *p_input_thread;
849 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
850 if( !p_input_thread )
853 var_Get( p_input_thread, "position", &val );
854 vlc_object_release( p_input_thread );
859 void libvlc_media_player_set_chapter(
860 libvlc_media_player_t *p_mi,
862 libvlc_exception_t *p_e )
864 input_thread_t *p_input_thread;
866 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
867 if( !p_input_thread )
870 var_SetInteger( p_input_thread, "chapter", chapter );
871 vlc_object_release( p_input_thread );
874 int libvlc_media_player_get_chapter(
875 libvlc_media_player_t *p_mi,
876 libvlc_exception_t *p_e )
878 input_thread_t *p_input_thread;
881 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
882 if( !p_input_thread )
885 var_Get( p_input_thread, "chapter", &val );
886 vlc_object_release( p_input_thread );
891 int libvlc_media_player_get_chapter_count(
892 libvlc_media_player_t *p_mi,
893 libvlc_exception_t *p_e )
895 input_thread_t *p_input_thread;
898 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
899 if( !p_input_thread )
902 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
903 vlc_object_release( p_input_thread );
908 int libvlc_media_player_get_chapter_count_for_title(
909 libvlc_media_player_t *p_mi,
911 libvlc_exception_t *p_e )
913 input_thread_t *p_input_thread;
916 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
917 if( !p_input_thread )
921 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
923 vlc_object_release( p_input_thread );
926 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
927 vlc_object_release( p_input_thread );
933 void libvlc_media_player_set_title(
934 libvlc_media_player_t *p_mi,
936 libvlc_exception_t *p_e )
938 input_thread_t *p_input_thread;
940 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
941 if( !p_input_thread )
944 var_SetInteger( p_input_thread, "title", i_title );
945 vlc_object_release( p_input_thread );
948 libvlc_event_t event;
949 event.type = libvlc_MediaPlayerTitleChanged;
950 event.u.media_player_title_changed.new_title = i_title;
951 libvlc_event_send( p_mi->p_event_manager, &event );
954 int libvlc_media_player_get_title(
955 libvlc_media_player_t *p_mi,
956 libvlc_exception_t *p_e )
958 input_thread_t *p_input_thread;
961 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
962 if( !p_input_thread )
965 var_Get( p_input_thread, "title", &val );
966 vlc_object_release( p_input_thread );
971 int libvlc_media_player_get_title_count(
972 libvlc_media_player_t *p_mi,
973 libvlc_exception_t *p_e )
975 input_thread_t *p_input_thread;
978 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
979 if( !p_input_thread )
982 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
983 vlc_object_release( p_input_thread );
988 void libvlc_media_player_next_chapter(
989 libvlc_media_player_t *p_mi,
990 libvlc_exception_t *p_e )
992 input_thread_t *p_input_thread;
994 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
995 if( !p_input_thread )
998 int i_type = var_Type( p_input_thread, "next-chapter" );
999 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1000 "next-chapter":"next-title", true );
1002 vlc_object_release( p_input_thread );
1005 void libvlc_media_player_previous_chapter(
1006 libvlc_media_player_t *p_mi,
1007 libvlc_exception_t *p_e )
1009 input_thread_t *p_input_thread;
1011 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1012 if( !p_input_thread )
1015 int i_type = var_Type( p_input_thread, "next-chapter" );
1016 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1017 "prev-chapter":"prev-title", true );
1019 vlc_object_release( p_input_thread );
1022 float libvlc_media_player_get_fps(
1023 libvlc_media_player_t *p_mi,
1024 libvlc_exception_t *p_e)
1026 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1029 if( p_input_thread )
1031 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1033 vlc_object_release( p_input_thread );
1038 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1039 libvlc_exception_t *p_e)
1041 input_thread_t *p_input_thread =
1042 libvlc_get_input_thread ( p_mi, p_e);
1043 if ( !p_input_thread )
1046 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1048 vlc_object_release( p_input_thread );
1051 vlc_object_release( p_input_thread );
1055 void libvlc_media_player_set_rate(
1056 libvlc_media_player_t *p_mi,
1058 libvlc_exception_t *p_e )
1060 input_thread_t *p_input_thread;
1063 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1064 if( !p_input_thread )
1067 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1068 if( (rate < 0.0) && !b_can_rewind )
1070 vlc_object_release( p_input_thread );
1071 libvlc_exception_raise( p_e, "Rate value is invalid" );
1075 var_SetInteger( p_input_thread, "rate", 1000.0f/rate );
1076 vlc_object_release( p_input_thread );
1079 float libvlc_media_player_get_rate(
1080 libvlc_media_player_t *p_mi,
1081 libvlc_exception_t *p_e )
1083 input_thread_t *p_input_thread;
1087 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1088 if( !p_input_thread )
1089 return 0.0; /* rate < 0 indicates rewind */
1091 var_Get( p_input_thread, "rate", &val );
1092 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1093 if( (val.i_int < 0) && !b_can_rewind )
1095 libvlc_exception_raise( p_e, "invalid rate" );
1098 vlc_object_release( p_input_thread );
1100 return (float)1000.0f/val.i_int;
1103 libvlc_state_t libvlc_media_player_get_state(
1104 libvlc_media_player_t *p_mi,
1105 libvlc_exception_t *p_e )
1107 input_thread_t *p_input_thread;
1108 libvlc_state_t state = libvlc_Ended;
1111 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1112 if( !p_input_thread )
1114 /* We do return the right value, no need to throw an exception */
1115 if( libvlc_exception_raised( p_e ) )
1116 libvlc_exception_clear( p_e );
1120 var_Get( p_input_thread, "state", &val );
1121 state = vlc_to_libvlc_state(val.i_int);
1123 if( state == libvlc_Playing )
1126 caching = var_GetFloat( p_input_thread, "cache" );
1127 if( caching > 0.0 && caching < 1.0 )
1128 state = libvlc_Buffering;
1130 vlc_object_release( p_input_thread );
1134 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1135 libvlc_exception_t *p_e )
1137 input_thread_t *p_input_thread;
1140 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1141 if ( !p_input_thread )
1143 /* We do return the right value, no need to throw an exception */
1144 if( libvlc_exception_raised( p_e ) )
1145 libvlc_exception_clear( p_e );
1148 var_Get( p_input_thread, "can-seek", &val );
1149 vlc_object_release( p_input_thread );
1154 /* internal function, used by audio, video */
1155 libvlc_track_description_t *
1156 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1157 const char *psz_variable,
1158 libvlc_exception_t *p_e )
1160 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1165 vlc_value_t val_list, text_list;
1166 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1168 if( val_list.p_list->i_count <= 0 ) /* no tracks */
1171 libvlc_track_description_t *p_track_description, *p_actual, *p_previous;
1172 p_track_description = ( libvlc_track_description_t * )
1173 malloc( sizeof( libvlc_track_description_t ) );
1174 if ( !p_track_description )
1176 var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1177 &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,
1194 &val_list, &text_list);
1195 vlc_object_release( p_input );
1196 libvlc_exception_raise( p_e, "no enough memory" );
1200 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1201 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1202 p_actual->p_next = NULL;
1204 p_previous->p_next = p_actual;
1205 p_previous = p_actual;
1208 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1209 vlc_object_release( p_input );
1211 return p_track_description;
1214 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1216 libvlc_track_description_t *p_actual, *p_before;
1221 free( p_actual->psz_name );
1222 p_before = p_actual;
1223 p_actual = p_before->p_next;
1228 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1229 libvlc_exception_t *p_e )
1231 input_thread_t *p_input_thread;
1234 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1235 if ( !p_input_thread )
1237 /* We do return the right value, no need to throw an exception */
1238 if( libvlc_exception_raised( p_e ) )
1239 libvlc_exception_clear( p_e );
1242 var_Get( p_input_thread, "can-pause", &val );
1243 vlc_object_release( p_input_thread );