1 /*****************************************************************************
2 * bluray.c: Blu-ray disc support plugin
3 *****************************************************************************
4 * Copyright © 2010-2011 VideoLAN, VLC authors and libbluray AUTHORS
6 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
28 #include <limits.h> /* PATH_MAX */
29 #if defined (HAVE_MNTENT_H) && defined(HAVE_SYS_STAT_H)
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_demux.h> /* demux_t */
37 #include <vlc_input.h> /* Seekpoints, chapters */
38 #include <vlc_dialog.h> /* BD+/AACS warnings */
40 #include <libbluray/bluray.h>
41 #include <libbluray/meta_data.h>
43 /*****************************************************************************
45 *****************************************************************************/
47 #define BD_MENU_TEXT N_( "Bluray menus" )
48 #define BD_MENU_LONGTEXT N_( "Use bluray menus. If disabled, "\
49 "the movie will start directly" )
52 static int blurayOpen ( vlc_object_t * );
53 static void blurayClose( vlc_object_t * );
56 set_shortname( N_("BluRay") )
57 set_description( N_("Blu-Ray Disc support (libbluray)") )
59 set_category( CAT_INPUT )
60 set_subcategory( SUBCAT_INPUT_ACCESS )
61 set_capability( "access_demux", 200)
62 add_bool( "bluray-menu", false, BD_MENU_TEXT, BD_MENU_LONGTEXT, false )
64 add_shortcut( "bluray", "file" )
66 set_callbacks( blurayOpen, blurayClose )
76 unsigned int i_longest_title;
77 input_title_t **pp_title;
86 /*****************************************************************************
88 *****************************************************************************/
89 static int blurayControl(demux_t *, int, va_list);
90 static int blurayDemux(demux_t *);
92 static int blurayInitTitles(demux_t *p_demux );
93 static int bluraySetTitle(demux_t *p_demux, int i_title);
95 #define FROM_TICKS(a) (a*CLOCK_FREQ / INT64_C(90000))
96 #define TO_TICKS(a) (a*INT64_C(90000)/CLOCK_FREQ)
97 #define CUR_LENGTH p_sys->pp_title[p_demux->info.i_title]->i_length
99 /*****************************************************************************
100 * blurayOpen: module init function
101 *****************************************************************************/
102 static int blurayOpen( vlc_object_t *object )
104 demux_t *p_demux = (demux_t*)object;
109 char bd_path[PATH_MAX] = { '\0' };
110 const char *error_msg = NULL;
112 if (strcmp(p_demux->psz_access, "bluray")) {
113 // TODO BDMV support, once we figure out what to do in libbluray
118 p_demux->p_sys = p_sys = malloc(sizeof(*p_sys));
119 if (unlikely(!p_sys)) {
122 p_sys->p_parser = NULL;
124 /* init demux info fields */
125 p_demux->info.i_update = 0;
126 p_demux->info.i_title = 0;
127 p_demux->info.i_seekpoint = 0;
129 TAB_INIT( p_sys->i_title, p_sys->pp_title );
131 /* store current bd_path */
132 if (p_demux->psz_file) {
133 strncpy(bd_path, p_demux->psz_file, sizeof(bd_path));
134 bd_path[PATH_MAX - 1] = '\0';
137 #if defined (HAVE_MNTENT_H) && defined (HAVE_SYS_STAT_H)
138 /* If we're passed a block device, try to convert it to the mount point. */
140 if ( !stat (bd_path, &st)) {
141 if (S_ISBLK (st.st_mode)) {
142 FILE* mtab = setmntent ("/proc/self/mounts", "r");
146 while ((m = getmntent_r (mtab, &mbuf, buf, sizeof(buf))) != NULL) {
147 if (!strcmp (m->mnt_fsname, bd_path)) {
148 strncpy (bd_path, m->mnt_dir, sizeof(bd_path));
149 bd_path[sizeof(bd_path) - 1] = '\0';
156 #endif /* HAVE_MNTENT_H && HAVE_SYS_STAT_H */
157 p_sys->bluray = bd_open(bd_path, NULL);
158 if (!p_sys->bluray) {
163 /* Warning the user about AACS/BD+ */
164 const BLURAY_DISC_INFO *disc_info = bd_get_disc_info(p_sys->bluray);
165 msg_Info(p_demux, "First play: %i, Top menu: %i\n"
166 "HDMV Titles: %i, BD-J Titles: %i, Other: %i",
167 disc_info->first_play_supported, disc_info->top_menu_supported,
168 disc_info->num_hdmv_titles, disc_info->num_bdj_titles,
169 disc_info->num_unsupported_titles);
172 if (disc_info->aacs_detected) {
173 if (!disc_info->libaacs_detected) {
174 error_msg = _("This Blu-Ray Disc needs a library for AACS decoding, "
175 "and your system does not have it.");
178 if (!disc_info->aacs_handled) {
179 error_msg = _("Your system AACS decoding library does not work. "
186 if (disc_info->bdplus_detected) {
187 if (!disc_info->libbdplus_detected) {
188 error_msg = _("This Blu-Ray Disc needs a library for BD+ decoding, "
189 "and your system does not have it.");
192 if (!disc_info->bdplus_handled) {
193 error_msg = _("Your system BD+ decoding library does not work. "
194 "Missing configuration?");
199 /* Get titles and chapters */
200 if (blurayInitTitles(p_demux) != VLC_SUCCESS) {
205 * Initialize the event queue, so we can receive events in blurayDemux(Menu).
207 bd_get_event(p_sys->bluray, NULL);
209 p_sys->b_menu = var_InheritBool( p_demux, "bluray-menu" );
213 * libbluray will start playback from "First-Title" title
214 * Therefore, We don't have to select any title.
216 bd_play( p_sys->bluray );
220 /* get title request */
221 if ((pos_title = strrchr(bd_path, ':'))) {
222 /* found character ':' for title information */
223 *(pos_title++) = '\0';
224 i_title = atoi(pos_title);
227 /* set start title number */
228 if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS) {
229 msg_Err( p_demux, "Could not set the title %d", i_title );
234 p_sys->p_parser = stream_DemuxNew(p_demux, "ts", p_demux->out);
235 if (!p_sys->p_parser) {
236 msg_Err(p_demux, "Failed to create TS demuxer");
240 p_demux->pf_control = blurayControl;
241 p_demux->pf_demux = blurayDemux;
247 dialog_Fatal(p_demux, _("Blu-Ray error"), "%s", error_msg);
253 /*****************************************************************************
254 * blurayClose: module destroy function
255 *****************************************************************************/
256 static void blurayClose( vlc_object_t *object )
258 demux_t *p_demux = (demux_t*)object;
259 demux_sys_t *p_sys = p_demux->p_sys;
262 stream_Delete(p_sys->p_parser);
265 for (unsigned int i = 0; i < p_sys->i_title; i++)
266 vlc_input_title_Delete(p_sys->pp_title[i]);
267 TAB_CLEAN( p_sys->i_title, p_sys->pp_title );
269 /* bd_close( NULL ) can crash */
270 assert(p_sys->bluray);
271 bd_close(p_sys->bluray);
276 static int blurayInitTitles(demux_t *p_demux )
278 demux_sys_t *p_sys = p_demux->p_sys;
280 /* get and set the titles */
281 unsigned i_title = bd_get_titles(p_sys->bluray, TITLES_RELEVANT, 60);
282 int64_t duration = 0;
284 for (unsigned int i = 0; i < i_title; i++) {
285 input_title_t *t = vlc_input_title_New();
289 BLURAY_TITLE_INFO *title_info = bd_get_title_info(p_sys->bluray, i, 0);
292 t->i_length = FROM_TICKS(title_info->duration);
294 if (t->i_length > duration) {
295 duration = t->i_length;
296 p_sys->i_longest_title = i;
299 for ( unsigned int j = 0; j < title_info->chapter_count; j++) {
300 seekpoint_t *s = vlc_seekpoint_New();
303 s->i_time_offset = title_info->chapters[j].offset;
305 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
307 TAB_APPEND( p_sys->i_title, p_sys->pp_title, t );
308 bd_free_title_info(title_info);
313 static void blurayResetParser( demux_t *p_demux )
316 * This is a hack and will have to be removed.
317 * The parser should be flushed, and not destroy/created each time
318 * we are changing title.
320 demux_sys_t *p_sys = p_demux->p_sys;
321 if (!p_sys->p_parser)
323 stream_Delete(p_sys->p_parser);
324 p_sys->p_parser = stream_DemuxNew(p_demux, "ts", p_demux->out);
325 if (!p_sys->p_parser) {
326 msg_Err(p_demux, "Failed to create TS demuxer");
330 static void blurayUpdateTitle( demux_t *p_demux, int i_title )
332 blurayResetParser(p_demux);
333 if (i_title >= p_demux->p_sys->i_title)
336 /* read title info and init some values */
337 p_demux->info.i_title = i_title;
338 p_demux->info.i_seekpoint = 0;
339 p_demux->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
342 /*****************************************************************************
343 * bluraySetTitle: select new BD title
344 *****************************************************************************/
345 static int bluraySetTitle(demux_t *p_demux, int i_title)
347 demux_sys_t *p_sys = p_demux->p_sys;
349 /* Looking for the main title, ie the longest duration */
351 i_title = p_sys->i_longest_title;
352 else if ((unsigned)i_title > p_sys->i_title)
355 msg_Dbg( p_demux, "Selecting Title %i", i_title);
357 /* Select Blu-Ray title */
358 if (bd_select_title(p_demux->p_sys->bluray, i_title) == 0 ) {
359 msg_Err(p_demux, "cannot select bd title '%d'", p_demux->info.i_title);
362 blurayUpdateTitle( p_demux, i_title );
368 /*****************************************************************************
369 * blurayControl: handle the controls
370 *****************************************************************************/
371 static int blurayControl(demux_t *p_demux, int query, va_list args)
373 demux_sys_t *p_sys = p_demux->p_sys;
379 case DEMUX_CAN_PAUSE:
380 case DEMUX_CAN_CONTROL_PACE:
381 pb_bool = (bool*)va_arg( args, bool * );
385 case DEMUX_GET_PTS_DELAY:
386 pi_64 = (int64_t*)va_arg( args, int64_t * );
388 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
391 case DEMUX_SET_PAUSE_STATE:
395 case DEMUX_SET_TITLE:
397 int i_title = (int)va_arg( args, int );
398 if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS)
402 case DEMUX_SET_SEEKPOINT:
404 int i_chapter = (int)va_arg( args, int );
405 bd_seek_chapter( p_sys->bluray, i_chapter );
406 p_demux->info.i_update = INPUT_UPDATE_SEEKPOINT;
410 case DEMUX_GET_TITLE_INFO:
412 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
413 int *pi_int = (int*)va_arg( args, int* );
414 int *pi_title_offset = (int*)va_arg( args, int* );
415 int *pi_chapter_offset = (int*)va_arg( args, int* );
418 *pi_title_offset = 0;
419 *pi_chapter_offset = 0;
421 /* Duplicate local title infos */
422 *pi_int = p_sys->i_title;
423 *ppp_title = calloc( p_sys->i_title, sizeof(input_title_t **) );
424 for( unsigned int i = 0; i < p_sys->i_title; i++ )
425 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->pp_title[i]);
430 case DEMUX_GET_LENGTH:
432 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;
438 int64_t i_time = (int64_t)va_arg(args, int64_t);
439 bd_seek_time(p_sys->bluray, TO_TICKS(i_time));
444 int64_t *pi_time = (int64_t*)va_arg(args, int64_t *);
445 *pi_time = (int64_t)FROM_TICKS(bd_tell_time(p_sys->bluray));
449 case DEMUX_GET_POSITION:
451 double *pf_position = (double*)va_arg( args, double * );
452 *pf_position = p_demux->info.i_title < p_sys->i_title ?
453 (double)FROM_TICKS(bd_tell_time(p_sys->bluray))/CUR_LENGTH : 0.0;
456 case DEMUX_SET_POSITION:
458 double f_position = (double)va_arg(args, double);
459 bd_seek_time(p_sys->bluray, TO_TICKS(f_position*CUR_LENGTH));
465 const struct meta_dl *meta = bd_get_meta(p_sys->bluray);
469 vlc_meta_t *p_meta = (vlc_meta_t *) va_arg (args, vlc_meta_t*);
471 if (!EMPTY_STR(meta->di_name)) vlc_meta_SetTitle(p_meta, meta->di_name);
473 if (!EMPTY_STR(meta->language_code)) vlc_meta_AddExtra(p_meta, "Language", meta->language_code);
474 if (!EMPTY_STR(meta->filename)) vlc_meta_AddExtra(p_meta, "Filename", meta->filename);
475 if (!EMPTY_STR(meta->di_alternative)) vlc_meta_AddExtra(p_meta, "Alternative", meta->di_alternative);
477 // if (meta->di_set_number > 0) vlc_meta_SetTrackNum(p_meta, meta->di_set_number);
478 // if (meta->di_num_sets > 0) vlc_meta_AddExtra(p_meta, "Discs numbers in Set", meta->di_num_sets);
480 if (meta->thumb_count > 0 && meta->thumbnails) {
481 vlc_meta_SetArtURL(p_meta, meta->thumbnails[0].path);
487 case DEMUX_CAN_RECORD:
489 case DEMUX_SET_GROUP:
490 case DEMUX_HAS_UNSUPPORTED_META:
491 case DEMUX_GET_ATTACHMENTS:
494 msg_Warn( p_demux, "unimplemented query (%d) in control", query );
500 static void blurayHandleEvent( demux_t *p_demux, const BD_EVENT *e )
502 demux_sys_t *p_sys = p_demux->p_sys;
507 blurayUpdateTitle( p_demux, e->param );
509 case BD_EVENT_PLAYITEM:
511 case BD_EVENT_AUDIO_STREAM:
513 case BD_EVENT_CHAPTER:
514 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
515 p_demux->info.i_seekpoint = 0;
518 case BD_EVENT_IG_STREAM:
520 msg_Warn( p_demux, "event: %d param: %d", e->event, e->param );
525 static void blurayHandleEvents( demux_t *p_demux )
529 while (bd_get_event(p_demux->p_sys->bluray, &e))
531 blurayHandleEvent(p_demux, &e);
535 #define BD_TS_PACKET_SIZE (192)
536 #define NB_TS_PACKETS (200)
538 static int blurayDemux(demux_t *p_demux)
540 demux_sys_t *p_sys = p_demux->p_sys;
542 block_t *p_block = block_New(p_demux, NB_TS_PACKETS * (int64_t)BD_TS_PACKET_SIZE);
548 if (p_sys->b_menu == false) {
549 blurayHandleEvents(p_demux);
550 nread = bd_read(p_sys->bluray, p_block->p_buffer,
551 NB_TS_PACKETS * BD_TS_PACKET_SIZE);
553 block_Release(p_block);
559 nread = bd_read_ext( p_sys->bluray, p_block->p_buffer,
560 NB_TS_PACKETS * BD_TS_PACKET_SIZE, &e );
562 if ( e.event == BD_EVENT_NONE )
563 msg_Info( p_demux, "We reached the end of a title" );
565 blurayHandleEvent( p_demux, &e );
566 block_Release(p_block);
571 p_block->i_buffer = nread;
573 stream_DemuxSend( p_sys->p_parser, p_block );