4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "audiodevice.h"
23 #include "audio1394.h"
24 #include "audioalsa.h"
26 #include "audiov4l2mpeg.h"
27 #include "audioesound.h"
31 #include "condition.h"
34 #include "edlsession.h"
38 #include "mwindowgui.h"
39 #include "playbackconfig.h"
40 #include "preferences.h"
41 #include "recordconfig.h"
42 #include "recordgui.h"
47 AudioLowLevel::AudioLowLevel(AudioDevice *device)
49 this->device = device;
52 AudioLowLevel::~AudioLowLevel()
57 AudioDevice::AudioDevice(MWindow *mwindow)
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");
76 total_samples_read = 0;
77 total_samples_input = 0;
79 total_samples_written = 0;
80 total_samples_output = 0;
83 AudioDevice::~AudioDevice()
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;
95 delete playback_timer;
102 int AudioDevice::initialize()
104 for(int i = 0; i < TOTAL_AUDIO_BUFFERS; i++) {
106 output[i].buffer = 0;
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.;
120 software_position_info = 0;
127 void AudioThread::initialize()
138 AudioThread(AudioDevice *device,
139 void (AudioDevice::*arun)(), void (AudioDevice::*aend)())
143 startup_lock = new Condition(0, "AudioThread::startup_lock");
144 this->device = device;
150 AudioThread::~AudioThread()
155 void AudioThread::stop(int wait)
157 if( !wait ) (device->*aend)();
162 void AudioThread::run()
164 startup_lock->lock();
168 void AudioThread::startup()
172 startup_lock->unlock();
177 void AudioDevice::stop_input()
179 device_lock->lock("AudioDevice::stop_input");
186 device_lock->unlock();
189 void AudioDevice::stop_output(int wait)
191 if( !wait ) playback_interrupted = 1;
192 device_lock->lock("AudioDevice::stop_output");
194 if( is_playing_back )
195 audio_out->stop(wait);
200 device_lock->unlock();
203 int AudioDevice::stop_audio(int wait)
205 if( audio_in ) stop_input();
206 if( audio_out ) stop_output(wait);
211 int AudioDevice::create_lowlevel(AudioLowLevel* &lowlevel, int driver,int in)
213 *(in ? &this->idriver : &this->odriver) = driver;
219 case AUDIO_OSS_ENVY24:
220 lowlevel = new AudioOSS(this);
226 lowlevel = new AudioESound(this);
234 lowlevel = new AudioALSA(this);
242 lowlevel = new Audio1394(this);
247 lowlevel = new AudioDVB(this);
251 lowlevel = new AudioV4L2MPEG(this);
258 int AudioDevice::open_input(AudioInConfig *config, VideoInConfig *vconfig,
259 int rate, int samples, int channels, int realtime)
261 device_lock->lock("AudioDevice::open_input");
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();
280 int AudioDevice::open_output(AudioOutConfig *config,
281 int rate, int samples, int channels, int realtime)
283 device_lock->lock("AudioDevice::open_output");
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();
299 int AudioDevice::open_monitor(AudioOutConfig *config,int mode)
301 device_lock->lock("AudioDevice::open_output");
303 *this->out_config = *config;
306 device_lock->unlock();
312 int AudioDevice::interrupt_crash()
315 lowlevel_in->interrupt_crash();
320 double AudioDevice::get_itimestamp()
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();
334 double AudioDevice::get_otimestamp()
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();
347 double AudioDevice::device_timestamp()
349 return lowlevel_in ? lowlevel_in->device_timestamp() : -1.;
352 int AudioDevice::close_all()
354 device_lock->lock("AudioDevice::close_all");
357 lowlevel_in->close_all();
363 lowlevel_out->close_all();
368 device_lock->unlock();
372 void AudioDevice::auto_update(int channels, int samplerate, int bits)
374 if( !in_config || !in_config->follow_audio ) return;
375 in_channels = channels;
376 in_samplerate = samplerate;
378 in_config_updated = 1;
379 polling_lock->unlock();
382 int AudioDevice::config_updated()
384 if( !in_config || !in_config->follow_audio ) return 0;
385 return in_config_updated;
388 void AudioDevice::config_update()
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;
405 int AudioDevice::start_toc(const char *path, const char *toc_path)
407 return lowlevel_in ? lowlevel_in->start_toc(path, toc_path) : -1;
410 int AudioDevice::start_record(int fd, int bsz)
412 return lowlevel_in ? lowlevel_in->start_record(fd, bsz) : -1;
415 int AudioDevice::stop_record()
417 return lowlevel_in ? lowlevel_in->stop_record() : -1;
420 int AudioDevice::total_audio_channels()
422 return lowlevel_in ? lowlevel_in->total_audio_channels() : 0;
425 DeviceMPEGInput *AudioDevice::mpeg_device()
427 return lowlevel_in ? lowlevel_in->mpeg_device() : 0;