skins2: use new vout_window_ReportSize
[vlc.git] / lib / media_player.c
blob64240aaa3cb0f0c2c2e7d2940be44869a1bc29bd
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 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <assert.h>
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>
35 #include <vlc_vout.h>
36 #include <vlc_aout.h>
37 #include <vlc_keys.h>
39 #include "libvlc_internal.h"
40 #include "media_internal.h" // libvlc_media_set_state()
41 #include "media_player_internal.h"
43 static int
44 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
45 vlc_value_t oldval, vlc_value_t newval,
46 void * p_userdata );
47 static int
48 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
49 vlc_value_t oldval, vlc_value_t newval,
50 void * p_userdata );
51 static int
52 input_scrambled_changed( vlc_object_t * p_this, char const * psz_cmd,
53 vlc_value_t oldval, vlc_value_t newval,
54 void * p_userdata );
55 static int
56 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
57 vlc_value_t oldval, vlc_value_t newval,
58 void * p_userdata );
60 static int
61 input_es_changed( vlc_object_t * p_this, char const * psz_cmd,
62 int action, vlc_value_t *p_val,
63 void *p_userdata);
65 static int
66 input_es_selected( vlc_object_t * p_this, char const * psz_cmd,
67 vlc_value_t oldval, vlc_value_t newval,
68 void * p_userdata );
70 static void
71 add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi );
73 static void
74 del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi );
76 static int
77 snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
78 vlc_value_t oldval, vlc_value_t newval, void *p_data );
80 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
83 * Shortcuts
86 #define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
87 static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type_t type)
89 libvlc_event_manager_register_event_type(mp->p_event_manager, type);
93 * The input lock protects the input and input resource pointer.
94 * It MUST NOT be used from callbacks.
96 * The object lock protects the reset, namely the media and the player state.
97 * It can, and usually needs to be taken from callbacks.
98 * The object lock can be acquired under the input lock... and consequently
99 * the opposite order is STRICTLY PROHIBITED.
101 static inline void lock(libvlc_media_player_t *mp)
103 vlc_mutex_lock(&mp->object_lock);
106 static inline void unlock(libvlc_media_player_t *mp)
108 vlc_mutex_unlock(&mp->object_lock);
111 static inline void lock_input(libvlc_media_player_t *mp)
113 vlc_mutex_lock(&mp->input.lock);
116 static inline void unlock_input(libvlc_media_player_t *mp)
118 vlc_mutex_unlock(&mp->input.lock);
122 * Release the associated input thread.
124 * Object lock is NOT held.
125 * Input lock is held or instance is being destroyed.
127 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
129 assert( p_mi );
131 input_thread_t *p_input_thread = p_mi->input.p_thread;
132 if( !p_input_thread )
133 return;
134 p_mi->input.p_thread = NULL;
136 var_DelCallback( p_input_thread, "can-seek",
137 input_seekable_changed, p_mi );
138 var_DelCallback( p_input_thread, "can-pause",
139 input_pausable_changed, p_mi );
140 var_DelCallback( p_input_thread, "program-scrambled",
141 input_scrambled_changed, p_mi );
142 var_DelCallback( p_input_thread, "intf-event",
143 input_event_changed, p_mi );
144 del_es_callbacks( p_input_thread, p_mi );
146 /* We owned this one */
147 input_Stop( p_input_thread, b_input_abort );
148 input_Close( p_input_thread );
152 * Retrieve the input thread. Be sure to release the object
153 * once you are done with it. (libvlc Internal)
155 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
157 input_thread_t *p_input_thread;
159 assert( p_mi );
161 lock_input(p_mi);
162 p_input_thread = p_mi->input.p_thread;
163 if( p_input_thread )
164 vlc_object_hold( p_input_thread );
165 else
166 libvlc_printerr( "No active input" );
167 unlock_input(p_mi);
169 return p_input_thread;
173 * Set the internal state of the media_player. (media player Internal)
175 * Function will lock the media_player.
177 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
178 bool b_locked )
180 if(!b_locked)
181 lock(p_mi);
182 p_mi->state = state;
184 libvlc_media_t *media = p_mi->p_md;
185 if (media)
186 libvlc_media_retain(media);
188 if(!b_locked)
189 unlock(p_mi);
191 if (media)
193 // Also set the state of the corresponding media
194 // This is strictly for convenience.
195 libvlc_media_set_state(media, state);
197 libvlc_media_release(media);
201 static int
202 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
203 vlc_value_t oldval, vlc_value_t newval,
204 void * p_userdata )
206 VLC_UNUSED(oldval);
207 VLC_UNUSED(p_this);
208 VLC_UNUSED(psz_cmd);
209 libvlc_media_player_t * p_mi = p_userdata;
210 libvlc_event_t event;
212 event.type = libvlc_MediaPlayerSeekableChanged;
213 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
215 libvlc_event_send( p_mi->p_event_manager, &event );
216 return VLC_SUCCESS;
219 static int
220 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
221 vlc_value_t oldval, vlc_value_t newval,
222 void * p_userdata )
224 VLC_UNUSED(oldval);
225 VLC_UNUSED(p_this);
226 VLC_UNUSED(psz_cmd);
227 libvlc_media_player_t * p_mi = p_userdata;
228 libvlc_event_t event;
230 event.type = libvlc_MediaPlayerPausableChanged;
231 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
233 libvlc_event_send( p_mi->p_event_manager, &event );
234 return VLC_SUCCESS;
237 static int
238 input_scrambled_changed( vlc_object_t * p_this, char const * psz_cmd,
239 vlc_value_t oldval, vlc_value_t newval,
240 void * p_userdata )
242 VLC_UNUSED(oldval);
243 VLC_UNUSED(p_this);
244 VLC_UNUSED(psz_cmd);
245 libvlc_media_player_t * p_mi = p_userdata;
246 libvlc_event_t event;
248 event.type = libvlc_MediaPlayerScrambledChanged;
249 event.u.media_player_scrambled_changed.new_scrambled = newval.b_bool;
251 libvlc_event_send( p_mi->p_event_manager, &event );
252 return VLC_SUCCESS;
255 static int
256 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
257 vlc_value_t oldval, vlc_value_t newval,
258 void * p_userdata )
260 VLC_UNUSED(oldval);
261 input_thread_t * p_input = (input_thread_t *)p_this;
262 libvlc_media_player_t * p_mi = p_userdata;
263 libvlc_event_t event;
265 assert( !strcmp( psz_cmd, "intf-event" ) );
267 if( newval.i_int == INPUT_EVENT_STATE )
269 libvlc_state_t libvlc_state;
271 switch ( var_GetInteger( p_input, "state" ) )
273 case INIT_S:
274 libvlc_state = libvlc_NothingSpecial;
275 event.type = libvlc_MediaPlayerNothingSpecial;
276 break;
277 case OPENING_S:
278 libvlc_state = libvlc_Opening;
279 event.type = libvlc_MediaPlayerOpening;
280 break;
281 case PLAYING_S:
282 libvlc_state = libvlc_Playing;
283 event.type = libvlc_MediaPlayerPlaying;
284 break;
285 case PAUSE_S:
286 libvlc_state = libvlc_Paused;
287 event.type = libvlc_MediaPlayerPaused;
288 break;
289 case END_S:
290 libvlc_state = libvlc_Ended;
291 event.type = libvlc_MediaPlayerEndReached;
292 break;
293 case ERROR_S:
294 libvlc_state = libvlc_Error;
295 event.type = libvlc_MediaPlayerEncounteredError;
296 break;
298 default:
299 return VLC_SUCCESS;
302 set_state( p_mi, libvlc_state, false );
303 libvlc_event_send( p_mi->p_event_manager, &event );
305 else if( newval.i_int == INPUT_EVENT_ABORT )
307 libvlc_state_t libvlc_state = libvlc_Stopped;
308 event.type = libvlc_MediaPlayerStopped;
310 set_state( p_mi, libvlc_state, false );
311 libvlc_event_send( p_mi->p_event_manager, &event );
313 else if( newval.i_int == INPUT_EVENT_POSITION )
315 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
316 return VLC_SUCCESS; /* Don't send the position while stopped */
318 /* */
319 event.type = libvlc_MediaPlayerPositionChanged;
320 event.u.media_player_position_changed.new_position =
321 var_GetFloat( p_input, "position" );
322 libvlc_event_send( p_mi->p_event_manager, &event );
324 /* */
325 event.type = libvlc_MediaPlayerTimeChanged;
326 event.u.media_player_time_changed.new_time =
327 from_mtime(var_GetTime( p_input, "time" ));
328 libvlc_event_send( p_mi->p_event_manager, &event );
330 else if( newval.i_int == INPUT_EVENT_LENGTH )
332 event.type = libvlc_MediaPlayerLengthChanged;
333 event.u.media_player_length_changed.new_length =
334 from_mtime(var_GetTime( p_input, "length" ));
335 libvlc_event_send( p_mi->p_event_manager, &event );
337 else if( newval.i_int == INPUT_EVENT_CACHE )
339 event.type = libvlc_MediaPlayerBuffering;
340 event.u.media_player_buffering.new_cache = (int)(100 *
341 var_GetFloat( p_input, "cache" ));
342 libvlc_event_send( p_mi->p_event_manager, &event );
344 else if( newval.i_int == INPUT_EVENT_VOUT )
346 vout_thread_t **pp_vout;
347 size_t i_vout;
348 if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
350 i_vout = 0;
352 else
354 for( size_t i = 0; i < i_vout; i++ )
355 vlc_object_release( pp_vout[i] );
356 free( pp_vout );
359 event.type = libvlc_MediaPlayerVout;
360 event.u.media_player_vout.new_count = i_vout;
361 libvlc_event_send( p_mi->p_event_manager, &event );
364 return VLC_SUCCESS;
367 static int track_type_from_name(const char *psz_name)
369 if( !strcmp( psz_name, "video-es" ) )
370 return libvlc_track_video;
371 else if( !strcmp( psz_name, "audio-es" ) )
372 return libvlc_track_audio;
373 else if( !strcmp( psz_name, "spu-es" ) )
374 return libvlc_track_text;
375 else
376 return libvlc_track_unknown;
379 static int input_es_changed( vlc_object_t *p_this,
380 char const *psz_cmd,
381 int action,
382 vlc_value_t *p_val,
383 void *p_userdata )
385 VLC_UNUSED(p_this);
386 libvlc_media_player_t *mp = p_userdata;
387 libvlc_event_t event;
389 /* Ignore the "Disable" element */
390 if (p_val && p_val->i_int < 0)
391 return VLC_EGENERIC;
393 switch (action)
395 case VLC_VAR_ADDCHOICE:
396 event.type = libvlc_MediaPlayerESAdded;
397 break;
398 case VLC_VAR_DELCHOICE:
399 case VLC_VAR_CLEARCHOICES:
400 event.type = libvlc_MediaPlayerESDeleted;
401 break;
402 default:
403 return VLC_EGENERIC;
406 event.u.media_player_es_changed.i_type = track_type_from_name(psz_cmd);
408 int i_id;
409 if (action != VLC_VAR_CLEARCHOICES)
411 if (!p_val)
412 return VLC_EGENERIC;
413 i_id = p_val->i_int;
415 else
417 /* -1 means all ES tracks of this type were deleted. */
418 i_id = -1;
420 event.u.media_player_es_changed.i_id = i_id;
422 libvlc_event_send(mp->p_event_manager, &event);
424 return VLC_SUCCESS;
427 static int
428 input_es_selected( vlc_object_t * p_this, char const * psz_cmd,
429 vlc_value_t oldval, vlc_value_t newval,
430 void * p_userdata )
432 VLC_UNUSED(p_this);
433 VLC_UNUSED(oldval);
434 libvlc_media_player_t *mp = p_userdata;
435 libvlc_event_t event;
437 event.type = libvlc_MediaPlayerESSelected;
438 event.u.media_player_es_changed.i_type = track_type_from_name(psz_cmd);
439 event.u.media_player_es_changed.i_id = newval.i_int;
441 libvlc_event_send(mp->p_event_manager, &event);
443 return VLC_SUCCESS;
446 /**************************************************************************
447 * Snapshot Taken Event.
449 * FIXME: This snapshot API interface makes no sense in media_player.
450 *************************************************************************/
451 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
452 vlc_value_t oldval, vlc_value_t newval, void *p_data )
454 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
456 libvlc_media_player_t *mp = p_data;
457 libvlc_event_t event;
458 event.type = libvlc_MediaPlayerSnapshotTaken;
459 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
460 libvlc_event_send(mp->p_event_manager, &event);
462 return VLC_SUCCESS;
465 /* */
466 static void libvlc_media_player_destroy( libvlc_media_player_t * );
469 /**************************************************************************
470 * Create a Media Instance object.
472 * Refcount strategy:
473 * - All items created by _new start with a refcount set to 1.
474 * - Accessor _release decrease the refcount by 1, if after that
475 * operation the refcount is 0, the object is destroyed.
476 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
478 * Object locking strategy:
479 * - No lock held while in constructor.
480 * - When accessing any member variable this lock is held. (XXX who locks?)
481 * - When attempting to destroy the object the lock is also held.
482 **************************************************************************/
483 libvlc_media_player_t *
484 libvlc_media_player_new( libvlc_instance_t *instance )
486 libvlc_media_player_t * mp;
488 assert(instance);
490 mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
491 if (unlikely(mp == NULL))
493 libvlc_printerr("Not enough memory");
494 return NULL;
497 /* Input */
498 var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
500 /* Video */
501 var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
502 var_Create (mp, "window", VLC_VAR_STRING);
503 var_Create (mp, "vmem-lock", VLC_VAR_ADDRESS);
504 var_Create (mp, "vmem-unlock", VLC_VAR_ADDRESS);
505 var_Create (mp, "vmem-display", VLC_VAR_ADDRESS);
506 var_Create (mp, "vmem-data", VLC_VAR_ADDRESS);
507 var_Create (mp, "vmem-setup", VLC_VAR_ADDRESS);
508 var_Create (mp, "vmem-cleanup", VLC_VAR_ADDRESS);
509 var_Create (mp, "vmem-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
510 var_Create (mp, "vmem-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
511 var_Create (mp, "vmem-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
512 var_Create (mp, "vmem-pitch", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
513 var_Create (mp, "avcodec-hw", VLC_VAR_STRING);
514 var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
515 #if defined (_WIN32) || defined (__OS2__)
516 var_Create (mp, "drawable-hwnd", VLC_VAR_INTEGER);
517 #endif
518 #ifdef __APPLE__
519 var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
520 var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
521 #endif
523 var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
524 var_SetBool (mp, "keyboard-events", true);
525 var_Create (mp, "mouse-events", VLC_VAR_BOOL);
526 var_SetBool (mp, "mouse-events", true);
528 var_Create (mp, "fullscreen", VLC_VAR_BOOL);
529 var_Create (mp, "autoscale", VLC_VAR_BOOL);
530 var_SetBool (mp, "autoscale", true);
531 var_Create (mp, "scale", VLC_VAR_FLOAT);
532 var_SetFloat (mp, "scale", 1.);
533 var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
534 var_Create (mp, "crop", VLC_VAR_STRING);
535 var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
536 var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
538 var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
540 var_Create (mp, "marq-marquee", VLC_VAR_STRING);
541 var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
542 var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
543 var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
544 var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
545 var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
546 var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
547 var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
548 var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
550 var_Create (mp, "logo-file", VLC_VAR_STRING);
551 var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
552 var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
553 var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
554 var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
555 var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
556 var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
558 var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
559 var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
560 var_Create (mp, "hue", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
561 var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
562 var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
564 /* Audio */
565 var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
566 var_Create (mp, "mute", VLC_VAR_BOOL);
567 var_Create (mp, "volume", VLC_VAR_FLOAT);
568 var_Create (mp, "corks", VLC_VAR_INTEGER);
569 var_Create (mp, "audio-filter", VLC_VAR_STRING);
570 var_Create (mp, "amem-data", VLC_VAR_ADDRESS);
571 var_Create (mp, "amem-setup", VLC_VAR_ADDRESS);
572 var_Create (mp, "amem-cleanup", VLC_VAR_ADDRESS);
573 var_Create (mp, "amem-play", VLC_VAR_ADDRESS);
574 var_Create (mp, "amem-pause", VLC_VAR_ADDRESS);
575 var_Create (mp, "amem-resume", VLC_VAR_ADDRESS);
576 var_Create (mp, "amem-flush", VLC_VAR_ADDRESS);
577 var_Create (mp, "amem-drain", VLC_VAR_ADDRESS);
578 var_Create (mp, "amem-set-volume", VLC_VAR_ADDRESS);
579 var_Create (mp, "amem-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
580 var_Create (mp, "amem-rate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
581 var_Create (mp, "amem-channels", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
583 /* Video Title */
584 var_Create (mp, "video-title-show", VLC_VAR_BOOL);
585 var_Create (mp, "video-title-position", VLC_VAR_INTEGER);
586 var_Create (mp, "video-title-timeout", VLC_VAR_INTEGER);
588 /* Equalizer */
589 var_Create (mp, "equalizer-preamp", VLC_VAR_FLOAT);
590 var_Create (mp, "equalizer-vlcfreqs", VLC_VAR_BOOL);
591 var_Create (mp, "equalizer-bands", VLC_VAR_STRING);
593 mp->p_md = NULL;
594 mp->state = libvlc_NothingSpecial;
595 mp->p_libvlc_instance = instance;
596 mp->input.p_thread = NULL;
597 mp->input.p_resource = input_resource_New(VLC_OBJECT(mp));
598 if (unlikely(mp->input.p_resource == NULL))
600 vlc_object_release(mp);
601 return NULL;
603 audio_output_t *aout = input_resource_GetAout(mp->input.p_resource);
604 if( aout != NULL )
605 input_resource_PutAout(mp->input.p_resource, aout);
607 vlc_mutex_init (&mp->input.lock);
608 mp->i_refcount = 1;
609 mp->p_event_manager = libvlc_event_manager_new(mp, instance);
610 if (unlikely(mp->p_event_manager == NULL))
612 input_resource_Release(mp->input.p_resource);
613 vlc_object_release(mp);
614 return NULL;
616 vlc_mutex_init(&mp->object_lock);
618 register_event(mp, NothingSpecial);
619 register_event(mp, Opening);
620 register_event(mp, Buffering);
621 register_event(mp, Playing);
622 register_event(mp, Paused);
623 register_event(mp, Stopped);
624 register_event(mp, Forward);
625 register_event(mp, Backward);
626 register_event(mp, EndReached);
627 register_event(mp, EncounteredError);
628 register_event(mp, SeekableChanged);
630 register_event(mp, PositionChanged);
631 register_event(mp, TimeChanged);
632 register_event(mp, LengthChanged);
633 register_event(mp, TitleChanged);
634 register_event(mp, PausableChanged);
636 register_event(mp, Vout);
637 register_event(mp, ScrambledChanged);
638 register_event(mp, ESAdded);
639 register_event(mp, ESDeleted);
640 register_event(mp, ESSelected);
642 /* Snapshot initialization */
643 register_event(mp, SnapshotTaken);
645 register_event(mp, MediaChanged);
647 /* Attach a var callback to the global object to provide the glue between
648 * vout_thread that generates the event and media_player that re-emits it
649 * with its own event manager
651 * FIXME: It's unclear why we want to put this in public API, and why we
652 * want to expose it in such a limiting and ugly way.
654 var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
656 libvlc_retain(instance);
657 return mp;
660 /**************************************************************************
661 * Create a Media Instance object with a media descriptor.
662 **************************************************************************/
663 libvlc_media_player_t *
664 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
666 libvlc_media_player_t * p_mi;
668 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
669 if( !p_mi )
670 return NULL;
672 libvlc_media_retain( p_md );
673 p_mi->p_md = p_md;
675 return p_mi;
678 /**************************************************************************
679 * Destroy a Media Instance object (libvlc internal)
681 * Warning: No lock held here, but hey, this is internal. Caller must lock.
682 **************************************************************************/
683 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
685 assert( p_mi );
687 /* Detach Callback from the main libvlc object */
688 var_DelCallback( p_mi->p_libvlc,
689 "snapshot-file", snapshot_was_taken, p_mi );
691 /* No need for lock_input() because no other threads knows us anymore */
692 if( p_mi->input.p_thread )
693 release_input_thread(p_mi, true);
694 input_resource_Terminate( p_mi->input.p_resource );
695 input_resource_Release( p_mi->input.p_resource );
696 vlc_mutex_destroy( &p_mi->input.lock );
698 libvlc_event_manager_release( p_mi->p_event_manager );
699 libvlc_media_release( p_mi->p_md );
700 vlc_mutex_destroy( &p_mi->object_lock );
702 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
703 vlc_object_release( p_mi );
704 libvlc_release(instance);
707 /**************************************************************************
708 * Release a Media Instance object.
710 * Function does the locking.
711 **************************************************************************/
712 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
714 bool destroy;
716 assert( p_mi );
717 lock(p_mi);
718 destroy = !--p_mi->i_refcount;
719 unlock(p_mi);
721 if( destroy )
722 libvlc_media_player_destroy( p_mi );
725 /**************************************************************************
726 * Retain a Media Instance object.
728 * Caller must hold the lock.
729 **************************************************************************/
730 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
732 assert( p_mi );
734 lock(p_mi);
735 p_mi->i_refcount++;
736 unlock(p_mi);
739 /**************************************************************************
740 * Set the Media descriptor associated with the instance.
742 * Enter without lock -- function will lock the object.
743 **************************************************************************/
744 void libvlc_media_player_set_media(
745 libvlc_media_player_t *p_mi,
746 libvlc_media_t *p_md )
748 lock_input(p_mi);
750 /* FIXME I am not sure if it is a user request or on die(eof/error)
751 * request here */
752 release_input_thread( p_mi,
753 p_mi->input.p_thread &&
754 !p_mi->input.p_thread->b_eof &&
755 !p_mi->input.p_thread->b_error );
757 lock( p_mi );
758 set_state( p_mi, libvlc_NothingSpecial, true );
759 unlock_input( p_mi );
761 libvlc_media_release( p_mi->p_md );
763 if( !p_md )
765 p_mi->p_md = NULL;
766 unlock(p_mi);
767 return; /* It is ok to pass a NULL md */
770 libvlc_media_retain( p_md );
771 p_mi->p_md = p_md;
773 /* The policy here is to ignore that we were created using a different
774 * libvlc_instance, because we don't really care */
775 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
777 unlock(p_mi);
779 /* Send an event for the newly available media */
780 libvlc_event_t event;
781 event.type = libvlc_MediaPlayerMediaChanged;
782 event.u.media_player_media_changed.new_media = p_md;
783 libvlc_event_send( p_mi->p_event_manager, &event );
787 /**************************************************************************
788 * Get the Media descriptor associated with the instance.
789 **************************************************************************/
790 libvlc_media_t *
791 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
793 libvlc_media_t *p_m;
795 lock( p_mi );
796 p_m = p_mi->p_md;
797 if( p_m )
798 libvlc_media_retain( p_m );
799 unlock( p_mi );
801 return p_m;
804 /**************************************************************************
805 * Get the event Manager.
806 **************************************************************************/
807 libvlc_event_manager_t *
808 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
810 return p_mi->p_event_manager;
813 static void add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
815 var_AddListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
816 var_AddListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
817 var_AddListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
818 var_AddCallback( p_input_thread, "video-es", input_es_selected, p_mi );
819 var_AddCallback( p_input_thread, "audio-es", input_es_selected, p_mi );
820 var_AddCallback( p_input_thread, "spu-es", input_es_selected, p_mi );
823 static void del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
825 var_DelListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
826 var_DelListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
827 var_DelListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
828 var_DelCallback( p_input_thread, "video-es", input_es_selected, p_mi );
829 var_DelCallback( p_input_thread, "audio-es", input_es_selected, p_mi );
830 var_DelCallback( p_input_thread, "spu-es", input_es_selected, p_mi );
833 /**************************************************************************
834 * Tell media player to start playing.
835 **************************************************************************/
836 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
838 lock_input( p_mi );
840 input_thread_t *p_input_thread = p_mi->input.p_thread;
841 if( p_input_thread )
843 /* A thread already exists, send it a play message */
844 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
845 unlock_input( p_mi );
846 return 0;
849 /* Ignore previous exception */
850 lock(p_mi);
852 if( !p_mi->p_md )
854 unlock(p_mi);
855 unlock_input( p_mi );
856 libvlc_printerr( "No associated media descriptor" );
857 return -1;
860 p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
861 p_mi->input.p_resource );
862 unlock(p_mi);
863 if( !p_input_thread )
865 unlock_input(p_mi);
866 libvlc_printerr( "Not enough memory" );
867 return -1;
870 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
871 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
872 var_AddCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
873 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
874 add_es_callbacks( p_input_thread, p_mi );
876 if( input_Start( p_input_thread ) )
878 unlock_input(p_mi);
879 del_es_callbacks( p_input_thread, p_mi );
880 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
881 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
882 var_DelCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
883 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
884 vlc_object_release( p_input_thread );
885 libvlc_printerr( "Input initialization failure" );
886 return -1;
888 p_mi->input.p_thread = p_input_thread;
889 unlock_input(p_mi);
890 return 0;
893 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
895 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
896 if( !p_input_thread )
897 return;
899 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
900 if( state == libvlc_Playing || state == libvlc_Buffering )
902 if( paused )
904 if( libvlc_media_player_can_pause( p_mi ) )
905 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
906 else
907 input_Stop( p_input_thread, true );
910 else
912 if( !paused )
913 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
916 vlc_object_release( p_input_thread );
919 /**************************************************************************
920 * Toggle pause.
921 **************************************************************************/
922 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
924 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
925 bool playing = (state == libvlc_Playing || state == libvlc_Buffering);
927 libvlc_media_player_set_pause( p_mi, playing );
930 /**************************************************************************
931 * Tells whether the media player is currently playing.
933 * Enter with lock held.
934 **************************************************************************/
935 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
937 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
938 return (libvlc_Playing == state) || (libvlc_Buffering == state);
941 /**************************************************************************
942 * Stop playing.
943 **************************************************************************/
944 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
946 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
948 lock_input(p_mi);
949 release_input_thread( p_mi, true ); /* This will stop the input thread */
951 /* Force to go to stopped state, in case we were in Ended, or Error
952 * state. */
953 if( state != libvlc_Stopped )
955 set_state( p_mi, libvlc_Stopped, false );
957 /* Construct and send the event */
958 libvlc_event_t event;
959 event.type = libvlc_MediaPlayerStopped;
960 libvlc_event_send( p_mi->p_event_manager, &event );
963 input_resource_Terminate( p_mi->input.p_resource );
964 unlock_input(p_mi);
968 void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
969 void *(*lock_cb) (void *, void **),
970 void (*unlock_cb) (void *, void *, void *const *),
971 void (*display_cb) (void *, void *),
972 void *opaque )
974 var_SetAddress( mp, "vmem-lock", lock_cb );
975 var_SetAddress( mp, "vmem-unlock", unlock_cb );
976 var_SetAddress( mp, "vmem-display", display_cb );
977 var_SetAddress( mp, "vmem-data", opaque );
978 var_SetString( mp, "vout", "vmem" );
979 var_SetString( mp, "avcodec-hw", "none" );
982 void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
983 libvlc_video_format_cb setup,
984 libvlc_video_cleanup_cb cleanup )
986 var_SetAddress( mp, "vmem-setup", setup );
987 var_SetAddress( mp, "vmem-cleanup", cleanup );
990 void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
991 unsigned width, unsigned height, unsigned pitch )
993 var_SetString( mp, "vmem-chroma", chroma );
994 var_SetInteger( mp, "vmem-width", width );
995 var_SetInteger( mp, "vmem-height", height );
996 var_SetInteger( mp, "vmem-pitch", pitch );
999 /**************************************************************************
1000 * set_nsobject
1001 **************************************************************************/
1002 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
1003 void * drawable )
1005 assert (p_mi != NULL);
1006 #ifdef __APPLE__
1007 var_SetAddress (p_mi, "drawable-nsobject", drawable);
1008 #else
1009 (void) p_mi; (void)drawable;
1010 #endif
1013 /**************************************************************************
1014 * get_nsobject
1015 **************************************************************************/
1016 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
1018 assert (p_mi != NULL);
1019 #ifdef __APPLE__
1020 return var_GetAddress (p_mi, "drawable-nsobject");
1021 #else
1022 return NULL;
1023 #endif
1026 /**************************************************************************
1027 * set_agl
1028 **************************************************************************/
1029 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
1030 uint32_t drawable )
1032 #ifdef __APPLE__
1033 var_SetInteger (p_mi, "drawable-agl", drawable);
1034 #else
1035 (void) p_mi; (void)drawable;
1036 #endif
1039 /**************************************************************************
1040 * get_agl
1041 **************************************************************************/
1042 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
1044 assert (p_mi != NULL);
1045 #ifdef __APPLE__
1046 return var_GetInteger (p_mi, "drawable-agl");
1047 #else
1048 return 0;
1049 #endif
1052 /**************************************************************************
1053 * set_xwindow
1054 **************************************************************************/
1055 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
1056 uint32_t drawable )
1058 assert (p_mi != NULL);
1060 var_SetString (p_mi, "avcodec-hw", "");
1061 var_SetString (p_mi, "vout", drawable ? "xid" : "any");
1062 var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "any");
1063 var_SetInteger (p_mi, "drawable-xid", drawable);
1066 /**************************************************************************
1067 * get_xwindow
1068 **************************************************************************/
1069 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
1071 return var_GetInteger (p_mi, "drawable-xid");
1074 /**************************************************************************
1075 * set_hwnd
1076 **************************************************************************/
1077 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
1078 void *drawable )
1080 assert (p_mi != NULL);
1081 #if defined (_WIN32) || defined (__OS2__)
1082 var_SetString (p_mi, "window",
1083 (drawable != NULL) ? "embed-hwnd,any" : "");
1084 var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
1085 #else
1086 (void) p_mi; (void) drawable;
1087 #endif
1090 /**************************************************************************
1091 * get_hwnd
1092 **************************************************************************/
1093 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
1095 assert (p_mi != NULL);
1096 #if defined (_WIN32) || defined (__OS2__)
1097 return (void *)(uintptr_t)var_GetInteger (p_mi, "drawable-hwnd");
1098 #else
1099 return NULL;
1100 #endif
1103 void libvlc_audio_set_callbacks( libvlc_media_player_t *mp,
1104 libvlc_audio_play_cb play_cb,
1105 libvlc_audio_pause_cb pause_cb,
1106 libvlc_audio_resume_cb resume_cb,
1107 libvlc_audio_flush_cb flush_cb,
1108 libvlc_audio_drain_cb drain_cb,
1109 void *opaque )
1111 var_SetAddress( mp, "amem-play", play_cb );
1112 var_SetAddress( mp, "amem-pause", pause_cb );
1113 var_SetAddress( mp, "amem-resume", resume_cb );
1114 var_SetAddress( mp, "amem-flush", flush_cb );
1115 var_SetAddress( mp, "amem-drain", drain_cb );
1116 var_SetAddress( mp, "amem-data", opaque );
1117 var_SetString( mp, "aout", "amem,none" );
1119 input_resource_ResetAout(mp->input.p_resource);
1122 void libvlc_audio_set_volume_callback( libvlc_media_player_t *mp,
1123 libvlc_audio_set_volume_cb cb )
1125 var_SetAddress( mp, "amem-set-volume", cb );
1127 input_resource_ResetAout(mp->input.p_resource);
1130 void libvlc_audio_set_format_callbacks( libvlc_media_player_t *mp,
1131 libvlc_audio_setup_cb setup,
1132 libvlc_audio_cleanup_cb cleanup )
1134 var_SetAddress( mp, "amem-setup", setup );
1135 var_SetAddress( mp, "amem-cleanup", cleanup );
1137 input_resource_ResetAout(mp->input.p_resource);
1140 void libvlc_audio_set_format( libvlc_media_player_t *mp, const char *format,
1141 unsigned rate, unsigned channels )
1143 var_SetString( mp, "amem-format", format );
1144 var_SetInteger( mp, "amem-rate", rate );
1145 var_SetInteger( mp, "amem-channels", channels );
1147 input_resource_ResetAout(mp->input.p_resource);
1151 /**************************************************************************
1152 * Getters for stream information
1153 **************************************************************************/
1154 libvlc_time_t libvlc_media_player_get_length(
1155 libvlc_media_player_t *p_mi )
1157 input_thread_t *p_input_thread;
1158 libvlc_time_t i_time;
1160 p_input_thread = libvlc_get_input_thread ( p_mi );
1161 if( !p_input_thread )
1162 return -1;
1164 i_time = from_mtime(var_GetTime( p_input_thread, "length" ));
1165 vlc_object_release( p_input_thread );
1167 return i_time;
1170 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
1172 input_thread_t *p_input_thread;
1173 libvlc_time_t i_time;
1175 p_input_thread = libvlc_get_input_thread ( p_mi );
1176 if( !p_input_thread )
1177 return -1;
1179 i_time = from_mtime(var_GetTime( p_input_thread , "time" ));
1180 vlc_object_release( p_input_thread );
1181 return i_time;
1184 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
1185 libvlc_time_t i_time )
1187 input_thread_t *p_input_thread;
1189 p_input_thread = libvlc_get_input_thread ( p_mi );
1190 if( !p_input_thread )
1191 return;
1193 var_SetTime( p_input_thread, "time", to_mtime(i_time) );
1194 vlc_object_release( p_input_thread );
1197 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
1198 float position )
1200 input_thread_t *p_input_thread;
1202 p_input_thread = libvlc_get_input_thread ( p_mi );
1203 if( !p_input_thread )
1204 return;
1206 var_SetFloat( p_input_thread, "position", position );
1207 vlc_object_release( p_input_thread );
1210 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
1212 input_thread_t *p_input_thread;
1213 float f_position;
1215 p_input_thread = libvlc_get_input_thread ( p_mi );
1216 if( !p_input_thread )
1217 return -1.0;
1219 f_position = var_GetFloat( p_input_thread, "position" );
1220 vlc_object_release( p_input_thread );
1222 return f_position;
1225 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
1226 int chapter )
1228 input_thread_t *p_input_thread;
1230 p_input_thread = libvlc_get_input_thread ( p_mi );
1231 if( !p_input_thread )
1232 return;
1234 var_SetInteger( p_input_thread, "chapter", chapter );
1235 vlc_object_release( p_input_thread );
1238 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
1240 input_thread_t *p_input_thread;
1241 int i_chapter;
1243 p_input_thread = libvlc_get_input_thread ( p_mi );
1244 if( !p_input_thread )
1245 return -1;
1247 i_chapter = var_GetInteger( p_input_thread, "chapter" );
1248 vlc_object_release( p_input_thread );
1250 return i_chapter;
1253 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1255 input_thread_t *p_input_thread;
1256 vlc_value_t val;
1258 p_input_thread = libvlc_get_input_thread ( p_mi );
1259 if( !p_input_thread )
1260 return -1;
1262 int i_ret = var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
1263 vlc_object_release( p_input_thread );
1265 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1268 int libvlc_media_player_get_chapter_count_for_title(
1269 libvlc_media_player_t *p_mi,
1270 int i_title )
1272 input_thread_t *p_input_thread;
1273 vlc_value_t val;
1275 p_input_thread = libvlc_get_input_thread ( p_mi );
1276 if( !p_input_thread )
1277 return -1;
1279 char *psz_name;
1280 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
1282 vlc_object_release( p_input_thread );
1283 return -1;
1285 int i_ret = var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1286 vlc_object_release( p_input_thread );
1287 free( psz_name );
1289 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1292 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1293 int i_title )
1295 input_thread_t *p_input_thread;
1297 p_input_thread = libvlc_get_input_thread ( p_mi );
1298 if( !p_input_thread )
1299 return;
1301 var_SetInteger( p_input_thread, "title", i_title );
1302 vlc_object_release( p_input_thread );
1304 //send event
1305 libvlc_event_t event;
1306 event.type = libvlc_MediaPlayerTitleChanged;
1307 event.u.media_player_title_changed.new_title = i_title;
1308 libvlc_event_send( p_mi->p_event_manager, &event );
1311 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1313 input_thread_t *p_input_thread;
1314 int i_title;
1316 p_input_thread = libvlc_get_input_thread ( p_mi );
1317 if( !p_input_thread )
1318 return -1;
1320 i_title = var_GetInteger( p_input_thread, "title" );
1321 vlc_object_release( p_input_thread );
1323 return i_title;
1326 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1328 input_thread_t *p_input_thread;
1329 vlc_value_t val;
1331 p_input_thread = libvlc_get_input_thread ( p_mi );
1332 if( !p_input_thread )
1333 return -1;
1335 int i_ret = var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1336 vlc_object_release( p_input_thread );
1338 return i_ret == VLC_SUCCESS ? val.i_int : -1;
1341 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1343 input_thread_t *p_input_thread;
1345 p_input_thread = libvlc_get_input_thread ( p_mi );
1346 if( !p_input_thread )
1347 return;
1349 int i_type = var_Type( p_input_thread, "next-chapter" );
1350 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1351 "next-chapter":"next-title" );
1353 vlc_object_release( p_input_thread );
1356 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1358 input_thread_t *p_input_thread;
1360 p_input_thread = libvlc_get_input_thread ( p_mi );
1361 if( !p_input_thread )
1362 return;
1364 int i_type = var_Type( p_input_thread, "next-chapter" );
1365 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1366 "prev-chapter":"prev-title" );
1368 vlc_object_release( p_input_thread );
1371 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1373 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1374 double f_fps = 0.0;
1376 if( p_input_thread )
1378 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1379 f_fps = 0.0;
1380 vlc_object_release( p_input_thread );
1382 return f_fps;
1385 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1387 bool b_will_play;
1388 input_thread_t *p_input_thread =
1389 libvlc_get_input_thread ( p_mi );
1390 if ( !p_input_thread )
1391 return false;
1393 b_will_play = !p_input_thread->b_dead;
1394 vlc_object_release( p_input_thread );
1396 return b_will_play;
1399 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1401 var_SetFloat (p_mi, "rate", rate);
1403 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1404 if( !p_input_thread )
1405 return 0;
1406 var_SetFloat( p_input_thread, "rate", rate );
1407 vlc_object_release( p_input_thread );
1408 return 0;
1411 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1413 return var_GetFloat (p_mi, "rate");
1416 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1418 lock(p_mi);
1419 libvlc_state_t state = p_mi->state;
1420 unlock(p_mi);
1421 return state;
1424 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1426 input_thread_t *p_input_thread;
1427 bool b_seekable;
1429 p_input_thread = libvlc_get_input_thread ( p_mi );
1430 if ( !p_input_thread )
1431 return false;
1432 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1433 vlc_object_release( p_input_thread );
1435 return b_seekable;
1438 void libvlc_media_player_navigate( libvlc_media_player_t* p_mi,
1439 unsigned navigate )
1441 static const vlc_action_t map[] =
1443 INPUT_NAV_ACTIVATE, INPUT_NAV_UP, INPUT_NAV_DOWN,
1444 INPUT_NAV_LEFT, INPUT_NAV_RIGHT,
1447 if( navigate >= sizeof(map) / sizeof(map[0]) )
1448 return;
1450 input_thread_t *p_input = libvlc_get_input_thread ( p_mi );
1451 if ( p_input == NULL )
1452 return;
1454 input_Control( p_input, map[navigate], NULL );
1455 vlc_object_release( p_input );
1458 /* internal function, used by audio, video */
1459 libvlc_track_description_t *
1460 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1461 const char *psz_variable )
1463 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1464 libvlc_track_description_t *p_track_description = NULL,
1465 *p_actual, *p_previous;
1467 if( !p_input )
1468 return NULL;
1470 vlc_value_t val_list, text_list;
1471 int i_ret = var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list );
1472 if( i_ret != VLC_SUCCESS )
1473 return NULL;
1475 /* no tracks */
1476 if( val_list.p_list->i_count <= 0 )
1477 goto end;
1479 p_track_description = ( libvlc_track_description_t * )
1480 malloc( sizeof( libvlc_track_description_t ) );
1481 if ( !p_track_description )
1483 libvlc_printerr( "Not enough memory" );
1484 goto end;
1486 p_actual = p_track_description;
1487 p_previous = NULL;
1488 for( int i = 0; i < val_list.p_list->i_count; i++ )
1490 if( !p_actual )
1492 p_actual = ( libvlc_track_description_t * )
1493 malloc( sizeof( libvlc_track_description_t ) );
1494 if ( !p_actual )
1496 libvlc_track_description_list_release( p_track_description );
1497 libvlc_printerr( "Not enough memory" );
1498 goto end;
1501 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1502 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1503 p_actual->p_next = NULL;
1504 if( p_previous )
1505 p_previous->p_next = p_actual;
1506 p_previous = p_actual;
1507 p_actual = NULL;
1510 end:
1511 var_FreeList( &val_list, &text_list );
1512 vlc_object_release( p_input );
1514 return p_track_description;
1517 // Deprecated alias for libvlc_track_description_list_release
1518 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1520 libvlc_track_description_list_release( p_td );
1523 void libvlc_track_description_list_release( libvlc_track_description_t *p_td )
1525 libvlc_track_description_t *p_actual, *p_before;
1526 p_actual = p_td;
1528 while ( p_actual )
1530 free( p_actual->psz_name );
1531 p_before = p_actual;
1532 p_actual = p_before->p_next;
1533 free( p_before );
1537 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1539 input_thread_t *p_input_thread;
1540 bool b_can_pause;
1542 p_input_thread = libvlc_get_input_thread ( p_mi );
1543 if ( !p_input_thread )
1544 return false;
1545 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1546 vlc_object_release( p_input_thread );
1548 return b_can_pause;
1551 int libvlc_media_player_program_scrambled( libvlc_media_player_t *p_mi )
1553 input_thread_t *p_input_thread;
1554 bool b_program_scrambled;
1556 p_input_thread = libvlc_get_input_thread ( p_mi );
1557 if ( !p_input_thread )
1558 return false;
1559 b_program_scrambled = var_GetBool( p_input_thread, "program-scrambled" );
1560 vlc_object_release( p_input_thread );
1562 return b_program_scrambled;
1565 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1567 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1568 if( p_input_thread != NULL )
1570 var_TriggerCallback( p_input_thread, "frame-next" );
1571 vlc_object_release( p_input_thread );
1576 * Private lookup table to get subpicture alignment flag values corresponding
1577 * to a libvlc_position_t enumerated value.
1579 static const unsigned char position_subpicture_alignment[] = {
1580 [libvlc_position_center] = 0,
1581 [libvlc_position_left] = SUBPICTURE_ALIGN_LEFT,
1582 [libvlc_position_right] = SUBPICTURE_ALIGN_RIGHT,
1583 [libvlc_position_top] = SUBPICTURE_ALIGN_TOP,
1584 [libvlc_position_top_left] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT,
1585 [libvlc_position_top_right] = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT,
1586 [libvlc_position_bottom] = SUBPICTURE_ALIGN_BOTTOM,
1587 [libvlc_position_bottom_left] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT,
1588 [libvlc_position_bottom_right] = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT
1591 void libvlc_media_player_set_video_title_display( libvlc_media_player_t *p_mi, libvlc_position_t position, unsigned timeout )
1593 assert( position >= libvlc_position_disable && position <= libvlc_position_bottom_right );
1595 if ( position != libvlc_position_disable )
1597 var_SetBool( p_mi, "video-title-show", true );
1598 var_SetInteger( p_mi, "video-title-position", position_subpicture_alignment[position] );
1599 var_SetInteger( p_mi, "video-title-timeout", timeout );
1601 else
1603 var_SetBool( p_mi, "video-title-show", false );
1608 * Maximum size of a formatted equalizer amplification band frequency value.
1610 * The allowed value range is supposed to be constrained from -20.0 to 20.0.
1612 * The format string " %.07f" with a minimum value of "-20" gives a maximum
1613 * string length of e.g. " -19.1234567", i.e. 12 bytes (not including the null
1614 * terminator).
1616 #define EQZ_BAND_VALUE_SIZE 12
1618 int libvlc_media_player_set_equalizer( libvlc_media_player_t *p_mi, libvlc_equalizer_t *p_equalizer )
1620 char bands[EQZ_BANDS_MAX * EQZ_BAND_VALUE_SIZE + 1];
1622 if( p_equalizer != NULL )
1624 for( unsigned i = 0, c = 0; i < EQZ_BANDS_MAX; i++ )
1626 c += snprintf( bands + c, sizeof(bands) - c, " %.07f",
1627 p_equalizer->f_amp[i] );
1628 if( unlikely(c >= sizeof(bands)) )
1629 return -1;
1632 var_SetFloat( p_mi, "equalizer-preamp", p_equalizer->f_preamp );
1633 var_SetString( p_mi, "equalizer-bands", bands );
1635 var_SetString( p_mi, "audio-filter", p_equalizer ? "equalizer" : "" );
1637 audio_output_t *p_aout = input_resource_HoldAout( p_mi->input.p_resource );
1638 if( p_aout != NULL )
1640 if( p_equalizer != NULL )
1642 var_SetFloat( p_aout, "equalizer-preamp", p_equalizer->f_preamp );
1643 var_SetString( p_aout, "equalizer-bands", bands );
1646 var_SetString( p_aout, "audio-filter", p_equalizer ? "equalizer" : "" );
1647 vlc_object_release( p_aout );
1650 return 0;