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 );
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;
352 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 );
382 p_mi->p_md = libvlc_media_new_from_input_item(
384 input_GetItem( p_input ), p_e );
387 libvlc_media_player_destroy( p_mi );
391 /* will be released in media_player_release() */
392 vlc_object_hold( p_input );
394 p_mi->p_input_thread = p_input;
395 p_mi->b_own_its_input_thread = false;
400 /**************************************************************************
401 * Destroy a Media Instance object (libvlc internal)
403 * Warning: No lock held here, but hey, this is internal. Caller must lock.
404 **************************************************************************/
405 void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
407 input_thread_t *p_input_thread;
408 libvlc_exception_t p_e;
413 libvlc_exception_init( &p_e );
415 /* Detach Callback from the main libvlc object */
416 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
417 "vout-snapshottaken", SnapshotTakenCallback, p_mi );
419 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
421 if( libvlc_exception_raised( &p_e ) )
423 libvlc_event_manager_release( p_mi->p_event_manager );
424 libvlc_exception_clear( &p_e );
426 return; /* no need to worry about no input thread */
428 vlc_mutex_destroy( &p_mi->object_lock );
430 vlc_object_release( p_input_thread );
432 libvlc_media_release( p_mi->p_md );
437 /**************************************************************************
438 * Release a Media Instance object.
440 * Function does the locking.
441 **************************************************************************/
442 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
447 vlc_mutex_lock( &p_mi->object_lock );
451 if( p_mi->i_refcount > 0 )
453 vlc_mutex_unlock( &p_mi->object_lock );
456 vlc_mutex_unlock( &p_mi->object_lock );
458 /* Detach Callback from the main libvlc object */
459 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
460 "vout-snapshottaken", SnapshotTakenCallback, p_mi );
462 vlc_mutex_destroy( &p_mi->object_lock );
464 release_input_thread( p_mi, true );
466 libvlc_event_manager_release( p_mi->p_event_manager );
468 libvlc_media_release( p_mi->p_md );
473 /**************************************************************************
474 * Retain a Media Instance object.
476 * Caller must hold the lock.
477 **************************************************************************/
478 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
486 /**************************************************************************
487 * Set the Media descriptor associated with the instance.
489 * Enter without lock -- function will lock the object.
490 **************************************************************************/
491 void libvlc_media_player_set_media(
492 libvlc_media_player_t *p_mi,
493 libvlc_media_t *p_md,
494 libvlc_exception_t *p_e )
501 vlc_mutex_lock( &p_mi->object_lock );
503 /* FIXME I am not sure if it is a user request or on die(eof/error)
505 release_input_thread( p_mi,
506 p_mi->p_input_thread &&
507 !p_mi->p_input_thread->b_eof &&
508 !p_mi->p_input_thread->b_error );
511 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
513 libvlc_media_release( p_mi->p_md );
518 vlc_mutex_unlock( &p_mi->object_lock );
519 return; /* It is ok to pass a NULL md */
522 libvlc_media_retain( p_md );
525 /* The policy here is to ignore that we were created using a different
526 * libvlc_instance, because we don't really care */
527 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
529 vlc_mutex_unlock( &p_mi->object_lock );
532 /**************************************************************************
533 * Get the Media descriptor associated with the instance.
534 **************************************************************************/
536 libvlc_media_player_get_media(
537 libvlc_media_player_t *p_mi,
538 libvlc_exception_t *p_e )
545 libvlc_media_retain( p_mi->p_md );
549 /**************************************************************************
550 * Get the event Manager.
551 **************************************************************************/
552 libvlc_event_manager_t *
553 libvlc_media_player_event_manager(
554 libvlc_media_player_t *p_mi,
555 libvlc_exception_t *p_e )
559 return p_mi->p_event_manager;
562 /**************************************************************************
563 * Trigger a snapshot Taken Event.
564 *************************************************************************/
565 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
566 vlc_value_t oldval, vlc_value_t newval, void *p_data )
568 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
571 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
572 libvlc_event_t event ;
573 event.type = libvlc_MediaPlayerSnapshotTaken ;
574 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
575 /* Snapshot psz data is a vlc_variable owned by libvlc object .
576 Its memmory management is taken care by the obj*/
577 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p"
578 " (%s)", event.u.media_player_snapshot_taken.psz_filename,
579 event.u.media_player_snapshot_taken.psz_filename );
580 libvlc_event_send( p_mi->p_event_manager, &event );
585 /**************************************************************************
586 * Tell media player to start playing.
587 **************************************************************************/
588 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
589 libvlc_exception_t *p_e )
591 input_thread_t * p_input_thread;
593 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
595 /* A thread already exists, send it a play message */
596 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
597 vlc_object_release( p_input_thread );
601 /* Ignore previous exception */
602 libvlc_exception_clear( p_e );
604 vlc_mutex_lock( &p_mi->object_lock );
608 libvlc_exception_raise( p_e, "no associated media descriptor" );
609 vlc_mutex_unlock( &p_mi->object_lock );
613 p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
614 p_mi->p_md->p_input_item, NULL, NULL );
616 if( !p_mi->p_input_thread )
618 vlc_mutex_unlock( &p_mi->object_lock );
622 p_input_thread = p_mi->p_input_thread;
624 var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
625 if( p_mi->drawable.agl )
626 var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
628 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
629 if( p_mi->drawable.xid )
630 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
632 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
633 if( p_mi->drawable.hwnd != NULL )
635 vlc_value_t val = { .p_address = p_mi->drawable.hwnd };
636 var_Set( p_input_thread, "drawable-hwnd", val );
639 var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
640 if( p_mi->drawable.nsobject != NULL )
642 vlc_value_t val = { .p_address = p_mi->drawable.nsobject };
643 var_Set( p_input_thread, "drawable-nsobject", val );
646 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
647 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
648 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
650 if( input_Start( p_input_thread ) )
652 vlc_object_release( p_input_thread );
653 p_mi->p_input_thread = NULL;
656 vlc_mutex_unlock( &p_mi->object_lock );
659 /**************************************************************************
661 **************************************************************************/
662 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
663 libvlc_exception_t *p_e )
665 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
666 if( !p_input_thread )
669 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
670 if( state == libvlc_Playing || state == libvlc_Buffering )
672 if( libvlc_media_player_can_pause( p_mi, p_e ) )
673 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
675 libvlc_media_player_stop( p_mi, p_e );
678 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
680 vlc_object_release( p_input_thread );
683 /**************************************************************************
684 * Tells whether the media player is currently playing.
686 * Enter with lock held.
687 **************************************************************************/
688 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
689 libvlc_exception_t *p_e )
691 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
692 return (libvlc_Playing == state) || (libvlc_Buffering == state);
695 /**************************************************************************
697 **************************************************************************/
698 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
699 libvlc_exception_t *p_e )
701 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
703 if( state == libvlc_Playing ||
704 state == libvlc_Paused ||
705 state == libvlc_Buffering )
707 /* Send a stop notification event only if we are in playing,
708 * buffering or paused states */
709 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
711 /* Construct and send the event */
712 libvlc_event_t event;
713 event.type = libvlc_MediaPlayerStopped;
714 libvlc_event_send( p_mi->p_event_manager, &event );
717 if( p_mi->b_own_its_input_thread )
719 vlc_mutex_lock( &p_mi->object_lock );
720 release_input_thread( p_mi, true ); /* This will stop the input thread */
721 vlc_mutex_unlock( &p_mi->object_lock );
725 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
726 if( !p_input_thread )
729 input_Stop( p_input_thread, true );
730 vlc_object_release( p_input_thread );
731 p_mi->p_input_thread = NULL;
735 /**************************************************************************
737 **************************************************************************/
738 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
740 libvlc_exception_t *p_e )
743 p_mi->drawable.nsobject = drawable;
746 /**************************************************************************
748 **************************************************************************/
749 uint32_t libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
751 return p_mi->drawable.nsobject;
754 /**************************************************************************
756 **************************************************************************/
757 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
759 libvlc_exception_t *p_e )
762 p_mi->drawable.agl = drawable;
765 /**************************************************************************
767 **************************************************************************/
768 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
770 return p_mi->drawable.agl;
773 /**************************************************************************
775 **************************************************************************/
776 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
778 libvlc_exception_t *p_e )
781 p_mi->drawable.xid = drawable;
784 /**************************************************************************
786 **************************************************************************/
787 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
789 return p_mi->drawable.xid;
792 /**************************************************************************
794 **************************************************************************/
795 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
797 libvlc_exception_t *p_e )
800 p_mi->drawable.hwnd = drawable;
803 /**************************************************************************
805 **************************************************************************/
806 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
808 return p_mi->drawable.hwnd;
811 /**************************************************************************
813 **************************************************************************/
814 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
815 libvlc_drawable_t drawable,
816 libvlc_exception_t *p_e )
819 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
820 p_mi->drawable.hwnd = (HWND)drawable;
822 libvlc_exception_raise(p_e, "Operation not supported");
823 #elif defined(__APPLE__)
824 p_mi->drawable.agl = drawable;
827 p_mi->drawable.xid = drawable;
832 /**************************************************************************
834 **************************************************************************/
836 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi,
837 libvlc_exception_t *p_e )
842 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
843 return (libvlc_drawable_t)p_mi->drawable.hwnd;
846 #elif defined(__APPLE__)
847 return p_mi->drawable.agl;
849 return p_mi->drawable.xid;
853 /**************************************************************************
854 * Getters for stream information
855 **************************************************************************/
856 libvlc_time_t libvlc_media_player_get_length(
857 libvlc_media_player_t *p_mi,
858 libvlc_exception_t *p_e )
860 input_thread_t *p_input_thread;
863 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
864 if( !p_input_thread )
867 var_Get( p_input_thread, "length", &val );
868 vlc_object_release( p_input_thread );
870 return (val.i_time+500LL)/1000LL;
873 libvlc_time_t libvlc_media_player_get_time(
874 libvlc_media_player_t *p_mi,
875 libvlc_exception_t *p_e )
877 input_thread_t *p_input_thread;
880 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
881 if( !p_input_thread )
884 var_Get( p_input_thread , "time", &val );
885 vlc_object_release( p_input_thread );
886 return (val.i_time+500LL)/1000LL;
889 void libvlc_media_player_set_time(
890 libvlc_media_player_t *p_mi,
892 libvlc_exception_t *p_e )
894 input_thread_t *p_input_thread;
896 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
897 if( !p_input_thread )
900 var_SetTime( p_input_thread, "time", time*1000LL );
901 vlc_object_release( p_input_thread );
904 void libvlc_media_player_set_position(
905 libvlc_media_player_t *p_mi,
907 libvlc_exception_t *p_e )
909 input_thread_t *p_input_thread;
911 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
912 if( !p_input_thread )
915 var_SetFloat( p_input_thread, "position", position );
916 vlc_object_release( p_input_thread );
919 float libvlc_media_player_get_position(
920 libvlc_media_player_t *p_mi,
921 libvlc_exception_t *p_e )
923 input_thread_t *p_input_thread;
926 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
927 if( !p_input_thread )
930 var_Get( p_input_thread, "position", &val );
931 vlc_object_release( p_input_thread );
936 void libvlc_media_player_set_chapter(
937 libvlc_media_player_t *p_mi,
939 libvlc_exception_t *p_e )
941 input_thread_t *p_input_thread;
943 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
944 if( !p_input_thread )
947 var_SetInteger( p_input_thread, "chapter", chapter );
948 vlc_object_release( p_input_thread );
951 int libvlc_media_player_get_chapter(
952 libvlc_media_player_t *p_mi,
953 libvlc_exception_t *p_e )
955 input_thread_t *p_input_thread;
958 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
959 if( !p_input_thread )
962 var_Get( p_input_thread, "chapter", &val );
963 vlc_object_release( p_input_thread );
968 int libvlc_media_player_get_chapter_count(
969 libvlc_media_player_t *p_mi,
970 libvlc_exception_t *p_e )
972 input_thread_t *p_input_thread;
975 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
976 if( !p_input_thread )
979 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
980 vlc_object_release( p_input_thread );
985 int libvlc_media_player_get_chapter_count_for_title(
986 libvlc_media_player_t *p_mi,
988 libvlc_exception_t *p_e )
990 input_thread_t *p_input_thread;
993 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
994 if( !p_input_thread )
998 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
1000 vlc_object_release( p_input_thread );
1003 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1004 vlc_object_release( p_input_thread );
1010 void libvlc_media_player_set_title(
1011 libvlc_media_player_t *p_mi,
1013 libvlc_exception_t *p_e )
1015 input_thread_t *p_input_thread;
1017 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1018 if( !p_input_thread )
1021 var_SetInteger( p_input_thread, "title", i_title );
1022 vlc_object_release( p_input_thread );
1025 libvlc_event_t event;
1026 event.type = libvlc_MediaPlayerTitleChanged;
1027 event.u.media_player_title_changed.new_title = i_title;
1028 libvlc_event_send( p_mi->p_event_manager, &event );
1031 int libvlc_media_player_get_title(
1032 libvlc_media_player_t *p_mi,
1033 libvlc_exception_t *p_e )
1035 input_thread_t *p_input_thread;
1038 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1039 if( !p_input_thread )
1042 var_Get( p_input_thread, "title", &val );
1043 vlc_object_release( p_input_thread );
1048 int libvlc_media_player_get_title_count(
1049 libvlc_media_player_t *p_mi,
1050 libvlc_exception_t *p_e )
1052 input_thread_t *p_input_thread;
1055 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1056 if( !p_input_thread )
1059 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1060 vlc_object_release( p_input_thread );
1065 void libvlc_media_player_next_chapter(
1066 libvlc_media_player_t *p_mi,
1067 libvlc_exception_t *p_e )
1069 input_thread_t *p_input_thread;
1071 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1072 if( !p_input_thread )
1075 int i_type = var_Type( p_input_thread, "next-chapter" );
1076 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1077 "next-chapter":"next-title", true );
1079 vlc_object_release( p_input_thread );
1082 void libvlc_media_player_previous_chapter(
1083 libvlc_media_player_t *p_mi,
1084 libvlc_exception_t *p_e )
1086 input_thread_t *p_input_thread;
1088 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1089 if( !p_input_thread )
1092 int i_type = var_Type( p_input_thread, "next-chapter" );
1093 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1094 "prev-chapter":"prev-title", true );
1096 vlc_object_release( p_input_thread );
1099 float libvlc_media_player_get_fps(
1100 libvlc_media_player_t *p_mi,
1101 libvlc_exception_t *p_e)
1103 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1106 if( p_input_thread )
1108 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1110 vlc_object_release( p_input_thread );
1115 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1116 libvlc_exception_t *p_e)
1118 input_thread_t *p_input_thread =
1119 libvlc_get_input_thread ( p_mi, p_e);
1120 if ( !p_input_thread )
1123 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1125 vlc_object_release( p_input_thread );
1128 vlc_object_release( p_input_thread );
1132 void libvlc_media_player_set_rate(
1133 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 )
1144 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1145 if( (rate < 0.0) && !b_can_rewind )
1147 vlc_object_release( p_input_thread );
1148 libvlc_exception_raise( p_e, "Rate value is invalid" );
1152 var_SetInteger( p_input_thread, "rate", 1000.0f/rate );
1153 vlc_object_release( p_input_thread );
1156 float libvlc_media_player_get_rate(
1157 libvlc_media_player_t *p_mi,
1158 libvlc_exception_t *p_e )
1160 input_thread_t *p_input_thread;
1164 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1165 if( !p_input_thread )
1166 return 0.0; /* rate < 0 indicates rewind */
1168 var_Get( p_input_thread, "rate", &val );
1169 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1170 if( (val.i_int < 0) && !b_can_rewind )
1172 libvlc_exception_raise( p_e, "invalid rate" );
1175 vlc_object_release( p_input_thread );
1177 return (float)1000.0f/val.i_int;
1180 libvlc_state_t libvlc_media_player_get_state(
1181 libvlc_media_player_t *p_mi,
1182 libvlc_exception_t *p_e )
1184 input_thread_t *p_input_thread;
1185 libvlc_state_t state = libvlc_Ended;
1188 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1189 if( !p_input_thread )
1191 /* We do return the right value, no need to throw an exception */
1192 if( libvlc_exception_raised( p_e ) )
1193 libvlc_exception_clear( p_e );
1197 var_Get( p_input_thread, "state", &val );
1198 state = vlc_to_libvlc_state(val.i_int);
1200 if( state == libvlc_Playing )
1203 caching = var_GetFloat( p_input_thread, "cache" );
1204 if( caching > 0.0 && caching < 1.0 )
1205 state = libvlc_Buffering;
1207 vlc_object_release( p_input_thread );
1211 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1212 libvlc_exception_t *p_e )
1214 input_thread_t *p_input_thread;
1217 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1218 if ( !p_input_thread )
1220 /* We do return the right value, no need to throw an exception */
1221 if( libvlc_exception_raised( p_e ) )
1222 libvlc_exception_clear( p_e );
1225 var_Get( p_input_thread, "can-seek", &val );
1226 vlc_object_release( p_input_thread );
1231 /* internal function, used by audio, video */
1232 libvlc_track_description_t *
1233 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1234 const char *psz_variable,
1235 libvlc_exception_t *p_e )
1237 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1242 vlc_value_t val_list, text_list;
1243 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1245 if( val_list.p_list->i_count <= 0 ) /* no tracks */
1248 libvlc_track_description_t *p_track_description, *p_actual, *p_previous;
1249 p_track_description = ( libvlc_track_description_t * )
1250 malloc( sizeof( libvlc_track_description_t ) );
1251 if ( !p_track_description )
1253 var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1254 &val_list, &text_list);
1255 vlc_object_release( p_input );
1256 libvlc_exception_raise( p_e, "no enough memory" );
1259 p_actual = p_track_description;
1261 for( int i = 0; i < val_list.p_list->i_count; i++ )
1265 p_actual = ( libvlc_track_description_t * )
1266 malloc( sizeof( libvlc_track_description_t ) );
1269 libvlc_track_description_release( p_track_description );
1270 var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1271 &val_list, &text_list);
1272 vlc_object_release( p_input );
1273 libvlc_exception_raise( p_e, "no enough memory" );
1277 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1278 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1279 p_actual->p_next = NULL;
1281 p_previous->p_next = p_actual;
1282 p_previous = p_actual;
1285 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1286 vlc_object_release( p_input );
1288 return p_track_description;
1291 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1293 libvlc_track_description_t *p_actual, *p_before;
1298 free( p_actual->psz_name );
1299 p_before = p_actual;
1300 p_actual = p_before->p_next;
1305 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1306 libvlc_exception_t *p_e )
1308 input_thread_t *p_input_thread;
1311 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1312 if ( !p_input_thread )
1314 /* We do return the right value, no need to throw an exception */
1315 if( libvlc_exception_raised( p_e ) )
1316 libvlc_exception_clear( p_e );
1319 var_Get( p_input_thread, "can-pause", &val );
1320 vlc_object_release( p_input_thread );