1 /*****************************************************************************
2 * media_player.c: Libvlc API Media Instance management functions
3 *****************************************************************************
4 * Copyright (C) 2005-2009 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
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>
40 #include "libvlc_internal.h"
41 #include "media_internal.h" // libvlc_media_set_state()
42 #include "media_player_internal.h"
45 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
46 vlc_value_t oldval, vlc_value_t newval,
49 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
50 vlc_value_t oldval, vlc_value_t newval,
53 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
54 vlc_value_t oldval, vlc_value_t newval,
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 );
63 mouse_moved( vlc_object_t *p_this, char const *psz_cmd,
64 vlc_value_t oldval, vlc_value_t newval, void *p_data );
66 mouse_button( vlc_object_t *p_this, char const *psz_cmd,
67 vlc_value_t oldval, vlc_value_t newval, void *p_data );
69 mouse_clicked( vlc_object_t *p_this, char const *psz_cmd,
70 vlc_value_t oldval, vlc_value_t newval, void *p_data );
72 mouse_object( vlc_object_t *p_this, char const *psz_cmd,
73 vlc_value_t oldval, vlc_value_t newval, void *p_data );
76 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
82 #define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
83 static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type_t type)
85 libvlc_event_manager_register_event_type(mp->p_event_manager, type);
88 static inline void lock(libvlc_media_player_t *mp)
90 vlc_mutex_lock(&mp->object_lock);
93 static inline void unlock(libvlc_media_player_t *mp)
95 vlc_mutex_unlock(&mp->object_lock);
99 * Release the associated input thread.
101 * Object lock is NOT held.
103 static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
105 input_thread_t * p_input_thread;
107 if( !p_mi || !p_mi->p_input_thread )
110 p_input_thread = p_mi->p_input_thread;
112 var_DelCallback( p_input_thread, "can-seek",
113 input_seekable_changed, p_mi );
114 var_DelCallback( p_input_thread, "can-pause",
115 input_pausable_changed, p_mi );
116 var_DelCallback( p_input_thread, "intf-event",
117 input_event_changed, p_mi );
119 /* We owned this one */
120 input_Stop( p_input_thread, b_input_abort );
122 vlc_thread_join( p_input_thread );
124 assert( p_mi->p_input_resource == NULL );
125 assert( p_input_thread->b_dead );
126 /* Store the input resource for future use. */
127 p_mi->p_input_resource = input_DetachResource( p_input_thread );
129 vlc_object_release( p_input_thread );
131 p_mi->p_input_thread = NULL;
135 * Retrieve the input thread. Be sure to release the object
136 * once you are done with it. (libvlc Internal)
138 * Function will lock the object.
140 input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
142 input_thread_t *p_input_thread;
147 p_input_thread = p_mi->p_input_thread;
149 vlc_object_hold( p_input_thread );
151 libvlc_printerr( "No active input" );
154 return p_input_thread;
158 * Get vout thread from current input
160 * Object lock is NOT held.
162 static vout_thread_t *get_vout_thread( libvlc_media_player_t *p_mi )
164 vout_thread_t *p_vout_thread;
166 p_vout_thread = input_GetVout( p_mi->p_input_thread );
169 var_AddCallback( p_vout_thread, "mouse-button-down", mouse_button, p_mi );
170 var_AddCallback( p_vout_thread, "mouse-moved", mouse_moved, p_mi );
171 var_AddCallback( p_vout_thread, "mouse-clicked", mouse_clicked, p_mi );
172 var_AddCallback( p_vout_thread, "mouse-object", mouse_object, p_mi );
175 return p_vout_thread;
179 * Release the associated vout thread.
181 * Object lock is NOT held.
183 static void release_vout_thread( libvlc_media_player_t *p_mi )
185 vout_thread_t *p_vout_thread;
187 if( !p_mi || !p_mi->p_vout_thread )
190 p_vout_thread = p_mi->p_vout_thread;
192 var_DelCallback( p_vout_thread, "mouse-button-down", mouse_button, p_mi );
193 var_DelCallback( p_vout_thread, "mouse-moved", mouse_moved, p_mi );
194 var_DelCallback( p_vout_thread, "mouse-clicked", mouse_clicked, p_mi );
195 var_DelCallback( p_vout_thread, "mouse-object", mouse_object, p_mi );
197 vlc_object_release( p_vout_thread );
198 p_mi->p_vout_thread = NULL;
202 * Set the internal state of the media_player. (media player Internal)
204 * Function will lock the media_player.
206 static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
213 libvlc_media_t *media = p_mi->p_md;
215 libvlc_media_retain(media);
222 // Also set the state of the corresponding media
223 // This is strictly for convenience.
224 libvlc_media_set_state(media, state);
226 libvlc_media_release(media);
231 input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
232 vlc_value_t oldval, vlc_value_t newval,
238 libvlc_media_player_t * p_mi = p_userdata;
239 libvlc_event_t event;
241 event.type = libvlc_MediaPlayerSeekableChanged;
242 event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
244 libvlc_event_send( p_mi->p_event_manager, &event );
249 input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
250 vlc_value_t oldval, vlc_value_t newval,
256 libvlc_media_player_t * p_mi = p_userdata;
257 libvlc_event_t event;
259 event.type = libvlc_MediaPlayerPausableChanged;
260 event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
262 libvlc_event_send( p_mi->p_event_manager, &event );
267 input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
268 vlc_value_t oldval, vlc_value_t newval,
272 input_thread_t * p_input = (input_thread_t *)p_this;
273 libvlc_media_player_t * p_mi = p_userdata;
274 libvlc_event_t event;
276 assert( !strcmp( psz_cmd, "intf-event" ) );
278 if( newval.i_int == INPUT_EVENT_STATE )
280 libvlc_state_t libvlc_state;
282 switch ( var_GetInteger( p_input, "state" ) )
285 libvlc_state = libvlc_NothingSpecial;
286 event.type = libvlc_MediaPlayerNothingSpecial;
289 libvlc_state = libvlc_Opening;
290 event.type = libvlc_MediaPlayerOpening;
293 libvlc_state = libvlc_Playing;
294 event.type = libvlc_MediaPlayerPlaying;
297 libvlc_state = libvlc_Paused;
298 event.type = libvlc_MediaPlayerPaused;
301 libvlc_state = libvlc_Ended;
302 event.type = libvlc_MediaPlayerEndReached;
305 libvlc_state = libvlc_Error;
306 event.type = libvlc_MediaPlayerEncounteredError;
313 set_state( p_mi, libvlc_state, false );
314 libvlc_event_send( p_mi->p_event_manager, &event );
316 else if( newval.i_int == INPUT_EVENT_ABORT )
318 libvlc_state_t libvlc_state = libvlc_Stopped;
319 event.type = libvlc_MediaPlayerStopped;
321 set_state( p_mi, libvlc_state, false );
322 libvlc_event_send( p_mi->p_event_manager, &event );
324 else if( newval.i_int == INPUT_EVENT_POSITION )
326 if( var_GetInteger( p_input, "state" ) != PLAYING_S )
327 return VLC_SUCCESS; /* Don't send the position while stopped */
330 event.type = libvlc_MediaPlayerPositionChanged;
331 event.u.media_player_position_changed.new_position =
332 var_GetFloat( p_input, "position" );
333 libvlc_event_send( p_mi->p_event_manager, &event );
336 event.type = libvlc_MediaPlayerTimeChanged;
337 event.u.media_player_time_changed.new_time =
338 from_mtime(var_GetTime( p_input, "time" ));
339 libvlc_event_send( p_mi->p_event_manager, &event );
341 else if( newval.i_int == INPUT_EVENT_LENGTH )
343 event.type = libvlc_MediaPlayerLengthChanged;
344 event.u.media_player_length_changed.new_length =
345 from_mtime(var_GetTime( p_input, "length" ));
346 libvlc_event_send( p_mi->p_event_manager, &event );
348 else if( newval.i_int == INPUT_EVENT_VOUT )
351 /* release old vout */
352 if( p_mi->p_vout_thread )
353 release_vout_thread( p_mi );
354 /* remember new vout */
355 p_mi->p_vout_thread = get_vout_thread( p_mi );
363 /**************************************************************************
364 * Snapshot Taken Event.
366 * FIXME: This snapshot API interface makes no sense in media_player.
367 *************************************************************************/
368 static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
369 vlc_value_t oldval, vlc_value_t newval, void *p_data )
371 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
373 libvlc_media_player_t *mp = p_data;
374 libvlc_event_t event;
375 event.type = libvlc_MediaPlayerSnapshotTaken;
376 event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
377 libvlc_event_send(mp->p_event_manager, &event);
382 /**************************************************************************
384 *************************************************************************/
387 mouse_moved( vlc_object_t *p_this, char const *psz_cmd,
388 vlc_value_t oldval, vlc_value_t newval, void *p_data )
390 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this); VLC_UNUSED(newval);
392 libvlc_media_player_t *mp = p_data;
393 libvlc_event_t event;
394 event.type = libvlc_MediaPlayerMouseMoved;
395 event.u.media_player_mouse_moved.x = var_GetInteger( mp->p_vout_thread, "mouse-x" );
396 event.u.media_player_mouse_moved.y = var_GetInteger( mp->p_vout_thread, "mouse-y" );
397 libvlc_event_send(mp->p_event_manager, &event);
403 mouse_button( vlc_object_t *p_this, char const *psz_cmd,
404 vlc_value_t oldval, vlc_value_t newval, void *p_data )
406 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
408 libvlc_media_player_t *mp = p_data;
409 libvlc_event_t event;
410 int pressed = newval.i_int;
412 event.type = libvlc_MediaPlayerMouseButton;
413 event.u.media_player_mouse_button.mb_left = (pressed & (1<<MOUSE_BUTTON_LEFT));
414 event.u.media_player_mouse_button.mb_center = (pressed & (1<<MOUSE_BUTTON_CENTER));
415 event.u.media_player_mouse_button.mb_right = (pressed & (1<<MOUSE_BUTTON_RIGHT));
416 event.u.media_player_mouse_button.mb_wheel_up = (pressed & (1<<MOUSE_BUTTON_WHEEL_UP));
417 event.u.media_player_mouse_button.mb_wheel_down = (pressed & (1<<MOUSE_BUTTON_WHEEL_DOWN));
418 libvlc_event_send(mp->p_event_manager, &event);
424 mouse_clicked( vlc_object_t *p_this, char const *psz_cmd,
425 vlc_value_t oldval, vlc_value_t newval, void *p_data )
427 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
429 libvlc_media_player_t *mp = p_data;
430 libvlc_event_t event;
431 event.type = libvlc_MediaPlayerMouseClick;
432 event.u.media_player_mouse_clicked.clicked = newval.b_bool ? 1 : 0;
433 libvlc_event_send(mp->p_event_manager, &event);
439 mouse_object( vlc_object_t *p_this, char const *psz_cmd,
440 vlc_value_t oldval, vlc_value_t newval, void *p_data )
442 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);
444 libvlc_media_player_t *mp = p_data;
445 libvlc_event_t event;
446 event.type = libvlc_MediaPlayerMouseObject;
447 event.u.media_player_mouse_object.moved = (newval.b_bool ? 1 : 0);
448 libvlc_event_send(mp->p_event_manager, &event);
454 static void libvlc_media_player_destroy( libvlc_media_player_t * );
457 /**************************************************************************
458 * Create a Media Instance object.
461 * - All items created by _new start with a refcount set to 1.
462 * - Accessor _release decrease the refcount by 1, if after that
463 * operation the refcount is 0, the object is destroyed.
464 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
466 * Object locking strategy:
467 * - No lock held while in constructor.
468 * - When accessing any member variable this lock is held. (XXX who locks?)
469 * - When attempting to destroy the object the lock is also held.
470 **************************************************************************/
471 libvlc_media_player_t *
472 libvlc_media_player_new( libvlc_instance_t *instance )
474 libvlc_media_player_t * mp;
478 mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
479 if (unlikely(mp == NULL))
481 libvlc_printerr("Not enough memory");
484 vlc_object_attach (mp, mp->p_libvlc);
487 var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
489 var_Create (mp, "drawable-hwnd", VLC_VAR_ADDRESS);
492 var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
493 var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
496 var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
497 var_SetBool (mp, "keyboard-events", true);
498 var_Create (mp, "mouse-events", VLC_VAR_BOOL);
499 var_SetBool (mp, "mouse-events", true);
501 var_Create (mp, "fullscreen", VLC_VAR_BOOL);
502 var_Create (mp, "autoscale", VLC_VAR_BOOL);
503 var_SetBool (mp, "autoscale", true);
504 var_Create (mp, "scale", VLC_VAR_FLOAT);
505 var_SetFloat (mp, "scale", 1.);
506 var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
507 var_Create (mp, "crop", VLC_VAR_STRING);
508 var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
509 var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
511 var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
513 var_Create (mp, "marq-marquee", VLC_VAR_STRING);
514 var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
515 var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
516 var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
517 var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
518 var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
519 var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
520 var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
521 var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
523 var_Create (mp, "logo-file", VLC_VAR_STRING);
524 var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
525 var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
526 var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
527 var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
528 var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
529 var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
532 var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
535 mp->state = libvlc_NothingSpecial;
536 mp->p_libvlc_instance = instance;
537 mp->p_input_thread = NULL;
538 mp->p_input_resource = NULL;
539 mp->p_vout_thread = NULL;
541 mp->p_event_manager = libvlc_event_manager_new(mp, instance);
542 if (unlikely(mp->p_event_manager == NULL))
544 vlc_object_release(mp);
547 vlc_mutex_init(&mp->object_lock);
549 register_event(mp, NothingSpecial);
550 register_event(mp, Opening);
551 register_event(mp, Buffering);
552 register_event(mp, Playing);
553 register_event(mp, Paused);
554 register_event(mp, Stopped);
555 register_event(mp, Forward);
556 register_event(mp, Backward);
557 register_event(mp, EndReached);
558 register_event(mp, EncounteredError);
559 register_event(mp, SeekableChanged);
561 register_event(mp, PositionChanged);
562 register_event(mp, TimeChanged);
563 register_event(mp, LengthChanged);
564 register_event(mp, TitleChanged);
565 register_event(mp, PausableChanged);
567 /* Snapshot initialization */
568 register_event(mp, SnapshotTaken);
570 register_event(mp, MediaChanged);
573 register_event(mp, MouseMoved);
574 register_event(mp, MouseButton);
575 register_event(mp, MouseClick);
576 register_event(mp, MouseObject);
578 /* Attach a var callback to the global object to provide the glue between
579 * vout_thread that generates the event and media_player that re-emits it
580 * with its own event manager
582 * FIXME: It's unclear why we want to put this in public API, and why we
583 * want to expose it in such a limiting and ugly way.
585 var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
587 libvlc_retain(instance);
591 /**************************************************************************
592 * Create a Media Instance object with a media descriptor.
593 **************************************************************************/
594 libvlc_media_player_t *
595 libvlc_media_player_new_from_media( libvlc_media_t * p_md )
597 libvlc_media_player_t * p_mi;
599 p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
603 libvlc_media_retain( p_md );
609 /**************************************************************************
610 * Destroy a Media Instance object (libvlc internal)
612 * Warning: No lock held here, but hey, this is internal. Caller must lock.
613 **************************************************************************/
614 static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
618 /* Detach Callback from the main libvlc object */
619 var_DelCallback( p_mi->p_libvlc,
620 "snapshot-file", snapshot_was_taken, p_mi );
622 /* If the input thread hasn't been already deleted it means
623 * that the owners didn't stop the thread before releasing it. */
624 assert(!p_mi->p_input_thread);
626 /* Fallback for those who don't use NDEBUG */
627 if( p_mi->p_vout_thread )
628 release_vout_thread( p_mi );
630 if(p_mi->p_input_thread )
631 release_input_thread(p_mi, true);
633 if( p_mi->p_input_resource )
635 input_resource_Delete( p_mi->p_input_resource );
636 p_mi->p_input_resource = NULL;
639 libvlc_event_manager_release( p_mi->p_event_manager );
640 libvlc_media_release( p_mi->p_md );
641 vlc_mutex_destroy( &p_mi->object_lock );
643 libvlc_instance_t *instance = p_mi->p_libvlc_instance;
644 vlc_object_release( p_mi );
645 libvlc_release(instance);
648 /**************************************************************************
649 * Release a Media Instance object.
651 * Function does the locking.
652 **************************************************************************/
653 void libvlc_media_player_release( libvlc_media_player_t *p_mi )
659 destroy = !--p_mi->i_refcount;
663 libvlc_media_player_destroy( p_mi );
666 /**************************************************************************
667 * Retain a Media Instance object.
669 * Caller must hold the lock.
670 **************************************************************************/
671 void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
680 /**************************************************************************
681 * Set the Media descriptor associated with the instance.
683 * Enter without lock -- function will lock the object.
684 **************************************************************************/
685 void libvlc_media_player_set_media(
686 libvlc_media_player_t *p_mi,
687 libvlc_media_t *p_md )
691 /* FIXME I am not sure if it is a user request or on die(eof/error)
693 release_input_thread( p_mi,
694 p_mi->p_input_thread &&
695 !p_mi->p_input_thread->b_eof &&
696 !p_mi->p_input_thread->b_error );
698 set_state( p_mi, libvlc_NothingSpecial, true );
700 libvlc_media_release( p_mi->p_md );
706 return; /* It is ok to pass a NULL md */
709 libvlc_media_retain( p_md );
712 /* The policy here is to ignore that we were created using a different
713 * libvlc_instance, because we don't really care */
714 p_mi->p_libvlc_instance = p_md->p_libvlc_instance;
718 /* Send an event for the newly available media */
719 libvlc_event_t event;
720 event.type = libvlc_MediaPlayerMediaChanged;
721 event.u.media_player_media_changed.new_media = p_md;
722 libvlc_event_send( p_mi->p_event_manager, &event );
726 /**************************************************************************
727 * Get the Media descriptor associated with the instance.
728 **************************************************************************/
730 libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
737 libvlc_media_retain( p_mi->p_md );
742 /**************************************************************************
743 * Get the event Manager.
744 **************************************************************************/
745 libvlc_event_manager_t *
746 libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
748 return p_mi->p_event_manager;
751 /**************************************************************************
752 * Tell media player to start playing.
753 **************************************************************************/
754 int libvlc_media_player_play( libvlc_media_player_t *p_mi )
756 input_thread_t * p_input_thread;
758 if( (p_input_thread = libvlc_get_input_thread( p_mi )) )
760 /* A thread already exists, send it a play message */
761 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
762 vlc_object_release( p_input_thread );
766 /* Ignore previous exception */
772 libvlc_printerr( "No associated media descriptor" );
776 p_mi->p_input_thread = input_Create( p_mi,
777 p_mi->p_md->p_input_item, NULL,
778 p_mi->p_input_resource );
779 if( !p_mi->p_input_thread )
782 libvlc_printerr( "Not enough memory" );
786 p_mi->p_input_resource = NULL;
787 p_input_thread = p_mi->p_input_thread;
789 var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
790 var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
791 var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
793 if( input_Start( p_input_thread ) )
795 vlc_object_release( p_input_thread );
796 p_mi->p_input_thread = NULL;
803 /**************************************************************************
805 **************************************************************************/
806 void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
808 input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
809 if( !p_input_thread )
812 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
813 if( state == libvlc_Playing || state == libvlc_Buffering )
815 if( libvlc_media_player_can_pause( p_mi ) )
816 input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
818 libvlc_media_player_stop( p_mi );
821 input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
823 vlc_object_release( p_input_thread );
826 /**************************************************************************
827 * Tells whether the media player is currently playing.
829 * Enter with lock held.
830 **************************************************************************/
831 int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
833 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
834 return (libvlc_Playing == state) || (libvlc_Buffering == state);
837 /**************************************************************************
839 **************************************************************************/
840 void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
842 libvlc_state_t state = libvlc_media_player_get_state( p_mi );
845 release_input_thread( p_mi, true ); /* This will stop the input thread */
848 /* Force to go to stopped state, in case we were in Ended, or Error
850 if( state != libvlc_Stopped )
852 set_state( p_mi, libvlc_Stopped, false );
854 /* Construct and send the event */
855 libvlc_event_t event;
856 event.type = libvlc_MediaPlayerStopped;
857 libvlc_event_send( p_mi->p_event_manager, &event );
861 /**************************************************************************
863 **************************************************************************/
864 void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
867 assert (p_mi != NULL);
869 var_SetAddress (p_mi, "drawable-nsobject", drawable);
871 (void) p_mi; (void)drawable;
875 /**************************************************************************
877 **************************************************************************/
878 void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
880 assert (p_mi != NULL);
882 return var_GetAddress (p_mi, "drawable-nsobject");
888 /**************************************************************************
890 **************************************************************************/
891 void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
895 var_SetInteger (p_mi, "drawable-agl", drawable);
897 (void) p_mi; (void)drawable;
901 /**************************************************************************
903 **************************************************************************/
904 uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
906 assert (p_mi != NULL);
908 return var_GetInteger (p_mi, "drawable-agl");
914 /**************************************************************************
916 **************************************************************************/
917 void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
920 assert (p_mi != NULL);
921 var_SetInteger (p_mi, "drawable-xid", drawable);
924 /**************************************************************************
926 **************************************************************************/
927 uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
929 return var_GetInteger (p_mi, "drawable-xid");
932 /**************************************************************************
934 **************************************************************************/
935 void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
938 assert (p_mi != NULL);
940 var_SetAddress (p_mi, "drawable-hwnd", drawable);
942 (void) p_mi; (void) drawable;
946 /**************************************************************************
948 **************************************************************************/
949 void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
951 assert (p_mi != NULL);
953 return var_GetAddress (p_mi, "drawable-hwnd");
959 /**************************************************************************
960 * Getters for stream information
961 **************************************************************************/
962 libvlc_time_t libvlc_media_player_get_length(
963 libvlc_media_player_t *p_mi )
965 input_thread_t *p_input_thread;
966 libvlc_time_t i_time;
968 p_input_thread = libvlc_get_input_thread ( p_mi );
969 if( !p_input_thread )
972 i_time = from_mtime(var_GetTime( p_input_thread, "length" ));
973 vlc_object_release( p_input_thread );
978 libvlc_time_t libvlc_media_player_get_time( libvlc_media_player_t *p_mi )
980 input_thread_t *p_input_thread;
981 libvlc_time_t i_time;
983 p_input_thread = libvlc_get_input_thread ( p_mi );
984 if( !p_input_thread )
987 i_time = from_mtime(var_GetTime( p_input_thread , "time" ));
988 vlc_object_release( p_input_thread );
992 void libvlc_media_player_set_time( libvlc_media_player_t *p_mi,
993 libvlc_time_t i_time )
995 input_thread_t *p_input_thread;
997 p_input_thread = libvlc_get_input_thread ( p_mi );
998 if( !p_input_thread )
1001 var_SetTime( p_input_thread, "time", to_mtime(i_time) );
1002 vlc_object_release( p_input_thread );
1005 void libvlc_media_player_set_position( libvlc_media_player_t *p_mi,
1008 input_thread_t *p_input_thread;
1010 p_input_thread = libvlc_get_input_thread ( p_mi );
1011 if( !p_input_thread )
1014 var_SetFloat( p_input_thread, "position", position );
1015 vlc_object_release( p_input_thread );
1018 float libvlc_media_player_get_position( libvlc_media_player_t *p_mi )
1020 input_thread_t *p_input_thread;
1023 p_input_thread = libvlc_get_input_thread ( p_mi );
1024 if( !p_input_thread )
1027 f_position = var_GetFloat( p_input_thread, "position" );
1028 vlc_object_release( p_input_thread );
1033 void libvlc_media_player_set_chapter( libvlc_media_player_t *p_mi,
1036 input_thread_t *p_input_thread;
1038 p_input_thread = libvlc_get_input_thread ( p_mi );
1039 if( !p_input_thread )
1042 var_SetInteger( p_input_thread, "chapter", chapter );
1043 vlc_object_release( p_input_thread );
1046 int libvlc_media_player_get_chapter( libvlc_media_player_t *p_mi )
1048 input_thread_t *p_input_thread;
1051 p_input_thread = libvlc_get_input_thread ( p_mi );
1052 if( !p_input_thread )
1055 i_chapter = var_GetInteger( p_input_thread, "chapter" );
1056 vlc_object_release( p_input_thread );
1061 int libvlc_media_player_get_chapter_count( libvlc_media_player_t *p_mi )
1063 input_thread_t *p_input_thread;
1066 p_input_thread = libvlc_get_input_thread ( p_mi );
1067 if( !p_input_thread )
1070 var_Change( p_input_thread, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
1071 vlc_object_release( p_input_thread );
1076 int libvlc_media_player_get_chapter_count_for_title(
1077 libvlc_media_player_t *p_mi,
1080 input_thread_t *p_input_thread;
1083 p_input_thread = libvlc_get_input_thread ( p_mi );
1084 if( !p_input_thread )
1088 if( asprintf( &psz_name, "title %2i", i_title ) == -1 )
1090 vlc_object_release( p_input_thread );
1093 var_Change( p_input_thread, psz_name, VLC_VAR_CHOICESCOUNT, &val, NULL );
1094 vlc_object_release( p_input_thread );
1100 void libvlc_media_player_set_title( libvlc_media_player_t *p_mi,
1103 input_thread_t *p_input_thread;
1105 p_input_thread = libvlc_get_input_thread ( p_mi );
1106 if( !p_input_thread )
1109 var_SetInteger( p_input_thread, "title", i_title );
1110 vlc_object_release( p_input_thread );
1113 libvlc_event_t event;
1114 event.type = libvlc_MediaPlayerTitleChanged;
1115 event.u.media_player_title_changed.new_title = i_title;
1116 libvlc_event_send( p_mi->p_event_manager, &event );
1119 int libvlc_media_player_get_title( libvlc_media_player_t *p_mi )
1121 input_thread_t *p_input_thread;
1124 p_input_thread = libvlc_get_input_thread ( p_mi );
1125 if( !p_input_thread )
1128 i_title = var_GetInteger( p_input_thread, "title" );
1129 vlc_object_release( p_input_thread );
1134 int libvlc_media_player_get_title_count( libvlc_media_player_t *p_mi )
1136 input_thread_t *p_input_thread;
1139 p_input_thread = libvlc_get_input_thread ( p_mi );
1140 if( !p_input_thread )
1143 var_Change( p_input_thread, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
1144 vlc_object_release( p_input_thread );
1149 void libvlc_media_player_next_chapter( libvlc_media_player_t *p_mi )
1151 input_thread_t *p_input_thread;
1153 p_input_thread = libvlc_get_input_thread ( p_mi );
1154 if( !p_input_thread )
1157 int i_type = var_Type( p_input_thread, "next-chapter" );
1158 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1159 "next-chapter":"next-title", true );
1161 vlc_object_release( p_input_thread );
1164 void libvlc_media_player_previous_chapter( libvlc_media_player_t *p_mi )
1166 input_thread_t *p_input_thread;
1168 p_input_thread = libvlc_get_input_thread ( p_mi );
1169 if( !p_input_thread )
1172 int i_type = var_Type( p_input_thread, "next-chapter" );
1173 var_SetBool( p_input_thread, (i_type & VLC_VAR_TYPE) != 0 ?
1174 "prev-chapter":"prev-title", true );
1176 vlc_object_release( p_input_thread );
1179 float libvlc_media_player_get_fps( libvlc_media_player_t *p_mi )
1181 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1184 if( p_input_thread )
1186 if( input_Control( p_input_thread, INPUT_GET_VIDEO_FPS, &f_fps ) )
1188 vlc_object_release( p_input_thread );
1193 int libvlc_media_player_will_play( libvlc_media_player_t *p_mi )
1196 input_thread_t *p_input_thread =
1197 libvlc_get_input_thread ( p_mi );
1198 if ( !p_input_thread )
1201 b_will_play = !p_input_thread->b_die && !p_input_thread->b_dead;
1202 vlc_object_release( p_input_thread );
1207 int libvlc_media_player_set_rate( libvlc_media_player_t *p_mi, float rate )
1209 input_thread_t *p_input_thread;
1212 p_input_thread = libvlc_get_input_thread ( p_mi );
1213 if( !p_input_thread )
1216 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1217 if( (rate < 0.0) && !b_can_rewind )
1219 vlc_object_release( p_input_thread );
1220 libvlc_printerr( "Invalid playback rate" );
1224 var_SetFloat( p_input_thread, "rate", rate );
1225 vlc_object_release( p_input_thread );
1229 float libvlc_media_player_get_rate( libvlc_media_player_t *p_mi )
1231 input_thread_t *p_input_thread;
1235 p_input_thread = libvlc_get_input_thread ( p_mi );
1236 if( !p_input_thread )
1237 return 0.0; /* rate < 0 indicates rewind */
1239 f_rate = var_GetFloat( p_input_thread, "rate" );
1240 b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
1241 /* FIXME: why are negative values forbidden ?? (rewinding) */
1242 if( f_rate < 0 && !b_can_rewind )
1244 vlc_object_release( p_input_thread );
1247 vlc_object_release( p_input_thread );
1252 libvlc_state_t libvlc_media_player_get_state( libvlc_media_player_t *p_mi )
1255 libvlc_state_t state = p_mi->state;
1260 int libvlc_media_player_is_seekable( libvlc_media_player_t *p_mi )
1262 input_thread_t *p_input_thread;
1265 p_input_thread = libvlc_get_input_thread ( p_mi );
1266 if ( !p_input_thread )
1268 b_seekable = var_GetBool( p_input_thread, "can-seek" );
1269 vlc_object_release( p_input_thread );
1274 /* internal function, used by audio, video */
1275 libvlc_track_description_t *
1276 libvlc_get_track_description( libvlc_media_player_t *p_mi,
1277 const char *psz_variable )
1279 input_thread_t *p_input = libvlc_get_input_thread( p_mi );
1280 libvlc_track_description_t *p_track_description = NULL,
1281 *p_actual, *p_previous;
1286 vlc_value_t val_list, text_list;
1287 var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list);
1290 if( val_list.p_list->i_count <= 0 )
1293 p_track_description = ( libvlc_track_description_t * )
1294 malloc( sizeof( libvlc_track_description_t ) );
1295 if ( !p_track_description )
1297 libvlc_printerr( "Not enough memory" );
1300 p_actual = p_track_description;
1302 for( int i = 0; i < val_list.p_list->i_count; i++ )
1306 p_actual = ( libvlc_track_description_t * )
1307 malloc( sizeof( libvlc_track_description_t ) );
1310 libvlc_track_description_release( p_track_description );
1311 libvlc_printerr( "Not enough memory" );
1315 p_actual->i_id = val_list.p_list->p_values[i].i_int;
1316 p_actual->psz_name = strdup( text_list.p_list->p_values[i].psz_string );
1317 p_actual->p_next = NULL;
1319 p_previous->p_next = p_actual;
1320 p_previous = p_actual;
1325 var_FreeList( &val_list, &text_list );
1326 vlc_object_release( p_input );
1328 return p_track_description;
1331 void libvlc_track_description_release( libvlc_track_description_t *p_td )
1333 libvlc_track_description_t *p_actual, *p_before;
1338 free( p_actual->psz_name );
1339 p_before = p_actual;
1340 p_actual = p_before->p_next;
1345 int libvlc_media_player_can_pause( libvlc_media_player_t *p_mi )
1347 input_thread_t *p_input_thread;
1350 p_input_thread = libvlc_get_input_thread ( p_mi );
1351 if ( !p_input_thread )
1353 b_can_pause = var_GetBool( p_input_thread, "can-pause" );
1354 vlc_object_release( p_input_thread );
1359 void libvlc_media_player_next_frame( libvlc_media_player_t *p_mi )
1361 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
1362 if( p_input_thread != NULL )
1364 var_TriggerCallback( p_input_thread, "frame-next" );
1365 vlc_object_release( p_input_thread );