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 "audioesound.h"
30 #include "audiopulse.h"
31 #include "audiov4l2mpeg.h"
34 #include "condition.h"
37 #include "edlsession.h"
41 #include "mwindowgui.h"
42 #include "playbackconfig.h"
43 #include "preferences.h"
44 #include "recordconfig.h"
45 #include "recordgui.h"
50 AudioLowLevel::AudioLowLevel(AudioDevice *device)
52 this->device = device;
55 AudioLowLevel::~AudioLowLevel()
60 AudioDevice::AudioDevice(MWindow *mwindow)
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");
79 total_samples_read = 0;
80 total_samples_input = 0;
82 total_samples_written = 0;
83 total_samples_output = 0;
86 AudioDevice::~AudioDevice()
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;
98 delete playback_timer;
105 int AudioDevice::initialize()
107 for(int i = 0; i < TOTAL_AUDIO_BUFFERS; i++) {
109 output[i].buffer = 0;
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.;
123 software_position_info = 0;
130 void AudioThread::initialize()
141 AudioThread(AudioDevice *device,
142 void (AudioDevice::*arun)(), void (AudioDevice::*aend)())
146 startup_lock = new Condition(0, "AudioThread::startup_lock");
147 this->device = device;
153 AudioThread::~AudioThread()
159 void AudioThread::stop(int wait)
161 if( !wait ) (device->*aend)();
166 void AudioThread::run()
168 startup_lock->lock();
172 void AudioThread::startup()
176 startup_lock->unlock();
181 void AudioDevice::stop_input()
183 device_lock->lock("AudioDevice::stop_input");
190 device_lock->unlock();
193 void AudioDevice::stop_output(int wait)
195 if( !wait ) playback_interrupted = 1;
196 device_lock->lock("AudioDevice::stop_output");
198 if( is_playing_back )
199 audio_out->stop(wait);
204 device_lock->unlock();
207 int AudioDevice::stop_audio(int wait)
209 if( audio_in ) stop_input();
210 if( audio_out ) stop_output(wait);
215 int AudioDevice::create_lowlevel(AudioLowLevel* &lowlevel, int driver,int in)
217 *(in ? &this->idriver : &this->odriver) = driver;
223 case AUDIO_OSS_ENVY24:
224 lowlevel = new AudioOSS(this);
230 lowlevel = new AudioESound(this);
238 lowlevel = new AudioALSA(this);
246 lowlevel = new Audio1394(this);
252 lowlevel = new AudioDVB(this);
256 #ifdef HAVE_VIDEO4LINUX2
258 lowlevel = new AudioV4L2MPEG(this);
264 lowlevel = new AudioPulse(this);
272 int AudioDevice::open_input(AudioInConfig *config, VideoInConfig *vconfig,
273 int rate, int samples, int channels, int realtime)
275 device_lock->lock("AudioDevice::open_input");
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();
294 int AudioDevice::open_output(AudioOutConfig *config,
295 int rate, int samples, int channels, int realtime)
297 device_lock->lock("AudioDevice::open_output");
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();
313 int AudioDevice::open_monitor(AudioOutConfig *config,int mode)
315 device_lock->lock("AudioDevice::open_output");
317 *this->out_config = *config;
320 device_lock->unlock();
326 int AudioDevice::interrupt_crash()
329 lowlevel_in->interrupt_crash();
334 double AudioDevice::get_itimestamp()
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();
348 double AudioDevice::get_otimestamp()
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();
361 double AudioDevice::device_timestamp()
363 return lowlevel_in ? lowlevel_in->device_timestamp() : -1.;
366 int AudioDevice::close_all()
368 device_lock->lock("AudioDevice::close_all");
371 lowlevel_in->close_all();
377 lowlevel_out->close_all();
382 device_lock->unlock();
386 void AudioDevice::auto_update(int channels, int samplerate, int bits)
388 if( !in_config || !in_config->follow_audio ) return;
389 in_channels = channels;
390 in_samplerate = samplerate;
392 in_config_updated = 1;
393 polling_lock->unlock();
396 int AudioDevice::config_updated()
398 if( !in_config || !in_config->follow_audio ) return 0;
399 return in_config_updated;
402 void AudioDevice::config_update()
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;
419 int AudioDevice::start_toc(const char *path, const char *toc_path)
421 return lowlevel_in ? lowlevel_in->start_toc(path, toc_path) : -1;
424 int AudioDevice::start_record(int fd, int bsz)
426 return lowlevel_in ? lowlevel_in->start_record(fd, bsz) : -1;
429 int AudioDevice::stop_record()
431 return lowlevel_in ? lowlevel_in->stop_record() : -1;
434 int AudioDevice::total_audio_channels()
436 return lowlevel_in ? lowlevel_in->total_audio_channels() : 0;
439 DeviceMPEGInput *AudioDevice::mpeg_device()
441 return lowlevel_in ? lowlevel_in->mpeg_device() : 0;