4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "audioconfig.h"
23 #include "audiodevice.h"
26 #include "condition.h"
29 #include "playbackconfig.h"
30 #include "preferences.h"
31 #include "recordconfig.h"
38 // These are only available in commercial OSS
41 #define AFMT_S32_LE 0x00001000
42 #define AFMT_S32_BE 0x00002000
46 // Synchronie multiple devices by using threads
48 OSSThread::OSSThread(AudioOSS *device)
54 this->device = device;
55 input_lock = new Condition(0, "OSSThread::input_lock");
56 output_lock = new Condition(1, "OSSThread::output_lock");
57 read_lock = new Condition(0, "OSSThread::read_lock");
58 write_lock = new Condition(0, "OSSThread::write_lock");
64 timer_lock = new Mutex("OSSThread::timer_lock");
68 OSSThread::~OSSThread()
84 input_lock->lock("OSSThread::run 1");
87 read(fd, data, bytes);
92 unsigned char *bp = data;
94 Thread::enable_cancel();
95 int ret = write(fd, bp, bytes);
96 Thread::disable_cancel();
101 timer_lock->lock("OSSThread::run");
102 if( !ioctl(fd, SNDCTL_DSP_GETODELAY, &delay) )
104 bytes_written += ret;
105 timer_lock->unlock();
107 write_lock->unlock();
109 output_lock->unlock();
113 void OSSThread::write_data(int fd, unsigned char *data, int bytes)
115 output_lock->lock("OSSThread::write_data");
121 input_lock->unlock();
124 void OSSThread::read_data(int fd, unsigned char *data, int bytes)
126 output_lock->lock("OSSThread::read_data");
132 input_lock->unlock();
135 void OSSThread::wait_read()
137 read_lock->lock("OSSThread::wait_read");
140 void OSSThread::wait_write()
142 write_lock->lock("OSSThread::wait_write");
147 int64_t OSSThread::device_position()
149 timer_lock->lock("OSSThread::device_position");
150 int64_t ret = bytes_written - delay;
151 timer_lock->unlock();
156 AudioOSS::AudioOSS(AudioDevice *device)
157 : AudioLowLevel(device)
159 for(int i = 0; i < MAXDEVICES; i++) {
160 dsp_in[i] = dsp_out[i] = -1;
163 data_allocated[i] = 0;
167 AudioOSS::~AudioOSS()
171 int AudioOSS::open_input()
173 device->in_bits = device->in_config->oss_in_bits;
174 // 24 bits not available in OSS
175 if(device->in_bits == 24) device->in_bits = 32;
177 for(int i = 0; i < MAXDEVICES; i++) {
178 if(device->in_config->oss_enable[i]) {
179 //printf("AudioOSS::open_input 10\n");
180 dsp_in[i] = open(device->in_config->oss_in_device[i], O_RDONLY/* | O_NDELAY*/);
181 //printf("AudioOSS::open_input 20\n");
183 fprintf(stderr, "AudioOSS::open_input %s: %s\n",
184 device->in_config->oss_in_device[i], strerror(errno));
189 int format = get_fmt(device->in_config->oss_in_bits);
190 int buffer_info = sizetofrag(device->in_samples,
191 device->get_ichannels(),
192 device->in_config->oss_in_bits);
194 set_cloexec_flag(dsp_in[i], 1);
196 // For the ice1712 the buffer must be maximum or no space will be allocated.
197 if(device->idriver == AUDIO_OSS_ENVY24)
198 buffer_info = 0x7fff000f;
200 if(ioctl(dsp_in[i], SNDCTL_DSP_SETFRAGMENT, &buffer_info))
201 fprintf(stderr, _("%s failed\n"), "SNDCTL_DSP_SETFRAGMENT");
202 else if(ioctl(dsp_in[i], SNDCTL_DSP_SETFMT, &format) < 0)
203 fprintf(stderr, _("%s failed\n"), "SNDCTL_DSP_SETFMT");
205 int channels = device->get_ichannels();
206 if(ioctl(dsp_in[i], SNDCTL_DSP_CHANNELS, &channels) < 0)
207 fprintf(stderr, _("%s failed\n"), "SNDCTL_DSP_CHANNELS");
208 else if(ioctl(dsp_in[i], SNDCTL_DSP_SPEED, &device->in_samplerate) < 0)
209 fprintf(stderr, _("%s failed\n"), "SNDCTL_DSP_SPEED");
218 audio_buf_info recinfo;
219 ioctl(dsp_in[i], SNDCTL_DSP_GETISPACE, &recinfo);
221 //printf("AudioOSS::open_input fragments=%d fragstotal=%d fragsize=%d bytes=%d\n",
222 // recinfo.fragments, recinfo.fragstotal, recinfo.fragsize, recinfo.bytes);
224 thread[i] = new OSSThread(this);
231 int AudioOSS::open_output()
233 device->out_bits = device->out_config->oss_out_bits;
234 // OSS only supports 8, 16, and 32
235 if(device->out_bits == 24) device->out_bits = 32;
237 for(int i = 0; i < MAXDEVICES; i++) {
238 if(device->out_config->oss_enable[i]) {
239 // Linux 2.4.18 no longer supports allocating the maximum buffer size.
240 // Need the shrink fragment size in preferences until it works.
242 open(device->out_config->oss_out_device[i],
243 O_WRONLY /*| O_NDELAY*/);
245 perror("AudioOSS::open_output");
250 int format = get_fmt(device->out_config->oss_out_bits);
251 int buffer_info = sizetofrag(device->out_samples,
252 device->get_ochannels(),
253 device->out_config->oss_out_bits);
255 set_cloexec_flag(dsp_out[i], 1);
257 // For the ice1712 the buffer must be maximum or no space will be allocated.
258 if(device->odriver == AUDIO_OSS_ENVY24)
259 buffer_info = 0x7fff000f;
261 if(ioctl(dsp_out[i], SNDCTL_DSP_SETFRAGMENT, &buffer_info))
262 fprintf(stderr,"SNDCTL_DSP_SETFRAGMENT 2 failed.\n");
263 else if(ioctl(dsp_out[i], SNDCTL_DSP_SETFMT, &format) < 0)
264 fprintf(stderr,"SNDCTL_DSP_SETFMT 2 failed\n");
266 int channels = device->get_ochannels();
267 if(ioctl(dsp_out[i], SNDCTL_DSP_CHANNELS, &channels) < 0)
268 fprintf(stderr,"SNDCTL_DSP_CHANNELS 2 failed\n");
269 else if(ioctl(dsp_out[i], SNDCTL_DSP_SPEED, &device->out_samplerate) < 0)
270 fprintf(stderr,"SNDCTL_DSP_SPEED 2 failed\n");
279 audio_buf_info playinfo;
280 ioctl(dsp_out[i], SNDCTL_DSP_GETOSPACE, &playinfo);
281 // printf("AudioOSS::open_output fragments=%d fragstotal=%d fragsize=%d bytes=%d\n",
282 // playinfo.fragments, playinfo.fragstotal, playinfo.fragsize, playinfo.bytes);
283 device->device_buffer = playinfo.bytes;
284 thread[i] = new OSSThread(this);
291 int AudioOSS::sizetofrag(int samples, int channels, int bits)
293 int testfrag = 2, fragsize = 1, grain = 4;
294 samples *= channels * bits / (8 * grain);
295 while(testfrag < samples) {
299 //printf("AudioOSS::sizetofrag %d\n", fragsize);
300 return (grain << 16) | fragsize;
303 int AudioOSS::get_fmt(int bits)
306 case 32: return AFMT_S32_LE; break;
307 case 16: return AFMT_S16_LE; break;
308 case 8: return AFMT_S8; break;
314 int AudioOSS::close_all()
316 //printf("AudioOSS::close_all 1\n");
317 for(int i = 0; i < MAXDEVICES; i++) {
319 ioctl(dsp_in[i], SNDCTL_DSP_RESET, 0);
324 if(dsp_out[i] >= 0) {
325 //printf("AudioOSS::close_all 2\n");
326 ioctl(dsp_out[i], SNDCTL_DSP_RESET, 0);
331 if(thread[i]) delete thread[i];
332 if(data[i]) delete [] data[i];
337 int AudioOSS::set_cloexec_flag(int desc, int value)
339 int oldflags = fcntl (desc, F_GETFD, 0);
340 if (oldflags < 0) return oldflags;
342 oldflags |= FD_CLOEXEC;
344 oldflags &= ~FD_CLOEXEC;
345 return fcntl(desc, F_SETFD, oldflags);
348 int64_t AudioOSS::device_position()
351 if(!ioctl(get_output(0), SNDCTL_DSP_GETOPTR, &info))
353 //printf("AudioOSS::device_position %d %d %d\n", info.bytes, device->get_obits(), device->get_ochannels());
354 // workaround for ALSA OSS emulation driver's bug
355 // the problem is that if the first write to sound device was not full lenght fragment then
356 // _GETOPTR returns insanely large numbers at first moments of play
357 if (info.bytes > 2100000000) return 0;
358 int frame = device->get_ochannels() * device->get_obits()/8;
359 return info.bytes / frame;
361 for (int i = 0; i < MAXDEVICES; i++)
364 return thread[i]->device_position() /
365 device->get_ochannels() /
366 (device->get_obits()/8) +
367 thread[i]->timer->get_scaled_difference(device->out_samplerate);
372 int AudioOSS::interrupt_playback()
374 //printf("AudioOSS::interrupt_playback 1\n");
375 for(int i = 0; i < MAXDEVICES; i++) {
378 thread[i]->input_lock->unlock();
379 thread[i]->write_lock->unlock();
383 //printf("AudioOSS::interrupt_playback 100\n");
387 int AudioOSS::read_buffer(char *buffer, int bytes)
389 int sample_size = device->get_ibits() / 8;
390 int out_frame_size = device->get_ichannels() * sample_size;
391 int samples = bytes / out_frame_size;
393 //printf("AudioOSS::read_buffer 1 %d\n", bytes);
395 for(int i = 0; i < MAXDEVICES; i++) {
397 int in_frame_size = device->get_ichannels() * sample_size;
399 if(data[i] && data_allocated[i] < bytes) {
404 data[i] = new unsigned char[bytes];
405 data_allocated[i] = bytes;
408 thread[i]->read_data(get_input(i), data[i], samples * in_frame_size);
412 //printf("AudioOSS::read_buffer 1 %d\n", device->get_ibits());
413 for(int i = 0, out_channel = 0; i < MAXDEVICES; i++) {
415 thread[i]->wait_read();
417 for(int in_channel = 0;
418 in_channel < device->get_ichannels();
420 int in_frame_size = device->get_ichannels() * sample_size;
422 for(int k = 0; k < samples; k++) {
426 buffer[out_channel * sample_size + k * out_frame_size + l] =
427 data[i][in_channel * sample_size + k * in_frame_size + l];
434 //printf("AudioOSS::read_buffer 2\n");
438 int AudioOSS::write_buffer(char *buffer, int bytes)
440 int sample_size = device->get_obits() / 8;
441 int in_frame_size = device->get_ochannels() * sample_size;
442 int samples = bytes / in_frame_size;
444 for(int i = 0, in_channel = 0; i < MAXDEVICES; i++) {
446 int out_frame_size = device->get_ochannels() * sample_size;
447 if(data[i] && data_allocated[i] < bytes) {
452 data[i] = new unsigned char[bytes];
453 data_allocated[i] = bytes;
456 for(int out_channel = 0;
457 out_channel < device->get_ochannels();
460 for(int k = 0; k < samples; k++) {
461 for(int l = 0; l < sample_size; l++) {
462 data[i][out_channel * sample_size + k * out_frame_size + l] =
463 buffer[in_channel * sample_size + k * in_frame_size + l];
469 thread[i]->write_data(get_output(i), data[i], samples * out_frame_size);
472 for(int i = 0; i < MAXDEVICES; i++) {
473 if( thread[i] ) thread[i]->wait_write();
478 int AudioOSS::flush_device()
480 for(int i = 0; i < MAXDEVICES; i++)
481 if(thread[i]) ioctl(get_output(i), SNDCTL_DSP_SYNC, 0);
485 int AudioOSS::get_output(int number)
487 return device->w ? dsp_out[number] : -1;
490 int AudioOSS::get_input(int number)
492 return device->r ? dsp_in[number] : -1;