bluray.c 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*****************************************************************************
 * bluray.c: Blu-ray disc support plugin
 *****************************************************************************
 * Copyright (C) 2010 VideoLAN, VLC authors and libbluray AUTHORS
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 *****************************************************************************/

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

#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif

#include <assert.h>
#include <limits.h>

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_access.h>
#include <vlc_messages.h>
36
#include <vlc_input.h>
37
#include <vlc_dialog.h>
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

#include <libbluray/bluray.h>

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define CACHING_TEXT N_("Caching value in ms")
#define CACHING_LONGTEXT N_( "Caching value for BDs. This "\
                             "value should be set in milliseconds." )

/* 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 )
    set_capability( "access", 60 )

60
    add_integer( "bluray-caching", 1000,
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        CACHING_TEXT, CACHING_LONGTEXT, true )

    add_shortcut( "bluray" )
    add_shortcut( "file" )

    set_callbacks( blurayOpen, blurayClose )
vlc_module_end ()

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

struct access_sys_t
{
    BLURAY *bluray; /* */
76 77 78 79 80 81

    /* Titles */
    unsigned int i_title;
    unsigned int i_longest_title;
    input_title_t   **pp_title;

82 83 84 85 86 87
    int i_bd_delay;
};

static ssize_t blurayRead   (access_t *, uint8_t *, size_t);
static int     bluraySeek   (access_t *, uint64_t);
static int     blurayControl(access_t *, int, va_list);
88 89
static int     blurayInitTitles(access_t *p_access );
static int     bluraySetTitle(access_t *p_access, int i_title);
90 91 92 93 94 95 96 97 98 99

/*****************************************************************************
 * blurayOpen: module init function
 *****************************************************************************/
static int blurayOpen( vlc_object_t *object )
{
    access_t *p_access = (access_t*)object;

    access_sys_t *p_sys;
    char *pos_title;
100
    int i_title = -1;
101 102
    char bd_path[PATH_MAX];

103 104
    if( strcmp( p_access->psz_access, "bluray" ) ) {
        // TODO BDMV support, once we figure out what to do in libbluray
105 106 107 108 109 110 111 112 113 114 115 116 117 118
        return VLC_EGENERIC;
    }

    /* init access fields */
    access_InitFields(p_access);

    /* register callback function for communication */
    ACCESS_SET_CALLBACKS(blurayRead, NULL, blurayControl, bluraySeek);

    p_access->p_sys = p_sys = malloc(sizeof(access_sys_t));
    if (unlikely(!p_sys)) {
        return VLC_ENOMEM;
    }

119 120
    TAB_INIT( p_sys->i_title, p_sys->pp_title );

121 122 123 124 125 126 127 128 129 130
    /* store current bd_path */
    strncpy(bd_path, p_access->psz_location, sizeof(bd_path));
    bd_path[PATH_MAX - 1] = '\0';

    p_sys->bluray = bd_open(bd_path, NULL);
    if ( !p_sys->bluray ) {
        free(p_sys);
        return VLC_EGENERIC;
    }

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    /* Warning the user about AACS/BD+ */
    const BLURAY_DISC_INFO *disc_info = bd_get_disc_info(p_sys->bluray);
    msg_Dbg (p_access, "First play: %i, Top menu: %i\n"
                       "HDMV Titles: %i, BDJ Titles: %i, Other: %i",
             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) {
            dialog_Fatal (p_access, _("Blu-Ray error"),
                    _("This Blu-Ray Disc needs a library for AACS decoding, "
                      "and your system does not have it."));
            blurayClose(object);
            return VLC_EGENERIC;
        }
        if (!disc_info->aacs_handled) {
            dialog_Fatal (p_access, _("Blu-Ray error"),
                    _("Your system AACS decoding library does not work. "
                      "Missing keys?"));
            blurayClose(object);
            return VLC_EGENERIC;
        }
    }

    /* BD+ */
    if (disc_info->bdplus_detected) {
        if (!disc_info->libbdplus_detected) {
            dialog_Fatal (p_access, _("Blu-Ray error"),
                    _("This Blu-Ray Disc needs a library for BD+ decoding, "
                      "and your system does not have it."));
            blurayClose(object);
            return VLC_EGENERIC;
        }
        if (!disc_info->bdplus_handled) {
            dialog_Fatal (p_access, _("Blu-Ray error"),
                    _("Your system BD+ decoding library does not work. "
                      "Missing configuration?"));
            blurayClose(object);
            return VLC_EGENERIC;
        }
    }

    /* Get titles and chapters */
176 177 178 179 180 181 182 183 184 185 186 187
    if (blurayInitTitles(p_access) != VLC_SUCCESS) {
        blurayClose(object);
        return VLC_EGENERIC;
    }

    /* get title request */
    if ( (pos_title = strrchr(bd_path, ':')) ) {
        /* found character ':' for title information */
        *(pos_title++) = '\0';
        i_title = atoi(pos_title);
    }

188 189
    /* set start title number */
    if ( bluraySetTitle(p_access, i_title) != VLC_SUCCESS ) {
190
        msg_Err( p_access, "Could not set the title %d", i_title );
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        blurayClose(object);
        return VLC_EGENERIC;
    }

    p_sys->i_bd_delay = var_InheritInteger(p_access, "bluray-caching");

    return VLC_SUCCESS;
}


/*****************************************************************************
 * blurayClose: module destroy function
 *****************************************************************************/
static void blurayClose( vlc_object_t *object )
{
    access_t *p_access = (access_t*)object;
    access_sys_t *p_sys = p_access->p_sys;

209 210 211 212 213
    /* 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 );

214 215 216 217 218 219
    /* bd_close( NULL ) can crash */
    assert(p_sys->bluray);
    bd_close(p_sys->bluray);
    free(p_sys);
}

220 221 222 223 224
static int blurayInitTitles(access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;

    /* get and set the titles */
225
    unsigned i_title = bd_get_titles(p_sys->bluray, TITLES_RELEVANT, 0);
226 227 228 229 230 231 232
    int64_t duration = 0;

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

233
        BLURAY_TITLE_INFO *title_info = bd_get_title_info(p_sys->bluray, i, 0);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        if (!title_info)
            break;
        t->i_length = title_info->duration * CLOCK_FREQ / INT64_C(90000);

        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();
            if( !s )
                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;
}
256 257 258 259 260 261 262 263

/*****************************************************************************
 * bluraySetTitle: select new BD title
 *****************************************************************************/
static int bluraySetTitle(access_t *p_access, int i_title)
{
    access_sys_t *p_sys = p_access->p_sys;

264
    /* Looking for the main title, ie the longest duration */
265 266 267 268
    if (i_title == -1)
        i_title = p_sys->i_longest_title;

    msg_Dbg( p_access, "Selecting Title %i", i_title);
269

270 271 272 273 274 275 276 277 278 279 280 281
    /* Select Blu-Ray title */
    if ( bd_select_title(p_access->p_sys->bluray, i_title) == 0 ) {
        msg_Err( p_access, "cannot select bd title '%d'", p_access->info.i_title);
        return VLC_EGENERIC;
    }

    /* read title length and init some values */
    p_access->info.i_title = i_title;
    p_access->info.i_size  = bd_get_title_size(p_sys->bluray);
    p_access->info.i_pos   = 0;
    p_access->info.b_eof   = false;
    p_access->info.i_seekpoint = 0;
282
    p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

    return VLC_SUCCESS;
}


/*****************************************************************************
 * blurayControl: handle the controls
 *****************************************************************************/
static int blurayControl(access_t *p_access, int query, va_list args)
{
    access_sys_t *p_sys = p_access->p_sys;
    bool     *pb_bool;
    int64_t  *pi_64;

    switch (query) {
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
             pb_bool = (bool*)va_arg( args, bool * );
             *pb_bool = true;
             break;

        case ACCESS_GET_PTS_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
            *pi_64 = p_sys->i_bd_delay;
            break;

        case ACCESS_SET_PAUSE_STATE:
            /* Nothing to do */
            break;

        case ACCESS_SET_TITLE:
        {
            int i_title = (int)va_arg( args, int );
            if( bluraySetTitle( p_access, i_title ) != VLC_SUCCESS )
                return VLC_EGENERIC;
            break;
        }
        case ACCESS_SET_SEEKPOINT:
        {
            int i_chapter = (int)va_arg( args, int );
            bd_seek_chapter( p_sys->bluray, i_chapter );
326
            p_access->info.i_update = INPUT_UPDATE_SEEKPOINT;
327 328
            break;
        }
329

330
        case ACCESS_GET_TITLE_INFO:
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
        {
            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;
        }
349 350
        case ACCESS_SET_PRIVATE_ID_STATE:
        case ACCESS_GET_CONTENT_TYPE:
351
        case ACCESS_GET_META:
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 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
            return VLC_EGENERIC;

        default:
            msg_Warn( p_access, "unimplemented query (%d) in control", query );
            return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}


/*****************************************************************************
 * bluraySeek: seek to the given position
 *****************************************************************************/
static int bluraySeek(access_t *p_access, uint64_t position)
{
    access_sys_t *p_sys = p_access->p_sys;

    p_access->info.i_pos = bd_seek(p_sys->bluray, position);
    p_access->info.b_eof = false;

    return VLC_SUCCESS;
}


/*****************************************************************************
 * blurayRead: read BD data into buffer
 *****************************************************************************/
static ssize_t blurayRead(access_t *p_access, uint8_t *data, size_t size)
{
    access_sys_t *p_sys = p_access->p_sys;
    int nread;

    if (p_access->info.b_eof) {
        return 0;
    }

    /* read data into buffer with given length */
    nread = bd_read(p_sys->bluray, data, size);

    if( nread == 0 ) {
        p_access->info.b_eof = true;
    }
    else if( nread > 0 ) {
        p_access->info.i_pos += nread;
    }

    return nread;
}