r1006: configure: Use libx264_pic instead of libx264 if available.
[cinelerra_cv/mob.git] / cinelerra / audiooss.C
blobfed8db7e2f52f4de653da835ac61471dfdf263b7
1 #include "audioconfig.h"
2 #include "audiodevice.h"
3 #include "audiooss.h"
4 #include "clip.h"
5 #include "condition.h"
6 #include "errno.h"
7 #include "playbackconfig.h"
8 #include "preferences.h"
9 #include "recordconfig.h"
11 #include <string.h>
13 #ifdef HAVE_OSS
15 // These are only available in commercial OSS
17 #ifndef AFMT_S32_LE
18 #define AFMT_S32_LE      0x00001000
19 #define AFMT_S32_BE      0x00002000
20 #endif
23 // Synchronie multiple devices by using threads
25 OSSThread::OSSThread(AudioOSS *device)
26  : Thread(1, 0, 0)
28         rd = 0;
29         wr = 0;
30         done = 0;
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()
40         done = 1;
41         input_lock->unlock();
42         Thread::join();
43         delete input_lock;
44         delete output_lock;
45         delete read_lock;
46         delete write_lock;
49 void OSSThread::run()
51         while(!done)
52         {
53                 input_lock->lock("OSSThread::run 1");
54                 if(rd)
55                 {
56                         int result = read(fd, data, bytes);
57                         read_lock->unlock();
58                 }
59                 else
60                 if(wr)
61                 {
62                         if(done) return;
65                         Thread::enable_cancel();
66                         write(fd, data, bytes);
67                         Thread::disable_cancel();
70                         if(done) return;
71                         write_lock->unlock();
72                 }
73                 output_lock->unlock();
74         }
77 void OSSThread::write_data(int fd, unsigned char *data, int bytes)
79         output_lock->lock("OSSThread::write_data");
80         wr = 1;
81         rd = 0;
82         this->data = data;
83         this->bytes = bytes;
84         this->fd = fd;
85         input_lock->unlock();
88 void OSSThread::read_data(int fd, unsigned char *data, int bytes)
90         output_lock->lock("OSSThread::read_data");
91         wr = 0;
92         rd = 1;
93         this->data = data;
94         this->bytes = bytes;
95         this->fd = fd;
96         input_lock->unlock();
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++)
123         {
124                 dsp_in[i] = dsp_out[i] = dsp_duplex[i] = 0;
125                 thread[i] = 0;
126                 data[i] = 0;
127                 data_allocated[i] = 0;
128         }
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++)
142         {
143                 if(device->in_config->oss_enable[i])
144                 {
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], 
150                                 strerror(errno));
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);
174                         thread[i]->start();
175                 }
176         }
177         return 0;
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++)
187         {
188                 if(device->out_config->oss_enable[i])
189                 {
190 // Linux 2.4.18 no longer supports allocating the maximum buffer size.
191 // Need the shrink fragment size in preferences until it works.
192                         dsp_out[i] = 
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);
217                         thread[i]->start();
218                 }
219         }
220         return 0;
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++)
229         {
230                 if(device->out_config->oss_enable[i])
231                 {
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);
254                         thread[i]->start();
255                 }
256         }
257         return 0;
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)
265         {
266                 fragsize++;
267                 testfrag *= 2;
268         }
269 //printf("AudioOSS::sizetofrag %d\n", fragsize);
270         return (4 << 16) | fragsize;
273 int AudioOSS::get_fmt(int bits)
275         switch(bits)
276         {
277                 case 32: return AFMT_S32_LE; break;
278                 case 16: return AFMT_S16_LE; break;
279                 case 8:  return AFMT_S8;  break;
280         }
281         return AFMT_S16_LE;
285 int AudioOSS::close_all()
287 //printf("AudioOSS::close_all 1\n");
288         for(int i = 0; i < MAXDEVICES; i++)
289         {
290                 if(dsp_in[i]) 
291                 {
292                         ioctl(dsp_in[i], SNDCTL_DSP_RESET, 0);         
293                         close(dsp_in[i]);      
294                 }
296                 if(dsp_out[i]) 
297                 {
298 //printf("AudioOSS::close_all 2\n");
299                         ioctl(dsp_out[i], SNDCTL_DSP_RESET, 0);        
300                         close(dsp_out[i]);     
301                 }
303                 if(dsp_duplex[i]) 
304                 {
305                         ioctl(dsp_duplex[i], SNDCTL_DSP_RESET, 0);     
306                         close(dsp_duplex[i]);  
307                 }
308                 
309                 if(thread[i]) delete thread[i];
310                 if(data[i]) delete [] data[i];
311         }
312         return 0;
315 int AudioOSS::set_cloexec_flag(int desc, int value)
317         int oldflags = fcntl (desc, F_GETFD, 0);
318         if (oldflags < 0) return oldflags;
319         if(value != 0) 
320                 oldflags |= FD_CLOEXEC;
321         else
322                 oldflags &= ~FD_CLOEXEC;
323         return fcntl(desc, F_SETFD, oldflags);
326 int64_t AudioOSS::device_position()
328         count_info info;
329         if(!ioctl(get_output(0), SNDCTL_DSP_GETOPTR, &info))
330         {
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) 
336                         return 0;
337                 else
338                         return info.bytes / 
339                                 (device->get_obits() / 8) / 
340                                 device->get_ochannels();
341         }
342         return 0;
345 int AudioOSS::interrupt_playback()
347 //printf("AudioOSS::interrupt_playback 1\n");
348         for(int i = 0; i < MAXDEVICES; i++)
349         {
350                 if(thread[i])
351                 {
352                         thread[i]->cancel();
353                         thread[i]->write_lock->unlock();
354                 }
355         }
356 //printf("AudioOSS::interrupt_playback 100\n");
357         return 0;
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);
367 // Fill temp buffers
368         for(int i = 0; i < MAXDEVICES; i++)
369         {
370                 if(thread[i])
371                 {
372                         int in_frame_size = device->get_ichannels() * sample_size;
374                         if(data[i] && data_allocated[i] < bytes)
375                         {
376                                 delete [] data[i];
377                                 data[i] = 0;
378                         }
379                         if(!data[i])
380                         {
381                                 data[i] = new unsigned char[bytes];
382                                 data_allocated[i] = bytes;
383                         }
385                         thread[i]->read_data(get_input(i), data[i], samples * in_frame_size);
386                 }
387         }
389 //printf("AudioOSS::read_buffer 1 %d\n", device->get_ibits());
390         for(int i = 0, out_channel = 0; i < MAXDEVICES; i++)
391         {
392                 if(thread[i])
393                 {
394                         thread[i]->wait_read();
396                         for(int in_channel = 0; 
397                                 in_channel < device->get_ichannels(); 
398                                 in_channel++)
399                         {
400                                 int in_frame_size = device->get_ichannels() * sample_size;
402                                 for(int k = 0; k < samples; k++)
403                                 {
404                                         for(int l = 0; 
405                                                 l < sample_size;
406                                                 l++)
407                                         {
408                                                 buffer[out_channel * sample_size + k * out_frame_size + l] = 
409                                                         data[i][in_channel * sample_size + k * in_frame_size + l];
410                                         }
411                                 }
412                                 out_channel++;
413                         }
414                 }
415         }
416 //printf("AudioOSS::read_buffer 2\n");
417         return 0;
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++)
427         {
428                 if(thread[i])
429                 {
430                         int out_frame_size = device->get_ochannels() * sample_size;
431                         if(data[i] && data_allocated[i] < bytes)
432                         {
433                                 delete [] data[i];
434                                 data[i] = 0;
435                         }
436                         if(!data[i])
437                         {
438                                 data[i] = new unsigned char[bytes];
439                                 data_allocated[i] = bytes;
440                         }
441                         
442                         for(int out_channel = 0;
443                                 out_channel < device->get_ochannels();
444                                 out_channel++)
445                         {
446                                 
447                                 for(int k = 0; k < samples; k++)
448                                 {
449                                         for(int l = 0; l < sample_size; l++)
450                                         {
451                                                 data[i][out_channel * sample_size + k * out_frame_size + l] = 
452                                                         buffer[in_channel * sample_size + k * in_frame_size + l];
453                                         }
454                                 }
455                                 in_channel++;
456                         }
457                         
458                         thread[i]->write_data(get_output(i), data[i], samples * out_frame_size);
459                 }
460         }
461         for(int i = 0, in_channel = 0; i < MAXDEVICES; i++)
462         {
463                 if(thread[i])
464                 {
465                         thread[i]->wait_write();
466                 }
467         }
468         return 0;
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);
475         return 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];
482         return 0;
485 int AudioOSS::get_input(int number)
487         if(device->r) return dsp_in[number];
488         else if(device->d) return dsp_duplex[number];
489         return 0;
492 #endif // HAVE_OSS