165 lines
6.2 KiB
C++
165 lines
6.2 KiB
C++
#include "call_audio_engine.h"
|
|
#include "sound_manager.h"
|
|
#include "../transport/gui_bridge.h"
|
|
#include "../transport/audio_diagnostics.h"
|
|
extern "C" {
|
|
#include "call/call_audio.h"
|
|
#include "debug_config.h"
|
|
}
|
|
#include <cstring>
|
|
|
|
CallAudioEngine* CallAudioEngine::instance() {
|
|
static auto* engine = new CallAudioEngine();
|
|
return engine;
|
|
}
|
|
|
|
CallAudioEngine::CallAudioEngine() {
|
|
m_playbackId = SoundManager::instance()->playbackDevice();
|
|
m_captureId = SoundManager::instance()->captureDevice();
|
|
connect(SoundManager::instance(), &SoundManager::devicesChanged, this, [this]() {
|
|
setPlaybackDevice(SoundManager::instance()->playbackDevice());
|
|
setCaptureDevice(SoundManager::instance()->captureDevice());
|
|
});
|
|
m_recoveryTimer.setInterval(200);
|
|
connect(&m_recoveryTimer, &QTimer::timeout, this, &CallAudioEngine::recoverDevices);
|
|
connect(SoundManager::instance(), &SoundManager::contextAboutToReset, this, &CallAudioEngine::pauseDevices);
|
|
connect(SoundManager::instance(), &SoundManager::contextRestored, this, [this]() {
|
|
m_capture.retryNow(); m_playback.retryNow();
|
|
if (m_active) recoverDevices();
|
|
});
|
|
}
|
|
|
|
int CallAudioEngine::start(quint64 callId) {
|
|
if (m_active && m_callId == callId && !m_ending) return 0;
|
|
stop();
|
|
if (!gui_bridge_get_inst() || call_audio_start(callId) != 0) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CALL, "CallAudio: cannot start call=%016llx", (unsigned long long)callId);
|
|
return -1;
|
|
}
|
|
++m_generation;
|
|
m_callId = callId;
|
|
m_captureBuf.assign(CALL_AUDIO_FRAME_SAMPLES, 0);
|
|
m_capturePos = 0;
|
|
m_muted = false;
|
|
m_ending = false;
|
|
m_active = true;
|
|
m_capture.retryNow(); m_playback.retryNow();
|
|
recoverDevices();
|
|
m_recoveryTimer.start();
|
|
DEBUG_INFO(DEBUG_CATEGORY_CALL, "CallAudio: session started call=%016llx", (unsigned long long)callId);
|
|
return 0;
|
|
}
|
|
|
|
bool CallAudioEngine::openDevice(bool playback) {
|
|
AudioDevice& device = playback ? m_playback : m_capture;
|
|
ma_context* context = SoundManager::instance()->context();
|
|
if (!context) return false;
|
|
ma_device_config config = ma_device_config_init(playback ? ma_device_type_playback : ma_device_type_capture);
|
|
config.capture.format = config.playback.format = ma_format_s16;
|
|
config.capture.channels = config.playback.channels = 1;
|
|
config.sampleRate = CALL_AUDIO_SAMPLE_RATE;
|
|
ma_device_id id;
|
|
bool selected = SoundManager::deviceId(playback ? m_playbackId : m_captureId, &id);
|
|
if (selected) {
|
|
if (playback) config.playback.pDeviceID = &id;
|
|
else config.capture.pDeviceID = &id;
|
|
}
|
|
auto callback = [this](const void* in, void* out, unsigned int frames) { dataCallback(in, out, frames); };
|
|
const char* role = playback ? "call-playback" : "call-capture";
|
|
auto attempt = [&]() {
|
|
if (!playback) m_capturePos = 0;
|
|
bool ready = device.init(context, config, role, m_callId.load(), callback) && device.start();
|
|
if (!ready) call_audio_reset_io(m_callId.load(), playback);
|
|
return ready;
|
|
};
|
|
if (attempt()) return true;
|
|
if (!selected) return false;
|
|
DEBUG_WARN(DEBUG_CATEGORY_CALL, "CallAudio: %s selected device unavailable, trying default", role);
|
|
config.capture.pDeviceID = config.playback.pDeviceID = nullptr;
|
|
return attempt();
|
|
}
|
|
|
|
void CallAudioEngine::recoverDevices() {
|
|
if (!m_active) return;
|
|
if (m_playback.needsRecovery()) {
|
|
m_playback.close();
|
|
call_audio_reset_io(m_callId.load(), 1);
|
|
m_playback.retryLater();
|
|
}
|
|
if (!m_ending && m_capture.needsRecovery()) {
|
|
m_capture.close();
|
|
m_capturePos = 0;
|
|
call_audio_reset_io(m_callId.load(), 0);
|
|
m_capture.retryLater();
|
|
}
|
|
if (m_playback.retryReady()) openDevice(true);
|
|
if (!m_ending && m_capture.retryReady()) openDevice(false);
|
|
}
|
|
|
|
void CallAudioEngine::pauseDevices() {
|
|
m_capture.close(); m_playback.close();
|
|
m_capturePos = 0;
|
|
if (m_active) call_audio_reset_io(m_callId.load(), 1);
|
|
}
|
|
|
|
void CallAudioEngine::setMuted(bool muted) {
|
|
if (m_muted.exchange(muted) == muted) return;
|
|
DEBUG_INFO(DEBUG_CATEGORY_CALL, "CallAudio: mute=%d", muted);
|
|
}
|
|
|
|
void CallAudioEngine::setPlaybackDevice(const QString& id) {
|
|
if (id == m_playbackId) return;
|
|
m_playbackId = id;
|
|
m_playback.close(); m_playback.retryNow();
|
|
if (m_active) { call_audio_reset_io(m_callId.load(), 1); recoverDevices(); }
|
|
}
|
|
|
|
void CallAudioEngine::setCaptureDevice(const QString& id) {
|
|
if (id == m_captureId) return;
|
|
m_captureId = id;
|
|
m_capture.close(); m_capture.retryNow();
|
|
m_capturePos = 0;
|
|
if (m_active) { call_audio_reset_io(m_callId.load(), 0); recoverDevices(); }
|
|
}
|
|
|
|
void CallAudioEngine::beginEnd(quint64 callId) {
|
|
if (!m_active || m_callId != callId || m_ending) return;
|
|
m_ending = true;
|
|
m_capture.close();
|
|
call_audio_begin_end(callId);
|
|
uint64_t generation = m_generation;
|
|
QTimer::singleShot(500, this, [this, generation]() { if (m_generation == generation) stop(); });
|
|
}
|
|
|
|
void CallAudioEngine::stop() {
|
|
++m_generation;
|
|
m_recoveryTimer.stop();
|
|
m_active = false;
|
|
pauseDevices();
|
|
uint64_t id = m_callId.exchange(0);
|
|
if (id) call_audio_release(id);
|
|
m_muted = false;
|
|
m_ending = false;
|
|
if (id) DEBUG_INFO(DEBUG_CATEGORY_CALL, "CallAudio: session stopped call=%016llx", (unsigned long long)id);
|
|
}
|
|
|
|
void CallAudioEngine::dataCallback(const void* in, void* out, unsigned int frames) {
|
|
if (in) {
|
|
const auto* pcm = static_cast<const int16_t*>(in);
|
|
bool muted = m_muted.load();
|
|
for (unsigned int i = 0; i < frames; ++i) {
|
|
m_captureBuf[m_capturePos++] = pcm[i];
|
|
if (m_capturePos == CALL_AUDIO_FRAME_SAMPLES) {
|
|
int result = call_audio_feed_pcm_muted(m_callId.load(), m_captureBuf.data(), CALL_AUDIO_FRAME_SAMPLES, muted);
|
|
if (result != 0) audio_diag_event(m_capture.device(), AUDIO_DIAG_ERROR, AUDIO_DIAG_FEED, 1, result);
|
|
m_capturePos = 0;
|
|
}
|
|
}
|
|
}
|
|
if (out) {
|
|
auto* pcm = static_cast<int16_t*>(out);
|
|
int n = call_audio_pull_pcm(m_callId.load(), pcm, (int)frames);
|
|
if (n < (int)frames) std::memset(pcm + n, 0, (frames - n) * sizeof(*pcm));
|
|
}
|
|
}
|