Add LibVLC API for vmem
[vlc/asuraparaju-public.git] / src / control / media_player.c
blobcf54fcd90541816804af116ba1e7de18a01ca5a8
1 /*****************************************************************************
2 * media_player.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005-2009 the VideoLAN team
5 * $Id$
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_events.h>
34 #include <vlc_demux.h>
35 #include <vlc_input.h>
36 #include <vlc_vout.h>
38 #include "libvlc.h"
40 #include "libvlc_internal.h"
41 #include "media_internal.h" // libvlc_media_set_state()
42 #include "media_player_internal.h"
44 static int
45 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
46 vlc_value_t oldval, vlc_value_t newval,
47 void * p_userdata );
48 static int
49 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
50 vlc_value_t oldval, vlc_value_t newval,
51 void * p_userdata );
52 static int
53 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
54 vlc_value_t oldval, vlc_value_t newval,
55 void * p_userdata );
57 static int
58 snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
59 vlc_value_t oldval, vlc_value_t newval, void *p_data );
61 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
64 * Shortcuts
67 #define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
68 static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type_t type)
70 libvlc_event_manager_register_event_type(mp->p_event_manager, type);
74 * The input lock protects the input and input resource pointer.
75 * It MUST NOT be used from callbacks.
77 * The object lock protects the reset, namely the media and the player state.
78 * It can, and usually needs to be taken from callbacks.
79 * The object lock can be acquired under the input lock... and consequently
80 * the opposite order is STRICTLY PROHIBITED.
82 static inline void lock(libvlc_media_player_t *mp)
84 vlc_mutex_lock(&mp->object_lock);
87 static inline void unlock(libvlc_media_player_t *mp)
89 vlc_mutex_unlock(&mp->object_lock);
92 static inline void lock_input(libvlc_media_player_t *mp)
94 vlc_mutex_lock(&mp->input.lock);
97 static inline void unlock_input(libvlc_media_player_t *mp)
99 vlc_mutex_unlock(&mp->input.lock);
103 * Release the associated input thread.
105 * Object lock is NOT held.
106 * Input lock is held or instance is being destroyed.
108 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
110 assert( p_mi );
112 input_thread_t *p_input_thread = p_mi->input.p_thread;
113 if( !p_input_thread )
114 return;
116 var_DelCallback( p_input_thread, "can-seek",
117 input_seekable_changed, p_mi );
118 var_DelCallback( p_input_thread, "can-pause",
119 input_pausable_changed, p_mi );
120 var_DelCallback( p_input_thread, "intf-event",
121 input_event_changed, p_mi );
123 /* We owned this one */
124 input_Stop( p_input_thread, b_input_abort );
126 vlc_thread_join( p_input_thread );
128 assert( p_input_thread->b_dead );
130 p_mi->input.p_thread = NULL;
131 vlc_object_release( p_input_thread );
135 * Retrieve the input thread. Be sure to release the object
136 * once you are done with it. (libvlc Internal)
138 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
140 input_thread_t *p_input_thread;
142 assert( p_mi );
144 lock_input(p_mi);
145 p_input_thread = p_mi->input.p_thread;
146 if( p_input_thread )
147 vlc_object_hold( p_input_thread );
148 else
149 libvlc_printerr( "No active input" );
150 unlock_input(p_mi);
152 return p_input_thread;
156 * Set the internal state of the media_player. (media player Internal)
158 * Function will lock the media_player.
160 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
161 bool b_locked )
163 if(!b_locked)
164 lock(p_mi);
165 p_mi->state = state;
167 libvlc_media_t *media = p_mi->p_md;
168 if (media)
169 libvlc_media_retain(media);
171 if(!b_locked)
172 unlock(p_mi);
174 if (media)
176 // Also set the state of the corresponding media
177 // This is strictly for convenience.
178 libvlc_media_set_state(media, state);
180 libvlc_media_release(media);
184 static int
185 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
186 vlc_value_t oldval, vlc_value_t newval,
187 void * p_userdata )
189 VLC_UNUSED(oldval);
190 VLC_UNUSED(p_this);
191 VLC_UNUSED(psz_cmd);
192 libvlc_media_player_t * p_mi = p_userdata;
193 libvlc_event_t event;
195 event.type = libvlc_MediaPlayerSeekableChanged;
196 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
198 libvlc_event_send( p_mi->p_event_manager, &event );
199 return VLC_SUCCESS;
202 static int
203 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
204 vlc_value_t oldval, vlc_value_t newval,
205 void * p_userdata )
207 VLC_UNUSED(oldval);
208 VLC_UNUSED(p_this);
209 VLC_UNUSED(psz_cmd);
210 libvlc_media_player_t * p_mi = p_userdata;
211 libvlc_event_t event;
213 event.type = libvlc_MediaPlayerPausableChanged;
214 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
216 libvlc_event_send( p_mi->p_event_manager, &event );
217 return VLC_SUCCESS;
220 static int
221 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
222 vlc_value_t oldval, vlc_value_t newval,
223 void * p_userdata )
225 VLC_UNUSED(oldval);
226 input_thread_t * p_input = (input_thread_t *)p_this;
227 libvlc_media_player_t * p_mi = p_userdata;
228 libvlc_event_t event;
230 assert( !strcmp( psz_cmd, "intf-event" ) );
232 if( newval.i_int == INPUT_EVENT_STATE )
234 libvlc_state_t libvlc_state;
236 switch ( var_GetInteger( p_input, "state" ) )
238 case INIT_S:
239 libvlc_state = libvlc_NothingSpecial;
240 event.type = libvlc_MediaPlayerNothingSpecial;
241 break;
242 case OPENING_S:
243 libvlc_state = libvlc_Opening;
244 event.type = libvlc_MediaPlayerOpening;
245 break;
246 case PLAYING_S:
247 libvlc_state = libvlc_Playing;
248 event.type = libvlc_MediaPlayerPlaying;
249 break;
250 case PAUSE_S:
251 libvlc_state = libvlc_Paused;
252 event.type = libvlc_MediaPlayerPaused;
253 break;
254 case END_S:
255 libvlc_state = libvlc_Ended;
256 event.type = libvlc_MediaPlayerEndReached;
257 break;
258 case ERROR_S:
259 libvlc_state = libvlc_Error;
260 event.type = libvlc_MediaPlayerEncounteredError;
261 break;
263 default:
264 return VLC_SUCCESS;
267 set_state( p_mi, libvlc_state, false );
268 libvlc_event_send( p_mi->p_event_manager, &event );
270 else if( newval.i_int == INPUT_EVENT_ABORT )
272 libvlc_state_t libvlc_state = libvlc_Stopped;
273 event.type = libvlc_MediaPlayerStopped;
275 set_state( p_mi, libvlc_state, false );
276 libvlc_event_send( p_mi->p_event_manager, &event );
278 else if( newval.i_int == INPUT_EVENT_POSITION )
280 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
281 return VLC_SUCCESS; /* Don't send the position while stopped */
283 /* */
284 event.type = libvlc_MediaPlayerPositionChanged;
285 event.u.media_player_position_changed.new_position =
286 var_GetFloat( p_input, "position" );
287 libvlc_event_send( p_mi->p_event_manager, &event );
289 /* */
290 event.type = libvlc_MediaPlayerTimeChanged;
291 event.u.media_player_time_changed.new_time =
292 from_mtime(var_GetTime( p_input, "time" ));
293 libvlc_event_send( p_mi->p_event_manager, &event );
295 else if( newval.i_int == INPUT_EVENT_LENGTH )
297 event.type = libvlc_MediaPlayerLengthChanged;
298 event.u.media_player_length_changed.new_length =
299 from_mtime(var_GetTime( p_input, "length" ));
300 libvlc_event_send( p_mi->p_event_manager, &event );
303 return VLC_SUCCESS;
307 /**************************************************************************
308 * Snapshot Taken Event.
310 * FIXME: This snapshot API interface makes no sense in media_player.
311 *************************************************************************/
312 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
313 vlc_value_t oldval, vlc_value_t newval, void *p_data )
315 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
317 libvlc_media_player_t *mp = p_data;
318 libvlc_event_t event;
319 event.type = libvlc_MediaPlayerSnapshotTaken;
320 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
321 libvlc_event_send(mp->p_event_manager, &event);
323 return VLC_SUCCESS;
326 static input_thread_t *find_input (vlc_object_t *obj)
328 libvlc_media_player_t *mp = (libvlc_media_player_t *)obj;
330 return libvlc_get_input_thread (mp);
333 /* */
334 static void libvlc_media_player_destroy( libvlc_media_player_t * );
337 /**************************************************************************
338 * Create a Media Instance object.
340 * Refcount strategy:
341 * - All items created by _new start with a refcount set to 1.
342 * - Accessor _release decrease the refcount by 1, if after that
343 * operation the refcount is 0, the object is destroyed.
344 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
346 * Object locking strategy:
347 * - No lock held while in constructor.
348 * - When accessing any member variable this lock is held. (XXX who locks?)
349 * - When attempting to destroy the object the lock is also held.
350 **************************************************************************/
351 libvlc_media_player_t *
352 libvlc_media_player_new( libvlc_instance_t *instance )
354 libvlc_media_player_t * mp;
356 assert(instance);
358 mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
359 if (unlikely(mp == NULL))
361 libvlc_printerr("Not enough memory");
362 return NULL;
364 vlc_object_attach (mp, mp->p_libvlc);
366 /* Input */
367 var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
369 /* Video */
370 var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
371 var_Create (mp, "window", VLC_VAR_STRING);
372 var_Create (mp, "vmem-lock", VLC_VAR_ADDRESS);
373 var_Create (mp, "vmem-unlock", VLC_VAR_ADDRESS);
374 var_Create (mp, "vmem-display", VLC_VAR_ADDRESS);
375 var_Create (mp, "vmem-data", VLC_VAR_ADDRESS);
376 var_Create (mp, "vmem-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
377 var_Create (mp, "vmem-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
378 var_Create (mp, "vmem-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
379 var_Create (mp, "vmem-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
380 var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
381 #ifdef WIN32
382 var_Create (mp, "drawable-hwnd", VLC_VAR_ADDRESS);
383 #endif
384 #ifdef __APPLE__
385 var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
386 var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
387 #endif
389 var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
390 var_SetBool (mp, "keyboard-events", true);
391 var_Create (mp, "mouse-events", VLC_VAR_BOOL);
392 var_SetBool (mp, "mouse-events", true);
394 var_Create (mp, "fullscreen", VLC_VAR_BOOL);
395 var_Create (mp, "autoscale", VLC_VAR_BOOL);
396 var_SetBool (mp, "autoscale", true);
397 var_Create (mp, "scale", VLC_VAR_FLOAT);
398 var_SetFloat (mp, "scale", 1.);
399 var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
400 var_Create (mp, "crop", VLC_VAR_STRING);
401 var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
402 var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
404 var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
406 var_Create (mp, "marq-marquee", VLC_VAR_STRING);
407 var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
408 var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
409 var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
410 var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
411 var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
412 var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
413 var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
414 var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
416 var_Create (mp, "logo-file", VLC_VAR_STRING);
417 var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
418 var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
419 var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
420 var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
421 var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
422 var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
424 var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
425 var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
426 var_Create (mp, "hue", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
427 var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
428 var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
430 /* Audio */
431 var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
432 var_Create (mp, "volume-muted", VLC_VAR_BOOL);
433 var_Create (mp, "saved-volume", VLC_VAR_INTEGER);
434 var_Create (mp, "volume-change", VLC_VAR_VOID);
435 var_Create (mp, "find-input-callback", VLC_VAR_ADDRESS);
436 var_SetAddress (mp, "find-input-callback", find_input);
438 mp->p_md = NULL;
439 mp->state = libvlc_NothingSpecial;
440 mp->p_libvlc_instance = instance;
441 mp->input.p_thread = NULL;
442 mp->input.p_resource = NULL;
443 vlc_mutex_init (&mp->input.lock);
444 mp->i_refcount = 1;
445 mp->p_event_manager = libvlc_event_manager_new(mp, instance);
446 if (unlikely(mp->p_event_manager == NULL))
448 vlc_object_release(mp);
449 return NULL;
451 vlc_mutex_init(&mp->object_lock);
453 register_event(mp, NothingSpecial);
454 register_event(mp, Opening);
455 register_event(mp, Buffering);
456 register_event(mp, Playing);
457 register_event(mp, Paused);
458 register_event(mp, Stopped);
459 register_event(mp, Forward);
460 register_event(mp, Backward);
461 register_event(mp, EndReached);
462 register_event(mp, EncounteredError);
463 register_event(mp, SeekableChanged);
465 register_event(mp, PositionChanged);
466 register_event(mp, TimeChanged);
467 register_event(mp, LengthChanged);
468 register_event(mp, TitleChanged);
469 register_event(mp, PausableChanged);
471 /* Snapshot initialization */
472 register_event(mp, SnapshotTaken);
474 register_event(mp, MediaChanged);
476 /* Attach a var callback to the global object to provide the glue between
477 * vout_thread that generates the event and media_player that re-emits it
478 * with its own event manager
480 * FIXME: It's unclear why we want to put this in public API, and why we
481 * want to expose it in such a limiting and ugly way.
483 var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
485 libvlc_retain(instance);
486 return mp;
489 /**************************************************************************
490 * Create a Media Instance object with a media descriptor.
491 **************************************************************************/
492 libvlc_media_player_t *
493 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
495 libvlc_media_player_t * p_mi;
497 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
498 if( !p_mi )
499 return NULL;
501 libvlc_media_retain( p_md );
502 p_mi->p_md = p_md;
504 return p_mi;
507 /**************************************************************************
508 * Destroy a Media Instance object (libvlc internal)
510 * Warning: No lock held here, but hey, this is internal. Caller must lock.
511 **************************************************************************/
512 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
514 assert( p_mi );
516 /* Detach Callback from the main libvlc object */
517 var_DelCallback( p_mi->p_libvlc,
518 "snapshot-file", snapshot_was_taken, p_mi );
520 /* No need for lock_input() because no other threads knows us anymore */
521 if( p_mi->input.p_thread )
522 release_input_thread(p_mi, true);
523 if( p_mi->input.p_resource )
525 input_resource_Terminate( p_mi->input.p_resource );
526 input_resource_Release( p_mi->input.p_resource );
527 p_mi->input.p_resource = NULL;
529 vlc_mutex_destroy( &p_mi->input.lock );
531 libvlc_event_manager_release( p_mi->p_event_manager );
532 libvlc_media_release( p_mi->p_md );
533 vlc_mutex_destroy( &p_mi->object_lock );
535 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
536 vlc_object_release( p_mi );
537 libvlc_release(instance);
540 /**************************************************************************
541 * Release a Media Instance object.
543 * Function does the locking.
544 **************************************************************************/
545 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
547 bool destroy;
549 assert( p_mi );
550 lock(p_mi);
551 destroy = !--p_mi->i_refcount;
552 unlock(p_mi);
554 if( destroy )
555 libvlc_media_player_destroy( p_mi );
558 /**************************************************************************
559 * Retain a Media Instance object.
561 * Caller must hold the lock.
562 **************************************************************************/
563 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
565 assert( p_mi );
567 lock(p_mi);
568 p_mi->i_refcount++;
569 unlock(p_mi);
572 /**************************************************************************
573 * Set the Media descriptor associated with the instance.
575 * Enter without lock -- function will lock the object.
576 **************************************************************************/
577 void libvlc_media_player_set_media(
578 libvlc_media_player_t *p_mi,
579 libvlc_media_t *p_md )
581 lock_input(p_mi);
583 /* FIXME I am not sure if it is a user request or on die(eof/error)
584 * request here */
585 release_input_thread( p_mi,
586 p_mi->input.p_thread &&
587 !p_mi->input.p_thread->b_eof &&
588 !p_mi->input.p_thread->b_error );
590 lock( p_mi );
591 set_state( p_mi, libvlc_NothingSpecial, true );
592 unlock_input( p_mi );
594 libvlc_media_release( p_mi->p_md );
596 if( !p_md )
598 p_mi->p_md = NULL;
599 unlock(p_mi);
600 return; /* It is ok to pass a NULL md */
603 libvlc_media_retain( p_md );
604 p_mi->p_md = p_md;
606 /* The policy here is to ignore that we were created using a different
607 * libvlc_instance, because we don't really care */
608 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
610 unlock(p_mi);
612 /* Send an event for the newly available media */
613 libvlc_event_t event;
614 event.type = libvlc_MediaPlayerMediaChanged;
615 event.u.media_player_media_changed.new_media = p_md;
616 libvlc_event_send( p_mi->p_event_manager, &event );
620 /**************************************************************************
621 * Get the Media descriptor associated with the instance.
622 **************************************************************************/
623 libvlc_media_t *
624 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
626 libvlc_media_t *p_m;
628 lock(p_mi);
629 p_m = p_mi->p_md;
630 if( p_m )
631 libvlc_media_retain( p_mi->p_md );
632 unlock(p_mi);
633 return p_mi->p_md;
636 /**************************************************************************
637 * Get the event Manager.
638 **************************************************************************/
639 libvlc_event_manager_t *
640 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
642 return p_mi->p_event_manager;
645 /**************************************************************************
646 * Tell media player to start playing.
647 **************************************************************************/
648 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
650 lock_input( p_mi );
652 input_thread_t *p_input_thread = p_mi->input.p_thread;
653 if( p_input_thread )
655 /* A thread already exists, send it a play message */
656 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
657 unlock_input( p_mi );
658 return 0;
661 /* Ignore previous exception */
662 lock(p_mi);
664 if( !p_mi->p_md )
666 unlock(p_mi);
667 unlock_input( p_mi );
668 libvlc_printerr( "No associated media descriptor" );
669 return -1;
672 if( !p_mi->input.p_resource )
673 p_mi->input.p_resource = input_resource_New( VLC_OBJECT( p_mi ) );
674 p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
675 p_mi->input.p_resource );
676 unlock(p_mi);
677 if( !p_input_thread )
679 unlock_input(p_mi);
680 libvlc_printerr( "Not enough memory" );
681 return -1;
684 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
685 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
686 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
688 if( input_Start( p_input_thread ) )
690 unlock_input(p_mi);
691 var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
692 var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
693 var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
694 vlc_object_release( p_input_thread );
695 libvlc_printerr( "Input initialization failure" );
696 return -1;
698 p_mi->input.p_thread = p_input_thread;
699 unlock_input(p_mi);
700 return 0;
703 void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
705 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
706 if( !p_input_thread )
707 return;
709 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
710 if( state == libvlc_Playing || state == libvlc_Buffering )
712 if( paused )
714 if( libvlc_media_player_can_pause( p_mi ) )
715 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
716 else
717 libvlc_media_player_stop( p_mi );
720 else
722 if( !paused )
723 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
726 vlc_object_release( p_input_thread );
729 /**************************************************************************
730 * Toggle pause.
731 **************************************************************************/
732 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
734 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
735 bool playing = (state == libvlc_Playing || state == libvlc_Buffering);
737 libvlc_media_player_set_pause( p_mi, playing );
740 /**************************************************************************
741 * Tells whether the media player is currently playing.
743 * Enter with lock held.
744 **************************************************************************/
745 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
747 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
748 return (libvlc_Playing == state) || (libvlc_Buffering == state);
751 /**************************************************************************
752 * Stop playing.
753 **************************************************************************/
754 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
756 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
758 lock_input(p_mi);
759 release_input_thread( p_mi, true ); /* This will stop the input thread */
761 /* Force to go to stopped state, in case we were in Ended, or Error
762 * state. */
763 if( state != libvlc_Stopped )
765 set_state( p_mi, libvlc_Stopped, false );
767 /* Construct and send the event */
768 libvlc_event_t event;
769 event.type = libvlc_MediaPlayerStopped;
770 libvlc_event_send( p_mi->p_event_manager, &event );
773 if( p_mi->input.p_resource != NULL )
774 input_resource_TerminateVout( p_mi->input.p_resource );
775 unlock_input(p_mi);
779 void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
780 void *(*lock_cb) (void *, void **),
781 void (*unlock_cb) (void *, void *, void *const *),
782 void (*display_cb) (void *, void *),
783 void *opaque )
785 var_SetAddress( mp, "vmem-lock", lock_cb );
786 var_SetAddress( mp, "vmem-unlock", unlock_cb );
787 var_SetAddress( mp, "vmem-display", display_cb );
788 var_SetAddress( mp, "vmem-data", opaque );
789 var_SetString( mp, "vout", "vmem" );
792 void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
793 unsigned width, unsigned height, unsigned pitch )
795 var_SetString( mp, "vmem-chroma", chroma );
796 var_SetInteger( mp, "vmem-width", width );
797 var_SetInteger( mp, "vmem-height", height );
798 var_SetInteger( mp, "vmem-pitch", pitch );
801 /**************************************************************************
802 * set_nsobject
803 **************************************************************************/
804 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
805 void * drawable )
807 assert (p_mi != NULL);
808 #ifdef __APPLE__
809 var_SetAddress (p_mi, "drawable-nsobject", drawable);
810 #else
811 (void) p_mi; (void)drawable;
812 #endif
815 /**************************************************************************
816 * get_nsobject
817 **************************************************************************/
818 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
820 assert (p_mi != NULL);
821 #ifdef __APPLE__
822 return var_GetAddress (p_mi, "drawable-nsobject");
823 #else
824 return NULL;
825 #endif
828 /**************************************************************************
829 * set_agl
830 **************************************************************************/
831 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
832 uint32_t drawable )
834 #ifdef __APPLE__
835 var_SetInteger (p_mi, "drawable-agl", drawable);
836 #else
837 (void) p_mi; (void)drawable;
838 #endif
841 /**************************************************************************
842 * get_agl
843 **************************************************************************/
844 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
846 assert (p_mi != NULL);
847 #ifdef __APPLE__
848 return var_GetInteger (p_mi, "drawable-agl");
849 #else
850 return 0;
851 #endif
854 /**************************************************************************
855 * set_xwindow
856 **************************************************************************/
857 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
858 uint32_t drawable )
860 assert (p_mi != NULL);
862 var_SetString (p_mi, "vout", drawable ? "xid" : "any");
863 var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "any");
864 var_SetInteger (p_mi, "drawable-xid", drawable);
867 /**************************************************************************
868 * get_xwindow
869 **************************************************************************/
870 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
872 return var_GetInteger (p_mi, "drawable-xid");
875 /**************************************************************************
876 * set_hwnd
877 **************************************************************************/
878 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
879 void *drawable )
881 assert (p_mi != NULL);
882 #ifdef WIN32
883 var_SetString (p_mi, "window",
884 (drawable != NULL) ? "embed-hwnd,any" : "");
885 var_SetAddress (p_mi, "drawable-hwnd", drawable);
886 #else
887 (void) p_mi; (void) drawable;
888 #endif
891 /**************************************************************************
892 * get_hwnd
893 **************************************************************************/
894 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
896 assert (p_mi != NULL);
897 #ifdef WIN32
898 return var_GetAddress (p_mi, "drawable-hwnd");
899 #else
900 return NULL;
901 #endif
904 /**************************************************************************
905 * Getters for stream information
906 **************************************************************************/
907 libvlc_time_t libvlc_media_player_get_length(
908 libvlc_media_player_t *p_mi )
910 input_thread_t *p_input_thread;
911 libvlc_time_t i_time;
913 p_input_thread = libvlc_get_input_thread ( p_mi );
914 if( !p_input_thread )
915 return -1;
917 i_time = from_mtime(var_GetTime( p_input_thread, "length" ));
918 vlc_object_release( p_input_thread );
920 return i_time;
923 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
925 input_thread_t *p_input_thread;
926 libvlc_time_t i_time;
928 p_input_thread = libvlc_get_input_thread ( p_mi );
929 if( !p_input_thread )
930 return -1;
932 i_time = from_mtime(var_GetTime( p_input_thread , "time" ));
933 vlc_object_release( p_input_thread );
934 return i_time;
937 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
938 libvlc_time_t i_time )
940 input_thread_t *p_input_thread;
942 p_input_thread = libvlc_get_input_thread ( p_mi );
943 if( !p_input_thread )
944 return;
946 var_SetTime( p_input_thread, "time", to_mtime(i_time) );
947 vlc_object_release( p_input_thread );
950 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
951 float position )
953 input_thread_t *p_input_thread;
955 p_input_thread = libvlc_get_input_thread ( p_mi );
956 if( !p_input_thread )
957 return;
959 var_SetFloat( p_input_thread, "position", position );
960 vlc_object_release( p_input_thread );
963 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
965 input_thread_t *p_input_thread;
966 float f_position;
968 p_input_thread = libvlc_get_input_thread ( p_mi );
969 if( !p_input_thread )
970 return -1.0;
972 f_position = var_GetFloat( p_input_thread, "position" );
973 vlc_object_release( p_input_thread );
975 return f_position;
978 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
979 int chapter )
981 input_thread_t *p_input_thread;
983 p_input_thread = libvlc_get_input_thread ( p_mi );
984 if( !p_input_thread )
985 return;
987 var_SetInteger( p_input_thread, "chapter", chapter );
988 vlc_object_release( p_input_thread );
991 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
993 input_thread_t *p_input_thread;
994 int i_chapter;
996 p_input_thread = libvlc_get_input_thread ( p_mi );
997 if( !p_input_thread )
998 return -1;
1000 i_chapter = var_GetInteger( p_input_thread, "chapter" );
1001 vlc_object_release( p_input_thread );
1003 return i_chapter;
1006 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1008 input_thread_t *p_input_thread;
1009 vlc_value_t val;
1011 p_input_thread = libvlc_get_input_thread ( p_mi );
1012 if( !p_input_thread )
1013 return -1;
1015 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
1016 vlc_object_release( p_input_thread );
1018 return val.i_int;
1021 int libvlc_media_player_get_chapter_count_for_title(
1022 libvlc_media_player_t *p_mi,
1023 int i_title )
1025 input_thread_t *p_input_thread;
1026 vlc_value_t val;
1028 p_input_thread = libvlc_get_input_thread ( p_mi );
1029 if( !p_input_thread )
1030 return -1;
1032 char *psz_name;
1033 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
1035 vlc_object_release( p_input_thread );
1036 return -1;
1038 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1039 vlc_object_release( p_input_thread );
1040 free( psz_name );
1042 return val.i_int;
1045 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1046 int i_title )
1048 input_thread_t *p_input_thread;
1050 p_input_thread = libvlc_get_input_thread ( p_mi );
1051 if( !p_input_thread )
1052 return;
1054 var_SetInteger( p_input_thread, "title", i_title );
1055 vlc_object_release( p_input_thread );
1057 //send event
1058 libvlc_event_t event;
1059 event.type = libvlc_MediaPlayerTitleChanged;
1060 event.u.media_player_title_changed.new_title = i_title;
1061 libvlc_event_send( p_mi->p_event_manager, &event );
1064 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1066 input_thread_t *p_input_thread;
1067 int i_title;
1069 p_input_thread = libvlc_get_input_thread ( p_mi );
1070 if( !p_input_thread )
1071 return -1;
1073 i_title = var_GetInteger( p_input_thread, "title" );
1074 vlc_object_release( p_input_thread );
1076 return i_title;
1079 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1081 input_thread_t *p_input_thread;
1082 vlc_value_t val;
1084 p_input_thread = libvlc_get_input_thread ( p_mi );
1085 if( !p_input_thread )
1086 return -1;
1088 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1089 vlc_object_release( p_input_thread );
1091 return val.i_int;
1094 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1096 input_thread_t *p_input_thread;
1098 p_input_thread = libvlc_get_input_thread ( p_mi );
1099 if( !p_input_thread )
1100 return;
1102 int i_type = var_Type( p_input_thread, "next-chapter" );
1103 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1104 "next-chapter":"next-title", true );
1106 vlc_object_release( p_input_thread );
1109 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1111 input_thread_t *p_input_thread;
1113 p_input_thread = libvlc_get_input_thread ( p_mi );
1114 if( !p_input_thread )
1115 return;
1117 int i_type = var_Type( p_input_thread, "next-chapter" );
1118 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1119 "prev-chapter":"prev-title", true );
1121 vlc_object_release( p_input_thread );
1124 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1126 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1127 double f_fps = 0.0;
1129 if( p_input_thread )
1131 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1132 f_fps = 0.0;
1133 vlc_object_release( p_input_thread );
1135 return f_fps;
1138 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1140 bool b_will_play;
1141 input_thread_t *p_input_thread =
1142 libvlc_get_input_thread ( p_mi );
1143 if ( !p_input_thread )
1144 return false;
1146 b_will_play = !p_input_thread->b_die && !p_input_thread->b_dead;
1147 vlc_object_release( p_input_thread );
1149 return b_will_play;
1152 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1154 if (rate < 0.)
1156 libvlc_printerr ("Playing backward not supported");
1157 return -1;
1160 var_SetFloat (p_mi, "rate", rate);
1162 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1163 if( !p_input_thread )
1164 return 0;
1165 var_SetFloat( p_input_thread, "rate", rate );
1166 vlc_object_release( p_input_thread );
1167 return 0;
1170 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1172 return var_GetFloat (p_mi, "rate");
1175 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1177 lock(p_mi);
1178 libvlc_state_t state = p_mi->state;
1179 unlock(p_mi);
1180 return state;
1183 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1185 input_thread_t *p_input_thread;
1186 bool b_seekable;
1188 p_input_thread = libvlc_get_input_thread ( p_mi );
1189 if ( !p_input_thread )
1190 return false;
1191 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1192 vlc_object_release( p_input_thread );
1194 return b_seekable;
1197 /* internal function, used by audio, video */
1198 libvlc_track_description_t *
1199 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1200 const char *psz_variable )
1202 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1203 libvlc_track_description_t *p_track_description = NULL,
1204 *p_actual, *p_previous;
1206 if( !p_input )
1207 return NULL;
1209 vlc_value_t val_list, text_list;
1210 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1212 /* no tracks */
1213 if( val_list.p_list->i_count <= 0 )
1214 goto end;
1216 p_track_description = ( libvlc_track_description_t * )
1217 malloc( sizeof( libvlc_track_description_t ) );
1218 if ( !p_track_description )
1220 libvlc_printerr( "Not enough memory" );
1221 goto end;
1223 p_actual = p_track_description;
1224 p_previous = NULL;
1225 for( int i = 0; i < val_list.p_list->i_count; i++ )
1227 if( !p_actual )
1229 p_actual = ( libvlc_track_description_t * )
1230 malloc( sizeof( libvlc_track_description_t ) );
1231 if ( !p_actual )
1233 libvlc_track_description_release( p_track_description );
1234 libvlc_printerr( "Not enough memory" );
1235 goto end;
1238 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1239 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1240 p_actual->p_next = NULL;
1241 if( p_previous )
1242 p_previous->p_next = p_actual;
1243 p_previous = p_actual;
1244 p_actual = NULL;
1247 end:
1248 var_FreeList( &val_list, &text_list );
1249 vlc_object_release( p_input );
1251 return p_track_description;
1254 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1256 libvlc_track_description_t *p_actual, *p_before;
1257 p_actual = p_td;
1259 while ( p_actual )
1261 free( p_actual->psz_name );
1262 p_before = p_actual;
1263 p_actual = p_before->p_next;
1264 free( p_before );
1268 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1270 input_thread_t *p_input_thread;
1271 bool b_can_pause;
1273 p_input_thread = libvlc_get_input_thread ( p_mi );
1274 if ( !p_input_thread )
1275 return false;
1276 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1277 vlc_object_release( p_input_thread );
1279 return b_can_pause;
1282 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1284 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1285 if( p_input_thread != NULL )
1287 var_TriggerCallback( p_input_thread, "frame-next" );
1288 vlc_object_release( p_input_thread );