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 );
86 vlc_thread_join( p_input_thread );
88 assert( p_mi->p_input_resource == NULL );
89 assert( p_input_thread->b_dead );
90 /* Store the input resource for future use. */
91 p_mi->p_input_resource = input_DetachResource( p_input_thread );
93 var_Destroy( p_input_thread, "drawable-hwnd" );
94 var_Destroy( p_input_thread, "drawable-xid" );
95 var_Destroy( p_input_thread, "drawable-agl" );
97 vlc_object_release( p_input_thread );
99 p_mi->p_input_thread = NULL;
103 * Retrieve the input thread. Be sure to release the object
104 * once you are done with it. (libvlc Internal)
106 * Function will lock the object.
108 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi,
109 libvlc_exception_t *p_e )
111 input_thread_t *p_input_thread;
113 if( !p_mi ) RAISENULL( "Media Instance is NULL" );
115 vlc_mutex_lock( &p_mi->object_lock );
117 if( !p_mi->p_input_thread )
119 vlc_mutex_unlock( &p_mi->object_lock );
120 RAISENULL( "Input is NULL" );
123 p_input_thread = p_mi->p_input_thread;
124 vlc_object_hold( p_input_thread );
126 vlc_mutex_unlock( &p_mi->object_lock );
128 return p_input_thread;
132 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
133 vlc_value_t oldval, vlc_value_t newval,
139 libvlc_media_player_t * p_mi = p_userdata;
140 libvlc_event_t event;
142 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
143 event.type = libvlc_MediaPlayerSeekableChanged;
144 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
146 libvlc_event_send( p_mi->p_event_manager, &event );
151 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
152 vlc_value_t oldval, vlc_value_t newval,
158 libvlc_media_player_t * p_mi = p_userdata;
159 libvlc_event_t event;
161 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, NULL);
162 event.type = libvlc_MediaPlayerPausableChanged;
163 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
165 libvlc_event_send( p_mi->p_event_manager, &event );
170 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
171 vlc_value_t oldval, vlc_value_t newval,
175 input_thread_t * p_input = (input_thread_t *)p_this;
176 libvlc_media_player_t * p_mi = p_userdata;
177 libvlc_event_t event;
179 assert( !strcmp( psz_cmd, "intf-event" ) );
181 if( newval.i_int == INPUT_EVENT_STATE )
183 libvlc_state_t libvlc_state;
185 switch ( var_GetInteger( p_input, "state" ) )
188 libvlc_state = libvlc_NothingSpecial;
189 event.type = libvlc_MediaPlayerNothingSpecial;
192 libvlc_state = libvlc_Opening;
193 event.type = libvlc_MediaPlayerOpening;
196 libvlc_state = libvlc_Playing;
197 event.type = libvlc_MediaPlayerPlaying;
200 libvlc_state = libvlc_Paused;
201 event.type = libvlc_MediaPlayerPaused;
204 libvlc_state = libvlc_Ended;
205 event.type = libvlc_MediaPlayerEndReached;
208 libvlc_state = libvlc_Error;
209 event.type = libvlc_MediaPlayerEncounteredError;
216 libvlc_media_set_state( p_mi->p_md, libvlc_state, NULL );
217 libvlc_event_send( p_mi->p_event_manager, &event );
219 else if( newval.i_int == INPUT_EVENT_ABORT )
221 libvlc_state_t libvlc_state = libvlc_Stopped;
222 event.type = libvlc_MediaPlayerStopped;
224 libvlc_media_set_state( p_mi->p_md, libvlc_state, NULL );
225 libvlc_event_send( p_mi->p_event_manager, &event );
227 else if( newval.i_int == INPUT_EVENT_POSITION )
229 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
230 return VLC_SUCCESS; /* Don't send the position while stopped */
233 event.type = libvlc_MediaPlayerPositionChanged;
234 event.u.media_player_position_changed.new_position =
235 var_GetFloat( p_input, "position" );
236 libvlc_event_send( p_mi->p_event_manager, &event );
239 event.type = libvlc_MediaPlayerTimeChanged;
240 event.u.media_player_time_changed.new_time =
241 var_GetTime( p_input, "time" );
242 libvlc_event_send( p_mi->p_event_manager, &event );
244 else if( newval.i_int == INPUT_EVENT_LENGTH )
246 event.type = libvlc_MediaPlayerLengthChanged;
247 event.u.media_player_length_changed.new_length =
248 var_GetTime( p_input, "length" );
249 libvlc_event_send( p_mi->p_event_manager, &event );
256 static void libvlc_media_player_destroy( libvlc_media_player_t * );
258 /**************************************************************************
259 * Create a Media Instance object.
262 * - All items created by _new start with a refcount set to 1.
263 * - Accessor _release decrease the refcount by 1, if after that
264 * operation the refcount is 0, the object is destroyed.
265 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
267 * Object locking strategy:
268 * - No lock held while in constructor.
269 * - When accessing any member variable this lock is held. (XXX who locks?)
270 * - When attempting to destroy the object the lock is also held.
271 **************************************************************************/
272 libvlc_media_player_t *
273 libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
274 libvlc_exception_t * p_e )
276 libvlc_media_player_t * p_mi;
278 if( !p_libvlc_instance )
280 libvlc_exception_raise( p_e, "invalid libvlc instance" );
284 p_mi = malloc( sizeof(libvlc_media_player_t) );
287 libvlc_exception_raise( p_e, "not enough memory" );
291 p_mi->drawable.agl = 0;
292 p_mi->drawable.xid = 0;
293 p_mi->drawable.hwnd = NULL;
294 p_mi->drawable.nsobject = NULL;
295 p_mi->p_libvlc_instance = p_libvlc_instance;
296 p_mi->p_input_thread = NULL;
297 p_mi->p_input_resource = NULL;
298 p_mi->i_refcount = 1;
299 vlc_mutex_init( &p_mi->object_lock );
300 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
301 p_libvlc_instance, p_e );
302 if( libvlc_exception_raised( p_e ) )
304 vlc_mutex_destroy( &p_mi->object_lock );
309 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
310 libvlc_MediaPlayerNothingSpecial, p_e );
311 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
312 libvlc_MediaPlayerOpening, p_e );
313 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
314 libvlc_MediaPlayerBuffering, p_e );
315 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
316 libvlc_MediaPlayerPlaying, p_e );
317 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
318 libvlc_MediaPlayerPaused, p_e );
319 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
320 libvlc_MediaPlayerStopped, p_e );
321 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
322 libvlc_MediaPlayerForward, p_e );
323 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
324 libvlc_MediaPlayerBackward, p_e );
325 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
326 libvlc_MediaPlayerEndReached, p_e );
327 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
328 libvlc_MediaPlayerEncounteredError, p_e );
330 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
331 libvlc_MediaPlayerPositionChanged, p_e );
332 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
333 libvlc_MediaPlayerTimeChanged, p_e );
334 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
335 libvlc_MediaPlayerLengthChanged, p_e );
336 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
337 libvlc_MediaPlayerTitleChanged, p_e );
338 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
339 libvlc_MediaPlayerSeekableChanged, p_e );
340 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
341 libvlc_MediaPlayerPausableChanged, p_e );
343 /* Snapshot initialization */
344 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
345 libvlc_MediaPlayerSnapshotTaken, p_e );
347 /* Attach a var callback to the global object to provide the glue between
348 vout_thread that generates the event and media_player that re-emits it
349 with its own event manager
351 var_AddCallback( p_libvlc_instance->p_libvlc_int, "snapshot-file",
352 SnapshotTakenCallback, p_mi );
357 /**************************************************************************
358 * Create a Media Instance object with a media descriptor.
359 **************************************************************************/
360 libvlc_media_player_t *
361 libvlc_media_player_new_from_media(
362 libvlc_media_t * p_md,
363 libvlc_exception_t *p_e )
365 libvlc_media_player_t * p_mi;
367 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
371 libvlc_media_retain( p_md );
377 /**************************************************************************
378 * Destroy a Media Instance object (libvlc internal)
380 * Warning: No lock held here, but hey, this is internal. Caller must lock.
381 **************************************************************************/
382 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
386 /* Detach Callback from the main libvlc object */
387 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int,
388 "snapshot-file", SnapshotTakenCallback, p_mi );
390 /* Release the input thread */
391 release_input_thread( p_mi, true );
393 if( p_mi->p_input_resource )
395 input_resource_Delete( p_mi->p_input_resource );
396 p_mi->p_input_resource = NULL;
399 libvlc_event_manager_release( p_mi->p_event_manager );
400 libvlc_media_release( p_mi->p_md );
401 vlc_mutex_destroy( &p_mi->object_lock );
405 /**************************************************************************
406 * Release a Media Instance object.
408 * Function does the locking.
409 **************************************************************************/
410 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
415 vlc_mutex_lock( &p_mi->object_lock );
416 destroy = !--p_mi->i_refcount;
417 vlc_mutex_unlock( &p_mi->object_lock );
420 libvlc_media_player_destroy( p_mi );
423 /**************************************************************************
424 * Retain a Media Instance object.
426 * Caller must hold the lock.
427 **************************************************************************/
428 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
432 vlc_mutex_lock( &p_mi->object_lock );
434 vlc_mutex_unlock( &p_mi->object_lock );
437 /**************************************************************************
438 * Set the Media descriptor associated with the instance.
440 * Enter without lock -- function will lock the object.
441 **************************************************************************/
442 void libvlc_media_player_set_media(
443 libvlc_media_player_t *p_mi,
444 libvlc_media_t *p_md,
445 libvlc_exception_t *p_e )
452 vlc_mutex_lock( &p_mi->object_lock );
454 /* FIXME I am not sure if it is a user request or on die(eof/error)
456 release_input_thread( p_mi,
457 p_mi->p_input_thread &&
458 !p_mi->p_input_thread->b_eof &&
459 !p_mi->p_input_thread->b_error );
462 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
464 libvlc_media_release( p_mi->p_md );
469 vlc_mutex_unlock( &p_mi->object_lock );
470 return; /* It is ok to pass a NULL md */
473 libvlc_media_retain( p_md );
476 /* The policy here is to ignore that we were created using a different
477 * libvlc_instance, because we don't really care */
478 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
480 vlc_mutex_unlock( &p_mi->object_lock );
483 /**************************************************************************
484 * Get the Media descriptor associated with the instance.
485 **************************************************************************/
487 libvlc_media_player_get_media(
488 libvlc_media_player_t *p_mi,
489 libvlc_exception_t *p_e )
494 vlc_mutex_lock( &p_mi->object_lock );
497 libvlc_media_retain( p_mi->p_md );
498 vlc_mutex_unlock( &p_mi->object_lock );
502 /**************************************************************************
503 * Get the event Manager.
504 **************************************************************************/
505 libvlc_event_manager_t *
506 libvlc_media_player_event_manager(
507 libvlc_media_player_t *p_mi,
508 libvlc_exception_t *p_e )
512 return p_mi->p_event_manager;
515 /**************************************************************************
516 * Trigger a snapshot Taken Event.
517 *************************************************************************/
518 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
519 vlc_value_t oldval, vlc_value_t newval, void *p_data )
521 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
524 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
525 libvlc_event_t event ;
526 event.type = libvlc_MediaPlayerSnapshotTaken ;
527 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
528 /* Snapshot psz data is a vlc_variable owned by libvlc object .
529 Its memmory management is taken care by the obj*/
530 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p"
531 " (%s)", event.u.media_player_snapshot_taken.psz_filename,
532 event.u.media_player_snapshot_taken.psz_filename );
533 libvlc_event_send( p_mi->p_event_manager, &event );
538 /**************************************************************************
539 * Tell media player to start playing.
540 **************************************************************************/
541 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
542 libvlc_exception_t *p_e )
544 input_thread_t * p_input_thread;
546 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
548 /* A thread already exists, send it a play message */
549 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
550 vlc_object_release( p_input_thread );
554 /* Ignore previous exception */
555 libvlc_exception_clear( p_e );
557 vlc_mutex_lock( &p_mi->object_lock );
561 libvlc_exception_raise( p_e, "no associated media descriptor" );
562 vlc_mutex_unlock( &p_mi->object_lock );
566 p_mi->p_input_thread = input_Create( p_mi->p_libvlc_instance->p_libvlc_int,
567 p_mi->p_md->p_input_item, NULL, p_mi->p_input_resource );
569 if( !p_mi->p_input_thread )
571 vlc_mutex_unlock( &p_mi->object_lock );
575 p_mi->p_input_resource = NULL;
576 p_input_thread = p_mi->p_input_thread;
578 var_Create( p_input_thread, "drawable-agl", VLC_VAR_INTEGER );
579 if( p_mi->drawable.agl )
580 var_SetInteger( p_input_thread, "drawable-agl", p_mi->drawable.agl );
582 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
583 if( p_mi->drawable.xid )
584 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
586 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
587 if( p_mi->drawable.hwnd != NULL )
588 var_SetAddress( p_input_thread, "drawable-hwnd", p_mi->drawable.hwnd );
590 var_Create( p_input_thread, "drawable-nsobject", VLC_VAR_ADDRESS );
591 if( p_mi->drawable.nsobject != NULL )
592 var_SetAddress( p_input_thread, "drawable-nsobject", p_mi->drawable.nsobject );
594 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
595 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
596 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
598 if( input_Start( p_input_thread ) )
600 vlc_object_release( p_input_thread );
601 p_mi->p_input_thread = NULL;
604 vlc_mutex_unlock( &p_mi->object_lock );
607 /**************************************************************************
609 **************************************************************************/
610 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
611 libvlc_exception_t *p_e )
613 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
614 if( !p_input_thread )
617 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
618 if( state == libvlc_Playing || state == libvlc_Buffering )
620 if( libvlc_media_player_can_pause( p_mi, p_e ) )
621 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
623 libvlc_media_player_stop( p_mi, p_e );
626 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
628 vlc_object_release( p_input_thread );
631 /**************************************************************************
632 * Tells whether the media player is currently playing.
634 * Enter with lock held.
635 **************************************************************************/
636 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
637 libvlc_exception_t *p_e )
639 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
640 return (libvlc_Playing == state) || (libvlc_Buffering == state);
643 /**************************************************************************
645 **************************************************************************/
646 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
647 libvlc_exception_t *p_e )
649 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
651 if( state == libvlc_Playing ||
652 state == libvlc_Paused ||
653 state == libvlc_Buffering )
655 /* Send a stop notification event only if we are in playing,
656 * buffering or paused states */
657 libvlc_media_set_state( p_mi->p_md, libvlc_Stopped, p_e );
659 /* Construct and send the event */
660 libvlc_event_t event;
661 event.type = libvlc_MediaPlayerStopped;
662 libvlc_event_send( p_mi->p_event_manager, &event );
665 vlc_mutex_lock( &p_mi->object_lock );
666 release_input_thread( p_mi, true ); /* This will stop the input thread */
667 vlc_mutex_unlock( &p_mi->object_lock );
670 /**************************************************************************
672 **************************************************************************/
673 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
675 libvlc_exception_t *p_e )
678 p_mi->drawable.nsobject = drawable;
681 /**************************************************************************
683 **************************************************************************/
684 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
686 return p_mi->drawable.nsobject;
689 /**************************************************************************
691 **************************************************************************/
692 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
694 libvlc_exception_t *p_e )
697 p_mi->drawable.agl = drawable;
700 /**************************************************************************
702 **************************************************************************/
703 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
705 return p_mi->drawable.agl;
708 /**************************************************************************
710 **************************************************************************/
711 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
713 libvlc_exception_t *p_e )
716 p_mi->drawable.xid = drawable;
719 /**************************************************************************
721 **************************************************************************/
722 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
724 return p_mi->drawable.xid;
727 /**************************************************************************
729 **************************************************************************/
730 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
732 libvlc_exception_t *p_e )
735 p_mi->drawable.hwnd = drawable;
738 /**************************************************************************
740 **************************************************************************/
741 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
743 return p_mi->drawable.hwnd;
746 /**************************************************************************
747 * Getters for stream information
748 **************************************************************************/
749 libvlc_time_t libvlc_media_player_get_length(
750 libvlc_media_player_t *p_mi,
751 libvlc_exception_t *p_e )
753 input_thread_t *p_input_thread;
754 libvlc_time_t i_time;
756 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
757 if( !p_input_thread )
760 i_time = var_GetTime( p_input_thread, "length" );
761 vlc_object_release( p_input_thread );
763 return (i_time+500LL)/1000LL;
766 libvlc_time_t libvlc_media_player_get_time(
767 libvlc_media_player_t *p_mi,
768 libvlc_exception_t *p_e )
770 input_thread_t *p_input_thread;
771 libvlc_time_t i_time;
773 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
774 if( !p_input_thread )
777 i_time = var_GetTime( p_input_thread , "time" );
778 vlc_object_release( p_input_thread );
779 return (i_time+500LL)/1000LL;
782 void libvlc_media_player_set_time(
783 libvlc_media_player_t *p_mi,
784 libvlc_time_t i_time,
785 libvlc_exception_t *p_e )
787 input_thread_t *p_input_thread;
789 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
790 if( !p_input_thread )
793 var_SetTime( p_input_thread, "time", i_time*1000LL );
794 vlc_object_release( p_input_thread );
797 void libvlc_media_player_set_position(
798 libvlc_media_player_t *p_mi,
800 libvlc_exception_t *p_e )
802 input_thread_t *p_input_thread;
804 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
805 if( !p_input_thread )
808 var_SetFloat( p_input_thread, "position", position );
809 vlc_object_release( p_input_thread );
812 float libvlc_media_player_get_position(
813 libvlc_media_player_t *p_mi,
814 libvlc_exception_t *p_e )
816 input_thread_t *p_input_thread;
819 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
820 if( !p_input_thread )
823 f_position = var_GetFloat( p_input_thread, "position" );
824 vlc_object_release( p_input_thread );
829 void libvlc_media_player_set_chapter(
830 libvlc_media_player_t *p_mi,
832 libvlc_exception_t *p_e )
834 input_thread_t *p_input_thread;
836 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
837 if( !p_input_thread )
840 var_SetInteger( p_input_thread, "chapter", chapter );
841 vlc_object_release( p_input_thread );
844 int libvlc_media_player_get_chapter(
845 libvlc_media_player_t *p_mi,
846 libvlc_exception_t *p_e )
848 input_thread_t *p_input_thread;
851 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
852 if( !p_input_thread )
855 i_chapter = var_GetInteger( p_input_thread, "chapter" );
856 vlc_object_release( p_input_thread );
861 int libvlc_media_player_get_chapter_count(
862 libvlc_media_player_t *p_mi,
863 libvlc_exception_t *p_e )
865 input_thread_t *p_input_thread;
868 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
869 if( !p_input_thread )
872 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
873 vlc_object_release( p_input_thread );
878 int libvlc_media_player_get_chapter_count_for_title(
879 libvlc_media_player_t *p_mi,
881 libvlc_exception_t *p_e )
883 input_thread_t *p_input_thread;
886 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
887 if( !p_input_thread )
891 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
893 vlc_object_release( p_input_thread );
896 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
897 vlc_object_release( p_input_thread );
903 void libvlc_media_player_set_title(
904 libvlc_media_player_t *p_mi,
906 libvlc_exception_t *p_e )
908 input_thread_t *p_input_thread;
910 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
911 if( !p_input_thread )
914 var_SetInteger( p_input_thread, "title", i_title );
915 vlc_object_release( p_input_thread );
918 libvlc_event_t event;
919 event.type = libvlc_MediaPlayerTitleChanged;
920 event.u.media_player_title_changed.new_title = i_title;
921 libvlc_event_send( p_mi->p_event_manager, &event );
924 int libvlc_media_player_get_title(
925 libvlc_media_player_t *p_mi,
926 libvlc_exception_t *p_e )
928 input_thread_t *p_input_thread;
931 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
932 if( !p_input_thread )
935 i_title = var_GetInteger( p_input_thread, "title" );
936 vlc_object_release( p_input_thread );
941 int libvlc_media_player_get_title_count(
942 libvlc_media_player_t *p_mi,
943 libvlc_exception_t *p_e )
945 input_thread_t *p_input_thread;
948 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
949 if( !p_input_thread )
952 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
953 vlc_object_release( p_input_thread );
958 void libvlc_media_player_next_chapter(
959 libvlc_media_player_t *p_mi,
960 libvlc_exception_t *p_e )
962 input_thread_t *p_input_thread;
964 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
965 if( !p_input_thread )
968 int i_type = var_Type( p_input_thread, "next-chapter" );
969 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
970 "next-chapter":"next-title", true );
972 vlc_object_release( p_input_thread );
975 void libvlc_media_player_previous_chapter(
976 libvlc_media_player_t *p_mi,
977 libvlc_exception_t *p_e )
979 input_thread_t *p_input_thread;
981 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
982 if( !p_input_thread )
985 int i_type = var_Type( p_input_thread, "next-chapter" );
986 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
987 "prev-chapter":"prev-title", true );
989 vlc_object_release( p_input_thread );
992 float libvlc_media_player_get_fps(
993 libvlc_media_player_t *p_mi,
994 libvlc_exception_t *p_e)
996 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1001 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1003 vlc_object_release( p_input_thread );
1008 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1009 libvlc_exception_t *p_e)
1011 input_thread_t *p_input_thread =
1012 libvlc_get_input_thread ( p_mi, p_e);
1013 if ( !p_input_thread )
1016 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1018 vlc_object_release( p_input_thread );
1021 vlc_object_release( p_input_thread );
1025 void libvlc_media_player_set_rate(
1026 libvlc_media_player_t *p_mi,
1028 libvlc_exception_t *p_e )
1030 input_thread_t *p_input_thread;
1033 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1034 if( !p_input_thread )
1037 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1038 if( (rate < 0.0) && !b_can_rewind )
1040 vlc_object_release( p_input_thread );
1041 libvlc_exception_raise( p_e, "Rate value is invalid" );
1045 var_SetInteger( p_input_thread, "rate", 1000.0f/rate );
1046 vlc_object_release( p_input_thread );
1049 float libvlc_media_player_get_rate(
1050 libvlc_media_player_t *p_mi,
1051 libvlc_exception_t *p_e )
1053 input_thread_t *p_input_thread;
1057 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1058 if( !p_input_thread )
1059 return 0.0; /* rate < 0 indicates rewind */
1061 i_rate = var_GetInteger( p_input_thread, "rate" );
1062 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1063 if( i_rate < 0 && !b_can_rewind )
1065 vlc_object_release( p_input_thread );
1066 libvlc_exception_raise( p_e, "invalid rate" );
1069 vlc_object_release( p_input_thread );
1071 return (float)1000.0f/i_rate;
1074 libvlc_state_t libvlc_media_player_get_state(
1075 libvlc_media_player_t *p_mi,
1076 libvlc_exception_t *p_e )
1078 input_thread_t *p_input_thread;
1079 libvlc_state_t state = libvlc_Ended;
1081 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1082 if( !p_input_thread )
1084 /* We do return the right value, no need to throw an exception */
1085 if( libvlc_exception_raised( p_e ) )
1086 libvlc_exception_clear( p_e );
1090 state = libvlc_media_get_state( p_mi->p_md, NULL );
1091 if( state == libvlc_Playing )
1094 caching = var_GetFloat( p_input_thread, "cache" );
1095 if( caching > 0.0 && caching < 1.0 )
1096 state = libvlc_Buffering;
1098 vlc_object_release( p_input_thread );
1102 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1103 libvlc_exception_t *p_e )
1105 input_thread_t *p_input_thread;
1108 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1109 if ( !p_input_thread )
1111 /* We do return the right value, no need to throw an exception */
1112 if( libvlc_exception_raised( p_e ) )
1113 libvlc_exception_clear( p_e );
1116 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1117 vlc_object_release( p_input_thread );
1122 /* internal function, used by audio, video */
1123 libvlc_track_description_t *
1124 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1125 const char *psz_variable,
1126 libvlc_exception_t *p_e )
1128 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1129 libvlc_track_description_t *p_track_description = NULL,
1130 *p_actual, *p_previous;
1135 vlc_value_t val_list, text_list;
1136 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1139 if( val_list.p_list->i_count <= 0 )
1142 p_track_description = ( libvlc_track_description_t * )
1143 malloc( sizeof( libvlc_track_description_t ) );
1144 if ( !p_track_description )
1146 libvlc_exception_raise( p_e, "not enough memory" );
1149 p_actual = p_track_description;
1151 for( int i = 0; i < val_list.p_list->i_count; i++ )
1155 p_actual = ( libvlc_track_description_t * )
1156 malloc( sizeof( libvlc_track_description_t ) );
1159 libvlc_track_description_release( p_track_description );
1160 libvlc_exception_raise( p_e, "not enough memory" );
1164 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1165 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1166 p_actual->p_next = NULL;
1168 p_previous->p_next = p_actual;
1169 p_previous = p_actual;
1174 var_FreeList( &val_list, &text_list );
1175 vlc_object_release( p_input );
1177 return p_track_description;
1180 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1182 libvlc_track_description_t *p_actual, *p_before;
1187 free( p_actual->psz_name );
1188 p_before = p_actual;
1189 p_actual = p_before->p_next;
1194 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1195 libvlc_exception_t *p_e )
1197 input_thread_t *p_input_thread;
1200 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1201 if ( !p_input_thread )
1203 /* We do return the right value, no need to throw an exception */
1204 if( libvlc_exception_raised( p_e ) )
1205 libvlc_exception_clear( p_e );
1208 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1209 vlc_object_release( p_input_thread );
1214 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
1216 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1217 if( p_input_thread != NULL )
1219 var_TriggerCallback( p_input_thread, "frame-next" );
1220 vlc_object_release( p_input_thread );
1223 libvlc_exception_raise( p_e, "Input thread is NULL" );