* string review
[vlc.git] / modules / access / pvr / pvr.c
1 /*****************************************************************************
2  * pvr.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Eric Petit <titer@videolan.org>
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 <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <linux/types.h>
37 #include <sys/ioctl.h>
38 #include "videodev2.h"
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
45
46 #define DEVICE_TEXT N_( "Device" )
47 #define DEVICE_LONGTEXT N_( "PVR video device" )
48
49 #define NORM_TEXT N_( "Norm" )
50 #define NORM_LONGTEXT N_( "Defines the norm of the stream (Automatic, SECAM, PAL, or NTSC)" )
51 static int i_norm_list[] = { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL,
52                              V4L2_STD_NTSC };
53 static char *psz_norm_list_text[] = { N_("Automatic"), N_("SECAM"),
54                                       N_("PAL"),  N_("NTSC") };
55
56 #define WIDTH_TEXT N_( "Width" )
57 #define WIDTH_LONGTEXT N_( "Width of the stream to capture (-1 for " \
58                            "autodetect)" )
59 #define HEIGHT_TEXT N_( "Height" )
60 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture (-1 for " \
61                            "autodetect)" )
62 #define FREQUENCY_TEXT N_( "Frequency" )
63 #define FREQUENCY_LONGTEXT N_( "Frequency to capture (in kHz), if applicable" )
64 #define FRAMERATE_TEXT N_( "Framerate" )
65 #define FRAMERATE_LONGTEXT N_( "Framerate to capture, if applicable (-1 for " \
66                                "auto)" )
67 #define KEYINT_TEXT N_( "Key interval" )
68 #define KEYINT_LONGTEXT N_( "Interval between keyframes (-1 for " \
69                                 " auto)" )
70 #define BFRAMES_TEXT N_( "B Frames" )
71 #define BFRAMES_LONGTEXT N_("If this option is set, B-Frames will be used. " \
72                             "Use this option to set the number of B-Frames.")
73 #define BITRATE_TEXT N_( "Bitrate" )
74 #define BITRATE_LONGTEXT N_( "Bitrate to use (-1 for default)" )
75 #define BITRATE_PEAK_TEXT N_( "Bitrate peak" )
76 #define BITRATE_PEAK_LONGTEXT N_( "Peak bitrate in VBR mode" )
77 #define BITRATE_MODE_TEXT N_( "Bitrate mode (vbr or cbr)" )
78 #define BITRATE_MODE_LONGTEXT N_( "Bitrate mode to use" )
79 #define BITMASK_TEXT N_( "Audio bitmask" )
80 #define BITMASK_LONGTEXT N_("This option allows setting of bitmask that will get used by the audio part of the card." )
81 #define CHAN_TEXT N_( "Channel" )
82 #define CHAN_LONGTEXT N_( "Channel of the card to use (Usually, 0 = tuner, " \
83                           "1 = composite, 2 = svideo)" )
84
85 static int i_bitrates[] = { 0, 1 };
86 static char *psz_bitrates_list_text[] = { N_("vbr"), N_("cbr") };
87
88 vlc_module_begin();
89     set_shortname( _("PVR") );
90     set_description( _("MPEG Encoding cards input (with ivtv drivers)") );
91     set_capability( "access2", 0 );
92     add_shortcut( "pvr" );
93
94     add_string( "pvr-device", "/dev/video0", NULL, DEVICE_TEXT,
95                             DEVICE_LONGTEXT, VLC_FALSE );
96
97     add_integer( "pvr-norm", V4L2_STD_UNKNOWN , NULL, NORM_TEXT,
98                              NORM_LONGTEXT, VLC_FALSE );
99        change_integer_list( i_norm_list, psz_norm_list_text, 0 );
100
101     add_integer( "pvr-width", -1, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_TRUE );
102     add_integer( "pvr-height", -1, NULL, WIDTH_TEXT, WIDTH_LONGTEXT,
103                                          VLC_TRUE );
104     add_integer( "pvr-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
105                                          VLC_FALSE );
106     add_integer( "pvr-framerate", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
107                                          VLC_TRUE );
108     add_integer( "pvr-keyint", -1, NULL, KEYINT_TEXT, KEYINT_LONGTEXT,
109                                          VLC_TRUE );
110     add_integer( "pvr-bframes", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
111                                          VLC_TRUE );
112     add_integer( "pvr-bitrate", -1, NULL, BITRATE_TEXT, BITRATE_LONGTEXT,
113                                          VLC_FALSE );
114     add_integer( "pvr-bitrate-peak", -1, NULL, BITRATE_PEAK_TEXT,
115                                          BITRATE_PEAK_LONGTEXT, VLC_TRUE );
116     add_integer( "pvr-bitrate-mode", -1, NULL, BITRATE_MODE_TEXT,
117                                          BITRATE_MODE_LONGTEXT, VLC_TRUE );
118         change_integer_list( i_bitrates, psz_bitrates_list_text, 0 );
119     add_integer( "pvr-audio-bitmask", -1, NULL, BITMASK_TEXT,
120                                          BITMASK_LONGTEXT, VLC_TRUE );
121     add_integer( "pvr-channel", -1, NULL, CHAN_TEXT,
122                                           CHAN_LONGTEXT, VLC_TRUE );
123
124     set_callbacks( Open, Close );
125 vlc_module_end();
126
127 /*****************************************************************************
128  * Prototypes
129  *****************************************************************************/
130 static int Read   ( access_t *, uint8_t *, int );
131 static int Control( access_t *, int, va_list );
132
133 /* ivtv specific ioctls */
134 #define IVTV_IOC_G_CODEC    0xFFEE7703
135 #define IVTV_IOC_S_CODEC    0xFFEE7704
136
137 /* for use with IVTV_IOC_G_CODEC and IVTV_IOC_S_CODEC */
138
139 struct ivtv_ioctl_codec {
140         uint32_t aspect;
141         uint32_t audio_bitmask;
142         uint32_t bframes;
143         uint32_t bitrate_mode;
144         uint32_t bitrate;
145         uint32_t bitrate_peak;
146         uint32_t dnr_mode;
147         uint32_t dnr_spatial;
148         uint32_t dnr_temporal;
149         uint32_t dnr_type;
150         uint32_t framerate;
151         uint32_t framespergop;
152         uint32_t gop_closure;
153         uint32_t pulldown;
154         uint32_t stream_type;
155 };
156
157 struct access_sys_t
158 {
159     /* file descriptor */
160     int i_fd;
161
162     /* options */
163     int i_standard;
164     int i_width;
165     int i_height;
166     int i_frequency;
167     int i_framerate;
168     int i_keyint;
169     int i_bframes;
170     int i_bitrate;
171     int i_bitrate_peak;
172     int i_bitrate_mode;
173     int i_audio_bitmask;
174     int i_input;
175 };
176
177 /*****************************************************************************
178  * Open: open the device
179  *****************************************************************************/
180 static int Open( vlc_object_t * p_this )
181 {
182     access_t *p_access = (access_t*) p_this;
183     access_sys_t * p_sys;
184     char * psz_tofree, * psz_parser, * psz_device;
185     vlc_value_t val;
186
187     struct v4l2_format vfmt;
188     struct v4l2_frequency vf;
189     struct ivtv_ioctl_codec codec;
190
191     //psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
192
193     p_access->pf_read = Read;
194     p_access->pf_block = NULL;
195     p_access->pf_seek = NULL;
196     p_access->pf_control = Control;
197     p_access->info.i_update = 0;
198     p_access->info.i_size = 0;
199     p_access->info.i_pos = 0;
200     p_access->info.b_eof = VLC_FALSE;
201     p_access->info.i_title = 0;
202     p_access->info.i_seekpoint = 0;
203
204     /* create private access data */
205     p_sys = calloc( sizeof( access_sys_t ), 1 );
206     p_access->p_sys = p_sys;
207
208     /* defaults values */
209     var_Create( p_access, "pvr-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
210
211     var_Create( p_access, "pvr-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
212     var_Get( p_access, "pvr-device" , &val);
213     psz_device = val.psz_string;
214
215     var_Create( p_access, "pvr-norm", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
216     var_Get( p_access, "pvr-norm" , &val);
217     p_sys->i_standard = val.i_int;
218
219     var_Create( p_access, "pvr-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
220     var_Get( p_access, "pvr-width" , &val);
221     p_sys->i_width = val.i_int;
222
223     var_Create( p_access, "pvr-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
224     var_Get( p_access, "pvr-height" , &val);
225     p_sys->i_height = val.i_int;
226
227     var_Create( p_access, "pvr-frequency", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
228     var_Get( p_access, "pvr-frequency" , &val);
229     p_sys->i_frequency = val.i_int;
230
231     var_Create( p_access, "pvr-framerate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
232     var_Get( p_access, "pvr-framerate" , &val);
233     p_sys->i_framerate = val.i_int;
234
235     var_Create( p_access, "pvr-keyint", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
236     var_Get( p_access, "pvr-keyint" , &val);
237     p_sys->i_keyint = val.i_int;
238
239     var_Create( p_access, "pvr-bframes", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
240     var_Get( p_access, "pvr-bframes" , &val);
241     p_sys->i_bframes = val.b_bool;
242
243     var_Create( p_access, "pvr-bitrate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
244     var_Get( p_access, "pvr-bitrate" , &val);
245     p_sys->i_bitrate = val.i_int;
246
247     var_Create( p_access, "pvr-bitrate-peak", VLC_VAR_INTEGER |
248                                               VLC_VAR_DOINHERIT );
249     var_Get( p_access, "pvr-bitrate-peak" , &val);
250     p_sys->i_bitrate_peak = val.i_int;
251
252     var_Create( p_access, "pvr-bitrate-mode", VLC_VAR_INTEGER |
253                                               VLC_VAR_DOINHERIT );
254     var_Get( p_access, "pvr-bitrate-mode" , &val);
255     p_sys->i_bitrate_mode = val.i_int;
256
257     var_Create( p_access, "pvr-audio-bitmask", VLC_VAR_INTEGER |
258                                               VLC_VAR_DOINHERIT );
259     var_Get( p_access, "pvr-audio-bitmask" , &val);
260     p_sys->i_audio_bitmask = val.i_int;
261     var_Create( p_access, "pvr-channel", VLC_VAR_INTEGER |
262                                               VLC_VAR_DOINHERIT );
263     var_Get( p_access, "pvr-channel" , &val);
264     p_sys->i_input = val.i_int;
265
266     /* parse command line options */
267     psz_tofree = strdup( p_access->psz_path );
268     psz_parser = psz_tofree;
269
270     if( *psz_parser )
271     {
272         for( ;; )
273         {
274             if ( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
275             {
276                 char *psz_parser_init;
277                 psz_parser += strlen( "norm=" );
278                 psz_parser_init = psz_parser;
279                 while ( *psz_parser != ':' && *psz_parser != ','
280                                                     && *psz_parser != '\0' )
281                 {
282                     psz_parser++;
283                 }
284
285                 if ( !strncmp( psz_parser_init, "secam" ,
286                                psz_parser - psz_parser_init ) )
287                 {
288                     p_sys->i_standard = V4L2_STD_SECAM;
289                 }
290                 else if ( !strncmp( psz_parser_init, "pal" ,
291                                     psz_parser - psz_parser_init ) )
292                 {
293                     p_sys->i_standard = V4L2_STD_PAL;
294                 }
295                 else if ( !strncmp( psz_parser_init, "ntsc" ,
296                                     psz_parser - psz_parser_init ) )
297                 {
298                     p_sys->i_standard = V4L2_STD_NTSC;
299                 }
300                 else
301                 {
302                     p_sys->i_standard = strtol( psz_parser_init ,
303                                                 &psz_parser, 0 );
304                 }
305             }
306             else if( !strncmp( psz_parser, "channel=",
307                                strlen( "channel=" ) ) )
308             {
309                 p_sys->i_input =
310                   strtol( psz_parser + strlen( "channel=" ),
311                             &psz_parser, 0 );
312             }
313             else if( !strncmp( psz_parser, "device=", strlen( "device=" ) ) )
314             {
315                 psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
316                 sprintf( psz_device, "/dev/video%ld",
317                             strtol( psz_parser + strlen( "device=" ),
318                             &psz_parser, 0 ) );
319             }
320             else if( !strncmp( psz_parser, "frequency=",
321                                strlen( "frequency=" ) ) )
322             {
323                 p_sys->i_frequency =
324                   strtol( psz_parser + strlen( "frequency=" ),
325                             &psz_parser, 0 );
326             }
327             else if( !strncmp( psz_parser, "framerate=",
328                                strlen( "framerate=" ) ) )
329             {
330                 p_sys->i_framerate =
331                     strtol( psz_parser + strlen( "framerate=" ),
332                             &psz_parser, 0 );
333             }
334             else if( !strncmp( psz_parser, "keyint=",
335                                strlen( "keyint=" ) ) )
336             {
337                 p_sys->i_keyint =
338                     strtol( psz_parser + strlen( "keyint=" ),
339                             &psz_parser, 0 );
340             }
341             else if( !strncmp( psz_parser, "bframes=",
342                                strlen( "bframes=" ) ) )
343             {
344                 p_sys->i_bframes =
345                     strtol( psz_parser + strlen( "bframes=" ),
346                             &psz_parser, 0 );
347             }
348
349             else if( !strncmp( psz_parser, "width=",
350                                strlen( "width=" ) ) )
351             {
352                 p_sys->i_width =
353                     strtol( psz_parser + strlen( "width=" ),
354                             &psz_parser, 0 );
355             }
356             else if( !strncmp( psz_parser, "height=",
357                                strlen( "height=" ) ) )
358             {
359                 p_sys->i_height =
360                     strtol( psz_parser + strlen( "height=" ),
361                             &psz_parser, 0 );
362             }
363             else if( !strncmp( psz_parser, "audio=",
364                                strlen( "audio=" ) ) )
365             {
366                 p_sys->i_audio_bitmask =
367                     strtol( psz_parser + strlen( "audio=" ),
368                             &psz_parser, 0 );
369             }
370             else if( !strncmp( psz_parser, "bitrate=",
371                                strlen( "bitrate=" ) ) )
372             {
373                 p_sys->i_bitrate =
374                     strtol( psz_parser + strlen( "bitrate=" ),
375                             &psz_parser, 0 );
376             }
377             else if( !strncmp( psz_parser, "maxbitrate=",
378                                strlen( "maxbitrate=" ) ) )
379             {
380                 p_sys->i_bitrate_peak =
381                     strtol( psz_parser + strlen( "maxbitrate=" ),
382                             &psz_parser, 0 );
383             }
384             else if( !strncmp( psz_parser, "bitratemode=",
385                                strlen( "bitratemode=" ) ) )
386             {
387                 char *psz_parser_init;
388                 psz_parser += strlen( "bitratemode=" );
389                 psz_parser_init = psz_parser;
390                 while ( *psz_parser != ':' && *psz_parser != ','
391                          && *psz_parser != '\0' )
392                 {
393                     psz_parser++;
394                 }
395
396                 if ( !strncmp( psz_parser_init, "vbr" ,
397                                psz_parser - psz_parser_init ) )
398                 {
399                      p_sys->i_bitrate_mode = 0;
400                 }
401                 else if ( !strncmp( psz_parser_init, "cbr" ,
402                                     psz_parser - psz_parser_init ) )
403                 {
404                     p_sys->i_bitrate_mode = 1;
405                 }
406             }
407             else if( !strncmp( psz_parser, "size=",
408                                strlen( "size=" ) ) )
409             {
410                 p_sys->i_width =
411                     strtol( psz_parser + strlen( "size=" ),
412                             &psz_parser, 0 );
413                 p_sys->i_height =
414                     strtol( psz_parser + 1 ,
415                             &psz_parser, 0 );
416             }
417             else
418             {
419                 char *psz_parser_init;
420                 psz_parser_init = psz_parser;
421                 while ( *psz_parser != ':' && *psz_parser != ',' && *psz_parser != '\0' )
422                 {
423                     psz_parser++;
424                 }
425                 psz_device = calloc( psz_parser - psz_parser_init + 1, 1 );
426                 strncpy( psz_device, psz_parser_init,
427                          psz_parser - psz_parser_init );
428             }
429             if( *psz_parser )
430                 psz_parser++;
431             else
432                 break;
433         }
434     }
435
436     //give a default value to psz_device if none has bee specified
437
438     if (!psz_device)
439     {
440         psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
441         strcpy( psz_device, "/dev/video0" );
442     }
443
444     free( psz_tofree );
445
446     /* open the device */
447     if( ( p_sys->i_fd = open( psz_device, O_RDWR ) ) < 0 )
448     {
449         msg_Err( p_access, "cannot open device (%s)", strerror( errno ) );
450         return VLC_EGENERIC;
451     }
452     else
453     {
454         msg_Dbg( p_access, "using video device: %s",psz_device);
455     }
456
457     free( psz_device );
458
459     /* set the input */
460     if ( p_sys->i_input != -1 )
461     {
462         if ( ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) < 0 )
463         {
464             msg_Warn( p_access, "VIDIOC_S_INPUT failed" );
465         }
466         else
467         {
468             msg_Dbg( p_access, "input set to:%d", p_sys->i_input);
469         }
470     }
471
472     /* set the video standard */
473     if ( p_sys->i_standard != V4L2_STD_UNKNOWN )
474     {
475         if ( ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard ) < 0 )
476         {
477             msg_Warn( p_access, "VIDIOC_S_STD failed" );
478         }
479         else
480         {
481             msg_Dbg( p_access, "video standard set to:%x", p_sys->i_standard);
482         }
483     }
484
485     /* set the picture size */
486     if ( p_sys->i_width != -1 || p_sys->i_height != -1 )
487     {
488         if ( ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt ) < 0 )
489         {
490             msg_Warn( p_access, "VIDIOC_G_FMT failed" );
491         }
492         else
493         {
494             if ( p_sys->i_width != -1 )
495             {
496                 vfmt.fmt.pix.width = p_sys->i_width;
497             }
498
499             if ( p_sys->i_height != -1 )
500             {
501                 vfmt.fmt.pix.height = p_sys->i_height;
502             }
503
504             if ( ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt ) < 0 )
505             {
506                 msg_Warn( p_access, "VIDIOC_S_FMT failed" );
507             }
508             else
509             {
510                 msg_Dbg( p_access, "picture size set to:%dx%d",
511                          vfmt.fmt.pix.width, vfmt.fmt.pix.height );
512             }
513         }
514     }
515
516     /* set the frequency */
517     if ( p_sys->i_frequency != -1 )
518     {
519         vf.tuner = 0; /* TODO: let the user choose the tuner */
520         if ( ioctl( p_sys->i_fd, VIDIOC_G_FREQUENCY, &vf ) < 0 )
521         {
522             msg_Warn( p_access, "VIDIOC_G_FREQUENCY failed (%s)",
523                       strerror( errno ) );
524         }
525         else
526         {
527             vf.frequency = p_sys->i_frequency * 16 / 1000;
528             if( ioctl( p_sys->i_fd, VIDIOC_S_FREQUENCY, &vf ) < 0 )
529             {
530                 msg_Warn( p_access, "VIDIOC_S_FREQUENCY failed (%s)",
531                           strerror( errno ) );
532             }
533             else
534             {
535                 msg_Dbg( p_access, "Tuner frequency set to:%d",
536                          p_sys->i_frequency);
537             }
538         }
539     }
540
541     /* codec parameters */
542     if ( p_sys->i_framerate != -1
543             || p_sys->i_bitrate_mode != -1
544             || p_sys->i_bitrate_peak != -1
545             || p_sys->i_keyint != -1
546             || p_sys->i_bframes != -1
547             || p_sys->i_bitrate != -1
548             || p_sys->i_audio_bitmask != -1 )
549     {
550         if ( ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec ) < 0 )
551         {
552             msg_Warn( p_access, "IVTV_IOC_G_CODEC failed" );
553         }
554         else
555         {
556             if ( p_sys->i_framerate != -1 )
557             {
558                 switch ( p_sys->i_framerate )
559                 {
560                     case 30:
561                         codec.framerate = 0;
562                         break;
563
564                     case 25:
565                         codec.framerate = 1;
566                         break;
567
568                     default:
569                         msg_Warn( p_access, "invalid framerate, reverting to 25" );
570                         codec.framerate = 1;
571                         break;
572                 }
573             }
574
575             if ( p_sys->i_bitrate != -1 )
576             {
577                 codec.bitrate = p_sys->i_bitrate;
578             }
579
580             if ( p_sys->i_bitrate_peak != -1 )
581             {
582                 codec.bitrate_peak = p_sys->i_bitrate_peak;
583             }
584
585             if ( p_sys->i_bitrate_mode != -1 )
586             {
587                 codec.bitrate_mode = p_sys->i_bitrate_mode;
588             }
589
590             if ( p_sys->i_audio_bitmask != -1 )
591             {
592                 codec.audio_bitmask = p_sys->i_audio_bitmask;
593             }
594             if ( p_sys->i_keyint != -1 )
595             {
596                 codec.framespergop = p_sys->i_keyint;
597             }
598
599             if ( p_sys->i_bframes != -1 )
600             {
601                 codec.bframes = p_sys->i_bframes;
602             }
603             if( ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec ) < 0 )
604             {
605                 msg_Warn( p_access, "IVTV_IOC_S_CODEC failed" );
606             }
607             else
608             {
609                 msg_Dbg( p_access, "Setting codec parameters to:  framerate: %d, bitrate: %d/%d/%d",
610                codec.framerate, codec.bitrate, codec.bitrate_peak, codec.bitrate_mode );
611             }
612         }
613     }
614
615     /* do a quick read */
616 #if 0
617     if ( p_sys->i_fd )
618     {
619         if ( read( p_sys->i_fd, psz_tmp, 1 ) )
620         {
621             msg_Dbg(p_input, "Could read byte from device");
622         }
623         else
624         {
625             msg_Warn(p_input, "Could not read byte from device");
626         }
627     }
628 #endif
629
630     return VLC_SUCCESS;
631 }
632
633 /*****************************************************************************
634  * Close: close the device
635  *****************************************************************************/
636 static void Close( vlc_object_t * p_this )
637 {
638     access_t *p_access = (access_t*) p_this;
639     access_sys_t * p_sys = p_access->p_sys;
640
641     close( p_sys->i_fd );
642     free( p_sys );
643 }
644
645 /*****************************************************************************
646  * Read
647  *****************************************************************************/
648 static int Read( access_t * p_access, uint8_t * p_buffer, int i_len )
649 {
650     access_sys_t * p_sys = p_access->p_sys;
651
652     int i_ret;
653
654     struct timeval timeout;
655     fd_set fds;
656
657     FD_ZERO( &fds );
658     FD_SET( p_sys->i_fd, &fds );
659     timeout.tv_sec = 0;
660     timeout.tv_usec = 500000;
661
662     if( p_access->info.b_eof )
663         return 0;
664
665     while( !( i_ret = select( p_sys->i_fd + 1, &fds, NULL, NULL, &timeout) ) )
666     {
667         FD_ZERO( &fds );
668         FD_SET( p_sys->i_fd, &fds );
669         timeout.tv_sec = 0;
670         timeout.tv_usec = 500000;
671
672         if( p_access->b_die )
673             return 0;
674     }
675
676     if( i_ret < 0 )
677     {
678         msg_Err( p_access, "select error (%s)", strerror( errno ) );
679         return -1;
680     }
681
682     i_ret = read( p_sys->i_fd, p_buffer, i_len );
683     if( i_ret == 0 )
684     {
685         p_access->info.b_eof = VLC_TRUE;
686     }
687     else if( i_ret > 0 )
688     {
689         p_access->info.i_pos += i_ret;
690     }
691
692     return i_ret;
693 }
694
695 /*****************************************************************************
696  * Control
697  *****************************************************************************/
698 static int Control( access_t *p_access, int i_query, va_list args )
699 {
700     access_sys_t *p_sys = p_access->p_sys;
701     vlc_bool_t   *pb_bool;
702     int          *pi_int;
703     int64_t      *pi_64;
704
705     switch( i_query )
706     {
707         /* */
708         case ACCESS_CAN_SEEK:
709         case ACCESS_CAN_FASTSEEK:
710             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
711             *pb_bool = VLC_FALSE;
712             break;
713         case ACCESS_CAN_PAUSE:
714             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
715             *pb_bool = VLC_FALSE;
716             break;
717         case ACCESS_CAN_CONTROL_PACE:
718             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
719             *pb_bool = VLC_FALSE;
720             break;
721
722         /* */
723         case ACCESS_GET_MTU:
724             pi_int = (int*)va_arg( args, int * );
725             *pi_int = 0;
726             break;
727
728         case ACCESS_GET_PTS_DELAY:
729             pi_64 = (int64_t*)va_arg( args, int64_t * );
730             *pi_64 = (int64_t)var_GetInteger( p_access, "pvr-caching" ) * I64C(1000);
731             break;
732
733         /* */
734         case ACCESS_SET_PAUSE_STATE:
735             /* Nothing to do */
736             break;
737
738         case ACCESS_GET_TITLE_INFO:
739         case ACCESS_SET_TITLE:
740         case ACCESS_SET_SEEKPOINT:
741         case ACCESS_SET_PRIVATE_ID_STATE:
742             return VLC_EGENERIC;
743
744         default:
745             msg_Warn( p_access, "unimplemented query in control" );
746             return VLC_EGENERIC;
747
748     }
749     return VLC_SUCCESS;
750 }
751