]> git.sesse.net Git - vlc/blob - modules/access/oss.c
3fe0b7c7ffc0bd688376e854933d1f6b5cc1cc3f
[vlc] / modules / access / oss.c
1 /*****************************************************************************
2  * oss.c : OSS input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Benjamin Pracht <bigben at videolan dot org>
8  *          Richard Hosking <richard at hovis dot net>
9  *          Antoine Cellerier <dionoea at videolan d.t org>
10  *          Dennis Lou <dlou99 at yahoo dot com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_access.h>
38 #include <vlc_demux.h>
39 #include <vlc_fs.h>
40
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <sys/ioctl.h>
44 #include <sys/mman.h>
45
46 #ifdef HAVE_SYS_SOUNDCARD_H
47 #  include <sys/soundcard.h>
48 #endif
49 #ifdef HAVE_SOUNDCARD_H
50 #  include <soundcard.h>
51 #endif
52
53 #include <poll.h>
54
55 /*****************************************************************************
56  * Module descriptior
57  *****************************************************************************/
58
59 static int  DemuxOpen ( vlc_object_t * );
60 static void DemuxClose( vlc_object_t * );
61
62
63 #define STEREO_TEXT N_( "Stereo" )
64 #define STEREO_LONGTEXT N_( \
65     "Capture the audio stream in stereo." )
66 #define SAMPLERATE_TEXT N_( "Samplerate" )
67 #define SAMPLERATE_LONGTEXT N_( \
68     "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
69
70 #define CACHING_TEXT N_("Caching value in ms")
71 #define CACHING_LONGTEXT N_( \
72     "Caching value for OSS captures. This " \
73     "value should be set in milliseconds." )
74
75 #define OSS_DEFAULT "/dev/dsp"
76
77 #define CFG_PREFIX "oss-"
78
79 vlc_module_begin ()
80     set_shortname( N_("OSS") )
81     set_description( N_("OSS input") )
82     set_category( CAT_INPUT )
83     set_subcategory( SUBCAT_INPUT_ACCESS )
84
85     add_shortcut( "oss" )
86     set_capability( "access_demux", 10 )
87     set_callbacks( DemuxOpen, DemuxClose )
88
89     add_bool( CFG_PREFIX "stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
90                 true )
91     add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
92                 SAMPLERATE_LONGTEXT, true )
93     add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
94                 CACHING_TEXT, CACHING_LONGTEXT, true )
95 vlc_module_end ()
96
97 /*****************************************************************************
98  * Access: local prototypes
99  *****************************************************************************/
100
101 static int DemuxControl( demux_t *, int, va_list );
102
103 static int Demux( demux_t * );
104
105 static block_t* GrabAudio( demux_t *p_demux );
106
107 static int OpenAudioDev( demux_t * );
108 static int OpenAudioDevOss( demux_t * );
109 static bool ProbeAudioDevOss( demux_t *, const char *psz_device );
110
111 struct buffer_t
112 {
113     void *  start;
114     size_t  length;
115 };
116
117 struct demux_sys_t
118 {
119     const char *psz_device;  /* OSS device from MRL */
120
121     int  i_fd;
122
123     /* Audio */
124     int i_cache;
125     unsigned int i_sample_rate;
126     bool b_stereo;
127     size_t i_max_frame_size;
128     block_t *p_block;
129     es_out_id_t *p_es;
130
131     int64_t i_next_demux_date; /* Used to handle oss:// as input-slave properly */
132 };
133
134 static int FindMainDevice( demux_t *p_demux )
135 {
136     msg_Dbg( p_demux, "opening device '%s'", p_demux->p_sys->psz_device );
137     if( ProbeAudioDevOss( p_demux, p_demux->p_sys->psz_device ) )
138     {
139         msg_Dbg( p_demux, "'%s' is an audio device", p_demux->p_sys->psz_device );
140         p_demux->p_sys->i_fd = OpenAudioDev( p_demux );
141     }
142
143     if( p_demux->p_sys->i_fd < 0 )
144         return VLC_EGENERIC;
145     return VLC_SUCCESS;
146 }
147
148 /*****************************************************************************
149  * DemuxOpen: opens oss device, access_demux callback
150  *****************************************************************************
151  *
152  * url: <oss device>::::
153  *
154  *****************************************************************************/
155 static int DemuxOpen( vlc_object_t *p_this )
156 {
157     demux_t     *p_demux = (demux_t*)p_this;
158     demux_sys_t *p_sys;
159
160     /* Only when selected */
161     if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
162
163     /* Set up p_demux */
164     p_demux->pf_control = DemuxControl;
165     p_demux->pf_demux = Demux;
166     p_demux->info.i_update = 0;
167     p_demux->info.i_title = 0;
168     p_demux->info.i_seekpoint = 0;
169
170     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
171     if( p_sys == NULL ) return VLC_ENOMEM;
172
173     p_sys->i_sample_rate = var_CreateGetInteger( p_demux, CFG_PREFIX "samplerate" );
174     p_sys->b_stereo = var_CreateGetBool( p_demux, CFG_PREFIX "stereo" );
175     p_sys->i_cache = var_CreateGetInteger( p_demux, CFG_PREFIX "caching" );
176     p_sys->i_fd = -1;
177     p_sys->p_es = NULL;
178     p_sys->p_block = NULL;
179     p_sys->i_next_demux_date = -1;
180
181     if( p_demux->psz_location && *p_demux->psz_location )
182         p_sys->psz_device = p_demux->psz_location;
183     else
184         p_sys->psz_device = OSS_DEFAULT;
185
186     if( FindMainDevice( p_demux ) != VLC_SUCCESS )
187     {
188         DemuxClose( p_this );
189         return VLC_EGENERIC;
190     }
191
192     return VLC_SUCCESS;
193 }
194
195 /*****************************************************************************
196  * Close: close device, free resources
197  *****************************************************************************/
198 static void DemuxClose( vlc_object_t *p_this )
199 {
200     demux_t     *p_demux = (demux_t *)p_this;
201     demux_sys_t *p_sys   = p_demux->p_sys;
202
203     if( p_sys->i_fd >= 0 ) close( p_sys->i_fd );
204
205     if( p_sys->p_block ) block_Release( p_sys->p_block );
206     free( p_sys );
207 }
208
209 /*****************************************************************************
210  * DemuxControl:
211  *****************************************************************************/
212 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
213 {
214     demux_sys_t *p_sys = p_demux->p_sys;
215     bool *pb;
216     int64_t    *pi64;
217
218     switch( i_query )
219     {
220         /* Special for access_demux */
221         case DEMUX_CAN_PAUSE:
222         case DEMUX_CAN_SEEK:
223         case DEMUX_CAN_CONTROL_PACE:
224             pb = (bool*)va_arg( args, bool * );
225             *pb = false;
226             return VLC_SUCCESS;
227
228         case DEMUX_GET_PTS_DELAY:
229             pi64 = (int64_t*)va_arg( args, int64_t * );
230             *pi64 = (int64_t)p_sys->i_cache * 1000;
231             return VLC_SUCCESS;
232
233         case DEMUX_GET_TIME:
234             pi64 = (int64_t*)va_arg( args, int64_t * );
235             *pi64 = mdate();
236             return VLC_SUCCESS;
237
238         case DEMUX_SET_NEXT_DEMUX_TIME:
239             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
240             return VLC_SUCCESS;
241
242         /* TODO implement others */
243         default:
244             return VLC_EGENERIC;
245     }
246
247     return VLC_EGENERIC;
248 }
249
250 /*****************************************************************************
251  * Demux: Processes the audio frame
252  *****************************************************************************/
253 static int Demux( demux_t *p_demux )
254 {
255     demux_sys_t *p_sys = p_demux->p_sys;
256
257     struct pollfd fd;
258     fd.fd = p_sys->i_fd;
259     fd.events = POLLIN|POLLPRI;
260     fd.revents = 0;
261
262     block_t *p_block = NULL;
263
264     do
265     {
266         if( p_block )
267         {
268             es_out_Send( p_demux->out, p_sys->p_es, p_block );
269             p_block = NULL;
270         }
271
272         /* Wait for data */
273         if( poll( &fd, 1, 10 ) ) /* Timeout after 0.01 seconds. Bigger delays are an issue when used with/as an input-slave since all the inputs run in the same thread. */
274         {
275             if( fd.revents & (POLLIN|POLLPRI) )
276             {
277                 p_block = GrabAudio( p_demux );
278                 if( p_block )
279                     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
280             }
281         }
282     } while( p_block && p_sys->i_next_demux_date > 0 &&
283              p_block->i_pts < p_sys->i_next_demux_date );
284
285     if( p_block )
286         es_out_Send( p_demux->out, p_sys->p_es, p_block );
287
288     return 1;
289 }
290
291 /*****************************************************************************
292  * GrabAudio: Grab an audio frame
293  *****************************************************************************/
294 static block_t* GrabAudio( demux_t *p_demux )
295 {
296     demux_sys_t *p_sys = p_demux->p_sys;
297     struct audio_buf_info buf_info;
298     int i_read = 0, i_correct;
299     block_t *p_block;
300
301     if( p_sys->p_block ) p_block = p_sys->p_block;
302     else p_block = block_New( p_demux, p_sys->i_max_frame_size );
303
304     if( !p_block )
305     {
306         msg_Warn( p_demux, "cannot get block" );
307         return 0;
308     }
309
310     p_sys->p_block = p_block;
311
312     i_read = read( p_sys->i_fd, p_block->p_buffer,
313                 p_sys->i_max_frame_size );
314
315     if( i_read <= 0 ) return 0;
316
317     p_block->i_buffer = i_read;
318     p_sys->p_block = 0;
319
320     /* Correct the date because of kernel buffering */
321     i_correct = i_read;
322     if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
323     {
324         i_correct += buf_info.bytes;
325     }
326
327     /* Timestamp */
328     p_block->i_pts = p_block->i_dts =
329         mdate() - INT64_C(1000000) * (mtime_t)i_correct /
330         2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
331
332     return p_block;
333 }
334
335 /*****************************************************************************
336  * OpenAudioDev: open and set up the audio device and probe for capabilities
337  *****************************************************************************/
338 static int OpenAudioDevOss( demux_t *p_demux )
339 {
340     int i_fd;
341     int i_format;
342
343     i_fd = vlc_open( p_demux->p_sys->psz_device, O_RDONLY | O_NONBLOCK );
344
345     if( i_fd < 0 )
346     {
347         msg_Err( p_demux, "cannot open OSS audio device (%m)" );
348         goto adev_fail;
349     }
350
351     i_format = AFMT_S16_LE;
352     if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
353         || i_format != AFMT_S16_LE )
354     {
355         msg_Err( p_demux,
356                  "cannot set audio format (16b little endian) (%m)" );
357         goto adev_fail;
358     }
359
360     if( ioctl( i_fd, SNDCTL_DSP_STEREO,
361                &p_demux->p_sys->b_stereo ) < 0 )
362     {
363         msg_Err( p_demux, "cannot set audio channels count (%m)" );
364         goto adev_fail;
365     }
366
367     if( ioctl( i_fd, SNDCTL_DSP_SPEED,
368                &p_demux->p_sys->i_sample_rate ) < 0 )
369     {
370         msg_Err( p_demux, "cannot set audio sample rate (%m)" );
371         goto adev_fail;
372     }
373
374     p_demux->p_sys->i_max_frame_size = 6 * 1024;
375
376     return i_fd;
377
378  adev_fail:
379
380     if( i_fd >= 0 ) close( i_fd );
381     return -1;
382 }
383
384 static int OpenAudioDev( demux_t *p_demux )
385 {
386     demux_sys_t *p_sys = p_demux->p_sys;
387     int i_fd = OpenAudioDevOss( p_demux );
388
389     if( i_fd < 0 )
390         return i_fd;
391
392     msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
393              p_sys->psz_device, p_sys->b_stereo ? "stereo" : "mono",
394              p_sys->i_sample_rate );
395
396     es_format_t fmt;
397     es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L );
398
399     fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
400     fmt.audio.i_rate = p_sys->i_sample_rate;
401     fmt.audio.i_bitspersample = 16;
402     fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
403     fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
404
405     msg_Dbg( p_demux, "new audio es %d channels %dHz",
406              fmt.audio.i_channels, fmt.audio.i_rate );
407
408     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
409
410     return i_fd;
411 }
412
413 /*****************************************************************************
414  * ProbeAudioDev: probe audio for capabilities
415  *****************************************************************************/
416 static bool ProbeAudioDevOss( demux_t *p_demux, const char *psz_device )
417 {
418     int i_caps;
419     int i_fd = vlc_open( psz_device, O_RDONLY | O_NONBLOCK );
420
421     if( i_fd < 0 )
422     {
423         msg_Err( p_demux, "cannot open device %s for OSS audio (%m)", psz_device );
424         goto open_failed;
425     }
426
427     /* this will fail if the device is video */
428     if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
429     {
430         msg_Err( p_demux, "cannot get audio caps (%m)" );
431         goto open_failed;
432     }
433
434     if( i_fd >= 0 ) close( i_fd );
435
436     return true;
437
438 open_failed:
439     if( i_fd >= 0 ) close( i_fd );
440     return false;
441 }