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()
157 void AudioThread::stop(int wait)
159 if( !wait ) (device->*aend)();
164 void AudioThread::run()
166 startup_lock->lock();
170 void AudioThread::startup()
174 startup_lock->unlock();
179 void AudioDevice::stop_input()
181 device_lock->lock("AudioDevice::stop_input");
188 device_lock->unlock();
191 void AudioDevice::stop_output(int wait)
193 if( !wait ) playback_interrupted = 1;
194 device_lock->lock("AudioDevice::stop_output");
196 if( is_playing_back )
197 audio_out->stop(wait);
202 device_lock->unlock();
205 int AudioDevice::stop_audio(int wait)
207 if( audio_in ) stop_input();
208 if( audio_out ) stop_output(wait);
213 int AudioDevice::create_lowlevel(AudioLowLevel* &lowlevel, int driver,int in)
215 *(in ? &this->idriver : &this->odriver) = driver;
221 case AUDIO_OSS_ENVY24:
222 lowlevel = new AudioOSS(this);
228 lowlevel = new AudioESound(this);
236 lowlevel = new AudioALSA(this);
244 lowlevel = new Audio1394(this);
249 lowlevel = new AudioDVB(this);
253 lowlevel = new AudioV4L2MPEG(this);
260 int AudioDevice::open_input(AudioInConfig *config, VideoInConfig *vconfig,
261 int rate, int samples, int channels, int realtime)
263 device_lock->lock("AudioDevice::open_input");
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();
282 int AudioDevice::open_output(AudioOutConfig *config,
283 int rate, int samples, int channels, int realtime)
285 device_lock->lock("AudioDevice::open_output");
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();
301 int AudioDevice::open_monitor(AudioOutConfig *config,int mode)
303 device_lock->lock("AudioDevice::open_output");
305 *this->out_config = *config;
308 device_lock->unlock();
314 int AudioDevice::interrupt_crash()
317 lowlevel_in->interrupt_crash();
322 double AudioDevice::get_itimestamp()
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();
336 double AudioDevice::get_otimestamp()
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();
349 double AudioDevice::device_timestamp()
351 return lowlevel_in ? lowlevel_in->device_timestamp() : -1.;
354 int AudioDevice::close_all()
356 device_lock->lock("AudioDevice::close_all");
359 lowlevel_in->close_all();
365 lowlevel_out->close_all();
370 device_lock->unlock();
374 void AudioDevice::auto_update(int channels, int samplerate, int bits)
376 if( !in_config || !in_config->follow_audio ) return;
377 in_channels = channels;
378 in_samplerate = samplerate;
380 in_config_updated = 1;
381 polling_lock->unlock();
384 int AudioDevice::config_updated()
386 if( !in_config || !in_config->follow_audio ) return 0;
387 return in_config_updated;
390 void AudioDevice::config_update()
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;
407 int AudioDevice::start_toc(const char *path, const char *toc_path)
409 return lowlevel_in ? lowlevel_in->start_toc(path, toc_path) : -1;
412 int AudioDevice::start_record(int fd, int bsz)
414 return lowlevel_in ? lowlevel_in->start_record(fd, bsz) : -1;
417 int AudioDevice::stop_record()
419 return lowlevel_in ? lowlevel_in->stop_record() : -1;
422 int AudioDevice::total_audio_channels()
424 return lowlevel_in ? lowlevel_in->total_audio_channels() : 0;
427 DeviceMPEGInput *AudioDevice::mpeg_device()
429 return lowlevel_in ? lowlevel_in->mpeg_device() : 0;