media_player.c 60.3 KB
Newer Older
1
/*****************************************************************************
2
 * media_player.c: Libvlc API Media Instance management functions
3
 *****************************************************************************
4
 * Copyright (C) 2005-2015 VLC authors and VideoLAN
5
 *
6
 * Authors: Clément Stenac <zorglub@videolan.org>
7
 *
Jean-Baptiste Kempf's avatar
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
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
Jean-Baptiste Kempf committed
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
17
 *
Jean-Baptiste Kempf's avatar
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.
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

29
#include <vlc/libvlc.h>
30
#include <vlc/libvlc_renderer_discoverer.h>
31
#include <vlc/libvlc_picture.h>
32 33 34
#include <vlc/libvlc_media.h>
#include <vlc/libvlc_events.h>

35
#include <vlc_demux.h>
36
#include <vlc_vout.h>
Mark Lee's avatar
Mark Lee committed
37
#include <vlc_aout.h>
38
#include <vlc_actions.h>
39
#include <vlc_http.h>
40 41 42 43

#include "libvlc_internal.h"
#include "media_internal.h" // libvlc_media_set_state()
#include "media_player_internal.h"
44
#include "renderer_discoverer_internal.h"
45

46 47
#define ES_INIT (-2) /* -1 is reserved for ES deselect */

48
static int
49 50
snapshot_was_taken( vlc_object_t *p_this, char const *psz_cmd,
                    vlc_value_t oldval, vlc_value_t newval, void *p_data );
51

52 53
static void media_attach_preparsed_event(libvlc_media_t *);
static void media_detach_preparsed_event(libvlc_media_t *);
54

55
static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi );
56

57
// player callbacks
58

59
static void
60 61 62 63
on_current_media_changed(vlc_player_t *player, input_item_t *new_media,
                         void *data)
{
    (void) player;
64

65
    libvlc_media_player_t *mp = data;
66

67
    libvlc_media_t *md = mp->p_md;
68

69 70 71 72
    input_item_t *media = md ? md->p_input_item : NULL;
    if (new_media == media)
        /* no changes */
        return;
73

74 75
    if (md)
        media_detach_preparsed_event(md);
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    if (new_media)
    {
        mp->p_md = libvlc_media_new_from_input_item(mp->p_libvlc_instance,
                                                    new_media);
        if (!mp->p_md)
            /* error already printed by the function call */
            return;

        media_attach_preparsed_event(mp->p_md);
    }
    else
        mp->p_md = NULL;

    libvlc_media_release(md);

    libvlc_event_t event;
    event.type = libvlc_MediaPlayerMediaChanged;
    event.u.media_player_media_changed.new_media = mp->p_md;
    libvlc_event_send(&mp->event_manager, &event);
96 97
}

98 99 100
static void
on_state_changed(vlc_player_t *player, enum vlc_player_state new_state,
                 void *data)
101
{
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    (void) player;

    libvlc_media_player_t *mp = data;

    libvlc_event_t event;
    switch (new_state) {
        case VLC_PLAYER_STATE_STOPPED:
            event.type = libvlc_MediaPlayerStopped;
            break;
        case VLC_PLAYER_STATE_STOPPING:
            event.type = libvlc_MediaPlayerEndReached;
            break;
        case VLC_PLAYER_STATE_STARTED:
            event.type = libvlc_MediaPlayerOpening;
            break;
        case VLC_PLAYER_STATE_PLAYING:
            event.type = libvlc_MediaPlayerPlaying;
            break;
        case VLC_PLAYER_STATE_PAUSED:
            event.type = libvlc_MediaPlayerPaused;
            break;
        default:
            vlc_assert_unreachable();
    }

    libvlc_event_send(&mp->event_manager, &event);
128 129
}

130 131
static void
on_error_changed(vlc_player_t *player, enum vlc_player_error error, void *data)
132
{
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    (void) player;

    libvlc_media_player_t *mp = data;

    libvlc_event_t event;
    switch (error) {
        case VLC_PLAYER_ERROR_NONE:
            event.type = libvlc_MediaPlayerNothingSpecial;
            break;
        case VLC_PLAYER_ERROR_GENERIC:
            event.type = libvlc_MediaPlayerEncounteredError;
            break;
        default:
            vlc_assert_unreachable();
    }

    libvlc_event_send(&mp->event_manager, &event);
150 151
}

152 153
static void
on_buffering_changed(vlc_player_t *player, float new_buffering, void *data)
154
{
155 156 157 158 159 160 161 162 163
    (void) player;

    libvlc_media_player_t *mp = data;

    libvlc_event_t event;
    event.type = libvlc_MediaPlayerBuffering;
    event.u.media_player_buffering.new_cache = 100 * new_buffering;

    libvlc_event_send(&mp->event_manager, &event);
164 165
}

166 167
static void
on_capabilities_changed(vlc_player_t *player, int old_caps, int new_caps, void *data)
168
{
169 170 171 172 173 174
    (void) player;

    libvlc_media_player_t *mp = data;

    libvlc_event_t event;

175 176
    bool old_seekable = old_caps & VLC_PLAYER_CAP_SEEK;
    bool new_seekable = new_caps & VLC_PLAYER_CAP_SEEK;
177
    if (new_seekable != old_seekable)
178
    {
179 180 181
        event.type = libvlc_MediaPlayerSeekableChanged;
        event.u.media_player_seekable_changed.new_seekable = new_seekable;
        libvlc_event_send(&mp->event_manager, &event);
182 183
    }

184 185
    bool old_pauseable = old_caps & VLC_PLAYER_CAP_PAUSE;
    bool new_pauseable = new_caps & VLC_PLAYER_CAP_PAUSE;
186 187 188 189 190 191
    if (new_pauseable != old_pauseable)
    {
        event.type = libvlc_MediaPlayerPausableChanged;
        event.u.media_player_pausable_changed.new_pausable = new_pauseable;
        libvlc_event_send(&mp->event_manager, &event);
    }
192 193
}

194 195 196
static void
on_position_changed(vlc_player_t *player, vlc_tick_t new_time, float new_pos,
                    void *data)
197
{
198 199 200 201 202 203 204 205 206 207 208 209 210
    (void) player;

    libvlc_media_player_t *mp = data;

    libvlc_event_t event;

    event.type = libvlc_MediaPlayerPositionChanged;
    event.u.media_player_position_changed.new_position = new_pos;
    libvlc_event_send(&mp->event_manager, &event);

    event.type = libvlc_MediaPlayerTimeChanged;
    event.u.media_player_time_changed.new_time = MS_FROM_VLC_TICK(new_time);
    libvlc_event_send(&mp->event_manager, &event);
211 212
}

213 214
static void
on_length_changed(vlc_player_t *player, vlc_tick_t new_length, void *data)
215
{
216
    (void) player;
217

218
    libvlc_media_player_t *mp = data;
219

220
    libvlc_event_t event;
221

222 223 224
    event.type = libvlc_MediaPlayerLengthChanged;
    event.u.media_player_length_changed.new_length =
        MS_FROM_VLC_TICK(new_length);
225

226
    libvlc_event_send(&mp->event_manager, &event);
227 228
}

229 230
static int
track_type_from_cat(enum es_format_category_e cat)
231
{
232 233 234 235 236 237 238 239 240 241 242 243
    switch (cat)
    {
        case VIDEO_ES:
            return libvlc_track_video;
        case AUDIO_ES:
            return libvlc_track_audio;
        case SPU_ES:
            return libvlc_track_text;
        default:
            return libvlc_track_unknown;
    }
}
244

245 246 247 248 249 250 251
static void
on_track_list_changed(vlc_player_t *player, enum vlc_player_list_action action,
                      const struct vlc_player_track *track, void *data)
{
    (void) player;

    libvlc_media_player_t *mp = data;
252

253 254 255 256 257
    libvlc_event_t event;
    if (action == VLC_PLAYER_LIST_ADDED)
        event.type = libvlc_MediaPlayerESAdded;
    else if (action == VLC_PLAYER_LIST_REMOVED)
        event.type = libvlc_MediaPlayerESDeleted;
258
    else
259 260
        /* no event to forward */
        return;
261

262 263 264 265 266
    event.u.media_player_es_changed.i_type =
        track_type_from_cat(track->fmt.i_cat);
    event.u.media_player_es_changed.i_id = vlc_es_id_GetInputId(track->es_id);

    libvlc_event_send(&mp->event_manager, &event);
267 268
}

269 270 271
static void
on_track_selection_changed(vlc_player_t *player, vlc_es_id_t *unselected_id,
                           vlc_es_id_t *selected_id, void *data)
272
{
273 274
    (void) player;
    (void) unselected_id;
275

276
    libvlc_media_player_t *mp = data;
277

278 279
    libvlc_event_t event;
    event.type = libvlc_MediaPlayerESSelected;
280

281
    if (selected_id)
282
    {
283 284 285 286 287
        enum es_format_category_e cat = vlc_es_id_GetCat(selected_id);
        event.u.media_player_es_changed.i_type = track_type_from_cat(cat);
        event.u.media_player_es_changed.i_id =
            vlc_es_id_GetInputId(selected_id);
        libvlc_event_send(&mp->event_manager, &event);
288 289 290
    }
}

291 292 293 294 295 296 297
static void
on_program_list_changed(vlc_player_t *player,
                        enum vlc_player_list_action action,
                        const struct vlc_player_program *prgm, void* data)
{
    (void) action;
    (void) prgm;
298

299
    libvlc_media_player_t *mp = data;
300

301 302 303 304
    const struct vlc_player_program *selected =
        vlc_player_GetSelectedProgram(player);
    if (!selected)
        return;
305 306

    libvlc_event_t event;
307 308
    event.type = libvlc_MediaPlayerScrambledChanged;
    event.u.media_player_scrambled_changed.new_scrambled = selected->scrambled;
309

310
    libvlc_event_send(&mp->event_manager, &event);
311 312
}

313 314 315 316 317
static void
on_program_selection_changed(vlc_player_t *player, int unselected_id,
                             int selected_id, void *data)
{
    (void) unselected_id;
318

319 320 321 322 323 324 325 326 327 328
    libvlc_media_player_t *mp = data;

    if (selected_id == -1)
        return;

    const struct vlc_player_program *program =
        vlc_player_GetSelectedProgram(player);
    assert(program);

    libvlc_event_t event;
329
    event.type = libvlc_MediaPlayerScrambledChanged;
330
    event.u.media_player_scrambled_changed.new_scrambled = program->scrambled;
331

332
    libvlc_event_send(&mp->event_manager, &event);
333 334
}

335 336 337 338
static void
on_title_selection_changed(vlc_player_t *player,
                           const struct vlc_player_title *new_title,
                           size_t new_idx, void *data)
339
{
340 341 342 343 344
    (void) player;
    (void) new_title;

    libvlc_media_player_t *mp = data;

Laurent Aimar's avatar
Laurent Aimar committed
345
    libvlc_event_t event;
346 347
    event.type = libvlc_MediaPlayerTitleChanged;
    event.u.media_player_title_changed.new_title = new_idx;
348

349 350
    libvlc_event_send(&mp->event_manager, &event);
}
351

352 353 354 355 356 357 358 359 360 361 362 363
static void
on_chapter_selection_changed(vlc_player_t *player,
                             const struct vlc_player_title *title,
                             size_t title_idx,
                             const struct vlc_player_chapter *new_chapter,
                             size_t new_chapter_idx,
                             void *data)
{
    (void) player;
    (void) title;
    (void) title_idx;
    (void) new_chapter;
364

365
    libvlc_media_player_t *mp = data;
366

367 368 369
    libvlc_event_t event;
    event.type = libvlc_MediaPlayerChapterChanged;
    event.u.media_player_chapter_changed.new_chapter = new_chapter_idx;
370

371 372
    libvlc_event_send(&mp->event_manager, &event);
}
373

374 375 376 377 378
static void
on_media_subitems_changed(vlc_player_t *player, input_item_t *media,
                          input_item_node_t *new_subitems, void *data)
{
    (void) player;
379

380
    libvlc_media_player_t *mp = data;
Laurent Aimar's avatar
Laurent Aimar committed
381

382 383 384
    input_item_t *current = mp->p_md ? mp->p_md->p_input_item : NULL;
    if (media == current)
        libvlc_media_add_subtree(mp->p_md, new_subitems);
385 386
}

387 388
static void
on_cork_changed(vlc_player_t *player, unsigned cork_count, void *data)
389
{
390 391 392 393 394 395 396 397 398
    (void) player;

    libvlc_media_player_t *mp = data;

    libvlc_event_t event;
    event.type = cork_count ? libvlc_MediaPlayerCorked
                            : libvlc_MediaPlayerUncorked;

    libvlc_event_send(&mp->event_manager, &event);
399 400
}

401 402
static void
on_vout_changed(vlc_player_t *player, enum vlc_player_vout_action action,
403 404
                vout_thread_t *vout, enum vlc_vout_order order,
                vlc_es_id_t *es_id, void *data)
405
{
406 407
    (void) action;
    (void) vout;
408 409 410 411
    (void) order;

    if (vlc_es_id_GetCat(es_id) != VIDEO_ES)
        return;
412 413 414 415 416 417 418 419 420 421 422

    libvlc_media_player_t *mp = data;

    size_t count;
    vout_thread_t **vouts = vlc_player_vout_HoldAll(player, &count);
    if (!vouts)
        return;
    for (size_t i = 0; i < count; ++i)
        vout_Release(vouts[i]);
    free(vouts);

423
    libvlc_event_t event;
424 425
    event.type = libvlc_MediaPlayerVout;
    event.u.media_player_vout.new_count = count;
426

427 428
    libvlc_event_send(&mp->event_manager, &event);
}
429

430
// player aout callbacks
431

432
static void
433
on_volume_changed(audio_output_t *aout, float new_volume, void *data)
434
{
435
    (void) aout;
436

437 438 439 440 441
    libvlc_media_player_t *mp = data;

    libvlc_event_t event;
    event.type = libvlc_MediaPlayerAudioVolume;
    event.u.media_player_audio_volume.volume = new_volume;
442

443
    libvlc_event_send(&mp->event_manager, &event);
444
}
445

446
static void
447
on_mute_changed(audio_output_t *aout, bool new_muted, void *data)
448
{
449
    (void) aout;
450 451 452 453 454 455 456 457

    libvlc_media_player_t *mp = data;

    libvlc_event_t event;
    event.type = new_muted ? libvlc_MediaPlayerMuted
                           : libvlc_MediaPlayerUnmuted;

    libvlc_event_send(&mp->event_manager, &event);
458 459
}

460
static void
461
on_audio_device_changed(audio_output_t *aout, const char *device, void *data)
462
{
463
    (void) aout;
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

    libvlc_media_player_t *mp = data;

    libvlc_event_t event;
    event.type = libvlc_MediaPlayerAudioDevice;
    event.u.media_player_audio_device.device = device;

    libvlc_event_send(&mp->event_manager, &event);
}

static const struct vlc_player_cbs vlc_player_cbs = {
    .on_current_media_changed = on_current_media_changed,
    .on_state_changed = on_state_changed,
    .on_error_changed = on_error_changed,
    .on_buffering_changed = on_buffering_changed,
    .on_capabilities_changed = on_capabilities_changed,
    .on_position_changed = on_position_changed,
    .on_length_changed = on_length_changed,
    .on_track_list_changed = on_track_list_changed,
    .on_track_selection_changed = on_track_selection_changed,
    .on_program_list_changed = on_program_list_changed,
    .on_program_selection_changed = on_program_selection_changed,
    .on_title_selection_changed = on_title_selection_changed,
    .on_chapter_selection_changed = on_chapter_selection_changed,
    .on_media_subitems_changed = on_media_subitems_changed,
    .on_cork_changed = on_cork_changed,
    .on_vout_changed = on_vout_changed,
};

static const struct vlc_player_aout_cbs vlc_player_aout_cbs = {
    .on_volume_changed = on_volume_changed,
    .on_mute_changed = on_mute_changed,
    .on_device_changed = on_audio_device_changed,
};

499 500 501 502 503 504 505 506 507 508 509 510 511 512
/**************************************************************************
 * 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;
513
    libvlc_event_send(&mp->event_manager, &event);
514 515 516 517

    return VLC_SUCCESS;
}

518 519
static void input_item_preparsed_changed( const vlc_event_t *p_event,
                                          void * user_data )
520
{
521 522
    libvlc_media_t *p_md = user_data;
    if( p_event->u.input_item_preparsed_changed.new_status & ITEM_PREPARSED )
523
    {
524
        /* Send the event */
525
        libvlc_event_t event;
526 527 528
        event.type = libvlc_MediaParsedChanged;
        event.u.media_parsed_changed.new_status = libvlc_media_parsed_status_done;
        libvlc_event_send( &p_md->event_manager, &event );
529 530 531
    }
}

532
static void media_attach_preparsed_event( libvlc_media_t *p_md )
533
{
534 535 536
    vlc_event_attach( &p_md->p_input_item->event_manager,
                      vlc_InputItemPreparsedChanged,
                      input_item_preparsed_changed, p_md );
537 538
}

539
static void media_detach_preparsed_event( libvlc_media_t *p_md )
540
{
541 542 543 544
    vlc_event_detach( &p_md->p_input_item->event_manager,
                      vlc_InputItemPreparsedChanged,
                      input_item_preparsed_changed,
                      p_md );
545 546
}

547
/**************************************************************************
548 549 550 551 552 553 554 555 556 557 558 559
 * 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.
560
 **************************************************************************/
561
libvlc_media_player_t *
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
562
libvlc_media_player_new( libvlc_instance_t *instance )
563
{
564
    libvlc_media_player_t * mp;
565

566
    assert(instance);
567

568 569
    mp = vlc_object_create (instance->p_libvlc_int, sizeof(*mp));
    if (unlikely(mp == NULL))
570
    {
571
        libvlc_printerr("Not enough memory");
572 573
        return NULL;
    }
574

575 576
    /* Input */
    var_Create (mp, "rate", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT);
577 578
    var_Create (mp, "sout", VLC_VAR_STRING);
    var_Create (mp, "demux-filter", VLC_VAR_STRING);
579

580
    /* Video */
581
    var_Create (mp, "vout", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
582
    var_Create (mp, "window", VLC_VAR_STRING);
583 584
    var_Create (mp, "gl", VLC_VAR_STRING);
    var_Create (mp, "gles2", VLC_VAR_STRING);
585 586 587 588
    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);
589 590
    var_Create (mp, "vmem-setup", VLC_VAR_ADDRESS);
    var_Create (mp, "vmem-cleanup", VLC_VAR_ADDRESS);
591 592 593
    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);
594
    var_Create (mp, "vmem-pitch", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
595

596 597 598
    var_Create( mp, "vout-cb-opaque", VLC_VAR_ADDRESS );
    var_Create( mp, "vout-cb-setup", VLC_VAR_ADDRESS );
    var_Create( mp, "vout-cb-cleanup", VLC_VAR_ADDRESS );
599
    var_Create( mp, "vout-cb-resize-cb", VLC_VAR_ADDRESS );
600
    var_Create( mp, "vout-cb-update-output", VLC_VAR_ADDRESS );
601 602 603
    var_Create( mp, "vout-cb-swap", VLC_VAR_ADDRESS );
    var_Create( mp, "vout-cb-get-proc-address", VLC_VAR_ADDRESS );
    var_Create( mp, "vout-cb-make-current", VLC_VAR_ADDRESS );
604
    var_Create( mp, "vout-cb-select-plane", VLC_VAR_ADDRESS );
605

606
    var_Create (mp, "dec-dev", VLC_VAR_STRING);
607
    var_Create (mp, "avcodec-hw", VLC_VAR_STRING);
608
    var_Create (mp, "drawable-xid", VLC_VAR_INTEGER);
609
#if defined (_WIN32) || defined (__OS2__)
610
    var_Create (mp, "drawable-hwnd", VLC_VAR_INTEGER);
611 612 613 614
#endif
#ifdef __APPLE__
    var_Create (mp, "drawable-nsobject", VLC_VAR_ADDRESS);
#endif
615 616 617
#ifdef __ANDROID__
    var_Create (mp, "drawable-androidwindow", VLC_VAR_ADDRESS);
#endif
618 619 620 621

    var_Create (mp, "keyboard-events", VLC_VAR_BOOL);
    var_SetBool (mp, "keyboard-events", true);
    var_Create (mp, "mouse-events", VLC_VAR_BOOL);
622
    var_SetBool (mp, "mouse-events", true);
623

624
    var_Create (mp, "fullscreen", VLC_VAR_BOOL);
625 626
    var_Create (mp, "autoscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
    var_Create (mp, "zoom", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
627 628
    var_Create (mp, "aspect-ratio", VLC_VAR_STRING);
    var_Create (mp, "crop", VLC_VAR_STRING);
629 630
    var_Create (mp, "deinterlace", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
    var_Create (mp, "deinterlace-mode", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
631

632
    var_Create (mp, "vbi-page", VLC_VAR_INTEGER);
633
    var_SetInteger (mp, "vbi-page", 100);
634

635 636 637 638
    var_Create (mp, "video-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
    var_Create (mp, "sub-source", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
    var_Create (mp, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    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);

657 658
    var_Create (mp, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
    var_Create (mp, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
659
    var_Create (mp, "hue", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
660 661 662
    var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
    var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);

663
     /* Audio */
664
    var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
665
    var_Create (mp, "audio-device", VLC_VAR_STRING);
666
    var_Create (mp, "mute", VLC_VAR_BOOL);
667
    var_Create (mp, "volume", VLC_VAR_FLOAT);
668
    var_Create (mp, "corks", VLC_VAR_INTEGER);
669
    var_Create (mp, "audio-filter", VLC_VAR_STRING);
670
    var_Create (mp, "role", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
671 672
    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
673
    var_Create (mp, "amem-cleanup", VLC_VAR_ADDRESS);
674
    var_Create (mp, "amem-play", VLC_VAR_ADDRESS);
675 676 677 678
    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);
679 680 681 682
    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);
683

684 685 686 687 688
    /* 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
689 690
    /* Equalizer */
    var_Create (mp, "equalizer-preamp", VLC_VAR_FLOAT);
691
    var_Create (mp, "equalizer-vlcfreqs", VLC_VAR_BOOL);
Mark Lee's avatar
Mark Lee committed
692 693
    var_Create (mp, "equalizer-bands", VLC_VAR_STRING);

694 695 696 697 698 699 700 701 702
    /* Initialize the shared HTTP cookie jar */
    vlc_value_t cookies;
    cookies.p_address = vlc_http_cookies_new();
    if ( likely(cookies.p_address) )
    {
        var_Create(mp, "http-cookies", VLC_VAR_ADDRESS);
        var_SetChecked(mp, "http-cookies", VLC_VAR_ADDRESS, cookies);
    }

703 704
    mp->p_md = NULL;
    mp->p_libvlc_instance = instance;
705 706 707 708 709 710 711
    /* use a reentrant lock to allow calling libvlc functions from callbacks */
    mp->player = vlc_player_New(VLC_OBJECT(mp), VLC_PLAYER_LOCK_REENTRANT,
                                NULL, NULL);
    if (unlikely(!mp->player))
        goto error1;

    vlc_player_Lock(mp->player);
712

713 714 715 716 717 718 719 720 721 722
    mp->listener = vlc_player_AddListener(mp->player, &vlc_player_cbs, mp);
    if (unlikely(!mp->listener))
        goto error2;

    mp->aout_listener =
        vlc_player_aout_AddListener(mp->player, &vlc_player_aout_cbs, mp);
    if (unlikely(!mp->aout_listener))
        goto error3;

    vlc_player_Unlock(mp->player);
723

724
    mp->i_refcount = 1;
725
    libvlc_event_manager_init(&mp->event_manager, mp);
726

727 728
    /* Snapshot initialization */
    /* Attach a var callback to the global object to provide the glue between
729 730 731 732 733 734
     * 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.
     */
735 736
    var_AddCallback(vlc_object_instance(mp),
                    "snapshot-file", snapshot_was_taken, mp);
737

738
    libvlc_retain(instance);
739
    return mp;
740 741 742 743 744 745 746 747 748

error3:
    vlc_player_RemoveListener(mp->player, mp->listener);
error2:
    vlc_player_Unlock(mp->player);
    vlc_player_Delete(mp->player);
error1:
    vlc_object_delete(mp);
    return NULL;
749 750 751
}

/**************************************************************************
752
 * Create a Media Instance object with a media descriptor.
753
 **************************************************************************/
754
libvlc_media_player_t *
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
755
libvlc_media_player_new_from_media( libvlc_media_t * p_md )
756
{
757
    libvlc_media_player_t * p_mi;
758

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
759
    p_mi = libvlc_media_player_new( p_md->p_libvlc_instance );
760
    if( !p_mi )
761 762
        return NULL;

763
    libvlc_media_retain( p_md );
764
    p_mi->p_md = p_md;
765 766 767 768 769 770 771 772 773 774 775 776 777
    media_attach_preparsed_event(p_md);

    vlc_player_Lock(p_mi->player);
    int ret = vlc_player_SetCurrentMedia(p_mi->player, p_md->p_input_item);
    vlc_player_Unlock(p_mi->player);

    if (ret != VLC_SUCCESS)
    {
        media_detach_preparsed_event(p_md);
        libvlc_media_release(p_md);
        p_mi->p_md = NULL;
        return NULL;
    }
778 779 780 781 782

    return p_mi;
}

/**************************************************************************
783
 * Destroy a Media Instance object (libvlc internal)
784
 *
785
 * Warning: No lock held here, but hey, this is internal. Caller must lock.
786
 **************************************************************************/
787
static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
788
{
789
    assert( p_mi );
790

791
    /* Detach Callback from the main libvlc object */
792
    var_DelCallback( vlc_object_instance(p_mi),
793
                     "snapshot-file", snapshot_was_taken, p_mi );
794

795 796 797 798
    vlc_player_Lock(p_mi->player);
    vlc_player_aout_RemoveListener(p_mi->player, p_mi->aout_listener);
    vlc_player_RemoveListener(p_mi->player, p_mi->listener);
    vlc_player_Unlock(p_mi->player);
799

800
    vlc_player_Delete(p_mi->player);
801

802 803
    if (p_mi->p_md)
        media_detach_preparsed_event(p_mi->p_md);
804
    libvlc_event_manager_destroy(&p_mi->event_manager);
805
    libvlc_media_release( p_mi->p_md );
806

807 808 809 810 811 812 813
    vlc_http_cookie_jar_t *cookies = var_GetAddress( p_mi, "http-cookies" );
    if ( cookies )
    {
        var_Destroy( p_mi, "http-cookies" );
        vlc_http_cookies_destroy( cookies );
    }

814
    libvlc_instance_t *instance = p_mi->p_libvlc_instance;
815
    vlc_object_delete(p_mi);
816
    libvlc_release(instance);
817 818 819
}

/**************************************************************************
820 821 822
 * Release a Media Instance object.
 *
 * Function does the locking.
823
 **************************************************************************/
824
void libvlc_media_player_release( libvlc_media_player_t *p_mi )
825
{
826
    bool destroy;
827

828
    assert( p_mi );
829
    vlc_player_Lock(p_mi->player);
830
    destroy = !--p_mi->i_refcount;
831
    vlc_player_Unlock(p_mi->player);
832

833 834
    if( destroy )
        libvlc_media_player_destroy( p_mi );
835
}
836

837
/**************************************************************************
838 839 840
 * Retain a Media Instance object.
 *
 * Caller must hold the lock.
841
 **************************************************************************/
842
void libvlc_media_player_retain( libvlc_media_player_t *p_mi )
843
{
844
    assert( p_mi );
845

846
    vlc_player_Lock(p_mi->player);
847
    p_mi->i_refcount++;
848
    vlc_player_Unlock(p_mi->player);
849
}
850

851
/**************************************************************************
852 853 854
 * Set the Media descriptor associated with the instance.
 *
 * Enter without lock -- function will lock the object.
855
 **************************************************************************/
856 857
void libvlc_media_player_set_media(
                            libvlc_media_player_t *p_mi,
858
                            libvlc_media_t *p_md )
859
{
860
    vlc_player_Lock(p_mi->player);
861

862 863
    if (p_mi->p_md)
        media_detach_preparsed_event(p_mi->p_md);
864

865
    libvlc_media_release( p_mi->p_md );
866

867
    if( p_md )
868
    {
869 870
        libvlc_media_retain( p_md );
        media_attach_preparsed_event(p_md);
871
    }
872
    p_mi->p_md = p_md;
873

874 875
    vlc_player_SetCurrentMedia(p_mi->player, p_md->p_input_item);

876 877 878 879
    /* 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;

880
    vlc_player_Unlock(p_mi->player);
881 882 883
}

/**************************************************************************
884
 * Get the Media descriptor associated with the instance.
885
 **************************************************************************/
886
libvlc_media_t *
887
libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
888
{
889
    libvlc_media_t *p_m;
890

891
    vlc_player_Lock(p_mi->player);
892 893
    p_m = p_mi->p_md;
    if( p_m )
894
        libvlc_media_retain( p_m );
895
    vlc_player_Unlock(p_mi->player);
896 897

    return p_m;
898 899
}

900
/**************************************************************************
901
 * Get the event Manager.
902 903
 **************************************************************************/
libvlc_event_manager_t *
904
libvlc_media_player_event_manager( libvlc_media_player_t *p_mi )
905
{
906
    return &p_mi->event_manager;
907 908
}

909
/**************************************************************************
910
 * Tell media player to start playing.
911
 **************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
912
int libvlc_media_player_play( libvlc_media_player_t *p_mi )
913
{
914 915
    vlc_player_t *player = p_mi->player;
    vlc_player_Lock(player);
916

917 918
    int ret = vlc_player_Start(player);
    if (ret == VLC_SUCCESS)
919
    {
920 921
        if (vlc_player_IsPaused(player))
            vlc_player_Resume(player);
922 923
    }

924 925
    vlc_player_Unlock(player);
    return ret;
926 927
}

928
void libvlc_media_player_set_pause( libvlc_media_player_t *p_mi, int paused )
929
{
930 931
    vlc_player_t *player = p_mi->player;
    vlc_player_Lock(player);
932

933
    if (paused)
934
    {
935 936
        if (vlc_player_CanPause(player))
            vlc_player_Pause(player);
937
        else
938
            vlc_player_Stop(player);
939
    }
940
    else
941
    {
942
        vlc_player_Resume(player);
943
    }
944

945
    vlc_player_Unlock(player);
946
}
947

948 949 950 951 952
/**************************************************************************
 * Toggle pause.
 **************************************************************************/
void libvlc_media_player_pause( libvlc_media_player_t *p_mi )
{
953 954 955 956 957 958
    vlc_player_t *player = p_mi->player;
    vlc_player_Lock(player);

    vlc_player_TogglePause(player);

    vlc_player_Unlock(player);
959 960
}

961
/**************************************************************************
962
 * Tells whether the media player is currently playing.
963
 **************************************************************************/
964
bool libvlc_media_player_is_playing(libvlc_media_player_t *p_mi)
965
{
966 967 968 969 970 971 972
    vlc_player_t *player = p_mi->player;
    vlc_player_Lock(player);

    bool ret = vlc_player_IsStarted(player) && !vlc_player_IsPaused(player);

    vlc_player_Unlock(player);
    return ret;
973 974
}

975
/**************************************************************************
976
 * Stop playing.
977
 **************************************************************************/
978
void libvlc_media_player_stop_async( libvlc_media_player_t *p_mi )
979
{
980 981
    vlc_player_t *player = p_mi->player;
    vlc_player_Lock(player);
982

983
    vlc_player_Stop(player);
984

985
    vlc_player_Unlock(player);
986 987
}

988
int libvlc_media_player_set_renderer( libvlc_media_player_t *p_mi,
989
                                      libvlc_renderer_item_t *p_litem )
990
{
991 992
    vlc_player_t *player = p_mi->player;
    vlc_player_Lock(player);
993

994 995
    vlc_renderer_item_t *renderer = libvlc_renderer_item_to_vlc(p_litem);
    vlc_player_SetRenderer(player, renderer);
996

997
    vlc_player_Unlock(player);
998 999
    return 0;
}
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

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 );
1011
    var_SetString( mp, "avcodec-hw", "none" );
1012
    var_SetString( mp, "vout", "vmem" );
1013
    var_SetString( mp, "window", "dummy" );
1014 1015
}

1016 1017 1018 1019 1020 1021 1022 1023
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 );
}

1024 1025 1026 1027 1028 1029 1030 1031 1032
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 );
}

1033 1034 1035 1036 1037 1038 1039 1040 1041
bool libvlc_video_set_output_callbacks(libvlc_media_player_t *mp,
                                       libvlc_video_engine_t engine,
                                       libvlc_video_setup_cb setup_cb,
                                       libvlc_video_cleanup_cb cleanup_cb,
                                       libvlc_video_update_output_cb update_output_cb,
                                       libvlc_video_swap_cb swap_cb,
                                       libvlc_video_makeCurrent_cb makeCurrent_cb,
                                       libvlc_video_getProcAddress_cb getProcAddress_cb,
                                       void *opaque)
1042 1043 1044 1045 1046 1047 1048 1049
{
#ifdef __ANDROID__
    //use the default android window
    var_SetString( mp, "window", "");
#else
    var_SetString( mp, "window", "wdummy");
#endif

1050
    if( engine == libvlc_video_engine_gles2 )
1051 1052 1053 1054
    {
        var_SetString ( mp, "vout", "gles2" );
        var_SetString ( mp, "gles2", "vgl" );
    }
1055
    else if( engine == libvlc_video_engine_opengl )
1056 1057 1058 1059
    {
        var_SetString ( mp, "vout", "gl" );
        var_SetString ( mp, "gl", "vgl");
    }
1060
    else
1061
        return false;
1062

1063 1064 1065
    var_SetAddress( mp, "vout-cb-opaque", opaque );
    var_SetAddress( mp, "vout-cb-setup", setup_cb );
    var_SetAddress( mp, "vout-cb-cleanup", cleanup_cb );
1066
    var_SetAddress( mp, "vout-cb-update-output", update_output_cb );
1067 1068 1069
    var_SetAddress( mp, "vout-cb-swap", swap_cb );
    var_SetAddress( mp, "vout-cb-get-proc-address", getProcAddress_cb );
    var_SetAddress( mp, "vout-cb-make-current", makeCurrent_cb );
1070
    return true;
1071 1072 1073
}


1074 1075 1076 1077
bool libvlc_video_direct3d_set_callbacks(libvlc_media_player_t *mp,
                                         libvlc_video_direct3d_engine_t engine,
                                         libvlc_video_direct3d_device_setup_cb setup_cb,
                                         libvlc_video_direct3d_device_cleanup_cb cleanup_cb,
1078
                                         libvlc_video_direct3d_set_resize_cb resize_cb,
1079 1080 1081 1082 1083
                                         libvlc_video_direct3d_update_output_cb update_output_cb,
                                         libvlc_video_swap_cb swap_cb,
                                         libvlc_video_direct3d_start_end_rendering_cb makeCurrent_cb,
                                         libvlc_video_direct3d_select_plane_cb select_plane_cb,
                                         void *opaque)
1084
{
1085
    var_SetString( mp, "window", "wextern");
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097

    if ( engine == libvlc_video_direct3d_engine_d3d11 )
    {
        var_SetString ( mp, "vout", "direct3d11" );
        var_SetString ( mp, "avcodec-hw", "d3d11va");
    }
    else if ( engine == libvlc_video_direct3d_engine_d3d9 )
    {
        var_SetString ( mp, "vout", "direct3d9" );
        var_SetString ( mp, "avcodec-hw", "dxva2");
    }
    else
1098
        return false;
1099 1100 1101 1102

    var_SetAddress( mp, "vout-cb-opaque", opaque );
    var_SetAddress( mp, "vout-cb-setup", setup_cb );
    var_SetAddress( mp, "vout-cb-cleanup", cleanup_cb );
1103
    var_SetAddress( mp, "vout-cb-resize-cb", resize_cb );
1104 1105 1106
    var_SetAddress( mp, "vout-cb-update-output", update_output_cb );
    var_SetAddress( mp, "vout-cb-swap", swap_cb );
    var_SetAddress( mp, "vout-cb-make-current", makeCurrent_cb );
1107
    var_SetAddress( mp, "vout-cb-select-plane", select_plane_cb );
1108
    return true;
1109 1110
}

1111 1112 1113 1114
/**************************************************************************
 * set_nsobject
 **************************************************************************/
void libvlc_media_player_set_nsobject( libvlc_media_player_t *p_mi,
1115
                                        void * drawable )
1116
{
1117 1118
    assert (p_mi != NULL);
#ifdef __APPLE__
1119 1120 1121
    var_SetString (p_mi, "avcodec-hw", "");
    var_SetString (p_mi, "vout", "");
    var_SetString (p_mi, "window", "");
1122 1123
    var_SetAddress (p_mi, "drawable-nsobject", drawable);
#else
1124 1125 1126 1127 1128
    (void)drawable;
    libvlc_printerr ("can't set nsobject: APPLE build required");
    assert(false);
    var_SetString (p_mi, "vout", "none");
    var_SetString (p_mi, "window", "none");
1129
#endif
1130 1131 1132
}

/**************************************************************************
1133
 * get_nsobject
1134
 **************************************************************************/
Pierre's avatar
Pierre committed
1135
void * libvlc_media_player_get_nsobject( libvlc_media_player_t *p_mi )
1136
{
1137 1138 1139 1140
    assert (p_mi != NULL);
#ifdef __APPLE__
    return var_GetAddress (p_mi, "drawable-nsobject");
#else
1141
    (void) p_mi;
1142 1143
    return NULL;
#endif
1144 1145
}

1146 1147 1148
/**************************************************************************
 * set_xwindow
 **************************************************************************/
1149
void libvlc_media_player_set_xwindow( libvlc_media_player_t *p_mi,
1150
                                      uint32_t drawable )
1151
{
1152
    assert (p_mi != NULL);
1153

1154
    var_SetString (p_mi, "avcodec-hw", "");
1155
    var_SetString (p_mi, "vout", "");
1156
    var_SetString (p_mi, "window", drawable ? "embed-xid,any" : "");
1157
    var_SetInteger (p_mi, "drawable-xid", drawable);
1158 1159
}

1160 1161 1162
/**************************************************************************
 * get_xwindow
 **************************************************************************/
1163 1164
uint32_t libvlc_media_player_get_xwindow( libvlc_media_player_t *p_mi )
{
1165
    return var_GetInteger (p_mi, "drawable-xid");
1166 1167
}

1168 1169 1170
/**************************************************************************
 * set_hwnd
 **************************************************************************/
1171
void libvlc_media_player_set_hwnd( libvlc_media_player_t *p_mi,
1172
                                   void *drawable )
1173
{
1174
    assert (p_mi != NULL);
1175
#if defined (_WIN32) || defined (__OS2__)
1176 1177
    var_SetString (p_mi, "avcodec-hw", "");
    var_SetString (p_mi, "vout", "");
1178 1179
    var_SetString (p_mi, "window",
                   (drawable != NULL) ? "embed-hwnd,any" : "");
1180
    var_SetInteger (p_mi, "drawable-hwnd", (uintptr_t)drawable);
1181
#else
1182
    (void) drawable;
1183
    libvlc_printerr ("can't set hwnd: WIN32 build required");
1184 1185 1186
    assert(false);
    var_SetString (p_mi, "vout", "none");
    var_SetString (p_mi, "window", "none");
1187
#endif
1188 1189
}

1190