1 #include "audioconfig.h"
2 #include "audiodevice.h"
7 #include "playbackconfig.h"
8 #include "preferences.h"
9 #include "recordconfig.h"
15 // These are only available in commercial OSS
18 #define AFMT_S32_LE 0x00001000
19 #define AFMT_S32_BE 0x00002000
23 // Synchronie multiple devices by using threads
25 OSSThread::OSSThread(AudioOSS *device)
31 this->device = device;
32 input_lock = new Condition(0, "OSSThread::input_lock");
33 output_lock = new Condition(1, "OSSThread::output_lock");
34 read_lock = new Condition(0, "OSSThread::read_lock");
35 write_lock = new Condition(0, "OSSThread::write_lock");
38 OSSThread::~OSSThread()
53 input_lock->lock("OSSThread::run 1");
56 int result = read(fd, data, bytes);
65 Thread::enable_cancel();
66 write(fd, data, bytes);
67 Thread::disable_cancel();
73 output_lock->unlock();
77 void OSSThread::write_data(int fd, unsigned char *data, int bytes)
79 output_lock->lock("OSSThread::write_data");
88 void OSSThread::read_data(int fd, unsigned char *data, int bytes)
90 output_lock->lock("OSSThread::read_data");
99 void OSSThread::wait_read()
101 read_lock->lock("OSSThread::wait_read");
104 void OSSThread::wait_write()
106 write_lock->lock("OSSThread::wait_write");
119 AudioOSS::AudioOSS(AudioDevice *device)
120 : AudioLowLevel(device)
122 for(int i = 0; i < MAXDEVICES; i++)
124 dsp_in[i] = dsp_out[i] = dsp_duplex[i] = 0;
127 data_allocated[i] = 0;
131 AudioOSS::~AudioOSS()
135 int AudioOSS::open_input()
137 device->in_bits = device->in_config->oss_in_bits;
138 // 24 bits not available in OSS
139 if(device->in_bits == 24) device->in_bits = 32;
141 for(int i = 0; i < MAXDEVICES; i++)
143 if(device->in_config->oss_enable[i])
145 //printf("AudioOSS::open_input 10\n");
146 dsp_in[i] = open(device->in_config->oss_in_device[i], O_RDONLY/* | O_NDELAY*/);
147 //printf("AudioOSS::open_input 20\n");
148 if(dsp_in[i] < 0) fprintf(stderr, "AudioOSS::open_input %s: %s\n",
149 device->in_config->oss_in_device[i],
152 int format = get_fmt(device->in_config->oss_in_bits);
153 int buffer_info = sizetofrag(device->in_samples,
154 device->get_ichannels(),
155 device->in_config->oss_in_bits);
157 set_cloexec_flag(dsp_in[i], 1);
159 // For the ice1712 the buffer must be maximum or no space will be allocated.
160 if(device->driver == AUDIO_OSS_ENVY24) buffer_info = 0x7fff000f;
161 if(ioctl(dsp_in[i], SNDCTL_DSP_SETFRAGMENT, &buffer_info)) printf("SNDCTL_DSP_SETFRAGMENT failed.\n");
162 if(ioctl(dsp_in[i], SNDCTL_DSP_SETFMT, &format) < 0) printf("SNDCTL_DSP_SETFMT failed\n");
163 int channels = device->get_ichannels();
164 if(ioctl(dsp_in[i], SNDCTL_DSP_CHANNELS, &channels) < 0) printf("SNDCTL_DSP_CHANNELS failed\n");
165 if(ioctl(dsp_in[i], SNDCTL_DSP_SPEED, &device->in_samplerate) < 0) printf("SNDCTL_DSP_SPEED failed\n");
167 audio_buf_info recinfo;
168 ioctl(dsp_in[i], SNDCTL_DSP_GETISPACE, &recinfo);
170 //printf("AudioOSS::open_input fragments=%d fragstotal=%d fragsize=%d bytes=%d\n",
171 // recinfo.fragments, recinfo.fragstotal, recinfo.fragsize, recinfo.bytes);
173 thread[i] = new OSSThread(this);
180 int AudioOSS::open_output()
182 device->out_bits = device->out_config->oss_out_bits;
183 // OSS only supports 8, 16, and 32
184 if(device->out_bits == 24) device->out_bits = 32;
186 for(int i = 0; i < MAXDEVICES; i++)
188 if(device->out_config->oss_enable[i])
190 // Linux 2.4.18 no longer supports allocating the maximum buffer size.
191 // Need the shrink fragment size in preferences until it works.
193 open(device->out_config->oss_out_device[i],
194 O_WRONLY /*| O_NDELAY*/);
195 if(dsp_out[i] < 0) perror("AudioOSS::open_output");
197 int format = get_fmt(device->out_config->oss_out_bits);
198 int buffer_info = sizetofrag(device->out_samples,
199 device->get_ochannels(),
200 device->out_config->oss_out_bits);
201 audio_buf_info playinfo;
203 set_cloexec_flag(dsp_out[i], 1);
205 // For the ice1712 the buffer must be maximum or no space will be allocated.
206 if(device->driver == AUDIO_OSS_ENVY24) buffer_info = 0x7fff000f;
207 if(ioctl(dsp_out[i], SNDCTL_DSP_SETFRAGMENT, &buffer_info)) printf("SNDCTL_DSP_SETFRAGMENT 2 failed.\n");
208 if(ioctl(dsp_out[i], SNDCTL_DSP_SETFMT, &format) < 0) printf("SNDCTL_DSP_SETFMT 2 failed\n");
209 int channels = device->get_ochannels();
210 if(ioctl(dsp_out[i], SNDCTL_DSP_CHANNELS, &channels) < 0) printf("SNDCTL_DSP_CHANNELS 2 failed\n");
211 if(ioctl(dsp_out[i], SNDCTL_DSP_SPEED, &device->out_samplerate) < 0) printf("SNDCTL_DSP_SPEED 2 failed\n");
212 ioctl(dsp_out[i], SNDCTL_DSP_GETOSPACE, &playinfo);
213 // printf("AudioOSS::open_output fragments=%d fragstotal=%d fragsize=%d bytes=%d\n",
214 // playinfo.fragments, playinfo.fragstotal, playinfo.fragsize, playinfo.bytes);
215 device->device_buffer = playinfo.bytes;
216 thread[i] = new OSSThread(this);
223 int AudioOSS::open_duplex()
225 device->duplex_bits = device->out_config->oss_out_bits;
226 if(device->duplex_bits == 24) device->duplex_bits = 32;
228 for(int i = 0; i < MAXDEVICES; i++)
230 if(device->out_config->oss_enable[i])
232 dsp_duplex[i] = open(device->out_config->oss_out_device[i], O_RDWR/* | O_NDELAY*/);
233 if(dsp_duplex[i] < 0) perror("AudioOSS::open_duplex");
235 int format = get_fmt(device->out_config->oss_out_bits);
236 int buffer_info = sizetofrag(device->duplex_samples,
237 device->get_ochannels(),
238 device->out_config->oss_out_bits);
239 audio_buf_info playinfo;
241 set_cloexec_flag(dsp_duplex[i], 1);
243 // For the ice1712 the buffer must be maximum or no space will be allocated.
244 if(device->driver == AUDIO_OSS_ENVY24) buffer_info = 0x7fff000f;
245 if(ioctl(dsp_duplex[i], SNDCTL_DSP_SETFRAGMENT, &buffer_info)) printf("SNDCTL_DSP_SETFRAGMENT failed.\n");
246 if(ioctl(dsp_duplex[i], SNDCTL_DSP_SETDUPLEX, 1) == -1) printf("SNDCTL_DSP_SETDUPLEX failed\n");
247 if(ioctl(dsp_duplex[i], SNDCTL_DSP_SETFMT, &format) < 0) printf("SNDCTL_DSP_SETFMT failed\n");
248 int channels = device->get_ochannels();
249 if(ioctl(dsp_duplex[i], SNDCTL_DSP_CHANNELS, &channels) < 0) printf("SNDCTL_DSP_CHANNELS failed\n");
250 if(ioctl(dsp_duplex[i], SNDCTL_DSP_SPEED, &device->duplex_samplerate) < 0) printf("SNDCTL_DSP_SPEED failed\n");
251 ioctl(dsp_duplex[i], SNDCTL_DSP_GETOSPACE, &playinfo);
252 device->device_buffer = playinfo.bytes;
253 thread[i] = new OSSThread(this);
260 int AudioOSS::sizetofrag(int samples, int channels, int bits)
262 int testfrag = 2, fragsize = 1;
263 samples *= channels * bits / 8;
264 while(testfrag < samples)
269 //printf("AudioOSS::sizetofrag %d\n", fragsize);
270 return (4 << 16) | fragsize;
273 int AudioOSS::get_fmt(int bits)
277 case 32: return AFMT_S32_LE; break;
278 case 16: return AFMT_S16_LE; break;
279 case 8: return AFMT_S8; break;
285 int AudioOSS::close_all()
287 //printf("AudioOSS::close_all 1\n");
288 for(int i = 0; i < MAXDEVICES; i++)
292 ioctl(dsp_in[i], SNDCTL_DSP_RESET, 0);
298 //printf("AudioOSS::close_all 2\n");
299 ioctl(dsp_out[i], SNDCTL_DSP_RESET, 0);
305 ioctl(dsp_duplex[i], SNDCTL_DSP_RESET, 0);
306 close(dsp_duplex[i]);
309 if(thread[i]) delete thread[i];
310 if(data[i]) delete [] data[i];
315 int AudioOSS::set_cloexec_flag(int desc, int value)
317 int oldflags = fcntl (desc, F_GETFD, 0);
318 if (oldflags < 0) return oldflags;
320 oldflags |= FD_CLOEXEC;
322 oldflags &= ~FD_CLOEXEC;
323 return fcntl(desc, F_SETFD, oldflags);
326 int64_t AudioOSS::device_position()
329 if(!ioctl(get_output(0), SNDCTL_DSP_GETOPTR, &info))
331 //printf("AudioOSS::device_position %d %d %d\n", info.bytes, device->get_obits(), device->get_ochannels());
332 // workaround for ALSA OSS emulation driver's bug
333 // the problem is that if the first write to sound device was not full lenght fragment then
334 // _GETOPTR returns insanely large numbers at first moments of play
335 if (info.bytes > 2100000000)
339 (device->get_obits() / 8) /
340 device->get_ochannels();
345 int AudioOSS::interrupt_playback()
347 //printf("AudioOSS::interrupt_playback 1\n");
348 for(int i = 0; i < MAXDEVICES; i++)
353 thread[i]->write_lock->unlock();
356 //printf("AudioOSS::interrupt_playback 100\n");
360 int AudioOSS::read_buffer(char *buffer, int bytes)
362 int sample_size = device->get_ibits() / 8;
363 int out_frame_size = device->get_ichannels() * sample_size;
364 int samples = bytes / out_frame_size;
366 //printf("AudioOSS::read_buffer 1 %d\n", bytes);
368 for(int i = 0; i < MAXDEVICES; i++)
372 int in_frame_size = device->get_ichannels() * sample_size;
374 if(data[i] && data_allocated[i] < bytes)
381 data[i] = new unsigned char[bytes];
382 data_allocated[i] = bytes;
385 thread[i]->read_data(get_input(i), data[i], samples * in_frame_size);
389 //printf("AudioOSS::read_buffer 1 %d\n", device->get_ibits());
390 for(int i = 0, out_channel = 0; i < MAXDEVICES; i++)
394 thread[i]->wait_read();
396 for(int in_channel = 0;
397 in_channel < device->get_ichannels();
400 int in_frame_size = device->get_ichannels() * sample_size;
402 for(int k = 0; k < samples; k++)
408 buffer[out_channel * sample_size + k * out_frame_size + l] =
409 data[i][in_channel * sample_size + k * in_frame_size + l];
416 //printf("AudioOSS::read_buffer 2\n");
420 int AudioOSS::write_buffer(char *buffer, int bytes)
422 int sample_size = device->get_obits() / 8;
423 int in_frame_size = device->get_ochannels() * sample_size;
424 int samples = bytes / in_frame_size;
426 for(int i = 0, in_channel = 0; i < MAXDEVICES; i++)
430 int out_frame_size = device->get_ochannels() * sample_size;
431 if(data[i] && data_allocated[i] < bytes)
438 data[i] = new unsigned char[bytes];
439 data_allocated[i] = bytes;
442 for(int out_channel = 0;
443 out_channel < device->get_ochannels();
447 for(int k = 0; k < samples; k++)
449 for(int l = 0; l < sample_size; l++)
451 data[i][out_channel * sample_size + k * out_frame_size + l] =
452 buffer[in_channel * sample_size + k * in_frame_size + l];
458 thread[i]->write_data(get_output(i), data[i], samples * out_frame_size);
461 for(int i = 0, in_channel = 0; i < MAXDEVICES; i++)
465 thread[i]->wait_write();
471 int AudioOSS::flush_device()
473 for(int i = 0; i < MAXDEVICES; i++)
474 if(thread[i]) ioctl(get_output(i), SNDCTL_DSP_SYNC, 0);
478 int AudioOSS::get_output(int number)
480 if(device->w) return dsp_out[number];
481 else if(device->d) return dsp_duplex[number];
485 int AudioOSS::get_input(int number)
487 if(device->r) return dsp_in[number];
488 else if(device->d) return dsp_duplex[number];
496 // c-file-style: "linux"