2014-11-17 03:49:20 +08:00
|
|
|
/*
|
2015-06-06 09:40:08 +08:00
|
|
|
Copyright © 2014-2015 by The qTox Project
|
|
|
|
|
2014-11-17 03:49:20 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2014-11-17 03:49:20 +08:00
|
|
|
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.
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2014-11-17 03:49:20 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-11-17 03:49:20 +08:00
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
2014-11-17 03:49:20 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
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"
|
2015-04-24 08:32:09 +08:00
|
|
|
#include "src/core/core.h"
|
2015-08-05 20:45:02 +08:00
|
|
|
#include "src/persistence/settings.h"
|
2015-06-26 19:00:16 +08:00
|
|
|
#include "src/core/coreav.h"
|
2014-11-17 02:04:45 +08:00
|
|
|
|
|
|
|
#include <QDebug>
|
2015-08-05 20:45:02 +08:00
|
|
|
#include <QTimer>
|
2015-10-18 21:23:33 +08:00
|
|
|
#include <QThread>
|
2015-06-26 19:00:16 +08:00
|
|
|
#include <QMutexLocker>
|
2014-11-20 05:26:04 +08:00
|
|
|
|
|
|
|
#include <cassert>
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2014-11-20 05:26:04 +08:00
|
|
|
Audio* Audio::instance{nullptr};
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Returns the singleton's instance. Will construct on first call.
|
|
|
|
*/
|
2014-11-20 05:26:04 +08:00
|
|
|
Audio& Audio::getInstance()
|
|
|
|
{
|
|
|
|
if (!instance)
|
|
|
|
{
|
|
|
|
instance = new Audio();
|
2015-10-18 21:23:33 +08:00
|
|
|
instance->startAudioThread();
|
2014-11-20 05:26:04 +08:00
|
|
|
}
|
|
|
|
return *instance;
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:23:33 +08:00
|
|
|
Audio::Audio()
|
|
|
|
: audioThread(new QThread())
|
|
|
|
, audioInLock(QMutex::Recursive)
|
|
|
|
, audioOutLock(QMutex::Recursive)
|
2015-10-19 03:19:33 +08:00
|
|
|
, inputSubscriptions(0)
|
2015-10-18 21:23:33 +08:00
|
|
|
, alOutDev(nullptr)
|
|
|
|
, alInDev(nullptr)
|
|
|
|
, outputVolume(1.0)
|
|
|
|
, inputVolume(1.0)
|
|
|
|
, alMainSource(0)
|
|
|
|
, alContext(nullptr)
|
|
|
|
, timer(new QTimer(this))
|
|
|
|
{
|
|
|
|
timer->setSingleShot(true);
|
|
|
|
connect(timer, &QTimer::timeout, this, &Audio::closeOutput);
|
|
|
|
|
|
|
|
audioThread->setObjectName("qTox Audio");
|
|
|
|
connect(audioThread, &QThread::finished, audioThread, &QThread::deleteLater);
|
|
|
|
}
|
|
|
|
|
2015-01-24 22:31:11 +08:00
|
|
|
Audio::~Audio()
|
|
|
|
{
|
2015-10-18 21:23:33 +08:00
|
|
|
audioThread->exit();
|
2015-01-25 02:32:07 +08:00
|
|
|
audioThread->wait();
|
|
|
|
if (audioThread->isRunning())
|
|
|
|
audioThread->terminate();
|
2015-10-18 21:23:33 +08:00
|
|
|
}
|
2015-04-24 19:01:50 +08:00
|
|
|
|
2015-10-20 09:41:18 +08:00
|
|
|
/**
|
|
|
|
Start the audio thread for capture and playback.
|
|
|
|
*/
|
2015-10-18 21:23:33 +08:00
|
|
|
void Audio::startAudioThread()
|
|
|
|
{
|
|
|
|
if (!audioThread->isRunning())
|
|
|
|
audioThread->start();
|
2015-10-20 09:42:02 +08:00
|
|
|
else
|
|
|
|
qWarning("Audio thread already started -> ignored.");
|
2015-10-18 21:23:33 +08:00
|
|
|
|
|
|
|
moveToThread(audioThread);
|
2015-01-24 22:31:11 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Returns the current output volume, between 0 and 1
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
qreal Audio::getOutputVolume()
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&audioOutLock);
|
2015-05-12 07:27:32 +08:00
|
|
|
return outputVolume;
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
The volume must be between 0 and 1
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::setOutputVolume(qreal volume)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&audioOutLock);
|
2015-05-12 07:27:32 +08:00
|
|
|
outputVolume = volume;
|
|
|
|
alSourcef(alMainSource, AL_GAIN, outputVolume);
|
|
|
|
|
2015-06-26 19:00:16 +08:00
|
|
|
for (const ToxGroupCall& call : CoreAV::groupCalls)
|
2015-05-12 07:27:32 +08:00
|
|
|
{
|
|
|
|
if (!call.active)
|
|
|
|
continue;
|
|
|
|
for (ALuint source : call.alSources)
|
|
|
|
alSourcef(source, AL_GAIN, outputVolume);
|
|
|
|
}
|
|
|
|
|
2015-06-26 19:00:16 +08:00
|
|
|
for (const ToxCall& call : CoreAV::calls)
|
2015-05-12 07:27:32 +08:00
|
|
|
{
|
|
|
|
if (!call.active)
|
|
|
|
continue;
|
|
|
|
alSourcef(call.alSource, AL_GAIN, outputVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
The volume must be between 0 and 2
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::setInputVolume(qreal volume)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&audioInLock);
|
2015-10-18 16:15:02 +08:00
|
|
|
inputVolume = volume;
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
2015-10-20 09:41:18 +08:00
|
|
|
@brief Subscribe to capture sound from the opened input device.
|
|
|
|
|
|
|
|
If the input device is not open, it will be opened before capturing.
|
2015-10-18 21:51:38 +08:00
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::subscribeInput()
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-10-19 03:19:33 +08:00
|
|
|
qDebug() << "subscribing input" << inputSubscriptions;
|
|
|
|
if (!inputSubscriptions++)
|
2015-01-05 03:24:11 +08:00
|
|
|
{
|
2015-10-20 09:46:44 +08:00
|
|
|
openInput(Settings::getInstance().getInDev());
|
|
|
|
openOutput(Settings::getInstance().getOutDev());
|
2015-08-05 20:45:02 +08:00
|
|
|
|
2015-01-05 03:24:11 +08:00
|
|
|
#if (!FIX_SND_PCM_PREPARE_BUG)
|
2015-08-05 20:45:02 +08:00
|
|
|
if (alInDev)
|
|
|
|
{
|
|
|
|
qDebug() << "starting capture";
|
|
|
|
alcCaptureStart(alInDev);
|
|
|
|
}
|
2015-01-05 03:24:11 +08:00
|
|
|
#endif
|
|
|
|
}
|
2015-10-18 21:23:33 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
2015-10-20 09:41:18 +08:00
|
|
|
@brief Unsubscribe from capturing from an opened input device.
|
|
|
|
|
|
|
|
If the input device has no more subscriptions, it will be closed.
|
2015-10-18 21:51:38 +08:00
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::unsubscribeInput()
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-10-19 03:19:33 +08:00
|
|
|
qDebug() << "unsubscribing input" << inputSubscriptions;
|
|
|
|
if (inputSubscriptions > 0)
|
|
|
|
inputSubscriptions--;
|
|
|
|
else if(inputSubscriptions < 0)
|
|
|
|
inputSubscriptions = 0;
|
2015-08-05 23:42:10 +08:00
|
|
|
|
2015-10-20 09:46:44 +08:00
|
|
|
if (!inputSubscriptions) {
|
2015-08-05 20:45:02 +08:00
|
|
|
closeOutput();
|
2015-08-05 23:42:10 +08:00
|
|
|
closeInput();
|
2015-01-05 03:24:11 +08:00
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Open an input device, use before suscribing
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::openInput(const QString& inDevDescr)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
|
|
|
QMutexLocker lock(&audioInLock);
|
2015-10-21 05:12:55 +08:00
|
|
|
|
|
|
|
if (alInDev) {
|
|
|
|
#if (!FIX_SND_PCM_PREPARE_BUG)
|
|
|
|
qDebug() << "stopping capture";
|
|
|
|
alcCaptureStop(alInDev);
|
|
|
|
#endif
|
|
|
|
alcCaptureCloseDevice(alInDev);
|
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
alInDev = nullptr;
|
2015-04-24 19:01:50 +08:00
|
|
|
|
2015-06-26 19:00:16 +08:00
|
|
|
/// TODO: Try to actually detect if our audio source is stereo
|
|
|
|
int stereoFlag = DefaultSettings::audioChannels ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
|
|
|
|
const uint32_t sampleRate = DefaultSettings::sampleRate;
|
|
|
|
const uint16_t frameDuration = DefaultSettings::frameDuration;
|
|
|
|
const uint32_t chnls = DefaultSettings::audioChannels;
|
2015-10-21 05:12:55 +08:00
|
|
|
const ALCsizei bufSize = (frameDuration * sampleRate * 4) / 1000 * chnls;
|
2014-11-17 02:04:45 +08:00
|
|
|
if (inDevDescr.isEmpty())
|
2015-10-21 05:12:55 +08:00
|
|
|
alInDev = alcCaptureOpenDevice(nullptr, sampleRate, stereoFlag, bufSize);
|
2014-11-17 02:04:45 +08:00
|
|
|
else
|
2015-10-21 05:12:55 +08:00
|
|
|
alInDev = alcCaptureOpenDevice(inDevDescr.toStdString().c_str(),
|
|
|
|
sampleRate, stereoFlag, bufSize);
|
|
|
|
|
|
|
|
if (alInDev)
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Opening audio input "<<inDevDescr;
|
2015-10-21 05:12:55 +08:00
|
|
|
else
|
|
|
|
qWarning() << "Cannot open input audio device " + inDevDescr;
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2015-05-20 01:40:01 +08:00
|
|
|
Core* core = Core::getInstance();
|
|
|
|
if (core)
|
2015-06-26 19:00:16 +08:00
|
|
|
CoreAV::resetCallSources(); // Force to regen each group call's sources
|
2014-11-17 02:04:45 +08:00
|
|
|
|
|
|
|
// Restart the capture if necessary
|
2015-10-18 17:14:26 +08:00
|
|
|
if (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)
|
2015-10-21 05:12:55 +08:00
|
|
|
alcCaptureStart(alInDev);
|
2015-01-05 03:24:11 +08:00
|
|
|
#endif
|
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Open an output device
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
bool Audio::openOutput(const QString &outDevDescr)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-10-21 05:14:07 +08:00
|
|
|
qDebug() << "Opening audio output " + outDevDescr;
|
2015-10-18 21:23:33 +08:00
|
|
|
QMutexLocker lock(&audioOutLock);
|
2015-10-21 05:14:07 +08:00
|
|
|
|
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());
|
2015-04-24 19:01:50 +08:00
|
|
|
|
2015-10-21 05:14:07 +08:00
|
|
|
if (alOutDev)
|
2014-11-17 02:04:45 +08:00
|
|
|
{
|
2015-01-05 00:55:39 +08:00
|
|
|
if (alContext && alcMakeContextCurrent(nullptr) == ALC_TRUE)
|
2014-11-27 06:21:57 +08:00
|
|
|
alcDestroyContext(alContext);
|
2015-04-24 19:01:50 +08:00
|
|
|
|
2014-12-01 03:04:55 +08:00
|
|
|
if (tmp)
|
|
|
|
alcCloseDevice(tmp);
|
2015-04-24 19:01:50 +08:00
|
|
|
|
2015-10-21 05:14:07 +08:00
|
|
|
alContext = alcCreateContext(alOutDev, nullptr);
|
|
|
|
if (alcMakeContextCurrent(alContext))
|
2014-11-17 02:04:45 +08:00
|
|
|
{
|
2015-10-21 05:14:07 +08:00
|
|
|
alGenSources(1, &alMainSource);
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
2015-04-24 19:01:50 +08:00
|
|
|
{
|
2015-10-21 05:14:07 +08:00
|
|
|
qWarning() << "Cannot create output audio context";
|
|
|
|
alcCloseDevice(alOutDev);
|
|
|
|
return false;
|
2015-04-24 19:01:50 +08:00
|
|
|
}
|
2015-10-21 05:14:07 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning() << "Cannot open output audio device " + outDevDescr;
|
|
|
|
return false;
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
2015-05-20 01:40:01 +08:00
|
|
|
Core* core = Core::getInstance();
|
|
|
|
if (core)
|
2015-06-26 19:00:16 +08:00
|
|
|
CoreAV::resetCallSources(); // Force to regen each group call's sources
|
2015-08-05 22:36:57 +08:00
|
|
|
|
|
|
|
return true;
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Close an input device, please don't use unless everyone's unsuscribed
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::closeInput()
|
2014-11-17 02:04:45 +08:00
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Closing input";
|
2015-10-18 21:23:33 +08:00
|
|
|
QMutexLocker locker(&audioInLock);
|
2014-11-17 02:04:45 +08:00
|
|
|
if (alInDev)
|
2015-01-05 03:24:11 +08:00
|
|
|
{
|
2015-10-20 09:46:44 +08:00
|
|
|
#if (!FIX_SND_PCM_PREPARE_BUG)
|
|
|
|
qDebug() << "stopping capture";
|
|
|
|
alcCaptureStop(alInDev);
|
|
|
|
#endif
|
|
|
|
|
2015-01-05 03:24:11 +08:00
|
|
|
if (alcCaptureCloseDevice(alInDev) == ALC_TRUE)
|
|
|
|
{
|
|
|
|
alInDev = nullptr;
|
2015-10-19 03:19:33 +08:00
|
|
|
inputSubscriptions = 0;
|
2015-01-05 03:24:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qWarning() << "Failed to close input";
|
2015-01-05 03:24:11 +08:00
|
|
|
}
|
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Close an output device
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::closeOutput()
|
2014-11-17 02:04:45 +08:00
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Closing output";
|
2015-10-18 21:23:33 +08:00
|
|
|
QMutexLocker locker(&audioOutLock);
|
2015-10-18 17:13:19 +08:00
|
|
|
|
2015-10-19 03:19:33 +08:00
|
|
|
if (inputSubscriptions > 0)
|
2015-10-18 17:13:19 +08:00
|
|
|
return;
|
|
|
|
|
2015-01-05 00:55:39 +08:00
|
|
|
if (alContext && alcMakeContextCurrent(nullptr) == ALC_TRUE)
|
2015-10-18 17:13:19 +08:00
|
|
|
{
|
2014-11-17 02:04:45 +08:00
|
|
|
alcDestroyContext(alContext);
|
2015-10-18 17:13:19 +08:00
|
|
|
alContext = nullptr;
|
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
|
|
|
|
if (alOutDev)
|
2015-01-05 03:24:11 +08:00
|
|
|
{
|
|
|
|
if (alcCloseDevice(alOutDev) == ALC_TRUE)
|
|
|
|
alOutDev = nullptr;
|
|
|
|
else
|
2015-05-11 20:54:03 +08:00
|
|
|
qWarning() << "Failed to close output";
|
2015-01-05 03:24:11 +08:00
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Play a 44100Hz mono 16bit PCM sound
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::playMono16Sound(const QByteArray& data)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
|
|
|
QMutexLocker lock(&audioOutLock);
|
2015-08-05 22:36:57 +08:00
|
|
|
|
2015-01-05 00:55:39 +08:00
|
|
|
if (!alOutDev)
|
2015-08-05 22:36:57 +08:00
|
|
|
{
|
|
|
|
if (!openOutput(Settings::getInstance().getOutDev()))
|
|
|
|
return;
|
|
|
|
}
|
2015-01-05 00:55:39 +08:00
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
ALuint buffer;
|
|
|
|
alGenBuffers(1, &buffer);
|
|
|
|
alBufferData(buffer, AL_FORMAT_MONO16, data.data(), data.size(), 44100);
|
2015-06-06 09:40:08 +08:00
|
|
|
alSourcef(alMainSource, AL_GAIN, outputVolume);
|
2014-11-17 02:04:45 +08:00
|
|
|
alSourcei(alMainSource, AL_BUFFER, buffer);
|
|
|
|
alSourcePlay(alMainSource);
|
2015-08-05 20:45:02 +08:00
|
|
|
|
|
|
|
ALint sizeInBytes;
|
|
|
|
ALint channels;
|
|
|
|
ALint bits;
|
|
|
|
|
|
|
|
alGetBufferi(buffer, AL_SIZE, &sizeInBytes);
|
|
|
|
alGetBufferi(buffer, AL_CHANNELS, &channels);
|
|
|
|
alGetBufferi(buffer, AL_BITS, &bits);
|
|
|
|
int lengthInSamples = sizeInBytes * 8 / (channels * bits);
|
|
|
|
|
|
|
|
ALint frequency;
|
|
|
|
alGetBufferi(buffer, AL_FREQUENCY, &frequency);
|
2015-10-19 03:20:39 +08:00
|
|
|
qreal duration = (lengthInSamples / static_cast<qreal>(frequency)) * 1000;
|
2015-08-05 20:45:02 +08:00
|
|
|
int remaining = timer->interval();
|
|
|
|
|
|
|
|
if (duration > remaining)
|
|
|
|
timer->start(duration);
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
alDeleteBuffers(1, &buffer);
|
|
|
|
}
|
2015-10-18 15:50:55 +08:00
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
@brief May be called from any thread, will always queue a call to playGroupAudio.
|
|
|
|
|
|
|
|
The first and last argument are ignored, but allow direct compatibility with toxcore.
|
|
|
|
*/
|
2015-06-26 19:00:16 +08:00
|
|
|
void Audio::playGroupAudioQueued(void*,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
|
|
|
}
|
|
|
|
|
2015-10-20 09:46:44 +08:00
|
|
|
|
2015-10-18 21:23:33 +08:00
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Must be called from the audio thread, plays a group call's received audio
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::playGroupAudio(int group, int peer, const int16_t* data,
|
2015-10-18 21:23:33 +08:00
|
|
|
unsigned samples, uint8_t channels, unsigned sample_rate)
|
2014-11-20 05:26:04 +08:00
|
|
|
{
|
|
|
|
assert(QThread::currentThread() == audioThread);
|
2015-10-18 21:23:33 +08:00
|
|
|
QMutexLocker lock(&audioOutLock);
|
2015-01-05 00:55:39 +08:00
|
|
|
|
2015-06-26 19:00:16 +08:00
|
|
|
ToxGroupCall& call = CoreAV::groupCalls[group];
|
2014-11-20 05:26:04 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-10-15 01:53:46 +08:00
|
|
|
qreal volume = 0.;
|
2015-08-19 01:40:11 +08:00
|
|
|
int bufsize = samples * 2 * channels;
|
|
|
|
for (int i = 0; i < bufsize; ++i)
|
2015-10-21 05:14:07 +08:00
|
|
|
volume += abs(data[i]);
|
2015-08-19 01:40:11 +08:00
|
|
|
|
2015-10-15 01:53:46 +08:00
|
|
|
emit groupAudioPlayed(group, peer, volume / bufsize);
|
2015-08-19 01:40:11 +08:00
|
|
|
|
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-10-18 21:23:33 +08:00
|
|
|
QMutexLocker lock(&audioOutLock);
|
2015-01-05 00:55:39 +08:00
|
|
|
|
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
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Dropped frame";
|
2014-11-20 05:26:04 +08:00
|
|
|
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
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Returns true if the input device is open and suscribed to
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
bool Audio::isInputReady()
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&audioInLock);
|
2015-10-19 03:19:33 +08:00
|
|
|
return alInDev && inputSubscriptions;
|
2015-01-05 00:55:39 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Returns true if the output device is open
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
bool Audio::isOutputClosed()
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&audioOutLock);
|
2015-10-19 03:20:39 +08:00
|
|
|
return alOutDev;
|
2015-01-05 00:55:39 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Does nothing and return false on failure
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
bool Audio::tryCaptureSamples(uint8_t* buf, int framesize)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
|
|
|
QMutexLocker lock(&audioInLock);
|
2015-01-05 00:55:39 +08:00
|
|
|
|
|
|
|
ALint samples=0;
|
|
|
|
alcGetIntegerv(Audio::alInDev, ALC_CAPTURE_SAMPLES, sizeof(samples), &samples);
|
|
|
|
if (samples < framesize)
|
|
|
|
return false;
|
|
|
|
|
2015-06-26 19:00:16 +08:00
|
|
|
memset(buf, 0, framesize * 2 * DefaultSettings::audioChannels); // Avoid uninitialized values (Valgrind)
|
2015-01-05 00:55:39 +08:00
|
|
|
alcCaptureSamples(Audio::alInDev, buf, framesize);
|
2015-10-18 16:15:02 +08:00
|
|
|
|
|
|
|
if (inputVolume != 1)
|
|
|
|
{
|
|
|
|
int16_t* bufReal = reinterpret_cast<int16_t*>(buf);
|
|
|
|
for (int i = 0; i < framesize; ++i)
|
|
|
|
{
|
|
|
|
int sample = bufReal[i] * pow(inputVolume, 2);
|
|
|
|
|
|
|
|
if (sample < std::numeric_limits<int16_t>::min())
|
|
|
|
sample = std::numeric_limits<int16_t>::min();
|
|
|
|
else if (sample > std::numeric_limits<int16_t>::max())
|
|
|
|
sample = std::numeric_limits<int16_t>::max();
|
|
|
|
|
|
|
|
bufReal[i] = sample;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 00:55:39 +08:00
|
|
|
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
|