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