1 /*****************************************************************************
2 * access.c: access capabilities for dvdplay plugin.
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: access.c,v 1.2 2002/07/25 20:34:35 stef 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>
38 #include <sys/types.h>
42 #ifdef STRNCASECMP_IN_STRINGS_H
47 # include <io.h> /* read() */
49 # include <sys/uio.h> /* struct iovec */
53 # include "input_iovec.h"
61 /*****************************************************************************
63 *****************************************************************************/
64 /* called from outside */
65 static int dvdplay_Open ( input_thread_t * );
66 static void dvdplay_Close ( input_thread_t * );
67 static int dvdplay_SetArea ( input_thread_t *,
69 static int dvdplay_SetProgram ( input_thread_t *,
70 pgrm_descriptor_t * );
71 static int dvdplay_Read ( input_thread_t *,
73 static void dvdplay_Seek ( input_thread_t *, off_t );
75 static void pf_vmg_callback ( void*, dvdplay_event_t );
77 /* only from inside */
79 static int dvdNewArea( input_thread_t *, input_area_t * );
80 static int dvdNewPGC ( input_thread_t * );
82 /*****************************************************************************
83 * Functions exported as capabilities. They are declared as static so that
84 * we don't pollute the namespace too much.
85 *****************************************************************************/
86 void _M( access_getfunctions )( function_list_t * p_function_list )
88 #define access p_function_list->functions.access
89 access.pf_open = dvdplay_Open;
90 access.pf_close = dvdplay_Close;
91 access.pf_read = dvdplay_Read;
92 access.pf_set_area = dvdplay_SetArea;
93 access.pf_set_program = dvdplay_SetProgram;
94 access.pf_seek = dvdplay_Seek;
98 /*****************************************************************************
99 * dvdplay_Open: open libdvdplay
100 *****************************************************************************/
101 static int dvdplay_Open( input_thread_t *p_input )
105 input_area_t * p_area;
112 p_dvd = malloc( sizeof(dvd_data_t) );
115 msg_Err( p_input, "dvdplay error: out of memory" );
119 p_input->p_access_data = (void *)p_dvd;
122 if( ( psz_source = dvdplay_ParseCL( p_input,
123 &i_title, &i_chapter, &i_angle ) ) == NULL )
129 /* Open libdvdplay */
130 p_dvd->vmg = dvdplay_open( psz_source, pf_vmg_callback, (void*)p_input );
132 /* free allocated strings */
135 if( p_dvd->vmg == NULL )
137 msg_Err( p_input, "dvdplay error: can't open source" );
142 p_dvd->p_intf = NULL;
144 p_dvd->i_still_time = 0;
149 /* Set stream and area data */
150 vlc_mutex_lock( &p_input->stream.stream_lock );
152 /* If we are here we can control the pace... */
153 p_input->stream.b_pace_control = 1;
154 /* seek is only allowed when we have size info */
155 p_input->stream.b_seekable = 0;
157 /* Initialize ES structures */
158 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
160 /* disc input method */
161 p_input->stream.i_method = INPUT_METHOD_DVD;
163 i_title_nr = dvdplay_title_nr( p_dvd->vmg );
164 #define area p_input->stream.pp_areas
166 /* Area 0 for menu */
167 area[0]->i_plugin_data = 0;
169 for( i = 1 ; i <= i_title_nr ; i++ )
171 input_AddArea( p_input );
176 /* Number of chapters */
177 area[i]->i_part_nb = dvdplay_chapter_nr( p_dvd->vmg, i );
179 area[i]->i_plugin_data = 0;
182 msg_Dbg( p_input, "number of titles: %d", i_title_nr );
184 i_title = i_title <= i_title_nr ? i_title : 0;
186 p_area = p_input->stream.pp_areas[i_title];
187 p_area->i_part = i_chapter;
188 p_input->stream.p_selected_area = NULL;
190 /* set title, chapter, audio and subpic */
191 if( dvdplay_SetArea( p_input, p_area ) )
193 vlc_mutex_unlock( &p_input->stream.stream_lock );
197 if( i_angle <= p_input->stream.i_pgrm_number )
199 dvdplay_SetProgram( p_input,
200 p_input->stream.pp_programs[i_angle - 1] );
203 vlc_mutex_unlock( &p_input->stream.stream_lock );
205 p_input->psz_demux = "dvdplay";
210 /*****************************************************************************
211 * dvdplay_Close: close libdvdplay
212 *****************************************************************************/
213 static void dvdplay_Close( input_thread_t *p_input )
217 p_dvd = (dvd_data_t *)p_input->p_access_data;
219 /* close libdvdplay */
220 dvdplay_close( p_dvd->vmg );
223 p_input->p_access_data = NULL;
227 /*****************************************************************************
228 * dvdplay_SetProgram: set dvd angle.
229 *****************************************************************************
230 * This is actually a hack to make angle change through vlc interface with
231 * no need for a specific button.
232 *****************************************************************************/
233 static int dvdplay_SetProgram( input_thread_t * p_input,
234 pgrm_descriptor_t * p_program )
236 if( p_input->stream.p_selected_program != p_program )
241 p_dvd = (dvd_data_t*)(p_input->p_access_data);
242 i_angle = p_program->i_number;
244 if( !dvdplay_angle( p_dvd->vmg, i_angle ) )
246 memcpy( p_program, p_input->stream.p_selected_program,
247 sizeof(pgrm_descriptor_t) );
248 p_program->i_number = i_angle;
249 p_input->stream.p_selected_program = p_program;
251 msg_Dbg( p_input, "angle %d selected", i_angle );
258 /*****************************************************************************
259 * dvdplay_SetArea: initialize input data for title x, chapter y.
260 * It should be called for each user navigation request.
261 *****************************************************************************
262 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
263 * Note that you have to take the lock before entering here.
264 *****************************************************************************/
265 static int dvdplay_SetArea( input_thread_t * p_input, input_area_t * p_area )
269 p_dvd = (dvd_data_t*)p_input->p_access_data;
274 if( p_area != p_input->stream.p_selected_area )
278 /* prevent intf to try to seek */
279 p_input->stream.b_seekable = 0;
281 /* Store selected chapter */
282 i_chapter = p_area->i_part;
284 dvdNewArea( p_input, p_area );
286 dvdplay_start( p_dvd->vmg, p_area->i_id );
288 p_area->i_part = i_chapter;
292 p_area = p_input->stream.p_selected_area;
299 if( p_area->i_part != dvdplay_chapter_cur( p_dvd->vmg ) )
301 if( ( p_area->i_part > 0 ) &&
302 ( p_area->i_part <= p_area->i_part_nb ))
304 dvdplay_pg( p_dvd->vmg, p_area->i_part );
306 p_area->i_part = dvdplay_chapter_cur( p_dvd->vmg );
309 /* warn interface that something has changed */
311 LB2OFF( dvdplay_position( p_dvd->vmg ) ) - p_area->i_start;
312 p_input->stream.b_changed = 1;
317 /*****************************************************************************
318 * dvdplay_Read: reads data packets.
319 *****************************************************************************
320 * Returns -1 in case of error, the number of bytes read if everything went
322 *****************************************************************************/
323 static int dvdplay_Read( input_thread_t * p_input,
324 byte_t * p_buffer, size_t i_count )
329 p_dvd = (dvd_data_t *)p_input->p_access_data;
331 vlc_mutex_lock( &p_input->stream.stream_lock );
333 i_read = LB2OFF( dvdplay_read( p_dvd->vmg, p_buffer, OFF2LB( i_count ) ) );
335 p_input->stream.p_selected_area->i_tell =
336 LB2OFF( dvdplay_position( p_dvd->vmg ) ) -
337 p_input->stream.p_selected_area->i_start;
339 vlc_mutex_unlock( &p_input->stream.stream_lock );
344 /*****************************************************************************
345 * dvdplay_Seek : Goes to a given position on the stream.
346 *****************************************************************************
347 * This one is used by the input and translate chronological position from
348 * input to logical position on the device.
349 * The lock should be taken before calling this function.
350 *****************************************************************************/
351 static void dvdplay_Seek( input_thread_t * p_input, off_t i_off )
355 p_dvd = (dvd_data_t *)p_input->p_access_data;
357 vlc_mutex_lock( &p_input->stream.stream_lock );
359 dvdplay_seek( p_dvd->vmg, OFF2LB( i_off ) );
361 vlc_mutex_unlock( &p_input->stream.stream_lock );
367 /*****************************************************************************
368 * pf_vmg_callback: called by libdvdplay when some event happens
369 *****************************************************************************
370 * The stream lock has to be taken before entering here
371 *****************************************************************************/
372 static void pf_vmg_callback( void* p_args, dvdplay_event_t event )
374 input_thread_t * p_input;
378 p_input = (input_thread_t*)p_args;
379 p_dvd = (dvd_data_t*)p_input->p_access_data;
391 /* prevent intf to try to seek by default */
392 p_input->stream.b_seekable = 0;
394 if( ( i = dvdplay_title_cur( p_dvd->vmg ) ) !=
395 p_input->stream.p_selected_area->i_id )
397 /* the title number has changed: update area */
398 msg_Warn( p_input, "new title %d (%d)", i,
399 p_input->stream.p_selected_area->i_id );
401 p_input->stream.pp_areas[i] );
404 /* new pgc in same title: reinit ES */
405 dvdNewPGC( p_input );
407 p_input->stream.b_changed = 1;
411 /* update current chapter */
412 p_input->stream.p_selected_area->i_part =
413 dvdplay_chapter_cur( p_dvd->vmg );
416 p_dvd->b_end_of_cell = 0;
419 p_dvd->b_end_of_cell = 1;
422 dvdplay_ES( p_input );
425 /* we must pause only from demux
426 * when the data in cache has been decoded */
427 p_dvd->i_still_time = dvdplay_still_time( p_dvd->vmg );
428 msg_Dbg( p_input, "still time %d", p_dvd->i_still_time );
436 msg_Err( p_input, "unknown event from libdvdplay (%d)",
443 static int dvdNewArea( input_thread_t * p_input, input_area_t * p_area )
446 int i_angle_nb, i_angle;
449 p_dvd = (dvd_data_t*)p_input->p_access_data;
451 p_input->stream.p_selected_area = p_area;
454 * One program for each angle
456 while( p_input->stream.i_pgrm_number )
458 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
461 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
462 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
464 dvdplay_angle_info( p_dvd->vmg, &i_angle_nb, &i_angle );
465 for( i = 1 ; i < i_angle_nb ; i++ )
467 input_AddProgram( p_input, i+1, 0 );
470 dvdplay_SetProgram( p_input,
471 p_input->stream.pp_programs[i_angle-1] );
473 // dvdNewPGC( p_input );
475 /* No PSM to read in DVD mode, we already have all information */
476 p_input->stream.p_selected_program->b_is_ok = 1;
481 static int dvdNewPGC( input_thread_t * p_input )
484 // int i_audio_nr = -1;
486 // int i_subp_nr = -1;
490 p_dvd = (dvd_data_t*)p_input->p_access_data;
492 // dvdplay_audio_info( p_dvd->vmg, &i_audio_nr, &i_audio );
493 // dvdplay_subp_info( p_dvd->vmg, &i_subp_nr, &i_subp );
495 dvdplay_ES( p_input );
496 p_input->stream.p_selected_area->i_start =
497 LB2OFF( dvdplay_title_first( p_dvd->vmg ) );
498 p_input->stream.p_selected_area->i_size =
499 LB2OFF( dvdplay_title_end ( p_dvd->vmg ) ) -
500 p_input->stream.p_selected_area->i_start;
502 if( p_input->stream.p_selected_area->i_size > 0 )
504 p_input->stream.b_seekable = 1;
508 p_input->stream.b_seekable = 0;
512 i_sec = dvdplay_title_time( p_dvd->vmg );
513 msg_Dbg( p_input, "title time: %d:%02d:%02d (%d)",
514 i_sec/3600, (i_sec%3600)/60, i_sec%60, i_sec );