Remove _GNU_SOURCE and string.h too
[vlc.git] / modules / access / v4l2.c
1 /*****************************************************************************
2  * v4l2.c : Video4Linux2 input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2004 the VideoLAN team
5  * $Id: v4l.c 16084 2006-07-19 09:45:02Z zorglub $
6  *
7  * Author: Benjamin Pracht <bigben at videolan dot 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include <stdio.h>
27
28 #include <vlc/vlc.h>
29 #include <vlc_access.h>
30 #include <vlc_demux.h>
31 #include <vlc_input.h>
32 #include <vlc_vout.h>
33
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/ioctl.h>
39 #include <sys/errno.h>
40
41 #include <asm/types.h>          /* for videodev2.h */
42
43 #include <linux/videodev2.h>
44
45 /*****************************************************************************
46  * Module descriptior
47   *****************************************************************************/
48
49 static int  Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
51
52 #define DEV_TEXT N_("Device name")
53 #define DEV_LONGTEXT N_( \
54     "Name of the device to use. " \
55     "If you don't specify anything, /dev/video0 will be used.")
56 #define INPUT_TEXT N_( "Input" )
57 #define INPUT_LONGTEXT N_( \
58     "Input of the card to use (Usually, 0 = tuner, " \
59     "1 = composite, 2 = svideo)." )
60
61
62 vlc_module_begin();
63     set_shortname( _("Video4Linux2") );
64     set_description( _("Video4Linux2 input") );
65     set_category( CAT_INPUT );
66     set_subcategory( SUBCAT_INPUT_ACCESS );
67
68     add_string( "v4l2-dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
69                 VLC_FALSE );
70     add_integer( "v4l2-input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
71                 VLC_TRUE );
72
73     add_shortcut( "v4l2" );
74     set_capability( "access_demux", 10 );
75     set_callbacks( Open, Close );
76 vlc_module_end();
77
78 /*****************************************************************************
79  * Access: local prototypes
80  *****************************************************************************/
81
82 static int DemuxMMAP( demux_t * );
83 static int Control( demux_t *, int, va_list );
84
85 static int ProbeDev( demux_t * );
86 static int OpenVideoDev( demux_t * );
87
88 struct demux_sys_t
89 {
90     char *psz_device;
91
92     int i_fd_video;
93
94     struct v4l2_capability dev_cap;
95
96     int i_input;
97     struct v4l2_input *p_inputs;
98     int i_selected_input;
99
100     int i_audio;
101     /* V4L2 devices cannot have more than 32 audio inputs */
102     struct v4l2_audio p_audios[32];
103
104     int i_tuner;
105     struct v4l2_tuner *p_tuners;
106
107     int i_codec;
108     struct v4l2_fmtdesc *p_codecs;
109 };
110
111 /*****************************************************************************
112  * Open: opens v4l device
113  *****************************************************************************
114  *
115  * url: <video device>::::
116  *
117  *****************************************************************************/
118 static int Open( vlc_object_t *p_this )
119 {
120     demux_t     *p_demux = (demux_t*)p_this;
121     demux_sys_t *p_sys;
122
123     /* Only when selected */
124     if( *p_demux->psz_access == '\0' )
125         return VLC_EGENERIC;
126
127     /* Set up p_demux */
128     p_demux->pf_control = Control;
129     p_demux->info.i_update = 0;
130     p_demux->info.i_title = 0;
131     p_demux->info.i_seekpoint = 0;
132
133     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
134     if( p_sys == NULL ) return VLC_ENOMEM;
135     memset( p_sys, 0, sizeof( demux_sys_t ) );
136
137     p_sys->psz_device = var_CreateGetString( p_demux, "v4l2-dev" );
138
139     if( ProbeDev( p_demux ) < 0 ) return VLC_EGENERIC;
140
141     if( OpenVideoDev( p_demux ) < 0 ) return VLC_EGENERIC;
142
143     return VLC_SUCCESS;
144 }
145
146 /*****************************************************************************
147  * Close: close device, free resources
148  *****************************************************************************/
149 static void Close( vlc_object_t *p_this )
150 {
151     demux_t     *p_demux = (demux_t *)p_this;
152     demux_sys_t *p_sys   = p_demux->p_sys;
153
154     if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
155
156     if( p_sys->psz_device ) free( p_sys->psz_device );
157     if( p_sys->p_inputs ) free( p_sys->p_inputs );
158     if( p_sys->p_tuners ) free( p_sys->p_tuners );
159     if( p_sys->p_codecs ) free( p_sys->p_codecs );
160
161     free( p_sys );
162 }
163
164 /*****************************************************************************
165  * Control:
166  *****************************************************************************/
167 static int Control( demux_t *p_demux, int i_query, va_list args )
168 {
169     return VLC_EGENERIC;
170 }
171
172 /*****************************************************************************
173  * Demux:
174  *****************************************************************************/
175 static int DemuxMMAP( demux_t *p_demux )
176 {
177     msleep( 40000 );
178     return 1;
179 }
180
181
182 /*****************************************************************************
183  * OpenVideoDev: open and set up the video device and probe for capabilities
184  *****************************************************************************/
185 int OpenVideoDev( demux_t *p_demux )
186 {
187     int i_fd;
188     demux_sys_t *p_sys = p_demux->p_sys;
189
190     if( ( i_fd = open( p_sys->psz_device, O_RDWR ) ) < 0 )
191     {
192         msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
193         goto open_failed;
194     }
195
196     p_sys->i_fd_video = i_fd;
197     p_sys->i_selected_input = var_CreateGetInteger( p_demux, "v4l2-input" );
198
199     if( p_sys->i_selected_input > p_sys->i_input )
200     {
201         msg_Warn( p_demux, "invalid input. Using the default one" );
202         p_sys->i_selected_input = 0;
203     }
204
205     if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
206     {
207        msg_Err( p_demux, "cannot set input (%s)", strerror( errno ) );
208        goto open_failed;
209     }
210
211     if( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING )
212     {
213         struct v4l2_requestbuffers reqbuf;
214
215         reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
216         reqbuf.memory = V4L2_MEMORY_MMAP;
217         reqbuf.count = 0;
218
219         if( ioctl( i_fd, VIDIOC_REQBUFS, &reqbuf ) < 0 )
220         {
221             msg_Err( p_demux, "cannot initiate I/O operation (%s). "
222                     "Only MMAP is supported at the moment", strerror( errno ) );
223             goto open_failed;
224         }
225         p_demux->pf_demux = DemuxMMAP;
226     }
227     else
228     {
229         msg_Warn( p_demux, "I/O method not supported at the moment" );
230         goto open_failed;
231     }
232
233     return VLC_SUCCESS;
234
235 open_failed:
236     if( i_fd ) close( i_fd );
237     p_sys->i_fd_video = 0;
238     return VLC_EGENERIC;
239
240 }
241
242 /*****************************************************************************
243  * ProbeDev: probe for capabilities
244  *****************************************************************************/
245 int ProbeDev( demux_t *p_demux )
246 {
247     int i_index;
248
249     int i_fd;
250     demux_sys_t *p_sys = p_demux->p_sys;
251
252     if( ( i_fd = open( p_sys->psz_device, O_RDWR ) ) < 0 )
253     {
254         msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
255         goto open_failed;
256     }
257
258     /* Get device capabilites */
259
260     if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
261     {
262         msg_Err( p_demux, "cannot get capabilities (%s)", strerror( errno ) );
263         goto open_failed;
264     }
265
266   msg_Dbg( p_demux, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
267                             p_sys->dev_cap.card,
268                             p_sys->dev_cap.driver,
269                             (p_sys->dev_cap.version >> 16) & 0xFF,
270                             (p_sys->dev_cap.version >> 8) & 0xFF,
271                             p_sys->dev_cap.version & 0xFF,
272                             p_sys->dev_cap.bus_info );
273
274     msg_Dbg( p_demux, "the device has the capabilities: (%c) Video Capure, "
275                                                        "(%c) Audio, "
276                                                        "(%c) Tuner",
277              ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE  ? 'X':' '),
278              ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO  ? 'X':' '),
279              ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER  ? 'X':' ') );
280
281     msg_Dbg( p_demux, "supported I/O methods are: (%c) Read/Write, "
282                                                  "(%c) Streaming, "
283                                                  "(%c) Asynchronous",
284             ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
285             ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
286             ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
287
288     /* Now, enumerate all the video inputs. This is useless at the moment
289        since we have no way to present that info to the user except with
290        debug messages */
291
292     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
293     {
294         while( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) >= 0 )
295         {
296             p_sys->i_input++;
297         }
298
299         p_sys->p_inputs = malloc( p_sys->i_input * sizeof( struct v4l2_input ) );
300         if( !p_sys->p_inputs ) goto open_failed;
301         memset( p_sys->p_inputs, 0, sizeof( struct v4l2_input ) );
302
303         for( i_index = 0; i_index < p_sys->i_input; i_index++ )
304         {
305             p_sys->p_inputs[i_index].index = i_index;
306
307             if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
308             {
309                 msg_Err( p_demux, "cannot get video input characteristics (%s)",
310                                                  strerror( errno ) );
311                 goto open_failed;
312             }
313             msg_Dbg( p_demux, "video input %i (%s) has type: %s",
314                                 i_index,
315                                 p_sys->p_inputs[i_index].name,
316                                 p_sys->p_inputs[i_index].type
317                                         == V4L2_INPUT_TYPE_TUNER ?
318                                         "Tuner adapter" :
319                                         "External analog input" );
320         }
321     }
322
323     /* initialize the structures for the ioctls */
324     for( i_index = 0; i_index < 32; i_index++ )
325     {
326         p_sys->p_audios[i_index].index = i_index;
327     }
328
329     /* Probe audio inputs */
330
331     if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
332     {
333         while( p_sys->i_audio < 32 &&
334                ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
335         {
336             if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
337             {
338                 msg_Err( p_demux, "cannot get video input characteristics (%s)",
339                                                  strerror( errno ) );
340                 goto open_failed;
341             }
342
343             msg_Dbg( p_demux, "audio device %i (%s) is %s",
344                                 p_sys->i_audio,
345                                 p_sys->p_audios[p_sys->i_audio].name,
346                                 p_sys->p_audios[p_sys->i_audio].capability &
347                                                     V4L2_AUDCAP_STEREO ?
348                                         "Stereo" : "Mono" );
349
350             p_sys->i_audio++;
351         }
352     }
353
354     if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
355     {
356         struct v4l2_tuner tuner;
357
358         memset( &tuner, 0, sizeof(tuner) );
359         while( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) >= 0 )
360         {
361             p_sys->i_tuner++;
362             tuner.index = p_sys->i_tuner;
363         }
364
365         p_sys->p_tuners = malloc( p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
366         if( !p_sys->p_tuners ) goto open_failed;
367         memset( p_sys->p_tuners, 0, sizeof( struct v4l2_tuner ) );
368
369         for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
370         {
371             p_sys->p_tuners[i_index].index = i_index;
372
373             if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
374             {
375                 msg_Err( p_demux, "cannot get tuner characteristics (%s)",
376                                                  strerror( errno ) );
377                 goto open_failed;
378             }
379             msg_Dbg( p_demux, "tuner %i (%s) has type: %s, "
380                               "frequency range: %.1f %s -> %.1f %s",
381                                 i_index,
382                                 p_sys->p_tuners[i_index].name,
383                                 p_sys->p_tuners[i_index].type
384                                         == V4L2_TUNER_RADIO ?
385                                         "Radio" : "Analog TV",
386                                 p_sys->p_tuners[i_index].rangelow * 62.5,
387                                 p_sys->p_tuners[i_index].capability &
388                                         V4L2_TUNER_CAP_LOW ?
389                                         "Hz" : "kHz",
390                                 p_sys->p_tuners[i_index].rangehigh * 62.5,
391                                 p_sys->p_tuners[i_index].capability &
392                                         V4L2_TUNER_CAP_LOW ?
393                                         "Hz" : "kHz" );
394         }
395     }
396
397     /* Probe for available chromas */
398     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
399     {
400         struct v4l2_fmtdesc codec;
401
402         i_index = 0;
403         memset( &codec, 0, sizeof(codec) );
404         codec.index = i_index;
405         codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
406
407         while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
408         {
409             i_index++;
410             codec.index = i_index;
411         }
412
413         p_sys->i_codec = i_index;
414
415         p_sys->p_codecs = malloc( p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
416         memset( p_sys->p_codecs, 0, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) ); 
417
418         for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
419         {
420             p_sys->p_codecs[i_index].index = i_index;
421             p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
422
423             if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
424             {
425                 msg_Err( p_demux, "cannot get codec description (%s)", strerror( errno ) );
426                 goto open_failed;
427             }
428
429             msg_Dbg( p_demux, "device supports Codec %s",
430                                 p_sys->p_codecs[i_index].description );
431         }
432     }
433
434
435     if( i_fd >= 0 ) close( i_fd );
436     return VLC_SUCCESS;
437
438 open_failed:
439     
440     if( i_fd >= 0 ) close( i_fd );
441     return VLC_EGENERIC;
442
443 }