1 /*****************************************************************************
2 * access.c: access capabilities for dvdplay plugin.
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: access.c,v 1.2 2002/08/07 00:29:36 sam Exp $
7 * Author: Stéphane Borel <stef@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc/input.h>
33 #include "../../demux/mpeg/system.h"
40 #include <sys/types.h>
44 #ifdef STRNCASECMP_IN_STRINGS_H
49 # include <io.h> /* read() */
51 # include <sys/uio.h> /* struct iovec */
55 # include "input_iovec.h"
63 /*****************************************************************************
65 *****************************************************************************/
66 /* called from outside */
67 static int dvdplay_SetArea ( input_thread_t *, input_area_t * );
68 static int dvdplay_SetProgram ( input_thread_t *, pgrm_descriptor_t * );
69 static int dvdplay_Read ( input_thread_t *, byte_t *, size_t );
70 static void dvdplay_Seek ( input_thread_t *, off_t );
72 static void pf_vmg_callback ( void*, dvdplay_event_t );
74 /* only from inside */
75 static int dvdNewArea( input_thread_t *, input_area_t * );
76 static int dvdNewPGC ( input_thread_t * );
78 /*****************************************************************************
79 * OpenDVD: open libdvdplay
80 *****************************************************************************/
81 int E_(OpenDVD) ( vlc_object_t *p_this )
83 input_thread_t * p_input = (input_thread_t *)p_this;
86 input_area_t * p_area;
93 p_dvd = malloc( sizeof(dvd_data_t) );
96 msg_Err( p_input, "dvdplay error: out of memory" );
100 p_input->p_access_data = (void *)p_dvd;
102 p_input->pf_read = dvdplay_Read;
103 p_input->pf_seek = dvdplay_Seek;
104 p_input->pf_set_area = dvdplay_SetArea;
105 p_input->pf_set_program = dvdplay_SetProgram;
108 if( ( psz_source = dvdplay_ParseCL( p_input,
109 &i_title, &i_chapter, &i_angle ) ) == NULL )
115 /* Open libdvdplay */
116 p_dvd->vmg = dvdplay_open( psz_source, pf_vmg_callback, (void*)p_input );
118 /* free allocated strings */
121 if( p_dvd->vmg == NULL )
123 msg_Err( p_input, "dvdplay error: can't open source" );
128 p_dvd->p_intf = NULL;
130 p_dvd->i_still_time = 0;
135 /* Set stream and area data */
136 vlc_mutex_lock( &p_input->stream.stream_lock );
138 /* If we are here we can control the pace... */
139 p_input->stream.b_pace_control = 1;
140 /* seek is only allowed when we have size info */
141 p_input->stream.b_seekable = 0;
143 /* Initialize ES structures */
144 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
146 /* disc input method */
147 p_input->stream.i_method = INPUT_METHOD_DVD;
149 i_title_nr = dvdplay_title_nr( p_dvd->vmg );
150 #define area p_input->stream.pp_areas
152 /* Area 0 for menu */
153 area[0]->i_plugin_data = 0;
155 for( i = 1 ; i <= i_title_nr ; i++ )
157 input_AddArea( p_input );
162 /* Number of chapters */
163 area[i]->i_part_nb = dvdplay_chapter_nr( p_dvd->vmg, i );
165 area[i]->i_plugin_data = 0;
168 msg_Dbg( p_input, "number of titles: %d", i_title_nr );
170 i_title = i_title <= i_title_nr ? i_title : 0;
172 p_area = p_input->stream.pp_areas[i_title];
173 p_area->i_part = i_chapter;
174 p_input->stream.p_selected_area = NULL;
176 /* set title, chapter, audio and subpic */
177 if( dvdplay_SetArea( p_input, p_area ) )
179 vlc_mutex_unlock( &p_input->stream.stream_lock );
183 if( i_angle <= p_input->stream.i_pgrm_number )
185 dvdplay_SetProgram( p_input,
186 p_input->stream.pp_programs[i_angle - 1] );
189 vlc_mutex_unlock( &p_input->stream.stream_lock );
191 p_input->psz_demux = "dvdplay";
196 /*****************************************************************************
197 * CloseDVD: close libdvdplay
198 *****************************************************************************/
199 void E_(CloseDVD) ( vlc_object_t *p_this )
201 input_thread_t * p_input = (input_thread_t *)p_this;
202 dvd_data_t * p_dvd = (dvd_data_t *)p_input->p_access_data;
204 /* close libdvdplay */
205 dvdplay_close( p_dvd->vmg );
208 p_input->p_access_data = NULL;
212 /*****************************************************************************
213 * dvdplay_SetProgram: set dvd angle.
214 *****************************************************************************
215 * This is actually a hack to make angle change through vlc interface with
216 * no need for a specific button.
217 *****************************************************************************/
218 static int dvdplay_SetProgram( input_thread_t * p_input,
219 pgrm_descriptor_t * p_program )
221 if( p_input->stream.p_selected_program != p_program )
226 p_dvd = (dvd_data_t*)(p_input->p_access_data);
227 i_angle = p_program->i_number;
229 if( !dvdplay_angle( p_dvd->vmg, i_angle ) )
231 memcpy( p_program, p_input->stream.p_selected_program,
232 sizeof(pgrm_descriptor_t) );
233 p_program->i_number = i_angle;
234 p_input->stream.p_selected_program = p_program;
236 msg_Dbg( p_input, "angle %d selected", i_angle );
243 /*****************************************************************************
244 * dvdplay_SetArea: initialize input data for title x, chapter y.
245 * It should be called for each user navigation request.
246 *****************************************************************************
247 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
248 * Note that you have to take the lock before entering here.
249 *****************************************************************************/
250 static int dvdplay_SetArea( input_thread_t * p_input, input_area_t * p_area )
254 p_dvd = (dvd_data_t*)p_input->p_access_data;
259 if( p_area != p_input->stream.p_selected_area )
263 /* prevent intf to try to seek */
264 p_input->stream.b_seekable = 0;
266 /* Store selected chapter */
267 i_chapter = p_area->i_part;
269 dvdNewArea( p_input, p_area );
271 dvdplay_start( p_dvd->vmg, p_area->i_id );
273 p_area->i_part = i_chapter;
277 p_area = p_input->stream.p_selected_area;
284 if( p_area->i_part != dvdplay_chapter_cur( p_dvd->vmg ) )
286 if( ( p_area->i_part > 0 ) &&
287 ( p_area->i_part <= p_area->i_part_nb ))
289 dvdplay_pg( p_dvd->vmg, p_area->i_part );
291 p_area->i_part = dvdplay_chapter_cur( p_dvd->vmg );
294 /* warn interface that something has changed */
296 LB2OFF( dvdplay_position( p_dvd->vmg ) ) - p_area->i_start;
297 p_input->stream.b_changed = 1;
302 /*****************************************************************************
303 * dvdplay_Read: reads data packets.
304 *****************************************************************************
305 * Returns -1 in case of error, the number of bytes read if everything went
307 *****************************************************************************/
308 static int dvdplay_Read( input_thread_t * p_input,
309 byte_t * p_buffer, size_t i_count )
314 p_dvd = (dvd_data_t *)p_input->p_access_data;
316 vlc_mutex_lock( &p_input->stream.stream_lock );
318 i_read = LB2OFF( dvdplay_read( p_dvd->vmg, p_buffer, OFF2LB( i_count ) ) );
320 p_input->stream.p_selected_area->i_tell =
321 LB2OFF( dvdplay_position( p_dvd->vmg ) ) -
322 p_input->stream.p_selected_area->i_start;
324 vlc_mutex_unlock( &p_input->stream.stream_lock );
329 /*****************************************************************************
330 * dvdplay_Seek : Goes to a given position on the stream.
331 *****************************************************************************
332 * This one is used by the input and translate chronological position from
333 * input to logical position on the device.
334 * The lock should be taken before calling this function.
335 *****************************************************************************/
336 static void dvdplay_Seek( input_thread_t * p_input, off_t i_off )
340 p_dvd = (dvd_data_t *)p_input->p_access_data;
342 vlc_mutex_lock( &p_input->stream.stream_lock );
344 dvdplay_seek( p_dvd->vmg, OFF2LB( i_off ) );
346 vlc_mutex_unlock( &p_input->stream.stream_lock );
352 /*****************************************************************************
353 * pf_vmg_callback: called by libdvdplay when some event happens
354 *****************************************************************************
355 * The stream lock has to be taken before entering here
356 *****************************************************************************/
357 static void pf_vmg_callback( void* p_args, dvdplay_event_t event )
359 input_thread_t * p_input;
363 p_input = (input_thread_t*)p_args;
364 p_dvd = (dvd_data_t*)p_input->p_access_data;
376 /* prevent intf to try to seek by default */
377 p_input->stream.b_seekable = 0;
379 if( ( i = dvdplay_title_cur( p_dvd->vmg ) ) !=
380 p_input->stream.p_selected_area->i_id )
382 /* the title number has changed: update area */
383 msg_Warn( p_input, "new title %d (%d)", i,
384 p_input->stream.p_selected_area->i_id );
386 p_input->stream.pp_areas[i] );
389 /* new pgc in same title: reinit ES */
390 dvdNewPGC( p_input );
392 p_input->stream.b_changed = 1;
396 /* update current chapter */
397 p_input->stream.p_selected_area->i_part =
398 dvdplay_chapter_cur( p_dvd->vmg );
401 p_dvd->b_end_of_cell = 0;
404 p_dvd->b_end_of_cell = 1;
407 dvdplay_ES( p_input );
410 /* we must pause only from demux
411 * when the data in cache has been decoded */
412 p_dvd->i_still_time = dvdplay_still_time( p_dvd->vmg );
413 msg_Dbg( p_input, "still time %d", p_dvd->i_still_time );
421 msg_Err( p_input, "unknown event from libdvdplay (%d)",
428 static int dvdNewArea( input_thread_t * p_input, input_area_t * p_area )
431 int i_angle_nb, i_angle;
434 p_dvd = (dvd_data_t*)p_input->p_access_data;
436 p_input->stream.p_selected_area = p_area;
439 * One program for each angle
441 while( p_input->stream.i_pgrm_number )
443 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
446 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
447 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
449 dvdplay_angle_info( p_dvd->vmg, &i_angle_nb, &i_angle );
450 for( i = 1 ; i < i_angle_nb ; i++ )
452 input_AddProgram( p_input, i+1, 0 );
455 dvdplay_SetProgram( p_input,
456 p_input->stream.pp_programs[i_angle-1] );
458 // dvdNewPGC( p_input );
460 /* No PSM to read in DVD mode, we already have all information */
461 p_input->stream.p_selected_program->b_is_ok = 1;
466 static int dvdNewPGC( input_thread_t * p_input )
469 // int i_audio_nr = -1;
471 // int i_subp_nr = -1;
475 p_dvd = (dvd_data_t*)p_input->p_access_data;
477 // dvdplay_audio_info( p_dvd->vmg, &i_audio_nr, &i_audio );
478 // dvdplay_subp_info( p_dvd->vmg, &i_subp_nr, &i_subp );
480 dvdplay_ES( p_input );
481 p_input->stream.p_selected_area->i_start =
482 LB2OFF( dvdplay_title_first( p_dvd->vmg ) );
483 p_input->stream.p_selected_area->i_size =
484 LB2OFF( dvdplay_title_end ( p_dvd->vmg ) ) -
485 p_input->stream.p_selected_area->i_start;
487 if( p_input->stream.p_selected_area->i_size > 0 )
489 p_input->stream.b_seekable = 1;
493 p_input->stream.b_seekable = 0;
497 i_sec = dvdplay_title_time( p_dvd->vmg );
498 msg_Dbg( p_input, "title time: %d:%02d:%02d (%d)",
499 i_sec/3600, (i_sec%3600)/60, i_sec%60, i_sec );