* modules/access/dvd*: changed some errors in warnings because the directory
[vlc.git] / modules / access / dvdplay / access.c
1 /*****************************************************************************
2  * access.c: access capabilities for dvdplay plugin.
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: access.c,v 1.14 2003/03/24 14:18:28 hartman Exp $
6  *
7  * Author: St├ęphane Borel <stef@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "../../demux/mpeg/system.h"
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h>
37 #endif
38
39 #include <fcntl.h>
40 #include <sys/types.h>
41 #include <string.h>
42 #include <errno.h>
43
44 #ifdef STRNCASECMP_IN_STRINGS_H
45 #   include <strings.h>
46 #endif
47
48 #include "dvd.h"
49 #include "es.h"
50 #include "tools.h"
51 #include "intf.h"
52
53 /*****************************************************************************
54  * Local prototypes
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 );
61
62 static void   pf_vmg_callback       ( void*, dvdplay_event_t );
63
64 /* only from inside */
65 static int dvdNewArea( input_thread_t *, input_area_t * );
66 static int dvdNewPGC ( input_thread_t * );
67
68 /*****************************************************************************
69  * OpenDVD: open libdvdplay
70  *****************************************************************************/
71 int E_(OpenDVD) ( vlc_object_t *p_this )
72 {
73     input_thread_t *        p_input = (input_thread_t *)p_this;
74     char *                  psz_source;
75     dvd_data_t *            p_dvd;
76     input_area_t *          p_area;
77     unsigned int            i_title_nr;
78     unsigned int            i_title;
79     unsigned int            i_chapter;
80     unsigned int            i_angle;
81     unsigned int            i;
82
83     p_dvd = malloc( sizeof(dvd_data_t) );
84     if( p_dvd == NULL )
85     {
86         msg_Err( p_input, "out of memory" );
87         return -1;
88     }
89
90     p_input->p_access_data = (void *)p_dvd;
91
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;
96
97     /* command line */
98     if( ( psz_source = dvdplay_ParseCL( p_input,
99                         &i_title, &i_chapter, &i_angle ) ) == NULL )
100     {
101         free( p_dvd );
102         return -1;
103     }
104
105     /* Open libdvdplay */
106     p_dvd->vmg = dvdplay_open( psz_source, pf_vmg_callback, (void*)p_input );
107
108     if( p_dvd->vmg == NULL )
109     {
110         msg_Warn( p_input, "cannot open %s", psz_source );
111         free( psz_source );
112         free( p_dvd );
113         return -1;
114     }
115
116     /* free allocated strings */
117     free( psz_source );
118
119     p_dvd->p_intf = NULL;
120
121     p_dvd->i_still_time = 0;
122
123     /* set up input  */
124     p_input->i_mtu = 0;
125
126     /* Set stream and area data */
127     vlc_mutex_lock( &p_input->stream.stream_lock );
128
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;
134
135     /* Initialize ES structures */
136     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
137
138     /* disc input method */
139     p_input->stream.i_method = INPUT_METHOD_DVD;
140
141     i_title_nr = dvdplay_title_nr( p_dvd->vmg );
142 #define area p_input->stream.pp_areas
143
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 );
148
149     for( i = 1 ; i <= i_title_nr ; i++ )
150     {
151         input_AddArea( p_input, i, dvdplay_chapter_nr( p_dvd->vmg, i ) );
152         area[i]->i_plugin_data = 0;
153     }
154 #undef area
155     msg_Dbg( p_input, "number of titles: %d", i_title_nr );
156
157     i_title = i_title <= i_title_nr ? i_title : 0;
158
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;
162
163     /* set title, chapter, audio and subpic */
164     if( dvdplay_SetArea( p_input, p_area ) )
165     {
166         vlc_mutex_unlock( &p_input->stream.stream_lock );
167         return -1;
168     }
169
170     if( i_angle <= p_input->stream.i_pgrm_number )
171     {
172         dvdplay_SetProgram( p_input,
173                             p_input->stream.pp_programs[i_angle - 1] );
174     }
175
176     vlc_mutex_unlock( &p_input->stream.stream_lock );
177
178     if( !p_input->psz_demux || !*p_input->psz_demux )
179     {
180         p_input->psz_demux = "dvdplay";
181     }
182
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 );
188
189     var_Create( p_input, "color", VLC_VAR_ADDRESS );
190     var_Create( p_input, "contrast", VLC_VAR_ADDRESS );
191
192     var_Create( p_input, "highlight", VLC_VAR_BOOL );
193     var_Create( p_input, "highlight-mutex", VLC_VAR_MUTEX );
194
195     return 0;
196 }
197
198 /*****************************************************************************
199  * CloseDVD: close libdvdplay
200  *****************************************************************************/
201 void E_(CloseDVD) ( vlc_object_t *p_this )
202 {
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;
205
206     var_Destroy( p_input, "highlight-mutex" );
207     var_Destroy( p_input, "highlight" );
208
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" );
213
214     var_Destroy( p_input, "color" );
215     var_Destroy( p_input, "contrast" );
216
217     /* close libdvdplay */
218     dvdplay_close( p_dvd->vmg );
219
220     free( p_dvd );
221     p_input->p_access_data = NULL;
222
223 }
224
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 )
233 {
234     if( p_input->stream.p_selected_program != p_program )
235     {
236         dvd_data_t *    p_dvd;
237         int             i_angle;
238         vlc_value_t     val;
239
240         p_dvd = (dvd_data_t*)(p_input->p_access_data);
241         i_angle = p_program->i_number;
242
243         if( !dvdplay_angle( p_dvd->vmg, i_angle ) )
244         {
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;
249
250             msg_Dbg( p_input, "angle %d selected", i_angle );
251         }
252
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 );
256     }
257
258     return 0;
259 }
260
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 )
269 {
270     dvd_data_t *    p_dvd;
271     vlc_value_t     val;
272
273     p_dvd = (dvd_data_t*)p_input->p_access_data;
274
275     /*
276      * Title selection
277      */
278     if( p_area != p_input->stream.p_selected_area )
279     {
280         int i_chapter;
281
282         /* prevent intf to try to seek */
283         p_input->stream.b_seekable = 0;
284
285         /* Store selected chapter */
286         i_chapter = p_area->i_part;
287
288         dvdNewArea( p_input, p_area );
289
290         /* Reinit ES */
291         dvdNewPGC( p_input );
292
293         dvdplay_start( p_dvd->vmg, p_area->i_id );
294
295         p_area->i_part = i_chapter;
296
297     } /* i_title >= 0 */
298     else
299     {
300         p_area = p_input->stream.p_selected_area;
301     }
302
303     /*
304      * Chapter selection
305      */
306
307     if( (int)p_area->i_part != dvdplay_chapter_cur( p_dvd->vmg ) )
308     {
309         if( ( p_area->i_part > 0 ) &&
310             ( p_area->i_part <= p_area->i_part_nb ))
311         {
312             dvdplay_pg( p_dvd->vmg, p_area->i_part );
313         }
314         p_area->i_part = dvdplay_chapter_cur( p_dvd->vmg );
315     }
316
317     /* warn interface that something has changed */
318     p_area->i_tell =
319         LB2OFF( dvdplay_position( p_dvd->vmg ) ) - p_area->i_start;
320     p_input->stream.b_changed = 1;
321
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 );
325
326     return 0;
327 }
328
329 /*****************************************************************************
330  * dvdplay_Read: reads data packets.
331  *****************************************************************************
332  * Returns -1 in case of error, the number of bytes read if everything went
333  * well.
334  *****************************************************************************/
335 static int dvdplay_Read( input_thread_t * p_input,
336                          byte_t * p_buffer, size_t i_count )
337 {
338     dvd_data_t *    p_dvd;
339     off_t           i_read;
340
341     p_dvd = (dvd_data_t *)p_input->p_access_data;
342
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 );
346
347     return i_read;
348 }
349
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 )
358 {
359     dvd_data_t *     p_dvd;
360
361     p_dvd = (dvd_data_t *)p_input->p_access_data;
362
363     vlc_mutex_lock( &p_input->stream.stream_lock );
364
365     dvdplay_seek( p_dvd->vmg, OFF2LB( i_off ) );
366
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;
370
371     vlc_mutex_unlock( &p_input->stream.stream_lock );
372
373     return;
374 }
375
376
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 )
383 {
384     input_thread_t *    p_input;
385     dvd_data_t *        p_dvd;
386     vlc_value_t         val;
387     unsigned int        i;
388
389     p_input = (input_thread_t*)p_args;
390     p_dvd   = (dvd_data_t*)p_input->p_access_data;
391
392     switch( event )
393     {
394     case NEW_DOMAIN:
395         break;
396     case NEW_VTS:
397         break;
398     case NEW_FILE:
399         break;
400     case NEW_PGC:
401         /* prevent intf to try to seek  by default */
402         p_input->stream.b_seekable = 0;
403
404         i = dvdplay_title_cur( p_dvd->vmg );
405         if( i != p_input->stream.p_selected_area->i_id )
406         {
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 );
410             dvdNewArea( p_input,
411                         p_input->stream.pp_areas[i] );
412         }
413
414         /* new pgc in same title: reinit ES */
415         dvdNewPGC( p_input );
416
417         p_input->stream.b_changed = 1;
418
419         break;
420     case NEW_PG:
421         /* update current chapter */
422         p_input->stream.p_selected_area->i_part =
423             dvdplay_chapter_cur( p_dvd->vmg );
424
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;
428
429         /* warn interface that something has changed */
430         p_input->stream.b_changed = 1;
431
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 );
435         break;
436     case NEW_CELL:
437         p_dvd->b_end_of_cell = 0;
438         break;
439     case END_OF_CELL:
440         p_dvd->b_end_of_cell = 1;
441         break;
442     case JUMP:
443         dvdplay_ES( p_input );
444         break;
445     case STILL_TIME:
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 );
450         break;
451     case COMPLETE_VIDEO:
452         break;
453     case NEW_HIGHLIGHT:
454         if( var_Get( p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
455         {
456             vlc_mutex_t *p_mutex = val.p_address;
457             vlc_mutex_lock( p_mutex );
458
459             /* Retrieve the highlight from dvdplay */
460             dvdplay_highlight( p_dvd->vmg, &p_dvd->hli );
461
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 )
464             {
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 );
474
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 );
479
480                 /* Tell the SPU decoder that there's a new highlight */
481                 val.b_bool = VLC_TRUE;
482             }
483             else
484             {
485                 /* Turn off the highlight */
486                 val.b_bool = VLC_FALSE;
487             }
488             var_Set( p_input, "highlight", val );
489
490             vlc_mutex_unlock( p_mutex );
491         }
492         break;
493     default:
494         msg_Err( p_input, "unknown event from libdvdplay (%d)", event );
495     }
496
497     return;
498 }
499
500 static int dvdNewArea( input_thread_t * p_input, input_area_t * p_area )
501 {
502     dvd_data_t *    p_dvd;
503     int             i_angle_nb, i_angle;
504     vlc_value_t     val;
505     int             i;
506
507     p_dvd = (dvd_data_t*)p_input->p_access_data;
508
509     p_input->stream.p_selected_area = p_area;
510
511     /*
512      * One program for each angle
513      */
514     while( p_input->stream.i_pgrm_number )
515     {
516         input_DelProgram( p_input, p_input->stream.pp_programs[0] );
517     }
518
519     input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
520     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
521
522     dvdplay_angle_info( p_dvd->vmg, &i_angle_nb, &i_angle );
523     for( i = 1 ; i < i_angle_nb ; i++ )
524     {
525         input_AddProgram( p_input, i+1, 0 );
526     }
527
528     dvdplay_SetProgram( p_input,
529                         p_input->stream.pp_programs[i_angle-1] );
530
531     /* No PSM to read in DVD mode, we already have all information */
532     p_input->stream.p_selected_program->b_is_ok = 1;
533
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++ )
539     {
540         val.i_int = i;
541         var_Change( p_input, "chapter", VLC_VAR_ADDCHOICE, &val );
542     }
543
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 );
547
548     return 0;
549 }
550
551 static int dvdNewPGC( input_thread_t * p_input )
552 {
553     dvd_data_t *    p_dvd;
554 //    int             i_audio_nr  = -1;
555 //    int             i_audio     = -1;
556 //    int             i_subp_nr   = -1;
557 //    int             i_subp      = -1;
558 //    int             i_sec;
559
560     p_dvd = (dvd_data_t*)p_input->p_access_data;
561
562 //    dvdplay_audio_info( p_dvd->vmg, &i_audio_nr, &i_audio );
563 //    dvdplay_subp_info( p_dvd->vmg, &i_subp_nr, &i_subp );
564
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;
572
573     if( p_input->stream.p_selected_area->i_size > 0 )
574     {
575         p_input->stream.b_seekable = 1;
576     }
577     else
578     {
579         p_input->stream.b_seekable = 0;
580     }
581
582 #if 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 );
586 #endif
587
588     return 0;
589 }