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 *****************************************************************************/
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_events.h>
34 #include <vlc_demux.h>
35 #include <vlc_input.h>
40 #include "libvlc_internal.h"
41 #include "media_internal.h" // libvlc_media_set_state()
42 #include "media_player_internal.h"
45 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
46 vlc_value_t oldval, vlc_value_t newval,
49 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
50 vlc_value_t oldval, vlc_value_t newval,
53 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
54 vlc_value_t oldval, vlc_value_t newval,
57 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
58 vlc_value_t oldval, vlc_value_t newval, void *p_data );
60 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
63 * Release the associated input thread.
65 * Object lock is NOT held.
67 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
69 input_thread_t * p_input_thread;
71 if( !p_mi || !p_mi->p_input_thread )
74 p_input_thread = p_mi->p_input_thread;
76 var_DelCallback( p_input_thread, "can-seek",
77 input_seekable_changed, p_mi );
78 var_DelCallback( p_input_thread, "can-pause",
79 input_pausable_changed, p_mi );
80 var_DelCallback( p_input_thread, "intf-event",
81 input_event_changed, p_mi );
83 /* We owned this one */
84 input_Stop( p_input_thread, b_input_abort );
85 vlc_thread_join( p_input_thread );
87 var_Destroy( p_input_thread, "drawable-hwnd" );
88 var_Destroy( p_input_thread, "drawable-xid" );
89 var_Destroy( p_input_thread, "drawable-agl" );
91 vlc_object_release( p_input_thread );
93 p_mi->p_input_thread = NULL;
97 * Retrieve the input thread. Be sure to release the object
98 * once you are done with it. (libvlc Internal)
100 * Function will lock the object.
102 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
103 libvlc_exception_t *p_e )
105 input_thread_t *p_input_thread;
107 if( !p_mi ) RAISENULL( "Media Instance is NULL" );
109 vlc_mutex_lock( &p_mi->object_lock );
111 if( !p_mi->p_input_thread )
113 vlc_mutex_unlock( &p_mi->object_lock );
114 RAISENULL( "Input is NULL" );
117 p_input_thread = p_mi->p_input_thread;
118 vlc_object_hold( p_input_thread );
120 vlc_mutex_unlock( &p_mi->object_lock );
122 return p_input_thread;
126 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
127 vlc_value_t oldval, vlc_value_t newval,
133 libvlc_media_player_t * p_mi = p_userdata;
134 libvlc_event_t event;
136 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
137 event.type = libvlc_MediaPlayerSeekableChanged;
138 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
140 libvlc_event_send( p_mi->p_event_manager, &event );
145 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
146 vlc_value_t oldval, vlc_value_t newval,
152 libvlc_media_player_t * p_mi = p_userdata;
153 libvlc_event_t event;
155 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
156 event.type = libvlc_MediaPlayerPausableChanged;
157 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
159 libvlc_event_send( p_mi->p_event_manager, &event );
164 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
165 vlc_value_t oldval, vlc_value_t newval,
169 input_thread_t * p_input = (input_thread_t *)p_this;
170 libvlc_media_player_t * p_mi = p_userdata;
171 libvlc_event_t event;
173 assert( !strcmp( psz_cmd, "intf-event" ) );
175 if( newval.i_int == INPUT_EVENT_STATE )
177 libvlc_state_t libvlc_state;
179 switch ( var_GetInteger( p_input, "state" ) )
182 libvlc_state = libvlc_NothingSpecial;
183 event.type = libvlc_MediaPlayerNothingSpecial;
186 libvlc_state = libvlc_Opening;
187 event.type = libvlc_MediaPlayerOpening;
190 libvlc_state = libvlc_Playing;
191 event.type = libvlc_MediaPlayerPlaying;
194 libvlc_state = libvlc_Paused;
195 event.type = libvlc_MediaPlayerPaused;
198 libvlc_state = libvlc_Ended;
199 event.type = libvlc_MediaPlayerEndReached;
202 libvlc_state = libvlc_Error;
203 event.type = libvlc_MediaPlayerEncounteredError;
210 libvlc_media_set_state( p_mi->p_md, libvlc_state, NULL );
211 libvlc_event_send( p_mi->p_event_manager, &event );
213 else if( newval.i_int == INPUT_EVENT_ABORT )
215 libvlc_state_t libvlc_state = libvlc_Stopped;
216 event.type = libvlc_MediaPlayerStopped;
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_POSITION )
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 );
238 else if( newval.i_int == INPUT_EVENT_LENGTH )
240 event.type = libvlc_MediaPlayerLengthChanged;
241 event.u.media_player_length_changed.new_length =
242 var_GetTime( p_input, "length" );
243 libvlc_event_send( p_mi->p_event_manager, &event );
250 static void libvlc_media_player_destroy( libvlc_media_player_t * );
252 /**************************************************************************
253 * Create a Media Instance object.
256 * - All items created by _new start with a refcount set to 1.
257 * - Accessor _release decrease the refcount by 1, if after that
258 * operation the refcount is 0, the object is destroyed.
259 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
261 * Object locking strategy:
262 * - No lock held while in constructor.
263 * - When accessing any member variable this lock is held. (XXX who locks?)
264 * - When attempting to destroy the object the lock is also held.
265 **************************************************************************/
266 libvlc_media_player_t *
267 libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
268 libvlc_exception_t * p_e )
270 libvlc_media_player_t * p_mi;
272 if( !p_libvlc_instance )
274 libvlc_exception_raise( p_e, "invalid libvlc instance" );
278 p_mi = malloc( sizeof(libvlc_media_player_t) );
281 libvlc_exception_raise( p_e, "not enough memory" );
285 p_mi->drawable.agl = 0;
286 p_mi->drawable.xid = 0;
287 p_mi->drawable.hwnd = NULL;
288 p_mi->drawable.nsobject = NULL;
289 p_mi->p_libvlc_instance = p_libvlc_instance;
290 p_mi->p_input_thread = NULL;
291 p_mi->i_refcount = 1;
292 vlc_mutex_init( &p_mi->object_lock );
293 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
294 p_libvlc_instance, p_e );
295 if( libvlc_exception_raised( p_e ) )
297 vlc_mutex_destroy( &p_mi->object_lock );
302 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
303 libvlc_MediaPlayerNothingSpecial, p_e );
304 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
305 libvlc_MediaPlayerOpening, p_e );
306 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
307 libvlc_MediaPlayerBuffering, p_e );
308 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
309 libvlc_MediaPlayerPlaying, p_e );
310 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
311 libvlc_MediaPlayerPaused, p_e );
312 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
313 libvlc_MediaPlayerStopped, p_e );
314 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
315 libvlc_MediaPlayerForward, p_e );
316 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
317 libvlc_MediaPlayerBackward, p_e );
318 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
319 libvlc_MediaPlayerEndReached, p_e );
320 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
321 libvlc_MediaPlayerEncounteredError, p_e );
323 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
324 libvlc_MediaPlayerPositionChanged, p_e );
325 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
326 libvlc_MediaPlayerTimeChanged, p_e );
327 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
328 libvlc_MediaPlayerLengthChanged, p_e );
329 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
330 libvlc_MediaPlayerTitleChanged, p_e );
331 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
332 libvlc_MediaPlayerSeekableChanged, p_e );
333 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
334 libvlc_MediaPlayerPausableChanged, p_e );
336 /* Snapshot initialization */
337 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
338 libvlc_MediaPlayerSnapshotTaken, p_e );
340 /* Attach a var callback to the global object to provide the glue between
341 vout_thread that generates the event and media_player that re-emits it
342 with its own event manager
344 var_Create( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
345 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
346 var_AddCallback( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken",
347 SnapshotTakenCallback, p_mi );
352 /**************************************************************************
353 * Create a Media Instance object with a media descriptor.
354 **************************************************************************/
355 libvlc_media_player_t *
356 libvlc_media_player_new_from_media(
357 libvlc_media_t * p_md,
358 libvlc_exception_t *p_e )
360 libvlc_media_player_t * p_mi;
362 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
366 libvlc_media_retain( p_md );
372 /**************************************************************************
373 * Destroy a Media Instance object (libvlc internal)
375 * Warning: No lock held here, but hey, this is internal. Caller must lock.
376 **************************************************************************/
377 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
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 /* Realease the input thread */
386 release_input_thread( p_mi, true );
388 libvlc_event_manager_release( p_mi->p_event_manager );
389 libvlc_media_release( p_mi->p_md );
390 vlc_mutex_destroy( &p_mi->object_lock );
394 /**************************************************************************
395 * Release a Media Instance object.
397 * Function does the locking.
398 **************************************************************************/
399 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
404 vlc_mutex_lock( &p_mi->object_lock );
405 destroy = !--p_mi->i_refcount;
406 vlc_mutex_unlock( &p_mi->object_lock );
409 libvlc_media_player_destroy( p_mi );
412 /**************************************************************************
413 * Retain a Media Instance object.
415 * Caller must hold the lock.
416 **************************************************************************/
417 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
421 vlc_mutex_lock( &p_mi->object_lock );
423 vlc_mutex_unlock( &p_mi->object_lock );
426 /**************************************************************************
427 * Set the Media descriptor associated with the instance.
429 * Enter without lock -- function will lock the object.
430 **************************************************************************/
431 void libvlc_media_player_set_media(
432 libvlc_media_player_t *p_mi,
433 libvlc_media_t *p_md,
434 libvlc_exception_t *p_e )
441 vlc_mutex_lock( &p_mi->object_lock );
443 /* FIXME I am not sure if it is a user request or on die(eof/error)
445 release_input_thread( p_mi,
446 p_mi->p_input_thread &&
447 !p_mi->p_input_thread->b_eof &&
448 !p_mi->p_input_thread->b_error );
451 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
453 libvlc_media_release( p_mi->p_md );
458 vlc_mutex_unlock( &p_mi->object_lock );
459 return; /* It is ok to pass a NULL md */
462 libvlc_media_retain( p_md );
465 /* The policy here is to ignore that we were created using a different
466 * libvlc_instance, because we don't really care */
467 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
469 vlc_mutex_unlock( &p_mi->object_lock );
472 /**************************************************************************
473 * Get the Media descriptor associated with the instance.
474 **************************************************************************/
476 libvlc_media_player_get_media(
477 libvlc_media_player_t *p_mi,
478 libvlc_exception_t *p_e )
483 vlc_mutex_lock( &p_mi->object_lock );
486 libvlc_media_retain( p_mi->p_md );
487 vlc_mutex_unlock( &p_mi->object_lock );
491 /**************************************************************************
492 * Get the event Manager.
493 **************************************************************************/
494 libvlc_event_manager_t *
495 libvlc_media_player_event_manager(
496 libvlc_media_player_t *p_mi,
497 libvlc_exception_t *p_e )
501 return p_mi->p_event_manager;
504 /**************************************************************************
505 * Trigger a snapshot Taken Event.
506 *************************************************************************/
507 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
508 vlc_value_t oldval, vlc_value_t newval, void *p_data )
510 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
513 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
514 libvlc_event_t event ;
515 event.type = libvlc_MediaPlayerSnapshotTaken ;
516 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
517 /* Snapshot psz data is a vlc_variable owned by libvlc object .
518 Its memmory management is taken care by the obj*/
519 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p"
520 " (%s)", event.u.media_player_snapshot_taken.psz_filename,
521 event.u.media_player_snapshot_taken.psz_filename );
522 libvlc_event_send( p_mi->p_event_manager, &event );
527 /**************************************************************************
528 * Tell media player to start playing.
529 **************************************************************************/
530 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
531 libvlc_exception_t *p_e )
533 input_thread_t * p_input_thread;
535 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
537 /* A thread already exists, send it a play message */
538 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
539 vlc_object_release( p_input_thread );
543 /* Ignore previous exception */
544 libvlc_exception_clear( p_e );
546 vlc_mutex_lock( &p_mi->object_lock );
550 libvlc_exception_raise( p_e, "no associated media descriptor" );
551 vlc_mutex_unlock( &p_mi->object_lock );
555 p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
556 p_mi->p_md->p_input_item, NULL, NULL );
558 if( !p_mi->p_input_thread )
560 vlc_mutex_unlock( &p_mi->object_lock );
564 p_input_thread = p_mi->p_input_thread;
566 var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
567 if( p_mi->drawable.agl )
568 var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
570 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
571 if( p_mi->drawable.xid )
572 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
574 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
575 if( p_mi->drawable.hwnd != NULL )
576 var_SetAddress( p_input_thread, "drawable-hwnd", p_mi->drawable.hwnd );
578 var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
579 if( p_mi->drawable.nsobject != NULL )
580 var_SetAddress( p_input_thread, "drawable-nsobject", p_mi->drawable.nsobject );
582 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
583 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
584 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
586 if( input_Start( p_input_thread ) )
588 vlc_object_release( p_input_thread );
589 p_mi->p_input_thread = NULL;
592 vlc_mutex_unlock( &p_mi->object_lock );
595 /**************************************************************************
597 **************************************************************************/
598 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
599 libvlc_exception_t *p_e )
601 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
602 if( !p_input_thread )
605 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
606 if( state == libvlc_Playing || state == libvlc_Buffering )
608 if( libvlc_media_player_can_pause( p_mi, p_e ) )
609 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
611 libvlc_media_player_stop( p_mi, p_e );
614 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
616 vlc_object_release( p_input_thread );
619 /**************************************************************************
620 * Tells whether the media player is currently playing.
622 * Enter with lock held.
623 **************************************************************************/
624 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
625 libvlc_exception_t *p_e )
627 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
628 return (libvlc_Playing == state) || (libvlc_Buffering == state);
631 /**************************************************************************
633 **************************************************************************/
634 void libvlc_media_player_stop( 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 );
639 if( state == libvlc_Playing ||
640 state == libvlc_Paused ||
641 state == libvlc_Buffering )
643 /* Send a stop notification event only if we are in playing,
644 * buffering or paused states */
645 libvlc_media_set_state( p_mi->p_md, libvlc_Stopped, p_e );
647 /* Construct and send the event */
648 libvlc_event_t event;
649 event.type = libvlc_MediaPlayerStopped;
650 libvlc_event_send( p_mi->p_event_manager, &event );
653 vlc_mutex_lock( &p_mi->object_lock );
654 release_input_thread( p_mi, true ); /* This will stop the input thread */
655 vlc_mutex_unlock( &p_mi->object_lock );
658 /**************************************************************************
660 **************************************************************************/
661 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
663 libvlc_exception_t *p_e )
666 p_mi->drawable.nsobject = drawable;
669 /**************************************************************************
671 **************************************************************************/
672 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
674 return p_mi->drawable.nsobject;
677 /**************************************************************************
679 **************************************************************************/
680 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
682 libvlc_exception_t *p_e )
685 p_mi->drawable.agl = drawable;
688 /**************************************************************************
690 **************************************************************************/
691 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
693 return p_mi->drawable.agl;
696 /**************************************************************************
698 **************************************************************************/
699 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
701 libvlc_exception_t *p_e )
704 p_mi->drawable.xid = drawable;
707 /**************************************************************************
709 **************************************************************************/
710 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
712 return p_mi->drawable.xid;
715 /**************************************************************************
717 **************************************************************************/
718 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
720 libvlc_exception_t *p_e )
723 p_mi->drawable.hwnd = drawable;
726 /**************************************************************************
728 **************************************************************************/
729 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
731 return p_mi->drawable.hwnd;
734 /**************************************************************************
736 **************************************************************************/
737 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
738 libvlc_drawable_t drawable,
739 libvlc_exception_t *p_e )
742 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
743 p_mi->drawable.hwnd = (HWND)drawable;
745 libvlc_exception_raise(p_e, "Operation not supported");
746 #elif defined(__APPLE__)
747 p_mi->drawable.agl = drawable;
750 p_mi->drawable.xid = drawable;
755 /**************************************************************************
757 **************************************************************************/
759 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi,
760 libvlc_exception_t *p_e )
765 if (sizeof (HWND) <= sizeof (libvlc_drawable_t))
766 return (libvlc_drawable_t)p_mi->drawable.hwnd;
769 #elif defined(__APPLE__)
770 return p_mi->drawable.agl;
772 return p_mi->drawable.xid;
776 /**************************************************************************
777 * Getters for stream information
778 **************************************************************************/
779 libvlc_time_t libvlc_media_player_get_length(
780 libvlc_media_player_t *p_mi,
781 libvlc_exception_t *p_e )
783 input_thread_t *p_input_thread;
784 libvlc_time_t i_time;
786 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
787 if( !p_input_thread )
790 i_time = var_GetTime( p_input_thread, "length" );
791 vlc_object_release( p_input_thread );
793 return (i_time+500LL)/1000LL;
796 libvlc_time_t libvlc_media_player_get_time(
797 libvlc_media_player_t *p_mi,
798 libvlc_exception_t *p_e )
800 input_thread_t *p_input_thread;
801 libvlc_time_t i_time;
803 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
804 if( !p_input_thread )
807 i_time = var_GetTime( p_input_thread , "time" );
808 vlc_object_release( p_input_thread );
809 return (i_time+500LL)/1000LL;
812 void libvlc_media_player_set_time(
813 libvlc_media_player_t *p_mi,
814 libvlc_time_t i_time,
815 libvlc_exception_t *p_e )
817 input_thread_t *p_input_thread;
819 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
820 if( !p_input_thread )
823 var_SetTime( p_input_thread, "time", i_time*1000LL );
824 vlc_object_release( p_input_thread );
827 void libvlc_media_player_set_position(
828 libvlc_media_player_t *p_mi,
830 libvlc_exception_t *p_e )
832 input_thread_t *p_input_thread;
834 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
835 if( !p_input_thread )
838 var_SetFloat( p_input_thread, "position", position );
839 vlc_object_release( p_input_thread );
842 float libvlc_media_player_get_position(
843 libvlc_media_player_t *p_mi,
844 libvlc_exception_t *p_e )
846 input_thread_t *p_input_thread;
849 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
850 if( !p_input_thread )
853 var_Get( p_input_thread, "position", &val );
854 vlc_object_release( p_input_thread );
859 void libvlc_media_player_set_chapter(
860 libvlc_media_player_t *p_mi,
862 libvlc_exception_t *p_e )
864 input_thread_t *p_input_thread;
866 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
867 if( !p_input_thread )
870 var_SetInteger( p_input_thread, "chapter", chapter );
871 vlc_object_release( p_input_thread );
874 int libvlc_media_player_get_chapter(
875 libvlc_media_player_t *p_mi,
876 libvlc_exception_t *p_e )
878 input_thread_t *p_input_thread;
881 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
882 if( !p_input_thread )
885 var_Get( p_input_thread, "chapter", &val );
886 vlc_object_release( p_input_thread );
891 int libvlc_media_player_get_chapter_count(
892 libvlc_media_player_t *p_mi,
893 libvlc_exception_t *p_e )
895 input_thread_t *p_input_thread;
898 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
899 if( !p_input_thread )
902 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
903 vlc_object_release( p_input_thread );
908 int libvlc_media_player_get_chapter_count_for_title(
909 libvlc_media_player_t *p_mi,
911 libvlc_exception_t *p_e )
913 input_thread_t *p_input_thread;
916 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
917 if( !p_input_thread )
921 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
923 vlc_object_release( p_input_thread );
926 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
927 vlc_object_release( p_input_thread );
933 void libvlc_media_player_set_title(
934 libvlc_media_player_t *p_mi,
936 libvlc_exception_t *p_e )
938 input_thread_t *p_input_thread;
940 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
941 if( !p_input_thread )
944 var_SetInteger( p_input_thread, "title", i_title );
945 vlc_object_release( p_input_thread );
948 libvlc_event_t event;
949 event.type = libvlc_MediaPlayerTitleChanged;
950 event.u.media_player_title_changed.new_title = i_title;
951 libvlc_event_send( p_mi->p_event_manager, &event );
954 int libvlc_media_player_get_title(
955 libvlc_media_player_t *p_mi,
956 libvlc_exception_t *p_e )
958 input_thread_t *p_input_thread;
961 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
962 if( !p_input_thread )
965 var_Get( p_input_thread, "title", &val );
966 vlc_object_release( p_input_thread );
971 int libvlc_media_player_get_title_count(
972 libvlc_media_player_t *p_mi,
973 libvlc_exception_t *p_e )
975 input_thread_t *p_input_thread;
978 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
979 if( !p_input_thread )
982 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
983 vlc_object_release( p_input_thread );
988 void libvlc_media_player_next_chapter(
989 libvlc_media_player_t *p_mi,
990 libvlc_exception_t *p_e )
992 input_thread_t *p_input_thread;
994 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
995 if( !p_input_thread )
998 int i_type = var_Type( p_input_thread, "next-chapter" );
999 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1000 "next-chapter":"next-title", true );
1002 vlc_object_release( p_input_thread );
1005 void libvlc_media_player_previous_chapter(
1006 libvlc_media_player_t *p_mi,
1007 libvlc_exception_t *p_e )
1009 input_thread_t *p_input_thread;
1011 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1012 if( !p_input_thread )
1015 int i_type = var_Type( p_input_thread, "next-chapter" );
1016 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1017 "prev-chapter":"prev-title", true );
1019 vlc_object_release( p_input_thread );
1022 float libvlc_media_player_get_fps(
1023 libvlc_media_player_t *p_mi,
1024 libvlc_exception_t *p_e)
1026 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1029 if( p_input_thread )
1031 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1033 vlc_object_release( p_input_thread );
1038 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1039 libvlc_exception_t *p_e)
1041 input_thread_t *p_input_thread =
1042 libvlc_get_input_thread ( p_mi, p_e);
1043 if ( !p_input_thread )
1046 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1048 vlc_object_release( p_input_thread );
1051 vlc_object_release( p_input_thread );
1055 void libvlc_media_player_set_rate(
1056 libvlc_media_player_t *p_mi,
1058 libvlc_exception_t *p_e )
1060 input_thread_t *p_input_thread;
1063 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1064 if( !p_input_thread )
1067 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1068 if( (rate < 0.0) && !b_can_rewind )
1070 vlc_object_release( p_input_thread );
1071 libvlc_exception_raise( p_e, "Rate value is invalid" );
1075 var_SetInteger( p_input_thread, "rate", 1000.0f/rate );
1076 vlc_object_release( p_input_thread );
1079 float libvlc_media_player_get_rate(
1080 libvlc_media_player_t *p_mi,
1081 libvlc_exception_t *p_e )
1083 input_thread_t *p_input_thread;
1087 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1088 if( !p_input_thread )
1089 return 0.0; /* rate < 0 indicates rewind */
1091 var_Get( p_input_thread, "rate", &val );
1092 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1093 if( (val.i_int < 0) && !b_can_rewind )
1095 libvlc_exception_raise( p_e, "invalid rate" );
1098 vlc_object_release( p_input_thread );
1100 return (float)1000.0f/val.i_int;
1103 libvlc_state_t libvlc_media_player_get_state(
1104 libvlc_media_player_t *p_mi,
1105 libvlc_exception_t *p_e )
1107 input_thread_t *p_input_thread;
1108 libvlc_state_t state = libvlc_Ended;
1111 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1112 if( !p_input_thread )
1114 /* We do return the right value, no need to throw an exception */
1115 if( libvlc_exception_raised( p_e ) )
1116 libvlc_exception_clear( p_e );
1120 state = libvlc_media_get_state( p_mi->p_md, NULL );
1121 if( state == libvlc_Playing )
1124 caching = var_GetFloat( p_input_thread, "cache" );
1125 if( caching > 0.0 && caching < 1.0 )
1126 state = libvlc_Buffering;
1128 vlc_object_release( p_input_thread );
1132 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1133 libvlc_exception_t *p_e )
1135 input_thread_t *p_input_thread;
1138 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1139 if ( !p_input_thread )
1141 /* We do return the right value, no need to throw an exception */
1142 if( libvlc_exception_raised( p_e ) )
1143 libvlc_exception_clear( p_e );
1146 var_Get( p_input_thread, "can-seek", &val );
1147 vlc_object_release( p_input_thread );
1152 /* internal function, used by audio, video */
1153 libvlc_track_description_t *
1154 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1155 const char *psz_variable,
1156 libvlc_exception_t *p_e )
1158 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1159 libvlc_track_description_t *p_track_description = NULL,
1160 *p_actual, *p_previous;
1165 vlc_value_t val_list, text_list;
1166 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1169 if( val_list.p_list->i_count <= 0 )
1172 p_track_description = ( libvlc_track_description_t * )
1173 malloc( sizeof( libvlc_track_description_t ) );
1174 if ( !p_track_description )
1176 libvlc_exception_raise( p_e, "not enough memory" );
1179 p_actual = p_track_description;
1181 for( int i = 0; i < val_list.p_list->i_count; i++ )
1185 p_actual = ( libvlc_track_description_t * )
1186 malloc( sizeof( libvlc_track_description_t ) );
1189 libvlc_track_description_release( p_track_description );
1190 libvlc_exception_raise( p_e, "not enough memory" );
1194 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1195 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1196 p_actual->p_next = NULL;
1198 p_previous->p_next = p_actual;
1199 p_previous = p_actual;
1204 var_FreeList( &val_list, &text_list );
1205 vlc_object_release( p_input );
1207 return p_track_description;
1210 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1212 libvlc_track_description_t *p_actual, *p_before;
1217 free( p_actual->psz_name );
1218 p_before = p_actual;
1219 p_actual = p_before->p_next;
1224 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1225 libvlc_exception_t *p_e )
1227 input_thread_t *p_input_thread;
1230 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1231 if ( !p_input_thread )
1233 /* We do return the right value, no need to throw an exception */
1234 if( libvlc_exception_raised( p_e ) )
1235 libvlc_exception_clear( p_e );
1238 var_Get( p_input_thread, "can-pause", &val );
1239 vlc_object_release( p_input_thread );