You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
6.7 KiB
191 lines
6.7 KiB
#include "radio_audio_engine.h" |
|
#include "sound_manager.h" |
|
#include "../transport/gui_bridge.h" |
|
|
|
#include "miniaudio.h" |
|
|
|
extern "C" { |
|
#include "radio/radio_audio.h" |
|
#include "../../lib/debug_config.h" |
|
#include "../../lib/u_async.h" |
|
} |
|
|
|
#include <cstring> |
|
|
|
static RadioAudioEngine* g_radioAudioEngine = nullptr; |
|
|
|
static void radioAudioDataCallback(ma_device* dev, void* out, const void* in, ma_uint32 frames) { |
|
RadioAudioEngine* e = (RadioAudioEngine*)dev->pUserData; |
|
if (e) e->dataCallback(in, out, frames); |
|
} |
|
|
|
RadioAudioEngine* RadioAudioEngine::instance() { |
|
if (!g_radioAudioEngine) g_radioAudioEngine = new RadioAudioEngine(); |
|
return g_radioAudioEngine; |
|
} |
|
|
|
int RadioAudioEngine::start(quint64 groupId) { |
|
if (m_active) { |
|
if (m_groupId == groupId) return 0; |
|
stop(); |
|
} |
|
m_groupId = groupId; |
|
|
|
struct UTUN_INSTANCE* inst = gui_bridge_get_inst(); |
|
if (!inst) { |
|
DEBUG_WARN(DEBUG_CATEGORY_DEBUG, "RadioAudio: no instance, cannot start"); |
|
return -1; |
|
} |
|
if (radio_audio_init(inst) != 0) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_DEBUG, "RadioAudio: radio_audio_init failed"); |
|
return -1; |
|
} |
|
if (radio_audio_start(groupId) != 0) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_DEBUG, "RadioAudio: radio_audio_start failed grp=%016llx", (unsigned long long)groupId); |
|
return -1; |
|
} |
|
|
|
m_captureBuf.assign(RADIO_AUDIO_FRAME_SAMPLES, 0); |
|
m_capturePos = 0; |
|
|
|
if (initDevice() != 0) { |
|
radio_audio_stop(); |
|
return -1; |
|
} |
|
|
|
m_active = true; |
|
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "RadioAudio: started grp=%016llx", (unsigned long long)groupId); |
|
return 0; |
|
} |
|
|
|
int RadioAudioEngine::initDevice() { |
|
ma_context* ctx = SoundManager::instance()->context(); |
|
if (!ctx) { |
|
DEBUG_WARN(DEBUG_CATEGORY_DEBUG, "RadioAudio: no audio context"); |
|
return -1; |
|
} |
|
|
|
m_device = new ma_device; |
|
std::memset(m_device, 0, sizeof(*m_device)); |
|
ma_device_config config = ma_device_config_init(ma_device_type_duplex); |
|
config.capture.format = ma_format_s16; |
|
config.capture.channels = 1; |
|
config.playback.format = ma_format_s16; |
|
config.playback.channels = 1; |
|
config.sampleRate = RADIO_AUDIO_SAMPLE_RATE; |
|
config.periodSizeInFrames = 480; |
|
config.dataCallback = radioAudioDataCallback; |
|
config.pUserData = this; |
|
|
|
if (ma_device_init(ctx, &config, m_device) != MA_SUCCESS) { |
|
DEBUG_WARN(DEBUG_CATEGORY_DEBUG, "RadioAudio: device init failed, trying default"); |
|
config.playback.pDeviceID = nullptr; |
|
config.capture.pDeviceID = nullptr; |
|
if (ma_device_init(ctx, &config, m_device) != MA_SUCCESS) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_DEBUG, "RadioAudio: default device init also failed"); |
|
delete m_device; m_device = nullptr; |
|
return -1; |
|
} |
|
} |
|
if (ma_device_start(m_device) != MA_SUCCESS) { |
|
DEBUG_WARN(DEBUG_CATEGORY_DEBUG, "RadioAudio: device start failed"); |
|
ma_device_uninit(m_device); |
|
delete m_device; m_device = nullptr; |
|
return -1; |
|
} |
|
m_deviceStarted = true; |
|
m_capturePos = 0; |
|
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, |
|
"RadioAudio: device started sr=%u cap='%s' fmt=%d internalSr=%u pb='%s' fmt=%d internalSr=%u", |
|
m_device->sampleRate, |
|
m_device->capture.name ? m_device->capture.name : "?", |
|
(int)m_device->capture.format, m_device->capture.internalSampleRate, |
|
m_device->playback.name ? m_device->playback.name : "?", |
|
(int)m_device->playback.format, m_device->playback.internalSampleRate); |
|
return 0; |
|
} |
|
|
|
void RadioAudioEngine::stop() { |
|
if (!m_active && !m_device && !m_deviceStarted) return; |
|
quint64 gid = m_groupId.load(); |
|
bool wasTalking = m_talking.exchange(false); |
|
if (wasTalking) radio_audio_talk_end(gid); /* честный FIN: пиры дренируют звук, а не ждут таймаут */ |
|
m_active = false; |
|
|
|
teardownDevice(); |
|
radio_audio_stop(); |
|
|
|
m_groupId = 0; |
|
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "RadioAudio: stopped"); |
|
} |
|
|
|
void RadioAudioEngine::talkBegin() { |
|
if (!m_active) return; |
|
m_talking = true; |
|
radio_audio_talk_begin(m_groupId.load()); |
|
} |
|
|
|
void RadioAudioEngine::talkEnd() { |
|
if (!m_active) return; |
|
m_talking = false; |
|
radio_audio_talk_end(m_groupId.load()); |
|
} |
|
|
|
void RadioAudioEngine::teardownDevice() { |
|
if (m_deviceStarted) { |
|
ma_device_stop(m_device); |
|
ma_device_uninit(m_device); |
|
m_deviceStarted = false; |
|
} |
|
if (m_device) { delete m_device; m_device = nullptr; } |
|
} |
|
|
|
void RadioAudioEngine::dataCallback(const void* in, void* out, unsigned int frames) { |
|
const int16_t* in16 = (const int16_t*)in; |
|
int16_t* out16 = (int16_t*)out; |
|
|
|
/* диагностика (DEBUG_CATEGORY_DEBUG): cadence коллбэка — gap между вызовами (снаружи) |
|
* и длительность тела (внутри), чтобы отличить «девайс не дёргает» от «заблокирован внутри». */ |
|
uint64_t now = get_time_tb(); |
|
if (m_dbgFrames == 0) { |
|
m_dbgFrames = frames; |
|
DEBUG_DEBUG(DEBUG_CATEGORY_DEBUG, "RadioAudio: callback frames=%u in=%s out=%s", |
|
frames, in16 ? "yes" : "no", out16 ? "yes" : "no"); |
|
} |
|
m_dbgCbCount++; |
|
if (m_dbgPrevCbTb != 0) { |
|
uint64_t gap = now - m_dbgPrevCbTb; |
|
if (gap > m_dbgGapMax) m_dbgGapMax = gap; |
|
} |
|
m_dbgPrevCbTb = now; |
|
if (m_dbgLastTb == 0) { |
|
m_dbgLastTb = now; |
|
} else if (now - m_dbgLastTb >= 10000) { |
|
DEBUG_DEBUG(DEBUG_CATEGORY_DEBUG, "RadioAudio: cb=%u/s frames=%u gapMax=%.1fms bodyMax=%.1fms", |
|
m_dbgCbCount, frames, (double)m_dbgGapMax / 10.0, (double)m_dbgBodyMax / 10.0); |
|
m_dbgCbCount = 0; |
|
m_dbgLastTb = now; |
|
m_dbgGapMax = 0; |
|
m_dbgBodyMax = 0; |
|
} |
|
|
|
/* захват: копим 960 samples → encode+send (сервисный слой). Только когда PTT зажата. */ |
|
if (in16 && m_talking.load()) { |
|
for (unsigned int i = 0; i < frames; i++) { |
|
m_captureBuf[m_capturePos++] = in16[i]; |
|
if (m_capturePos >= (int)m_captureBuf.size()) { |
|
radio_audio_feed_pcm(m_groupId.load(), m_captureBuf.data(), (int)m_captureBuf.size()); |
|
m_capturePos = 0; |
|
} |
|
} |
|
} |
|
|
|
/* воспроизведение: микшированный PCM из сервисного слоя */ |
|
int n = radio_audio_pull_pcm(m_groupId.load(), out16, (int)frames); |
|
if (n < (int)frames) |
|
std::memset(out16 + n, 0, (size_t)(frames - n) * sizeof(int16_t)); |
|
|
|
uint64_t end = get_time_tb(); |
|
uint64_t body = end - now; |
|
if (body > m_dbgBodyMax) m_dbgBodyMax = body; |
|
}
|
|
|