]> git.sesse.net Git - vlc/blob - modules/access/bluray.c
bluray: Hack to avoid crashing when changing title.
[vlc] / modules / access / bluray.c
1 /*****************************************************************************
2  * bluray.c: Blu-ray disc support plugin
3  *****************************************************************************
4  * Copyright © 2010-2011 VideoLAN, VLC authors and libbluray AUTHORS
5  *
6  * Authors: Jean-Baptiste Kempf <jb@videolan.org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <assert.h>
28 #include <limits.h>                         /* PATH_MAX */
29 #if defined (HAVE_MNTENT_H) && defined(HAVE_SYS_STAT_H)
30 #include <mntent.h>
31 #include <sys/stat.h>
32 #endif
33
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 */
39
40 #include <libbluray/bluray.h>
41 #include <libbluray/meta_data.h>
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46
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" )
50
51 /* Callbacks */
52 static int  blurayOpen ( vlc_object_t * );
53 static void blurayClose( vlc_object_t * );
54
55 vlc_module_begin ()
56     set_shortname( N_("BluRay") )
57     set_description( N_("Blu-Ray Disc support (libbluray)") )
58
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 )
63
64     add_shortcut( "bluray", "file" )
65
66     set_callbacks( blurayOpen, blurayClose )
67 vlc_module_end ()
68
69
70 struct demux_sys_t
71 {
72     BLURAY         *bluray;
73
74     /* Titles */
75     unsigned int    i_title;
76     unsigned int    i_longest_title;
77     input_title_t **pp_title;
78
79     /* Menus */
80     bool            b_menu;
81
82     /* TS stream */
83     stream_t       *p_parser;
84 };
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89 static int     blurayControl(demux_t *, int, va_list);
90 static int     blurayDemux(demux_t *);
91
92 static int     blurayInitTitles(demux_t *p_demux );
93 static int     bluraySetTitle(demux_t *p_demux, int i_title);
94
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
98
99 /*****************************************************************************
100  * blurayOpen: module init function
101  *****************************************************************************/
102 static int blurayOpen( vlc_object_t *object )
103 {
104     demux_t *p_demux = (demux_t*)object;
105     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         return VLC_EGENERIC;
115     }
116
117     /* */
118     p_demux->p_sys = p_sys = malloc(sizeof(*p_sys));
119     if (unlikely(!p_sys)) {
120         return VLC_ENOMEM;
121     }
122     p_sys->p_parser = NULL;
123
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;
128
129     TAB_INIT( p_sys->i_title, p_sys->pp_title );
130
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';
135     }
136
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. */
139     struct stat st;
140     if ( !stat (bd_path, &st)) {
141         if (S_ISBLK (st.st_mode)) {
142             FILE* mtab = setmntent ("/proc/self/mounts", "r");
143             struct mntent* m;
144             struct mntent mbuf;
145             char buf [8192];
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';
150                     break;
151                 }
152             }
153             endmntent (mtab);
154         }
155     }
156 #endif /* HAVE_MNTENT_H && HAVE_SYS_STAT_H */
157     p_sys->bluray = bd_open(bd_path, NULL);
158     if (!p_sys->bluray) {
159         free(p_sys);
160         return VLC_EGENERIC;
161     }
162
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);
170
171     /* AACS */
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.");
176             goto error;
177         }
178         if (!disc_info->aacs_handled) {
179             error_msg = _("Your system AACS decoding library does not work. "
180                       "Missing keys?");
181             goto error;
182         }
183     }
184
185     /* BD+ */
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.");
190             goto error;
191         }
192         if (!disc_info->bdplus_handled) {
193             error_msg = _("Your system BD+ decoding library does not work. "
194                       "Missing configuration?");
195             goto error;
196         }
197     }
198
199     /* Get titles and chapters */
200     if (blurayInitTitles(p_demux) != VLC_SUCCESS) {
201         goto error;
202     }
203
204     /*
205      * Initialize the event queue, so we can receive events in blurayDemux(Menu).
206      */
207     bd_get_event(p_sys->bluray, NULL);
208
209     p_sys->b_menu = var_InheritBool( p_demux, "bluray-menu" );
210     if ( p_sys->b_menu )
211     {
212         /*
213          * libbluray will start playback from "First-Title" title
214          * Therefore, We don't have to select any title.
215          */
216         bd_play( p_sys->bluray );
217     }
218     else
219     {
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);
225         }
226
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 );
230             goto error;
231         }
232     }
233
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");
237         goto error;
238     }
239
240     p_demux->pf_control = blurayControl;
241     p_demux->pf_demux   = blurayDemux;
242
243     return VLC_SUCCESS;
244
245 error:
246     if (error_msg)
247         dialog_Fatal(p_demux, _("Blu-Ray error"), "%s", error_msg);
248     blurayClose(object);
249     return VLC_EGENERIC;
250 }
251
252
253 /*****************************************************************************
254  * blurayClose: module destroy function
255  *****************************************************************************/
256 static void blurayClose( vlc_object_t *object )
257 {
258     demux_t *p_demux = (demux_t*)object;
259     demux_sys_t *p_sys = p_demux->p_sys;
260
261     if (p_sys->p_parser)
262         stream_Delete(p_sys->p_parser);
263
264     /* Titles */
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 );
268
269     /* bd_close( NULL ) can crash */
270     assert(p_sys->bluray);
271     bd_close(p_sys->bluray);
272     free(p_sys);
273 }
274
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     int64_t duration = 0;
283
284     for (unsigned int i = 0; i < i_title; i++) {
285         input_title_t *t = vlc_input_title_New();
286         if (!t)
287             break;
288
289         BLURAY_TITLE_INFO *title_info = bd_get_title_info(p_sys->bluray, i, 0);
290         if (!title_info)
291             break;
292         t->i_length = FROM_TICKS(title_info->duration);
293
294         if (t->i_length > duration) {
295             duration = t->i_length;
296             p_sys->i_longest_title = i;
297         }
298
299         for ( unsigned int j = 0; j < title_info->chapter_count; j++) {
300             seekpoint_t *s = vlc_seekpoint_New();
301             if (!s)
302                 break;
303             s->i_time_offset = title_info->chapters[j].offset;
304
305             TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
306         }
307         TAB_APPEND( p_sys->i_title, p_sys->pp_title, t );
308         bd_free_title_info(title_info);
309     }
310     return VLC_SUCCESS;
311 }
312
313 static void blurayResetParser( demux_t *p_demux )
314 {
315     /*
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.
319      */
320     demux_sys_t *p_sys = p_demux->p_sys;
321     if (!p_sys->p_parser)
322         return;
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");
327     }
328 }
329
330 static void blurayUpdateTitle( demux_t *p_demux, int i_title )
331 {
332     blurayResetParser(p_demux);
333     if (i_title >= p_demux->p_sys->i_title)
334         return;
335
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;
340 }
341
342 /*****************************************************************************
343  * bluraySetTitle: select new BD title
344  *****************************************************************************/
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     else if ((unsigned)i_title > p_sys->i_title)
353         return VLC_EGENERIC;
354
355     msg_Dbg( p_demux, "Selecting Title %i", i_title);
356
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);
360         return VLC_EGENERIC;
361     }
362     blurayUpdateTitle( p_demux, i_title );
363
364     return VLC_SUCCESS;
365 }
366
367
368 /*****************************************************************************
369  * blurayControl: handle the controls
370  *****************************************************************************/
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     bool     *pb_bool;
375     int64_t  *pi_64;
376
377     switch (query) {
378         case DEMUX_CAN_SEEK:
379         case DEMUX_CAN_PAUSE:
380         case DEMUX_CAN_CONTROL_PACE:
381              pb_bool = (bool*)va_arg( args, bool * );
382              *pb_bool = true;
383              break;
384
385         case DEMUX_GET_PTS_DELAY:
386             pi_64 = (int64_t*)va_arg( args, int64_t * );
387             *pi_64 =
388                 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
389             break;
390
391         case DEMUX_SET_PAUSE_STATE:
392             /* Nothing to do */
393             break;
394
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                 return VLC_EGENERIC;
400             break;
401         }
402         case DEMUX_SET_SEEKPOINT:
403         {
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;
407             break;
408         }
409
410         case DEMUX_GET_TITLE_INFO:
411         {
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* );
416
417             /* */
418             *pi_title_offset   = 0;
419             *pi_chapter_offset = 0;
420
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]);
426
427             return VLC_SUCCESS;
428         }
429
430         case DEMUX_GET_LENGTH:
431         {
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;
434             return VLC_SUCCESS;
435         }
436         case DEMUX_SET_TIME:
437         {
438             int64_t i_time = (int64_t)va_arg(args, int64_t);
439             bd_seek_time(p_sys->bluray, TO_TICKS(i_time));
440             return VLC_SUCCESS;
441         }
442         case DEMUX_GET_TIME:
443         {
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));
446             return VLC_SUCCESS;
447         }
448
449         case DEMUX_GET_POSITION:
450         {
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;
454             return VLC_SUCCESS;
455         }
456         case DEMUX_SET_POSITION:
457         {
458             double f_position = (double)va_arg(args, double);
459             bd_seek_time(p_sys->bluray, TO_TICKS(f_position*CUR_LENGTH));
460             return VLC_SUCCESS;
461         }
462
463         case DEMUX_GET_META:
464         {
465             const struct meta_dl *meta = bd_get_meta(p_sys->bluray);
466             if(!meta)
467                 return VLC_EGENERIC;
468
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             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);
476
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);
479
480             if (meta->thumb_count > 0 && meta->thumbnails) {
481                 vlc_meta_SetArtURL(p_meta, meta->thumbnails[0].path);
482             }
483
484             return VLC_SUCCESS;
485         }
486
487         case DEMUX_CAN_RECORD:
488         case DEMUX_GET_FPS:
489         case DEMUX_SET_GROUP:
490         case DEMUX_HAS_UNSUPPORTED_META:
491         case DEMUX_GET_ATTACHMENTS:
492             return VLC_EGENERIC;
493         default:
494             msg_Warn( p_demux, "unimplemented query (%d) in control", query );
495             return VLC_EGENERIC;
496     }
497     return VLC_SUCCESS;
498 }
499
500 static void blurayHandleEvent( demux_t *p_demux, const BD_EVENT *e )
501 {
502     demux_sys_t *p_sys = p_demux->p_sys;
503
504     switch (e->event)
505     {
506         case BD_EVENT_TITLE:
507             blurayUpdateTitle( p_demux, e->param );
508             break;
509         case BD_EVENT_PLAYITEM:
510             break;
511         case BD_EVENT_AUDIO_STREAM:
512             break;
513         case BD_EVENT_CHAPTER:
514             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
515             p_demux->info.i_seekpoint = 0;
516             break;
517         case BD_EVENT_ANGLE:
518         case BD_EVENT_IG_STREAM:
519         default:
520             msg_Warn( p_demux, "event: %d param: %d", e->event, e->param );
521             break;
522     }
523 }
524
525 static void blurayHandleEvents( demux_t *p_demux )
526 {
527     BD_EVENT e;
528
529     while (bd_get_event(p_demux->p_sys->bluray, &e))
530     {
531         blurayHandleEvent(p_demux, &e);
532     }
533 }
534
535 #define BD_TS_PACKET_SIZE (192)
536 #define NB_TS_PACKETS (200)
537
538 static int blurayDemux(demux_t *p_demux)
539 {
540     demux_sys_t *p_sys = p_demux->p_sys;
541
542     block_t *p_block = block_New(p_demux, NB_TS_PACKETS * (int64_t)BD_TS_PACKET_SIZE);
543     if (!p_block) {
544         return -1;
545     }
546
547     int nread = -1;
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);
552         if (nread < 0) {
553             block_Release(p_block);
554             return nread;
555         }
556     }
557     else {
558         BD_EVENT e;
559         nread = bd_read_ext( p_sys->bluray, p_block->p_buffer,
560                 NB_TS_PACKETS * BD_TS_PACKET_SIZE, &e );
561         if ( nread == 0 ) {
562             if ( e.event == BD_EVENT_NONE )
563                 msg_Info( p_demux, "We reached the end of a title" );
564             else
565                 blurayHandleEvent( p_demux, &e );
566             block_Release(p_block);
567             return 1;
568         }
569     }
570
571     p_block->i_buffer = nread;
572
573     stream_DemuxSend( p_sys->p_parser, p_block );
574
575     return 1;
576 }