]> git.sesse.net Git - vlc/blob - modules/access/bluray.c
Bluray: try to find mount point if block device file is passed.
[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 /* Callbacks */
48 static int  blurayOpen ( vlc_object_t * );
49 static void blurayClose( vlc_object_t * );
50
51 vlc_module_begin ()
52     set_shortname( N_("BluRay") )
53     set_description( N_("Blu-Ray Disc support (libbluray)") )
54
55     set_category( CAT_INPUT )
56     set_subcategory( SUBCAT_INPUT_ACCESS )
57     set_capability( "access_demux", 200)
58
59     add_shortcut( "bluray", "file" )
60
61     set_callbacks( blurayOpen, blurayClose )
62 vlc_module_end ()
63
64
65 struct demux_sys_t
66 {
67     BLURAY         *bluray;
68
69     /* Titles */
70     unsigned int    i_title;
71     unsigned int    i_longest_title;
72     input_title_t **pp_title;
73
74     /* TS stream */
75     stream_t       *p_parser;
76 };
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 static int     blurayControl(demux_t *, int, va_list);
82 static int     blurayDemux  (demux_t *);
83
84 static int     blurayInitTitles(demux_t *p_demux );
85 static int     bluraySetTitle(demux_t *p_demux, int i_title);
86
87 #define FROM_TICKS(a) (a*CLOCK_FREQ / INT64_C(90000))
88 #define TO_TICKS(a)   (a*INT64_C(90000)/CLOCK_FREQ)
89 #define CUR_LENGTH    p_sys->pp_title[p_demux->info.i_title]->i_length
90
91 /*****************************************************************************
92  * blurayOpen: module init function
93  *****************************************************************************/
94 static int blurayOpen( vlc_object_t *object )
95 {
96     demux_t *p_demux = (demux_t*)object;
97     demux_sys_t *p_sys;
98
99     char *pos_title;
100     int i_title = -1;
101     char bd_path[PATH_MAX] = { '\0' };
102     const char *error_msg = NULL;
103
104     if (strcmp(p_demux->psz_access, "bluray")) {
105         // TODO BDMV support, once we figure out what to do in libbluray
106         return VLC_EGENERIC;
107     }
108
109     /* */
110     p_demux->p_sys = p_sys = malloc(sizeof(*p_sys));
111     if (unlikely(!p_sys)) {
112         return VLC_ENOMEM;
113     }
114     p_sys->p_parser = NULL;
115
116     /* init demux info fields */
117     p_demux->info.i_update    = 0;
118     p_demux->info.i_title     = 0;
119     p_demux->info.i_seekpoint = 0;
120
121     TAB_INIT( p_sys->i_title, p_sys->pp_title );
122
123     /* store current bd_path */
124     if (p_demux->psz_file) {
125         strncpy(bd_path, p_demux->psz_file, sizeof(bd_path));
126         bd_path[PATH_MAX - 1] = '\0';
127     }
128
129 #if defined (HAVE_MNTENT_H) && defined (HAVE_SYS_STAT_H)
130     /* If we're passed a block device, try to convert it to the mount point. */
131     struct stat st;
132     if ( !stat (bd_path, &st)) {
133         if (S_ISBLK (st.st_mode)) {
134             FILE* mtab = setmntent ("/proc/self/mounts", "r");
135             struct mntent* m;
136             struct mntent mbuf;
137             char buf [8192];
138             while ((m = getmntent_r (mtab, &mbuf, buf, sizeof(buf))) != NULL) {
139                 if (!strcmp (m->mnt_fsname, bd_path)) {
140                     strncpy (bd_path, m->mnt_dir, sizeof(bd_path));
141                     bd_path[sizeof(bd_path) - 1] = '\0';
142                     break;
143                 }
144             }
145             endmntent (mtab);
146         }
147     }
148 #endif /* HAVE_MNTENT_H && HAVE_SYS_STAT_H */
149     p_sys->bluray = bd_open(bd_path, NULL);
150     if (!p_sys->bluray) {
151         free(p_sys);
152         return VLC_EGENERIC;
153     }
154
155     /* Warning the user about AACS/BD+ */
156     const BLURAY_DISC_INFO *disc_info = bd_get_disc_info(p_sys->bluray);
157     msg_Info(p_demux, "First play: %i, Top menu: %i\n"
158                       "HDMV Titles: %i, BD-J Titles: %i, Other: %i",
159              disc_info->first_play_supported, disc_info->top_menu_supported,
160              disc_info->num_hdmv_titles, disc_info->num_bdj_titles,
161              disc_info->num_unsupported_titles);
162
163     /* AACS */
164     if (disc_info->aacs_detected) {
165         if (!disc_info->libaacs_detected) {
166             error_msg = _("This Blu-Ray Disc needs a library for AACS decoding, "
167                       "and your system does not have it.");
168             goto error;
169         }
170         if (!disc_info->aacs_handled) {
171             error_msg = _("Your system AACS decoding library does not work. "
172                       "Missing keys?");
173             goto error;
174         }
175     }
176
177     /* BD+ */
178     if (disc_info->bdplus_detected) {
179         if (!disc_info->libbdplus_detected) {
180             error_msg = _("This Blu-Ray Disc needs a library for BD+ decoding, "
181                       "and your system does not have it.");
182             goto error;
183         }
184         if (!disc_info->bdplus_handled) {
185             error_msg = _("Your system BD+ decoding library does not work. "
186                       "Missing configuration?");
187             goto error;
188         }
189     }
190
191     /* Get titles and chapters */
192     if (blurayInitTitles(p_demux) != VLC_SUCCESS) {
193         goto error;
194     }
195
196     /* get title request */
197     if ((pos_title = strrchr(bd_path, ':'))) {
198         /* found character ':' for title information */
199         *(pos_title++) = '\0';
200         i_title = atoi(pos_title);
201     }
202
203     /* set start title number */
204     if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS) {
205         msg_Err( p_demux, "Could not set the title %d", i_title );
206         goto error;
207     }
208
209     p_sys->p_parser   = stream_DemuxNew(p_demux, "ts", p_demux->out);
210     if (!p_sys->p_parser) {
211         msg_Err(p_demux, "Failed to create TS demuxer");
212         goto error;
213     }
214
215     p_demux->pf_control = blurayControl;
216     p_demux->pf_demux   = blurayDemux;
217
218     return VLC_SUCCESS;
219
220 error:
221     if (error_msg)
222         dialog_Fatal(p_demux, _("Blu-Ray error"), "%s", error_msg);
223     blurayClose(object);
224     return VLC_EGENERIC;
225 }
226
227
228 /*****************************************************************************
229  * blurayClose: module destroy function
230  *****************************************************************************/
231 static void blurayClose( vlc_object_t *object )
232 {
233     demux_t *p_demux = (demux_t*)object;
234     demux_sys_t *p_sys = p_demux->p_sys;
235
236     if (p_sys->p_parser)
237         stream_Delete(p_sys->p_parser);
238
239     /* Titles */
240     for (unsigned int i = 0; i < p_sys->i_title; i++)
241         vlc_input_title_Delete(p_sys->pp_title[i]);
242     TAB_CLEAN( p_sys->i_title, p_sys->pp_title );
243
244     /* bd_close( NULL ) can crash */
245     assert(p_sys->bluray);
246     bd_close(p_sys->bluray);
247     free(p_sys);
248 }
249
250
251 static int blurayInitTitles(demux_t *p_demux )
252 {
253     demux_sys_t *p_sys = p_demux->p_sys;
254
255     /* get and set the titles */
256     unsigned i_title = bd_get_titles(p_sys->bluray, TITLES_RELEVANT, 60);
257     int64_t duration = 0;
258
259     for (unsigned int i = 0; i < i_title; i++) {
260         input_title_t *t = vlc_input_title_New();
261         if (!t)
262             break;
263
264         BLURAY_TITLE_INFO *title_info = bd_get_title_info(p_sys->bluray, i, 0);
265         if (!title_info)
266             break;
267         t->i_length = FROM_TICKS(title_info->duration);
268
269         if (t->i_length > duration) {
270             duration = t->i_length;
271             p_sys->i_longest_title = i;
272         }
273
274         for ( unsigned int j = 0; j < title_info->chapter_count; j++) {
275             seekpoint_t *s = vlc_seekpoint_New();
276             if (!s)
277                 break;
278             s->i_time_offset = title_info->chapters[j].offset;
279
280             TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
281         }
282         TAB_APPEND( p_sys->i_title, p_sys->pp_title, t );
283         bd_free_title_info(title_info);
284     }
285     return VLC_SUCCESS;
286 }
287
288
289 /*****************************************************************************
290  * bluraySetTitle: select new BD title
291  *****************************************************************************/
292 static int bluraySetTitle(demux_t *p_demux, int i_title)
293 {
294     demux_sys_t *p_sys = p_demux->p_sys;
295
296     /* Looking for the main title, ie the longest duration */
297     if (i_title < 0)
298         i_title = p_sys->i_longest_title;
299     else if ((unsigned)i_title > p_sys->i_title)
300         return VLC_EGENERIC;
301
302     msg_Dbg( p_demux, "Selecting Title %i", i_title);
303
304     /* Select Blu-Ray title */
305     if (bd_select_title(p_demux->p_sys->bluray, i_title) == 0 ) {
306         msg_Err(p_demux, "cannot select bd title '%d'", p_demux->info.i_title);
307         return VLC_EGENERIC;
308     }
309
310     /* read title info and init some values */
311     p_demux->info.i_title = i_title;
312     p_demux->info.i_seekpoint = 0;
313     p_demux->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
314
315     return VLC_SUCCESS;
316 }
317
318
319 /*****************************************************************************
320  * blurayControl: handle the controls
321  *****************************************************************************/
322 static int blurayControl(demux_t *p_demux, int query, va_list args)
323 {
324     demux_sys_t *p_sys = p_demux->p_sys;
325     bool     *pb_bool;
326     int64_t  *pi_64;
327
328     switch (query) {
329         case DEMUX_CAN_SEEK:
330         case DEMUX_CAN_PAUSE:
331         case DEMUX_CAN_CONTROL_PACE:
332              pb_bool = (bool*)va_arg( args, bool * );
333              *pb_bool = true;
334              break;
335
336         case DEMUX_GET_PTS_DELAY:
337             pi_64 = (int64_t*)va_arg( args, int64_t * );
338             *pi_64 =
339                 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
340             break;
341
342         case DEMUX_SET_PAUSE_STATE:
343             /* Nothing to do */
344             break;
345
346         case DEMUX_SET_TITLE:
347         {
348             int i_title = (int)va_arg( args, int );
349             if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS)
350                 return VLC_EGENERIC;
351             break;
352         }
353         case DEMUX_SET_SEEKPOINT:
354         {
355             int i_chapter = (int)va_arg( args, int );
356             bd_seek_chapter( p_sys->bluray, i_chapter );
357             p_demux->info.i_update = INPUT_UPDATE_SEEKPOINT;
358             break;
359         }
360
361         case DEMUX_GET_TITLE_INFO:
362         {
363             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
364             int *pi_int             = (int*)va_arg( args, int* );
365             int *pi_title_offset    = (int*)va_arg( args, int* );
366             int *pi_chapter_offset  = (int*)va_arg( args, int* );
367
368             /* */
369             *pi_title_offset   = 0;
370             *pi_chapter_offset = 0;
371
372             /* Duplicate local title infos */
373             *pi_int = p_sys->i_title;
374             *ppp_title = calloc( p_sys->i_title, sizeof(input_title_t **) );
375             for( unsigned int i = 0; i < p_sys->i_title; i++ )
376                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->pp_title[i]);
377
378             return VLC_SUCCESS;
379         }
380
381         case DEMUX_GET_LENGTH:
382         {
383             int64_t *pi_length = (int64_t*)va_arg(args, int64_t *);
384             *pi_length = CUR_LENGTH;
385             return VLC_SUCCESS;
386         }
387         case DEMUX_SET_TIME:
388         {
389             int64_t i_time = (int64_t)va_arg(args, int64_t);
390             bd_seek_time(p_sys->bluray, TO_TICKS(i_time));
391             return VLC_SUCCESS;
392         }
393         case DEMUX_GET_TIME:
394         {
395             int64_t *pi_time = (int64_t*)va_arg(args, int64_t *);
396             *pi_time = (int64_t)FROM_TICKS(bd_tell_time(p_sys->bluray));
397             return VLC_SUCCESS;
398         }
399
400         case DEMUX_GET_POSITION:
401         {
402             double *pf_position = (double*)va_arg( args, double * );
403             *pf_position = (double)FROM_TICKS(bd_tell_time(p_sys->bluray))/CUR_LENGTH;
404             return VLC_SUCCESS;
405         }
406         case DEMUX_SET_POSITION:
407         {
408             double f_position = (double)va_arg(args, double);
409             bd_seek_time(p_sys->bluray, TO_TICKS(f_position*CUR_LENGTH));
410             return VLC_SUCCESS;
411         }
412
413         case DEMUX_GET_META:
414         {
415             struct meta_dl *meta = bd_get_meta(p_sys->bluray);
416             if(!meta)
417                 return VLC_EGENERIC;
418
419             vlc_meta_t *p_meta = (vlc_meta_t *) va_arg (args, vlc_meta_t*);
420
421             if (!EMPTY_STR(meta->di_name)) vlc_meta_SetTitle(p_meta, meta->di_name);
422
423             if (!EMPTY_STR(meta->language_code)) vlc_meta_AddExtra(p_meta, "Language", meta->language_code);
424             if (!EMPTY_STR(meta->filename)) vlc_meta_AddExtra(p_meta, "Filename", meta->filename);
425             if (!EMPTY_STR(meta->di_alternative)) vlc_meta_AddExtra(p_meta, "Alternative", meta->di_alternative);
426
427             // if (meta->di_set_number > 0) vlc_meta_SetTrackNum(p_meta, meta->di_set_number);
428             // if (meta->di_num_sets > 0) vlc_meta_AddExtra(p_meta, "Discs numbers in Set", meta->di_num_sets);
429
430             if (meta->thumb_count > 0 && meta->thumbnails) {
431                 vlc_meta_SetArtURL(p_meta, meta->thumbnails[0].path);
432             }
433
434             return VLC_SUCCESS;
435         }
436
437         case DEMUX_CAN_RECORD:
438         case DEMUX_GET_FPS:
439         case DEMUX_SET_GROUP:
440         case DEMUX_HAS_UNSUPPORTED_META:
441         case DEMUX_GET_ATTACHMENTS:
442             return VLC_EGENERIC;
443         default:
444             msg_Warn( p_demux, "unimplemented query (%d) in control", query );
445             return VLC_EGENERIC;
446     }
447     return VLC_SUCCESS;
448 }
449
450
451 #define BD_TS_PACKET_SIZE (192)
452 #define NB_TS_PACKETS (200)
453
454 static int blurayDemux(demux_t *p_demux)
455 {
456     demux_sys_t *p_sys = p_demux->p_sys;
457
458     block_t *p_block = block_New(p_demux, NB_TS_PACKETS * (int64_t)BD_TS_PACKET_SIZE);
459     if (!p_block) {
460         return -1;
461     }
462
463     int nread = bd_read(p_sys->bluray, p_block->p_buffer,
464                         NB_TS_PACKETS * BD_TS_PACKET_SIZE);
465     if (nread < 0) {
466         block_Release(p_block);
467         return nread;
468     }
469
470     p_block->i_buffer = nread;
471
472     stream_DemuxSend( p_sys->p_parser, p_block );
473
474     return 1;
475 }