Correct spelling: s/[sS]yncro/[sS]ynchro/
[vlc.git] / lib / media_player.c
blob936640038917fe6ef982ebe648494856905b81dc
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_keys.h>
38 #include "libvlc_internal.h"
39 #include "media_internal.h" // libvlc_media_set_state()
40 #include "media_player_internal.h"
42 static int
43 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
44 vlc_value_t oldval, vlc_value_t newval,
45 void * p_userdata );
46 static int
47 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
48 vlc_value_t oldval, vlc_value_t newval,
49 void * p_userdata );
50 static int
51 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
52 vlc_value_t oldval, vlc_value_t newval,
53 void * p_userdata );
55 static int
56 snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
57 vlc_value_t oldval, vlc_value_t newval, void *p_data );
59 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
62 * Shortcuts
65 #define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
66 static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type_t type)
68 libvlc_event_manager_register_event_type(mp->p_event_manager, type);
72 * The input lock protects the input and input resource pointer.
73 * It MUST NOT be used from callbacks.
75 * The object lock protects the reset, namely the media and the player state.
76 * It can, and usually needs to be taken from callbacks.
77 * The object lock can be acquired under the input lock... and consequently
78 * the opposite order is STRICTLY PROHIBITED.
80 static inline void lock(libvlc_media_player_t *mp)
82 vlc_mutex_lock(&mp->object_lock);
85 static inline void unlock(libvlc_media_player_t *mp)
87 vlc_mutex_unlock(&mp->object_lock);
90 static inline void lock_input(libvlc_media_player_t *mp)
92 vlc_mutex_lock(&mp->input.lock);
95 static inline void unlock_input(libvlc_media_player_t *mp)
97 vlc_mutex_unlock(&mp->input.lock);
101 * Release the associated input thread.
103 * Object lock is NOT held.
104 * Input lock is held or instance is being destroyed.
106 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
108 assert( p_mi );
110 input_thread_t *p_input_thread = p_mi->input.p_thread;
111 if( !p_input_thread )
112 return;
113 p_mi->input.p_thread = NULL;
115 var_DelCallback( p_input_thread, "can-seek",
116 input_seekable_changed, p_mi );
117 var_DelCallback( p_input_thread, "can-pause",
118 input_pausable_changed, p_mi );
119 var_DelCallback( p_input_thread, "intf-event",
120 input_event_changed, p_mi );
122 /* We owned this one */
123 input_Stop( p_input_thread, b_input_abort );
124 input_Close( p_input_thread );
128 * Retrieve the input thread. Be sure to release the object
129 * once you are done with it. (libvlc Internal)
131 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
133 input_thread_t *p_input_thread;
135 assert( p_mi );
137 lock_input(p_mi);
138 p_input_thread = p_mi->input.p_thread;
139 if( p_input_thread )
140 vlc_object_hold( p_input_thread );
141 else
142 libvlc_printerr( "No active input" );
143 unlock_input(p_mi);
145 return p_input_thread;
149 * Set the internal state of the media_player. (media player Internal)
151 * Function will lock the media_player.
153 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
154 bool b_locked )
156 if(!b_locked)
157 lock(p_mi);
158 p_mi->state = state;
160 libvlc_media_t *media = p_mi->p_md;
161 if (media)
162 libvlc_media_retain(media);
164 if(!b_locked)
165 unlock(p_mi);
167 if (media)
169 // Also set the state of the corresponding media
170 // This is strictly for convenience.
171 libvlc_media_set_state(media, state);
173 libvlc_media_release(media);
177 static int
178 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
179 vlc_value_t oldval, vlc_value_t newval,
180 void * p_userdata )
182 VLC_UNUSED(oldval);
183 VLC_UNUSED(p_this);
184 VLC_UNUSED(psz_cmd);
185 libvlc_media_player_t * p_mi = p_userdata;
186 libvlc_event_t event;
188 event.type = libvlc_MediaPlayerSeekableChanged;
189 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
191 libvlc_event_send( p_mi->p_event_manager, &event );
192 return VLC_SUCCESS;
195 static int
196 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
197 vlc_value_t oldval, vlc_value_t newval,
198 void * p_userdata )
200 VLC_UNUSED(oldval);
201 VLC_UNUSED(p_this);
202 VLC_UNUSED(psz_cmd);
203 libvlc_media_player_t * p_mi = p_userdata;
204 libvlc_event_t event;
206 event.type = libvlc_MediaPlayerPausableChanged;
207 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
209 libvlc_event_send( p_mi->p_event_manager, &event );
210 return VLC_SUCCESS;
213 static int
214 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
215 vlc_value_t oldval, vlc_value_t newval,
216 void * p_userdata )
218 VLC_UNUSED(oldval);
219 input_thread_t * p_input = (input_thread_t *)p_this;
220 libvlc_media_player_t * p_mi = p_userdata;
221 libvlc_event_t event;
223 assert( !strcmp( psz_cmd, "intf-event" ) );
225 if( newval.i_int == INPUT_EVENT_STATE )
227 libvlc_state_t libvlc_state;
229 switch ( var_GetInteger( p_input, "state" ) )
231 case INIT_S:
232 libvlc_state = libvlc_NothingSpecial;
233 event.type = libvlc_MediaPlayerNothingSpecial;
234 break;
235 case OPENING_S:
236 libvlc_state = libvlc_Opening;
237 event.type = libvlc_MediaPlayerOpening;
238 break;
239 case PLAYING_S:
240 libvlc_state = libvlc_Playing;
241 event.type = libvlc_MediaPlayerPlaying;
242 break;
243 case PAUSE_S:
244 libvlc_state = libvlc_Paused;
245 event.type = libvlc_MediaPlayerPaused;
246 break;
247 case END_S:
248 libvlc_state = libvlc_Ended;
249 event.type = libvlc_MediaPlayerEndReached;
250 break;
251 case ERROR_S:
252 libvlc_state = libvlc_Error;
253 event.type = libvlc_MediaPlayerEncounteredError;
254 break;
256 default:
257 return VLC_SUCCESS;
260 set_state( p_mi, libvlc_state, false );
261 libvlc_event_send( p_mi->p_event_manager, &event );
263 else if( newval.i_int == INPUT_EVENT_ABORT )
265 libvlc_state_t libvlc_state = libvlc_Stopped;
266 event.type = libvlc_MediaPlayerStopped;
268 set_state( p_mi, libvlc_state, false );
269 libvlc_event_send( p_mi->p_event_manager, &event );
271 else if( newval.i_int == INPUT_EVENT_POSITION )
273 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
274 return VLC_SUCCESS; /* Don't send the position while stopped */
276 /* */
277 event.type = libvlc_MediaPlayerPositionChanged;
278 event.u.media_player_position_changed.new_position =
279 var_GetFloat( p_input, "position" );
280 libvlc_event_send( p_mi->p_event_manager, &event );
282 /* */
283 event.type = libvlc_MediaPlayerTimeChanged;
284 event.u.media_player_time_changed.new_time =
285 from_mtime(var_GetTime( p_input, "time" ));
286 libvlc_event_send( p_mi->p_event_manager, &event );
288 else if( newval.i_int == INPUT_EVENT_LENGTH )
290 event.type = libvlc_MediaPlayerLengthChanged;
291 event.u.media_player_length_changed.new_length =
292 from_mtime(var_GetTime( p_input, "length" ));
293 libvlc_event_send( p_mi->p_event_manager, &event );
295 else if( newval.i_int == INPUT_EVENT_CACHE )
297 event.type = libvlc_MediaPlayerBuffering;
298 event.u.media_player_buffering.new_cache = (int)(100 *
299 var_GetFloat( p_input, "cache" ));
300 libvlc_event_send( p_mi->p_event_manager, &event );
302 else if( newval.i_int == INPUT_EVENT_VOUT )
304 vout_thread_t **pp_vout;
305 size_t i_vout;
306 if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
308 i_vout = 0;
310 else
312 for( size_t i = 0; i < i_vout; i++ )
313 vlc_object_release( pp_vout[i] );
314 free( pp_vout );
317 event.type = libvlc_MediaPlayerVout;
318 event.u.media_player_vout.new_count = i_vout;
319 libvlc_event_send( p_mi->p_event_manager, &event );
322 return VLC_SUCCESS;
325 /**************************************************************************
326 * Snapshot Taken Event.
328 * FIXME: This snapshot API interface makes no sense in media_player.
329 *************************************************************************/
330 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
331 vlc_value_t oldval, vlc_value_t newval, void *p_data )
333 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
335 libvlc_media_player_t *mp = p_data;
336 libvlc_event_t event;
337 event.type = libvlc_MediaPlayerSnapshotTaken;
338 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
339 libvlc_event_send(mp->p_event_manager, &event);
341 return VLC_SUCCESS;
344 /* */
345 static void libvlc_media_player_destroy( libvlc_media_player_t * );
348 /**************************************************************************
349 * Create a Media Instance object.
351 * Refcount strategy:
352 * - All items created by _new start with a refcount set to 1.
353 * - Accessor _release decrease the refcount by 1, if after that
354 * operation the refcount is 0, the object is destroyed.
355 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
357 * Object locking strategy:
358 * - No lock held while in constructor.
359 * - When accessing any member variable this lock is held. (XXX who locks?)
360 * - When attempting to destroy the object the lock is also held.
361 **************************************************************************/
362 libvlc_media_player_t *
363 libvlc_media_player_new( libvlc_instance_t *instance )
365 libvlc_media_player_t * mp;
367 assert(instance);
369 mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
370 if (unlikely(mp == NULL))
372 libvlc_printerr("Not enough memory");
373 return NULL;
376 /* Input */
377 var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
379 /* Video */
380 var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
381 var_Create (mp, "window", VLC_VAR_STRING);
382 var_Create (mp, "vmem-lock", VLC_VAR_ADDRESS);
383 var_Create (mp, "vmem-unlock", VLC_VAR_ADDRESS);
384 var_Create (mp, "vmem-display", VLC_VAR_ADDRESS);
385 var_Create (mp, "vmem-data", VLC_VAR_ADDRESS);
386 var_Create (mp, "vmem-setup", VLC_VAR_ADDRESS);
387 var_Create (mp, "vmem-cleanup", VLC_VAR_ADDRESS);
388 var_Create (mp, "vmem-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
389 var_Create (mp, "vmem-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
390 var_Create (mp, "vmem-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
391 var_Create (mp, "vmem-pitch", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
392 var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
393 #if defined (WIN32) || defined (__OS2__)
394 var_Create (mp, "drawable-hwnd", VLC_VAR_INTEGER);
395 #endif
396 #ifdef __APPLE__
397 var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
398 var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
399 #endif
401 var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
402 var_SetBool (mp, "keyboard-events", true);
403 var_Create (mp, "mouse-events", VLC_VAR_BOOL);
404 var_SetBool (mp, "mouse-events", true);
406 var_Create (mp, "fullscreen", VLC_VAR_BOOL);
407 var_Create (mp, "autoscale", VLC_VAR_BOOL);
408 var_SetBool (mp, "autoscale", true);
409 var_Create (mp, "scale", VLC_VAR_FLOAT);
410 var_SetFloat (mp, "scale", 1.);
411 var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
412 var_Create (mp, "crop", VLC_VAR_STRING);
413 var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
414 var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
416 var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
418 var_Create (mp, "marq-marquee", VLC_VAR_STRING);
419 var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
420 var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
421 var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
422 var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
423 var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
424 var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
425 var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
426 var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
428 var_Create (mp, "logo-file", VLC_VAR_STRING);
429 var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
430 var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
431 var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
432 var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
433 var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
434 var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
436 var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
437 var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
438 var_Create (mp, "hue", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
439 var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
440 var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
442 /* Audio */
443 var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
444 var_Create (mp, "mute", VLC_VAR_BOOL);
445 var_Create (mp, "volume", VLC_VAR_FLOAT);
446 var_Create (mp, "corks", VLC_VAR_INTEGER);
447 var_Create (mp, "amem-data", VLC_VAR_ADDRESS);
448 var_Create (mp, "amem-setup", VLC_VAR_ADDRESS);
449 var_Create (mp, "amem-cleanup", VLC_VAR_ADDRESS);
450 var_Create (mp, "amem-play", VLC_VAR_ADDRESS);
451 var_Create (mp, "amem-pause", VLC_VAR_ADDRESS);
452 var_Create (mp, "amem-resume", VLC_VAR_ADDRESS);
453 var_Create (mp, "amem-flush", VLC_VAR_ADDRESS);
454 var_Create (mp, "amem-drain", VLC_VAR_ADDRESS);
455 var_Create (mp, "amem-set-volume", VLC_VAR_ADDRESS);
456 var_Create (mp, "amem-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
457 var_Create (mp, "amem-rate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
458 var_Create (mp, "amem-channels", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
460 mp->p_md = NULL;
461 mp->state = libvlc_NothingSpecial;
462 mp->p_libvlc_instance = instance;
463 mp->input.p_thread = NULL;
464 mp->input.p_resource = input_resource_New(VLC_OBJECT(mp));
465 if (unlikely(mp->input.p_resource == NULL))
467 vlc_object_release(mp);
468 return NULL;
470 vlc_mutex_init (&mp->input.lock);
471 mp->i_refcount = 1;
472 mp->p_event_manager = libvlc_event_manager_new(mp, instance);
473 if (unlikely(mp->p_event_manager == NULL))
475 input_resource_Release(mp->input.p_resource);
476 vlc_object_release(mp);
477 return NULL;
479 vlc_mutex_init(&mp->object_lock);
481 register_event(mp, NothingSpecial);
482 register_event(mp, Opening);
483 register_event(mp, Buffering);
484 register_event(mp, Playing);
485 register_event(mp, Paused);
486 register_event(mp, Stopped);
487 register_event(mp, Forward);
488 register_event(mp, Backward);
489 register_event(mp, EndReached);
490 register_event(mp, EncounteredError);
491 register_event(mp, SeekableChanged);
493 register_event(mp, PositionChanged);
494 register_event(mp, TimeChanged);
495 register_event(mp, LengthChanged);
496 register_event(mp, TitleChanged);
497 register_event(mp, PausableChanged);
499 register_event(mp, Vout);
501 /* Snapshot initialization */
502 register_event(mp, SnapshotTaken);
504 register_event(mp, MediaChanged);
506 /* Attach a var callback to the global object to provide the glue between
507 * vout_thread that generates the event and media_player that re-emits it
508 * with its own event manager
510 * FIXME: It's unclear why we want to put this in public API, and why we
511 * want to expose it in such a limiting and ugly way.
513 var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
515 libvlc_retain(instance);
516 return mp;
519 /**************************************************************************
520 * Create a Media Instance object with a media descriptor.
521 **************************************************************************/
522 libvlc_media_player_t *
523 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
525 libvlc_media_player_t * p_mi;
527 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
528 if( !p_mi )
529 return NULL;
531 libvlc_media_retain( p_md );
532 p_mi->p_md = p_md;
534 return p_mi;
537 /**************************************************************************
538 * Destroy a Media Instance object (libvlc internal)
540 * Warning: No lock held here, but hey, this is internal. Caller must lock.
541 **************************************************************************/
542 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
544 assert( p_mi );
546 /* Detach Callback from the main libvlc object */
547 var_DelCallback( p_mi->p_libvlc,
548 "snapshot-file", snapshot_was_taken, p_mi );
550 /* No need for lock_input() because no other threads knows us anymore */
551 if( p_mi->input.p_thread )
552 release_input_thread(p_mi, true);
553 input_resource_Terminate( p_mi->input.p_resource );
554 input_resource_Release( p_mi->input.p_resource );
555 vlc_mutex_destroy( &p_mi->input.lock );
557 libvlc_event_manager_release( p_mi->p_event_manager );
558 libvlc_media_release( p_mi->p_md );
559 vlc_mutex_destroy( &p_mi->object_lock );
561 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
562 vlc_object_release( p_mi );
563 libvlc_release(instance);
566 /**************************************************************************
567 * Release a Media Instance object.
569 * Function does the locking.
570 **************************************************************************/
571 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
573 bool destroy;
575 assert( p_mi );
576 lock(p_mi);
577 destroy = !--p_mi->i_refcount;
578 unlock(p_mi);
580 if( destroy )
581 libvlc_media_player_destroy( p_mi );
584 /**************************************************************************
585 * Retain a Media Instance object.
587 * Caller must hold the lock.
588 **************************************************************************/
589 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
591 assert( p_mi );
593 lock(p_mi);
594 p_mi->i_refcount++;
595 unlock(p_mi);
598 /**************************************************************************
599 * Set the Media descriptor associated with the instance.
601 * Enter without lock -- function will lock the object.
602 **************************************************************************/
603 void libvlc_media_player_set_media(
604 libvlc_media_player_t *p_mi,
605 libvlc_media_t *p_md )
607 lock_input(p_mi);
609 /* FIXME I am not sure if it is a user request or on die(eof/error)
610 * request here */
611 release_input_thread( p_mi,
612 p_mi->input.p_thread &&
613 !p_mi->input.p_thread->b_eof &&
614 !p_mi->input.p_thread->b_error );
616 lock( p_mi );
617 set_state( p_mi, libvlc_NothingSpecial, true );
618 unlock_input( p_mi );
620 libvlc_media_release( p_mi->p_md );
622 if( !p_md )
624 p_mi->p_md = NULL;
625 unlock(p_mi);
626 return; /* It is ok to pass a NULL md */
629 libvlc_media_retain( p_md );
630 p_mi->p_md = p_md;
632 /* The policy here is to ignore that we were created using a different
633 * libvlc_instance, because we don't really care */
634 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
636 unlock(p_mi);
638 /* Send an event for the newly available media */
639 libvlc_event_t event;
640 event.type = libvlc_MediaPlayerMediaChanged;
641 event.u.media_player_media_changed.new_media = p_md;
642 libvlc_event_send( p_mi->p_event_manager, &event );
646 /**************************************************************************
647 * Get the Media descriptor associated with the instance.
648 **************************************************************************/
649 libvlc_media_t *
650 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
652 libvlc_media_t *p_m;
654 lock(p_mi);
655 p_m = p_mi->p_md;
656 if( p_m )
657 libvlc_media_retain( p_mi->p_md );
658 unlock(p_mi);
659 return p_mi->p_md;
662 /**************************************************************************
663 * Get the event Manager.
664 **************************************************************************/
665 libvlc_event_manager_t *
666 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
668 return p_mi->p_event_manager;
671 /**************************************************************************
672 * Tell media player to start playing.
673 **************************************************************************/
674 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
676 lock_input( p_mi );
678 input_thread_t *p_input_thread = p_mi->input.p_thread;
679 if( p_input_thread )
681 /* A thread already exists, send it a play message */
682 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
683 unlock_input( p_mi );
684 return 0;
687 /* Ignore previous exception */
688 lock(p_mi);
690 if( !p_mi->p_md )
692 unlock(p_mi);
693 unlock_input( p_mi );
694 libvlc_printerr( "No associated media descriptor" );
695 return -1;
698 p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
699 p_mi->input.p_resource );
700 unlock(p_mi);
701 if( !p_input_thread )
703 unlock_input(p_mi);
704 libvlc_printerr( "Not enough memory" );
705 return -1;
708 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
709 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
710 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
712 if( input_Start( p_input_thread ) )
714 unlock_input(p_mi);
715 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
716 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
717 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
718 vlc_object_release( p_input_thread );
719 libvlc_printerr( "Input initialization failure" );
720 return -1;
722 p_mi->input.p_thread = p_input_thread;
723 unlock_input(p_mi);
724 return 0;
727 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
729 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
730 if( !p_input_thread )
731 return;
733 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
734 if( state == libvlc_Playing || state == libvlc_Buffering )
736 if( paused )
738 if( libvlc_media_player_can_pause( p_mi ) )
739 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
740 else
741 libvlc_media_player_stop( p_mi );
744 else
746 if( !paused )
747 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
750 vlc_object_release( p_input_thread );
753 /**************************************************************************
754 * Toggle pause.
755 **************************************************************************/
756 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
758 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
759 bool playing = (state == libvlc_Playing || state == libvlc_Buffering);
761 libvlc_media_player_set_pause( p_mi, playing );
764 /**************************************************************************
765 * Tells whether the media player is currently playing.
767 * Enter with lock held.
768 **************************************************************************/
769 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
771 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
772 return (libvlc_Playing == state) || (libvlc_Buffering == state);
775 /**************************************************************************
776 * Stop playing.
777 **************************************************************************/
778 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
780 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
782 lock_input(p_mi);
783 release_input_thread( p_mi, true ); /* This will stop the input thread */
785 /* Force to go to stopped state, in case we were in Ended, or Error
786 * state. */
787 if( state != libvlc_Stopped )
789 set_state( p_mi, libvlc_Stopped, false );
791 /* Construct and send the event */
792 libvlc_event_t event;
793 event.type = libvlc_MediaPlayerStopped;
794 libvlc_event_send( p_mi->p_event_manager, &event );
797 input_resource_Terminate( p_mi->input.p_resource );
798 unlock_input(p_mi);
802 void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
803 void *(*lock_cb) (void *, void **),
804 void (*unlock_cb) (void *, void *, void *const *),
805 void (*display_cb) (void *, void *),
806 void *opaque )
808 var_SetAddress( mp, "vmem-lock", lock_cb );
809 var_SetAddress( mp, "vmem-unlock", unlock_cb );
810 var_SetAddress( mp, "vmem-display", display_cb );
811 var_SetAddress( mp, "vmem-data", opaque );
812 var_SetString( mp, "vout", "vmem" );
815 void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
816 libvlc_video_format_cb setup,
817 libvlc_video_cleanup_cb cleanup )
819 var_SetAddress( mp, "vmem-setup", setup );
820 var_SetAddress( mp, "vmem-cleanup", cleanup );
823 void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
824 unsigned width, unsigned height, unsigned pitch )
826 var_SetString( mp, "vmem-chroma", chroma );
827 var_SetInteger( mp, "vmem-width", width );
828 var_SetInteger( mp, "vmem-height", height );
829 var_SetInteger( mp, "vmem-pitch", pitch );
832 /**************************************************************************
833 * set_nsobject
834 **************************************************************************/
835 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
836 void * drawable )
838 assert (p_mi != NULL);
839 #ifdef __APPLE__
840 var_SetAddress (p_mi, "drawable-nsobject", drawable);
841 #else
842 (void) p_mi; (void)drawable;
843 #endif
846 /**************************************************************************
847 * get_nsobject
848 **************************************************************************/
849 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
851 assert (p_mi != NULL);
852 #ifdef __APPLE__
853 return var_GetAddress (p_mi, "drawable-nsobject");
854 #else
855 return NULL;
856 #endif
859 /**************************************************************************
860 * set_agl
861 **************************************************************************/
862 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
863 uint32_t drawable )
865 #ifdef __APPLE__
866 var_SetInteger (p_mi, "drawable-agl", drawable);
867 #else
868 (void) p_mi; (void)drawable;
869 #endif
872 /**************************************************************************
873 * get_agl
874 **************************************************************************/
875 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
877 assert (p_mi != NULL);
878 #ifdef __APPLE__
879 return var_GetInteger (p_mi, "drawable-agl");
880 #else
881 return 0;
882 #endif
885 /**************************************************************************
886 * set_xwindow
887 **************************************************************************/
888 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
889 uint32_t drawable )
891 assert (p_mi != NULL);
893 var_SetString (p_mi, "vout", drawable ? "xid" : "any");
894 var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "any");
895 var_SetInteger (p_mi, "drawable-xid", drawable);
898 /**************************************************************************
899 * get_xwindow
900 **************************************************************************/
901 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
903 return var_GetInteger (p_mi, "drawable-xid");
906 /**************************************************************************
907 * set_hwnd
908 **************************************************************************/
909 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
910 void *drawable )
912 assert (p_mi != NULL);
913 #if defined (WIN32) || defined (__OS2__)
914 var_SetString (p_mi, "window",
915 (drawable != NULL) ? "embed-hwnd,any" : "");
916 var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
917 #else
918 (void) p_mi; (void) drawable;
919 #endif
922 /**************************************************************************
923 * get_hwnd
924 **************************************************************************/
925 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
927 assert (p_mi != NULL);
928 #if defined (WIN32) || defined (__OS2__)
929 return (void *)(uintptr_t)var_GetInteger (p_mi, "drawable-hwnd");
930 #else
931 return NULL;
932 #endif
935 void libvlc_audio_set_callbacks( libvlc_media_player_t *mp,
936 libvlc_audio_play_cb play_cb,
937 libvlc_audio_pause_cb pause_cb,
938 libvlc_audio_resume_cb resume_cb,
939 libvlc_audio_flush_cb flush_cb,
940 libvlc_audio_drain_cb drain_cb,
941 void *opaque )
943 var_SetAddress( mp, "amem-play", play_cb );
944 var_SetAddress( mp, "amem-pause", pause_cb );
945 var_SetAddress( mp, "amem-resume", resume_cb );
946 var_SetAddress( mp, "amem-flush", flush_cb );
947 var_SetAddress( mp, "amem-drain", drain_cb );
948 var_SetAddress( mp, "amem-data", opaque );
949 var_SetString( mp, "aout", "amem,none" );
952 void libvlc_audio_set_volume_callback( libvlc_media_player_t *mp,
953 libvlc_audio_set_volume_cb cb )
955 var_SetAddress( mp, "amem-set-volume", cb );
958 void libvlc_audio_set_format_callbacks( libvlc_media_player_t *mp,
959 libvlc_audio_setup_cb setup,
960 libvlc_audio_cleanup_cb cleanup )
962 var_SetAddress( mp, "amem-setup", setup );
963 var_SetAddress( mp, "amem-cleanup", cleanup );
966 void libvlc_audio_set_format( libvlc_media_player_t *mp, const char *format,
967 unsigned rate, unsigned channels )
969 var_SetString( mp, "amem-format", format );
970 var_SetInteger( mp, "amem-rate", rate );
971 var_SetInteger( mp, "amem-channels", channels );
975 /**************************************************************************
976 * Getters for stream information
977 **************************************************************************/
978 libvlc_time_t libvlc_media_player_get_length(
979 libvlc_media_player_t *p_mi )
981 input_thread_t *p_input_thread;
982 libvlc_time_t i_time;
984 p_input_thread = libvlc_get_input_thread ( p_mi );
985 if( !p_input_thread )
986 return -1;
988 i_time = from_mtime(var_GetTime( p_input_thread, "length" ));
989 vlc_object_release( p_input_thread );
991 return i_time;
994 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
996 input_thread_t *p_input_thread;
997 libvlc_time_t i_time;
999 p_input_thread = libvlc_get_input_thread ( p_mi );
1000 if( !p_input_thread )
1001 return -1;
1003 i_time = from_mtime(var_GetTime( p_input_thread , "time" ));
1004 vlc_object_release( p_input_thread );
1005 return i_time;
1008 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
1009 libvlc_time_t i_time )
1011 input_thread_t *p_input_thread;
1013 p_input_thread = libvlc_get_input_thread ( p_mi );
1014 if( !p_input_thread )
1015 return;
1017 var_SetTime( p_input_thread, "time", to_mtime(i_time) );
1018 vlc_object_release( p_input_thread );
1021 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
1022 float position )
1024 input_thread_t *p_input_thread;
1026 p_input_thread = libvlc_get_input_thread ( p_mi );
1027 if( !p_input_thread )
1028 return;
1030 var_SetFloat( p_input_thread, "position", position );
1031 vlc_object_release( p_input_thread );
1034 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
1036 input_thread_t *p_input_thread;
1037 float f_position;
1039 p_input_thread = libvlc_get_input_thread ( p_mi );
1040 if( !p_input_thread )
1041 return -1.0;
1043 f_position = var_GetFloat( p_input_thread, "position" );
1044 vlc_object_release( p_input_thread );
1046 return f_position;
1049 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
1050 int chapter )
1052 input_thread_t *p_input_thread;
1054 p_input_thread = libvlc_get_input_thread ( p_mi );
1055 if( !p_input_thread )
1056 return;
1058 var_SetInteger( p_input_thread, "chapter", chapter );
1059 vlc_object_release( p_input_thread );
1062 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
1064 input_thread_t *p_input_thread;
1065 int i_chapter;
1067 p_input_thread = libvlc_get_input_thread ( p_mi );
1068 if( !p_input_thread )
1069 return -1;
1071 i_chapter = var_GetInteger( p_input_thread, "chapter" );
1072 vlc_object_release( p_input_thread );
1074 return i_chapter;
1077 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1079 input_thread_t *p_input_thread;
1080 vlc_value_t val;
1082 p_input_thread = libvlc_get_input_thread ( p_mi );
1083 if( !p_input_thread )
1084 return -1;
1086 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
1087 vlc_object_release( p_input_thread );
1089 return val.i_int;
1092 int libvlc_media_player_get_chapter_count_for_title(
1093 libvlc_media_player_t *p_mi,
1094 int i_title )
1096 input_thread_t *p_input_thread;
1097 vlc_value_t val;
1099 p_input_thread = libvlc_get_input_thread ( p_mi );
1100 if( !p_input_thread )
1101 return -1;
1103 char *psz_name;
1104 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
1106 vlc_object_release( p_input_thread );
1107 return -1;
1109 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1110 vlc_object_release( p_input_thread );
1111 free( psz_name );
1113 return val.i_int;
1116 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1117 int i_title )
1119 input_thread_t *p_input_thread;
1121 p_input_thread = libvlc_get_input_thread ( p_mi );
1122 if( !p_input_thread )
1123 return;
1125 var_SetInteger( p_input_thread, "title", i_title );
1126 vlc_object_release( p_input_thread );
1128 //send event
1129 libvlc_event_t event;
1130 event.type = libvlc_MediaPlayerTitleChanged;
1131 event.u.media_player_title_changed.new_title = i_title;
1132 libvlc_event_send( p_mi->p_event_manager, &event );
1135 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1137 input_thread_t *p_input_thread;
1138 int i_title;
1140 p_input_thread = libvlc_get_input_thread ( p_mi );
1141 if( !p_input_thread )
1142 return -1;
1144 i_title = var_GetInteger( p_input_thread, "title" );
1145 vlc_object_release( p_input_thread );
1147 return i_title;
1150 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1152 input_thread_t *p_input_thread;
1153 vlc_value_t val;
1155 p_input_thread = libvlc_get_input_thread ( p_mi );
1156 if( !p_input_thread )
1157 return -1;
1159 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1160 vlc_object_release( p_input_thread );
1162 return val.i_int;
1165 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1167 input_thread_t *p_input_thread;
1169 p_input_thread = libvlc_get_input_thread ( p_mi );
1170 if( !p_input_thread )
1171 return;
1173 int i_type = var_Type( p_input_thread, "next-chapter" );
1174 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1175 "next-chapter":"next-title" );
1177 vlc_object_release( p_input_thread );
1180 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1182 input_thread_t *p_input_thread;
1184 p_input_thread = libvlc_get_input_thread ( p_mi );
1185 if( !p_input_thread )
1186 return;
1188 int i_type = var_Type( p_input_thread, "next-chapter" );
1189 var_TriggerCallback( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1190 "prev-chapter":"prev-title" );
1192 vlc_object_release( p_input_thread );
1195 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1197 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1198 double f_fps = 0.0;
1200 if( p_input_thread )
1202 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1203 f_fps = 0.0;
1204 vlc_object_release( p_input_thread );
1206 return f_fps;
1209 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1211 bool b_will_play;
1212 input_thread_t *p_input_thread =
1213 libvlc_get_input_thread ( p_mi );
1214 if ( !p_input_thread )
1215 return false;
1217 b_will_play = !p_input_thread->b_dead;
1218 vlc_object_release( p_input_thread );
1220 return b_will_play;
1223 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1225 if (rate < 0.)
1227 libvlc_printerr ("Playing backward not supported");
1228 return -1;
1231 var_SetFloat (p_mi, "rate", rate);
1233 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1234 if( !p_input_thread )
1235 return 0;
1236 var_SetFloat( p_input_thread, "rate", rate );
1237 vlc_object_release( p_input_thread );
1238 return 0;
1241 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1243 return var_GetFloat (p_mi, "rate");
1246 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1248 lock(p_mi);
1249 libvlc_state_t state = p_mi->state;
1250 unlock(p_mi);
1251 return state;
1254 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1256 input_thread_t *p_input_thread;
1257 bool b_seekable;
1259 p_input_thread = libvlc_get_input_thread ( p_mi );
1260 if ( !p_input_thread )
1261 return false;
1262 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1263 vlc_object_release( p_input_thread );
1265 return b_seekable;
1268 void libvlc_media_player_navigate( libvlc_media_player_t* p_mi,
1269 unsigned navigate )
1271 static const vlc_action_t map[] =
1273 INPUT_NAV_ACTIVATE, INPUT_NAV_UP, INPUT_NAV_DOWN,
1274 INPUT_NAV_LEFT, INPUT_NAV_RIGHT,
1277 if( navigate >= sizeof(map) / sizeof(map[0]) )
1278 return;
1280 input_thread_t *p_input = libvlc_get_input_thread ( p_mi );
1281 if ( p_input == NULL )
1282 return;
1284 input_Control( p_input, map[navigate], NULL );
1285 vlc_object_release( p_input );
1288 /* internal function, used by audio, video */
1289 libvlc_track_description_t *
1290 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1291 const char *psz_variable )
1293 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1294 libvlc_track_description_t *p_track_description = NULL,
1295 *p_actual, *p_previous;
1297 if( !p_input )
1298 return NULL;
1300 vlc_value_t val_list, text_list;
1301 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1303 /* no tracks */
1304 if( val_list.p_list->i_count <= 0 )
1305 goto end;
1307 p_track_description = ( libvlc_track_description_t * )
1308 malloc( sizeof( libvlc_track_description_t ) );
1309 if ( !p_track_description )
1311 libvlc_printerr( "Not enough memory" );
1312 goto end;
1314 p_actual = p_track_description;
1315 p_previous = NULL;
1316 for( int i = 0; i < val_list.p_list->i_count; i++ )
1318 if( !p_actual )
1320 p_actual = ( libvlc_track_description_t * )
1321 malloc( sizeof( libvlc_track_description_t ) );
1322 if ( !p_actual )
1324 libvlc_track_description_list_release( p_track_description );
1325 libvlc_printerr( "Not enough memory" );
1326 goto end;
1329 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1330 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1331 p_actual->p_next = NULL;
1332 if( p_previous )
1333 p_previous->p_next = p_actual;
1334 p_previous = p_actual;
1335 p_actual = NULL;
1338 end:
1339 var_FreeList( &val_list, &text_list );
1340 vlc_object_release( p_input );
1342 return p_track_description;
1345 // Deprecated alias for libvlc_track_description_list_release
1346 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1348 libvlc_track_description_list_release( p_td );
1351 void libvlc_track_description_list_release( libvlc_track_description_t *p_td )
1353 libvlc_track_description_t *p_actual, *p_before;
1354 p_actual = p_td;
1356 while ( p_actual )
1358 free( p_actual->psz_name );
1359 p_before = p_actual;
1360 p_actual = p_before->p_next;
1361 free( p_before );
1365 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1367 input_thread_t *p_input_thread;
1368 bool b_can_pause;
1370 p_input_thread = libvlc_get_input_thread ( p_mi );
1371 if ( !p_input_thread )
1372 return false;
1373 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1374 vlc_object_release( p_input_thread );
1376 return b_can_pause;
1379 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1381 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1382 if( p_input_thread != NULL )
1384 var_TriggerCallback( p_input_thread, "frame-next" );
1385 vlc_object_release( p_input_thread );