media_player.c 52.4 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 63 64 65 66 67 68 69 70 71 72 73 74 75
static int
input_es_changed( vlc_object_t * p_this, char const * psz_cmd,
                  int action, vlc_value_t *p_val,
                  void *p_userdata);

static int
input_es_selected( vlc_object_t * p_this, char const * psz_cmd,
                   vlc_value_t oldval, vlc_value_t newval,
                   void * p_userdata );

static void
add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi );

static void
del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi );

76 77 78
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 );
79

80
static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
81

82 83 84 85 86 87 88
/*
 * 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)
{
89
    libvlc_event_manager_register_event_type(mp->p_event_manager, type);
90 91
}

92 93 94 95 96 97 98 99 100
/*
 * 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.
 */
101 102 103 104 105 106 107 108 109 110
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);
}

111 112 113 114 115 116 117 118 119 120
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);
}

121
/*
JP Dinger's avatar
JP Dinger committed
122
 * Release the associated input thread.
123 124
 *
 * Object lock is NOT held.
125
 * Input lock is held or instance is being destroyed.
126
 */
127
static void release_input_thread( libvlc_media_player_t *p_mi, bool b_input_abort )
128
{
129
    assert( p_mi );
130

131
    input_thread_t *p_input_thread = p_mi->input.p_thread;
132
    if( !p_input_thread )
133
        return;
134
    p_mi->input.p_thread = NULL;
135

136 137 138 139
    var_DelCallback( p_input_thread, "can-seek",
                     input_seekable_changed, p_mi );
    var_DelCallback( p_input_thread, "can-pause",
                    input_pausable_changed, p_mi );
140 141
    var_DelCallback( p_input_thread, "program-scrambled",
                    input_scrambled_changed, p_mi );
142 143
    var_DelCallback( p_input_thread, "intf-event",
                     input_event_changed, p_mi );
144
    del_es_callbacks( p_input_thread, p_mi );
145 146 147

    /* We owned this one */
    input_Stop( p_input_thread, b_input_abort );
148
    input_Close( p_input_thread );
149 150
}

151 152
/*
 * Retrieve the input thread. Be sure to release the object
153
 * once you are done with it. (libvlc Internal)
154
 */
155
input_thread_t *libvlc_get_input_thread( libvlc_media_player_t *p_mi )
Clément Stenac's avatar
Clément Stenac committed
156 157 158
{
    input_thread_t *p_input_thread;

159
    assert( p_mi );
160

161 162
    lock_input(p_mi);
    p_input_thread = p_mi->input.p_thread;
163 164
    if( p_input_thread )
        vlc_object_hold( p_input_thread );
165 166
    else
        libvlc_printerr( "No active input" );
167
    unlock_input(p_mi);
168

169 170 171
    return p_input_thread;
}

172 173 174 175 176
/*
 * Set the internal state of the media_player. (media player Internal)
 *
 * Function will lock the media_player.
 */
177 178 179 180 181
static void set_state( libvlc_media_player_t *p_mi, libvlc_state_t state,
    bool b_locked )
{
    if(!b_locked)
        lock(p_mi);
182
    p_mi->state = state;
183

184 185 186
    libvlc_media_t *media = p_mi->p_md;
    if (media)
        libvlc_media_retain(media);
187

188 189
    if(!b_locked)
        unlock(p_mi);
190

191 192
    if (media)
    {
193 194 195
        // Also set the state of the corresponding media
        // This is strictly for convenience.
        libvlc_media_set_state(media, state);
196 197

        libvlc_media_release(media);
198 199 200
    }
}

201
static int
202
input_seekable_changed( vlc_object_t * p_this, char const * psz_cmd,
203 204 205 206
                        vlc_value_t oldval, vlc_value_t newval,
                        void * p_userdata )
{
    VLC_UNUSED(oldval);
207 208
    VLC_UNUSED(p_this);
    VLC_UNUSED(psz_cmd);
209
    libvlc_media_player_t * p_mi = p_userdata;
210 211
    libvlc_event_t event;

212
    event.type = libvlc_MediaPlayerSeekableChanged;
213
    event.u.media_player_seekable_changed.new_seekable = newval.b_bool;
214 215 216 217 218 219 220 221 222 223 224

    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);
225 226
    VLC_UNUSED(p_this);
    VLC_UNUSED(psz_cmd);
227
    libvlc_media_player_t * p_mi = p_userdata;
228 229
    libvlc_event_t event;

230
    event.type = libvlc_MediaPlayerPausableChanged;
231
    event.u.media_player_pausable_changed.new_pausable = newval.b_bool;
232 233 234 235 236

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

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
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;
}

255
static int
Laurent Aimar's avatar
Laurent Aimar committed
256
input_event_changed( vlc_object_t * p_this, char const * psz_cmd,
257 258 259
                     vlc_value_t oldval, vlc_value_t newval,
                     void * p_userdata )
{
260
    VLC_UNUSED(oldval);
Laurent Aimar's avatar
Laurent Aimar committed
261
    input_thread_t * p_input = (input_thread_t *)p_this;
262
    libvlc_media_player_t * p_mi = p_userdata;
Laurent Aimar's avatar
Laurent Aimar committed
263
    libvlc_event_t event;
264

265
    assert( !strcmp( psz_cmd, "intf-event" ) );
266

267 268
    if( newval.i_int == INPUT_EVENT_STATE )
    {
269
        libvlc_state_t libvlc_state;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

        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;
        }
301

302
        set_state( p_mi, libvlc_state, false );
303 304
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
305 306 307 308 309
    else if( newval.i_int == INPUT_EVENT_ABORT )
    {
        libvlc_state_t libvlc_state = libvlc_Stopped;
        event.type = libvlc_MediaPlayerStopped;

310
        set_state( p_mi, libvlc_state, false );
311 312
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
313
    else if( newval.i_int == INPUT_EVENT_POSITION )
314 315 316 317 318 319
    {
        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
320 321
        event.u.media_player_position_changed.new_position =
                                          var_GetFloat( p_input, "position" );
322 323 324 325
        libvlc_event_send( p_mi->p_event_manager, &event );

        /* */
        event.type = libvlc_MediaPlayerTimeChanged;
JP Dinger's avatar
JP Dinger committed
326
        event.u.media_player_time_changed.new_time =
327
           from_mtime(var_GetTime( p_input, "time" ));
328 329
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
330 331 332 333
    else if( newval.i_int == INPUT_EVENT_LENGTH )
    {
        event.type = libvlc_MediaPlayerLengthChanged;
        event.u.media_player_length_changed.new_length =
334
           from_mtime(var_GetTime( p_input, "length" ));
335 336
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
337 338 339 340 341 342 343
    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 );
    }
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    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
363

364 365 366
    return VLC_SUCCESS;
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
static int track_type_from_name(const char *psz_name)
{
   if( !strcmp( psz_name, "video-es" ) )
       return libvlc_track_video;
    else if( !strcmp( psz_name, "audio-es" ) )
        return libvlc_track_audio;
    else if( !strcmp( psz_name, "spu-es" ) )
        return libvlc_track_text;
    else
        return libvlc_track_unknown;
}

static int input_es_changed( vlc_object_t *p_this,
                             char const *psz_cmd,
                             int action,
                             vlc_value_t *p_val,
                             void *p_userdata )
{
    VLC_UNUSED(p_this);
    libvlc_media_player_t *mp = p_userdata;
    libvlc_event_t event;

    /* Ignore the "Disable" element */
    if (p_val && p_val->i_int < 0)
        return VLC_EGENERIC;

    switch (action)
    {
    case VLC_VAR_ADDCHOICE:
        event.type = libvlc_MediaPlayerESAdded;
        break;
    case VLC_VAR_DELCHOICE:
    case VLC_VAR_CLEARCHOICES:
        event.type = libvlc_MediaPlayerESDeleted;
        break;
    default:
        return VLC_EGENERIC;
    }

    event.u.media_player_es_changed.i_type = track_type_from_name(psz_cmd);

    int i_id;
    if (action != VLC_VAR_CLEARCHOICES)
    {
        if (!p_val)
            return VLC_EGENERIC;
        i_id = p_val->i_int;
    }
    else
    {
        /* -1 means all ES tracks of this type were deleted. */
        i_id = -1;
    }
    event.u.media_player_es_changed.i_id = i_id;

    libvlc_event_send(mp->p_event_manager, &event);

    return VLC_SUCCESS;
}

static int
input_es_selected( vlc_object_t * p_this, char const * psz_cmd,
                   vlc_value_t oldval, vlc_value_t newval,
                   void * p_userdata )
{
    VLC_UNUSED(p_this);
    VLC_UNUSED(oldval);
    libvlc_media_player_t *mp = p_userdata;
    libvlc_event_t event;

    event.type = libvlc_MediaPlayerESSelected;
    event.u.media_player_es_changed.i_type = track_type_from_name(psz_cmd);
    event.u.media_player_es_changed.i_id = newval.i_int;

    libvlc_event_send(mp->p_event_manager, &event);

    return VLC_SUCCESS;
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
/**************************************************************************
 * 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;
}

465
/* */
466 467
static void libvlc_media_player_destroy( libvlc_media_player_t * );

468

469
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
470 471 472 473 474 475 476 477 478 479 480 481
 * 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.
482
 **************************************************************************/
483
libvlc_media_player_t *
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
484
libvlc_media_player_new( libvlc_instance_t *instance )
485
{
486
    libvlc_media_player_t * mp;
487

488
    assert(instance);
489

490 491
    mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
    if (unlikely(mp == NULL))
492
    {
493
        libvlc_printerr("Not enough memory");
494 495
        return NULL;
    }
496

497 498 499
    /* Input */
    var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);

500
    /* Video */
501
    var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
502
    var_Create (mp, "window", VLC_VAR_STRING);
503 504 505 506
    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);
507 508
    var_Create (mp, "vmem-setup", VLC_VAR_ADDRESS);
    var_Create (mp, "vmem-cleanup", VLC_VAR_ADDRESS);
509 510 511
    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);
512
    var_Create (mp, "vmem-pitch", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
513
    var_Create (mp, "avcodec-hw", VLC_VAR_STRING);
514
    var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
515
#if defined (_WIN32) || defined (__OS2__)
516
    var_Create (mp, "drawable-hwnd", VLC_VAR_INTEGER);
517 518 519 520 521 522 523 524 525
#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);
526
    var_SetBool (mp, "mouse-events", true);
527

528 529 530 531 532 533 534 535 536
    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);
537

538
    var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
539

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
    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);

558 559
    var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
    var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
560
    var_Create (mp, "hue", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
561 562 563
    var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
    var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);

564
     /* Audio */
565
    var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
566
    var_Create (mp, "mute", VLC_VAR_BOOL);
567
    var_Create (mp, "volume", VLC_VAR_FLOAT);
568
    var_Create (mp, "corks", VLC_VAR_INTEGER);
569
    var_Create (mp, "audio-filter", VLC_VAR_STRING);
570 571
    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
572
    var_Create (mp, "amem-cleanup", VLC_VAR_ADDRESS);
573
    var_Create (mp, "amem-play", VLC_VAR_ADDRESS);
574 575 576 577
    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);
578 579 580 581
    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);
582

583 584 585 586 587
    /* 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
588 589
    /* Equalizer */
    var_Create (mp, "equalizer-preamp", VLC_VAR_FLOAT);
590
    var_Create (mp, "equalizer-vlcfreqs", VLC_VAR_BOOL);
Mark Lee's avatar
Mark Lee committed
591 592
    var_Create (mp, "equalizer-bands", VLC_VAR_STRING);

593
    mp->p_md = NULL;
594
    mp->state = libvlc_NothingSpecial;
595
    mp->p_libvlc_instance = instance;
596
    mp->input.p_thread = NULL;
597 598 599 600 601 602
    mp->input.p_resource = input_resource_New(VLC_OBJECT(mp));
    if (unlikely(mp->input.p_resource == NULL))
    {
        vlc_object_release(mp);
        return NULL;
    }
603 604 605 606
    audio_output_t *aout = input_resource_GetAout(mp->input.p_resource);
    if( aout != NULL )
        input_resource_PutAout(mp->input.p_resource, aout);

607
    vlc_mutex_init (&mp->input.lock);
608
    mp->i_refcount = 1;
609 610
    mp->p_event_manager = libvlc_event_manager_new(mp, instance);
    if (unlikely(mp->p_event_manager == NULL))
611
    {
612
        input_resource_Release(mp->input.p_resource);
613
        vlc_object_release(mp);
614 615
        return NULL;
    }
616
    vlc_mutex_init(&mp->object_lock);
617

618 619 620 621 622 623 624 625 626 627
    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);
628
    register_event(mp, SeekableChanged);
629 630 631 632 633 634

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

636
    register_event(mp, Vout);
637
    register_event(mp, ScrambledChanged);
638 639 640
    register_event(mp, ESAdded);
    register_event(mp, ESDeleted);
    register_event(mp, ESSelected);
641

642
    /* Snapshot initialization */
643
    register_event(mp, SnapshotTaken);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
644

645 646
    register_event(mp, MediaChanged);

647
    /* Attach a var callback to the global object to provide the glue between
648 649 650 651 652 653
     * 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.
     */
654
    var_AddCallback(mp->p_libvlc, "snapshot-file", snapshot_was_taken, mp);
655

656
    libvlc_retain(instance);
657
    return mp;
658 659 660
}

/**************************************************************************
JP Dinger's avatar
JP Dinger committed
661
 * Create a Media Instance object with a media descriptor.
662
 **************************************************************************/
663
libvlc_media_player_t *
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
664
libvlc_media_player_new_from_media( libvlc_media_t * p_md )
665
{
666
    libvlc_media_player_t * p_mi;
667

Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
668
    p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
669
    if( !p_mi )
670 671
        return NULL;

672
    libvlc_media_retain( p_md );
673
    p_mi->p_md = p_md;
674 675 676 677 678

    return p_mi;
}

/**************************************************************************
679
 * Destroy a Media Instance object (libvlc internal)
680
 *
JP Dinger's avatar
JP Dinger committed
681
 * Warning: No lock held here, but hey, this is internal. Caller must lock.
682
 **************************************************************************/
683
static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
684
{
685
    assert( p_mi );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
686

687
    /* Detach Callback from the main libvlc object */
688
    var_DelCallback( p_mi->p_libvlc,
689
                     "snapshot-file", snapshot_was_taken, p_mi );
690

691 692
    /* No need for lock_input() because no other threads knows us anymore */
    if( p_mi->input.p_thread )
693
        release_input_thread(p_mi, true);
694 695
    input_resource_Terminate( p_mi->input.p_resource );
    input_resource_Release( p_mi->input.p_resource );
696
    vlc_mutex_destroy( &p_mi->input.lock );
697

698
    libvlc_event_manager_release( p_mi->p_event_manager );
699
    libvlc_media_release( p_mi->p_md );
700
    vlc_mutex_destroy( &p_mi->object_lock );
701 702

    libvlc_instance_t *instance = p_mi->p_libvlc_instance;
703
    vlc_object_release( p_mi );
704
    libvlc_release(instance);
705 706 707
}

/**************************************************************************
JP Dinger's avatar
JP Dinger committed
708 709 710
 * Release a Media Instance object.
 *
 * Function does the locking.
711
 **************************************************************************/
712
void libvlc_media_player_release( libvlc_media_player_t *p_mi )
713
{
714
    bool destroy;
715

716
    assert( p_mi );
717
    lock(p_mi);
718
    destroy = !--p_mi->i_refcount;
719
    unlock(p_mi);
720

721 722
    if( destroy )
        libvlc_media_player_destroy( p_mi );
723
}
724

725
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
726 727 728
 * Retain a Media Instance object.
 *
 * Caller must hold the lock.
729
 **************************************************************************/
730
void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
731
{
732
    assert( p_mi );
733

734
    lock(p_mi);
735
    p_mi->i_refcount++;
736
    unlock(p_mi);
737
}
738

739
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
740 741 742
 * Set the Media descriptor associated with the instance.
 *
 * Enter without lock -- function will lock the object.
743
 **************************************************************************/
744 745
void libvlc_media_player_set_media(
                            libvlc_media_player_t *p_mi,
746
                            libvlc_media_t *p_md )
747
{
748
    lock_input(p_mi);
749

750 751 752
    /* FIXME I am not sure if it is a user request or on die(eof/error)
     * request here */
    release_input_thread( p_mi,
753 754 755
                          p_mi->input.p_thread &&
                          !p_mi->input.p_thread->b_eof &&
                          !p_mi->input.p_thread->b_error );
756

757
    lock( p_mi );
758
    set_state( p_mi, libvlc_NothingSpecial, true );
759
    unlock_input( p_mi );
760

761
    libvlc_media_release( p_mi->p_md );
762

763 764 765
    if( !p_md )
    {
        p_mi->p_md = NULL;
766
        unlock(p_mi);
767 768 769
        return; /* It is ok to pass a NULL md */
    }

770
    libvlc_media_retain( p_md );
771
    p_mi->p_md = p_md;
772

773 774 775 776
    /* 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;

777
    unlock(p_mi);
778 779 780 781 782 783

    /* 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 );
784

785 786 787
}

/**************************************************************************
JP Dinger's avatar
JP Dinger committed
788
 * Get the Media descriptor associated with the instance.
789
 **************************************************************************/
790
libvlc_media_t *
791
libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
792
{
793
    libvlc_media_t *p_m;
794

795
    lock( p_mi );
796 797
    p_m = p_mi->p_md;
    if( p_m )
798 799 800 801
        libvlc_media_retain( p_m );
    unlock( p_mi );

    return p_m;
802 803
}

804
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
805
 * Get the event Manager.
806 807
 **************************************************************************/
libvlc_event_manager_t *
808
libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
809 810 811 812
{
    return p_mi->p_event_manager;
}

813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
static void add_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
{
    var_AddListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
    var_AddListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
    var_AddListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
    var_AddCallback( p_input_thread, "video-es", input_es_selected, p_mi );
    var_AddCallback( p_input_thread, "audio-es", input_es_selected, p_mi );
    var_AddCallback( p_input_thread, "spu-es", input_es_selected, p_mi );
}

static void del_es_callbacks( input_thread_t *p_input_thread, libvlc_media_player_t *p_mi )
{
    var_DelListCallback( p_input_thread, "video-es", input_es_changed, p_mi );
    var_DelListCallback( p_input_thread, "audio-es", input_es_changed, p_mi );
    var_DelListCallback( p_input_thread, "spu-es", input_es_changed, p_mi );
    var_DelCallback( p_input_thread, "video-es", input_es_selected, p_mi );
    var_DelCallback( p_input_thread, "audio-es", input_es_selected, p_mi );
    var_DelCallback( p_input_thread, "spu-es", input_es_selected, p_mi );
}

833
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
834
 * Tell media player to start playing.
835
 **************************************************************************/
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
836
int libvlc_media_player_play( libvlc_media_player_t *p_mi )
837
{
838
    lock_input( p_mi );
839

840 841
    input_thread_t *p_input_thread = p_mi->input.p_thread;
    if( p_input_thread )
842
    {
Sam Hocevar's avatar
Sam Hocevar committed
843
        /* A thread already exists, send it a play message */
844
        input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
845
        unlock_input( p_mi );
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
846
        return 0;
847 848
    }

849
    /* Ignore previous exception */
850
    lock(p_mi);
851

852 853
    if( !p_mi->p_md )
    {
854
        unlock(p_mi);
855
        unlock_input( p_mi );
856
        libvlc_printerr( "No associated media descriptor" );
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
857
        return -1;
858 859
    }

860 861 862 863
    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 )
864
    {
865
        unlock_input(p_mi);
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
866 867
        libvlc_printerr( "Not enough memory" );
        return -1;
868
    }
869

870 871
    var_AddCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
    var_AddCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
872
    var_AddCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
Laurent Aimar's avatar
Laurent Aimar committed
873
    var_AddCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
874
    add_es_callbacks( p_input_thread, p_mi );
875

876 877
    if( input_Start( p_input_thread ) )
    {
878
        unlock_input(p_mi);
879
        del_es_callbacks( p_input_thread, p_mi );
880 881
        var_DelCallback( p_input_thread, "intf-event", input_event_changed, p_mi );
        var_DelCallback( p_input_thread, "can-pause", input_pausable_changed, p_mi );
882
        var_DelCallback( p_input_thread, "program-scrambled", input_scrambled_changed, p_mi );
883
        var_DelCallback( p_input_thread, "can-seek", input_seekable_changed, p_mi );
884
        vlc_object_release( p_input_thread );
885 886
        libvlc_printerr( "Input initialization failure" );
        return -1;
887
    }
888 889
    p_mi->input.p_thread = p_input_thread;
    unlock_input(p_mi);
Rémi Denis-Courmont's avatar
good  
Rémi Denis-Courmont committed
890
    return 0;
891 892
}

893
void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
894
{
895
    input_thread_t * p_input_thread = libvlc_get_input_thread( p_mi );
896
    if( !p_input_thread )
897 898
        return;

899
    libvlc_state_t state = libvlc_media_player_get_state( p_mi );
900
    if( state == libvlc_Playing || state == libvlc_Buffering )
901
    {
902 903 904 905 906
        if( paused )
        {
            if( libvlc_media_player_can_pause( p_mi ) )
                input_Control( p_input_thread, INPUT_SET_STATE, PAUSE_S );
            else
907
                input_Stop( p_input_thread, true );
908
        }
909
    }
910
    else
911 912 913 914
    {
        if( !paused )
            input_Control( p_input_thread, INPUT_SET_STATE, PLAYING_S );
    }
915

916
    vlc_object_release( p_input_thread );
917
}
918

919 920 921 922 923 924 925 926 927 928 929
/**************************************************************************
 * 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 );
}

930
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
931 932 933
 * Tells whether the media player is currently playing.
 *
 * Enter with lock held.
934
 **************************************************************************/
935
int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
936
{
937
    libvlc_state_t state = libvlc_media_player_get_state( p_mi );
938
    return (libvlc_Playing == state) || (libvlc_Buffering == state);
939 940
}

941
/**************************************************************************
JP Dinger's avatar
JP Dinger committed
942
 * Stop playing.
943
 **************************************************************************/
944
void libvlc_media_player_stop( libvlc_media_player_t *p_mi )
945
{
946
    libvlc_state_t state = libvlc_media_player_get_state( p_mi );
947

948
    lock_input(p_mi);
949 950 951 952 953
    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 )
954
    {
955
        set_state( p_mi, libvlc_Stopped, false );
956 957 958

        /* Construct and send the event */
        libvlc_event_t event;
959
        event.type = libvlc_MediaPlayerStopped;
960 961
        libvlc_event_send( p_mi->p_event_manager, &event );
    }
962

963
    input_resource_Terminate( p_mi->input.p_resource );
964
    unlock_input(p_mi);
965 966
}

967 968 969 970 971 972 973 974 975 976 977 978

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" );
979
    var_SetString( mp, "avcodec-hw", "none" );
980 981
}

982 983 984 985 986 987 988 989
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 );
}

990 991 992 993 994 995 996 997 998
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 );
}

999 1000 1001 1002
/**************************************************************************
 * set_nsobject
 **************************************************************************/
void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
1003
                                        void * drawable )
1004
{
1005 1006 1007 1008 1009 1010
    assert (p_mi != NULL);
#ifdef __APPLE__
    var_SetAddress (p_mi, "drawable-nsobject", drawable);
#else
    (void) p_mi; (void)drawable;
#endif
1011 1012 1013
}

/**************************************************************************
1014
 * get_nsobject
1015
 **************************************************************************/
Pierre's avatar
Pierre committed
1016
void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
1017
{
1018 1019 1020 1021 1022 1023
    assert (p_mi != NULL);
#ifdef __APPLE__
    return var_GetAddress (p_mi, "drawable-nsobject");
#else
    return NULL;
#endif
1024 1025
}

1026 1027 1028 1029
/**************************************************************************
 * set_agl
 **************************************************************************/
void libvlc_media_player_set_agl( libvlc_media_player_t *p_mi,
1030
                                  uint32_t drawable )
1031
{
1032 1033 1034 1035 1036
#ifdef __APPLE__
    var_SetInteger (p_mi, "drawable-agl", drawable);
#else
    (void) p_mi; (void)drawable;
#endif
1037 1038 1039 1040 1041 1042 1043
}

/**************************************************************************
 * get_agl
 **************************************************************************/
uint32_t libvlc_media_player_get_agl( libvlc_media_player_t *p_mi )
{
1044 1045 1046 1047 1048 1049
    assert (p_mi != NULL);
#ifdef __APPLE__
    return var_GetInteger (p_mi, "drawable-agl");
#else
    return 0;
#endif
1050 1051
}

JP Dinger's avatar
JP Dinger committed
1052 1053 1054
/**************************************************************************
 * set_xwindow
 **************************************************************************/
1055
void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
1056
                                      uint32_t drawable )
1057
{
1058
    assert (p_mi != NULL);
1059

1060
    var_SetString (p_mi, "avcodec-hw", "");
1061
    var_SetString (p_mi, "vout", drawable ? "xid" : "any");
1062
    var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "any");
1063
    var_SetInteger (p_mi, "drawable-xid", drawable);
1064 1065