1 /*****************************************************************************
2 * media_player.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005-2009 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27 #include <vlc_demux.h>
28 #include <vlc_input.h>
34 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
35 vlc_value_t oldval, vlc_value_t newval,
38 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
39 vlc_value_t oldval, vlc_value_t newval,
42 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
43 vlc_value_t oldval, vlc_value_t newval,
46 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
47 vlc_value_t oldval, vlc_value_t newval, void *p_data );
49 static const libvlc_state_t vlc_to_libvlc_state_array[] =
51 [INIT_S] = libvlc_NothingSpecial,
52 [OPENING_S] = libvlc_Opening,
53 [PLAYING_S] = libvlc_Playing,
54 [PAUSE_S] = libvlc_Paused,
55 [END_S] = libvlc_Ended,
56 [ERROR_S] = libvlc_Error,
59 static inline libvlc_state_t vlc_to_libvlc_state( int vlc_state )
61 if( vlc_state < 0 || vlc_state > 6 )
64 return vlc_to_libvlc_state_array[vlc_state];
68 * Release the associated input thread.
70 * Object lock is NOT held.
72 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
74 input_thread_t * p_input_thread;
76 if( !p_mi || !p_mi->p_input_thread )
79 p_input_thread = p_mi->p_input_thread;
81 var_DelCallback( p_input_thread, "can-seek",
82 input_seekable_changed, p_mi );
83 var_DelCallback( p_input_thread, "can-pause",
84 input_pausable_changed, p_mi );
85 var_DelCallback( p_input_thread, "intf-event",
86 input_event_changed, p_mi );
88 /* We owned this one */
89 input_Stop( p_input_thread, b_input_abort );
90 vlc_thread_join( p_input_thread );
92 var_Destroy( p_input_thread, "drawable-hwnd" );
93 var_Destroy( p_input_thread, "drawable-xid" );
94 var_Destroy( p_input_thread, "drawable-agl" );
96 vlc_object_release( p_input_thread );
98 p_mi->p_input_thread = NULL;
102 * Retrieve the input thread. Be sure to release the object
103 * once you are done with it. (libvlc Internal)
105 * Function will lock the object.
107 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
108 libvlc_exception_t *p_e )
110 input_thread_t *p_input_thread;
112 if( !p_mi ) RAISENULL( "Media Instance is NULL" );
114 vlc_mutex_lock( &p_mi->object_lock );
116 if( !p_mi->p_input_thread )
118 vlc_mutex_unlock( &p_mi->object_lock );
119 RAISENULL( "Input is NULL" );
122 p_input_thread = p_mi->p_input_thread;
123 vlc_object_hold( p_input_thread );
125 vlc_mutex_unlock( &p_mi->object_lock );
127 return p_input_thread;
131 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
132 vlc_value_t oldval, vlc_value_t newval,
138 libvlc_media_player_t * p_mi = p_userdata;
139 libvlc_event_t event;
141 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
142 event.type = libvlc_MediaPlayerSeekableChanged;
143 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
145 libvlc_event_send( p_mi->p_event_manager, &event );
150 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
151 vlc_value_t oldval, vlc_value_t newval,
157 libvlc_media_player_t * p_mi = p_userdata;
158 libvlc_event_t event;
160 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
161 event.type = libvlc_MediaPlayerPausableChanged;
162 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
164 libvlc_event_send( p_mi->p_event_manager, &event );
169 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
170 vlc_value_t oldval, vlc_value_t newval,
174 input_thread_t * p_input = (input_thread_t *)p_this;
175 libvlc_media_player_t * p_mi = p_userdata;
176 libvlc_event_t event;
178 assert( !strcmp( psz_cmd, "intf-event" ) );
180 if( newval.i_int == INPUT_EVENT_STATE )
182 libvlc_state_t libvlc_state;
184 switch ( var_GetInteger( p_input, "state" ) )
187 libvlc_state = libvlc_NothingSpecial;
188 event.type = libvlc_MediaPlayerNothingSpecial;
191 libvlc_state = libvlc_Opening;
192 event.type = libvlc_MediaPlayerOpening;
195 libvlc_state = libvlc_Playing;
196 event.type = libvlc_MediaPlayerPlaying;
199 libvlc_state = libvlc_Paused;
200 event.type = libvlc_MediaPlayerPaused;
203 libvlc_state = libvlc_Ended;
204 event.type = libvlc_MediaPlayerEndReached;
207 libvlc_state = libvlc_Error;
208 event.type = libvlc_MediaPlayerEncounteredError;
215 libvlc_media_set_state( p_mi->p_md, libvlc_state, NULL );
216 libvlc_event_send( p_mi->p_event_manager, &event );
218 else if( newval.i_int == INPUT_EVENT_POSITION )
220 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
221 return VLC_SUCCESS; /* Don't send the position while stopped */
224 event.type = libvlc_MediaPlayerPositionChanged;
225 event.u.media_player_position_changed.new_position =
226 var_GetFloat( p_input, "position" );
227 libvlc_event_send( p_mi->p_event_manager, &event );
230 event.type = libvlc_MediaPlayerTimeChanged;
231 event.u.media_player_time_changed.new_time =
232 var_GetTime( p_input, "time" );
233 libvlc_event_send( p_mi->p_event_manager, &event );
235 else if( newval.i_int == INPUT_EVENT_LENGTH )
237 event.type = libvlc_MediaPlayerLengthChanged;
238 event.u.media_player_length_changed.new_length =
239 var_GetTime( p_input, "length" );
240 libvlc_event_send( p_mi->p_event_manager, &event );
247 static void libvlc_media_player_destroy( libvlc_media_player_t * );
249 /**************************************************************************
250 * Create a Media Instance object.
253 * - All items created by _new start with a refcount set to 1.
254 * - Accessor _release decrease the refcount by 1, if after that
255 * operation the refcount is 0, the object is destroyed.
256 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
258 * Object locking strategy:
259 * - No lock held while in constructor.
260 * - When accessing any member variable this lock is held. (XXX who locks?)
261 * - When attempting to destroy the object the lock is also held.
262 **************************************************************************/
263 libvlc_media_player_t *
264 libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
265 libvlc_exception_t * p_e )
267 libvlc_media_player_t * p_mi;
269 if( !p_libvlc_instance )
271 libvlc_exception_raise( p_e, "invalid libvlc instance" );
275 p_mi = malloc( sizeof(libvlc_media_player_t) );
278 libvlc_exception_raise( p_e, "Not enough memory" );
282 p_mi->drawable.agl = 0;
283 p_mi->drawable.xid = 0;
284 p_mi->drawable.hwnd = NULL;
285 p_mi->drawable.nsobject = NULL;
286 p_mi->p_libvlc_instance = p_libvlc_instance;
287 p_mi->p_input_thread = NULL;
288 p_mi->i_refcount = 1;
289 vlc_mutex_init( &p_mi->object_lock );
290 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
291 p_libvlc_instance, p_e );
292 if( libvlc_exception_raised( p_e ) )
294 vlc_mutex_destroy( &p_mi->object_lock );
299 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
300 libvlc_MediaPlayerNothingSpecial, p_e );
301 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
302 libvlc_MediaPlayerOpening, p_e );
303 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
304 libvlc_MediaPlayerBuffering, p_e );
305 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
306 libvlc_MediaPlayerPlaying, p_e );
307 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
308 libvlc_MediaPlayerPaused, p_e );
309 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
310 libvlc_MediaPlayerStopped, p_e );
311 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
312 libvlc_MediaPlayerForward, p_e );
313 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
314 libvlc_MediaPlayerBackward, p_e );
315 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
316 libvlc_MediaPlayerEndReached, p_e );
317 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
318 libvlc_MediaPlayerEncounteredError, p_e );
320 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
321 libvlc_MediaPlayerPositionChanged, p_e );
322 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
323 libvlc_MediaPlayerTimeChanged, p_e );
324 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
325 libvlc_MediaPlayerLengthChanged, p_e );
326 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
327 libvlc_MediaPlayerTitleChanged, p_e );
328 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
329 libvlc_MediaPlayerSeekableChanged, p_e );
330 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
331 libvlc_MediaPlayerPausableChanged, p_e );
333 /* Snapshot initialization */
334 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
335 libvlc_MediaPlayerSnapshotTaken, p_e );
337 /* Attach a var callback to the global object to provide the glue between
338 vout_thread that generates the event and media_player that re-emits it
339 with its own event manager
341 var_Create( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
342 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
343 var_AddCallback( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
344 SnapshotTakenCallback, p_mi );
349 /**************************************************************************
350 * Create a Media Instance object with a media descriptor.
351 **************************************************************************/
352 libvlc_media_player_t *
353 libvlc_media_player_new_from_media(
354 libvlc_media_t * p_md,
355 libvlc_exception_t *p_e )
357 libvlc_media_player_t * p_mi;
359 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
363 libvlc_media_retain( p_md );
369 /**************************************************************************
370 * Destroy a Media Instance object (libvlc internal)
372 * Warning: No lock held here, but hey, this is internal. Caller must lock.
373 **************************************************************************/
374 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
376 input_thread_t *p_input_thread;
377 libvlc_exception_t p_e;
381 /* Detach Callback from the main libvlc object */
382 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
383 "vout-snapshottaken", SnapshotTakenCallback, p_mi );
385 libvlc_exception_init( &p_e );
386 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
388 if( libvlc_exception_raised( &p_e ) )
389 /* no need to worry about no input thread */
390 libvlc_exception_clear( &p_e );
392 release_input_thread( p_mi, true );
394 libvlc_event_manager_release( p_mi->p_event_manager );
395 libvlc_media_release( p_mi->p_md );
396 vlc_mutex_destroy( &p_mi->object_lock );
400 /**************************************************************************
401 * Release a Media Instance object.
403 * Function does the locking.
404 **************************************************************************/
405 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
410 vlc_mutex_lock( &p_mi->object_lock );
411 destroy = !--p_mi->i_refcount;
412 vlc_mutex_unlock( &p_mi->object_lock );
415 libvlc_media_player_destroy( p_mi );
418 /**************************************************************************
419 * Retain a Media Instance object.
421 * Caller must hold the lock.
422 **************************************************************************/
423 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
427 vlc_mutex_lock( &p_mi->object_lock );
429 vlc_mutex_unlock( &p_mi->object_lock );
432 /**************************************************************************
433 * Set the Media descriptor associated with the instance.
435 * Enter without lock -- function will lock the object.
436 **************************************************************************/
437 void libvlc_media_player_set_media(
438 libvlc_media_player_t *p_mi,
439 libvlc_media_t *p_md,
440 libvlc_exception_t *p_e )
447 vlc_mutex_lock( &p_mi->object_lock );
449 /* FIXME I am not sure if it is a user request or on die(eof/error)
451 release_input_thread( p_mi,
452 p_mi->p_input_thread &&
453 !p_mi->p_input_thread->b_eof &&
454 !p_mi->p_input_thread->b_error );
457 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
459 libvlc_media_release( p_mi->p_md );
464 vlc_mutex_unlock( &p_mi->object_lock );
465 return; /* It is ok to pass a NULL md */
468 libvlc_media_retain( p_md );
471 /* The policy here is to ignore that we were created using a different
472 * libvlc_instance, because we don't really care */
473 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
475 vlc_mutex_unlock( &p_mi->object_lock );
478 /**************************************************************************
479 * Get the Media descriptor associated with the instance.
480 **************************************************************************/
482 libvlc_media_player_get_media(
483 libvlc_media_player_t *p_mi,
484 libvlc_exception_t *p_e )
491 libvlc_media_retain( p_mi->p_md );
495 /**************************************************************************
496 * Get the event Manager.
497 **************************************************************************/
498 libvlc_event_manager_t *
499 libvlc_media_player_event_manager(
500 libvlc_media_player_t *p_mi,
501 libvlc_exception_t *p_e )
505 return p_mi->p_event_manager;
508 /**************************************************************************
509 * Trigger a snapshot Taken Event.
510 *************************************************************************/
511 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
512 vlc_value_t oldval, vlc_value_t newval, void *p_data )
514 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
517 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
518 libvlc_event_t event ;
519 event.type = libvlc_MediaPlayerSnapshotTaken ;
520 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
521 /* Snapshot psz data is a vlc_variable owned by libvlc object .
522 Its memmory management is taken care by the obj*/
523 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p"
524 " (%s)", event.u.media_player_snapshot_taken.psz_filename,
525 event.u.media_player_snapshot_taken.psz_filename );
526 libvlc_event_send( p_mi->p_event_manager, &event );
531 /**************************************************************************
532 * Tell media player to start playing.
533 **************************************************************************/
534 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
535 libvlc_exception_t *p_e )
537 input_thread_t * p_input_thread;
539 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
541 /* A thread already exists, send it a play message */
542 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
543 vlc_object_release( p_input_thread );
547 /* Ignore previous exception */
548 libvlc_exception_clear( p_e );
550 vlc_mutex_lock( &p_mi->object_lock );
554 libvlc_exception_raise( p_e, "no associated media descriptor" );
555 vlc_mutex_unlock( &p_mi->object_lock );
559 p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
560 p_mi->p_md->p_input_item, NULL, NULL );
562 if( !p_mi->p_input_thread )
564 vlc_mutex_unlock( &p_mi->object_lock );
568 p_input_thread = p_mi->p_input_thread;
570 var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
571 if( p_mi->drawable.agl )
572 var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
574 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
575 if( p_mi->drawable.xid )
576 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
578 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
579 if( p_mi->drawable.hwnd != NULL )
581 vlc_value_t val = { .p_address = p_mi->drawable.hwnd };
582 var_Set( p_input_thread, "drawable-hwnd", val );
585 var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
586 if( p_mi->drawable.nsobject != NULL )
588 vlc_value_t val = { .p_address = p_mi->drawable.nsobject };
589 var_Set( p_input_thread, "drawable-nsobject", val );
592 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
593 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
594 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
596 if( input_Start( p_input_thread ) )
598 vlc_object_release( p_input_thread );
599 p_mi->p_input_thread = NULL;
602 vlc_mutex_unlock( &p_mi->object_lock );
605 /**************************************************************************
607 **************************************************************************/
608 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
609 libvlc_exception_t *p_e )
611 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
612 if( !p_input_thread )
615 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
616 if( state == libvlc_Playing || state == libvlc_Buffering )
618 if( libvlc_media_player_can_pause( p_mi, p_e ) )
619 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
621 libvlc_media_player_stop( p_mi, p_e );
624 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
626 vlc_object_release( p_input_thread );
629 /**************************************************************************
630 * Tells whether the media player is currently playing.
632 * Enter with lock held.
633 **************************************************************************/
634 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
635 libvlc_exception_t *p_e )
637 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
638 return (libvlc_Playing == state) || (libvlc_Buffering == state);
641 /**************************************************************************
643 **************************************************************************/
644 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
645 libvlc_exception_t *p_e )
647 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
649 if( state == libvlc_Playing ||
650 state == libvlc_Paused ||
651 state == libvlc_Buffering )
653 /* Send a stop notification event only if we are in playing,
654 * buffering or paused states */
655 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
657 /* Construct and send the event */
658 libvlc_event_t event;
659 event.type = libvlc_MediaPlayerStopped;
660 libvlc_event_send( p_mi->p_event_manager, &event );
663 vlc_mutex_lock( &p_mi->object_lock );
664 release_input_thread( p_mi, true ); /* This will stop the input thread */
665 vlc_mutex_unlock( &p_mi->object_lock );
668 /**************************************************************************
670 **************************************************************************/
671 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
673 libvlc_exception_t *p_e )
676 p_mi->drawable.nsobject = drawable;
679 /**************************************************************************
681 **************************************************************************/
682 uint32_t libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
684 return p_mi->drawable.nsobject;
687 /**************************************************************************
689 **************************************************************************/
690 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
692 libvlc_exception_t *p_e )
695 p_mi->drawable.agl = drawable;
698 /**************************************************************************
700 **************************************************************************/
701 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
703 return p_mi->drawable.agl;
706 /**************************************************************************
708 **************************************************************************/
709 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
711 libvlc_exception_t *p_e )
714 p_mi->drawable.xid = drawable;
717 /**************************************************************************
719 **************************************************************************/
720 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
722 return p_mi->drawable.xid;
725 /**************************************************************************
727 **************************************************************************/
728 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
730 libvlc_exception_t *p_e )
733 p_mi->drawable.hwnd = drawable;
736 /**************************************************************************
738 **************************************************************************/
739 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
741 return p_mi->drawable.hwnd;
744 /**************************************************************************
746 **************************************************************************/
747 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
748 libvlc_drawable_t drawable,
749 libvlc_exception_t *p_e )
752 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
753 p_mi->drawable.hwnd = (HWND)drawable;
755 libvlc_exception_raise(p_e, "Operation not supported");
756 #elif defined(__APPLE__)
757 p_mi->drawable.agl = drawable;
760 p_mi->drawable.xid = drawable;
765 /**************************************************************************
767 **************************************************************************/
769 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi,
770 libvlc_exception_t *p_e )
775 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
776 return (libvlc_drawable_t)p_mi->drawable.hwnd;
779 #elif defined(__APPLE__)
780 return p_mi->drawable.agl;
782 return p_mi->drawable.xid;
786 /**************************************************************************
787 * Getters for stream information
788 **************************************************************************/
789 libvlc_time_t libvlc_media_player_get_length(
790 libvlc_media_player_t *p_mi,
791 libvlc_exception_t *p_e )
793 input_thread_t *p_input_thread;
796 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
797 if( !p_input_thread )
800 var_Get( p_input_thread, "length", &val );
801 vlc_object_release( p_input_thread );
803 return (val.i_time+500LL)/1000LL;
806 libvlc_time_t libvlc_media_player_get_time(
807 libvlc_media_player_t *p_mi,
808 libvlc_exception_t *p_e )
810 input_thread_t *p_input_thread;
813 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
814 if( !p_input_thread )
817 var_Get( p_input_thread , "time", &val );
818 vlc_object_release( p_input_thread );
819 return (val.i_time+500LL)/1000LL;
822 void libvlc_media_player_set_time(
823 libvlc_media_player_t *p_mi,
825 libvlc_exception_t *p_e )
827 input_thread_t *p_input_thread;
829 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
830 if( !p_input_thread )
833 var_SetTime( p_input_thread, "time", time*1000LL );
834 vlc_object_release( p_input_thread );
837 void libvlc_media_player_set_position(
838 libvlc_media_player_t *p_mi,
840 libvlc_exception_t *p_e )
842 input_thread_t *p_input_thread;
844 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
845 if( !p_input_thread )
848 var_SetFloat( p_input_thread, "position", position );
849 vlc_object_release( p_input_thread );
852 float libvlc_media_player_get_position(
853 libvlc_media_player_t *p_mi,
854 libvlc_exception_t *p_e )
856 input_thread_t *p_input_thread;
859 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
860 if( !p_input_thread )
863 var_Get( p_input_thread, "position", &val );
864 vlc_object_release( p_input_thread );
869 void libvlc_media_player_set_chapter(
870 libvlc_media_player_t *p_mi,
872 libvlc_exception_t *p_e )
874 input_thread_t *p_input_thread;
876 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
877 if( !p_input_thread )
880 var_SetInteger( p_input_thread, "chapter", chapter );
881 vlc_object_release( p_input_thread );
884 int libvlc_media_player_get_chapter(
885 libvlc_media_player_t *p_mi,
886 libvlc_exception_t *p_e )
888 input_thread_t *p_input_thread;
891 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
892 if( !p_input_thread )
895 var_Get( p_input_thread, "chapter", &val );
896 vlc_object_release( p_input_thread );
901 int libvlc_media_player_get_chapter_count(
902 libvlc_media_player_t *p_mi,
903 libvlc_exception_t *p_e )
905 input_thread_t *p_input_thread;
908 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
909 if( !p_input_thread )
912 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
913 vlc_object_release( p_input_thread );
918 int libvlc_media_player_get_chapter_count_for_title(
919 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 )
931 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
933 vlc_object_release( p_input_thread );
936 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
937 vlc_object_release( p_input_thread );
943 void libvlc_media_player_set_title(
944 libvlc_media_player_t *p_mi,
946 libvlc_exception_t *p_e )
948 input_thread_t *p_input_thread;
950 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
951 if( !p_input_thread )
954 var_SetInteger( p_input_thread, "title", i_title );
955 vlc_object_release( p_input_thread );
958 libvlc_event_t event;
959 event.type = libvlc_MediaPlayerTitleChanged;
960 event.u.media_player_title_changed.new_title = i_title;
961 libvlc_event_send( p_mi->p_event_manager, &event );
964 int libvlc_media_player_get_title(
965 libvlc_media_player_t *p_mi,
966 libvlc_exception_t *p_e )
968 input_thread_t *p_input_thread;
971 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
972 if( !p_input_thread )
975 var_Get( p_input_thread, "title", &val );
976 vlc_object_release( p_input_thread );
981 int libvlc_media_player_get_title_count(
982 libvlc_media_player_t *p_mi,
983 libvlc_exception_t *p_e )
985 input_thread_t *p_input_thread;
988 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
989 if( !p_input_thread )
992 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
993 vlc_object_release( p_input_thread );
998 void libvlc_media_player_next_chapter(
999 libvlc_media_player_t *p_mi,
1000 libvlc_exception_t *p_e )
1002 input_thread_t *p_input_thread;
1004 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1005 if( !p_input_thread )
1008 int i_type = var_Type( p_input_thread, "next-chapter" );
1009 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1010 "next-chapter":"next-title", true );
1012 vlc_object_release( p_input_thread );
1015 void libvlc_media_player_previous_chapter(
1016 libvlc_media_player_t *p_mi,
1017 libvlc_exception_t *p_e )
1019 input_thread_t *p_input_thread;
1021 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1022 if( !p_input_thread )
1025 int i_type = var_Type( p_input_thread, "next-chapter" );
1026 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1027 "prev-chapter":"prev-title", true );
1029 vlc_object_release( p_input_thread );
1032 float libvlc_media_player_get_fps(
1033 libvlc_media_player_t *p_mi,
1034 libvlc_exception_t *p_e)
1036 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1039 if( p_input_thread )
1041 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1043 vlc_object_release( p_input_thread );
1048 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1049 libvlc_exception_t *p_e)
1051 input_thread_t *p_input_thread =
1052 libvlc_get_input_thread ( p_mi, p_e);
1053 if ( !p_input_thread )
1056 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1058 vlc_object_release( p_input_thread );
1061 vlc_object_release( p_input_thread );
1065 void libvlc_media_player_set_rate(
1066 libvlc_media_player_t *p_mi,
1068 libvlc_exception_t *p_e )
1070 input_thread_t *p_input_thread;
1073 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1074 if( !p_input_thread )
1077 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1078 if( (rate < 0.0) && !b_can_rewind )
1080 vlc_object_release( p_input_thread );
1081 libvlc_exception_raise( p_e, "Rate value is invalid" );
1085 var_SetInteger( p_input_thread, "rate", 1000.0f/rate );
1086 vlc_object_release( p_input_thread );
1089 float libvlc_media_player_get_rate(
1090 libvlc_media_player_t *p_mi,
1091 libvlc_exception_t *p_e )
1093 input_thread_t *p_input_thread;
1097 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1098 if( !p_input_thread )
1099 return 0.0; /* rate < 0 indicates rewind */
1101 var_Get( p_input_thread, "rate", &val );
1102 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1103 if( (val.i_int < 0) && !b_can_rewind )
1105 libvlc_exception_raise( p_e, "invalid rate" );
1108 vlc_object_release( p_input_thread );
1110 return (float)1000.0f/val.i_int;
1113 libvlc_state_t libvlc_media_player_get_state(
1114 libvlc_media_player_t *p_mi,
1115 libvlc_exception_t *p_e )
1117 input_thread_t *p_input_thread;
1118 libvlc_state_t state = libvlc_Ended;
1121 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1122 if( !p_input_thread )
1124 /* We do return the right value, no need to throw an exception */
1125 if( libvlc_exception_raised( p_e ) )
1126 libvlc_exception_clear( p_e );
1130 var_Get( p_input_thread, "state", &val );
1131 state = vlc_to_libvlc_state(val.i_int);
1133 if( state == libvlc_Playing )
1136 caching = var_GetFloat( p_input_thread, "cache" );
1137 if( caching > 0.0 && caching < 1.0 )
1138 state = libvlc_Buffering;
1140 vlc_object_release( p_input_thread );
1144 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1145 libvlc_exception_t *p_e )
1147 input_thread_t *p_input_thread;
1150 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1151 if ( !p_input_thread )
1153 /* We do return the right value, no need to throw an exception */
1154 if( libvlc_exception_raised( p_e ) )
1155 libvlc_exception_clear( p_e );
1158 var_Get( p_input_thread, "can-seek", &val );
1159 vlc_object_release( p_input_thread );
1164 /* internal function, used by audio, video */
1165 libvlc_track_description_t *
1166 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1167 const char *psz_variable,
1168 libvlc_exception_t *p_e )
1170 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1171 libvlc_track_description_t *p_track_description = NULL,
1172 *p_actual, *p_previous;
1177 vlc_value_t val_list, text_list;
1178 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1181 if( val_list.p_list->i_count <= 0 )
1184 p_track_description = ( libvlc_track_description_t * )
1185 malloc( sizeof( libvlc_track_description_t ) );
1186 if ( !p_track_description )
1188 libvlc_exception_raise( p_e, "no enough memory" );
1191 p_actual = p_track_description;
1193 for( int i = 0; i < val_list.p_list->i_count; i++ )
1197 p_actual = ( libvlc_track_description_t * )
1198 malloc( sizeof( libvlc_track_description_t ) );
1201 libvlc_track_description_release( p_track_description );
1202 libvlc_exception_raise( p_e, "no enough memory" );
1206 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1207 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1208 p_actual->p_next = NULL;
1210 p_previous->p_next = p_actual;
1211 p_previous = p_actual;
1216 var_FreeList( &val_list, &text_list );
1217 vlc_object_release( p_input );
1219 return p_track_description;
1222 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1224 libvlc_track_description_t *p_actual, *p_before;
1229 free( p_actual->psz_name );
1230 p_before = p_actual;
1231 p_actual = p_before->p_next;
1236 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1237 libvlc_exception_t *p_e )
1239 input_thread_t *p_input_thread;
1242 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1243 if ( !p_input_thread )
1245 /* We do return the right value, no need to throw an exception */
1246 if( libvlc_exception_raised( p_e ) )
1247 libvlc_exception_clear( p_e );
1250 var_Get( p_input_thread, "can-pause", &val );
1251 vlc_object_release( p_input_thread );