contrib: soxr: enable by default
[vlc.git] / modules / access / oss.c
blob477b1c64cd67df0f7918f7b40b5bd750234ac804
1 /*****************************************************************************
2 * oss.c : OSS input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2009 VLC authors and VideoLAN
5 * $Id$
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>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
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>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <sys/ioctl.h>
45 #include <sys/mman.h>
47 #ifdef HAVE_SYS_SOUNDCARD_H
48 # include <sys/soundcard.h>
49 #endif
50 #ifdef HAVE_SOUNDCARD_H
51 # include <soundcard.h>
52 #endif
54 #include <poll.h>
56 /*****************************************************************************
57 * Module descriptior
58 *****************************************************************************/
60 static int DemuxOpen ( vlc_object_t * );
61 static void DemuxClose( vlc_object_t * );
64 #define STEREO_TEXT N_( "Stereo" )
65 #define STEREO_LONGTEXT N_( \
66 "Capture the audio stream in stereo." )
67 #define SAMPLERATE_TEXT N_( "Samplerate" )
68 #define SAMPLERATE_LONGTEXT N_( \
69 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
71 #define OSS_DEFAULT "/dev/dsp"
73 #define CFG_PREFIX "oss-"
75 vlc_module_begin ()
76 set_shortname( N_("OSS") )
77 set_description( N_("OSS input") )
78 set_category( CAT_INPUT )
79 set_subcategory( SUBCAT_INPUT_ACCESS )
81 add_shortcut( "oss" )
82 set_capability( "access_demux", 0 )
83 set_callbacks( DemuxOpen, DemuxClose )
85 add_bool( CFG_PREFIX "stereo", true, STEREO_TEXT, STEREO_LONGTEXT,
86 true )
87 add_integer( CFG_PREFIX "samplerate", 48000, SAMPLERATE_TEXT,
88 SAMPLERATE_LONGTEXT, true )
89 vlc_module_end ()
91 /*****************************************************************************
92 * Access: local prototypes
93 *****************************************************************************/
95 static int DemuxControl( demux_t *, int, va_list );
97 static int Demux( demux_t * );
99 static block_t* GrabAudio( demux_t *p_demux );
101 static int OpenAudioDev( demux_t * );
102 static int OpenAudioDevOss( demux_t * );
103 static bool ProbeAudioDevOss( demux_t *, const char *psz_device );
105 struct buffer_t
107 void * start;
108 size_t length;
111 struct demux_sys_t
113 const char *psz_device; /* OSS device from MRL */
115 int i_fd;
117 /* Audio */
118 unsigned int i_sample_rate;
119 bool b_stereo;
120 size_t i_max_frame_size;
121 block_t *p_block;
122 es_out_id_t *p_es;
124 int64_t i_next_demux_date; /* Used to handle oss:// as input-slave properly */
127 static int FindMainDevice( demux_t *p_demux )
129 demux_sys_t *p_sys = p_demux->p_sys;
131 msg_Dbg( p_demux, "opening device '%s'", p_sys->psz_device );
132 if( ProbeAudioDevOss( p_demux, p_sys->psz_device ) )
134 msg_Dbg( p_demux, "'%s' is an audio device", p_sys->psz_device );
135 p_sys->i_fd = OpenAudioDev( p_demux );
138 if( p_sys->i_fd < 0 )
139 return VLC_EGENERIC;
140 return VLC_SUCCESS;
143 /*****************************************************************************
144 * DemuxOpen: opens oss device, access_demux callback
145 *****************************************************************************
147 * url: <oss device>::::
149 *****************************************************************************/
150 static int DemuxOpen( vlc_object_t *p_this )
152 demux_t *p_demux = (demux_t*)p_this;
153 demux_sys_t *p_sys;
155 /* Set up p_demux */
156 p_demux->pf_control = DemuxControl;
157 p_demux->pf_demux = Demux;
159 p_demux->p_sys = p_sys = vlc_obj_calloc( p_this, 1, sizeof( demux_sys_t ) );
160 if( p_sys == NULL ) return VLC_ENOMEM;
162 p_sys->i_sample_rate = var_InheritInteger( p_demux, CFG_PREFIX "samplerate" );
163 p_sys->b_stereo = var_InheritBool( p_demux, CFG_PREFIX "stereo" );
164 p_sys->i_fd = -1;
165 p_sys->p_es = NULL;
166 p_sys->p_block = NULL;
167 p_sys->i_next_demux_date = -1;
169 if( p_demux->psz_location && *p_demux->psz_location )
170 p_sys->psz_device = p_demux->psz_location;
171 else
172 p_sys->psz_device = OSS_DEFAULT;
174 if( FindMainDevice( p_demux ) != VLC_SUCCESS )
176 DemuxClose( p_this );
177 return VLC_EGENERIC;
180 return VLC_SUCCESS;
183 /*****************************************************************************
184 * Close: close device, free resources
185 *****************************************************************************/
186 static void DemuxClose( vlc_object_t *p_this )
188 demux_t *p_demux = (demux_t *)p_this;
189 demux_sys_t *p_sys = p_demux->p_sys;
191 if( p_sys->i_fd >= 0 )
192 vlc_close( p_sys->i_fd );
194 if( p_sys->p_block ) block_Release( p_sys->p_block );
197 /*****************************************************************************
198 * DemuxControl:
199 *****************************************************************************/
200 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
202 demux_sys_t *p_sys = p_demux->p_sys;
203 bool *pb;
204 int64_t *pi64;
206 switch( i_query )
208 /* Special for access_demux */
209 case DEMUX_CAN_PAUSE:
210 case DEMUX_CAN_SEEK:
211 case DEMUX_CAN_CONTROL_PACE:
212 pb = va_arg( args, bool * );
213 *pb = false;
214 return VLC_SUCCESS;
216 case DEMUX_GET_PTS_DELAY:
217 pi64 = va_arg( args, int64_t * );
218 *pi64 = INT64_C(1000)
219 * var_InheritInteger( p_demux, "live-caching" );
220 return VLC_SUCCESS;
222 case DEMUX_GET_TIME:
223 pi64 = va_arg( args, int64_t * );
224 *pi64 = mdate();
225 return VLC_SUCCESS;
227 case DEMUX_SET_NEXT_DEMUX_TIME:
228 p_sys->i_next_demux_date = va_arg( args, int64_t );
229 return VLC_SUCCESS;
231 /* TODO implement others */
232 default:
233 return VLC_EGENERIC;
236 return VLC_EGENERIC;
239 /*****************************************************************************
240 * Demux: Processes the audio frame
241 *****************************************************************************/
242 static int Demux( demux_t *p_demux )
244 demux_sys_t *p_sys = p_demux->p_sys;
246 struct pollfd fd;
247 fd.fd = p_sys->i_fd;
248 fd.events = POLLIN|POLLPRI;
249 fd.revents = 0;
251 block_t *p_block = NULL;
255 if( p_block )
257 es_out_Send( p_demux->out, p_sys->p_es, p_block );
258 p_block = NULL;
261 /* Wait for data */
262 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. */
264 if( errno == EINTR )
265 continue;
266 if( fd.revents & (POLLIN|POLLPRI) )
268 p_block = GrabAudio( p_demux );
269 if( p_block )
270 es_out_SetPCR( p_demux->out, p_block->i_pts );
273 } while( p_block && p_sys->i_next_demux_date > 0 &&
274 p_block->i_pts < p_sys->i_next_demux_date );
276 if( p_block )
277 es_out_Send( p_demux->out, p_sys->p_es, p_block );
279 return 1;
282 /*****************************************************************************
283 * GrabAudio: Grab an audio frame
284 *****************************************************************************/
285 static block_t* GrabAudio( demux_t *p_demux )
287 demux_sys_t *p_sys = p_demux->p_sys;
288 struct audio_buf_info buf_info;
289 int i_read = 0, i_correct;
290 block_t *p_block;
292 if( p_sys->p_block ) p_block = p_sys->p_block;
293 else p_block = block_Alloc( p_sys->i_max_frame_size );
295 if( !p_block )
297 msg_Warn( p_demux, "cannot get block" );
298 return NULL;
301 p_sys->p_block = p_block;
303 i_read = read( p_sys->i_fd, p_block->p_buffer,
304 p_sys->i_max_frame_size );
306 if( i_read <= 0 ) return NULL;
308 p_block->i_buffer = i_read;
309 p_sys->p_block = NULL;
311 /* Correct the date because of kernel buffering */
312 i_correct = i_read;
313 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
315 i_correct += buf_info.bytes;
318 /* Timestamp */
319 p_block->i_pts = p_block->i_dts =
320 mdate() - INT64_C(1000000) * (mtime_t)i_correct /
321 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
323 return p_block;
326 /*****************************************************************************
327 * OpenAudioDev: open and set up the audio device and probe for capabilities
328 *****************************************************************************/
329 static int OpenAudioDevOss( demux_t *p_demux )
331 demux_sys_t *p_sys = (demux_sys_t *)p_demux->p_sys;
332 int i_fd;
333 int i_format;
335 i_fd = vlc_open( p_sys->psz_device, O_RDONLY | O_NONBLOCK );
337 if( i_fd < 0 )
339 msg_Err( p_demux, "cannot open OSS audio device (%s)",
340 vlc_strerror_c(errno) );
341 goto adev_fail;
344 i_format = AFMT_S16_LE;
345 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
346 || i_format != AFMT_S16_LE )
348 msg_Err( p_demux,
349 "cannot set audio format (16b little endian) (%s)",
350 vlc_strerror_c(errno) );
351 goto adev_fail;
354 if( ioctl( i_fd, SNDCTL_DSP_STEREO, &p_sys->b_stereo ) < 0 )
356 msg_Err( p_demux, "cannot set audio channels count (%s)",
357 vlc_strerror_c(errno) );
358 goto adev_fail;
361 if( ioctl( i_fd, SNDCTL_DSP_SPEED, &p_sys->i_sample_rate ) < 0 )
363 msg_Err( p_demux, "cannot set audio sample rate (%s)",
364 vlc_strerror_c(errno) );
365 goto adev_fail;
368 p_sys->i_max_frame_size = 6 * 1024;
370 return i_fd;
372 adev_fail:
374 if( i_fd >= 0 )
375 vlc_close( i_fd );
376 return -1;
379 static int OpenAudioDev( demux_t *p_demux )
381 demux_sys_t *p_sys = p_demux->p_sys;
382 int i_fd = OpenAudioDevOss( p_demux );
384 if( i_fd < 0 )
385 return i_fd;
387 msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
388 p_sys->psz_device, p_sys->b_stereo ? "stereo" : "mono",
389 p_sys->i_sample_rate );
391 es_format_t fmt;
392 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L );
394 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
395 fmt.audio.i_rate = p_sys->i_sample_rate;
396 fmt.audio.i_bitspersample = 16;
397 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
398 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
400 msg_Dbg( p_demux, "new audio es %d channels %dHz",
401 fmt.audio.i_channels, fmt.audio.i_rate );
403 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
405 return i_fd;
408 /*****************************************************************************
409 * ProbeAudioDev: probe audio for capabilities
410 *****************************************************************************/
411 static bool ProbeAudioDevOss( demux_t *p_demux, const char *psz_device )
413 int i_caps;
414 int i_fd = vlc_open( psz_device, O_RDONLY | O_NONBLOCK );
416 if( i_fd < 0 )
418 msg_Err( p_demux, "cannot open device %s for OSS audio (%s)",
419 psz_device, vlc_strerror_c(errno) );
420 goto open_failed;
423 /* this will fail if the device is video */
424 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
426 msg_Err( p_demux, "cannot get audio caps (%s)",
427 vlc_strerror_c(errno) );
428 goto open_failed;
431 if( i_fd >= 0 )
432 vlc_close( i_fd );
434 return true;
436 open_failed:
437 if( i_fd >= 0 )
438 vlc_close( i_fd );
439 return false;