]> git.sesse.net Git - vlc/blob - modules/access/bluray.c
bluray: Fixing a warning
[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     /*
197      * Initialize the event queue, so we can receive events in blurayDemux(Menu).
198      */
199     bd_get_event(p_sys->bluray, NULL);
200
201     /* get title request */
202     if ((pos_title = strrchr(bd_path, ':'))) {
203         /* found character ':' for title information */
204         *(pos_title++) = '\0';
205         i_title = atoi(pos_title);
206     }
207
208     /* set start title number */
209     if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS) {
210         msg_Err( p_demux, "Could not set the title %d", i_title );
211         goto error;
212     }
213
214     p_sys->p_parser   = stream_DemuxNew(p_demux, "ts", p_demux->out);
215     if (!p_sys->p_parser) {
216         msg_Err(p_demux, "Failed to create TS demuxer");
217         goto error;
218     }
219
220     p_demux->pf_control = blurayControl;
221     p_demux->pf_demux   = blurayDemux;
222
223     return VLC_SUCCESS;
224
225 error:
226     if (error_msg)
227         dialog_Fatal(p_demux, _("Blu-Ray error"), "%s", error_msg);
228     blurayClose(object);
229     return VLC_EGENERIC;
230 }
231
232
233 /*****************************************************************************
234  * blurayClose: module destroy function
235  *****************************************************************************/
236 static void blurayClose( vlc_object_t *object )
237 {
238     demux_t *p_demux = (demux_t*)object;
239     demux_sys_t *p_sys = p_demux->p_sys;
240
241     if (p_sys->p_parser)
242         stream_Delete(p_sys->p_parser);
243
244     /* Titles */
245     for (unsigned int i = 0; i < p_sys->i_title; i++)
246         vlc_input_title_Delete(p_sys->pp_title[i]);
247     TAB_CLEAN( p_sys->i_title, p_sys->pp_title );
248
249     /* bd_close( NULL ) can crash */
250     assert(p_sys->bluray);
251     bd_close(p_sys->bluray);
252     free(p_sys);
253 }
254
255
256 static int blurayInitTitles(demux_t *p_demux )
257 {
258     demux_sys_t *p_sys = p_demux->p_sys;
259
260     /* get and set the titles */
261     unsigned i_title = bd_get_titles(p_sys->bluray, TITLES_RELEVANT, 60);
262     int64_t duration = 0;
263
264     for (unsigned int i = 0; i < i_title; i++) {
265         input_title_t *t = vlc_input_title_New();
266         if (!t)
267             break;
268
269         BLURAY_TITLE_INFO *title_info = bd_get_title_info(p_sys->bluray, i, 0);
270         if (!title_info)
271             break;
272         t->i_length = FROM_TICKS(title_info->duration);
273
274         if (t->i_length > duration) {
275             duration = t->i_length;
276             p_sys->i_longest_title = i;
277         }
278
279         for ( unsigned int j = 0; j < title_info->chapter_count; j++) {
280             seekpoint_t *s = vlc_seekpoint_New();
281             if (!s)
282                 break;
283             s->i_time_offset = title_info->chapters[j].offset;
284
285             TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
286         }
287         TAB_APPEND( p_sys->i_title, p_sys->pp_title, t );
288         bd_free_title_info(title_info);
289     }
290     return VLC_SUCCESS;
291 }
292
293 static void blurayUpdateTitle( demux_t *p_demux, int i_title )
294 {
295     /* read title info and init some values */
296     p_demux->info.i_title = i_title;
297     p_demux->info.i_seekpoint = 0;
298     p_demux->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
299 }
300
301 /*****************************************************************************
302  * bluraySetTitle: select new BD title
303  *****************************************************************************/
304 static int bluraySetTitle(demux_t *p_demux, int i_title)
305 {
306     demux_sys_t *p_sys = p_demux->p_sys;
307
308     /* Looking for the main title, ie the longest duration */
309     if (i_title < 0)
310         i_title = p_sys->i_longest_title;
311     else if ((unsigned)i_title > p_sys->i_title)
312         return VLC_EGENERIC;
313
314     msg_Dbg( p_demux, "Selecting Title %i", i_title);
315
316     /* Select Blu-Ray title */
317     if (bd_select_title(p_demux->p_sys->bluray, i_title) == 0 ) {
318         msg_Err(p_demux, "cannot select bd title '%d'", p_demux->info.i_title);
319         return VLC_EGENERIC;
320     }
321     blurayUpdateTitle( p_demux, i_title );
322
323     return VLC_SUCCESS;
324 }
325
326
327 /*****************************************************************************
328  * blurayControl: handle the controls
329  *****************************************************************************/
330 static int blurayControl(demux_t *p_demux, int query, va_list args)
331 {
332     demux_sys_t *p_sys = p_demux->p_sys;
333     bool     *pb_bool;
334     int64_t  *pi_64;
335
336     switch (query) {
337         case DEMUX_CAN_SEEK:
338         case DEMUX_CAN_PAUSE:
339         case DEMUX_CAN_CONTROL_PACE:
340              pb_bool = (bool*)va_arg( args, bool * );
341              *pb_bool = true;
342              break;
343
344         case DEMUX_GET_PTS_DELAY:
345             pi_64 = (int64_t*)va_arg( args, int64_t * );
346             *pi_64 =
347                 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
348             break;
349
350         case DEMUX_SET_PAUSE_STATE:
351             /* Nothing to do */
352             break;
353
354         case DEMUX_SET_TITLE:
355         {
356             int i_title = (int)va_arg( args, int );
357             if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS)
358                 return VLC_EGENERIC;
359             break;
360         }
361         case DEMUX_SET_SEEKPOINT:
362         {
363             int i_chapter = (int)va_arg( args, int );
364             bd_seek_chapter( p_sys->bluray, i_chapter );
365             p_demux->info.i_update = INPUT_UPDATE_SEEKPOINT;
366             break;
367         }
368
369         case DEMUX_GET_TITLE_INFO:
370         {
371             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
372             int *pi_int             = (int*)va_arg( args, int* );
373             int *pi_title_offset    = (int*)va_arg( args, int* );
374             int *pi_chapter_offset  = (int*)va_arg( args, int* );
375
376             /* */
377             *pi_title_offset   = 0;
378             *pi_chapter_offset = 0;
379
380             /* Duplicate local title infos */
381             *pi_int = p_sys->i_title;
382             *ppp_title = calloc( p_sys->i_title, sizeof(input_title_t **) );
383             for( unsigned int i = 0; i < p_sys->i_title; i++ )
384                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->pp_title[i]);
385
386             return VLC_SUCCESS;
387         }
388
389         case DEMUX_GET_LENGTH:
390         {
391             int64_t *pi_length = (int64_t*)va_arg(args, int64_t *);
392             *pi_length = p_demux->info.i_title < p_sys->i_title ? CUR_LENGTH : 0;
393             return VLC_SUCCESS;
394         }
395         case DEMUX_SET_TIME:
396         {
397             int64_t i_time = (int64_t)va_arg(args, int64_t);
398             bd_seek_time(p_sys->bluray, TO_TICKS(i_time));
399             return VLC_SUCCESS;
400         }
401         case DEMUX_GET_TIME:
402         {
403             int64_t *pi_time = (int64_t*)va_arg(args, int64_t *);
404             *pi_time = (int64_t)FROM_TICKS(bd_tell_time(p_sys->bluray));
405             return VLC_SUCCESS;
406         }
407
408         case DEMUX_GET_POSITION:
409         {
410             double *pf_position = (double*)va_arg( args, double * );
411             *pf_position = p_demux->info.i_title < p_sys->i_title ?
412                         (double)FROM_TICKS(bd_tell_time(p_sys->bluray))/CUR_LENGTH : 0.0;
413             return VLC_SUCCESS;
414         }
415         case DEMUX_SET_POSITION:
416         {
417             double f_position = (double)va_arg(args, double);
418             bd_seek_time(p_sys->bluray, TO_TICKS(f_position*CUR_LENGTH));
419             return VLC_SUCCESS;
420         }
421
422         case DEMUX_GET_META:
423         {
424             const struct meta_dl *meta = bd_get_meta(p_sys->bluray);
425             if(!meta)
426                 return VLC_EGENERIC;
427
428             vlc_meta_t *p_meta = (vlc_meta_t *) va_arg (args, vlc_meta_t*);
429
430             if (!EMPTY_STR(meta->di_name)) vlc_meta_SetTitle(p_meta, meta->di_name);
431
432             if (!EMPTY_STR(meta->language_code)) vlc_meta_AddExtra(p_meta, "Language", meta->language_code);
433             if (!EMPTY_STR(meta->filename)) vlc_meta_AddExtra(p_meta, "Filename", meta->filename);
434             if (!EMPTY_STR(meta->di_alternative)) vlc_meta_AddExtra(p_meta, "Alternative", meta->di_alternative);
435
436             // if (meta->di_set_number > 0) vlc_meta_SetTrackNum(p_meta, meta->di_set_number);
437             // if (meta->di_num_sets > 0) vlc_meta_AddExtra(p_meta, "Discs numbers in Set", meta->di_num_sets);
438
439             if (meta->thumb_count > 0 && meta->thumbnails) {
440                 vlc_meta_SetArtURL(p_meta, meta->thumbnails[0].path);
441             }
442
443             return VLC_SUCCESS;
444         }
445
446         case DEMUX_CAN_RECORD:
447         case DEMUX_GET_FPS:
448         case DEMUX_SET_GROUP:
449         case DEMUX_HAS_UNSUPPORTED_META:
450         case DEMUX_GET_ATTACHMENTS:
451             return VLC_EGENERIC;
452         default:
453             msg_Warn( p_demux, "unimplemented query (%d) in control", query );
454             return VLC_EGENERIC;
455     }
456     return VLC_SUCCESS;
457 }
458
459 static void blurayHandleEvent( demux_t *p_demux, const BD_EVENT *e )
460 {
461     demux_sys_t *p_sys = p_demux->p_sys;
462
463     switch (e->event)
464     {
465         case BD_EVENT_TITLE:
466             if (e->param < p_sys->i_title)
467                 blurayUpdateTitle( p_demux, e->param );
468             break;
469         case BD_EVENT_PLAYITEM:
470             break;
471         case BD_EVENT_AUDIO_STREAM:
472             break;
473         case BD_EVENT_CHAPTER:
474             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
475             p_demux->info.i_seekpoint = 0;
476             break;
477         case BD_EVENT_ANGLE:
478         case BD_EVENT_IG_STREAM:
479         default:
480             msg_Warn( p_demux, "event: %d param: %d", e->event, e->param );
481             break;
482     }
483 }
484
485 static void blurayHandleEvents( demux_t *p_demux )
486 {
487     BD_EVENT e;
488
489     while (bd_get_event(p_demux->p_sys->bluray, &e))
490     {
491         blurayHandleEvent(p_demux, &e);
492     }
493 }
494
495 #define BD_TS_PACKET_SIZE (192)
496 #define NB_TS_PACKETS (200)
497
498 static int blurayDemux(demux_t *p_demux)
499 {
500     demux_sys_t *p_sys = p_demux->p_sys;
501
502     block_t *p_block = block_New(p_demux, NB_TS_PACKETS * (int64_t)BD_TS_PACKET_SIZE);
503     if (!p_block) {
504         return -1;
505     }
506
507     blurayHandleEvents(p_demux);
508     int nread = bd_read(p_sys->bluray, p_block->p_buffer,
509                         NB_TS_PACKETS * BD_TS_PACKET_SIZE);
510     if (nread < 0) {
511         block_Release(p_block);
512         return nread;
513     }
514
515     p_block->i_buffer = nread;
516
517     stream_DemuxSend( p_sys->p_parser, p_block );
518
519     return 1;
520 }