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 /* No one is tracking this input_thread apart from us. Destroy it. */
82 if( p_mi->b_own_its_input_thread )
84 var_DelCallback( p_input_thread, "can-seek",
85 input_seekable_changed, p_mi );
86 var_DelCallback( p_input_thread, "can-pause",
87 input_pausable_changed, p_mi );
88 var_DelCallback( p_input_thread, "intf-event",
89 input_event_changed, p_mi );
91 /* We owned this one */
92 input_Stop( p_input_thread, b_input_abort );
93 vlc_thread_join( p_input_thread );
95 var_Destroy( p_input_thread, "drawable-hwnd" );
96 var_Destroy( p_input_thread, "drawable-xid" );
97 var_Destroy( p_input_thread, "drawable-agl" );
100 vlc_object_release( p_input_thread );
102 p_mi->p_input_thread = NULL;
106 * Retrieve the input thread. Be sure to release the object
107 * once you are done with it. (libvlc Internal)
109 * Function will lock the object.
111 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
112 libvlc_exception_t *p_e )
114 input_thread_t *p_input_thread;
116 if( !p_mi ) RAISENULL( "Media Instance is NULL" );
118 vlc_mutex_lock( &p_mi->object_lock );
120 if( !p_mi->p_input_thread )
122 vlc_mutex_unlock( &p_mi->object_lock );
123 RAISENULL( "Input is NULL" );
126 p_input_thread = p_mi->p_input_thread;
127 vlc_object_hold( p_input_thread );
129 vlc_mutex_unlock( &p_mi->object_lock );
131 return p_input_thread;
135 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
136 vlc_value_t oldval, vlc_value_t newval,
142 libvlc_media_player_t * p_mi = p_userdata;
143 libvlc_event_t event;
145 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
146 event.type = libvlc_MediaPlayerSeekableChanged;
147 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
149 libvlc_event_send( p_mi->p_event_manager, &event );
154 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
155 vlc_value_t oldval, vlc_value_t newval,
161 libvlc_media_player_t * p_mi = p_userdata;
162 libvlc_event_t event;
164 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
165 event.type = libvlc_MediaPlayerPausableChanged;
166 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
168 libvlc_event_send( p_mi->p_event_manager, &event );
173 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
174 vlc_value_t oldval, vlc_value_t newval,
178 input_thread_t * p_input = (input_thread_t *)p_this;
179 libvlc_media_player_t * p_mi = p_userdata;
180 libvlc_event_t event;
182 assert( !strcmp( psz_cmd, "intf-event" ) );
184 if( newval.i_int == INPUT_EVENT_STATE )
186 libvlc_state_t libvlc_state;
188 switch ( var_GetInteger( p_input, "state" ) )
191 libvlc_state = libvlc_NothingSpecial;
192 event.type = libvlc_MediaPlayerNothingSpecial;
195 libvlc_state = libvlc_Opening;
196 event.type = libvlc_MediaPlayerOpening;
199 libvlc_state = libvlc_Playing;
200 event.type = libvlc_MediaPlayerPlaying;
203 libvlc_state = libvlc_Paused;
204 event.type = libvlc_MediaPlayerPaused;
207 libvlc_state = libvlc_Ended;
208 event.type = libvlc_MediaPlayerEndReached;
211 libvlc_state = libvlc_Error;
212 event.type = libvlc_MediaPlayerEncounteredError;
219 libvlc_media_set_state( p_mi->p_md, libvlc_state, NULL );
220 libvlc_event_send( p_mi->p_event_manager, &event );
222 else if( newval.i_int == INPUT_EVENT_POSITION )
224 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
225 return VLC_SUCCESS; /* Don't send the position while stopped */
228 event.type = libvlc_MediaPlayerPositionChanged;
229 event.u.media_player_position_changed.new_position =
230 var_GetFloat( p_input, "position" );
231 libvlc_event_send( p_mi->p_event_manager, &event );
234 event.type = libvlc_MediaPlayerTimeChanged;
235 event.u.media_player_time_changed.new_time =
236 var_GetTime( p_input, "time" );
237 libvlc_event_send( p_mi->p_event_manager, &event );
244 /**************************************************************************
245 * Create a Media Instance object.
248 * - All items created by _new start with a refcount set to 1.
249 * - Accessor _release decrease the refcount by 1, if after that
250 * operation the refcount is 0, the object is destroyed.
251 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
253 * Object locking strategy:
254 * - No lock held while in constructor.
255 * - When accessing any member variable this lock is held. (XXX who locks?)
256 * - When attempting to destroy the object the lock is also held.
257 **************************************************************************/
258 libvlc_media_player_t *
259 libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
260 libvlc_exception_t * p_e )
262 libvlc_media_player_t * p_mi;
264 if( !p_libvlc_instance )
266 libvlc_exception_raise( p_e, "invalid libvlc instance" );
270 p_mi = malloc( sizeof(libvlc_media_player_t) );
273 libvlc_exception_raise( p_e, "Not enough memory" );
277 p_mi->drawable.agl = 0;
278 p_mi->drawable.xid = 0;
279 p_mi->drawable.hwnd = NULL;
280 p_mi->p_libvlc_instance = p_libvlc_instance;
281 p_mi->p_input_thread = NULL;
282 p_mi->i_refcount = 1;
283 p_mi->b_own_its_input_thread = true;
284 vlc_mutex_init( &p_mi->object_lock );
285 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
286 p_libvlc_instance, p_e );
287 if( libvlc_exception_raised( p_e ) )
289 vlc_mutex_destroy( &p_mi->object_lock );
294 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
295 libvlc_MediaPlayerNothingSpecial, p_e );
296 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
297 libvlc_MediaPlayerOpening, p_e );
298 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
299 libvlc_MediaPlayerBuffering, p_e );
300 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
301 libvlc_MediaPlayerPlaying, p_e );
302 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
303 libvlc_MediaPlayerPaused, p_e );
304 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
305 libvlc_MediaPlayerStopped, p_e );
306 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
307 libvlc_MediaPlayerForward, p_e );
308 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
309 libvlc_MediaPlayerBackward, p_e );
310 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
311 libvlc_MediaPlayerEndReached, p_e );
312 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
313 libvlc_MediaPlayerEncounteredError, p_e );
315 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
316 libvlc_MediaPlayerPositionChanged, p_e );
317 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
318 libvlc_MediaPlayerTimeChanged, p_e );
319 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
320 libvlc_MediaPlayerTitleChanged, p_e );
321 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
322 libvlc_MediaPlayerSeekableChanged, p_e );
323 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
324 libvlc_MediaPlayerPausableChanged, p_e );
326 /* Snapshot initialization */
327 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
328 libvlc_MediaPlayerSnapshotTaken, p_e );
329 /* Attach a var callback to the global object to provide the glue between
330 vout_thread that generates the event and media_player that re-emits it
331 with its own event manager
333 var_Create( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
334 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
335 var_AddCallback( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
336 SnapshotTakenCallback, p_mi );
341 /**************************************************************************
342 * Create a Media Instance object with a media descriptor.
343 **************************************************************************/
344 libvlc_media_player_t *
345 libvlc_media_player_new_from_media(
346 libvlc_media_t * p_md,
347 libvlc_exception_t *p_e )
349 libvlc_media_player_t * p_mi;
350 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
355 libvlc_media_retain( p_md );
361 /**************************************************************************
362 * Create a new media instance object from an input_thread (Libvlc Internal).
363 **************************************************************************/
364 libvlc_media_player_t * libvlc_media_player_new_from_input_thread(
365 struct libvlc_instance_t *p_libvlc_instance,
366 input_thread_t *p_input,
367 libvlc_exception_t *p_e )
369 libvlc_media_player_t * p_mi;
373 libvlc_exception_raise( p_e, "invalid input thread" );
377 p_mi = libvlc_media_player_new( p_libvlc_instance, p_e );
382 p_mi->p_md = libvlc_media_new_from_input_item(
384 input_GetItem( p_input ), p_e );
388 libvlc_media_player_destroy( p_mi );
392 /* will be released in media_player_release() */
393 vlc_object_hold( p_input );
395 p_mi->p_input_thread = p_input;
396 p_mi->b_own_its_input_thread = false;
401 /**************************************************************************
402 * Destroy a Media Instance object (libvlc internal)
404 * Warning: No lock held here, but hey, this is internal. Caller must lock.
405 **************************************************************************/
406 void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
408 input_thread_t *p_input_thread;
409 libvlc_exception_t p_e;
411 libvlc_exception_init( &p_e );
416 /* Detach Callback from the main libvlc object */
417 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
418 "vout-snapshottaken", SnapshotTakenCallback, p_mi );
420 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
422 if( libvlc_exception_raised( &p_e ) )
424 libvlc_event_manager_release( p_mi->p_event_manager );
425 libvlc_exception_clear( &p_e );
427 return; /* no need to worry about no input thread */
429 vlc_mutex_destroy( &p_mi->object_lock );
431 vlc_object_release( p_input_thread );
433 libvlc_media_release( p_mi->p_md );
438 /**************************************************************************
439 * Release a Media Instance object.
441 * Function does the locking.
442 **************************************************************************/
443 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
448 vlc_mutex_lock( &p_mi->object_lock );
452 if( p_mi->i_refcount > 0 )
454 vlc_mutex_unlock( &p_mi->object_lock );
457 vlc_mutex_unlock( &p_mi->object_lock );
458 vlc_mutex_destroy( &p_mi->object_lock );
460 release_input_thread( p_mi, true );
462 libvlc_event_manager_release( p_mi->p_event_manager );
464 libvlc_media_release( p_mi->p_md );
469 /**************************************************************************
470 * Retain a Media Instance object.
472 * Caller must hold the lock.
473 **************************************************************************/
474 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
482 /**************************************************************************
483 * Set the Media descriptor associated with the instance.
485 * Enter without lock -- function will lock the object.
486 **************************************************************************/
487 void libvlc_media_player_set_media(
488 libvlc_media_player_t *p_mi,
489 libvlc_media_t *p_md,
490 libvlc_exception_t *p_e )
497 vlc_mutex_lock( &p_mi->object_lock );
499 /* FIXME I am not sure if it is a user request or on die(eof/error)
501 release_input_thread( p_mi,
502 p_mi->p_input_thread &&
503 !p_mi->p_input_thread->b_eof &&
504 !p_mi->p_input_thread->b_error );
507 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
509 libvlc_media_release( p_mi->p_md );
514 vlc_mutex_unlock( &p_mi->object_lock );
515 return; /* It is ok to pass a NULL md */
518 libvlc_media_retain( p_md );
521 /* The policy here is to ignore that we were created using a different
522 * libvlc_instance, because we don't really care */
523 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
525 vlc_mutex_unlock( &p_mi->object_lock );
528 /**************************************************************************
529 * Get the Media descriptor associated with the instance.
530 **************************************************************************/
532 libvlc_media_player_get_media(
533 libvlc_media_player_t *p_mi,
534 libvlc_exception_t *p_e )
541 libvlc_media_retain( p_mi->p_md );
545 /**************************************************************************
546 * Get the event Manager.
547 **************************************************************************/
548 libvlc_event_manager_t *
549 libvlc_media_player_event_manager(
550 libvlc_media_player_t *p_mi,
551 libvlc_exception_t *p_e )
555 return p_mi->p_event_manager;
558 /**************************************************************************
559 * Trigger a snapshot Taken Event.
560 *************************************************************************/
561 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
562 vlc_value_t oldval, vlc_value_t newval, void *p_data )
564 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
567 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
568 libvlc_event_t event ;
569 event.type = libvlc_MediaPlayerSnapshotTaken ;
570 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
571 /* Snapshot psz data is a vlc_variable owned by libvlc object .
572 Its memmory management is taken care by the obj*/
573 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p"
574 " (%s)", event.u.media_player_snapshot_taken.psz_filename,
575 event.u.media_player_snapshot_taken.psz_filename );
576 libvlc_event_send( p_mi->p_event_manager, &event );
581 /**************************************************************************
582 * Tell media player to start playing.
583 **************************************************************************/
584 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
585 libvlc_exception_t *p_e )
587 input_thread_t * p_input_thread;
589 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
591 /* A thread already exists, send it a play message */
592 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
593 vlc_object_release( p_input_thread );
597 /* Ignore previous exception */
598 libvlc_exception_clear( p_e );
600 vlc_mutex_lock( &p_mi->object_lock );
604 libvlc_exception_raise( p_e, "no associated media descriptor" );
605 vlc_mutex_unlock( &p_mi->object_lock );
609 p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
610 p_mi->p_md->p_input_item, NULL, NULL );
612 if( !p_mi->p_input_thread )
614 vlc_mutex_unlock( &p_mi->object_lock );
618 p_input_thread = p_mi->p_input_thread;
620 var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
621 if( p_mi->drawable.agl )
622 var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
624 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
625 if( p_mi->drawable.xid )
626 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
628 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
629 if( p_mi->drawable.hwnd != NULL )
631 vlc_value_t val = { .p_address = p_mi->drawable.hwnd };
632 var_Set( p_input_thread, "drawable-hwnd", val );
635 var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
636 if( p_mi->drawable.nsobject != NULL )
638 vlc_value_t val = { .p_address = p_mi->drawable.nsobject };
639 var_Set( p_input_thread, "drawable-nsobject", val );
642 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
643 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
644 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
646 if( input_Start( p_input_thread ) )
648 vlc_object_release( p_input_thread );
649 p_mi->p_input_thread = NULL;
652 vlc_mutex_unlock( &p_mi->object_lock );
655 /**************************************************************************
657 **************************************************************************/
658 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
659 libvlc_exception_t *p_e )
661 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
662 if( !p_input_thread )
665 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
666 if( state == libvlc_Playing || state == libvlc_Buffering )
668 if( libvlc_media_player_can_pause( p_mi, p_e ) )
669 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
671 libvlc_media_player_stop( p_mi, p_e );
674 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
676 vlc_object_release( p_input_thread );
679 /**************************************************************************
680 * Tells whether the media player is currently playing.
682 * Enter with lock held.
683 **************************************************************************/
684 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
685 libvlc_exception_t *p_e )
687 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
688 return (libvlc_Playing == state) || (libvlc_Buffering == state);
691 /**************************************************************************
693 **************************************************************************/
694 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
695 libvlc_exception_t *p_e )
697 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
699 if( state == libvlc_Playing ||
700 state == libvlc_Paused ||
701 state == libvlc_Buffering )
703 /* Send a stop notification event only if we are in playing,
704 * buffering or paused states */
705 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
707 /* Construct and send the event */
708 libvlc_event_t event;
709 event.type = libvlc_MediaPlayerStopped;
710 libvlc_event_send( p_mi->p_event_manager, &event );
713 if( p_mi->b_own_its_input_thread )
715 vlc_mutex_lock( &p_mi->object_lock );
716 release_input_thread( p_mi, true ); /* This will stop the input thread */
717 vlc_mutex_unlock( &p_mi->object_lock );
721 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
723 if( !p_input_thread )
726 input_Stop( p_input_thread, true );
727 vlc_object_release( p_input_thread );
728 p_mi->p_input_thread = NULL;
732 /**************************************************************************
734 **************************************************************************/
735 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
737 libvlc_exception_t *p_e )
740 p_mi->drawable.nsobject = drawable;
743 /**************************************************************************
745 **************************************************************************/
746 uint32_t libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
748 return p_mi->drawable.nsobject;
751 /**************************************************************************
753 **************************************************************************/
754 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
756 libvlc_exception_t *p_e )
759 p_mi->drawable.agl = drawable;
762 /**************************************************************************
764 **************************************************************************/
765 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
767 return p_mi->drawable.agl;
770 /**************************************************************************
772 **************************************************************************/
773 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
775 libvlc_exception_t *p_e )
778 p_mi->drawable.xid = drawable;
781 /**************************************************************************
783 **************************************************************************/
784 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
786 return p_mi->drawable.xid;
789 /**************************************************************************
791 **************************************************************************/
792 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
794 libvlc_exception_t *p_e )
797 p_mi->drawable.hwnd = drawable;
800 /**************************************************************************
802 **************************************************************************/
803 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
805 return p_mi->drawable.hwnd;
808 /**************************************************************************
810 **************************************************************************/
811 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
812 libvlc_drawable_t drawable,
813 libvlc_exception_t *p_e )
816 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
817 p_mi->drawable.hwnd = (HWND)drawable;
819 libvlc_exception_raise(p_e, "Operation not supported");
820 #elif defined(__APPLE__)
821 p_mi->drawable.agl = drawable;
824 p_mi->drawable.xid = drawable;
829 /**************************************************************************
831 **************************************************************************/
833 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi,
834 libvlc_exception_t *p_e )
839 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
840 return (libvlc_drawable_t)p_mi->drawable.hwnd;
843 #elif defined(__APPLE__)
844 return p_mi->drawable.agl;
846 return p_mi->drawable.xid;
850 /**************************************************************************
851 * Getters for stream information
852 **************************************************************************/
853 libvlc_time_t libvlc_media_player_get_length(
854 libvlc_media_player_t *p_mi,
855 libvlc_exception_t *p_e )
857 input_thread_t *p_input_thread;
860 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
861 if( !p_input_thread )
864 var_Get( p_input_thread, "length", &val );
865 vlc_object_release( p_input_thread );
867 return (val.i_time+500LL)/1000LL;
870 libvlc_time_t libvlc_media_player_get_time(
871 libvlc_media_player_t *p_mi,
872 libvlc_exception_t *p_e )
874 input_thread_t *p_input_thread;
877 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
878 if( !p_input_thread )
881 var_Get( p_input_thread , "time", &val );
882 vlc_object_release( p_input_thread );
883 return (val.i_time+500LL)/1000LL;
886 void libvlc_media_player_set_time(
887 libvlc_media_player_t *p_mi,
889 libvlc_exception_t *p_e )
891 input_thread_t *p_input_thread;
893 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
894 if( !p_input_thread )
897 var_SetTime( p_input_thread, "time", time*1000LL );
898 vlc_object_release( p_input_thread );
901 void libvlc_media_player_set_position(
902 libvlc_media_player_t *p_mi,
904 libvlc_exception_t *p_e )
906 input_thread_t *p_input_thread;
908 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
909 if( !p_input_thread )
912 var_SetFloat( p_input_thread, "position", position );
913 vlc_object_release( p_input_thread );
916 float libvlc_media_player_get_position(
917 libvlc_media_player_t *p_mi,
918 libvlc_exception_t *p_e )
920 input_thread_t *p_input_thread;
923 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
924 if( !p_input_thread )
927 var_Get( p_input_thread, "position", &val );
928 vlc_object_release( p_input_thread );
933 void libvlc_media_player_set_chapter(
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, "chapter", chapter );
945 vlc_object_release( p_input_thread );
948 int libvlc_media_player_get_chapter(
949 libvlc_media_player_t *p_mi,
950 libvlc_exception_t *p_e )
952 input_thread_t *p_input_thread;
955 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
956 if( !p_input_thread )
959 var_Get( p_input_thread, "chapter", &val );
960 vlc_object_release( p_input_thread );
965 int libvlc_media_player_get_chapter_count(
966 libvlc_media_player_t *p_mi,
967 libvlc_exception_t *p_e )
969 input_thread_t *p_input_thread;
972 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
973 if( !p_input_thread )
976 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
977 vlc_object_release( p_input_thread );
982 int libvlc_media_player_get_chapter_count_for_title(
983 libvlc_media_player_t *p_mi,
985 libvlc_exception_t *p_e )
987 input_thread_t *p_input_thread;
990 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
991 if( !p_input_thread )
995 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
997 vlc_object_release( p_input_thread );
1000 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1001 vlc_object_release( p_input_thread );
1007 void libvlc_media_player_set_title(
1008 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 var_SetInteger( p_input_thread, "title", i_title );
1019 vlc_object_release( p_input_thread );
1022 libvlc_event_t event;
1023 event.type = libvlc_MediaPlayerTitleChanged;
1024 event.u.media_player_title_changed.new_title = i_title;
1025 libvlc_event_send( p_mi->p_event_manager, &event );
1028 int libvlc_media_player_get_title(
1029 libvlc_media_player_t *p_mi,
1030 libvlc_exception_t *p_e )
1032 input_thread_t *p_input_thread;
1035 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1036 if( !p_input_thread )
1039 var_Get( p_input_thread, "title", &val );
1040 vlc_object_release( p_input_thread );
1045 int libvlc_media_player_get_title_count(
1046 libvlc_media_player_t *p_mi,
1047 libvlc_exception_t *p_e )
1049 input_thread_t *p_input_thread;
1052 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1053 if( !p_input_thread )
1056 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1057 vlc_object_release( p_input_thread );
1062 void libvlc_media_player_next_chapter(
1063 libvlc_media_player_t *p_mi,
1064 libvlc_exception_t *p_e )
1066 input_thread_t *p_input_thread;
1068 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1069 if( !p_input_thread )
1072 int i_type = var_Type( p_input_thread, "next-chapter" );
1073 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1074 "next-chapter":"next-title", true );
1076 vlc_object_release( p_input_thread );
1079 void libvlc_media_player_previous_chapter(
1080 libvlc_media_player_t *p_mi,
1081 libvlc_exception_t *p_e )
1083 input_thread_t *p_input_thread;
1085 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1086 if( !p_input_thread )
1089 int i_type = var_Type( p_input_thread, "next-chapter" );
1090 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1091 "prev-chapter":"prev-title", true );
1093 vlc_object_release( p_input_thread );
1096 float libvlc_media_player_get_fps(
1097 libvlc_media_player_t *p_mi,
1098 libvlc_exception_t *p_e)
1100 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1103 if( p_input_thread )
1105 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1107 vlc_object_release( p_input_thread );
1112 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1113 libvlc_exception_t *p_e)
1115 input_thread_t *p_input_thread =
1116 libvlc_get_input_thread ( p_mi, p_e);
1117 if ( !p_input_thread )
1120 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1122 vlc_object_release( p_input_thread );
1125 vlc_object_release( p_input_thread );
1129 void libvlc_media_player_set_rate(
1130 libvlc_media_player_t *p_mi,
1132 libvlc_exception_t *p_e )
1134 input_thread_t *p_input_thread;
1137 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1138 if( !p_input_thread )
1141 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1142 if( (rate < 0.0) && !b_can_rewind )
1144 vlc_object_release( p_input_thread );
1145 libvlc_exception_raise( p_e, "Rate value is invalid" );
1149 var_SetInteger( p_input_thread, "rate", 1000.0f/rate );
1150 vlc_object_release( p_input_thread );
1153 float libvlc_media_player_get_rate(
1154 libvlc_media_player_t *p_mi,
1155 libvlc_exception_t *p_e )
1157 input_thread_t *p_input_thread;
1161 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1162 if( !p_input_thread )
1163 return 0.0; /* rate < 0 indicates rewind */
1165 var_Get( p_input_thread, "rate", &val );
1166 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1167 if( (val.i_int < 0) && !b_can_rewind )
1169 libvlc_exception_raise( p_e, "invalid rate" );
1172 vlc_object_release( p_input_thread );
1174 return (float)1000.0f/val.i_int;
1177 libvlc_state_t libvlc_media_player_get_state(
1178 libvlc_media_player_t *p_mi,
1179 libvlc_exception_t *p_e )
1181 input_thread_t *p_input_thread;
1182 libvlc_state_t state = libvlc_Ended;
1185 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1186 if( !p_input_thread )
1188 /* We do return the right value, no need to throw an exception */
1189 if( libvlc_exception_raised( p_e ) )
1190 libvlc_exception_clear( p_e );
1194 var_Get( p_input_thread, "state", &val );
1195 state = vlc_to_libvlc_state(val.i_int);
1197 if( state == libvlc_Playing )
1200 caching = var_GetFloat( p_input_thread, "cache" );
1201 if( caching > 0.0 && caching < 1.0 )
1202 state = libvlc_Buffering;
1204 vlc_object_release( p_input_thread );
1208 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1209 libvlc_exception_t *p_e )
1211 input_thread_t *p_input_thread;
1214 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1215 if ( !p_input_thread )
1217 /* We do return the right value, no need to throw an exception */
1218 if( libvlc_exception_raised( p_e ) )
1219 libvlc_exception_clear( p_e );
1222 var_Get( p_input_thread, "can-seek", &val );
1223 vlc_object_release( p_input_thread );
1228 /* internal function, used by audio, video */
1229 libvlc_track_description_t *
1230 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1231 const char *psz_variable,
1232 libvlc_exception_t *p_e )
1234 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1239 vlc_value_t val_list, text_list;
1240 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1242 if( val_list.p_list->i_count <= 0 ) /* no tracks */
1245 libvlc_track_description_t *p_track_description, *p_actual, *p_previous;
1246 p_track_description = ( libvlc_track_description_t * )
1247 malloc( sizeof( libvlc_track_description_t ) );
1248 if ( !p_track_description )
1250 var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1251 &val_list, &text_list);
1252 vlc_object_release( p_input );
1253 libvlc_exception_raise( p_e, "no enough memory" );
1256 p_actual = p_track_description;
1258 for( int i = 0; i < val_list.p_list->i_count; i++ )
1262 p_actual = ( libvlc_track_description_t * )
1263 malloc( sizeof( libvlc_track_description_t ) );
1266 libvlc_track_description_release( p_track_description );
1267 var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1268 &val_list, &text_list);
1269 vlc_object_release( p_input );
1270 libvlc_exception_raise( p_e, "no enough memory" );
1274 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1275 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1276 p_actual->p_next = NULL;
1278 p_previous->p_next = p_actual;
1279 p_previous = p_actual;
1282 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1283 vlc_object_release( p_input );
1285 return p_track_description;
1288 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1290 libvlc_track_description_t *p_actual, *p_before;
1295 free( p_actual->psz_name );
1296 p_before = p_actual;
1297 p_actual = p_before->p_next;
1302 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1303 libvlc_exception_t *p_e )
1305 input_thread_t *p_input_thread;
1308 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1309 if ( !p_input_thread )
1311 /* We do return the right value, no need to throw an exception */
1312 if( libvlc_exception_raised( p_e ) )
1313 libvlc_exception_clear( p_e );
1316 var_Get( p_input_thread, "can-pause", &val );
1317 vlc_object_release( p_input_thread );