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" );
99 vlc_object_release( p_input_thread );
101 p_mi->p_input_thread = NULL;
105 * Retrieve the input thread. Be sure to release the object
106 * once you are done with it. (libvlc Internal)
108 * Function will lock the object.
110 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
111 libvlc_exception_t *p_e )
113 input_thread_t *p_input_thread;
115 if( !p_mi ) RAISENULL( "Media Instance is NULL" );
117 vlc_mutex_lock( &p_mi->object_lock );
119 if( !p_mi->p_input_thread )
121 vlc_mutex_unlock( &p_mi->object_lock );
122 RAISENULL( "Input is NULL" );
125 p_input_thread = p_mi->p_input_thread;
126 vlc_object_hold( p_input_thread );
128 vlc_mutex_unlock( &p_mi->object_lock );
130 return p_input_thread;
134 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
135 vlc_value_t oldval, vlc_value_t newval,
141 libvlc_media_player_t * p_mi = p_userdata;
142 libvlc_event_t event;
144 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
145 event.type = libvlc_MediaPlayerSeekableChanged;
146 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
148 libvlc_event_send( p_mi->p_event_manager, &event );
153 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
154 vlc_value_t oldval, vlc_value_t newval,
160 libvlc_media_player_t * p_mi = p_userdata;
161 libvlc_event_t event;
163 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
164 event.type = libvlc_MediaPlayerPausableChanged;
165 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
167 libvlc_event_send( p_mi->p_event_manager, &event );
172 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
173 vlc_value_t oldval, vlc_value_t newval,
177 input_thread_t * p_input = (input_thread_t *)p_this;
178 libvlc_media_player_t * p_mi = p_userdata;
179 libvlc_event_t event;
181 assert( !strcmp( psz_cmd, "intf-event" ) );
183 if( newval.i_int == INPUT_EVENT_STATE )
185 libvlc_state_t libvlc_state;
187 switch ( var_GetInteger( p_input, "state" ) )
190 libvlc_state = libvlc_NothingSpecial;
191 event.type = libvlc_MediaPlayerNothingSpecial;
194 libvlc_state = libvlc_Opening;
195 event.type = libvlc_MediaPlayerOpening;
198 libvlc_state = libvlc_Playing;
199 event.type = libvlc_MediaPlayerPlaying;
202 libvlc_state = libvlc_Paused;
203 event.type = libvlc_MediaPlayerPaused;
206 libvlc_state = libvlc_Ended;
207 event.type = libvlc_MediaPlayerEndReached;
210 libvlc_state = libvlc_Error;
211 event.type = libvlc_MediaPlayerEncounteredError;
218 libvlc_media_set_state( p_mi->p_md, libvlc_state, NULL );
219 libvlc_event_send( p_mi->p_event_manager, &event );
221 else if( newval.i_int == INPUT_EVENT_TIMES )
223 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
224 return VLC_SUCCESS; /* Don't send the position while stopped */
227 event.type = libvlc_MediaPlayerPositionChanged;
228 event.u.media_player_position_changed.new_position =
229 var_GetFloat( p_input, "position" );
230 libvlc_event_send( p_mi->p_event_manager, &event );
233 event.type = libvlc_MediaPlayerTimeChanged;
234 event.u.media_player_time_changed.new_time =
235 var_GetTime( p_input, "time" );
236 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.xid = 0;
278 p_mi->drawable.hwnd = NULL;
279 p_mi->p_libvlc_instance = p_libvlc_instance;
280 p_mi->p_input_thread = NULL;
281 p_mi->i_refcount = 1;
282 p_mi->b_own_its_input_thread = true;
283 vlc_mutex_init( &p_mi->object_lock );
284 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
285 p_libvlc_instance, p_e );
286 if( libvlc_exception_raised( p_e ) )
292 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
293 libvlc_MediaPlayerNothingSpecial, p_e );
294 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
295 libvlc_MediaPlayerOpening, p_e );
296 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
297 libvlc_MediaPlayerBuffering, p_e );
298 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
299 libvlc_MediaPlayerPlaying, p_e );
300 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
301 libvlc_MediaPlayerPaused, p_e );
302 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
303 libvlc_MediaPlayerStopped, p_e );
304 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
305 libvlc_MediaPlayerForward, p_e );
306 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
307 libvlc_MediaPlayerBackward, p_e );
308 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
309 libvlc_MediaPlayerEndReached, p_e );
310 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
311 libvlc_MediaPlayerEncounteredError, p_e );
313 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
314 libvlc_MediaPlayerPositionChanged, p_e );
315 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
316 libvlc_MediaPlayerTimeChanged, p_e );
317 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
318 libvlc_MediaPlayerTitleChanged, p_e );
319 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
320 libvlc_MediaPlayerSeekableChanged, p_e );
321 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
322 libvlc_MediaPlayerPausableChanged, p_e );
324 /* Snapshot initialization */
325 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
326 libvlc_MediaPlayerSnapshotTaken, p_e );
327 /* Attach a var callback to the global object to provide the glue between
328 vout_thread that generates the event and media_player that re-emits it
329 with its own event manager
331 var_Create( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
332 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
333 var_AddCallback( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
334 SnapshotTakenCallback, p_mi );
339 /**************************************************************************
340 * Create a Media Instance object with a media descriptor.
341 **************************************************************************/
342 libvlc_media_player_t *
343 libvlc_media_player_new_from_media(
344 libvlc_media_t * p_md,
345 libvlc_exception_t *p_e )
347 libvlc_media_player_t * p_mi;
348 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
353 libvlc_media_retain( p_md );
359 /**************************************************************************
360 * Create a new media instance object from an input_thread (Libvlc Internal).
361 **************************************************************************/
362 libvlc_media_player_t * libvlc_media_player_new_from_input_thread(
363 struct libvlc_instance_t *p_libvlc_instance,
364 input_thread_t *p_input,
365 libvlc_exception_t *p_e )
367 libvlc_media_player_t * p_mi;
371 libvlc_exception_raise( p_e, "invalid input thread" );
375 p_mi = libvlc_media_player_new( p_libvlc_instance, p_e );
380 p_mi->p_md = libvlc_media_new_from_input_item(
382 input_GetItem( p_input ), p_e );
386 libvlc_media_player_destroy( p_mi );
390 /* will be released in media_player_release() */
391 vlc_object_hold( p_input );
393 p_mi->p_input_thread = p_input;
394 p_mi->b_own_its_input_thread = false;
399 /**************************************************************************
400 * Destroy a Media Instance object (libvlc internal)
402 * Warning: No lock held here, but hey, this is internal. Caller must lock.
403 **************************************************************************/
404 void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
406 input_thread_t *p_input_thread;
407 libvlc_exception_t p_e;
409 libvlc_exception_init( &p_e );
414 /* Detach Callback from the main libvlc object */
415 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
416 "vout-snapshottaken", SnapshotTakenCallback, p_mi );
418 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
420 if( libvlc_exception_raised( &p_e ) )
422 libvlc_event_manager_release( p_mi->p_event_manager );
423 libvlc_exception_clear( &p_e );
425 return; /* no need to worry about no input thread */
427 vlc_mutex_destroy( &p_mi->object_lock );
429 vlc_object_release( p_input_thread );
431 libvlc_media_release( p_mi->p_md );
436 /**************************************************************************
437 * Release a Media Instance object.
439 * Function does the locking.
440 **************************************************************************/
441 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
446 vlc_mutex_lock( &p_mi->object_lock );
450 if( p_mi->i_refcount > 0 )
452 vlc_mutex_unlock( &p_mi->object_lock );
455 vlc_mutex_unlock( &p_mi->object_lock );
456 vlc_mutex_destroy( &p_mi->object_lock );
458 release_input_thread( p_mi, true );
460 libvlc_event_manager_release( p_mi->p_event_manager );
462 libvlc_media_release( p_mi->p_md );
467 /**************************************************************************
468 * Retain a Media Instance object.
470 * Caller must hold the lock.
471 **************************************************************************/
472 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
480 /**************************************************************************
481 * Set the Media descriptor associated with the instance.
483 * Enter without lock -- function will lock the object.
484 **************************************************************************/
485 void libvlc_media_player_set_media(
486 libvlc_media_player_t *p_mi,
487 libvlc_media_t *p_md,
488 libvlc_exception_t *p_e )
495 vlc_mutex_lock( &p_mi->object_lock );
497 /* FIXME I am not sure if it is a user request or on die(eof/error)
499 release_input_thread( p_mi,
500 p_mi->p_input_thread &&
501 !p_mi->p_input_thread->b_eof &&
502 !p_mi->p_input_thread->b_error );
505 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
507 libvlc_media_release( p_mi->p_md );
512 vlc_mutex_unlock( &p_mi->object_lock );
513 return; /* It is ok to pass a NULL md */
516 libvlc_media_retain( p_md );
519 /* The policy here is to ignore that we were created using a different
520 * libvlc_instance, because we don't really care */
521 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
523 vlc_mutex_unlock( &p_mi->object_lock );
526 /**************************************************************************
527 * Get the Media descriptor associated with the instance.
528 **************************************************************************/
530 libvlc_media_player_get_media(
531 libvlc_media_player_t *p_mi,
532 libvlc_exception_t *p_e )
539 libvlc_media_retain( p_mi->p_md );
543 /**************************************************************************
544 * Get the event Manager.
545 **************************************************************************/
546 libvlc_event_manager_t *
547 libvlc_media_player_event_manager(
548 libvlc_media_player_t *p_mi,
549 libvlc_exception_t *p_e )
553 return p_mi->p_event_manager;
556 /**************************************************************************
557 * Trigger a snapshot Taken Event.
558 *************************************************************************/
559 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
560 vlc_value_t oldval, vlc_value_t newval, void *p_data )
562 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
565 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
566 libvlc_event_t event ;
567 event.type = libvlc_MediaPlayerSnapshotTaken ;
568 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
569 /* Snapshot psz data is a vlc_variable owned by libvlc object .
570 Its memmory management is taken care by the obj*/
571 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p"
572 " (%s)", event.u.media_player_snapshot_taken.psz_filename,
573 event.u.media_player_snapshot_taken.psz_filename );
574 libvlc_event_send( p_mi->p_event_manager, &event );
579 /**************************************************************************
580 * Tell media player to start playing.
581 **************************************************************************/
582 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
583 libvlc_exception_t *p_e )
585 input_thread_t * p_input_thread;
587 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
589 /* A thread already exists, send it a play message */
590 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
591 vlc_object_release( p_input_thread );
595 /* Ignore previous exception */
596 libvlc_exception_clear( p_e );
598 vlc_mutex_lock( &p_mi->object_lock );
602 libvlc_exception_raise( p_e, "no associated media descriptor" );
603 vlc_mutex_unlock( &p_mi->object_lock );
607 p_mi->p_input_thread = input_CreateThread(
608 p_mi->p_libvlc_instance->p_libvlc_int, p_mi->p_md->p_input_item );
611 if( !p_mi->p_input_thread )
613 vlc_mutex_unlock( &p_mi->object_lock );
617 p_input_thread = p_mi->p_input_thread;
619 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
620 if( p_mi->drawable.xid )
621 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
623 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
624 if( p_mi->drawable.hwnd != NULL )
626 vlc_value_t val = { .p_address = p_mi->drawable.hwnd };
627 var_Set( p_input_thread, "drawable-hwnd", val );
630 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
631 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
632 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
634 vlc_mutex_unlock( &p_mi->object_lock );
637 /**************************************************************************
639 **************************************************************************/
640 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
641 libvlc_exception_t *p_e )
643 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
645 if( !p_input_thread )
648 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
650 if( state == libvlc_Playing )
652 if( libvlc_media_player_can_pause( p_mi, p_e ) )
653 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
655 libvlc_media_player_stop( p_mi, p_e );
658 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
660 vlc_object_release( p_input_thread );
663 /**************************************************************************
664 * Tells whether the media player is currently playing.
666 * Enter with lock held.
667 **************************************************************************/
668 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
669 libvlc_exception_t *p_e )
671 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
673 if( !p_input_thread )
676 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
678 vlc_object_release( p_input_thread );
680 if( state == libvlc_Playing )
688 /**************************************************************************
690 **************************************************************************/
691 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
692 libvlc_exception_t *p_e )
694 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
696 if( state == libvlc_Playing || state == libvlc_Paused )
698 /* Send a stop notification event only if we are in playing or
700 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
702 /* Construct and send the event */
703 libvlc_event_t event;
704 event.type = libvlc_MediaPlayerStopped;
705 libvlc_event_send( p_mi->p_event_manager, &event );
708 if( p_mi->b_own_its_input_thread )
710 vlc_mutex_lock( &p_mi->object_lock );
711 release_input_thread( p_mi, true ); /* This will stop the input thread */
712 vlc_mutex_unlock( &p_mi->object_lock );
716 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
718 if( !p_input_thread )
721 input_StopThread( p_input_thread, true );
722 vlc_object_release( p_input_thread );
726 /**************************************************************************
728 **************************************************************************/
729 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
731 libvlc_exception_t *p_e )
734 p_mi->drawable.xid = drawable;
737 /**************************************************************************
739 **************************************************************************/
740 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
742 return p_mi->drawable.xid;
745 /**************************************************************************
747 **************************************************************************/
748 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
750 libvlc_exception_t *p_e )
753 p_mi->drawable.hwnd = drawable;
756 /**************************************************************************
758 **************************************************************************/
759 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
761 return p_mi->drawable.hwnd;
764 /**************************************************************************
766 **************************************************************************/
767 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
768 libvlc_drawable_t drawable,
769 libvlc_exception_t *p_e )
772 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
773 p_mi->drawable.hwnd = (HWND)drawable;
775 libvlc_exception_raise(p_e, "Operation not supported");
777 p_mi->drawable.xid = drawable;
781 /**************************************************************************
783 **************************************************************************/
785 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi,
786 libvlc_exception_t *p_e )
791 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
792 return (libvlc_drawable_t)p_mi->drawable.hwnd;
796 return p_mi->drawable.xid;
800 /**************************************************************************
801 * Getters for stream information
802 **************************************************************************/
803 libvlc_time_t libvlc_media_player_get_length(
804 libvlc_media_player_t *p_mi,
805 libvlc_exception_t *p_e )
807 input_thread_t *p_input_thread;
810 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
811 if( !p_input_thread )
814 var_Get( p_input_thread, "length", &val );
815 vlc_object_release( p_input_thread );
817 return (val.i_time+500LL)/1000LL;
820 libvlc_time_t libvlc_media_player_get_time(
821 libvlc_media_player_t *p_mi,
822 libvlc_exception_t *p_e )
824 input_thread_t *p_input_thread;
827 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
828 if( !p_input_thread )
831 var_Get( p_input_thread , "time", &val );
832 vlc_object_release( p_input_thread );
833 return (val.i_time+500LL)/1000LL;
836 void libvlc_media_player_set_time(
837 libvlc_media_player_t *p_mi,
839 libvlc_exception_t *p_e )
841 input_thread_t *p_input_thread;
844 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
845 if( !p_input_thread )
848 value.i_time = time*1000LL;
849 var_Set( p_input_thread, "time", value );
850 vlc_object_release( p_input_thread );
853 void libvlc_media_player_set_position(
854 libvlc_media_player_t *p_mi,
856 libvlc_exception_t *p_e )
858 input_thread_t *p_input_thread;
860 val.f_float = position;
862 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
863 if( !p_input_thread )
866 var_Set( p_input_thread, "position", val );
867 vlc_object_release( p_input_thread );
870 float libvlc_media_player_get_position(
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, "position", &val );
882 vlc_object_release( p_input_thread );
887 void libvlc_media_player_set_chapter(
888 libvlc_media_player_t *p_mi,
890 libvlc_exception_t *p_e )
892 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_Set( p_input_thread, "chapter", val );
901 vlc_object_release( p_input_thread );
904 int libvlc_media_player_get_chapter(
905 libvlc_media_player_t *p_mi,
906 libvlc_exception_t *p_e )
908 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_Get( p_input_thread, "chapter", &val );
916 vlc_object_release( p_input_thread );
921 int libvlc_media_player_get_chapter_count(
922 libvlc_media_player_t *p_mi,
923 libvlc_exception_t *p_e )
925 input_thread_t *p_input_thread;
928 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
929 if( !p_input_thread )
932 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
933 vlc_object_release( p_input_thread );
938 int libvlc_media_player_get_chapter_count_for_title(
939 libvlc_media_player_t *p_mi,
941 libvlc_exception_t *p_e )
943 input_thread_t *p_input_thread;
946 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
947 if( !p_input_thread )
951 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
953 vlc_object_release( p_input_thread );
956 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
957 vlc_object_release( p_input_thread );
963 void libvlc_media_player_set_title(
964 libvlc_media_player_t *p_mi,
966 libvlc_exception_t *p_e )
968 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_Set( p_input_thread, "title", val );
977 vlc_object_release( p_input_thread );
980 libvlc_event_t event;
981 event.type = libvlc_MediaPlayerTitleChanged;
982 event.u.media_player_title_changed.new_title = i_title;
983 libvlc_event_send( p_mi->p_event_manager, &event );
986 int libvlc_media_player_get_title(
987 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 )
997 var_Get( p_input_thread, "title", &val );
998 vlc_object_release( p_input_thread );
1003 int libvlc_media_player_get_title_count(
1004 libvlc_media_player_t *p_mi,
1005 libvlc_exception_t *p_e )
1007 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_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1015 vlc_object_release( p_input_thread );
1020 void libvlc_media_player_next_chapter(
1021 libvlc_media_player_t *p_mi,
1022 libvlc_exception_t *p_e )
1024 input_thread_t *p_input_thread;
1026 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1027 if( !p_input_thread )
1030 int i_type = var_Type( p_input_thread, "next-chapter" );
1033 var_Set( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1034 "next-chapter":"next-title", val );
1036 vlc_object_release( p_input_thread );
1039 void libvlc_media_player_previous_chapter(
1040 libvlc_media_player_t *p_mi,
1041 libvlc_exception_t *p_e )
1043 input_thread_t *p_input_thread;
1045 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1046 if( !p_input_thread )
1049 int i_type = var_Type( p_input_thread, "next-chapter" );
1052 var_Set( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1053 "prev-chapter":"prev-title", val );
1055 vlc_object_release( p_input_thread );
1058 float libvlc_media_player_get_fps(
1059 libvlc_media_player_t *p_mi,
1060 libvlc_exception_t *p_e)
1062 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1065 if( p_input_thread )
1067 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1069 vlc_object_release( p_input_thread );
1074 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1075 libvlc_exception_t *p_e)
1077 input_thread_t *p_input_thread =
1078 libvlc_get_input_thread ( p_mi, p_e);
1079 if ( !p_input_thread )
1082 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1084 vlc_object_release( p_input_thread );
1087 vlc_object_release( p_input_thread );
1091 void libvlc_media_player_set_rate(
1092 libvlc_media_player_t *p_mi,
1094 libvlc_exception_t *p_e )
1096 input_thread_t *p_input_thread;
1100 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1101 if( !p_input_thread )
1104 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1105 if( (rate < 0.0) && !b_can_rewind )
1107 vlc_object_release( p_input_thread );
1108 libvlc_exception_raise( p_e, "Rate value is invalid" );
1112 val.i_int = 1000.0f/rate;
1113 var_Set( p_input_thread, "rate", val );
1114 vlc_object_release( p_input_thread );
1117 float libvlc_media_player_get_rate(
1118 libvlc_media_player_t *p_mi,
1119 libvlc_exception_t *p_e )
1121 input_thread_t *p_input_thread;
1125 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1126 if( !p_input_thread )
1127 return 0.0; /* rate < 0 indicates rewind */
1129 var_Get( p_input_thread, "rate", &val );
1130 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1131 if( (val.i_int < 0) && !b_can_rewind )
1133 libvlc_exception_raise( p_e, "invalid rate" );
1136 vlc_object_release( p_input_thread );
1138 return (float)1000.0f/val.i_int;
1141 libvlc_state_t libvlc_media_player_get_state(
1142 libvlc_media_player_t *p_mi,
1143 libvlc_exception_t *p_e )
1145 input_thread_t *p_input_thread;
1146 libvlc_state_t state = libvlc_Ended;
1149 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1150 if( !p_input_thread )
1152 /* We do return the right value, no need to throw an exception */
1153 if( libvlc_exception_raised( p_e ) )
1154 libvlc_exception_clear( p_e );
1158 var_Get( p_input_thread, "state", &val );
1159 state = vlc_to_libvlc_state(val.i_int);
1161 if( state == PLAYING_S )
1164 caching = var_GetFloat( p_input_thread, "cache" );
1165 if( caching > 0.0 && caching < 1.0 )
1166 state = libvlc_Buffering;
1168 vlc_object_release( p_input_thread );
1172 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1173 libvlc_exception_t *p_e )
1175 input_thread_t *p_input_thread;
1178 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1179 if ( !p_input_thread )
1181 /* We do return the right value, no need to throw an exception */
1182 if( libvlc_exception_raised( p_e ) )
1183 libvlc_exception_clear( p_e );
1186 var_Get( p_input_thread, "can-seek", &val );
1187 vlc_object_release( p_input_thread );
1192 /* internal function, used by audio, video */
1193 libvlc_track_description_t *
1194 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1195 const char *psz_variable,
1196 libvlc_exception_t *p_e )
1198 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1203 vlc_value_t val_list, text_list;
1204 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1206 if( val_list.p_list->i_count <= 0 ) /* no tracks */
1209 libvlc_track_description_t *p_track_description, *p_actual, *p_previous;
1210 p_track_description = ( libvlc_track_description_t * )
1211 malloc( sizeof( libvlc_track_description_t ) );
1212 if ( !p_track_description )
1214 var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1215 &val_list, &text_list);
1216 vlc_object_release( p_input );
1217 libvlc_exception_raise( p_e, "no enough memory" );
1220 p_actual = p_track_description;
1222 for( int i = 0; i < val_list.p_list->i_count; i++ )
1226 p_actual = ( libvlc_track_description_t * )
1227 malloc( sizeof( libvlc_track_description_t ) );
1230 libvlc_track_description_release( p_track_description );
1231 var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1232 &val_list, &text_list);
1233 vlc_object_release( p_input );
1234 libvlc_exception_raise( p_e, "no enough memory" );
1238 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1239 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1240 p_actual->p_next = NULL;
1242 p_previous->p_next = p_actual;
1243 p_previous = p_actual;
1246 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1247 vlc_object_release( p_input );
1249 return p_track_description;
1252 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1254 libvlc_track_description_t *p_actual, *p_before;
1259 free( p_actual->psz_name );
1260 p_before = p_actual;
1261 p_actual = p_before->p_next;
1266 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1267 libvlc_exception_t *p_e )
1269 input_thread_t *p_input_thread;
1272 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1273 if ( !p_input_thread )
1275 /* We do return the right value, no need to throw an exception */
1276 if( libvlc_exception_raised( p_e ) )
1277 libvlc_exception_clear( p_e );
1280 var_Get( p_input_thread, "can-pause", &val );
1281 vlc_object_release( p_input_thread );