bluray.c 18 KB
Newer Older
1 2 3
/*****************************************************************************
 * bluray.c: Blu-ray disc support plugin
 *****************************************************************************
4 5 6
 * Copyright © 2010-2011 VideoLAN, VLC authors and libbluray AUTHORS
 *
 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
7
 *
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 15
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
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 23 24 25 26 27
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
28
#include <limits.h>                         /* PATH_MAX */
29 30 31 32
#if defined (HAVE_MNTENT_H) && defined(HAVE_SYS_STAT_H)
#include <mntent.h>
#include <sys/stat.h>
#endif
33 34 35

#include <vlc_common.h>
#include <vlc_plugin.h>
36 37 38
#include <vlc_demux.h>                      /* demux_t */
#include <vlc_input.h>                      /* Seekpoints, chapters */
#include <vlc_dialog.h>                     /* BD+/AACS warnings */
39 40

#include <libbluray/bluray.h>
41
#include <libbluray/meta_data.h>
42 43 44 45 46

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

47 48 49 50
#define BD_MENU_TEXT        N_( "Bluray menus" )
#define BD_MENU_LONGTEXT    N_( "Use bluray menus. If disabled, "\
                                "the movie will start directly" )

51 52 53 54 55 56 57 58 59 60
/* Callbacks */
static int  blurayOpen ( vlc_object_t * );
static void blurayClose( vlc_object_t * );

vlc_module_begin ()
    set_shortname( N_("BluRay") )
    set_description( N_("Blu-Ray Disc support (libbluray)") )

    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )
61
    set_capability( "access_demux", 200)
62
    add_bool( "bluray-menu", false, BD_MENU_TEXT, BD_MENU_LONGTEXT, false )
63

64
    add_shortcut( "bluray", "file" )
65 66 67 68 69

    set_callbacks( blurayOpen, blurayClose )
vlc_module_end ()


70
struct demux_sys_t
71
{
72
    BLURAY         *bluray;
73 74

    /* Titles */
75 76 77 78
    unsigned int    i_title;
    unsigned int    i_longest_title;
    input_title_t **pp_title;

79 80 81
    /* Menus */
    bool            b_menu;

82 83
    /* TS stream */
    stream_t       *p_parser;
84 85
};

86 87 88 89
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int     blurayControl(demux_t *, int, va_list);
90
static int     blurayDemux(demux_t *);
91 92 93 94 95 96 97

static int     blurayInitTitles(demux_t *p_demux );
static int     bluraySetTitle(demux_t *p_demux, int i_title);

#define FROM_TICKS(a) (a*CLOCK_FREQ / INT64_C(90000))
#define TO_TICKS(a)   (a*INT64_C(90000)/CLOCK_FREQ)
#define CUR_LENGTH    p_sys->pp_title[p_demux->info.i_title]->i_length
98 99 100 101 102 103

/*****************************************************************************
 * blurayOpen: module init function
 *****************************************************************************/
static int blurayOpen( vlc_object_t *object )
{
104 105
    demux_t *p_demux = (demux_t*)object;
    demux_sys_t *p_sys;
106 107

    char *pos_title;
108
    int i_title = -1;
109
    char bd_path[PATH_MAX] = { '\0' };
110
    const char *error_msg = NULL;
111

112
    if (strcmp(p_demux->psz_access, "bluray")) {
113
        // TODO BDMV support, once we figure out what to do in libbluray
114 115 116
        return VLC_EGENERIC;
    }

117 118
    /* */
    p_demux->p_sys = p_sys = malloc(sizeof(*p_sys));
119 120 121
    if (unlikely(!p_sys)) {
        return VLC_ENOMEM;
    }
122 123 124 125 126 127
    p_sys->p_parser = NULL;

    /* init demux info fields */
    p_demux->info.i_update    = 0;
    p_demux->info.i_title     = 0;
    p_demux->info.i_seekpoint = 0;
128

129 130
    TAB_INIT( p_sys->i_title, p_sys->pp_title );

131
    /* store current bd_path */
132 133 134 135
    if (p_demux->psz_file) {
        strncpy(bd_path, p_demux->psz_file, sizeof(bd_path));
        bd_path[PATH_MAX - 1] = '\0';
    }
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#if defined (HAVE_MNTENT_H) && defined (HAVE_SYS_STAT_H)
    /* If we're passed a block device, try to convert it to the mount point. */
    struct stat st;
    if ( !stat (bd_path, &st)) {
        if (S_ISBLK (st.st_mode)) {
            FILE* mtab = setmntent ("/proc/self/mounts", "r");
            struct mntent* m;
            struct mntent mbuf;
            char buf [8192];
            while ((m = getmntent_r (mtab, &mbuf, buf, sizeof(buf))) != NULL) {
                if (!strcmp (m->mnt_fsname, bd_path)) {
                    strncpy (bd_path, m->mnt_dir, sizeof(bd_path));
                    bd_path[sizeof(bd_path) - 1] = '\0';
                    break;
                }
            }
            endmntent (mtab);
        }
    }
#endif /* HAVE_MNTENT_H && HAVE_SYS_STAT_H */
157
    p_sys->bluray = bd_open(bd_path, NULL);
158
    if (!p_sys->bluray) {
159 160 161 162
        free(p_sys);
        return VLC_EGENERIC;
    }

163 164
    /* Warning the user about AACS/BD+ */
    const BLURAY_DISC_INFO *disc_info = bd_get_disc_info(p_sys->bluray);
165 166
    msg_Info(p_demux, "First play: %i, Top menu: %i\n"
                      "HDMV Titles: %i, BD-J Titles: %i, Other: %i",
167 168 169 170 171 172 173
             disc_info->first_play_supported, disc_info->top_menu_supported,
             disc_info->num_hdmv_titles, disc_info->num_bdj_titles,
             disc_info->num_unsupported_titles);

    /* AACS */
    if (disc_info->aacs_detected) {
        if (!disc_info->libaacs_detected) {
174 175 176
            error_msg = _("This Blu-Ray Disc needs a library for AACS decoding, "
                      "and your system does not have it.");
            goto error;
177 178
        }
        if (!disc_info->aacs_handled) {
179 180 181
            error_msg = _("Your system AACS decoding library does not work. "
                      "Missing keys?");
            goto error;
182 183 184 185 186 187
        }
    }

    /* BD+ */
    if (disc_info->bdplus_detected) {
        if (!disc_info->libbdplus_detected) {
188 189 190
            error_msg = _("This Blu-Ray Disc needs a library for BD+ decoding, "
                      "and your system does not have it.");
            goto error;
191 192
        }
        if (!disc_info->bdplus_handled) {
193 194 195
            error_msg = _("Your system BD+ decoding library does not work. "
                      "Missing configuration?");
            goto error;
196 197 198 199
        }
    }

    /* Get titles and chapters */
200
    if (blurayInitTitles(p_demux) != VLC_SUCCESS) {
201
        goto error;
202 203
    }

204 205 206 207 208
    /*
     * Initialize the event queue, so we can receive events in blurayDemux(Menu).
     */
    bd_get_event(p_sys->bluray, NULL);

209 210 211 212 213 214 215 216
    p_sys->b_menu = var_InheritBool( p_demux, "bluray-menu" );
    if ( p_sys->b_menu )
    {
        /*
         * libbluray will start playback from "First-Title" title
         * Therefore, We don't have to select any title.
         */
        bd_play( p_sys->bluray );
217
    }
218 219 220 221 222 223 224 225
    else
    {
        /* get title request */
        if ((pos_title = strrchr(bd_path, ':'))) {
            /* found character ':' for title information */
            *(pos_title++) = '\0';
            i_title = atoi(pos_title);
        }
226

227 228 229 230 231
        /* set start title number */
        if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS) {
            msg_Err( p_demux, "Could not set the title %d", i_title );
            goto error;
        }
232 233
    }

234 235 236
    p_sys->p_parser   = stream_DemuxNew(p_demux, "ts", p_demux->out);
    if (!p_sys->p_parser) {
        msg_Err(p_demux, "Failed to create TS demuxer");
237
        goto error;
238 239 240 241
    }

    p_demux->pf_control = blurayControl;
    p_demux->pf_demux   = blurayDemux;
242 243

    return VLC_SUCCESS;
244 245 246 247 248 249

error:
    if (error_msg)
        dialog_Fatal(p_demux, _("Blu-Ray error"), "%s", error_msg);
    blurayClose(object);
    return VLC_EGENERIC;
250 251 252 253 254 255 256 257
}


/*****************************************************************************
 * blurayClose: module destroy function
 *****************************************************************************/
static void blurayClose( vlc_object_t *object )
{
258 259 260 261 262
    demux_t *p_demux = (demux_t*)object;
    demux_sys_t *p_sys = p_demux->p_sys;

    if (p_sys->p_parser)
        stream_Delete(p_sys->p_parser);
263

264 265 266 267 268
    /* Titles */
    for (unsigned int i = 0; i < p_sys->i_title; i++)
        vlc_input_title_Delete(p_sys->pp_title[i]);
    TAB_CLEAN( p_sys->i_title, p_sys->pp_title );

269 270 271 272 273 274
    /* bd_close( NULL ) can crash */
    assert(p_sys->bluray);
    bd_close(p_sys->bluray);
    free(p_sys);
}

275 276

static int blurayInitTitles(demux_t *p_demux )
277
{
278
    demux_sys_t *p_sys = p_demux->p_sys;
279 280

    /* get and set the titles */
281
    unsigned i_title = bd_get_titles(p_sys->bluray, TITLES_RELEVANT, 60);
282 283 284 285 286 287 288
    int64_t duration = 0;

    for (unsigned int i = 0; i < i_title; i++) {
        input_title_t *t = vlc_input_title_New();
        if (!t)
            break;

289
        BLURAY_TITLE_INFO *title_info = bd_get_title_info(p_sys->bluray, i, 0);
290 291
        if (!title_info)
            break;
292
        t->i_length = FROM_TICKS(title_info->duration);
293 294 295 296 297 298 299 300

        if (t->i_length > duration) {
            duration = t->i_length;
            p_sys->i_longest_title = i;
        }

        for ( unsigned int j = 0; j < title_info->chapter_count; j++) {
            seekpoint_t *s = vlc_seekpoint_New();
301
            if (!s)
302 303 304 305 306 307 308 309 310 311
                break;
            s->i_time_offset = title_info->chapters[j].offset;

            TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
        }
        TAB_APPEND( p_sys->i_title, p_sys->pp_title, t );
        bd_free_title_info(title_info);
    }
    return VLC_SUCCESS;
}
312

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
static void blurayResetParser( demux_t *p_demux )
{
    /*
     * This is a hack and will have to be removed.
     * The parser should be flushed, and not destroy/created each time
     * we are changing title.
     */
    demux_sys_t *p_sys = p_demux->p_sys;
    if (!p_sys->p_parser)
        return;
    stream_Delete(p_sys->p_parser);
    p_sys->p_parser = stream_DemuxNew(p_demux, "ts", p_demux->out);
    if (!p_sys->p_parser) {
        msg_Err(p_demux, "Failed to create TS demuxer");
    }
}

330 331
static void blurayUpdateTitle( demux_t *p_demux, int i_title )
{
332 333 334 335
    blurayResetParser(p_demux);
    if (i_title >= p_demux->p_sys->i_title)
        return;

336 337 338 339 340
    /* read title info and init some values */
    p_demux->info.i_title = i_title;
    p_demux->info.i_seekpoint = 0;
    p_demux->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
}
341

342 343 344
/*****************************************************************************
 * bluraySetTitle: select new BD title
 *****************************************************************************/
345
static int bluraySetTitle(demux_t *p_demux, int i_title)
346
{
347
    demux_sys_t *p_sys = p_demux->p_sys;
348

349
    /* Looking for the main title, ie the longest duration */
350
    if (i_title < 0)
351
        i_title = p_sys->i_longest_title;
352 353
    else if ((unsigned)i_title > p_sys->i_title)
        return VLC_EGENERIC;
354

355
    msg_Dbg( p_demux, "Selecting Title %i", i_title);
356

357
    /* Select Blu-Ray title */
358 359
    if (bd_select_title(p_demux->p_sys->bluray, i_title) == 0 ) {
        msg_Err(p_demux, "cannot select bd title '%d'", p_demux->info.i_title);
360 361
        return VLC_EGENERIC;
    }
362
    blurayUpdateTitle( p_demux, i_title );
363 364 365 366 367 368 369 370

    return VLC_SUCCESS;
}


/*****************************************************************************
 * blurayControl: handle the controls
 *****************************************************************************/
371
static int blurayControl(demux_t *p_demux, int query, va_list args)
372
{
373
    demux_sys_t *p_sys = p_demux->p_sys;
374 375 376 377
    bool     *pb_bool;
    int64_t  *pi_64;

    switch (query) {
378 379 380
        case DEMUX_CAN_SEEK:
        case DEMUX_CAN_PAUSE:
        case DEMUX_CAN_CONTROL_PACE:
381 382 383 384
             pb_bool = (bool*)va_arg( args, bool * );
             *pb_bool = true;
             break;

385
        case DEMUX_GET_PTS_DELAY:
386
            pi_64 = (int64_t*)va_arg( args, int64_t * );
387 388
            *pi_64 =
                INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
389 390
            break;

391
        case DEMUX_SET_PAUSE_STATE:
392 393 394
            /* Nothing to do */
            break;

395
        case DEMUX_SET_TITLE:
396 397
        {
            int i_title = (int)va_arg( args, int );
398
            if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS)
399 400 401
                return VLC_EGENERIC;
            break;
        }
402
        case DEMUX_SET_SEEKPOINT:
403 404 405
        {
            int i_chapter = (int)va_arg( args, int );
            bd_seek_chapter( p_sys->bluray, i_chapter );
406
            p_demux->info.i_update = INPUT_UPDATE_SEEKPOINT;
407 408
            break;
        }
409

410
        case DEMUX_GET_TITLE_INFO:
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
        {
            input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
            int *pi_int             = (int*)va_arg( args, int* );
            int *pi_title_offset    = (int*)va_arg( args, int* );
            int *pi_chapter_offset  = (int*)va_arg( args, int* );

            /* */
            *pi_title_offset   = 0;
            *pi_chapter_offset = 0;

            /* Duplicate local title infos */
            *pi_int = p_sys->i_title;
            *ppp_title = calloc( p_sys->i_title, sizeof(input_title_t **) );
            for( unsigned int i = 0; i < p_sys->i_title; i++ )
                (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->pp_title[i]);

            return VLC_SUCCESS;
        }
429

430 431 432
        case DEMUX_GET_LENGTH:
        {
            int64_t *pi_length = (int64_t*)va_arg(args, int64_t *);
433
            *pi_length = p_demux->info.i_title < p_sys->i_title ? CUR_LENGTH : 0;
434 435 436 437 438 439 440 441 442 443 444 445 446 447
            return VLC_SUCCESS;
        }
        case DEMUX_SET_TIME:
        {
            int64_t i_time = (int64_t)va_arg(args, int64_t);
            bd_seek_time(p_sys->bluray, TO_TICKS(i_time));
            return VLC_SUCCESS;
        }
        case DEMUX_GET_TIME:
        {
            int64_t *pi_time = (int64_t*)va_arg(args, int64_t *);
            *pi_time = (int64_t)FROM_TICKS(bd_tell_time(p_sys->bluray));
            return VLC_SUCCESS;
        }
448

449 450 451
        case DEMUX_GET_POSITION:
        {
            double *pf_position = (double*)va_arg( args, double * );
452 453
            *pf_position = p_demux->info.i_title < p_sys->i_title ?
                        (double)FROM_TICKS(bd_tell_time(p_sys->bluray))/CUR_LENGTH : 0.0;
454 455 456 457 458 459 460 461 462 463 464
            return VLC_SUCCESS;
        }
        case DEMUX_SET_POSITION:
        {
            double f_position = (double)va_arg(args, double);
            bd_seek_time(p_sys->bluray, TO_TICKS(f_position*CUR_LENGTH));
            return VLC_SUCCESS;
        }

        case DEMUX_GET_META:
        {
465
            const struct meta_dl *meta = bd_get_meta(p_sys->bluray);
466 467 468
            if(!meta)
                return VLC_EGENERIC;

469
            vlc_meta_t *p_meta = (vlc_meta_t *) va_arg (args, vlc_meta_t*);
470

471
            if (!EMPTY_STR(meta->di_name)) vlc_meta_SetTitle(p_meta, meta->di_name);
472

473 474 475
            if (!EMPTY_STR(meta->language_code)) vlc_meta_AddExtra(p_meta, "Language", meta->language_code);
            if (!EMPTY_STR(meta->filename)) vlc_meta_AddExtra(p_meta, "Filename", meta->filename);
            if (!EMPTY_STR(meta->di_alternative)) vlc_meta_AddExtra(p_meta, "Alternative", meta->di_alternative);
476

477 478
            // if (meta->di_set_number > 0) vlc_meta_SetTrackNum(p_meta, meta->di_set_number);
            // if (meta->di_num_sets > 0) vlc_meta_AddExtra(p_meta, "Discs numbers in Set", meta->di_num_sets);
479

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
            if (meta->thumb_count > 0 && meta->thumbnails) {
                vlc_meta_SetArtURL(p_meta, meta->thumbnails[0].path);
            }

            return VLC_SUCCESS;
        }

        case DEMUX_CAN_RECORD:
        case DEMUX_GET_FPS:
        case DEMUX_SET_GROUP:
        case DEMUX_HAS_UNSUPPORTED_META:
        case DEMUX_GET_ATTACHMENTS:
            return VLC_EGENERIC;
        default:
            msg_Warn( p_demux, "unimplemented query (%d) in control", query );
            return VLC_EGENERIC;
    }
497 498 499
    return VLC_SUCCESS;
}

500 501 502 503 504 505 506
static void blurayHandleEvent( demux_t *p_demux, const BD_EVENT *e )
{
    demux_sys_t *p_sys = p_demux->p_sys;

    switch (e->event)
    {
        case BD_EVENT_TITLE:
507
            blurayUpdateTitle( p_demux, e->param );
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
            break;
        case BD_EVENT_PLAYITEM:
            break;
        case BD_EVENT_AUDIO_STREAM:
            break;
        case BD_EVENT_CHAPTER:
            p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
            p_demux->info.i_seekpoint = 0;
            break;
        case BD_EVENT_ANGLE:
        case BD_EVENT_IG_STREAM:
        default:
            msg_Warn( p_demux, "event: %d param: %d", e->event, e->param );
            break;
    }
}

static void blurayHandleEvents( demux_t *p_demux )
{
    BD_EVENT e;

    while (bd_get_event(p_demux->p_sys->bluray, &e))
    {
        blurayHandleEvent(p_demux, &e);
    }
}
534

535 536 537 538
#define BD_TS_PACKET_SIZE (192)
#define NB_TS_PACKETS (200)

static int blurayDemux(demux_t *p_demux)
539
{
540
    demux_sys_t *p_sys = p_demux->p_sys;
541

542 543 544
    block_t *p_block = block_New(p_demux, NB_TS_PACKETS * (int64_t)BD_TS_PACKET_SIZE);
    if (!p_block) {
        return -1;
545 546
    }

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    int nread = -1;
    if (p_sys->b_menu == false) {
        blurayHandleEvents(p_demux);
        nread = bd_read(p_sys->bluray, p_block->p_buffer,
                NB_TS_PACKETS * BD_TS_PACKET_SIZE);
        if (nread < 0) {
            block_Release(p_block);
            return nread;
        }
    }
    else {
        BD_EVENT e;
        nread = bd_read_ext( p_sys->bluray, p_block->p_buffer,
                NB_TS_PACKETS * BD_TS_PACKET_SIZE, &e );
        if ( nread == 0 ) {
            if ( e.event == BD_EVENT_NONE )
                msg_Info( p_demux, "We reached the end of a title" );
            else
                blurayHandleEvent( p_demux, &e );
            block_Release(p_block);
            return 1;
        }
569 570
    }

571
    p_block->i_buffer = nread;
572

573 574 575 576
    stream_DemuxSend( p_sys->p_parser, p_block );

    return 1;
}