2014-11-17 03:49:20 +08:00
|
|
|
/*
|
|
|
|
Copyright (C) 2014 by Project Tox <https://tox.im>
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
This program is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-01-05 03:24:11 +08:00
|
|
|
// Output some extra debug info
|
|
|
|
#define AUDIO_DEBUG 1
|
|
|
|
|
|
|
|
// Fix a 7 years old openal-soft/alsa bug
|
|
|
|
// http://blog.gmane.org/gmane.comp.lib.openal.devel/month=20080501
|
|
|
|
// If set to 1, the capture will be started as long as the device is open
|
|
|
|
#define FIX_SND_PCM_PREPARE_BUG 0
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
#include "audio.h"
|
|
|
|
#include "src/core.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
2014-11-20 05:26:04 +08:00
|
|
|
#include <QThread>
|
2015-01-05 00:55:39 +08:00
|
|
|
#include <QMutexLocker>
|
2014-11-20 05:26:04 +08:00
|
|
|
|
|
|
|
#include <cassert>
|
2014-11-17 02:04:45 +08:00
|
|
|
|
|
|
|
std::atomic<int> Audio::userCount{0};
|
2014-11-20 05:26:04 +08:00
|
|
|
Audio* Audio::instance{nullptr};
|
|
|
|
QThread* Audio::audioThread{nullptr};
|
2015-01-05 00:55:39 +08:00
|
|
|
QMutex* Audio::audioInLock{nullptr};
|
|
|
|
QMutex* Audio::audioOutLock{nullptr};
|
2014-11-17 02:04:45 +08:00
|
|
|
ALCdevice* Audio::alInDev{nullptr};
|
|
|
|
ALCdevice* Audio::alOutDev{nullptr};
|
2014-11-20 05:26:04 +08:00
|
|
|
ALCcontext* Audio::alContext{nullptr};
|
2014-11-17 02:04:45 +08:00
|
|
|
ALuint Audio::alMainSource{0};
|
2015-02-01 23:58:59 +08:00
|
|
|
float Audio::outputVolume{1.0};
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2015-01-05 03:24:11 +08:00
|
|
|
void audioDebugLog(QString msg)
|
|
|
|
{
|
|
|
|
#if (AUDIO_DEBUG)
|
|
|
|
qDebug()<<"Audio: "<<msg;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-11-20 05:26:04 +08:00
|
|
|
Audio& Audio::getInstance()
|
|
|
|
{
|
|
|
|
if (!instance)
|
|
|
|
{
|
|
|
|
instance = new Audio();
|
|
|
|
audioThread = new QThread(instance);
|
|
|
|
audioThread->setObjectName("qTox Audio");
|
|
|
|
audioThread->start();
|
2015-01-05 00:55:39 +08:00
|
|
|
audioInLock = new QMutex(QMutex::Recursive);
|
|
|
|
audioOutLock = new QMutex(QMutex::Recursive);
|
2014-11-20 05:26:04 +08:00
|
|
|
instance->moveToThread(audioThread);
|
|
|
|
}
|
|
|
|
return *instance;
|
|
|
|
}
|
|
|
|
|
2015-01-24 22:31:11 +08:00
|
|
|
Audio::~Audio()
|
|
|
|
{
|
2015-01-25 02:32:07 +08:00
|
|
|
qDebug() << "Deleting Audio";
|
|
|
|
audioThread->exit(0);
|
|
|
|
audioThread->wait();
|
|
|
|
if (audioThread->isRunning())
|
|
|
|
audioThread->terminate();
|
2015-01-24 22:31:11 +08:00
|
|
|
delete audioThread;
|
|
|
|
delete audioInLock;
|
|
|
|
delete audioOutLock;
|
|
|
|
}
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
void Audio::suscribeInput()
|
|
|
|
{
|
2015-01-05 03:24:11 +08:00
|
|
|
if (!alInDev)
|
|
|
|
{
|
|
|
|
qWarning()<<"Audio::suscribeInput: input device is closed";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
audioDebugLog("suscribing");
|
2015-01-05 00:55:39 +08:00
|
|
|
QMutexLocker lock(audioInLock);
|
2014-11-17 03:51:44 +08:00
|
|
|
if (!userCount++ && alInDev)
|
2015-01-05 03:24:11 +08:00
|
|
|
{
|
|
|
|
#if (!FIX_SND_PCM_PREPARE_BUG)
|
|
|
|
audioDebugLog("starting capture");
|
2014-11-17 03:51:44 +08:00
|
|
|
alcCaptureStart(alInDev);
|
2015-01-05 03:24:11 +08:00
|
|
|
#endif
|
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::unsuscribeInput()
|
|
|
|
{
|
2015-01-05 03:24:11 +08:00
|
|
|
if (!alInDev)
|
|
|
|
{
|
|
|
|
qWarning()<<"Audio::unsuscribeInput: input device is closed";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
audioDebugLog("unsuscribing");
|
2015-01-05 00:55:39 +08:00
|
|
|
QMutexLocker lock(audioInLock);
|
2014-11-17 03:51:44 +08:00
|
|
|
if (!--userCount && alInDev)
|
2015-01-05 03:24:11 +08:00
|
|
|
{
|
|
|
|
#if (!FIX_SND_PCM_PREPARE_BUG)
|
|
|
|
audioDebugLog("stopping capture");
|
2014-11-17 03:51:44 +08:00
|
|
|
alcCaptureStop(alInDev);
|
2015-01-05 03:24:11 +08:00
|
|
|
#endif
|
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::openInput(const QString& inDevDescr)
|
|
|
|
{
|
2015-01-05 03:24:11 +08:00
|
|
|
audioDebugLog("Trying to open input "+inDevDescr);
|
2015-01-05 00:55:39 +08:00
|
|
|
QMutexLocker lock(audioInLock);
|
2014-11-17 02:04:45 +08:00
|
|
|
auto* tmp = alInDev;
|
|
|
|
alInDev = nullptr;
|
2014-11-27 06:21:57 +08:00
|
|
|
if (tmp)
|
|
|
|
alcCaptureCloseDevice(tmp);
|
2014-11-17 02:04:45 +08:00
|
|
|
int stereoFlag = av_DefaultSettings.audio_channels==1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
|
|
|
|
if (inDevDescr.isEmpty())
|
|
|
|
alInDev = alcCaptureOpenDevice(nullptr,av_DefaultSettings.audio_sample_rate, stereoFlag,
|
|
|
|
(av_DefaultSettings.audio_frame_duration * av_DefaultSettings.audio_sample_rate * 4)
|
|
|
|
/ 1000 * av_DefaultSettings.audio_channels);
|
|
|
|
else
|
|
|
|
alInDev = alcCaptureOpenDevice(inDevDescr.toStdString().c_str(),av_DefaultSettings.audio_sample_rate, stereoFlag,
|
|
|
|
(av_DefaultSettings.audio_frame_duration * av_DefaultSettings.audio_sample_rate * 4)
|
|
|
|
/ 1000 * av_DefaultSettings.audio_channels);
|
|
|
|
if (!alInDev)
|
|
|
|
qWarning() << "Audio: Cannot open input audio device";
|
|
|
|
else
|
|
|
|
qDebug() << "Audio: Opening audio input "<<inDevDescr;
|
|
|
|
|
|
|
|
Core::getInstance()->resetCallSources(); // Force to regen each group call's sources
|
|
|
|
|
|
|
|
// Restart the capture if necessary
|
2014-11-27 06:21:57 +08:00
|
|
|
if (userCount.load() != 0 && alInDev)
|
2015-01-05 03:24:11 +08:00
|
|
|
{
|
2014-11-17 02:04:45 +08:00
|
|
|
alcCaptureStart(alInDev);
|
2015-01-05 03:24:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if (FIX_SND_PCM_PREPARE_BUG)
|
|
|
|
alcCaptureStart(alInDev);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::openOutput(const QString& outDevDescr)
|
|
|
|
{
|
2015-01-05 03:24:11 +08:00
|
|
|
audioDebugLog("Trying to open output "+outDevDescr);
|
2015-01-05 00:55:39 +08:00
|
|
|
QMutexLocker lock(audioOutLock);
|
2014-11-17 02:04:45 +08:00
|
|
|
auto* tmp = alOutDev;
|
|
|
|
alOutDev = nullptr;
|
|
|
|
if (outDevDescr.isEmpty())
|
|
|
|
alOutDev = alcOpenDevice(nullptr);
|
|
|
|
else
|
|
|
|
alOutDev = alcOpenDevice(outDevDescr.toStdString().c_str());
|
|
|
|
if (!alOutDev)
|
|
|
|
{
|
|
|
|
qWarning() << "Audio: Cannot open output audio device";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-05 00:55:39 +08:00
|
|
|
if (alContext && alcMakeContextCurrent(nullptr) == ALC_TRUE)
|
2014-11-27 06:21:57 +08:00
|
|
|
alcDestroyContext(alContext);
|
2014-12-01 03:04:55 +08:00
|
|
|
if (tmp)
|
|
|
|
alcCloseDevice(tmp);
|
2014-11-17 02:04:45 +08:00
|
|
|
alContext=alcCreateContext(alOutDev,nullptr);
|
|
|
|
if (!alcMakeContextCurrent(alContext))
|
|
|
|
{
|
|
|
|
qWarning() << "Audio: Cannot create output audio context";
|
|
|
|
alcCloseDevice(alOutDev);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
alGenSources(1, &alMainSource);
|
|
|
|
|
|
|
|
|
|
|
|
qDebug() << "Audio: Opening audio output "<<outDevDescr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::getInstance()->resetCallSources(); // Force to regen each group call's sources
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::closeInput()
|
|
|
|
{
|
2015-01-05 03:24:11 +08:00
|
|
|
audioDebugLog("Closing input");
|
2015-01-05 00:55:39 +08:00
|
|
|
QMutexLocker lock(audioInLock);
|
2014-11-17 02:04:45 +08:00
|
|
|
if (alInDev)
|
2015-01-05 03:24:11 +08:00
|
|
|
{
|
|
|
|
if (alcCaptureCloseDevice(alInDev) == ALC_TRUE)
|
|
|
|
{
|
|
|
|
alInDev = nullptr;
|
|
|
|
userCount = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning() << "Audio: Failed to close input";
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::closeOutput()
|
|
|
|
{
|
2015-01-05 03:24:11 +08:00
|
|
|
audioDebugLog("Closing output");
|
2015-01-05 00:55:39 +08:00
|
|
|
QMutexLocker lock(audioOutLock);
|
|
|
|
if (alContext && alcMakeContextCurrent(nullptr) == ALC_TRUE)
|
2014-11-17 02:04:45 +08:00
|
|
|
alcDestroyContext(alContext);
|
|
|
|
|
|
|
|
if (alOutDev)
|
2015-01-05 03:24:11 +08:00
|
|
|
{
|
|
|
|
if (alcCloseDevice(alOutDev) == ALC_TRUE)
|
|
|
|
{
|
|
|
|
alOutDev = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning() << "Audio: Failed to close output";
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::playMono16Sound(const QByteArray& data)
|
|
|
|
{
|
2015-01-05 00:55:39 +08:00
|
|
|
QMutexLocker lock(audioOutLock);
|
|
|
|
if (!alOutDev)
|
|
|
|
return;
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
ALuint buffer;
|
|
|
|
alGenBuffers(1, &buffer);
|
|
|
|
alBufferData(buffer, AL_FORMAT_MONO16, data.data(), data.size(), 44100);
|
|
|
|
alSourcei(alMainSource, AL_BUFFER, buffer);
|
|
|
|
alSourcePlay(alMainSource);
|
|
|
|
alDeleteBuffers(1, &buffer);
|
|
|
|
}
|
2014-11-20 05:26:04 +08:00
|
|
|
|
|
|
|
void Audio::playGroupAudioQueued(Tox*,int group, int peer, const int16_t* data,
|
2015-03-13 14:00:12 +08:00
|
|
|
unsigned samples, uint8_t channels, unsigned sample_rate, void* core)
|
2014-11-20 05:26:04 +08:00
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(instance, "playGroupAudio", Qt::BlockingQueuedConnection,
|
|
|
|
Q_ARG(int,group), Q_ARG(int,peer), Q_ARG(const int16_t*,data),
|
|
|
|
Q_ARG(unsigned,samples), Q_ARG(uint8_t,channels), Q_ARG(unsigned,sample_rate));
|
2015-03-13 14:00:12 +08:00
|
|
|
emit static_cast<Core*>(core)->groupPeerAudioPlaying(group, peer);
|
2014-11-20 05:26:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::playGroupAudio(int group, int peer, const int16_t* data,
|
|
|
|
unsigned samples, uint8_t channels, unsigned sample_rate)
|
|
|
|
{
|
|
|
|
assert(QThread::currentThread() == audioThread);
|
|
|
|
|
2015-01-05 00:55:39 +08:00
|
|
|
QMutexLocker lock(audioOutLock);
|
|
|
|
|
2014-11-20 05:26:04 +08:00
|
|
|
ToxGroupCall& call = Core::groupCalls[group];
|
|
|
|
|
|
|
|
if (!call.active || call.muteVol)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!call.alSources.contains(peer))
|
2015-02-01 23:58:59 +08:00
|
|
|
{
|
2014-11-20 05:26:04 +08:00
|
|
|
alGenSources(1, &call.alSources[peer]);
|
2015-02-01 23:58:59 +08:00
|
|
|
alSourcef(call.alSources[peer], AL_GAIN, outputVolume);
|
|
|
|
}
|
2014-11-20 05:26:04 +08:00
|
|
|
|
|
|
|
playAudioBuffer(call.alSources[peer], data, samples, channels, sample_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, unsigned channels, int sampleRate)
|
|
|
|
{
|
|
|
|
assert(channels == 1 || channels == 2);
|
|
|
|
|
2015-01-05 00:55:39 +08:00
|
|
|
QMutexLocker lock(audioOutLock);
|
|
|
|
|
2014-11-20 05:26:04 +08:00
|
|
|
ALuint bufid;
|
|
|
|
ALint processed = 0, queued = 16;
|
|
|
|
alGetSourcei(alSource, AL_BUFFERS_PROCESSED, &processed);
|
|
|
|
alGetSourcei(alSource, AL_BUFFERS_QUEUED, &queued);
|
|
|
|
alSourcei(alSource, AL_LOOPING, AL_FALSE);
|
|
|
|
|
2014-12-12 02:05:52 +08:00
|
|
|
if (processed)
|
2014-11-20 05:26:04 +08:00
|
|
|
{
|
|
|
|
ALuint bufids[processed];
|
|
|
|
alSourceUnqueueBuffers(alSource, processed, bufids);
|
|
|
|
alDeleteBuffers(processed - 1, bufids + 1);
|
|
|
|
bufid = bufids[0];
|
|
|
|
}
|
2014-12-12 02:05:52 +08:00
|
|
|
else if (queued < 16)
|
2014-11-20 05:26:04 +08:00
|
|
|
{
|
|
|
|
alGenBuffers(1, &bufid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "Audio: Dropped frame";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
alBufferData(bufid, (channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, data,
|
|
|
|
samples * 2 * channels, sampleRate);
|
|
|
|
alSourceQueueBuffers(alSource, 1, &bufid);
|
|
|
|
|
|
|
|
ALint state;
|
|
|
|
alGetSourcei(alSource, AL_SOURCE_STATE, &state);
|
2015-02-01 23:58:59 +08:00
|
|
|
alSourcef(alSource, AL_GAIN, outputVolume);
|
2014-12-12 02:05:52 +08:00
|
|
|
if (state != AL_PLAYING)
|
2014-11-20 05:26:04 +08:00
|
|
|
alSourcePlay(alSource);
|
|
|
|
}
|
2015-01-05 00:55:39 +08:00
|
|
|
|
|
|
|
bool Audio::isInputReady()
|
|
|
|
{
|
|
|
|
return (alInDev && userCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Audio::isOutputClosed()
|
|
|
|
{
|
|
|
|
return (alOutDev);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Audio::tryCaptureSamples(uint8_t* buf, int framesize)
|
|
|
|
{
|
|
|
|
QMutexLocker lock(audioInLock);
|
|
|
|
|
|
|
|
ALint samples=0;
|
|
|
|
alcGetIntegerv(Audio::alInDev, ALC_CAPTURE_SAMPLES, sizeof(samples), &samples);
|
|
|
|
if (samples < framesize)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
memset(buf, 0, framesize * 2 * av_DefaultSettings.audio_channels); // Avoid uninitialized values (Valgrind)
|
|
|
|
alcCaptureSamples(Audio::alInDev, buf, framesize);
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-18 14:25:25 +08:00
|
|
|
|
|
|
|
#ifdef QTOX_FILTER_AUDIO
|
|
|
|
#include "audiofilterer.h"
|
2015-02-19 01:17:06 +08:00
|
|
|
|
|
|
|
/* include for compatibility with older versions of OpenAL */
|
|
|
|
#ifndef ALC_ALL_DEVICES_SPECIFIER
|
2015-02-18 14:25:25 +08:00
|
|
|
#include <AL/alext.h>
|
2015-02-19 01:17:06 +08:00
|
|
|
#endif
|
2015-02-18 14:25:25 +08:00
|
|
|
|
|
|
|
void Audio::getEchoesToFilter(AudioFilterer* filterer, int framesize)
|
|
|
|
{
|
|
|
|
#ifdef ALC_LOOPBACK_CAPTURE_SAMPLES
|
|
|
|
ALint samples;
|
|
|
|
alcGetIntegerv(Audio::alOutDev, ALC_LOOPBACK_CAPTURE_SAMPLES, sizeof(samples), &samples);
|
|
|
|
if (samples >= framesize)
|
|
|
|
{
|
|
|
|
int16_t buf[framesize];
|
|
|
|
alcCaptureSamplesLoopback(Audio::alOutDev, buf, framesize);
|
|
|
|
filterer->passAudioOutput(buf, framesize);
|
|
|
|
filterer->setEchoDelayMs(5); // This 5ms is configurable I believe
|
|
|
|
}
|
2015-02-20 08:55:43 +08:00
|
|
|
#else
|
|
|
|
Q_UNUSED(filterer);
|
|
|
|
Q_UNUSED(framesize);
|
2015-02-18 14:25:25 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|