]> git.sesse.net Git - vlc/blob - modules/access/dvdplay/access.c
9dcaf75bfd68680897b426faca86d795807df9a2
[vlc] / 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.2 2002/08/07 00:29:36 sam 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 #if defined( WIN32 )
49 #   include <io.h>                                                 /* read() */
50 #else
51 #   include <sys/uio.h>                                      /* struct iovec */
52 #endif
53
54 #if defined( WIN32 )
55 #   include "input_iovec.h"
56 #endif
57
58 #include "dvd.h"
59 #include "es.h"
60 #include "tools.h"
61 #include "intf.h"
62
63 /*****************************************************************************
64  * Local prototypes
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 );
71
72 static void   pf_vmg_callback       ( void*, dvdplay_event_t );
73
74 /* only from inside */
75 static int dvdNewArea( input_thread_t *, input_area_t * );
76 static int dvdNewPGC ( input_thread_t * );
77
78 /*****************************************************************************
79  * OpenDVD: open libdvdplay
80  *****************************************************************************/
81 int E_(OpenDVD) ( vlc_object_t *p_this )
82 {
83     input_thread_t *        p_input = (input_thread_t *)p_this;
84     char *                  psz_source;
85     dvd_data_t *            p_dvd;
86     input_area_t *          p_area;
87     int                     i_title_nr;
88     int                     i_title;
89     int                     i_chapter;
90     int                     i_angle;
91     int                     i;
92     
93     p_dvd = malloc( sizeof(dvd_data_t) );
94     if( p_dvd == NULL )
95     {
96         msg_Err( p_input, "dvdplay error: out of memory" );
97         return -1;
98     }
99
100     p_input->p_access_data = (void *)p_dvd;
101
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;
106
107     /* command line */
108     if( ( psz_source = dvdplay_ParseCL( p_input, 
109                         &i_title, &i_chapter, &i_angle ) ) == NULL )
110     {
111         free( p_dvd );
112         return -1;
113     }
114
115     /* Open libdvdplay */
116     p_dvd->vmg = dvdplay_open( psz_source, pf_vmg_callback, (void*)p_input );
117
118     /* free allocated strings */
119     free( psz_source );
120
121     if( p_dvd->vmg == NULL )
122     {
123         msg_Err( p_input, "dvdplay error: can't open source" );
124         free( p_dvd );
125         return -1;
126     }
127
128     p_dvd->p_intf = NULL;
129
130     p_dvd->i_still_time = 0;
131     
132     /* set up input  */
133     p_input->i_mtu = 0;
134
135     /* Set stream and area data */
136     vlc_mutex_lock( &p_input->stream.stream_lock );
137
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;
142     
143     /* Initialize ES structures */
144     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
145
146     /* disc input method */
147     p_input->stream.i_method = INPUT_METHOD_DVD;
148
149     i_title_nr = dvdplay_title_nr( p_dvd->vmg );
150 #define area p_input->stream.pp_areas
151
152     /* Area 0 for menu */
153     area[0]->i_plugin_data = 0;
154     
155     for( i = 1 ; i <= i_title_nr ; i++ )
156     {
157         input_AddArea( p_input );
158         
159         /* Titles id */
160         area[i]->i_id = i;
161
162         /* Number of chapters */
163         area[i]->i_part_nb = dvdplay_chapter_nr( p_dvd->vmg, i );
164
165         area[i]->i_plugin_data = 0;
166     }
167 #undef area
168     msg_Dbg( p_input, "number of titles: %d", i_title_nr );
169
170     i_title = i_title <= i_title_nr ? i_title : 0;
171
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;
175
176     /* set title, chapter, audio and subpic */
177     if( dvdplay_SetArea( p_input, p_area ) )
178     {
179         vlc_mutex_unlock( &p_input->stream.stream_lock );
180         return -1;
181     }
182     
183     if( i_angle <= p_input->stream.i_pgrm_number )
184     {
185         dvdplay_SetProgram( p_input,
186                             p_input->stream.pp_programs[i_angle - 1] );
187     }
188
189     vlc_mutex_unlock( &p_input->stream.stream_lock );
190
191     p_input->psz_demux = "dvdplay";
192
193     return 0;
194 }
195
196 /*****************************************************************************
197  * CloseDVD: close libdvdplay
198  *****************************************************************************/
199 void E_(CloseDVD) ( vlc_object_t *p_this )
200 {
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;
203
204     /* close libdvdplay */
205     dvdplay_close( p_dvd->vmg );
206
207     free( p_dvd );
208     p_input->p_access_data = NULL;
209
210 }
211
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 )
220 {
221     if( p_input->stream.p_selected_program != p_program )
222     {
223         dvd_data_t *    p_dvd;
224         int             i_angle;
225     
226         p_dvd = (dvd_data_t*)(p_input->p_access_data);
227         i_angle = p_program->i_number;
228
229         if( !dvdplay_angle( p_dvd->vmg, i_angle ) )
230         {
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;
235
236             msg_Dbg( p_input, "angle %d selected", i_angle );
237         }
238     }
239
240     return 0;
241 }
242
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 )
251 {
252     dvd_data_t *    p_dvd;
253
254     p_dvd = (dvd_data_t*)p_input->p_access_data;
255
256     /*
257      * Title selection
258      */
259     if( p_area != p_input->stream.p_selected_area )
260     {
261         int i_chapter;
262         
263         /* prevent intf to try to seek */
264         p_input->stream.b_seekable = 0;
265         
266         /* Store selected chapter */
267         i_chapter = p_area->i_part;
268
269         dvdNewArea( p_input, p_area );
270         
271         dvdplay_start( p_dvd->vmg, p_area->i_id );
272         
273         p_area->i_part = i_chapter;
274     } /* i_title >= 0 */
275     else
276     {
277         p_area = p_input->stream.p_selected_area;
278     }
279
280     /*
281      * Chapter selection
282      */
283
284     if( p_area->i_part != dvdplay_chapter_cur( p_dvd->vmg ) )
285     {
286         if( ( p_area->i_part > 0 ) &&
287             ( p_area->i_part <= p_area->i_part_nb ))
288         {
289             dvdplay_pg( p_dvd->vmg, p_area->i_part );
290         }
291         p_area->i_part = dvdplay_chapter_cur( p_dvd->vmg );
292     }
293
294     /* warn interface that something has changed */
295     p_area->i_tell =
296         LB2OFF( dvdplay_position( p_dvd->vmg ) ) - p_area->i_start;
297     p_input->stream.b_changed = 1;
298
299     return 0;
300 }
301
302 /*****************************************************************************
303  * dvdplay_Read: reads data packets.
304  *****************************************************************************
305  * Returns -1 in case of error, the number of bytes read if everything went
306  * well.
307  *****************************************************************************/
308 static int dvdplay_Read( input_thread_t * p_input,
309                          byte_t * p_buffer, size_t i_count )
310 {
311     dvd_data_t *    p_dvd;
312     off_t           i_read;
313     
314     p_dvd = (dvd_data_t *)p_input->p_access_data;
315
316     vlc_mutex_lock( &p_input->stream.stream_lock );
317
318     i_read = LB2OFF( dvdplay_read( p_dvd->vmg, p_buffer, OFF2LB( i_count ) ) );
319     
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;
323
324     vlc_mutex_unlock( &p_input->stream.stream_lock );
325     
326     return i_read;
327 }
328
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 )
337 {
338     dvd_data_t *     p_dvd;
339     
340     p_dvd = (dvd_data_t *)p_input->p_access_data;
341
342     vlc_mutex_lock( &p_input->stream.stream_lock );
343     
344     dvdplay_seek( p_dvd->vmg, OFF2LB( i_off ) );
345
346     vlc_mutex_unlock( &p_input->stream.stream_lock );
347
348     return;
349 }
350
351
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 )
358 {
359     input_thread_t *    p_input;
360     dvd_data_t *        p_dvd;
361     int                 i;
362
363     p_input = (input_thread_t*)p_args;
364     p_dvd   = (dvd_data_t*)p_input->p_access_data;
365     
366     switch( event )
367     {
368     case NEW_DOMAIN:
369         break;
370     case NEW_VTS:
371         break;
372     case NEW_FILE:
373
374         break;
375     case NEW_PGC:
376         /* prevent intf to try to seek  by default */
377         p_input->stream.b_seekable = 0;
378
379         if( ( i = dvdplay_title_cur( p_dvd->vmg ) ) != 
380                 p_input->stream.p_selected_area->i_id )
381         {
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 );
385             dvdNewArea( p_input,
386                         p_input->stream.pp_areas[i] );
387         }
388
389         /* new pgc in same title: reinit ES */
390         dvdNewPGC( p_input );
391         
392         p_input->stream.b_changed = 1;
393
394         break;
395     case NEW_PG:
396         /* update current chapter */
397         p_input->stream.p_selected_area->i_part =
398             dvdplay_chapter_cur( p_dvd->vmg );
399         break;
400     case NEW_CELL:
401         p_dvd->b_end_of_cell = 0;
402         break;
403     case END_OF_CELL:
404         p_dvd->b_end_of_cell = 1;
405         break;
406     case JUMP:
407         dvdplay_ES( p_input );
408         break;
409     case STILL_TIME:
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 );
414         break;
415     case COMPLETE_VIDEO:
416         break;
417     case NEW_HIGHLIGHT:
418         
419         break;
420     default:
421         msg_Err( p_input, "unknown event from libdvdplay (%d)",
422                       event );
423     }
424
425     return;
426 }
427
428 static int dvdNewArea( input_thread_t * p_input, input_area_t * p_area )
429 {
430     dvd_data_t *    p_dvd;
431     int             i_angle_nb, i_angle;
432     int             i;
433
434     p_dvd = (dvd_data_t*)p_input->p_access_data;
435
436     p_input->stream.p_selected_area = p_area;
437
438     /*
439      * One program for each angle
440      */
441     while( p_input->stream.i_pgrm_number )
442     {
443         input_DelProgram( p_input, p_input->stream.pp_programs[0] );
444     }
445
446     input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
447     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
448
449     dvdplay_angle_info( p_dvd->vmg, &i_angle_nb, &i_angle );
450     for( i = 1 ; i < i_angle_nb ; i++ )
451     {
452         input_AddProgram( p_input, i+1, 0 );
453     }
454     
455     dvdplay_SetProgram( p_input,
456                         p_input->stream.pp_programs[i_angle-1] ); 
457
458 //    dvdNewPGC( p_input );
459
460     /* No PSM to read in DVD mode, we already have all information */
461     p_input->stream.p_selected_program->b_is_ok = 1;
462
463     return 0;
464 }
465
466 static int dvdNewPGC( input_thread_t * p_input )
467 {
468     dvd_data_t *    p_dvd;
469 //    int             i_audio_nr  = -1;
470 //    int             i_audio     = -1;
471 //    int             i_subp_nr   = -1;
472 //    int             i_subp      = -1;
473 //    int             i_sec;
474     
475     p_dvd = (dvd_data_t*)p_input->p_access_data;
476
477 //    dvdplay_audio_info( p_dvd->vmg, &i_audio_nr, &i_audio );
478 //    dvdplay_subp_info( p_dvd->vmg, &i_subp_nr, &i_subp );
479
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;
486
487     if( p_input->stream.p_selected_area->i_size > 0 )
488     {
489         p_input->stream.b_seekable = 1;
490     }
491     else
492     {
493         p_input->stream.b_seekable = 0;
494     }
495     
496 #if 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 );
500 #endif
501
502     return 0;
503 }