media_player.c 47.3 KB
Newer Older
Clément Stenac's avatar
Clément Stenac committed
1
/*****************************************************************************
2
 * media_player.c: Libvlc API Media Instance management functions
Clément Stenac's avatar
Clément Stenac committed
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2005-2011 VLC authors and VideoLAN
Clément Stenac's avatar
Clément Stenac committed
5
 *
6
 * Authors: Clément Stenac <zorglub@videolan.org>
Clément Stenac's avatar
Clément Stenac committed
7
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
8 9 10
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
Clément Stenac's avatar
Clément Stenac committed
11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
Clément Stenac's avatar
Clément Stenac committed
17
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
18 19 20
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Clément Stenac's avatar
Clément Stenac committed
21 22
 *****************************************************************************/

Geoffroy Couprie's avatar
Geoffroy Couprie committed
23 24 25 26
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

27
#include <assert.h>
28

Clément Stenac's avatar
Clément Stenac committed
29
#include <vlc/libvlc.h>
30 31 32
#include <vlc/libvlc_media.h>
#include <vlc/libvlc_events.h>

Clément Stenac's avatar
Clément Stenac committed
33 34
#include <vlc_demux.h>
#include <vlc_input.h>
35
#include <vlc_vout.h>
Mark Lee's avatar
Mark Lee committed
36
#include <vlc_aout.h>
37
#include <vlc_keys.h>
38 39 40 41

#include "libvlc_internal.h"
#include "media_internal.h" // libvlc_media_set_state()
#include "media_player_internal.h"
Clément Stenac's avatar
Clément Stenac committed
42

43
static int
44 45 46 47 48 49 50 51
input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
                        vlc_value_t oldval, vlc_value_t newval,
                        void * p_userdata );
static int
input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
                        vlc_value_t oldval, vlc_value_t newval,
                        void * p_userdata );
static int
52 53 54 55
input_scrambled_changed( vlc_object_t * p_this, char const * psz_cmd,
                        vlc_value_t oldval, vlc_value_t newval,
                        void * p_userdata );
static int
Laurent Aimar's avatar
Laurent Aimar committed
56
input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
57 58 59
                     vlc_value_t oldval, vlc_value_t newval,
                     void * p_userdata );

60 61 62
static int
snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
                    vlc_value_t oldval, vlc_value_t newval, void *p_data );
63

64
static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
65

66 67 68 69 70 71 72
/*
 * Shortcuts
 */

#define register_event(a, b) __register_event(a, libvlc_MediaPlayer ## b)
static inline void __register_event(libvlc_media_player_t *mp, libvlc_event_type_t type)
{
73
    libvlc_event_manager_register_event_type(mp->p_event_manager, type);
74 75
}

76 77 78 79 80 81 82 83 84
/*
 * The input lock protects the input and input resource pointer.
 * It MUST NOT be used from callbacks.
 *
 * The object lock protects the reset, namely the media and the player state.
 * It can, and usually needs to be taken from callbacks.
 * The object lock can be acquired under the input lock... and consequently
 * the opposite order is STRICTLY PROHIBITED.
 */
85 86 87 88 89 90 91 92 93 94
static inline void lock(libvlc_media_player_t *mp)
{
    vlc_mutex_lock(&mp->object_lock);
}

static inline void unlock(libvlc_media_player_t *mp)
{
    vlc_mutex_unlock(&mp->object_lock);
}

95 96 97 98 99 100 101 102 103 104
static inline void lock_input(libvlc_media_player_t *mp)
{
    vlc_mutex_lock(&mp->input.lock);
}

static inline void unlock_input(libvlc_media_player_t *mp)
{
    vlc_mutex_unlock(&mp->input.lock);
}

105
/*
JP Dinger's avatar
JP Dinger committed
106
 * Release the associated input thread.
107 108
 *
 * Object lock is NOT held.
109
 * Input lock is held or instance is being destroyed.
110
 */
111
static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
112
{
113
    assert( p_mi );
114

115
    input_thread_t *p_input_thread = p_mi->input.p_thread;
116
    if( !p_input_thread )
117
        return;
118
    p_mi->input.p_thread = NULL;
119

120 121 122 123
    var_DelCallback( p_input_thread, "can-seek",
                     input_seekable_changed, p_mi );
    var_DelCallback( p_input_thread, "can-pause",
                    input_pausable_changed, p_mi );
124 125
    var_DelCallback( p_input_thread, "program-scrambled",
                    input_scrambled_changed, p_mi );
126 127 128 129 130
    var_DelCallback( p_input_thread, "intf-event",
                     input_event_changed, p_mi );

    /* We owned this one */
    input_Stop( p_input_thread, b_input_abort );
131
    input_Close( p_input_thread );
132 133
}

134 135
/*
 * Retrieve the input thread. Be sure to release the object
136
 * once you are done with it. (libvlc Internal)
137
 */
138
input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
Clément Stenac's avatar
Clément Stenac committed
139 140 141
{
    input_thread_t *p_input_thread;

142
    assert( p_mi );
143

144 145
    lock_input(p_mi);
    p_input_thread = p_mi->input.p_thread;
146 147
    if( p_input_thread )
        vlc_object_hold( p_input_thread );
148 149
    else
        libvlc_printerr( "No active input" );
150
    unlock_input(p_mi);
151

152 153 154
    return p_input_thread;
}

155 156 157 158 159
/*
 * Set the internal state of the media_player. (media player Internal)
 *
 * Function will lock the media_player.
 */
160 161 162 163 164
static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
    bool b_locked )
{
    if(!b_locked)
        lock(p_mi);
165
    p_mi->state = state;
166

167 168 169
    libvlc_media_t *media = p_mi->p_md;
    if (media)
        libvlc_media_retain(media);
170

171 172
    if(!b_locked)
        unlock(p_mi);
173

174 175
    if (media)
    {
176 177 178
        // Also set the state of the corresponding media
        // This is strictly for convenience.
        libvlc_media_set_state(media, state);
179 180

        libvlc_media_release(media);
181 182 183
    }
}

184
static int
185
input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
186 187 188 189
                        vlc_value_t oldval, vlc_value_t newval,
                        void * p_userdata )
{
    VLC_UNUSED(oldval);
190 191
    VLC_UNUSED(p_this);
    VLC_UNUSED(psz_cmd);
192
    libvlc_media_player_t * p_mi = p_userdata;
193 194
    libvlc_event_t event;

195
    event.type = libvlc_MediaPlayerSeekableChanged;
196
    event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
197 198 199 200 201 202 203 204 205 206 207

    libvlc_event_send( p_mi->p_event_manager, &event );
    return VLC_SUCCESS;
}

static int
input_pausable_changed( vlc_object_t * p_this, char const * psz_cmd,
                        vlc_value_t oldval, vlc_value_t newval,
                        void * p_userdata )
{
    VLC_UNUSED(oldval);
208 209
    VLC_UNUSED(p_this);
    VLC_UNUSED(psz_cmd);
210
    libvlc_media_player_t * p_mi = p_userdata;
211 212
    libvlc_event_t event;

213
    event.type = libvlc_MediaPlayerPausableChanged;
214
    event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
215 216 217 218 219

    libvlc_event_send( p_mi->p_event_manager, &event );
    return VLC_SUCCESS;
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
static int
input_scrambled_changed( vlc_object_t * p_this, char const * psz_cmd,
                        vlc_value_t oldval, vlc_value_t newval,
                        void * p_userdata )
{
    VLC_UNUSED(oldval);
    VLC_UNUSED(p_this);
    VLC_UNUSED(psz_cmd);
    libvlc_media_player_t * p_mi = p_userdata;
    libvlc_event_t event;

    event.type = libvlc_MediaPlayerScrambledChanged;
    event.u.media_player_scrambled_changed.new_scrambled = newval.b_bool;

    libvlc_event_send( p_mi->p_event_manager, &event );
    return VLC_SUCCESS;
}

238
static int
Laurent Aimar's avatar
Laurent Aimar committed
239
input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
240 241 242
                     vlc_value_t oldval, vlc_value_t newval,
                     void * p_userdata )
{
243
    VLC_UNUSED(oldval);
Laurent Aimar's avatar
Laurent Aimar committed
244
    input_thread_t * p_input = (input_thread_t *)p_this;
245
    libvlc_media_player_t * p_mi = p_userdata;
Laurent Aimar's avatar
Laurent Aimar committed
246
    libvlc_event_t event;
247

248
    assert( !strcmp( psz_cmd, "intf-event" ) );
249

250 251
    if( newval.i_int == INPUT_EVENT_STATE )
    {
252
        libvlc_state_t libvlc_state;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

        switch ( var_GetInteger( p_input, "state" ) )
        {
            case INIT_S:
                libvlc_state = libvlc_NothingSpecial;
                event.type = libvlc_MediaPlayerNothingSpecial;
                break;
            case OPENING_S:
                libvlc_state = libvlc_Opening;
                event.type = libvlc_MediaPlayerOpening;
                break;
            case PLAYING_S:
                libvlc_state = libvlc_Playing;
                event.type = libvlc_MediaPlayerPlaying;
                break;
            case PAUSE_S:
                libvlc_state = libvlc_Paused;
                event.type = libvlc_MediaPlayerPaused;
                break;
            case END_S:
                libvlc_state = libvlc_Ended;
                event.type = libvlc_MediaPlayerEndReached;
                break;
            case ERROR_S:
                libvlc_state = libvlc_Error;
                event.type = libvlc_MediaPlayerEncounteredError;
                break;

            default:
                return VLC_SUCCESS;
        }
284

285
        set_state( p_mi, libvlc_state, false );
286 287
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
288 289 290 291 292
    else if( newval.i_int == INPUT_EVENT_ABORT )
    {
        libvlc_state_t libvlc_state = libvlc_Stopped;
        event.type = libvlc_MediaPlayerStopped;

293
        set_state( p_mi, libvlc_state, false );
294 295
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
296
    else if( newval.i_int == INPUT_EVENT_POSITION )
297 298 299 300 301 302
    {
        if( var_GetInteger( p_input, "state" ) != PLAYING_S )
            return VLC_SUCCESS; /* Don't send the position while stopped */

        /* */
        event.type = libvlc_MediaPlayerPositionChanged;
JP Dinger's avatar
JP Dinger committed
303 304
        event.u.media_player_position_changed.new_position =
                                          var_GetFloat( p_input, "position" );
305 306 307 308
        libvlc_event_send( p_mi->p_event_manager, &event );

        /* */
        event.type = libvlc_MediaPlayerTimeChanged;
JP Dinger's avatar
JP Dinger committed
309
        event.u.media_player_time_changed.new_time =
310
           from_mtime(var_GetTime( p_input, "time" ));
311 312
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
313 314 315 316
    else if( newval.i_int == INPUT_EVENT_LENGTH )
    {
        event.type = libvlc_MediaPlayerLengthChanged;
        event.u.media_player_length_changed.new_length =
317
           from_mtime(var_GetTime( p_input, "length" ));
318 319
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
320 321 322 323 324 325 326
    else if( newval.i_int == INPUT_EVENT_CACHE )
    {
        event.type = libvlc_MediaPlayerBuffering;
        event.u.media_player_buffering.new_cache = (int)(100 *
            var_GetFloat( p_input, "cache" ));
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    else if( newval.i_int == INPUT_EVENT_VOUT )
    {
        vout_thread_t **pp_vout;
        size_t i_vout;
        if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
        {
            i_vout  = 0;
        }
        else
        {
            for( size_t i = 0; i < i_vout; i++ )
                vlc_object_release( pp_vout[i] );
            free( pp_vout );
        }

        event.type = libvlc_MediaPlayerVout;
        event.u.media_player_vout.new_count = i_vout;
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
Laurent Aimar's avatar
Laurent Aimar committed
346

347 348 349
    return VLC_SUCCESS;
}

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/**************************************************************************
 * Snapshot Taken Event.
 *
 * FIXME: This snapshot API interface makes no sense in media_player.
 *************************************************************************/
static int snapshot_was_taken(vlc_object_t *p_this, char const *psz_cmd,
                              vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_this);

    libvlc_media_player_t *mp = p_data;
    libvlc_event_t event;
    event.type = libvlc_MediaPlayerSnapshotTaken;
    event.u.media_player_snapshot_taken.psz_filename = newval.psz_string;
    libvlc_event_send(mp->p_event_manager, &event);

    return VLC_SUCCESS;
}

369
/* */
370 371
static void libvlc_media_player_destroy( libvlc_media_player_t * );

372

373
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
374 375 376 377 378 379 380 381 382 383 384 385
 * Create a Media Instance object.
 *
 * Refcount strategy:
 * - All items created by _new start with a refcount set to 1.
 * - Accessor _release decrease the refcount by 1, if after that
 *   operation the refcount is 0, the object is destroyed.
 * - Accessor _retain increase the refcount by 1 (XXX: to implement)
 *
 * Object locking strategy:
 * - No lock held while in constructor.
 * - When accessing any member variable this lock is held. (XXX who locks?)
 * - When attempting to destroy the object the lock is also held.
386
 **************************************************************************/
387
libvlc_media_player_t *
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
388
libvlc_media_player_new( libvlc_instance_t *instance )
389
{
390
    libvlc_media_player_t * mp;
391

392
    assert(instance);
393

394 395
    mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
    if (unlikely(mp == NULL))
396
    {
397
        libvlc_printerr("Not enough memory");
398 399
        return NULL;
    }
400

401 402 403
    /* Input */
    var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);

404
    /* Video */
405
    var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
406
    var_Create (mp, "window", VLC_VAR_STRING);
407 408 409 410
    var_Create (mp, "vmem-lock", VLC_VAR_ADDRESS);
    var_Create (mp, "vmem-unlock", VLC_VAR_ADDRESS);
    var_Create (mp, "vmem-display", VLC_VAR_ADDRESS);
    var_Create (mp, "vmem-data", VLC_VAR_ADDRESS);
411 412
    var_Create (mp, "vmem-setup", VLC_VAR_ADDRESS);
    var_Create (mp, "vmem-cleanup", VLC_VAR_ADDRESS);
413 414 415
    var_Create (mp, "vmem-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
    var_Create (mp, "vmem-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "vmem-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
416
    var_Create (mp, "vmem-pitch", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
417
    var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
418
#if defined (_WIN32) || defined (__OS2__)
419
    var_Create (mp, "drawable-hwnd", VLC_VAR_INTEGER);
420 421 422 423 424 425 426 427 428
#endif
#ifdef __APPLE__
    var_Create (mp, "drawable-agl", VLC_VAR_INTEGER);
    var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
#endif

    var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
    var_SetBool (mp, "keyboard-events", true);
    var_Create (mp, "mouse-events", VLC_VAR_BOOL);
429
    var_SetBool (mp, "mouse-events", true);
430

431 432 433 434 435 436 437 438 439
    var_Create (mp, "fullscreen", VLC_VAR_BOOL);
    var_Create (mp, "autoscale", VLC_VAR_BOOL);
    var_SetBool (mp, "autoscale", true);
    var_Create (mp, "scale", VLC_VAR_FLOAT);
    var_SetFloat (mp, "scale", 1.);
    var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
    var_Create (mp, "crop", VLC_VAR_STRING);
    var_Create (mp, "deinterlace", VLC_VAR_INTEGER);
    var_Create (mp, "deinterlace-mode", VLC_VAR_STRING);
440

441
    var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
442

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    var_Create (mp, "marq-marquee", VLC_VAR_STRING);
    var_Create (mp, "marq-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "marq-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "marq-refresh", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "marq-size", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "marq-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "marq-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);

    var_Create (mp, "logo-file", VLC_VAR_STRING);
    var_Create (mp, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "logo-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "logo-repeat", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "logo-opacity", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);

461 462 463 464 465 466
    var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
    var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
    var_Create (mp, "hue", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
    var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);

467
     /* Audio */
468
    var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
469
    var_Create (mp, "mute", VLC_VAR_BOOL);
470
    var_Create (mp, "volume", VLC_VAR_FLOAT);
471
    var_Create (mp, "corks", VLC_VAR_INTEGER);
472 473
    var_Create (mp, "amem-data", VLC_VAR_ADDRESS);
    var_Create (mp, "amem-setup", VLC_VAR_ADDRESS);
Sébastien Toque's avatar
Sébastien Toque committed
474
    var_Create (mp, "amem-cleanup", VLC_VAR_ADDRESS);
475
    var_Create (mp, "amem-play", VLC_VAR_ADDRESS);
476 477 478 479
    var_Create (mp, "amem-pause", VLC_VAR_ADDRESS);
    var_Create (mp, "amem-resume", VLC_VAR_ADDRESS);
    var_Create (mp, "amem-flush", VLC_VAR_ADDRESS);
    var_Create (mp, "amem-drain", VLC_VAR_ADDRESS);
480 481 482 483
    var_Create (mp, "amem-set-volume", VLC_VAR_ADDRESS);
    var_Create (mp, "amem-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
    var_Create (mp, "amem-rate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "amem-channels", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
484

485 486 487 488 489
    /* Video Title */
    var_Create (mp, "video-title-show", VLC_VAR_BOOL);
    var_Create (mp, "video-title-position", VLC_VAR_INTEGER);
    var_Create (mp, "video-title-timeout", VLC_VAR_INTEGER);

Mark Lee's avatar
Mark Lee committed
490 491 492 493
    /* Equalizer */
    var_Create (mp, "equalizer-preamp", VLC_VAR_FLOAT);
    var_Create (mp, "equalizer-bands", VLC_VAR_STRING);

494
    mp->p_md = NULL;
495
    mp->state = libvlc_NothingSpecial;
496
    mp->p_libvlc_instance = instance;
497
    mp->input.p_thread = NULL;
498 499 500 501 502 503
    mp->input.p_resource = input_resource_New(VLC_OBJECT(mp));
    if (unlikely(mp->input.p_resource == NULL))
    {
        vlc_object_release(mp);
        return NULL;
    }
504 505 506 507
    audio_output_t *aout = input_resource_GetAout(mp->input.p_resource);
    if( aout != NULL )
        input_resource_PutAout(mp->input.p_resource, aout);

508
    vlc_mutex_init (&mp->input.lock);
509
    mp->i_refcount = 1;
510 511
    mp->p_event_manager = libvlc_event_manager_new(mp, instance);
    if (unlikely(mp->p_event_manager == NULL))
512
    {
513
        input_resource_Release(mp->input.p_resource);
514
        vlc_object_release(mp);
515 516
        return NULL;
    }
517
    vlc_mutex_init(&mp->object_lock);
518

519 520 521 522 523 524 525 526 527 528
    register_event(mp, NothingSpecial);
    register_event(mp, Opening);
    register_event(mp, Buffering);
    register_event(mp, Playing);
    register_event(mp, Paused);
    register_event(mp, Stopped);
    register_event(mp, Forward);
    register_event(mp, Backward);
    register_event(mp, EndReached);
    register_event(mp, EncounteredError);
529
    register_event(mp, SeekableChanged);
530 531 532 533 534 535

    register_event(mp, PositionChanged);
    register_event(mp, TimeChanged);
    register_event(mp, LengthChanged);
    register_event(mp, TitleChanged);
    register_event(mp, PausableChanged);
536

537
    register_event(mp, Vout);
538
    register_event(mp, ScrambledChanged);
539

540
    /* Snapshot initialization */
541
    register_event(mp, SnapshotTaken);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
542

543 544
    register_event(mp, MediaChanged);

545
    /* Attach a var callback to the global object to provide the glue between
546 547 548 549 550 551
     * vout_thread that generates the event and media_player that re-emits it
     * with its own event manager
     *
     * FIXME: It's unclear why we want to put this in public API, and why we
     * want to expose it in such a limiting and ugly way.
     */
552
    var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
553

554
    libvlc_retain(instance);
555
    return mp;
556 557 558
}

/**************************************************************************
JP Dinger's avatar
JP Dinger committed
559
 * Create a Media Instance object with a media descriptor.
560
 **************************************************************************/
561
libvlc_media_player_t *
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
562
libvlc_media_player_new_from_media( libvlc_media_t * p_md )
563
{
564
    libvlc_media_player_t * p_mi;
565

Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
566
    p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
567
    if( !p_mi )
568 569
        return NULL;

570
    libvlc_media_retain( p_md );
571
    p_mi->p_md = p_md;
572 573 574 575 576

    return p_mi;
}

/**************************************************************************
577
 * Destroy a Media Instance object (libvlc internal)
578
 *
JP Dinger's avatar
JP Dinger committed
579
 * Warning: No lock held here, but hey, this is internal. Caller must lock.
580
 **************************************************************************/
581
static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
582
{
583
    assert( p_mi );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
584

585
    /* Detach Callback from the main libvlc object */
586
    var_DelCallback( p_mi->p_libvlc,
587
                     "snapshot-file", snapshot_was_taken, p_mi );
588

589 590
    /* No need for lock_input() because no other threads knows us anymore */
    if( p_mi->input.p_thread )
591
        release_input_thread(p_mi, true);
592 593
    input_resource_Terminate( p_mi->input.p_resource );
    input_resource_Release( p_mi->input.p_resource );
594
    vlc_mutex_destroy( &p_mi->input.lock );
595

596
    libvlc_event_manager_release( p_mi->p_event_manager );
597
    libvlc_media_release( p_mi->p_md );
598
    vlc_mutex_destroy( &p_mi->object_lock );
599 600

    libvlc_instance_t *instance = p_mi->p_libvlc_instance;
601
    vlc_object_release( p_mi );
602
    libvlc_release(instance);
603 604 605
}

/**************************************************************************
JP Dinger's avatar
JP Dinger committed
606 607 608
 * Release a Media Instance object.
 *
 * Function does the locking.
609
 **************************************************************************/
610
void libvlc_media_player_release( libvlc_media_player_t *p_mi )
611
{
612
    bool destroy;
613

614
    assert( p_mi );
615
    lock(p_mi);
616
    destroy = !--p_mi->i_refcount;
617
    unlock(p_mi);
618

619 620
    if( destroy )
        libvlc_media_player_destroy( p_mi );
621
}
622

623
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
624 625 626
 * Retain a Media Instance object.
 *
 * Caller must hold the lock.
627
 **************************************************************************/
628
void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
629
{
630
    assert( p_mi );
631

632
    lock(p_mi);
633
    p_mi->i_refcount++;
634
    unlock(p_mi);
635
}
636

637
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
638 639 640
 * Set the Media descriptor associated with the instance.
 *
 * Enter without lock -- function will lock the object.
641
 **************************************************************************/
642 643
void libvlc_media_player_set_media(
                            libvlc_media_player_t *p_mi,
644
                            libvlc_media_t *p_md )
645
{
646
    lock_input(p_mi);
647

648 649 650
    /* FIXME I am not sure if it is a user request or on die(eof/error)
     * request here */
    release_input_thread( p_mi,
651 652 653
                          p_mi->input.p_thread &&
                          !p_mi->input.p_thread->b_eof &&
                          !p_mi->input.p_thread->b_error );
654

655
    lock( p_mi );
656
    set_state( p_mi, libvlc_NothingSpecial, true );
657
    unlock_input( p_mi );
658

659
    libvlc_media_release( p_mi->p_md );
660

661 662 663
    if( !p_md )
    {
        p_mi->p_md = NULL;
664
        unlock(p_mi);
665 666 667
        return; /* It is ok to pass a NULL md */
    }

668
    libvlc_media_retain( p_md );
669
    p_mi->p_md = p_md;
670

671 672 673 674
    /* The policy here is to ignore that we were created using a different
     * libvlc_instance, because we don't really care */
    p_mi->p_libvlc_instance = p_md->p_libvlc_instance;

675
    unlock(p_mi);
676 677 678 679 680 681

    /* Send an event for the newly available media */
    libvlc_event_t event;
    event.type = libvlc_MediaPlayerMediaChanged;
    event.u.media_player_media_changed.new_media = p_md;
    libvlc_event_send( p_mi->p_event_manager, &event );
682

683 684 685
}

/**************************************************************************
JP Dinger's avatar
JP Dinger committed
686
 * Get the Media descriptor associated with the instance.
687
 **************************************************************************/
688
libvlc_media_t *
689
libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
690
{
691
    libvlc_media_t *p_m;
692

693
    lock(p_mi);
694 695 696
    p_m = p_mi->p_md;
    if( p_m )
        libvlc_media_retain( p_mi->p_md );
697
    unlock(p_mi);
698
    return p_mi->p_md;
699 700
}

701
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
702
 * Get the event Manager.
703 704
 **************************************************************************/
libvlc_event_manager_t *
705
libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
706 707 708 709
{
    return p_mi->p_event_manager;
}

710
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
711
 * Tell media player to start playing.
712
 **************************************************************************/
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
713
int libvlc_media_player_play( libvlc_media_player_t *p_mi )
714
{
715
    lock_input( p_mi );
716

717 718
    input_thread_t *p_input_thread = p_mi->input.p_thread;
    if( p_input_thread )
719
    {
Sam Hocevar's avatar
Sam Hocevar committed
720
        /* A thread already exists, send it a play message */
721
        input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
722
        unlock_input( p_mi );
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
723
        return 0;
724 725
    }

726
    /* Ignore previous exception */
727
    lock(p_mi);
728

729 730
    if( !p_mi->p_md )
    {
731
        unlock(p_mi);
732
        unlock_input( p_mi );
733
        libvlc_printerr( "No associated media descriptor" );
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
734
        return -1;
735 736
    }

737 738 739 740
    p_input_thread = input_Create( p_mi, p_mi->p_md->p_input_item, NULL,
                                   p_mi->input.p_resource );
    unlock(p_mi);
    if( !p_input_thread )
741
    {
742
        unlock_input(p_mi);
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
743 744
        libvlc_printerr( "Not enough memory" );
        return -1;
745
    }
746

747 748
    var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
    var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
749
    var_AddCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
Laurent Aimar's avatar
Laurent Aimar committed
750
    var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
751

752 753
    if( input_Start( p_input_thread ) )
    {
754 755 756
        unlock_input(p_mi);
        var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
        var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
757
        var_DelCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
758
        var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
759
        vlc_object_release( p_input_thread );
760 761
        libvlc_printerr( "Input initialization failure" );
        return -1;
762
    }
763 764
    p_mi->input.p_thread = p_input_thread;
    unlock_input(p_mi);
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
765
    return 0;
766 767
}

768
void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
769
{
770
    input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
771
    if( !p_input_thread )
772 773
        return;

774
    libvlc_state_t state = libvlc_media_player_get_state( p_mi );
775
    if( state == libvlc_Playing || state == libvlc_Buffering )
776
    {
777 778 779 780 781 782 783
        if( paused )
        {
            if( libvlc_media_player_can_pause( p_mi ) )
                input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
            else
                libvlc_media_player_stop( p_mi );
        }
784
    }
785
    else
786 787 788 789
    {
        if( !paused )
            input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
    }
790

791
    vlc_object_release( p_input_thread );
792
}
793

794 795 796 797 798 799 800 801 802 803 804
/**************************************************************************
 * Toggle pause.
 **************************************************************************/
void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
{
    libvlc_state_t state = libvlc_media_player_get_state( p_mi );
    bool playing = (state == libvlc_Playing || state == libvlc_Buffering);

    libvlc_media_player_set_pause( p_mi, playing );
}

805
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
806 807 808
 * Tells whether the media player is currently playing.
 *
 * Enter with lock held.
809
 **************************************************************************/
810
int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
811
{
812
    libvlc_state_t state = libvlc_media_player_get_state( p_mi );
813
    return (libvlc_Playing == state) || (libvlc_Buffering == state);
814 815
}

816
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
817
 * Stop playing.
818
 **************************************************************************/
819
void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
820
{
821
    libvlc_state_t state = libvlc_media_player_get_state( p_mi );
822

823
    lock_input(p_mi);
824 825 826 827 828
    release_input_thread( p_mi, true ); /* This will stop the input thread */

    /* Force to go to stopped state, in case we were in Ended, or Error
     * state. */
    if( state != libvlc_Stopped )
829
    {
830
        set_state( p_mi, libvlc_Stopped, false );
831 832 833

        /* Construct and send the event */
        libvlc_event_t event;
834
        event.type = libvlc_MediaPlayerStopped;
835 836
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
837

838
    input_resource_Terminate( p_mi->input.p_resource );
839
    unlock_input(p_mi);
840 841
}

842 843 844 845 846 847 848 849 850 851 852 853 854 855

void libvlc_video_set_callbacks( libvlc_media_player_t *mp,
    void *(*lock_cb) (void *, void **),
    void (*unlock_cb) (void *, void *, void *const *),
    void (*display_cb) (void *, void *),
    void *opaque )
{
    var_SetAddress( mp, "vmem-lock", lock_cb );
    var_SetAddress( mp, "vmem-unlock", unlock_cb );
    var_SetAddress( mp, "vmem-display", display_cb );
    var_SetAddress( mp, "vmem-data", opaque );
    var_SetString( mp, "vout", "vmem" );
}

856 857 858 859 860 861 862 863
void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
                                        libvlc_video_format_cb setup,
                                        libvlc_video_cleanup_cb cleanup )
{
    var_SetAddress( mp, "vmem-setup", setup );
    var_SetAddress( mp, "vmem-cleanup", cleanup );
}

864 865 866 867 868 869 870 871 872
void libvlc_video_set_format( libvlc_media_player_t *mp, const char *chroma,
                              unsigned width, unsigned height, unsigned pitch )
{
    var_SetString( mp, "vmem-chroma", chroma );
    var_SetInteger( mp, "vmem-width", width );
    var_SetInteger( mp, "vmem-height", height );
    var_SetInteger( mp, "vmem-pitch", pitch );
}

873 874 875 876
/**************************************************************************
 * set_nsobject
 **************************************************************************/
void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
877
                                        void * drawable )
878
{
879 880 881 882 883 884
    assert (p_mi != NULL);
#ifdef __APPLE__
    var_SetAddress (p_mi, "drawable-nsobject", drawable);
#else
    (void) p_mi; (void)drawable;
#endif
885 886 887
}

/**************************************************************************
888
 * get_nsobject
889
 **************************************************************************/
Pierre's avatar
Pierre committed
890
void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
891
{
892 893 894 895 896 897
    assert (p_mi != NULL);
#ifdef __APPLE__
    return var_GetAddress (p_mi, "drawable-nsobject");
#else
    return NULL;
#endif
898 899
}

900 901 902 903
/**************************************************************************
 * set_agl
 **************************************************************************/
void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
904
                                  uint32_t drawable )
905
{
906 907 908 909 910
#ifdef __APPLE__
    var_SetInteger (p_mi, "drawable-agl", drawable);
#else
    (void) p_mi; (void)drawable;
#endif
911 912 913 914 915 916 917
}

/**************************************************************************
 * get_agl
 **************************************************************************/
uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
{
918 919 920 921 922 923
    assert (p_mi != NULL);
#ifdef __APPLE__
    return var_GetInteger (p_mi, "drawable-agl");
#else
    return 0;
#endif
924 925
}

JP Dinger's avatar
JP Dinger committed
926 927 928
/**************************************************************************
 * set_xwindow
 **************************************************************************/
929
void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
930
                                      uint32_t drawable )
931
{
932
    assert (p_mi != NULL);
933 934

    var_SetString (p_mi, "vout", drawable ? "xid" : "any");
935
    var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "any");
936
    var_SetInteger (p_mi, "drawable-xid", drawable);
937 938
}

JP Dinger's avatar
JP Dinger committed
939 940 941
/**************************************************************************
 * get_xwindow
 **************************************************************************/
942 943
uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
{
944
    return var_GetInteger (p_mi, "drawable-xid");
945 946
}

JP Dinger's avatar
JP Dinger committed
947 948 949
/**************************************************************************
 * set_hwnd
 **************************************************************************/
950
void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
951
                                   void *drawable )
952
{
953
    assert (p_mi != NULL);
954
#if defined (_WIN32) || defined (__OS2__)
955 956
    var_SetString (p_mi, "window",
                   (drawable != NULL) ? "embed-hwnd,any" : "");
957
    var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
958 959 960
#else
    (void) p_mi; (void) drawable;
#endif
961 962
}

JP Dinger's avatar
JP Dinger committed
963 964 965
/**************************************************************************
 * get_hwnd
 **************************************************************************/
966 967
void *libvlc_media_player_get_hwnd( libvlc_media_player_t *p_mi )
{
968
    assert (p_mi != NULL);
969
#if defined (_WIN32) || defined (__OS2__)
970
    return (void *)(uintptr_t)var_GetInteger (p_mi, "drawable-hwnd");
971 972 973
#else
    return NULL;
#endif
974 975
}

976 977
void libvlc_audio_set_callbacks( libvlc_media_player_t *mp,
                                 libvlc_audio_play_cb play_cb,
978 979 980 981
                                 libvlc_audio_pause_cb pause_cb,
                                 libvlc_audio_resume_cb resume_cb,
                                 libvlc_audio_flush_cb flush_cb,
                                 libvlc_audio_drain_cb drain_cb,
982 983 984
                                 void *opaque )
{
    var_SetAddress( mp, "amem-play", play_cb );
985 986 987 988
    var_SetAddress( mp, "amem-pause", pause_cb );
    var_SetAddress( mp, "amem-resume", resume_cb );
    var_SetAddress( mp, "amem-flush", flush_cb );
    var_SetAddress( mp, "amem-drain", drain_cb );
989
    var_SetAddress( mp, "amem-data", opaque );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
990
    var_SetString( mp, "aout", "amem,none" );
991 992

    input_resource_ResetAout(mp->input.p_resource);
993 994
}

995 996 997 998
void libvlc_audio_set_volume_callback( libvlc_media_player_t *mp,
                                       libvlc_audio_set_volume_cb cb )
{
    var_SetAddress( mp, "amem-set-volume", cb );
999 1000

    input_resource_ResetAout(mp->input.p_resource);
1001 1002
}

1003 1004 1005 1006 1007 1008
void libvlc_audio_set_format_callbacks( libvlc_media_player_t *m