]> git.sesse.net Git - vlc/blob - modules/access/bluray.c
Update LGPL license blurb, choosing v2.1+.
[vlc] / modules / access / bluray.c
1 /*****************************************************************************
2  * bluray.c: Blu-ray disc support plugin
3  *****************************************************************************
4  * Copyright (C) 2010 VideoLAN, VLC authors and libbluray AUTHORS
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #ifdef HAVE_SYS_STAT_H
26 #   include <sys/stat.h>
27 #endif
28
29 #include <assert.h>
30 #include <limits.h>
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
35 #include <vlc_messages.h>
36
37 #include <libbluray/bluray.h>
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 #define CACHING_TEXT N_("Caching value in ms")
43 #define CACHING_LONGTEXT N_( "Caching value for BDs. This "\
44                              "value should be set in milliseconds." )
45
46 /* Callbacks */
47 static int  blurayOpen ( vlc_object_t * );
48 static void blurayClose( vlc_object_t * );
49
50 vlc_module_begin ()
51     set_shortname( N_("BluRay") )
52     set_description( N_("Blu-Ray Disc support (libbluray)") )
53
54     set_category( CAT_INPUT )
55     set_subcategory( SUBCAT_INPUT_ACCESS )
56     set_capability( "access", 60 )
57
58     add_integer( "bluray-caching", 1000, NULL,
59         CACHING_TEXT, CACHING_LONGTEXT, true )
60
61     add_shortcut( "bluray" )
62     add_shortcut( "file" )
63
64     set_callbacks( blurayOpen, blurayClose )
65 vlc_module_end ()
66
67 /*****************************************************************************
68  * Local prototypes
69  *****************************************************************************/
70
71 struct access_sys_t
72 {
73     BLURAY *bluray; /* */
74     int i_bd_delay;
75 };
76
77 static ssize_t blurayRead   (access_t *, uint8_t *, size_t);
78 static int     bluraySeek   (access_t *, uint64_t);
79 static int     blurayControl(access_t *, int, va_list);
80 static int     bluraySetTitle(access_t *p_access, int i_tile);
81
82 /*****************************************************************************
83  * blurayOpen: module init function
84  *****************************************************************************/
85 static int blurayOpen( vlc_object_t *object )
86 {
87     access_t *p_access = (access_t*)object;
88
89     access_sys_t *p_sys;
90     char *pos_title;
91     int i_title = 0;
92     char bd_path[PATH_MAX];
93
94     if( !p_access->psz_location || !*p_access->psz_location ) {
95         return VLC_EGENERIC;
96     }
97
98     /* init access fields */
99     access_InitFields(p_access);
100
101     /* register callback function for communication */
102     ACCESS_SET_CALLBACKS(blurayRead, NULL, blurayControl, bluraySeek);
103
104     p_access->p_sys = p_sys = malloc(sizeof(access_sys_t));
105     if (unlikely(!p_sys)) {
106         return VLC_ENOMEM;
107     }
108
109     /* store current bd_path */
110     strncpy(bd_path, p_access->psz_location, sizeof(bd_path));
111     bd_path[PATH_MAX - 1] = '\0';
112
113     if ( (pos_title = strrchr(bd_path, ':')) ) {
114         /* found character ':' for title information */
115         *(pos_title++) = '\0';
116         i_title = atoi(pos_title);
117     }
118
119     p_sys->bluray = bd_open(bd_path, NULL);
120     if ( !p_sys->bluray ) {
121         free(p_sys);
122         return VLC_EGENERIC;
123     }
124
125     /* set start title number */
126     if ( bluraySetTitle(p_access, i_title) != VLC_SUCCESS ) {
127         blurayClose(object);
128         return VLC_EGENERIC;
129     }
130
131     p_sys->i_bd_delay = var_InheritInteger(p_access, "bluray-caching");
132
133     return VLC_SUCCESS;
134 }
135
136
137 /*****************************************************************************
138  * blurayClose: module destroy function
139  *****************************************************************************/
140 static void blurayClose( vlc_object_t *object )
141 {
142     access_t *p_access = (access_t*)object;
143     access_sys_t *p_sys = p_access->p_sys;
144
145     /* bd_close( NULL ) can crash */
146     assert(p_sys->bluray);
147     bd_close(p_sys->bluray);
148     free(p_sys);
149 }
150
151
152 /*****************************************************************************
153  * bluraySetTitle: select new BD title
154  *****************************************************************************/
155 static int bluraySetTitle(access_t *p_access, int i_title)
156 {
157     access_sys_t *p_sys = p_access->p_sys;
158
159     /* Select Blu-Ray title */
160     if ( bd_select_title(p_access->p_sys->bluray, i_title) == 0 ) {
161         msg_Err( p_access, "cannot select bd title '%d'", p_access->info.i_title);
162         return VLC_EGENERIC;
163     }
164
165     /* read title length and init some values */
166     p_access->info.i_title = i_title;
167     p_access->info.i_size  = bd_get_title_size(p_sys->bluray);
168     p_access->info.i_pos   = 0;
169     p_access->info.b_eof   = false;
170     p_access->info.i_seekpoint = 0;
171
172     return VLC_SUCCESS;
173 }
174
175
176 /*****************************************************************************
177  * blurayControl: handle the controls
178  *****************************************************************************/
179 static int blurayControl(access_t *p_access, int query, va_list args)
180 {
181     access_sys_t *p_sys = p_access->p_sys;
182     bool     *pb_bool;
183     int64_t  *pi_64;
184
185     switch (query) {
186         case ACCESS_CAN_SEEK:
187         case ACCESS_CAN_FASTSEEK:
188         case ACCESS_CAN_PAUSE:
189         case ACCESS_CAN_CONTROL_PACE:
190              pb_bool = (bool*)va_arg( args, bool * );
191              *pb_bool = true;
192              break;
193
194         case ACCESS_GET_PTS_DELAY:
195             pi_64 = (int64_t*)va_arg( args, int64_t * );
196             *pi_64 = p_sys->i_bd_delay;
197             break;
198
199         case ACCESS_SET_PAUSE_STATE:
200             /* Nothing to do */
201             break;
202
203         case ACCESS_SET_TITLE:
204         {
205             int i_title = (int)va_arg( args, int );
206             if( bluraySetTitle( p_access, i_title ) != VLC_SUCCESS )
207                 return VLC_EGENERIC;
208             break;
209         }
210         case ACCESS_SET_SEEKPOINT:
211         {
212             int i_chapter = (int)va_arg( args, int );
213             bd_seek_chapter( p_sys->bluray, i_chapter );
214             break;
215         }
216         case ACCESS_GET_META:
217         case ACCESS_GET_TITLE_INFO:
218         case ACCESS_SET_PRIVATE_ID_STATE:
219         case ACCESS_GET_CONTENT_TYPE:
220             return VLC_EGENERIC;
221
222         default:
223             msg_Warn( p_access, "unimplemented query (%d) in control", query );
224             return VLC_EGENERIC;
225     }
226
227     return VLC_SUCCESS;
228 }
229
230
231 /*****************************************************************************
232  * bluraySeek: seek to the given position
233  *****************************************************************************/
234 static int bluraySeek(access_t *p_access, uint64_t position)
235 {
236     access_sys_t *p_sys = p_access->p_sys;
237
238     p_access->info.i_pos = bd_seek(p_sys->bluray, position);
239     p_access->info.b_eof = false;
240
241     return VLC_SUCCESS;
242 }
243
244
245 /*****************************************************************************
246  * blurayRead: read BD data into buffer
247  *****************************************************************************/
248 static ssize_t blurayRead(access_t *p_access, uint8_t *data, size_t size)
249 {
250     access_sys_t *p_sys = p_access->p_sys;
251     int nread;
252
253     if (p_access->info.b_eof) {
254         return 0;
255     }
256
257     /* read data into buffer with given length */
258     nread = bd_read(p_sys->bluray, data, size);
259
260     if( nread == 0 ) {
261         p_access->info.b_eof = true;
262     }
263     else if( nread > 0 ) {
264         p_access->info.i_pos += nread;
265     }
266
267     return nread;
268 }
269