1 /*****************************************************************************
2 * access.c: access capabilities for dvdplay plugin.
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: access.c,v 1.13 2003/03/11 23:56:53 gbazin 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
53 /*****************************************************************************
55 *****************************************************************************/
56 /* called from outside */
57 static int dvdplay_SetArea ( input_thread_t *, input_area_t * );
58 static int dvdplay_SetProgram ( input_thread_t *, pgrm_descriptor_t * );
59 static int dvdplay_Read ( input_thread_t *, byte_t *, size_t );
60 static void dvdplay_Seek ( input_thread_t *, off_t );
62 static void pf_vmg_callback ( void*, dvdplay_event_t );
64 /* only from inside */
65 static int dvdNewArea( input_thread_t *, input_area_t * );
66 static int dvdNewPGC ( input_thread_t * );
68 /*****************************************************************************
69 * OpenDVD: open libdvdplay
70 *****************************************************************************/
71 int E_(OpenDVD) ( vlc_object_t *p_this )
73 input_thread_t * p_input = (input_thread_t *)p_this;
76 input_area_t * p_area;
77 unsigned int i_title_nr;
79 unsigned int i_chapter;
83 p_dvd = malloc( sizeof(dvd_data_t) );
86 msg_Err( p_input, "out of memory" );
90 p_input->p_access_data = (void *)p_dvd;
92 p_input->pf_read = dvdplay_Read;
93 p_input->pf_seek = dvdplay_Seek;
94 p_input->pf_set_area = dvdplay_SetArea;
95 p_input->pf_set_program = dvdplay_SetProgram;
98 if( ( psz_source = dvdplay_ParseCL( p_input,
99 &i_title, &i_chapter, &i_angle ) ) == NULL )
105 /* Open libdvdplay */
106 p_dvd->vmg = dvdplay_open( psz_source, pf_vmg_callback, (void*)p_input );
108 if( p_dvd->vmg == NULL )
110 msg_Err( p_input, "cannot open %s", psz_source );
116 /* free allocated strings */
119 p_dvd->p_intf = NULL;
121 p_dvd->i_still_time = 0;
126 /* Set stream and area data */
127 vlc_mutex_lock( &p_input->stream.stream_lock );
129 /* If we are here we can control the pace... */
130 p_input->stream.b_pace_control = 1;
131 /* seek is only allowed when we have size info */
132 p_input->stream.b_seekable = 0;
133 p_input->stream.b_connected = 1;
135 /* Initialize ES structures */
136 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
138 /* disc input method */
139 p_input->stream.i_method = INPUT_METHOD_DVD;
141 i_title_nr = dvdplay_title_nr( p_dvd->vmg );
142 #define area p_input->stream.pp_areas
144 /* Area 0 for menu */
145 area[0]->i_plugin_data = 0;
146 input_DelArea( p_input, p_input->stream.pp_areas[0] );
147 input_AddArea( p_input, 0, 1 );
149 for( i = 1 ; i <= i_title_nr ; i++ )
151 input_AddArea( p_input, i, dvdplay_chapter_nr( p_dvd->vmg, i ) );
152 area[i]->i_plugin_data = 0;
155 msg_Dbg( p_input, "number of titles: %d", i_title_nr );
157 i_title = i_title <= i_title_nr ? i_title : 0;
159 p_area = p_input->stream.pp_areas[i_title];
160 p_area->i_part = i_chapter;
161 p_input->stream.p_selected_area = NULL;
163 /* set title, chapter, audio and subpic */
164 if( dvdplay_SetArea( p_input, p_area ) )
166 vlc_mutex_unlock( &p_input->stream.stream_lock );
170 if( i_angle <= p_input->stream.i_pgrm_number )
172 dvdplay_SetProgram( p_input,
173 p_input->stream.pp_programs[i_angle - 1] );
176 vlc_mutex_unlock( &p_input->stream.stream_lock );
178 if( !p_input->psz_demux || !*p_input->psz_demux )
180 p_input->psz_demux = "dvdplay";
183 /* FIXME: we might lose variables here */
184 var_Create( p_input, "x-start", VLC_VAR_INTEGER );
185 var_Create( p_input, "y-start", VLC_VAR_INTEGER );
186 var_Create( p_input, "x-end", VLC_VAR_INTEGER );
187 var_Create( p_input, "y-end", VLC_VAR_INTEGER );
189 var_Create( p_input, "color", VLC_VAR_ADDRESS );
190 var_Create( p_input, "contrast", VLC_VAR_ADDRESS );
192 var_Create( p_input, "highlight", VLC_VAR_BOOL );
193 var_Create( p_input, "highlight-mutex", VLC_VAR_MUTEX );
198 /*****************************************************************************
199 * CloseDVD: close libdvdplay
200 *****************************************************************************/
201 void E_(CloseDVD) ( vlc_object_t *p_this )
203 input_thread_t * p_input = (input_thread_t *)p_this;
204 dvd_data_t * p_dvd = (dvd_data_t *)p_input->p_access_data;
206 var_Destroy( p_input, "highlight-mutex" );
207 var_Destroy( p_input, "highlight" );
209 var_Destroy( p_input, "x-start" );
210 var_Destroy( p_input, "x-end" );
211 var_Destroy( p_input, "y-start" );
212 var_Destroy( p_input, "y-end" );
214 var_Destroy( p_input, "color" );
215 var_Destroy( p_input, "contrast" );
217 /* close libdvdplay */
218 dvdplay_close( p_dvd->vmg );
221 p_input->p_access_data = NULL;
225 /*****************************************************************************
226 * dvdplay_SetProgram: set dvd angle.
227 *****************************************************************************
228 * This is actually a hack to make angle change through vlc interface with
229 * no need for a specific button.
230 *****************************************************************************/
231 static int dvdplay_SetProgram( input_thread_t * p_input,
232 pgrm_descriptor_t * p_program )
234 if( p_input->stream.p_selected_program != p_program )
240 p_dvd = (dvd_data_t*)(p_input->p_access_data);
241 i_angle = p_program->i_number;
243 if( !dvdplay_angle( p_dvd->vmg, i_angle ) )
245 memcpy( p_program, p_input->stream.p_selected_program,
246 sizeof(pgrm_descriptor_t) );
247 p_program->i_number = i_angle;
248 p_input->stream.p_selected_program = p_program;
250 msg_Dbg( p_input, "angle %d selected", i_angle );
253 /* Update the navigation variables without triggering a callback */
254 val.i_int = p_program->i_number;
255 var_Change( p_input, "program", VLC_VAR_SETVALUE, &val );
261 /*****************************************************************************
262 * dvdplay_SetArea: initialize input data for title x, chapter y.
263 * It should be called for each user navigation request.
264 *****************************************************************************
265 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
266 * Note that you have to take the lock before entering here.
267 *****************************************************************************/
268 static int dvdplay_SetArea( input_thread_t * p_input, input_area_t * p_area )
273 p_dvd = (dvd_data_t*)p_input->p_access_data;
278 if( p_area != p_input->stream.p_selected_area )
282 /* prevent intf to try to seek */
283 p_input->stream.b_seekable = 0;
285 /* Store selected chapter */
286 i_chapter = p_area->i_part;
288 dvdNewArea( p_input, p_area );
291 dvdNewPGC( p_input );
293 dvdplay_start( p_dvd->vmg, p_area->i_id );
295 p_area->i_part = i_chapter;
300 p_area = p_input->stream.p_selected_area;
307 if( (int)p_area->i_part != dvdplay_chapter_cur( p_dvd->vmg ) )
309 if( ( p_area->i_part > 0 ) &&
310 ( p_area->i_part <= p_area->i_part_nb ))
312 dvdplay_pg( p_dvd->vmg, p_area->i_part );
314 p_area->i_part = dvdplay_chapter_cur( p_dvd->vmg );
317 /* warn interface that something has changed */
319 LB2OFF( dvdplay_position( p_dvd->vmg ) ) - p_area->i_start;
320 p_input->stream.b_changed = 1;
322 /* Update the navigation variables without triggering a callback */
323 val.i_int = p_area->i_part;
324 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val );
329 /*****************************************************************************
330 * dvdplay_Read: reads data packets.
331 *****************************************************************************
332 * Returns -1 in case of error, the number of bytes read if everything went
334 *****************************************************************************/
335 static int dvdplay_Read( input_thread_t * p_input,
336 byte_t * p_buffer, size_t i_count )
341 p_dvd = (dvd_data_t *)p_input->p_access_data;
343 vlc_mutex_lock( &p_input->stream.stream_lock );
344 i_read = LB2OFF( dvdplay_read( p_dvd->vmg, p_buffer, OFF2LB( i_count ) ) );
345 vlc_mutex_unlock( &p_input->stream.stream_lock );
350 /*****************************************************************************
351 * dvdplay_Seek : Goes to a given position on the stream.
352 *****************************************************************************
353 * This one is used by the input and translate chronological position from
354 * input to logical position on the device.
355 * The lock should be taken before calling this function.
356 *****************************************************************************/
357 static void dvdplay_Seek( input_thread_t * p_input, off_t i_off )
361 p_dvd = (dvd_data_t *)p_input->p_access_data;
363 vlc_mutex_lock( &p_input->stream.stream_lock );
365 dvdplay_seek( p_dvd->vmg, OFF2LB( i_off ) );
367 p_input->stream.p_selected_area->i_tell =
368 LB2OFF( dvdplay_position( p_dvd->vmg ) ) -
369 p_input->stream.p_selected_area->i_start;
371 vlc_mutex_unlock( &p_input->stream.stream_lock );
377 /*****************************************************************************
378 * pf_vmg_callback: called by libdvdplay when some event happens
379 *****************************************************************************
380 * The stream lock has to be taken before entering here
381 *****************************************************************************/
382 static void pf_vmg_callback( void* p_args, dvdplay_event_t event )
384 input_thread_t * p_input;
389 p_input = (input_thread_t*)p_args;
390 p_dvd = (dvd_data_t*)p_input->p_access_data;
401 /* prevent intf to try to seek by default */
402 p_input->stream.b_seekable = 0;
404 i = dvdplay_title_cur( p_dvd->vmg );
405 if( i != p_input->stream.p_selected_area->i_id )
407 /* the title number has changed: update area */
408 msg_Warn( p_input, "new title %d (%d)", i,
409 p_input->stream.p_selected_area->i_id );
411 p_input->stream.pp_areas[i] );
414 /* new pgc in same title: reinit ES */
415 dvdNewPGC( p_input );
417 p_input->stream.b_changed = 1;
421 /* update current chapter */
422 p_input->stream.p_selected_area->i_part =
423 dvdplay_chapter_cur( p_dvd->vmg );
425 p_input->stream.p_selected_area->i_tell =
426 LB2OFF( dvdplay_position( p_dvd->vmg ) ) -
427 p_input->stream.p_selected_area->i_start;
429 /* warn interface that something has changed */
430 p_input->stream.b_changed = 1;
432 /* Update the navigation variables without triggering a callback */
433 val.i_int = p_input->stream.p_selected_area->i_part;
434 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val );
437 p_dvd->b_end_of_cell = 0;
440 p_dvd->b_end_of_cell = 1;
443 dvdplay_ES( p_input );
446 /* we must pause only from demux
447 * when the data in cache has been decoded */
448 p_dvd->i_still_time = dvdplay_still_time( p_dvd->vmg );
449 msg_Dbg( p_input, "still time %d", p_dvd->i_still_time );
454 if( var_Get( p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
456 vlc_mutex_t *p_mutex = val.p_address;
457 vlc_mutex_lock( p_mutex );
459 /* Retrieve the highlight from dvdplay */
460 dvdplay_highlight( p_dvd->vmg, &p_dvd->hli );
462 if( p_dvd->hli.i_x_start || p_dvd->hli.i_y_start ||
463 p_dvd->hli.i_x_end || p_dvd->hli.i_y_end )
465 /* Fill our internal variables with this data */
466 val.i_int = p_dvd->hli.i_x_start;
467 var_Set( p_input, "x-start", val );
468 val.i_int = p_dvd->hli.i_y_start;
469 var_Set( p_input, "y-start", val );
470 val.i_int = p_dvd->hli.i_x_end;
471 var_Set( p_input, "x-end", val );
472 val.i_int = p_dvd->hli.i_y_end;
473 var_Set( p_input, "y-end", val );
475 val.p_address = (void *)p_dvd->hli.pi_color;
476 var_Set( p_input, "color", val );
477 val.p_address = (void *)p_dvd->hli.pi_contrast;
478 var_Set( p_input, "contrast", val );
480 /* Tell the SPU decoder that there's a new highlight */
481 val.b_bool = VLC_TRUE;
485 /* Turn off the highlight */
486 val.b_bool = VLC_FALSE;
488 var_Set( p_input, "highlight", val );
490 vlc_mutex_unlock( p_mutex );
494 msg_Err( p_input, "unknown event from libdvdplay (%d)", event );
500 static int dvdNewArea( input_thread_t * p_input, input_area_t * p_area )
503 int i_angle_nb, i_angle;
507 p_dvd = (dvd_data_t*)p_input->p_access_data;
509 p_input->stream.p_selected_area = p_area;
512 * One program for each angle
514 while( p_input->stream.i_pgrm_number )
516 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
519 input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
520 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
522 dvdplay_angle_info( p_dvd->vmg, &i_angle_nb, &i_angle );
523 for( i = 1 ; i < i_angle_nb ; i++ )
525 input_AddProgram( p_input, i+1, 0 );
528 dvdplay_SetProgram( p_input,
529 p_input->stream.pp_programs[i_angle-1] );
531 /* No PSM to read in DVD mode, we already have all information */
532 p_input->stream.p_selected_program->b_is_ok = 1;
534 /* Update the navigation variables without triggering a callback */
535 val.i_int = p_area->i_id;
536 var_Change( p_input, "title", VLC_VAR_SETVALUE, &val );
537 var_Change( p_input, "chapter", VLC_VAR_CLEARCHOICES, NULL );
538 for( i = 1; (unsigned int)i <= p_area->i_part_nb; i++ )
541 var_Change( p_input, "chapter", VLC_VAR_ADDCHOICE, &val );
544 /* Update the navigation variables without triggering a callback */
545 val.i_int = p_area->i_part;
546 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val );
551 static int dvdNewPGC( input_thread_t * p_input )
554 // int i_audio_nr = -1;
556 // int i_subp_nr = -1;
560 p_dvd = (dvd_data_t*)p_input->p_access_data;
562 // dvdplay_audio_info( p_dvd->vmg, &i_audio_nr, &i_audio );
563 // dvdplay_subp_info( p_dvd->vmg, &i_subp_nr, &i_subp );
565 dvdplay_ES( p_input );
566 p_input->stream.p_selected_area->i_start =
567 LB2OFF( dvdplay_title_first( p_dvd->vmg ) );
568 p_input->stream.p_selected_area->i_size =
569 LB2OFF( dvdplay_title_end ( p_dvd->vmg ) ) -
570 p_input->stream.p_selected_area->i_start;
571 p_input->stream.p_selected_area->i_tell = 0;
573 if( p_input->stream.p_selected_area->i_size > 0 )
575 p_input->stream.b_seekable = 1;
579 p_input->stream.b_seekable = 0;
583 i_sec = dvdplay_title_time( p_dvd->vmg );
584 msg_Dbg( p_input, "title time: %d:%02d:%02d (%d)",
585 i_sec/3600, (i_sec%3600)/60, i_sec%60, i_sec );