Fix(audio): keep audio packets block-aligned end to end
waveOut only converts whole 65-byte GSM610 blocks; a trailing partial block is dropped, never carried over. Windows hosts record into a 1000-byte buffer but the driver fills only 975 (15 blocks) - the last 25 bytes are stale heap the driver never wrote. Clients ship all 1000 bytes anyway, so PlayBuffer has to truncate each packet to whole blocks and throw that tail away. Accumulating the remainder across packets instead - the first attempt at this fix - inserted 25 bytes of garbage every 1000 bytes and shifted the block boundary, turning voice monitoring of existing Windows clients into pure noise while Android stayed fine. Both symptoms are the same "per packet" versus "across packets" opposition, and per packet is the one that matches the wire. So the invariant is: one packet carries a whole number of 65-byte blocks, and nothing is ever carried into the next packet. Windows hosts satisfy it for free. Android now packs 15 blocks (975 bytes) and pads the rest with zeros to keep the 1000-byte length that older controllers expect - see AudioHandler.h. GetRecordBuffer keeps returning the full 1000 bytes on purpose: shortening the packet would make a v1.4.2 controller memcpy m_ulBufferLength and read past the end, which is real garbage rather than harmless stale bytes. Measured against the real waveOut API with a 2 s 1 kHz tone (3250 bytes = 50 blocks = 16000 samples): one 3250-byte buffer played 16000 samples, four 1000-byte buffers played 15360 (25 bytes lost per packet), four 975-byte buffers played 16000. The encoder was never at fault - the same libgsm WAV49 stream decodes through Windows ACM with a correlation of 0.9999 when handed over whole. Also check WHDR_INQUEUE before reusing a header and honour the waveOutWrite return value, since the old code dropped data silently on WAVERR_BUSY; size the playback buffers for the largest packet; and unprepare the waveOut headers in the destructor, which was unpreparing the waveIn headers after they had already been freed. Android APKs built before this commit still send unaligned 1000-byte packets and will sound noisy against this controller - rebuild the APK. Co-Authored-By: DeepSeek-V4.1-Flash
This commit is contained in:
1 parent
d987941e57
commit
3ae54c808e
5 files changed
+190
-36
No files matched your search
@@ -4,17 +4,19 @@
|
||||
// 协议与 Windows 客户端完全一致(client/AudioManager.cpp + client/Audio.cpp):
|
||||
// 主连接 [COMMAND_AUDIO] → 子连接(ConnAuth)→ 首包单字节 [TOKEN_AUDIO_START]
|
||||
// → 服务端弹出 CAudioDlg 后回单字节 [COMMAND_NEXT] → 双向音频开始
|
||||
// 上行:[TOKEN_AUDIO_DATA:1][1000 字节 GSM 流](服务端 CAudio::PlayBuffer 固定按
|
||||
// 1000 字节一块拷贝,包长必须正好 1000,且不能短——短了服务端会越界读)
|
||||
// 上行:[TOKEN_AUDIO_DATA:1][1000 字节]。前 975 字节是 15 个完整 GSM 块,
|
||||
// 后 25 字节补零。
|
||||
// waveOut 只转换整数个 65 字节块,末尾不足一块的字节既不转换也不保留,
|
||||
// 所以 65 字节块绝不能跨包切开 —— 切开的半块会被直接丢掉,块边界一错位
|
||||
// 整条流就是噪声。服务端 CAudio::PlayBuffer 按包截断到整块,那 25 个零
|
||||
// 正好被丢掉。包长仍保持 1000:老版本服务端按 m_ulBufferLength 整块
|
||||
// 拷贝,包长小于 1000 会越界读。
|
||||
// 下行:服务端直接发裸 GSM 流(无 token,不带长度头),也是 1000 字节一块
|
||||
//
|
||||
// 编码格式是服务端 ACM 的 WAVE_FORMAT_GSM610:8000 Hz 单声道,
|
||||
// 65 字节 / 320 采样(40ms)。libgsm 的 WAV49 模式产出完全相同的打包
|
||||
// (每块 2 × 160 采样帧:第一帧 32 字节、第二帧 33 字节),见 gsm/README.md。
|
||||
// 编码与解码必须各用一个 gsm 状态:frame_index/frame_chain 是共用的。
|
||||
//
|
||||
// 语音流是连续的:1000 不是 65 的整数倍,65 字节块会跨包切开,这是允许的——
|
||||
// 服务端把每个 1000 字节缓冲按顺序写给 waveOut,编解码状态跨缓冲延续。
|
||||
#include "common/commands.h"
|
||||
#include "common/logger.h"
|
||||
#include "client/IOCPClient.h"
|
||||
@@ -30,6 +32,12 @@
|
||||
#define LOGI_AUD(...) android_log_tag(ANDROID_LOG_INFO, "YAMA_AUD", __VA_ARGS__)
|
||||
#define LOGE_AUD(...) android_log_tag(ANDROID_LOG_ERROR, "YAMA_AUD", __VA_ARGS__)
|
||||
|
||||
// 上行包:15 个 GSM 块(975 字节)+ 25 字节补零 = 1000 字节。
|
||||
// 补零是为了让包长与老协议一致(老服务端按 m_ulBufferLength 整块 memcpy,
|
||||
// 包长小于 1000 会越界读),服务端按块截断时正好把这 25 个零丢掉。
|
||||
static const size_t AUDIO_PKT_BLOCKS = 15 * 65; // 975 = 整块载荷
|
||||
static const size_t AUDIO_PKT_WIRE = 1000; // 线上包长(含尾部补零)
|
||||
|
||||
extern "C" {
|
||||
#include "gsm.h"
|
||||
}
|
||||
@@ -125,18 +133,26 @@ private:
|
||||
gsm_encode(m_enc, (gsm_signal*)samples, m_frameBuf + 32); // 33 字节
|
||||
m_frameFill = 0;
|
||||
|
||||
// 65 字节块可能跨 1000 字节包切分;音频流仍然是连续的
|
||||
// 一个包只装整数个 65 字节块,剩下的补零 —— 绝不让块跨包切开。
|
||||
// m_frameBuf 正好一块(65)、AUDIO_PKT_BLOCKS 是 15 块,所以 m_pktFill 恒为
|
||||
// 65 的倍数,room 恒 ≥ 65,一轮就拷完。room 为 0 只在常量被改成非整块数时
|
||||
// 才可能出现,那时必须跳出,否则 n=0 会死循环卡住音频线程。
|
||||
size_t off = 0;
|
||||
while (off < sizeof(m_frameBuf)) {
|
||||
const size_t n = std::min(sizeof(m_frameBuf) - off, (size_t)1000 - m_pktFill);
|
||||
const size_t room = AUDIO_PKT_BLOCKS - m_pktFill;
|
||||
const size_t n = std::min(sizeof(m_frameBuf) - off, room);
|
||||
if (n == 0)
|
||||
break;
|
||||
memcpy(m_pkt + 1 + m_pktFill, m_frameBuf + off, n);
|
||||
m_pktFill += n;
|
||||
off += n;
|
||||
if (m_pktFill == 1000) {
|
||||
m_client->Send2Server((char*)m_pkt, 1 + 1000);
|
||||
if (m_pktFill == AUDIO_PKT_BLOCKS) {
|
||||
memset(m_pkt + 1 + AUDIO_PKT_BLOCKS, 0, AUDIO_PKT_WIRE - AUDIO_PKT_BLOCKS);
|
||||
m_client->Send2Server((char*)m_pkt, 1 + AUDIO_PKT_WIRE);
|
||||
m_pktFill = 0;
|
||||
if (++m_pktsSent == 1)
|
||||
LOGI_AUD("first uplink packet sent (1+1000 bytes = 615 ms GSM)");
|
||||
LOGI_AUD("first uplink packet sent (1+%u bytes = %u ms GSM)",
|
||||
(unsigned)(1 + AUDIO_PKT_WIRE), (unsigned)(AUDIO_PKT_BLOCKS * 40 / 65));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -179,7 +195,7 @@ private:
|
||||
size_t m_pendingCount;
|
||||
uint8_t m_frameBuf[65];
|
||||
size_t m_frameFill; // 0 = 下一帧写 32 字节;32 = 下一帧写 33 字节
|
||||
uint8_t m_pkt[1 + 1000]; // [TOKEN_AUDIO_DATA][音频]
|
||||
uint8_t m_pkt[1 + AUDIO_PKT_WIRE]; // [TOKEN_AUDIO_DATA][975 字节整块 + 25 补零]
|
||||
size_t m_pktFill;
|
||||
unsigned m_pktsSent;
|
||||
|
||||
|
||||
Reference in new issue
Block a user