1 /*****************************************************************************
2 * media_player.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005-2011 VLC authors and VideoLAN
6 * Authors: Clément Stenac <zorglub@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
29 #include <vlc/libvlc.h>
30 #include <vlc/libvlc_media.h>
31 #include <vlc/libvlc_events.h>
33 #include <vlc_demux.h>
34 #include <vlc_input.h>
39 #include "libvlc_internal.h"
40 #include "media_internal.h" // libvlc_media_set_state()
41 #include "media_player_internal.h"
44 input_seekable_changed( vlc_object_t
* p_this
, char const * psz_cmd
,
45 vlc_value_t oldval
, vlc_value_t newval
,
48 input_pausable_changed( vlc_object_t
* p_this
, char const * psz_cmd
,
49 vlc_value_t oldval
, vlc_value_t newval
,
52 input_event_changed( vlc_object_t
* p_this
, char const * psz_cmd
,
53 vlc_value_t oldval
, vlc_value_t newval
,
57 snapshot_was_taken( 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
);
66 #define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
67 static inline void __register_event(libvlc_media_player_t
*mp
, libvlc_event_type_t type
)
69 libvlc_event_manager_register_event_type(mp
->p_event_manager
, type
);
73 * The input lock protects the input and input resource pointer.
74 * It MUST NOT be used from callbacks.
76 * The object lock protects the reset, namely the media and the player state.
77 * It can, and usually needs to be taken from callbacks.
78 * The object lock can be acquired under the input lock... and consequently
79 * the opposite order is STRICTLY PROHIBITED.
81 static inline void lock(libvlc_media_player_t
*mp
)
83 vlc_mutex_lock(&mp
->object_lock
);
86 static inline void unlock(libvlc_media_player_t
*mp
)
88 vlc_mutex_unlock(&mp
->object_lock
);
91 static inline void lock_input(libvlc_media_player_t
*mp
)
93 vlc_mutex_lock(&mp
->input
.lock
);
96 static inline void unlock_input(libvlc_media_player_t
*mp
)
98 vlc_mutex_unlock(&mp
->input
.lock
);
102 * Release the associated input thread.
104 * Object lock is NOT held.
105 * Input lock is held or instance is being destroyed.
107 static void release_input_thread( libvlc_media_player_t
*p_mi
, bool b_input_abort
)
111 input_thread_t
*p_input_thread
= p_mi
->input
.p_thread
;
112 if( !p_input_thread
)
114 p_mi
->input
.p_thread
= NULL
;
116 var_DelCallback( p_input_thread
, "can-seek",
117 input_seekable_changed
, p_mi
);
118 var_DelCallback( p_input_thread
, "can-pause",
119 input_pausable_changed
, p_mi
);
120 var_DelCallback( p_input_thread
, "intf-event",
121 input_event_changed
, p_mi
);
123 /* We owned this one */
124 input_Stop( p_input_thread
, b_input_abort
);
125 input_Close( p_input_thread
);
129 * Retrieve the input thread. Be sure to release the object
130 * once you are done with it. (libvlc Internal)
132 input_thread_t
*libvlc_get_input_thread( libvlc_media_player_t
*p_mi
)
134 input_thread_t
*p_input_thread
;
139 p_input_thread
= p_mi
->input
.p_thread
;
141 vlc_object_hold( p_input_thread
);
143 libvlc_printerr( "No active input" );
146 return p_input_thread
;
150 * Set the internal state of the media_player. (media player Internal)
152 * Function will lock the media_player.
154 static void set_state( libvlc_media_player_t
*p_mi
, libvlc_state_t state
,
161 libvlc_media_t
*media
= p_mi
->p_md
;
163 libvlc_media_retain(media
);
170 // Also set the state of the corresponding media
171 // This is strictly for convenience.
172 libvlc_media_set_state(media
, state
);
174 libvlc_media_release(media
);
179 input_seekable_changed( vlc_object_t
* p_this
, char const * psz_cmd
,
180 vlc_value_t oldval
, vlc_value_t newval
,
186 libvlc_media_player_t
* p_mi
= p_userdata
;
187 libvlc_event_t event
;
189 event
.type
= libvlc_MediaPlayerSeekableChanged
;
190 event
.u
.media_player_seekable_changed
.new_seekable
= newval
.b_bool
;
192 libvlc_event_send( p_mi
->p_event_manager
, &event
);
197 input_pausable_changed( vlc_object_t
* p_this
, char const * psz_cmd
,
198 vlc_value_t oldval
, vlc_value_t newval
,
204 libvlc_media_player_t
* p_mi
= p_userdata
;
205 libvlc_event_t event
;
207 event
.type
= libvlc_MediaPlayerPausableChanged
;
208 event
.u
.media_player_pausable_changed
.new_pausable
= newval
.b_bool
;
210 libvlc_event_send( p_mi
->p_event_manager
, &event
);
215 input_event_changed( vlc_object_t
* p_this
, char const * psz_cmd
,
216 vlc_value_t oldval
, vlc_value_t newval
,
220 input_thread_t
* p_input
= (input_thread_t
*)p_this
;
221 libvlc_media_player_t
* p_mi
= p_userdata
;
222 libvlc_event_t event
;
224 assert( !strcmp( psz_cmd
, "intf-event" ) );
226 if( newval
.i_int
== INPUT_EVENT_STATE
)
228 libvlc_state_t libvlc_state
;
230 switch ( var_GetInteger( p_input
, "state" ) )
233 libvlc_state
= libvlc_NothingSpecial
;
234 event
.type
= libvlc_MediaPlayerNothingSpecial
;
237 libvlc_state
= libvlc_Opening
;
238 event
.type
= libvlc_MediaPlayerOpening
;
241 libvlc_state
= libvlc_Playing
;
242 event
.type
= libvlc_MediaPlayerPlaying
;
245 libvlc_state
= libvlc_Paused
;
246 event
.type
= libvlc_MediaPlayerPaused
;
249 libvlc_state
= libvlc_Ended
;
250 event
.type
= libvlc_MediaPlayerEndReached
;
253 libvlc_state
= libvlc_Error
;
254 event
.type
= libvlc_MediaPlayerEncounteredError
;
261 set_state( p_mi
, libvlc_state
, false );
262 libvlc_event_send( p_mi
->p_event_manager
, &event
);
264 else if( newval
.i_int
== INPUT_EVENT_ABORT
)
266 libvlc_state_t libvlc_state
= libvlc_Stopped
;
267 event
.type
= libvlc_MediaPlayerStopped
;
269 set_state( p_mi
, libvlc_state
, false );
270 libvlc_event_send( p_mi
->p_event_manager
, &event
);
272 else if( newval
.i_int
== INPUT_EVENT_POSITION
)
274 if( var_GetInteger( p_input
, "state" ) != PLAYING_S
)
275 return VLC_SUCCESS
; /* Don't send the position while stopped */
278 event
.type
= libvlc_MediaPlayerPositionChanged
;
279 event
.u
.media_player_position_changed
.new_position
=
280 var_GetFloat( p_input
, "position" );
281 libvlc_event_send( p_mi
->p_event_manager
, &event
);
284 event
.type
= libvlc_MediaPlayerTimeChanged
;
285 event
.u
.media_player_time_changed
.new_time
=
286 from_mtime(var_GetTime( p_input
, "time" ));
287 libvlc_event_send( p_mi
->p_event_manager
, &event
);
289 else if( newval
.i_int
== INPUT_EVENT_LENGTH
)
291 event
.type
= libvlc_MediaPlayerLengthChanged
;
292 event
.u
.media_player_length_changed
.new_length
=
293 from_mtime(var_GetTime( p_input
, "length" ));
294 libvlc_event_send( p_mi
->p_event_manager
, &event
);
296 else if( newval
.i_int
== INPUT_EVENT_CACHE
)
298 event
.type
= libvlc_MediaPlayerBuffering
;
299 event
.u
.media_player_buffering
.new_cache
= (int)(100 *
300 var_GetFloat( p_input
, "cache" ));
301 libvlc_event_send( p_mi
->p_event_manager
, &event
);
303 else if( newval
.i_int
== INPUT_EVENT_VOUT
)
305 vout_thread_t
**pp_vout
;
307 if( input_Control( p_input
, INPUT_GET_VOUTS
, &pp_vout
, &i_vout
) )
313 for( size_t i
= 0; i
< i_vout
; i
++ )
314 vlc_object_release( pp_vout
[i
] );
318 event
.type
= libvlc_MediaPlayerVout
;
319 event
.u
.media_player_vout
.new_count
= i_vout
;
320 libvlc_event_send( p_mi
->p_event_manager
, &event
);
326 /**************************************************************************
327 * Snapshot Taken Event.
329 * FIXME: This snapshot API interface makes no sense in media_player.
330 *************************************************************************/
331 static int snapshot_was_taken(vlc_object_t
*p_this
, char const *psz_cmd
,
332 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
334 VLC_UNUSED(psz_cmd
); VLC_UNUSED(oldval
); VLC_UNUSED(p_this
);
336 libvlc_media_player_t
*mp
= p_data
;
337 libvlc_event_t event
;
338 event
.type
= libvlc_MediaPlayerSnapshotTaken
;
339 event
.u
.media_player_snapshot_taken
.psz_filename
= newval
.psz_string
;
340 libvlc_event_send(mp
->p_event_manager
, &event
);
346 static void libvlc_media_player_destroy( libvlc_media_player_t
* );
349 /**************************************************************************
350 * Create a Media Instance object.
353 * - All items created by _new start with a refcount set to 1.
354 * - Accessor _release decrease the refcount by 1, if after that
355 * operation the refcount is 0, the object is destroyed.
356 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
358 * Object locking strategy:
359 * - No lock held while in constructor.
360 * - When accessing any member variable this lock is held. (XXX who locks?)
361 * - When attempting to destroy the object the lock is also held.
362 **************************************************************************/
363 libvlc_media_player_t
*
364 libvlc_media_player_new( libvlc_instance_t
*instance
)
366 libvlc_media_player_t
* mp
;
370 mp
= vlc_object_create (instance
->p_libvlc_int
, sizeof(*mp
));
371 if (unlikely(mp
== NULL
))
373 libvlc_printerr("Not enough memory");
378 var_Create (mp
, "rate", VLC_VAR_FLOAT
|VLC_VAR_DOINHERIT
);
381 var_Create (mp
, "vout", VLC_VAR_STRING
|VLC_VAR_DOINHERIT
);
382 var_Create (mp
, "window", VLC_VAR_STRING
);
383 var_Create (mp
, "vmem-lock", VLC_VAR_ADDRESS
);
384 var_Create (mp
, "vmem-unlock", VLC_VAR_ADDRESS
);
385 var_Create (mp
, "vmem-display", VLC_VAR_ADDRESS
);
386 var_Create (mp
, "vmem-data", VLC_VAR_ADDRESS
);
387 var_Create (mp
, "vmem-setup", VLC_VAR_ADDRESS
);
388 var_Create (mp
, "vmem-cleanup", VLC_VAR_ADDRESS
);
389 var_Create (mp
, "vmem-chroma", VLC_VAR_STRING
| VLC_VAR_DOINHERIT
);
390 var_Create (mp
, "vmem-width", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
391 var_Create (mp
, "vmem-height", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
392 var_Create (mp
, "vmem-pitch", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
393 var_Create (mp
, "drawable-xid", VLC_VAR_INTEGER
);
394 #if defined (_WIN32) || defined (__OS2__)
395 var_Create (mp
, "drawable-hwnd", VLC_VAR_INTEGER
);
398 var_Create (mp
, "drawable-agl", VLC_VAR_INTEGER
);
399 var_Create (mp
, "drawable-nsobject", VLC_VAR_ADDRESS
);
402 var_Create (mp
, "keyboard-events", VLC_VAR_BOOL
);
403 var_SetBool (mp
, "keyboard-events", true);
404 var_Create (mp
, "mouse-events", VLC_VAR_BOOL
);
405 var_SetBool (mp
, "mouse-events", true);
407 var_Create (mp
, "fullscreen", VLC_VAR_BOOL
);
408 var_Create (mp
, "autoscale", VLC_VAR_BOOL
);
409 var_SetBool (mp
, "autoscale", true);
410 var_Create (mp
, "scale", VLC_VAR_FLOAT
);
411 var_SetFloat (mp
, "scale", 1.);
412 var_Create (mp
, "aspect-ratio", VLC_VAR_STRING
);
413 var_Create (mp
, "crop", VLC_VAR_STRING
);
414 var_Create (mp
, "deinterlace", VLC_VAR_INTEGER
);
415 var_Create (mp
, "deinterlace-mode", VLC_VAR_STRING
);
417 var_Create (mp
, "vbi-page", VLC_VAR_INTEGER
);
419 var_Create (mp
, "marq-marquee", VLC_VAR_STRING
);
420 var_Create (mp
, "marq-color", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
421 var_Create (mp
, "marq-opacity", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
422 var_Create (mp
, "marq-position", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
423 var_Create (mp
, "marq-refresh", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
424 var_Create (mp
, "marq-size", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
425 var_Create (mp
, "marq-timeout", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
426 var_Create (mp
, "marq-x", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
427 var_Create (mp
, "marq-y", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
429 var_Create (mp
, "logo-file", VLC_VAR_STRING
);
430 var_Create (mp
, "logo-x", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
431 var_Create (mp
, "logo-y", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
432 var_Create (mp
, "logo-delay", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
433 var_Create (mp
, "logo-repeat", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
434 var_Create (mp
, "logo-opacity", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
435 var_Create (mp
, "logo-position", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
437 var_Create (mp
, "contrast", VLC_VAR_FLOAT
| VLC_VAR_DOINHERIT
);
438 var_Create (mp
, "brightness", VLC_VAR_FLOAT
| VLC_VAR_DOINHERIT
);
439 var_Create (mp
, "hue", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
440 var_Create (mp
, "saturation", VLC_VAR_FLOAT
| VLC_VAR_DOINHERIT
);
441 var_Create (mp
, "gamma", VLC_VAR_FLOAT
| VLC_VAR_DOINHERIT
);
444 var_Create (mp
, "aout", VLC_VAR_STRING
| VLC_VAR_DOINHERIT
);
445 var_Create (mp
, "mute", VLC_VAR_BOOL
);
446 var_Create (mp
, "volume", VLC_VAR_FLOAT
);
447 var_Create (mp
, "corks", VLC_VAR_INTEGER
);
448 var_Create (mp
, "amem-data", VLC_VAR_ADDRESS
);
449 var_Create (mp
, "amem-setup", VLC_VAR_ADDRESS
);
450 var_Create (mp
, "amem-cleanup", VLC_VAR_ADDRESS
);
451 var_Create (mp
, "amem-play", VLC_VAR_ADDRESS
);
452 var_Create (mp
, "amem-pause", VLC_VAR_ADDRESS
);
453 var_Create (mp
, "amem-resume", VLC_VAR_ADDRESS
);
454 var_Create (mp
, "amem-flush", VLC_VAR_ADDRESS
);
455 var_Create (mp
, "amem-drain", VLC_VAR_ADDRESS
);
456 var_Create (mp
, "amem-set-volume", VLC_VAR_ADDRESS
);
457 var_Create (mp
, "amem-format", VLC_VAR_STRING
| VLC_VAR_DOINHERIT
);
458 var_Create (mp
, "amem-rate", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
459 var_Create (mp
, "amem-channels", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
462 var_Create (mp
, "video-title-show", VLC_VAR_BOOL
);
463 var_Create (mp
, "video-title-position", VLC_VAR_INTEGER
);
464 var_Create (mp
, "video-title-timeout", VLC_VAR_INTEGER
);
467 var_Create (mp
, "equalizer-preamp", VLC_VAR_FLOAT
);
468 var_Create (mp
, "equalizer-bands", VLC_VAR_STRING
);
471 mp
->state
= libvlc_NothingSpecial
;
472 mp
->p_libvlc_instance
= instance
;
473 mp
->input
.p_thread
= NULL
;
474 mp
->input
.p_resource
= input_resource_New(VLC_OBJECT(mp
));
475 if (unlikely(mp
->input
.p_resource
== NULL
))
477 vlc_object_release(mp
);
480 vlc_mutex_init (&mp
->input
.lock
);
482 mp
->p_event_manager
= libvlc_event_manager_new(mp
, instance
);
483 if (unlikely(mp
->p_event_manager
== NULL
))
485 input_resource_Release(mp
->input
.p_resource
);
486 vlc_object_release(mp
);
489 vlc_mutex_init(&mp
->object_lock
);
491 register_event(mp
, NothingSpecial
);
492 register_event(mp
, Opening
);
493 register_event(mp
, Buffering
);
494 register_event(mp
, Playing
);
495 register_event(mp
, Paused
);
496 register_event(mp
, Stopped
);
497 register_event(mp
, Forward
);
498 register_event(mp
, Backward
);
499 register_event(mp
, EndReached
);
500 register_event(mp
, EncounteredError
);
501 register_event(mp
, SeekableChanged
);
503 register_event(mp
, PositionChanged
);
504 register_event(mp
, TimeChanged
);
505 register_event(mp
, LengthChanged
);
506 register_event(mp
, TitleChanged
);
507 register_event(mp
, PausableChanged
);
509 register_event(mp
, Vout
);
511 /* Snapshot initialization */
512 register_event(mp
, SnapshotTaken
);
514 register_event(mp
, MediaChanged
);
516 /* Attach a var callback to the global object to provide the glue between
517 * vout_thread that generates the event and media_player that re-emits it
518 * with its own event manager
520 * FIXME: It's unclear why we want to put this in public API, and why we
521 * want to expose it in such a limiting and ugly way.
523 var_AddCallback(mp
->p_libvlc
, "snapshot-file", snapshot_was_taken
, mp
);
525 libvlc_retain(instance
);
529 /**************************************************************************
530 * Create a Media Instance object with a media descriptor.
531 **************************************************************************/
532 libvlc_media_player_t
*
533 libvlc_media_player_new_from_media( libvlc_media_t
* p_md
)
535 libvlc_media_player_t
* p_mi
;
537 p_mi
= libvlc_media_player_new( p_md
->p_libvlc_instance
);
541 libvlc_media_retain( p_md
);
547 /**************************************************************************
548 * Destroy a Media Instance object (libvlc internal)
550 * Warning: No lock held here, but hey, this is internal. Caller must lock.
551 **************************************************************************/
552 static void libvlc_media_player_destroy( libvlc_media_player_t
*p_mi
)
556 /* Detach Callback from the main libvlc object */
557 var_DelCallback( p_mi
->p_libvlc
,
558 "snapshot-file", snapshot_was_taken
, p_mi
);
560 /* No need for lock_input() because no other threads knows us anymore */
561 if( p_mi
->input
.p_thread
)
562 release_input_thread(p_mi
, true);
563 input_resource_Terminate( p_mi
->input
.p_resource
);
564 input_resource_Release( p_mi
->input
.p_resource
);
565 vlc_mutex_destroy( &p_mi
->input
.lock
);
567 libvlc_event_manager_release( p_mi
->p_event_manager
);
568 libvlc_media_release( p_mi
->p_md
);
569 vlc_mutex_destroy( &p_mi
->object_lock
);
571 libvlc_instance_t
*instance
= p_mi
->p_libvlc_instance
;
572 vlc_object_release( p_mi
);
573 libvlc_release(instance
);
576 /**************************************************************************
577 * Release a Media Instance object.
579 * Function does the locking.
580 **************************************************************************/
581 void libvlc_media_player_release( libvlc_media_player_t
*p_mi
)
587 destroy
= !--p_mi
->i_refcount
;
591 libvlc_media_player_destroy( p_mi
);
594 /**************************************************************************
595 * Retain a Media Instance object.
597 * Caller must hold the lock.
598 **************************************************************************/
599 void libvlc_media_player_retain( libvlc_media_player_t
*p_mi
)
608 /**************************************************************************
609 * Set the Media descriptor associated with the instance.
611 * Enter without lock -- function will lock the object.
612 **************************************************************************/
613 void libvlc_media_player_set_media(
614 libvlc_media_player_t
*p_mi
,
615 libvlc_media_t
*p_md
)
619 /* FIXME I am not sure if it is a user request or on die(eof/error)
621 release_input_thread( p_mi
,
622 p_mi
->input
.p_thread
&&
623 !p_mi
->input
.p_thread
->b_eof
&&
624 !p_mi
->input
.p_thread
->b_error
);
627 set_state( p_mi
, libvlc_NothingSpecial
, true );
628 unlock_input( p_mi
);
630 libvlc_media_release( p_mi
->p_md
);
636 return; /* It is ok to pass a NULL md */
639 libvlc_media_retain( p_md
);
642 /* The policy here is to ignore that we were created using a different
643 * libvlc_instance, because we don't really care */
644 p_mi
->p_libvlc_instance
= p_md
->p_libvlc_instance
;
648 /* Send an event for the newly available media */
649 libvlc_event_t event
;
650 event
.type
= libvlc_MediaPlayerMediaChanged
;
651 event
.u
.media_player_media_changed
.new_media
= p_md
;
652 libvlc_event_send( p_mi
->p_event_manager
, &event
);
656 /**************************************************************************
657 * Get the Media descriptor associated with the instance.
658 **************************************************************************/
660 libvlc_media_player_get_media( libvlc_media_player_t
*p_mi
)
667 libvlc_media_retain( p_mi
->p_md
);
672 /**************************************************************************
673 * Get the event Manager.
674 **************************************************************************/
675 libvlc_event_manager_t
*
676 libvlc_media_player_event_manager( libvlc_media_player_t
*p_mi
)
678 return p_mi
->p_event_manager
;
681 /**************************************************************************
682 * Tell media player to start playing.
683 **************************************************************************/
684 int libvlc_media_player_play( libvlc_media_player_t
*p_mi
)
688 input_thread_t
*p_input_thread
= p_mi
->input
.p_thread
;
691 /* A thread already exists, send it a play message */
692 input_Control( p_input_thread
, INPUT_SET_STATE
, PLAYING_S
);
693 unlock_input( p_mi
);
697 /* Ignore previous exception */
703 unlock_input( p_mi
);
704 libvlc_printerr( "No associated media descriptor" );
708 p_input_thread
= input_Create( p_mi
, p_mi
->p_md
->p_input_item
, NULL
,
709 p_mi
->input
.p_resource
);
711 if( !p_input_thread
)
714 libvlc_printerr( "Not enough memory" );
718 var_AddCallback( p_input_thread
, "can-seek", input_seekable_changed
, p_mi
);
719 var_AddCallback( p_input_thread
, "can-pause", input_pausable_changed
, p_mi
);
720 var_AddCallback( p_input_thread
, "intf-event", input_event_changed
, p_mi
);
722 if( input_Start( p_input_thread
) )
725 var_DelCallback( p_input_thread
, "intf-event", input_event_changed
, p_mi
);
726 var_DelCallback( p_input_thread
, "can-pause", input_pausable_changed
, p_mi
);
727 var_DelCallback( p_input_thread
, "can-seek", input_seekable_changed
, p_mi
);
728 vlc_object_release( p_input_thread
);
729 libvlc_printerr( "Input initialization failure" );
732 p_mi
->input
.p_thread
= p_input_thread
;
737 void libvlc_media_player_set_pause( libvlc_media_player_t
*p_mi
, int paused
)
739 input_thread_t
* p_input_thread
= libvlc_get_input_thread( p_mi
);
740 if( !p_input_thread
)
743 libvlc_state_t state
= libvlc_media_player_get_state( p_mi
);
744 if( state
== libvlc_Playing
|| state
== libvlc_Buffering
)
748 if( libvlc_media_player_can_pause( p_mi
) )
749 input_Control( p_input_thread
, INPUT_SET_STATE
, PAUSE_S
);
751 libvlc_media_player_stop( p_mi
);
757 input_Control( p_input_thread
, INPUT_SET_STATE
, PLAYING_S
);
760 vlc_object_release( p_input_thread
);
763 /**************************************************************************
765 **************************************************************************/
766 void libvlc_media_player_pause( libvlc_media_player_t
*p_mi
)
768 libvlc_state_t state
= libvlc_media_player_get_state( p_mi
);
769 bool playing
= (state
== libvlc_Playing
|| state
== libvlc_Buffering
);
771 libvlc_media_player_set_pause( p_mi
, playing
);
774 /**************************************************************************
775 * Tells whether the media player is currently playing.
777 * Enter with lock held.
778 **************************************************************************/
779 int libvlc_media_player_is_playing( libvlc_media_player_t
*p_mi
)
781 libvlc_state_t state
= libvlc_media_player_get_state( p_mi
);
782 return (libvlc_Playing
== state
) || (libvlc_Buffering
== state
);
785 /**************************************************************************
787 **************************************************************************/
788 void libvlc_media_player_stop( libvlc_media_player_t
*p_mi
)
790 libvlc_state_t state
= libvlc_media_player_get_state( p_mi
);
793 release_input_thread( p_mi
, true ); /* This will stop the input thread */
795 /* Force to go to stopped state, in case we were in Ended, or Error
797 if( state
!= libvlc_Stopped
)
799 set_state( p_mi
, libvlc_Stopped
, false );
801 /* Construct and send the event */
802 libvlc_event_t event
;
803 event
.type
= libvlc_MediaPlayerStopped
;
804 libvlc_event_send( p_mi
->p_event_manager
, &event
);
807 input_resource_Terminate( p_mi
->input
.p_resource
);
812 void libvlc_video_set_callbacks( libvlc_media_player_t
*mp
,
813 void *(*lock_cb
) (void *, void **),
814 void (*unlock_cb
) (void *, void *, void *const *),
815 void (*display_cb
) (void *, void *),
818 var_SetAddress( mp
, "vmem-lock", lock_cb
);
819 var_SetAddress( mp
, "vmem-unlock", unlock_cb
);
820 var_SetAddress( mp
, "vmem-display", display_cb
);
821 var_SetAddress( mp
, "vmem-data", opaque
);
822 var_SetString( mp
, "vout", "vmem" );
825 void libvlc_video_set_format_callbacks( libvlc_media_player_t
*mp
,
826 libvlc_video_format_cb setup
,
827 libvlc_video_cleanup_cb cleanup
)
829 var_SetAddress( mp
, "vmem-setup", setup
);
830 var_SetAddress( mp
, "vmem-cleanup", cleanup
);
833 void libvlc_video_set_format( libvlc_media_player_t
*mp
, const char *chroma
,
834 unsigned width
, unsigned height
, unsigned pitch
)
836 var_SetString( mp
, "vmem-chroma", chroma
);
837 var_SetInteger( mp
, "vmem-width", width
);
838 var_SetInteger( mp
, "vmem-height", height
);
839 var_SetInteger( mp
, "vmem-pitch", pitch
);
842 /**************************************************************************
844 **************************************************************************/
845 void libvlc_media_player_set_nsobject( libvlc_media_player_t
*p_mi
,
848 assert (p_mi
!= NULL
);
850 var_SetAddress (p_mi
, "drawable-nsobject", drawable
);
852 (void) p_mi
; (void)drawable
;
856 /**************************************************************************
858 **************************************************************************/
859 void * libvlc_media_player_get_nsobject( libvlc_media_player_t
*p_mi
)
861 assert (p_mi
!= NULL
);
863 return var_GetAddress (p_mi
, "drawable-nsobject");
869 /**************************************************************************
871 **************************************************************************/
872 void libvlc_media_player_set_agl( libvlc_media_player_t
*p_mi
,
876 var_SetInteger (p_mi
, "drawable-agl", drawable
);
878 (void) p_mi
; (void)drawable
;
882 /**************************************************************************
884 **************************************************************************/
885 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t
*p_mi
)
887 assert (p_mi
!= NULL
);
889 return var_GetInteger (p_mi
, "drawable-agl");
895 /**************************************************************************
897 **************************************************************************/
898 void libvlc_media_player_set_xwindow( libvlc_media_player_t
*p_mi
,
901 assert (p_mi
!= NULL
);
903 var_SetString (p_mi
, "vout", drawable
? "xid" : "any");
904 var_SetString (p_mi
, "window", drawable
? "embed-xid,any" : "any");
905 var_SetInteger (p_mi
, "drawable-xid", drawable
);
908 /**************************************************************************
910 **************************************************************************/
911 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t
*p_mi
)
913 return var_GetInteger (p_mi
, "drawable-xid");
916 /**************************************************************************
918 **************************************************************************/
919 void libvlc_media_player_set_hwnd( libvlc_media_player_t
*p_mi
,
922 assert (p_mi
!= NULL
);
923 #if defined (_WIN32) || defined (__OS2__)
924 var_SetString (p_mi
, "window",
925 (drawable
!= NULL
) ? "embed-hwnd,any" : "");
926 var_SetInteger (p_mi
, "drawable-hwnd", (uintptr_t)drawable
);
928 (void) p_mi
; (void) drawable
;
932 /**************************************************************************
934 **************************************************************************/
935 void *libvlc_media_player_get_hwnd( libvlc_media_player_t
*p_mi
)
937 assert (p_mi
!= NULL
);
938 #if defined (_WIN32) || defined (__OS2__)
939 return (void *)(uintptr_t)var_GetInteger (p_mi
, "drawable-hwnd");
945 void libvlc_audio_set_callbacks( libvlc_media_player_t
*mp
,
946 libvlc_audio_play_cb play_cb
,
947 libvlc_audio_pause_cb pause_cb
,
948 libvlc_audio_resume_cb resume_cb
,
949 libvlc_audio_flush_cb flush_cb
,
950 libvlc_audio_drain_cb drain_cb
,
953 var_SetAddress( mp
, "amem-play", play_cb
);
954 var_SetAddress( mp
, "amem-pause", pause_cb
);
955 var_SetAddress( mp
, "amem-resume", resume_cb
);
956 var_SetAddress( mp
, "amem-flush", flush_cb
);
957 var_SetAddress( mp
, "amem-drain", drain_cb
);
958 var_SetAddress( mp
, "amem-data", opaque
);
959 var_SetString( mp
, "aout", "amem,none" );
962 void libvlc_audio_set_volume_callback( libvlc_media_player_t
*mp
,
963 libvlc_audio_set_volume_cb cb
)
965 var_SetAddress( mp
, "amem-set-volume", cb
);
968 void libvlc_audio_set_format_callbacks( libvlc_media_player_t
*mp
,
969 libvlc_audio_setup_cb setup
,
970 libvlc_audio_cleanup_cb cleanup
)
972 var_SetAddress( mp
, "amem-setup", setup
);
973 var_SetAddress( mp
, "amem-cleanup", cleanup
);
976 void libvlc_audio_set_format( libvlc_media_player_t
*mp
, const char *format
,
977 unsigned rate
, unsigned channels
)
979 var_SetString( mp
, "amem-format", format
);
980 var_SetInteger( mp
, "amem-rate", rate
);
981 var_SetInteger( mp
, "amem-channels", channels
);
985 /**************************************************************************
986 * Getters for stream information
987 **************************************************************************/
988 libvlc_time_t
libvlc_media_player_get_length(
989 libvlc_media_player_t
*p_mi
)
991 input_thread_t
*p_input_thread
;
992 libvlc_time_t i_time
;
994 p_input_thread
= libvlc_get_input_thread ( p_mi
);
995 if( !p_input_thread
)
998 i_time
= from_mtime(var_GetTime( p_input_thread
, "length" ));
999 vlc_object_release( p_input_thread
);
1004 libvlc_time_t
libvlc_media_player_get_time( libvlc_media_player_t
*p_mi
)
1006 input_thread_t
*p_input_thread
;
1007 libvlc_time_t i_time
;
1009 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1010 if( !p_input_thread
)
1013 i_time
= from_mtime(var_GetTime( p_input_thread
, "time" ));
1014 vlc_object_release( p_input_thread
);
1018 void libvlc_media_player_set_time( libvlc_media_player_t
*p_mi
,
1019 libvlc_time_t i_time
)
1021 input_thread_t
*p_input_thread
;
1023 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1024 if( !p_input_thread
)
1027 var_SetTime( p_input_thread
, "time", to_mtime(i_time
) );
1028 vlc_object_release( p_input_thread
);
1031 void libvlc_media_player_set_position( libvlc_media_player_t
*p_mi
,
1034 input_thread_t
*p_input_thread
;
1036 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1037 if( !p_input_thread
)
1040 var_SetFloat( p_input_thread
, "position", position
);
1041 vlc_object_release( p_input_thread
);
1044 float libvlc_media_player_get_position( libvlc_media_player_t
*p_mi
)
1046 input_thread_t
*p_input_thread
;
1049 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1050 if( !p_input_thread
)
1053 f_position
= var_GetFloat( p_input_thread
, "position" );
1054 vlc_object_release( p_input_thread
);
1059 void libvlc_media_player_set_chapter( libvlc_media_player_t
*p_mi
,
1062 input_thread_t
*p_input_thread
;
1064 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1065 if( !p_input_thread
)
1068 var_SetInteger( p_input_thread
, "chapter", chapter
);
1069 vlc_object_release( p_input_thread
);
1072 int libvlc_media_player_get_chapter( libvlc_media_player_t
*p_mi
)
1074 input_thread_t
*p_input_thread
;
1077 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1078 if( !p_input_thread
)
1081 i_chapter
= var_GetInteger( p_input_thread
, "chapter" );
1082 vlc_object_release( p_input_thread
);
1087 int libvlc_media_player_get_chapter_count( libvlc_media_player_t
*p_mi
)
1089 input_thread_t
*p_input_thread
;
1092 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1093 if( !p_input_thread
)
1096 var_Change( p_input_thread
, "chapter", VLC_VAR_CHOICESCOUNT
, &val
, NULL
);
1097 vlc_object_release( p_input_thread
);
1102 int libvlc_media_player_get_chapter_count_for_title(
1103 libvlc_media_player_t
*p_mi
,
1106 input_thread_t
*p_input_thread
;
1109 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1110 if( !p_input_thread
)
1114 if( asprintf( &psz_name
, "title %2i", i_title
) == -1 )
1116 vlc_object_release( p_input_thread
);
1119 var_Change( p_input_thread
, psz_name
, VLC_VAR_CHOICESCOUNT
, &val
, NULL
);
1120 vlc_object_release( p_input_thread
);
1126 void libvlc_media_player_set_title( libvlc_media_player_t
*p_mi
,
1129 input_thread_t
*p_input_thread
;
1131 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1132 if( !p_input_thread
)
1135 var_SetInteger( p_input_thread
, "title", i_title
);
1136 vlc_object_release( p_input_thread
);
1139 libvlc_event_t event
;
1140 event
.type
= libvlc_MediaPlayerTitleChanged
;
1141 event
.u
.media_player_title_changed
.new_title
= i_title
;
1142 libvlc_event_send( p_mi
->p_event_manager
, &event
);
1145 int libvlc_media_player_get_title( libvlc_media_player_t
*p_mi
)
1147 input_thread_t
*p_input_thread
;
1150 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1151 if( !p_input_thread
)
1154 i_title
= var_GetInteger( p_input_thread
, "title" );
1155 vlc_object_release( p_input_thread
);
1160 int libvlc_media_player_get_title_count( libvlc_media_player_t
*p_mi
)
1162 input_thread_t
*p_input_thread
;
1165 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1166 if( !p_input_thread
)
1169 var_Change( p_input_thread
, "title", VLC_VAR_CHOICESCOUNT
, &val
, NULL
);
1170 vlc_object_release( p_input_thread
);
1175 void libvlc_media_player_next_chapter( libvlc_media_player_t
*p_mi
)
1177 input_thread_t
*p_input_thread
;
1179 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1180 if( !p_input_thread
)
1183 int i_type
= var_Type( p_input_thread
, "next-chapter" );
1184 var_TriggerCallback( p_input_thread
, (i_type
& VLC_VAR_TYPE
) != 0 ?
1185 "next-chapter":"next-title" );
1187 vlc_object_release( p_input_thread
);
1190 void libvlc_media_player_previous_chapter( libvlc_media_player_t
*p_mi
)
1192 input_thread_t
*p_input_thread
;
1194 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1195 if( !p_input_thread
)
1198 int i_type
= var_Type( p_input_thread
, "next-chapter" );
1199 var_TriggerCallback( p_input_thread
, (i_type
& VLC_VAR_TYPE
) != 0 ?
1200 "prev-chapter":"prev-title" );
1202 vlc_object_release( p_input_thread
);
1205 float libvlc_media_player_get_fps( libvlc_media_player_t
*p_mi
)
1207 input_thread_t
*p_input_thread
= libvlc_get_input_thread ( p_mi
);
1210 if( p_input_thread
)
1212 if( input_Control( p_input_thread
, INPUT_GET_VIDEO_FPS
, &f_fps
) )
1214 vlc_object_release( p_input_thread
);
1219 int libvlc_media_player_will_play( libvlc_media_player_t
*p_mi
)
1222 input_thread_t
*p_input_thread
=
1223 libvlc_get_input_thread ( p_mi
);
1224 if ( !p_input_thread
)
1227 b_will_play
= !p_input_thread
->b_dead
;
1228 vlc_object_release( p_input_thread
);
1233 int libvlc_media_player_set_rate( libvlc_media_player_t
*p_mi
, float rate
)
1235 var_SetFloat (p_mi
, "rate", rate
);
1237 input_thread_t
*p_input_thread
= libvlc_get_input_thread ( p_mi
);
1238 if( !p_input_thread
)
1240 var_SetFloat( p_input_thread
, "rate", rate
);
1241 vlc_object_release( p_input_thread
);
1245 float libvlc_media_player_get_rate( libvlc_media_player_t
*p_mi
)
1247 return var_GetFloat (p_mi
, "rate");
1250 libvlc_state_t
libvlc_media_player_get_state( libvlc_media_player_t
*p_mi
)
1253 libvlc_state_t state
= p_mi
->state
;
1258 int libvlc_media_player_is_seekable( libvlc_media_player_t
*p_mi
)
1260 input_thread_t
*p_input_thread
;
1263 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1264 if ( !p_input_thread
)
1266 b_seekable
= var_GetBool( p_input_thread
, "can-seek" );
1267 vlc_object_release( p_input_thread
);
1272 void libvlc_media_player_navigate( libvlc_media_player_t
* p_mi
,
1275 static const vlc_action_t map
[] =
1277 INPUT_NAV_ACTIVATE
, INPUT_NAV_UP
, INPUT_NAV_DOWN
,
1278 INPUT_NAV_LEFT
, INPUT_NAV_RIGHT
,
1281 if( navigate
>= sizeof(map
) / sizeof(map
[0]) )
1284 input_thread_t
*p_input
= libvlc_get_input_thread ( p_mi
);
1285 if ( p_input
== NULL
)
1288 input_Control( p_input
, map
[navigate
], NULL
);
1289 vlc_object_release( p_input
);
1292 /* internal function, used by audio, video */
1293 libvlc_track_description_t
*
1294 libvlc_get_track_description( libvlc_media_player_t
*p_mi
,
1295 const char *psz_variable
)
1297 input_thread_t
*p_input
= libvlc_get_input_thread( p_mi
);
1298 libvlc_track_description_t
*p_track_description
= NULL
,
1299 *p_actual
, *p_previous
;
1304 vlc_value_t val_list
, text_list
;
1305 var_Change( p_input
, psz_variable
, VLC_VAR_GETLIST
, &val_list
, &text_list
);
1308 if( val_list
.p_list
->i_count
<= 0 )
1311 p_track_description
= ( libvlc_track_description_t
* )
1312 malloc( sizeof( libvlc_track_description_t
) );
1313 if ( !p_track_description
)
1315 libvlc_printerr( "Not enough memory" );
1318 p_actual
= p_track_description
;
1320 for( int i
= 0; i
< val_list
.p_list
->i_count
; i
++ )
1324 p_actual
= ( libvlc_track_description_t
* )
1325 malloc( sizeof( libvlc_track_description_t
) );
1328 libvlc_track_description_list_release( p_track_description
);
1329 libvlc_printerr( "Not enough memory" );
1333 p_actual
->i_id
= val_list
.p_list
->p_values
[i
].i_int
;
1334 p_actual
->psz_name
= strdup( text_list
.p_list
->p_values
[i
].psz_string
);
1335 p_actual
->p_next
= NULL
;
1337 p_previous
->p_next
= p_actual
;
1338 p_previous
= p_actual
;
1343 var_FreeList( &val_list
, &text_list
);
1344 vlc_object_release( p_input
);
1346 return p_track_description
;
1349 // Deprecated alias for libvlc_track_description_list_release
1350 void libvlc_track_description_release( libvlc_track_description_t
*p_td
)
1352 libvlc_track_description_list_release( p_td
);
1355 void libvlc_track_description_list_release( libvlc_track_description_t
*p_td
)
1357 libvlc_track_description_t
*p_actual
, *p_before
;
1362 free( p_actual
->psz_name
);
1363 p_before
= p_actual
;
1364 p_actual
= p_before
->p_next
;
1369 int libvlc_media_player_can_pause( libvlc_media_player_t
*p_mi
)
1371 input_thread_t
*p_input_thread
;
1374 p_input_thread
= libvlc_get_input_thread ( p_mi
);
1375 if ( !p_input_thread
)
1377 b_can_pause
= var_GetBool( p_input_thread
, "can-pause" );
1378 vlc_object_release( p_input_thread
);
1383 void libvlc_media_player_next_frame( libvlc_media_player_t
*p_mi
)
1385 input_thread_t
*p_input_thread
= libvlc_get_input_thread ( p_mi
);
1386 if( p_input_thread
!= NULL
)
1388 var_TriggerCallback( p_input_thread
, "frame-next" );
1389 vlc_object_release( p_input_thread
);
1393 void libvlc_media_player_set_video_title_display( libvlc_media_player_t
*p_mi
, libvlc_position_t position
, unsigned timeout
)
1395 if ( position
!= libvlc_position_disable
)
1397 var_SetBool( p_mi
, "video-title-show", true );
1398 var_SetInteger( p_mi
, "video-title-position", position
);
1399 var_SetInteger( p_mi
, "video-title-timeout", timeout
);
1403 var_SetBool( p_mi
, "video-title-show", false );
1408 * Maximum size of a formatted equalizer amplification band frequency value.
1410 * The allowed value range is supposed to be constrained from -20.0 to 20.0.
1412 * The format string " %.07f" with a minimum value of "-20" gives a maximum
1413 * string length of e.g. " -19.1234567", i.e. 12 bytes (not including the null
1416 #define EQZ_BAND_VALUE_SIZE 12
1418 int libvlc_media_player_set_equalizer( libvlc_media_player_t
*p_mi
, libvlc_equalizer_t
*p_equalizer
)
1425 f_preamp
= p_equalizer
->f_preamp
;
1427 psz_bands
= malloc( EQZ_BANDS_MAX
* EQZ_BAND_VALUE_SIZE
+ 1 );
1428 if ( unlikely( psz_bands
== NULL
) )
1431 char *p
= psz_bands
;
1433 for ( int i
= 0; i
< EQZ_BANDS_MAX
; i
++ )
1435 c
= snprintf( p
, EQZ_BAND_VALUE_SIZE
+ 1, " %.07f", p_equalizer
->f_amp
[i
] );
1436 if ( unlikely( c
>= EQZ_BAND_VALUE_SIZE
+ 1 ) )
1451 var_SetFloat( p_mi
, "equalizer-preamp", f_preamp
);
1452 var_SetString( p_mi
, "equalizer-bands", psz_bands
);
1454 audio_output_t
*p_aout
= input_resource_HoldAout( p_mi
->input
.p_resource
);
1457 var_SetFloat( p_aout
, "equalizer-preamp", f_preamp
);
1458 var_SetString( p_aout
, "equalizer-bands", psz_bands
);
1460 vlc_object_release( p_aout
);