1 /*****************************************************************************
2 * bluray.c: Blu-ray disc support plugin
3 *****************************************************************************
4 * Copyright (C) 2010 VideoLAN, VLC authors and libbluray AUTHORS
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
25 #ifdef HAVE_SYS_STAT_H
26 # include <sys/stat.h>
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
35 #include <vlc_messages.h>
36 #include <vlc_input.h>
38 #include <libbluray/bluray.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 #define CACHING_TEXT N_("Caching value in ms")
44 #define CACHING_LONGTEXT N_( "Caching value for BDs. This "\
45 "value should be set in milliseconds." )
48 static int blurayOpen ( vlc_object_t * );
49 static void blurayClose( vlc_object_t * );
52 set_shortname( N_("BluRay") )
53 set_description( N_("Blu-Ray Disc support (libbluray)") )
55 set_category( CAT_INPUT )
56 set_subcategory( SUBCAT_INPUT_ACCESS )
57 set_capability( "access", 60 )
59 add_integer( "bluray-caching", 1000,
60 CACHING_TEXT, CACHING_LONGTEXT, true )
62 add_shortcut( "bluray" )
63 add_shortcut( "file" )
65 set_callbacks( blurayOpen, blurayClose )
68 /*****************************************************************************
70 *****************************************************************************/
78 unsigned int i_longest_title;
79 input_title_t **pp_title;
84 static ssize_t blurayRead (access_t *, uint8_t *, size_t);
85 static int bluraySeek (access_t *, uint64_t);
86 static int blurayControl(access_t *, int, va_list);
87 static int blurayInitTitles(access_t *p_access );
88 static int bluraySetTitle(access_t *p_access, int i_title);
90 /*****************************************************************************
91 * blurayOpen: module init function
92 *****************************************************************************/
93 static int blurayOpen( vlc_object_t *object )
95 access_t *p_access = (access_t*)object;
100 char bd_path[PATH_MAX];
102 if( strcmp( p_access->psz_access, "bluray" ) ) {
103 // TODO BDMV support, once we figure out what to do in libbluray
107 /* init access fields */
108 access_InitFields(p_access);
110 /* register callback function for communication */
111 ACCESS_SET_CALLBACKS(blurayRead, NULL, blurayControl, bluraySeek);
113 p_access->p_sys = p_sys = malloc(sizeof(access_sys_t));
114 if (unlikely(!p_sys)) {
118 TAB_INIT( p_sys->i_title, p_sys->pp_title );
120 /* store current bd_path */
121 strncpy(bd_path, p_access->psz_location, sizeof(bd_path));
122 bd_path[PATH_MAX - 1] = '\0';
124 p_sys->bluray = bd_open(bd_path, NULL);
125 if ( !p_sys->bluray ) {
130 if (blurayInitTitles(p_access) != VLC_SUCCESS) {
135 /* get title request */
136 if ( (pos_title = strrchr(bd_path, ':')) ) {
137 /* found character ':' for title information */
138 *(pos_title++) = '\0';
139 i_title = atoi(pos_title);
142 /* set start title number */
143 if ( bluraySetTitle(p_access, i_title) != VLC_SUCCESS ) {
144 msg_Err( p_access, "Could not set the title %d", i_title );
149 p_sys->i_bd_delay = var_InheritInteger(p_access, "bluray-caching");
155 /*****************************************************************************
156 * blurayClose: module destroy function
157 *****************************************************************************/
158 static void blurayClose( vlc_object_t *object )
160 access_t *p_access = (access_t*)object;
161 access_sys_t *p_sys = p_access->p_sys;
164 for (unsigned int i = 0; i < p_sys->i_title; i++)
165 vlc_input_title_Delete(p_sys->pp_title[i]);
166 TAB_CLEAN( p_sys->i_title, p_sys->pp_title );
168 /* bd_close( NULL ) can crash */
169 assert(p_sys->bluray);
170 bd_close(p_sys->bluray);
174 static int blurayInitTitles(access_t *p_access )
176 access_sys_t *p_sys = p_access->p_sys;
178 /* get and set the titles */
179 unsigned i_title = bd_get_titles(p_sys->bluray, TITLES_RELEVANT);
180 int64_t duration = 0;
182 for (unsigned int i = 0; i < i_title; i++) {
183 input_title_t *t = vlc_input_title_New();
187 BLURAY_TITLE_INFO *title_info = bd_get_title_info(p_sys->bluray,i);
190 t->i_length = title_info->duration * CLOCK_FREQ / INT64_C(90000);
192 if (t->i_length > duration) {
193 duration = t->i_length;
194 p_sys->i_longest_title = i;
197 for ( unsigned int j = 0; j < title_info->chapter_count; j++) {
198 seekpoint_t *s = vlc_seekpoint_New();
201 s->i_time_offset = title_info->chapters[j].offset;
203 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
205 TAB_APPEND( p_sys->i_title, p_sys->pp_title, t );
206 bd_free_title_info(title_info);
211 /*****************************************************************************
212 * bluraySetTitle: select new BD title
213 *****************************************************************************/
214 static int bluraySetTitle(access_t *p_access, int i_title)
216 access_sys_t *p_sys = p_access->p_sys;
218 /* Looking for the main title, ie the longest duration */
220 i_title = p_sys->i_longest_title;
222 msg_Dbg( p_access, "Selecting Title %i", i_title);
224 /* Select Blu-Ray title */
225 if ( bd_select_title(p_access->p_sys->bluray, i_title) == 0 ) {
226 msg_Err( p_access, "cannot select bd title '%d'", p_access->info.i_title);
230 /* read title length and init some values */
231 p_access->info.i_title = i_title;
232 p_access->info.i_size = bd_get_title_size(p_sys->bluray);
233 p_access->info.i_pos = 0;
234 p_access->info.b_eof = false;
235 p_access->info.i_seekpoint = 0;
236 p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
242 /*****************************************************************************
243 * blurayControl: handle the controls
244 *****************************************************************************/
245 static int blurayControl(access_t *p_access, int query, va_list args)
247 access_sys_t *p_sys = p_access->p_sys;
252 case ACCESS_CAN_SEEK:
253 case ACCESS_CAN_FASTSEEK:
254 case ACCESS_CAN_PAUSE:
255 case ACCESS_CAN_CONTROL_PACE:
256 pb_bool = (bool*)va_arg( args, bool * );
260 case ACCESS_GET_PTS_DELAY:
261 pi_64 = (int64_t*)va_arg( args, int64_t * );
262 *pi_64 = p_sys->i_bd_delay;
265 case ACCESS_SET_PAUSE_STATE:
269 case ACCESS_SET_TITLE:
271 int i_title = (int)va_arg( args, int );
272 if( bluraySetTitle( p_access, i_title ) != VLC_SUCCESS )
276 case ACCESS_SET_SEEKPOINT:
278 int i_chapter = (int)va_arg( args, int );
279 bd_seek_chapter( p_sys->bluray, i_chapter );
280 p_access->info.i_update = INPUT_UPDATE_SEEKPOINT;
284 case ACCESS_GET_TITLE_INFO:
286 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
287 int *pi_int = (int*)va_arg( args, int* );
288 int *pi_title_offset = (int*)va_arg( args, int* );
289 int *pi_chapter_offset = (int*)va_arg( args, int* );
292 *pi_title_offset = 0;
293 *pi_chapter_offset = 0;
295 /* Duplicate local title infos */
296 *pi_int = p_sys->i_title;
297 *ppp_title = calloc( p_sys->i_title, sizeof(input_title_t **) );
298 for( unsigned int i = 0; i < p_sys->i_title; i++ )
299 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->pp_title[i]);
303 case ACCESS_SET_PRIVATE_ID_STATE:
304 case ACCESS_GET_CONTENT_TYPE:
305 case ACCESS_GET_META:
309 msg_Warn( p_access, "unimplemented query (%d) in control", query );
317 /*****************************************************************************
318 * bluraySeek: seek to the given position
319 *****************************************************************************/
320 static int bluraySeek(access_t *p_access, uint64_t position)
322 access_sys_t *p_sys = p_access->p_sys;
324 p_access->info.i_pos = bd_seek(p_sys->bluray, position);
325 p_access->info.b_eof = false;
331 /*****************************************************************************
332 * blurayRead: read BD data into buffer
333 *****************************************************************************/
334 static ssize_t blurayRead(access_t *p_access, uint8_t *data, size_t size)
336 access_sys_t *p_sys = p_access->p_sys;
339 if (p_access->info.b_eof) {
343 /* read data into buffer with given length */
344 nread = bd_read(p_sys->bluray, data, size);
347 p_access->info.b_eof = true;
349 else if( nread > 0 ) {
350 p_access->info.i_pos += nread;