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