1 /*****************************************************************************
2 * media_player.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27 #include <vlc_demux.h>
28 #include <vlc_input.h>
34 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
35 vlc_value_t oldval, vlc_value_t newval,
38 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
39 vlc_value_t oldval, vlc_value_t newval,
42 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
43 vlc_value_t oldval, vlc_value_t newval,
46 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
47 vlc_value_t oldval, vlc_value_t newval, void *p_data );
49 static const libvlc_state_t vlc_to_libvlc_state_array[] =
51 [INIT_S] = libvlc_NothingSpecial,
52 [OPENING_S] = libvlc_Opening,
53 [PLAYING_S] = libvlc_Playing,
54 [PAUSE_S] = libvlc_Paused,
55 [END_S] = libvlc_Ended,
56 [ERROR_S] = libvlc_Error,
59 static inline libvlc_state_t vlc_to_libvlc_state( int vlc_state )
61 if( vlc_state < 0 || vlc_state > 6 )
64 return vlc_to_libvlc_state_array[vlc_state];
68 * Release the associated input thread
70 * Object lock is NOT held.
72 static void release_input_thread( libvlc_media_player_t *p_mi )
74 input_thread_t * p_input_thread;
76 if( !p_mi || !p_mi->p_input_thread )
79 p_input_thread = p_mi->p_input_thread;
81 /* No one is tracking this input_thread appart us. Destroy it */
82 if( p_mi->b_own_its_input_thread )
84 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
85 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
86 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
88 /* We owned this one */
89 input_StopThread( p_input_thread );
90 vlc_thread_join( p_input_thread );
92 var_Destroy( p_input_thread, "drawable-hwnd" );
93 var_Destroy( p_input_thread, "drawable-xid" );
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 * Object lock is held.
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_TIMES )
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 = var_GetFloat( p_input, "position" );;
226 libvlc_event_send( p_mi->p_event_manager, &event );
229 event.type = libvlc_MediaPlayerTimeChanged;
230 event.u.media_player_time_changed.new_time = var_GetTime( p_input, "time" );
231 libvlc_event_send( p_mi->p_event_manager, &event );
239 /**************************************************************************
240 * Create a Media Instance object
241 **************************************************************************/
242 libvlc_media_player_t *
243 libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
244 libvlc_exception_t * p_e )
246 libvlc_media_player_t * p_mi;
248 if( !p_libvlc_instance )
250 libvlc_exception_raise( p_e, "invalid libvlc instance" );
254 p_mi = malloc( sizeof(libvlc_media_player_t) );
257 libvlc_exception_raise( p_e, "Not enough memory" );
261 p_mi->drawable.xid = 0;
262 p_mi->drawable.hwnd = NULL;
263 p_mi->p_libvlc_instance = p_libvlc_instance;
264 p_mi->p_input_thread = NULL;
265 /* refcount strategy:
266 * - All items created by _new start with a refcount set to 1
267 * - Accessor _release decrease the refcount by 1, if after that
268 * operation the refcount is 0, the object is destroyed.
269 * - Accessor _retain increase the refcount by 1 (XXX: to implement) */
270 p_mi->i_refcount = 1;
271 p_mi->b_own_its_input_thread = true;
272 /* object_lock strategy:
273 * - No lock held in constructor
274 * - Lock when accessing all variable this lock is held
275 * - Lock when attempting to destroy the object the lock is also held */
276 vlc_mutex_init( &p_mi->object_lock );
277 p_mi->p_event_manager = libvlc_event_manager_new( p_mi,
278 p_libvlc_instance, p_e );
279 if( libvlc_exception_raised( p_e ) )
285 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
286 libvlc_MediaPlayerNothingSpecial, p_e );
287 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
288 libvlc_MediaPlayerOpening, p_e );
289 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
290 libvlc_MediaPlayerBuffering, p_e );
291 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
292 libvlc_MediaPlayerPlaying, p_e );
293 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
294 libvlc_MediaPlayerPaused, p_e );
295 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
296 libvlc_MediaPlayerStopped, p_e );
297 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
298 libvlc_MediaPlayerForward, p_e );
299 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
300 libvlc_MediaPlayerBackward, p_e );
301 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
302 libvlc_MediaPlayerEndReached, p_e );
303 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
304 libvlc_MediaPlayerEncounteredError, p_e );
306 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
307 libvlc_MediaPlayerPositionChanged, p_e );
308 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
309 libvlc_MediaPlayerTimeChanged, p_e );
310 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
311 libvlc_MediaPlayerTitleChanged, p_e );
312 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
313 libvlc_MediaPlayerSeekableChanged, p_e );
314 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
315 libvlc_MediaPlayerPausableChanged, p_e );
317 /* Snapshot initialization */
318 libvlc_event_manager_register_event_type( p_mi->p_event_manager,
319 libvlc_MediaPlayerSnapshotTaken, p_e );
320 /* Attach a var callback to the global object to provide the glue between
321 vout_thread that generates the event and media_player that re-emits it
322 with its own event manager
324 var_Create( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
325 var_AddCallback( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", SnapshotTakenCallback, p_mi );
330 /**************************************************************************
331 * Create a Media Instance object with a media descriptor
332 **************************************************************************/
333 libvlc_media_player_t *
334 libvlc_media_player_new_from_media(
335 libvlc_media_t * p_md,
336 libvlc_exception_t *p_e )
338 libvlc_media_player_t * p_mi;
339 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance, p_e );
344 libvlc_media_retain( p_md );
350 /**************************************************************************
351 * Create a new media instance object from an input_thread (Libvlc Internal)
352 **************************************************************************/
353 libvlc_media_player_t * libvlc_media_player_new_from_input_thread(
354 struct libvlc_instance_t *p_libvlc_instance,
355 input_thread_t *p_input,
356 libvlc_exception_t *p_e )
358 libvlc_media_player_t * p_mi;
362 libvlc_exception_raise( p_e, "invalid input thread" );
366 p_mi = libvlc_media_player_new( p_libvlc_instance, p_e );
371 p_mi->p_md = libvlc_media_new_from_input_item(
373 input_GetItem( p_input ), p_e );
377 libvlc_media_player_destroy( p_mi );
381 /* will be released in media_player_release() */
382 vlc_object_hold( p_input );
384 p_mi->p_input_thread = p_input;
385 p_mi->b_own_its_input_thread = false;
390 /**************************************************************************
391 * Destroy a Media Instance object (libvlc internal)
393 * Warning: No lock held here, but hey, this is internal.
394 **************************************************************************/
395 void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
397 input_thread_t *p_input_thread;
398 libvlc_exception_t p_e;
400 libvlc_exception_init( &p_e );
405 /* Detach Callback from the main libvlc object */
406 var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", SnapshotTakenCallback, p_mi );
408 p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
410 if( libvlc_exception_raised( &p_e ) )
412 libvlc_event_manager_release( p_mi->p_event_manager );
413 libvlc_exception_clear( &p_e );
415 return; /* no need to worry about no input thread */
417 vlc_mutex_destroy( &p_mi->object_lock );
419 vlc_object_release( p_input_thread );
421 libvlc_media_release( p_mi->p_md );
426 /**************************************************************************
427 * Release a Media Instance object
428 **************************************************************************/
429 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
434 vlc_mutex_lock( &p_mi->object_lock );
438 if( p_mi->i_refcount > 0 )
440 vlc_mutex_unlock( &p_mi->object_lock );
443 vlc_mutex_unlock( &p_mi->object_lock );
444 vlc_mutex_destroy( &p_mi->object_lock );
446 release_input_thread( p_mi );
448 libvlc_event_manager_release( p_mi->p_event_manager );
450 libvlc_media_release( p_mi->p_md );
455 /**************************************************************************
456 * Retain a Media Instance object
457 **************************************************************************/
458 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
466 /**************************************************************************
467 * Set the Media descriptor associated with the instance
468 **************************************************************************/
469 void libvlc_media_player_set_media(
470 libvlc_media_player_t *p_mi,
471 libvlc_media_t *p_md,
472 libvlc_exception_t *p_e )
479 vlc_mutex_lock( &p_mi->object_lock );
481 release_input_thread( p_mi );
484 libvlc_media_set_state( p_mi->p_md, libvlc_NothingSpecial, p_e );
486 libvlc_media_release( p_mi->p_md );
491 vlc_mutex_unlock( &p_mi->object_lock );
492 return; /* It is ok to pass a NULL md */
495 libvlc_media_retain( p_md );
498 /* The policy here is to ignore that we were created using a different
499 * libvlc_instance, because we don't really care */
500 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
502 vlc_mutex_unlock( &p_mi->object_lock );
505 /**************************************************************************
506 * Get the Media descriptor associated with the instance
507 **************************************************************************/
509 libvlc_media_player_get_media(
510 libvlc_media_player_t *p_mi,
511 libvlc_exception_t *p_e )
518 libvlc_media_retain( p_mi->p_md );
522 /**************************************************************************
523 * Get the event Manager
524 **************************************************************************/
525 libvlc_event_manager_t *
526 libvlc_media_player_event_manager(
527 libvlc_media_player_t *p_mi,
528 libvlc_exception_t *p_e )
532 return p_mi->p_event_manager;
535 /**************************************************************************
536 * Trigger a snapshot Taken Event
537 *************************************************************************/
538 static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
539 vlc_value_t oldval, vlc_value_t newval, void *p_data )
541 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
544 libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
545 libvlc_event_t event ;
546 event.type = libvlc_MediaPlayerSnapshotTaken ;
547 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
548 /* Snapshot psz data is a vlc_variable owned by libvlc object .
549 Its memmory management is taken care by the obj*/
550 msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%p (%s)",
551 event.u.media_player_snapshot_taken.psz_filename ,event.u.media_player_snapshot_taken.psz_filename );
552 libvlc_event_send( p_mi->p_event_manager, &event );
557 /**************************************************************************
559 **************************************************************************/
560 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
561 libvlc_exception_t *p_e )
563 input_thread_t * p_input_thread;
565 if( (p_input_thread = libvlc_get_input_thread( p_mi, p_e )) )
567 /* A thread already exists, send it a play message */
568 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
569 vlc_object_release( p_input_thread );
573 /* Ignore previous exception */
574 libvlc_exception_clear( p_e );
576 vlc_mutex_lock( &p_mi->object_lock );
580 libvlc_exception_raise( p_e, "no associated media descriptor" );
581 vlc_mutex_unlock( &p_mi->object_lock );
585 p_mi->p_input_thread = input_CreateThread( p_mi->p_libvlc_instance->p_libvlc_int,
586 p_mi->p_md->p_input_item );
589 if( !p_mi->p_input_thread )
591 vlc_mutex_unlock( &p_mi->object_lock );
595 p_input_thread = p_mi->p_input_thread;
597 var_Create( p_input_thread, "drawable-xid", VLC_VAR_INTEGER );
598 if( p_mi->drawable.xid )
599 var_SetInteger( p_input_thread, "drawable-xid", p_mi->drawable.xid );
601 var_Create( p_input_thread, "drawable-hwnd", VLC_VAR_ADDRESS );
602 if( p_mi->drawable.hwnd != NULL )
604 vlc_value_t val = { .p_address = p_mi->drawable.hwnd };
605 var_Set( p_input_thread, "drawable-hwnd", val );
608 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
609 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
610 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
612 vlc_mutex_unlock( &p_mi->object_lock );
615 /**************************************************************************
617 **************************************************************************/
618 void libvlc_media_player_pause( libvlc_media_player_t *p_mi,
619 libvlc_exception_t *p_e )
621 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
623 if( !p_input_thread )
626 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
628 if( state == libvlc_Playing )
630 if( libvlc_media_player_can_pause( p_mi, p_e ) )
631 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
633 libvlc_media_player_stop( p_mi, p_e );
636 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
638 vlc_object_release( p_input_thread );
641 /**************************************************************************
643 **************************************************************************/
644 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi,
645 libvlc_exception_t *p_e )
647 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
649 if( !p_input_thread )
652 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
654 vlc_object_release( p_input_thread );
656 if( state == libvlc_Playing )
664 /**************************************************************************
666 **************************************************************************/
667 void libvlc_media_player_stop( libvlc_media_player_t *p_mi,
668 libvlc_exception_t *p_e )
670 libvlc_state_t state = libvlc_media_player_get_state( p_mi, p_e );
672 if( state == libvlc_Playing || state == libvlc_Paused )
674 /* Send a stop notification event only of we are in playing or paused states */
675 libvlc_media_set_state( p_mi->p_md, libvlc_Ended, p_e );
677 /* Construct and send the event */
678 libvlc_event_t event;
679 event.type = libvlc_MediaPlayerStopped;
680 libvlc_event_send( p_mi->p_event_manager, &event );
683 if( p_mi->b_own_its_input_thread )
685 vlc_mutex_lock( &p_mi->object_lock );
686 release_input_thread( p_mi ); /* This will stop the input thread */
687 vlc_mutex_unlock( &p_mi->object_lock );
691 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi, p_e );
693 if( !p_input_thread )
696 input_StopThread( p_input_thread );
697 vlc_object_release( p_input_thread );
701 void libvlc_media_player_set_xid( libvlc_media_player_t *p_mi,
703 libvlc_exception_t *p_e )
706 p_mi->drawable.xid = drawable;
709 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
711 libvlc_exception_t *p_e )
714 p_mi->drawable.hwnd = drawable;
717 /**************************************************************************
719 **************************************************************************/
720 void libvlc_media_player_set_drawable( libvlc_media_player_t *p_mi,
721 libvlc_drawable_t drawable,
722 libvlc_exception_t *p_e )
724 input_thread_t *p_input_thread;
725 vout_thread_t *p_vout = NULL;
727 p_mi->drawable.xid = drawable;
729 /* Allow on the fly drawable changing. This is tricky has this may
730 * not be supported by every vout. We though can't disable it
731 * because of some creepy drawable type that are not flexible enough
732 * (Win32 HWND for instance) */
733 p_input_thread = libvlc_get_input_thread( p_mi, p_e );
734 if( !p_input_thread ) {
735 /* No input, nothing more to do, we are fine */
736 libvlc_exception_clear( p_e );
740 p_vout = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
743 vout_Control( p_vout , VOUT_REPARENT, drawable);
744 vlc_object_release( p_vout );
746 vlc_object_release( p_input_thread );
749 /**************************************************************************
751 **************************************************************************/
753 libvlc_media_player_get_drawable ( libvlc_media_player_t *p_mi, libvlc_exception_t *p_e )
756 return p_mi->drawable.xid;
759 /**************************************************************************
760 * Getters for stream information
761 **************************************************************************/
762 libvlc_time_t libvlc_media_player_get_length(
763 libvlc_media_player_t *p_mi,
764 libvlc_exception_t *p_e )
766 input_thread_t *p_input_thread;
769 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
770 if( !p_input_thread )
773 var_Get( p_input_thread, "length", &val );
774 vlc_object_release( p_input_thread );
776 return (val.i_time+500LL)/1000LL;
779 libvlc_time_t libvlc_media_player_get_time(
780 libvlc_media_player_t *p_mi,
781 libvlc_exception_t *p_e )
783 input_thread_t *p_input_thread;
786 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
787 if( !p_input_thread )
790 var_Get( p_input_thread , "time", &val );
791 vlc_object_release( p_input_thread );
792 return (val.i_time+500LL)/1000LL;
795 void libvlc_media_player_set_time(
796 libvlc_media_player_t *p_mi,
798 libvlc_exception_t *p_e )
800 input_thread_t *p_input_thread;
803 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
804 if( !p_input_thread )
807 value.i_time = time*1000LL;
808 var_Set( p_input_thread, "time", value );
809 vlc_object_release( p_input_thread );
812 void libvlc_media_player_set_position(
813 libvlc_media_player_t *p_mi,
815 libvlc_exception_t *p_e )
817 input_thread_t *p_input_thread;
819 val.f_float = position;
821 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
822 if( !p_input_thread )
825 var_Set( p_input_thread, "position", val );
826 vlc_object_release( p_input_thread );
829 float libvlc_media_player_get_position(
830 libvlc_media_player_t *p_mi,
831 libvlc_exception_t *p_e )
833 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_Get( p_input_thread, "position", &val );
841 vlc_object_release( p_input_thread );
846 void libvlc_media_player_set_chapter(
847 libvlc_media_player_t *p_mi,
849 libvlc_exception_t *p_e )
851 input_thread_t *p_input_thread;
855 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
856 if( !p_input_thread )
859 var_Set( p_input_thread, "chapter", val );
860 vlc_object_release( p_input_thread );
863 int libvlc_media_player_get_chapter(
864 libvlc_media_player_t *p_mi,
865 libvlc_exception_t *p_e )
867 input_thread_t *p_input_thread;
870 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
871 if( !p_input_thread )
874 var_Get( p_input_thread, "chapter", &val );
875 vlc_object_release( p_input_thread );
880 int libvlc_media_player_get_chapter_count(
881 libvlc_media_player_t *p_mi,
882 libvlc_exception_t *p_e )
884 input_thread_t *p_input_thread;
887 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
888 if( !p_input_thread )
891 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
892 vlc_object_release( p_input_thread );
897 int libvlc_media_player_get_chapter_count_for_title(
898 libvlc_media_player_t *p_mi,
900 libvlc_exception_t *p_e )
902 input_thread_t *p_input_thread;
905 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
906 if( !p_input_thread )
910 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
912 vlc_object_release( p_input_thread );
915 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
916 vlc_object_release( p_input_thread );
922 void libvlc_media_player_set_title(
923 libvlc_media_player_t *p_mi,
925 libvlc_exception_t *p_e )
927 input_thread_t *p_input_thread;
931 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
932 if( !p_input_thread )
935 var_Set( p_input_thread, "title", val );
936 vlc_object_release( p_input_thread );
939 libvlc_event_t event;
940 event.type = libvlc_MediaPlayerTitleChanged;
941 event.u.media_player_title_changed.new_title = i_title;
942 libvlc_event_send( p_mi->p_event_manager, &event );
945 int libvlc_media_player_get_title(
946 libvlc_media_player_t *p_mi,
947 libvlc_exception_t *p_e )
949 input_thread_t *p_input_thread;
952 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
953 if( !p_input_thread )
956 var_Get( p_input_thread, "title", &val );
957 vlc_object_release( p_input_thread );
962 int libvlc_media_player_get_title_count(
963 libvlc_media_player_t *p_mi,
964 libvlc_exception_t *p_e )
966 input_thread_t *p_input_thread;
969 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
970 if( !p_input_thread )
973 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
974 vlc_object_release( p_input_thread );
979 void libvlc_media_player_next_chapter(
980 libvlc_media_player_t *p_mi,
981 libvlc_exception_t *p_e )
983 input_thread_t *p_input_thread;
985 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
986 if( !p_input_thread )
989 int i_type = var_Type( p_input_thread, "next-chapter" );
992 var_Set( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
993 "next-chapter":"next-title", val );
995 vlc_object_release( p_input_thread );
998 void libvlc_media_player_previous_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" );
1011 var_Set( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1012 "prev-chapter":"prev-title", val );
1014 vlc_object_release( p_input_thread );
1017 float libvlc_media_player_get_fps(
1018 libvlc_media_player_t *p_mi,
1019 libvlc_exception_t *p_e)
1021 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1024 if( p_input_thread )
1026 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1028 vlc_object_release( p_input_thread );
1033 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi,
1034 libvlc_exception_t *p_e)
1036 input_thread_t *p_input_thread =
1037 libvlc_get_input_thread ( p_mi, p_e);
1038 if ( !p_input_thread )
1041 if ( !p_input_thread->b_die && !p_input_thread->b_dead )
1043 vlc_object_release( p_input_thread );
1046 vlc_object_release( p_input_thread );
1050 void libvlc_media_player_set_rate(
1051 libvlc_media_player_t *p_mi,
1053 libvlc_exception_t *p_e )
1055 input_thread_t *p_input_thread;
1060 RAISEVOID( "Rate value is invalid" );
1062 p_input_thread = libvlc_get_input_thread ( p_mi, p_e);
1063 if( !p_input_thread )
1066 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1067 if( (rate < 0) && !b_can_rewind )
1069 vlc_object_release( p_input_thread );
1070 libvlc_exception_raise( p_e, "Rate value is invalid" );
1074 val.i_int = 1000.0f/rate;
1075 var_Set( p_input_thread, "rate", val );
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;
1110 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1111 if ( !p_input_thread )
1113 /* We do return the right value, no need to throw an exception */
1114 if( libvlc_exception_raised( p_e ) )
1115 libvlc_exception_clear( p_e );
1116 return libvlc_Ended;
1119 var_Get( p_input_thread, "state", &val );
1120 vlc_object_release( p_input_thread );
1122 return vlc_to_libvlc_state(val.i_int);
1125 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi,
1126 libvlc_exception_t *p_e )
1128 input_thread_t *p_input_thread;
1131 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1132 if ( !p_input_thread )
1134 /* We do return the right value, no need to throw an exception */
1135 if( libvlc_exception_raised( p_e ) )
1136 libvlc_exception_clear( p_e );
1139 var_Get( p_input_thread, "can-seek", &val );
1140 vlc_object_release( p_input_thread );
1145 /* internal function, used by audio, video */
1146 libvlc_track_description_t *
1147 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1148 const char *psz_variable,
1149 libvlc_exception_t *p_e )
1151 input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_e );
1156 vlc_value_t val_list, text_list;
1157 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1159 if( val_list.p_list->i_count <= 0 ) /* no tracks */
1162 libvlc_track_description_t *p_track_description, *p_actual, *p_previous;
1163 p_track_description = ( libvlc_track_description_t * )
1164 malloc( sizeof( libvlc_track_description_t ) );
1165 if ( !p_track_description )
1167 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1168 vlc_object_release( p_input );
1169 libvlc_exception_raise( p_e, "no enough memory" );
1172 p_actual = p_track_description;
1174 for( int i = 0; i < val_list.p_list->i_count; i++ )
1178 p_actual = ( libvlc_track_description_t * )
1179 malloc( sizeof( libvlc_track_description_t ) );
1182 libvlc_track_description_release( p_track_description );
1183 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1184 vlc_object_release( p_input );
1185 libvlc_exception_raise( p_e, "no enough memory" );
1189 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1190 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1191 p_actual->p_next = NULL;
1193 p_previous->p_next = p_actual;
1194 p_previous = p_actual;
1197 var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list);
1198 vlc_object_release( p_input );
1200 return p_track_description;
1203 void libvlc_track_description_release( libvlc_track_description_t *p_track_description )
1205 libvlc_track_description_t *p_actual, *p_before;
1206 p_actual = p_track_description;
1210 free( p_actual->psz_name );
1211 p_before = p_actual;
1212 p_actual = p_before->p_next;
1217 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi,
1218 libvlc_exception_t *p_e )
1220 input_thread_t *p_input_thread;
1223 p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
1224 if ( !p_input_thread )
1226 /* We do return the right value, no need to throw an exception */
1227 if( libvlc_exception_raised( p_e ) )
1228 libvlc_exception_clear( p_e );
1231 var_Get( p_input_thread, "can-pause", &val );
1232 vlc_object_release( p_input_thread );