1 /*****************************************************************************
2 * media_player.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27 #include <vlc_demux.h>
28 #include <vlc_input.h>
34 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
35 vlc_value_t oldval, vlc_value_t newval,
38 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
39 vlc_value_t oldval, vlc_value_t newval,
42 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
43 vlc_value_t oldval, vlc_value_t newval,
46 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
47 vlc_value_t oldval, vlc_value_t newval, void *p_data );
49 static const libvlc_state_t vlc_to_libvlc_state_array[] =
51 [INIT_S] = libvlc_NothingSpecial,
52 [OPENING_S] = libvlc_Opening,
53 [PLAYING_S] = libvlc_Playing,
54 [PAUSE_S] = libvlc_Paused,
55 [END_S] = libvlc_Ended,
56 [ERROR_S] = libvlc_Error,
59 static inline libvlc_state_t vlc_to_libvlc_state( int vlc_state )
61 if( vlc_state < 0 || vlc_state > 6 )
64 return vlc_to_libvlc_state_array[vlc_state];
68 * Release the associated input thread.
70 * Object lock is NOT held.
72 static void release_input_thread( libvlc_media_player_t *p_mi, 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_StopThread( 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_TIMES )
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 );
245 /**************************************************************************
246 * Create a Media Instance object.
249 * - All items created by _new start with a refcount set to 1.
250 * - Accessor _release decrease the refcount by 1, if after that
251 * operation the refcount is 0, the object is destroyed.
252 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
254 * Object locking strategy:
255 * - No lock held while in constructor.
256 * - When accessing any member variable this lock is held. (XXX who locks?)
257 * - When attempting to destroy the object the lock is also held.
258 **************************************************************************/
259 libvlc_media_player_t *
260 libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
261 libvlc_exception_t * p_e )
263 libvlc_media_player_t * p_mi;
265 if( !p_libvlc_instance )
267 libvlc_exception_raise( p_e, "invalid libvlc instance" );
271 p_mi = malloc( sizeof(libvlc_media_player_t) );
274 libvlc_exception_raise( p_e, "Not enough memory" );
278 p_mi->drawable.agl = 0;
279 p_mi->drawable.xid = 0;
280 p_mi->drawable.hwnd = NULL;
281 p_mi->p_libvlc_instance = p_libvlc_instance;
282 p_mi->p_input_thread = NULL;
283 p_mi->i_refcount = 1;
284 p_mi->b_own_its_input_thread = true;
285 vlc_mutex_init( &p_mi->object_lock );
286 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
287 p_libvlc_instance, p_e );
288 if( libvlc_exception_raised( p_e ) )
290 vlc_mutex_destroy( &p_mi->object_lock );
295 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
296 libvlc_MediaPlayerNothingSpecial, p_e );
297 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
298 libvlc_MediaPlayerOpening, p_e );
299 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
300 libvlc_MediaPlayerBuffering, p_e );
301 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
302 libvlc_MediaPlayerPlaying, p_e );
303 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
304 libvlc_MediaPlayerPaused, p_e );
305 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
306 libvlc_MediaPlayerStopped, p_e );
307 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
308 libvlc_MediaPlayerForward, p_e );
309 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
310 libvlc_MediaPlayerBackward, p_e );
311 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
312 libvlc_MediaPlayerEndReached, p_e );
313 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
314 libvlc_MediaPlayerEncounteredError, p_e );
316 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
317 libvlc_MediaPlayerPositionChanged, p_e );
318 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
319 libvlc_MediaPlayerTimeChanged, p_e );
320 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
321 libvlc_MediaPlayerTitleChanged, p_e );
322 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
323 libvlc_MediaPlayerSeekableChanged, p_e );
324 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
325 libvlc_MediaPlayerPausableChanged, p_e );
327 /* Snapshot initialization */
328 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
329 libvlc_MediaPlayerSnapshotTaken, p_e );
330 /* Attach a var callback to the global object to provide the glue between
331 vout_thread that generates the event and media_player that re-emits it
332 with its own event manager
334 var_Create( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
335 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
336 var_AddCallback( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
337 SnapshotTakenCallback, p_mi );
342 /**************************************************************************
343 * Create a Media Instance object with a media descriptor.
344 **************************************************************************/
345 libvlc_media_player_t *
346 libvlc_media_player_new_from_media(
347 libvlc_media_t * p_md,
348 libvlc_exception_t *p_e )
350 libvlc_media_player_t * p_mi;
351 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
356 libvlc_media_retain( p_md );
362 /**************************************************************************
363 * Create a new media instance object from an input_thread (Libvlc Internal).
364 **************************************************************************/
365 libvlc_media_player_t * libvlc_media_player_new_from_input_thread(
366 struct libvlc_instance_t *p_libvlc_instance,
367 input_thread_t *p_input,
368 libvlc_exception_t *p_e )
370 libvlc_media_player_t * p_mi;
374 libvlc_exception_raise( p_e, "invalid input thread" );
378 p_mi = libvlc_media_player_new( p_libvlc_instance, p_e );
383 p_mi->p_md = libvlc_media_new_from_input_item(
385 input_GetItem( p_input ), p_e );
389 libvlc_media_player_destroy( p_mi );
393 /* will be released in media_player_release() */
394 vlc_object_hold( p_input );
396 p_mi->p_input_thread = p_input;
397 p_mi->b_own_its_input_thread = false;
402 /**************************************************************************
403 * Destroy a Media Instance object (libvlc internal)
405 * Warning: No lock held here, but hey, this is internal. Caller must lock.
406 **************************************************************************/
407 void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
409 input_thread_t *p_input_thread;
410 libvlc_exception_t p_e;
412 libvlc_exception_init( &p_e );
417 /* Detach Callback from the main libvlc object */
418 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
419 "vout-snapshottaken", SnapshotTakenCallback, p_mi );
421 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
423 if( libvlc_exception_raised( &p_e ) )
425 libvlc_event_manager_release( p_mi->p_event_manager );
426 libvlc_exception_clear( &p_e );
428 return; /* no need to worry about no input thread */
430 vlc_mutex_destroy( &p_mi->object_lock );
432 vlc_object_release( p_input_thread );
434 libvlc_media_release( p_mi->p_md );
439 /**************************************************************************
440 * Release a Media Instance object.
442 * Function does the locking.
443 **************************************************************************/
444 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
449 vlc_mutex_lock( &p_mi->object_lock );
453 if( p_mi->i_refcount > 0 )
455 vlc_mutex_unlock( &p_mi->object_lock );
458 vlc_mutex_unlock( &p_mi->object_lock );
459 vlc_mutex_destroy( &p_mi->object_lock );
461 release_input_thread( p_mi, true );
463 libvlc_event_manager_release( p_mi->p_event_manager );
465 libvlc_media_release( p_mi->p_md );
470 /**************************************************************************
471 * Retain a Media Instance object.
473 * Caller must hold the lock.
474 **************************************************************************/
475 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
483 /**************************************************************************
484 * Set the Media descriptor associated with the instance.
486 * Enter without lock -- function will lock the object.
487 **************************************************************************/
488 void libvlc_media_player_set_media(
489 libvlc_media_player_t *p_mi,
490 libvlc_media_t *p_md,
491 libvlc_exception_t *p_e )
498 vlc_mutex_lock( &p_mi->object_lock );
500 /* FIXME I am not sure if it is a user request or on die(eof/error)
502 release_input_thread( p_mi,
503 p_mi->p_input_thread &&
504 !p_mi->p_input_thread->b_eof &&
505 !p_mi->p_input_thread->b_error );
508 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
510 libvlc_media_release( p_mi->p_md );
515 vlc_mutex_unlock( &p_mi->object_lock );
516 return; /* It is ok to pass a NULL md */
519 libvlc_media_retain( p_md );
522 /* The policy here is to ignore that we were created using a different
523 * libvlc_instance, because we don't really care */
524 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
526 vlc_mutex_unlock( &p_mi->object_lock );
529 /**************************************************************************
530 * Get the Media descriptor associated with the instance.
531 **************************************************************************/
533 libvlc_media_player_get_media(
534 libvlc_media_player_t *p_mi,
535 libvlc_exception_t *p_e )
542 libvlc_media_retain( p_mi->p_md );
546 /**************************************************************************
547 * Get the event Manager.
548 **************************************************************************/
549 libvlc_event_manager_t *
550 libvlc_media_player_event_manager(
551 libvlc_media_player_t *p_mi,
552 libvlc_exception_t *p_e )
556 return p_mi->p_event_manager;
559 /**************************************************************************
560 * Trigger a snapshot Taken Event.
561 *************************************************************************/
562 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
563 vlc_value_t oldval, vlc_value_t newval, void *p_data )
565 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
568 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
569 libvlc_event_t event ;
570 event.type = libvlc_MediaPlayerSnapshotTaken ;
571 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
572 /* Snapshot psz data is a vlc_variable owned by libvlc object .
573 Its memmory management is taken care by the obj*/
574 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p"
575 " (%s)", event.u.media_player_snapshot_taken.psz_filename,
576 event.u.media_player_snapshot_taken.psz_filename );
577 libvlc_event_send( p_mi->p_event_manager, &event );
582 /**************************************************************************
583 * Tell media player to start playing.
584 **************************************************************************/
585 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
586 libvlc_exception_t *p_e )
588 input_thread_t * p_input_thread;
590 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
592 /* A thread already exists, send it a play message */
593 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
594 vlc_object_release( p_input_thread );
598 /* Ignore previous exception */
599 libvlc_exception_clear( p_e );
601 vlc_mutex_lock( &p_mi->object_lock );
605 libvlc_exception_raise( p_e, "no associated media descriptor" );
606 vlc_mutex_unlock( &p_mi->object_lock );
610 p_mi->p_input_thread = input_CreateThread(
611 p_mi->p_libvlc_instance->p_libvlc_int, p_mi->p_md->p_input_item );
613 if( !p_mi->p_input_thread )
615 vlc_mutex_unlock( &p_mi->object_lock );
619 p_input_thread = p_mi->p_input_thread;
621 var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
622 if( p_mi->drawable.agl )
623 var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
625 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
626 if( p_mi->drawable.xid )
627 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
629 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
630 if( p_mi->drawable.hwnd != NULL )
632 vlc_value_t val = { .p_address = p_mi->drawable.hwnd };
633 var_Set( p_input_thread, "drawable-hwnd", val );
636 var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
637 if( p_mi->drawable.nsobject != NULL )
639 vlc_value_t val = { .p_address = p_mi->drawable.nsobject };
640 var_Set( p_input_thread, "drawable-nsobject", val );
643 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
644 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
645 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
647 vlc_mutex_unlock( &p_mi->object_lock );
650 /**************************************************************************
652 **************************************************************************/
653 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
654 libvlc_exception_t *p_e )
656 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
658 if( !p_input_thread )
661 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
663 if( state == libvlc_Playing )
665 if( libvlc_media_player_can_pause( p_mi, p_e ) )
666 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
668 libvlc_media_player_stop( p_mi, p_e );
671 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
673 vlc_object_release( p_input_thread );
676 /**************************************************************************
677 * Tells whether the media player is currently playing.
679 * Enter with lock held.
680 **************************************************************************/
681 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
682 libvlc_exception_t *p_e )
684 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
685 return libvlc_Playing == state;
689 /**************************************************************************
691 **************************************************************************/
692 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
693 libvlc_exception_t *p_e )
695 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
697 if( state == libvlc_Playing || state == libvlc_Paused )
699 /* Send a stop notification event only if we are in playing or
701 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
703 /* Construct and send the event */
704 libvlc_event_t event;
705 event.type = libvlc_MediaPlayerStopped;
706 libvlc_event_send( p_mi->p_event_manager, &event );
709 if( p_mi->b_own_its_input_thread )
711 vlc_mutex_lock( &p_mi->object_lock );
712 release_input_thread( p_mi, true ); /* This will stop the input thread */
713 vlc_mutex_unlock( &p_mi->object_lock );
717 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
719 if( !p_input_thread )
722 input_StopThread( p_input_thread, true );
723 vlc_object_release( p_input_thread );
724 p_mi->p_input_thread = NULL;
728 /**************************************************************************
730 **************************************************************************/
731 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
733 libvlc_exception_t *p_e )
736 p_mi->drawable.nsobject = drawable;
739 /**************************************************************************
741 **************************************************************************/
742 uint32_t libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
744 return p_mi->drawable.nsobject;
747 /**************************************************************************
749 **************************************************************************/
750 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
752 libvlc_exception_t *p_e )
755 p_mi->drawable.agl = drawable;
758 /**************************************************************************
760 **************************************************************************/
761 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
763 return p_mi->drawable.agl;
766 /**************************************************************************
768 **************************************************************************/
769 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
771 libvlc_exception_t *p_e )
774 p_mi->drawable.xid = drawable;
777 /**************************************************************************
779 **************************************************************************/
780 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
782 return p_mi->drawable.xid;
785 /**************************************************************************
787 **************************************************************************/
788 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
790 libvlc_exception_t *p_e )
793 p_mi->drawable.hwnd = drawable;
796 /**************************************************************************
798 **************************************************************************/
799 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
801 return p_mi->drawable.hwnd;
804 /**************************************************************************
806 **************************************************************************/
807 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
808 libvlc_drawable_t drawable,
809 libvlc_exception_t *p_e )
812 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
813 p_mi->drawable.hwnd = (HWND)drawable;
815 libvlc_exception_raise(p_e, "Operation not supported");
816 #elif defined(__APPLE__)
817 p_mi->drawable.agl = drawable;
820 p_mi->drawable.xid = drawable;
825 /**************************************************************************
827 **************************************************************************/
829 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi,
830 libvlc_exception_t *p_e )
835 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
836 return (libvlc_drawable_t)p_mi->drawable.hwnd;
839 #elif defined(__APPLE__)
840 return p_mi->drawable.agl;
842 return p_mi->drawable.xid;
846 /**************************************************************************
847 * Getters for stream information
848 **************************************************************************/
849 libvlc_time_t libvlc_media_player_get_length(
850 libvlc_media_player_t *p_mi,
851 libvlc_exception_t *p_e )
853 input_thread_t *p_input_thread;
856 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
857 if( !p_input_thread )
860 var_Get( p_input_thread, "length", &val );
861 vlc_object_release( p_input_thread );
863 return (val.i_time+500LL)/1000LL;
866 libvlc_time_t libvlc_media_player_get_time(
867 libvlc_media_player_t *p_mi,
868 libvlc_exception_t *p_e )
870 input_thread_t *p_input_thread;
873 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
874 if( !p_input_thread )
877 var_Get( p_input_thread , "time", &val );
878 vlc_object_release( p_input_thread );
879 return (val.i_time+500LL)/1000LL;
882 void libvlc_media_player_set_time(
883 libvlc_media_player_t *p_mi,
885 libvlc_exception_t *p_e )
887 input_thread_t *p_input_thread;
889 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
890 if( !p_input_thread )
893 var_SetTime( p_input_thread, "time", time*1000LL );
894 vlc_object_release( p_input_thread );
897 void libvlc_media_player_set_position(
898 libvlc_media_player_t *p_mi,
900 libvlc_exception_t *p_e )
902 input_thread_t *p_input_thread;
904 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
905 if( !p_input_thread )
908 var_SetFloat( p_input_thread, "position", position );
909 vlc_object_release( p_input_thread );
912 float libvlc_media_player_get_position(
913 libvlc_media_player_t *p_mi,
914 libvlc_exception_t *p_e )
916 input_thread_t *p_input_thread;
919 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
920 if( !p_input_thread )
923 var_Get( p_input_thread, "position", &val );
924 vlc_object_release( p_input_thread );
929 void libvlc_media_player_set_chapter(
930 libvlc_media_player_t *p_mi,
932 libvlc_exception_t *p_e )
934 input_thread_t *p_input_thread;
936 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
937 if( !p_input_thread )
940 var_SetInteger( p_input_thread, "chapter", chapter );
941 vlc_object_release( p_input_thread );
944 int libvlc_media_player_get_chapter(
945 libvlc_media_player_t *p_mi,
946 libvlc_exception_t *p_e )
948 input_thread_t *p_input_thread;
951 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
952 if( !p_input_thread )
955 var_Get( p_input_thread, "chapter", &val );
956 vlc_object_release( p_input_thread );
961 int libvlc_media_player_get_chapter_count(
962 libvlc_media_player_t *p_mi,
963 libvlc_exception_t *p_e )
965 input_thread_t *p_input_thread;
968 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
969 if( !p_input_thread )
972 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
973 vlc_object_release( p_input_thread );
978 int libvlc_media_player_get_chapter_count_for_title(
979 libvlc_media_player_t *p_mi,
981 libvlc_exception_t *p_e )
983 input_thread_t *p_input_thread;
986 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
987 if( !p_input_thread )
991 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
993 vlc_object_release( p_input_thread );
996 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
997 vlc_object_release( p_input_thread );
1003 void libvlc_media_player_set_title(
1004 libvlc_media_player_t *p_mi,
1006 libvlc_exception_t *p_e )
1008 input_thread_t *p_input_thread;
1010 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1011 if( !p_input_thread )
1014 var_SetInteger( p_input_thread, "title", i_title );
1015 vlc_object_release( p_input_thread );
1018 libvlc_event_t event;
1019 event.type = libvlc_MediaPlayerTitleChanged;
1020 event.u.media_player_title_changed.new_title = i_title;
1021 libvlc_event_send( p_mi->p_event_manager, &event );
1024 int libvlc_media_player_get_title(
1025 libvlc_media_player_t *p_mi,
1026 libvlc_exception_t *p_e )
1028 input_thread_t *p_input_thread;
1031 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1032 if( !p_input_thread )
1035 var_Get( p_input_thread, "title", &val );
1036 vlc_object_release( p_input_thread );
1041 int libvlc_media_player_get_title_count(
1042 libvlc_media_player_t *p_mi,
1043 libvlc_exception_t *p_e )
1045 input_thread_t *p_input_thread;
1048 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1049 if( !p_input_thread )
1052 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1053 vlc_object_release( p_input_thread );
1058 void libvlc_media_player_next_chapter(
1059 libvlc_media_player_t *p_mi,
1060 libvlc_exception_t *p_e )
1062 input_thread_t *p_input_thread;
1064 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1065 if( !p_input_thread )
1068 int i_type = var_Type( p_input_thread, "next-chapter" );
1069 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1070 "next-chapter":"next-title", true );
1072 vlc_object_release( p_input_thread );
1075 void libvlc_media_player_previous_chapter(
1076 libvlc_media_player_t *p_mi,
1077 libvlc_exception_t *p_e )
1079 input_thread_t *p_input_thread;
1081 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1082 if( !p_input_thread )
1085 int i_type = var_Type( p_input_thread, "next-chapter" );
1086 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1087 "prev-chapter":"prev-title", true );
1089 vlc_object_release( p_input_thread );
1092 float libvlc_media_player_get_fps(
1093 libvlc_media_player_t *p_mi,
1094 libvlc_exception_t *p_e)
1096 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1099 if( p_input_thread )
1101 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1103 vlc_object_release( p_input_thread );
1108 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1109 libvlc_exception_t *p_e)
1111 input_thread_t *p_input_thread =
1112 libvlc_get_input_thread ( p_mi, p_e);
1113 if ( !p_input_thread )
1116 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1118 vlc_object_release( p_input_thread );
1121 vlc_object_release( p_input_thread );
1125 void libvlc_media_player_set_rate(
1126 libvlc_media_player_t *p_mi,
1128 libvlc_exception_t *p_e )
1130 input_thread_t *p_input_thread;
1133 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1134 if( !p_input_thread )
1137 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1138 if( (rate < 0.0) && !b_can_rewind )
1140 vlc_object_release( p_input_thread );
1141 libvlc_exception_raise( p_e, "Rate value is invalid" );
1145 var_SetInteger( p_input_thread, "rate", 1000.0f/rate );
1146 vlc_object_release( p_input_thread );
1149 float libvlc_media_player_get_rate(
1150 libvlc_media_player_t *p_mi,
1151 libvlc_exception_t *p_e )
1153 input_thread_t *p_input_thread;
1157 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1158 if( !p_input_thread )
1159 return 0.0; /* rate < 0 indicates rewind */
1161 var_Get( p_input_thread, "rate", &val );
1162 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1163 if( (val.i_int < 0) && !b_can_rewind )
1165 libvlc_exception_raise( p_e, "invalid rate" );
1168 vlc_object_release( p_input_thread );
1170 return (float)1000.0f/val.i_int;
1173 libvlc_state_t libvlc_media_player_get_state(
1174 libvlc_media_player_t *p_mi,
1175 libvlc_exception_t *p_e )
1177 input_thread_t *p_input_thread;
1178 libvlc_state_t state = libvlc_Ended;
1181 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1182 if( !p_input_thread )
1184 /* We do return the right value, no need to throw an exception */
1185 if( libvlc_exception_raised( p_e ) )
1186 libvlc_exception_clear( p_e );
1190 var_Get( p_input_thread, "state", &val );
1191 state = vlc_to_libvlc_state(val.i_int);
1193 if( state == libvlc_Playing )
1196 caching = var_GetFloat( p_input_thread, "cache" );
1197 if( caching > 0.0 && caching < 1.0 )
1198 state = libvlc_Buffering;
1200 vlc_object_release( p_input_thread );
1204 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1205 libvlc_exception_t *p_e )
1207 input_thread_t *p_input_thread;
1210 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1211 if ( !p_input_thread )
1213 /* We do return the right value, no need to throw an exception */
1214 if( libvlc_exception_raised( p_e ) )
1215 libvlc_exception_clear( p_e );
1218 var_Get( p_input_thread, "can-seek", &val );
1219 vlc_object_release( p_input_thread );
1224 /* internal function, used by audio, video */
1225 libvlc_track_description_t *
1226 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1227 const char *psz_variable,
1228 libvlc_exception_t *p_e )
1230 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1235 vlc_value_t val_list, text_list;
1236 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1238 if( val_list.p_list->i_count <= 0 ) /* no tracks */
1241 libvlc_track_description_t *p_track_description, *p_actual, *p_previous;
1242 p_track_description = ( libvlc_track_description_t * )
1243 malloc( sizeof( libvlc_track_description_t ) );
1244 if ( !p_track_description )
1246 var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1247 &val_list, &text_list);
1248 vlc_object_release( p_input );
1249 libvlc_exception_raise( p_e, "no enough memory" );
1252 p_actual = p_track_description;
1254 for( int i = 0; i < val_list.p_list->i_count; i++ )
1258 p_actual = ( libvlc_track_description_t * )
1259 malloc( sizeof( libvlc_track_description_t ) );
1262 libvlc_track_description_release( p_track_description );
1263 var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1264 &val_list, &text_list);
1265 vlc_object_release( p_input );
1266 libvlc_exception_raise( p_e, "no enough memory" );
1270 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1271 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1272 p_actual->p_next = NULL;
1274 p_previous->p_next = p_actual;
1275 p_previous = p_actual;
1278 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1279 vlc_object_release( p_input );
1281 return p_track_description;
1284 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1286 libvlc_track_description_t *p_actual, *p_before;
1291 free( p_actual->psz_name );
1292 p_before = p_actual;
1293 p_actual = p_before->p_next;
1298 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1299 libvlc_exception_t *p_e )
1301 input_thread_t *p_input_thread;
1304 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1305 if ( !p_input_thread )
1307 /* We do return the right value, no need to throw an exception */
1308 if( libvlc_exception_raised( p_e ) )
1309 libvlc_exception_clear( p_e );
1312 var_Get( p_input_thread, "can-pause", &val );
1313 vlc_object_release( p_input_thread );