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"
24 #include "audio1394.h"
26 #include "audioalsa.h"
28 #include "audiov4l2mpeg.h"
29 #include "audioesound.h"
33 #include "condition.h"
36 #include "edlsession.h"
40 #include "mwindowgui.h"
41 #include "playbackconfig.h"
42 #include "preferences.h"
43 #include "recordconfig.h"
44 #include "recordgui.h"
49 AudioLowLevel::AudioLowLevel(AudioDevice *device)
51 this->device = device;
54 AudioLowLevel::~AudioLowLevel()
59 AudioDevice::AudioDevice(MWindow *mwindow)
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");
78 total_samples_read = 0;
79 total_samples_input = 0;
81 total_samples_written = 0;
82 total_samples_output = 0;
85 AudioDevice::~AudioDevice()
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;
97 delete playback_timer;
104 int AudioDevice::initialize()
106 for(int i = 0; i < TOTAL_AUDIO_BUFFERS; i++) {
108 output[i].buffer = 0;
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.;
122 software_position_info = 0;
129 void AudioThread::initialize()
140 AudioThread(AudioDevice *device,
141 void (AudioDevice::*arun)(), void (AudioDevice::*aend)())
145 startup_lock = new Condition(0, "AudioThread::startup_lock");
146 this->device = device;
152 AudioThread::~AudioThread()
158 void AudioThread::stop(int wait)
160 if( !wait ) (device->*aend)();
165 void AudioThread::run()
167 startup_lock->lock();
171 void AudioThread::startup()
175 startup_lock->unlock();
180 void AudioDevice::stop_input()
182 device_lock->lock("AudioDevice::stop_input");
189 device_lock->unlock();
192 void AudioDevice::stop_output(int wait)
194 if( !wait ) playback_interrupted = 1;
195 device_lock->lock("AudioDevice::stop_output");
197 if( is_playing_back )
198 audio_out->stop(wait);
203 device_lock->unlock();
206 int AudioDevice::stop_audio(int wait)
208 if( audio_in ) stop_input();
209 if( audio_out ) stop_output(wait);
214 int AudioDevice::create_lowlevel(AudioLowLevel* &lowlevel, int driver,int in)
216 *(in ? &this->idriver : &this->odriver) = driver;
222 case AUDIO_OSS_ENVY24:
223 lowlevel = new AudioOSS(this);
229 lowlevel = new AudioESound(this);
237 lowlevel = new AudioALSA(this);
245 lowlevel = new Audio1394(this);
251 lowlevel = new AudioDVB(this);
255 #ifdef HAVE_VIDEO4LINUX2
257 lowlevel = new AudioV4L2MPEG(this);
265 int AudioDevice::open_input(AudioInConfig *config, VideoInConfig *vconfig,
266 int rate, int samples, int channels, int realtime)
268 device_lock->lock("AudioDevice::open_input");
270 this->in_config->copy_from(config);
271 this->vconfig->copy_from(vconfig);
272 in_samplerate = rate;
273 in_samples = samples;
274 in_realtime = realtime;
275 in51_2 = config->map51_2 && channels == 2;
276 if( in51_2 ) channels = 6;
277 in_channels = channels;
278 rec_gain = config->rec_gain;
279 create_lowlevel(lowlevel_in, config->driver, 1);
280 lowlevel_in->open_input();
281 in_config_updated = 0;
282 record_timer->update();
283 device_lock->unlock();
287 int AudioDevice::open_output(AudioOutConfig *config,
288 int rate, int samples, int channels, int realtime)
290 device_lock->lock("AudioDevice::open_output");
292 *this->out_config = *config;
293 out_samplerate = rate;
294 out_samples = samples;
295 out51_2 = config->map51_2 && channels == 6;
296 if( out51_2 ) channels = 2;
297 out_channels = channels;
298 out_realtime = realtime;
299 play_gain = config->play_gain;
300 create_lowlevel(lowlevel_out, config->driver,0);
301 int result = lowlevel_out ? lowlevel_out->open_output() : 0;
302 device_lock->unlock();
306 int AudioDevice::open_monitor(AudioOutConfig *config,int mode)
308 device_lock->lock("AudioDevice::open_output");
310 *this->out_config = *config;
313 device_lock->unlock();
319 int AudioDevice::interrupt_crash()
322 lowlevel_in->interrupt_crash();
327 double AudioDevice::get_itimestamp()
329 buffer_lock->lock("AudioDevice::get_itimestamp");
330 timer_lock->lock("AudioDevice::get_otimestamp");
331 input_buffer_t *ibfr = &input[input_buffer_out];
332 int frame = get_ichannels() * get_ibits() / 8;
333 int64_t samples = frame ? input_buffer_offset / frame : 0;
334 double timestamp = !in_samplerate || ibfr->timestamp < 0. ? -1. :
335 ibfr->timestamp + (double) samples / in_samplerate;
336 timer_lock->unlock();
337 buffer_lock->unlock();
341 double AudioDevice::get_otimestamp()
343 buffer_lock->lock("AudioDevice::get_otimestamp");
344 timer_lock->lock("AudioDevice::get_otimestamp");
345 int64_t unplayed_samples = total_samples_output - current_position();
346 double unplayed_time = !out_samplerate || last_buffer_time < 0. ? -1. :
347 (double)unplayed_samples / out_samplerate;
348 double timestamp = last_buffer_time - unplayed_time;
349 timer_lock->unlock();
350 buffer_lock->unlock();
354 double AudioDevice::device_timestamp()
356 return lowlevel_in ? lowlevel_in->device_timestamp() : -1.;
359 int AudioDevice::close_all()
361 device_lock->lock("AudioDevice::close_all");
364 lowlevel_in->close_all();
370 lowlevel_out->close_all();
375 device_lock->unlock();
379 void AudioDevice::auto_update(int channels, int samplerate, int bits)
381 if( !in_config || !in_config->follow_audio ) return;
382 in_channels = channels;
383 in_samplerate = samplerate;
385 in_config_updated = 1;
386 polling_lock->unlock();
389 int AudioDevice::config_updated()
391 if( !in_config || !in_config->follow_audio ) return 0;
392 return in_config_updated;
395 void AudioDevice::config_update()
397 in_config_updated = 0;
398 AudioInConfig *aconfig_in = mwindow->edl->session->aconfig_in;
399 in51_2 = aconfig_in->map51_2 && in_channels == 6 ? 1 : 0;
400 int ichannels = in51_2 ? 2 : in_channels;
401 AudioOutConfig *aconfig_out = mwindow->edl->session->playback_config->aconfig;
402 out51_2 = aconfig_out->map51_2 && ichannels == 6 ? 1 : 0;
403 aconfig_in->in_samplerate = in_samplerate;
404 aconfig_in->channels = ichannels;
405 aconfig_in->oss_in_bits = in_bits;
406 aconfig_in->alsa_in_bits = in_bits;
407 total_samples_read = 0;
408 total_samples_input = 0;
412 int AudioDevice::start_toc(const char *path, const char *toc_path)
414 return lowlevel_in ? lowlevel_in->start_toc(path, toc_path) : -1;
417 int AudioDevice::start_record(int fd, int bsz)
419 return lowlevel_in ? lowlevel_in->start_record(fd, bsz) : -1;
422 int AudioDevice::stop_record()
424 return lowlevel_in ? lowlevel_in->stop_record() : -1;
427 int AudioDevice::total_audio_channels()
429 return lowlevel_in ? lowlevel_in->total_audio_channels() : 0;
432 DeviceMPEGInput *AudioDevice::mpeg_device()
434 return lowlevel_in ? lowlevel_in->mpeg_device() : 0;