centos build workarounds
[goodguy/history.git] / cinelerra-5.0 / cinelerra / audiodevice.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
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.
10  * 
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.
15  * 
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
19  * 
20  */
21
22 #include "audiodevice.h"
23 #include "audio1394.h"
24 #include "audioalsa.h"
25 #include "audiodvb.h"
26 #include "audiov4l2mpeg.h"
27 #include "audioesound.h"
28 #include "audiooss.h"
29 #include "asset.h"
30 #include "bctimer.h"
31 #include "condition.h"
32 #include "dcoffset.h"
33 #include "edl.h"
34 #include "edlsession.h"
35 #include "mainmenu.h"
36 #include "mutex.h"
37 #include "mwindow.h"
38 #include "mwindowgui.h"
39 #include "playbackconfig.h"
40 #include "preferences.h"
41 #include "recordconfig.h"
42 #include "recordgui.h"
43 #include "record.h"
44 #include "sema.h"
45
46
47 AudioLowLevel::AudioLowLevel(AudioDevice *device)
48 {
49         this->device = device;
50 }
51
52 AudioLowLevel::~AudioLowLevel()
53 {
54 }
55
56
57 AudioDevice::AudioDevice(MWindow *mwindow)
58 {
59         initialize();
60         this->mwindow = mwindow;
61         this->out_config = new AudioOutConfig();
62         this->in_config = new AudioInConfig;
63         this->vconfig = new VideoInConfig;
64         device_lock = new Mutex("AudioDevice::device_lock",1);
65         timer_lock = new Mutex("AudioDevice::timer_lock");
66         buffer_lock = new Mutex("AudioDevice::buffer_lock");
67         polling_lock = new Condition(0, "AudioDevice::polling_lock");
68         playback_timer = new Timer;
69         record_timer = new Timer;
70         for(int i = 0; i < TOTAL_AUDIO_BUFFERS; i++) {
71                 output_buffer_t *obfr = &output[i];
72                 obfr->play_lock = new Condition(0, "AudioDevice::play_lock");
73                 obfr->arm_lock = new Condition(1, "AudioDevice::arm_lock");
74         }
75         reset_input();
76         total_samples_read = 0;
77         total_samples_input = 0;
78         reset_output();
79         total_samples_written = 0;
80         total_samples_output = 0;
81 }
82
83 AudioDevice::~AudioDevice()
84 {
85         stop_audio(0);
86         delete out_config;
87         delete in_config;
88         delete vconfig;
89         delete timer_lock;
90         for( int i=0; i < TOTAL_AUDIO_BUFFERS; ++i ) {
91                 output_buffer_t *obfr = &output[i];
92                 delete obfr->play_lock;
93                 delete obfr->arm_lock;
94         }
95         delete playback_timer;
96         delete record_timer;
97         delete buffer_lock;
98         delete polling_lock;
99         delete device_lock;
100 }
101
102 int AudioDevice::initialize()
103 {
104         for(int i = 0; i < TOTAL_AUDIO_BUFFERS; i++) {
105                 input[i].buffer = 0;
106                 output[i].buffer = 0;
107         }
108         audio_in = audio_out = 0;
109         in_bits = out_bits = 0;
110         in_channels = out_channels = 0;
111         in_samples = out_samples = 0;
112         in_samplerate = out_samplerate = 0;
113         in_realtime = out_realtime = 0;
114         lowlevel_out = lowlevel_in = 0;
115         in51_2 = out51_2 = 0;
116         in_config_updated = 0;
117         rec_dither = play_dither = 0;
118         rec_gain = play_gain = 1.;
119         r = w = 0;
120         software_position_info = 0;
121         vdevice = 0;
122         sharing = 0;
123         return 0;
124 }
125
126
127 void AudioThread::initialize()
128 {
129         device = 0;
130         arun = 0;
131         aend = 0;
132         startup_lock = 0;
133         started = 0;
134 }
135
136
137 AudioThread::
138 AudioThread(AudioDevice *device,
139         void (AudioDevice::*arun)(), void (AudioDevice::*aend)())
140  : Thread(1, 0, 0)
141 {
142         initialize();
143         startup_lock = new Condition(0, "AudioThread::startup_lock");
144         this->device = device;
145         this->arun = arun;
146         this->aend = aend;
147         Thread::start();
148 }
149
150 AudioThread::~AudioThread()
151 {
152         delete startup_lock;
153 }
154
155 void AudioThread::stop(int wait)
156 {
157         if( !wait ) (device->*aend)();
158         startup();
159         Thread::join();
160 }
161
162 void AudioThread::run()
163 {
164         startup_lock->lock();
165         (device->*arun)();
166 }
167
168 void AudioThread::startup()
169 {
170         if( !started ) {
171                 started = 1;
172                 startup_lock->unlock();
173         }
174 }
175
176
177 void AudioDevice::stop_input()
178 {
179         device_lock->lock("AudioDevice::stop_input");
180         if( audio_in ) {
181                 audio_in->stop(0);
182                 delete audio_in;
183                 audio_in = 0;
184         }
185         reset_input();
186         device_lock->unlock();
187 }
188
189 void AudioDevice::stop_output(int wait)
190 {
191         if( !wait ) playback_interrupted = 1;
192         device_lock->lock("AudioDevice::stop_output");
193         if( audio_out ) {
194                 if( is_playing_back )
195                         audio_out->stop(wait);
196                 delete audio_out;
197                 audio_out = 0;
198         }
199         reset_output();
200         device_lock->unlock();
201 }
202
203 int AudioDevice::stop_audio(int wait)
204 {
205         if( audio_in ) stop_input();
206         if( audio_out ) stop_output(wait);
207         return 0;
208 }
209
210
211 int AudioDevice::create_lowlevel(AudioLowLevel* &lowlevel, int driver,int in)
212 {
213         *(in ? &this->idriver : &this->odriver) = driver;
214
215         if(!lowlevel) {
216                 switch(driver) {
217 #ifdef HAVE_OSS
218                 case AUDIO_OSS:
219                 case AUDIO_OSS_ENVY24:
220                         lowlevel = new AudioOSS(this);
221                         break;
222 #endif
223
224 #ifdef HAVE_ESOUND
225                 case AUDIO_ESOUND:
226                         lowlevel = new AudioESound(this);
227                         break;
228 #endif
229                 case AUDIO_NAS:
230                         break;
231
232 #ifdef HAVE_ALSA
233                 case AUDIO_ALSA:
234                         lowlevel = new AudioALSA(this);
235                         break;
236 #endif
237
238 #ifdef HAVE_FIREWIRE    
239                 case AUDIO_1394:
240                 case AUDIO_DV1394:
241                 case AUDIO_IEC61883:
242                         lowlevel = new Audio1394(this);
243                         break;
244 #endif
245
246                 case AUDIO_DVB:
247                         lowlevel = new AudioDVB(this);
248                         break;
249
250                 case AUDIO_V4L2MPEG:
251                         lowlevel = new AudioV4L2MPEG(this);
252                         break;
253                 }
254         }
255         return 0;
256 }
257
258 int AudioDevice::open_input(AudioInConfig *config, VideoInConfig *vconfig,
259         int rate, int samples, int channels, int realtime)
260 {
261         device_lock->lock("AudioDevice::open_input");
262         r = 1;
263         this->in_config->copy_from(config);
264         this->vconfig->copy_from(vconfig);
265         in_samplerate = rate;
266         in_samples = samples;
267         in_realtime = realtime;
268         in51_2 = config->map51_2 && channels == 2;
269         if( in51_2 ) channels = 6;
270         in_channels = channels;
271         rec_gain = config->rec_gain;
272         create_lowlevel(lowlevel_in, config->driver, 1);
273         lowlevel_in->open_input();
274         in_config_updated = 0;
275         record_timer->update();
276         device_lock->unlock();
277         return 0;
278 }
279
280 int AudioDevice::open_output(AudioOutConfig *config, 
281         int rate, int samples, int channels, int realtime)
282 {
283         device_lock->lock("AudioDevice::open_output");
284         w = 1;
285         *this->out_config = *config;
286         out_samplerate = rate;
287         out_samples = samples;
288         out51_2 = config->map51_2 && channels == 6;
289         if( out51_2 ) channels = 2;
290         out_channels = channels;
291         out_realtime = realtime;
292         play_gain = config->play_gain;
293         create_lowlevel(lowlevel_out, config->driver,0);
294         int result = lowlevel_out ? lowlevel_out->open_output() : 0;
295         device_lock->unlock();
296         return result;
297 }
298
299 int AudioDevice::open_monitor(AudioOutConfig *config,int mode)
300 {
301         device_lock->lock("AudioDevice::open_output");
302         w = 1;
303         *this->out_config = *config;
304         monitoring = mode;
305         monitor_open = 0;
306         device_lock->unlock();
307         return 0;
308 }
309
310
311
312 int AudioDevice::interrupt_crash()
313 {
314         if( lowlevel_in )
315                 lowlevel_in->interrupt_crash();
316         stop_input();
317         return 0;
318 }
319
320 double AudioDevice::get_itimestamp()
321 {
322         buffer_lock->lock("AudioDevice::get_itimestamp");
323         timer_lock->lock("AudioDevice::get_otimestamp");
324         input_buffer_t *ibfr = &input[input_buffer_out];
325         int frame = get_ichannels() * get_ibits() / 8;
326         int64_t samples = frame ? input_buffer_offset / frame : 0;
327         double timestamp = !in_samplerate || ibfr->timestamp < 0. ? -1. :
328                 ibfr->timestamp + (double) samples / in_samplerate;
329         timer_lock->unlock();
330         buffer_lock->unlock();
331         return timestamp;
332 }
333
334 double AudioDevice::get_otimestamp()
335 {
336         buffer_lock->lock("AudioDevice::get_otimestamp");
337         timer_lock->lock("AudioDevice::get_otimestamp");
338         int64_t unplayed_samples = total_samples_output - current_position();
339         double unplayed_time = !out_samplerate || last_buffer_time < 0. ? -1. :
340                 (double)unplayed_samples / out_samplerate;
341         double timestamp = last_buffer_time - unplayed_time;
342         timer_lock->unlock();
343         buffer_lock->unlock();
344         return timestamp;
345 }
346
347 double AudioDevice::device_timestamp()
348 {
349         return lowlevel_in ? lowlevel_in->device_timestamp() : -1.;
350 }
351
352 int AudioDevice::close_all()
353 {
354         device_lock->lock("AudioDevice::close_all");
355         stop_audio(0);
356         if( lowlevel_in ) {
357                 lowlevel_in->close_all();
358                 delete lowlevel_in;
359                 lowlevel_in = 0;
360         }
361         if( lowlevel_out )
362         {
363                 lowlevel_out->close_all();
364                 delete lowlevel_out;
365                 lowlevel_out = 0;
366         }
367         initialize();
368         device_lock->unlock();
369         return 0;
370 }
371
372 void AudioDevice::auto_update(int channels, int samplerate, int bits)
373 {
374         if( !in_config || !in_config->follow_audio ) return;
375         in_channels = channels;
376         in_samplerate = samplerate;
377         in_bits = bits;
378         in_config_updated = 1;
379         polling_lock->unlock();
380 }
381
382 int AudioDevice::config_updated()
383 {
384         if( !in_config || !in_config->follow_audio ) return 0;
385         return in_config_updated;
386 }
387
388 void AudioDevice::config_update()
389 {
390         in_config_updated = 0;
391         AudioInConfig *aconfig_in = mwindow->edl->session->aconfig_in;
392         in51_2 = aconfig_in->map51_2 && in_channels == 6 ? 1 : 0;
393         int ichannels = in51_2 ? 2 : in_channels;
394         AudioOutConfig *aconfig_out = mwindow->edl->session->playback_config->aconfig;
395         out51_2 = aconfig_out->map51_2 && ichannels == 6 ? 1 : 0;
396         aconfig_in->in_samplerate = in_samplerate;
397         aconfig_in->channels = ichannels;
398         aconfig_in->oss_in_bits = in_bits;
399         aconfig_in->alsa_in_bits = in_bits;
400         total_samples_read = 0;
401         total_samples_input = 0;
402         monitor_open = 0;
403 }
404
405 int AudioDevice::start_toc(const char *path, const char *toc_path)
406 {
407         return lowlevel_in ? lowlevel_in->start_toc(path, toc_path) : -1;
408 }
409
410 int AudioDevice::start_record(int fd, int bsz)
411 {
412         return lowlevel_in ? lowlevel_in->start_record(fd, bsz) : -1;
413 }
414
415 int AudioDevice::stop_record()
416 {
417         return lowlevel_in ? lowlevel_in->stop_record() : -1;
418 }
419
420 int AudioDevice::total_audio_channels()
421 {
422         return lowlevel_in ? lowlevel_in->total_audio_channels() : 0;
423 }
424
425 DeviceMPEGInput *AudioDevice::mpeg_device()
426 {
427         return lowlevel_in ? lowlevel_in->mpeg_device() : 0;
428 }
429