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
295 lines
10 KiB
C++
295 lines
10 KiB
C++
// Audio.cpp: implementation of the CAudio class.
|
||
//
|
||
//////////////////////////////////////////////////////////////////////
|
||
|
||
#include "stdafx.h"
|
||
#include "Audio.h"
|
||
#include <iostream>
|
||
|
||
#ifdef _WINDOWS
|
||
#define __CreateThread CreateThread
|
||
#else
|
||
#include "SafeThread.h"
|
||
#endif
|
||
|
||
//////////////////////////////////////////////////////////////////////
|
||
// Construction/Destruction
|
||
//////////////////////////////////////////////////////////////////////
|
||
|
||
CAudio::CAudio()
|
||
{
|
||
m_bExit = FALSE;
|
||
m_hThreadCallBack = false;
|
||
m_Thread = NULL;
|
||
m_bIsWaveInUsed = FALSE;
|
||
m_bIsWaveOutUsed = FALSE;
|
||
m_nWaveInIndex = 0;
|
||
m_nWaveOutIndex = 0;
|
||
m_hEventWaveIn = CreateEvent(NULL, FALSE, FALSE, NULL);
|
||
m_hStartRecord = CreateEvent(NULL, FALSE, FALSE, NULL);
|
||
memset(&m_GSMWavefmt, 0, sizeof(GSM610WAVEFORMAT));
|
||
|
||
m_GSMWavefmt.wfx.wFormatTag = WAVE_FORMAT_GSM610;
|
||
m_GSMWavefmt.wfx.nChannels = 1;
|
||
m_GSMWavefmt.wfx.nSamplesPerSec = 8000;
|
||
m_GSMWavefmt.wfx.nAvgBytesPerSec = 1625;
|
||
m_GSMWavefmt.wfx.nBlockAlign = 65;
|
||
m_GSMWavefmt.wfx.wBitsPerSample = 0;
|
||
m_GSMWavefmt.wfx.cbSize = 2;
|
||
m_GSMWavefmt.wSamplesPerBlock = 320;
|
||
|
||
m_ulBufferLength = 1000;
|
||
|
||
for (int i = 0; i < 2; ++i) {
|
||
m_InAudioData[i] = new BYTE[m_ulBufferLength];
|
||
m_InAudioHeader[i] = new WAVEHDR;
|
||
|
||
// 播放缓冲按 AUDIO_OUT_MAX(16 个 GSM 块 = 1040 字节)分配:一个包最多
|
||
// 交给 waveOut 这么多,超出的部分在 PlayBuffer 里分多次写。
|
||
m_OutAudioData[i] = new BYTE[AUDIO_OUT_MAX];
|
||
m_OutAudioHeader[i] = new WAVEHDR;
|
||
}
|
||
}
|
||
|
||
CAudio::~CAudio()
|
||
{
|
||
m_bExit = TRUE;
|
||
|
||
if (m_hEventWaveIn) {
|
||
SetEvent(m_hEventWaveIn);
|
||
SAFE_CLOSE_HANDLE(m_hEventWaveIn);
|
||
m_hEventWaveIn = NULL;
|
||
}
|
||
if (m_hStartRecord) {
|
||
SetEvent(m_hStartRecord);
|
||
SAFE_CLOSE_HANDLE(m_hStartRecord);
|
||
m_hStartRecord = NULL;
|
||
}
|
||
|
||
if (m_bIsWaveInUsed) {
|
||
waveInStop(m_hWaveIn);
|
||
waveInReset(m_hWaveIn);
|
||
for (int i = 0; i < 2; ++i)
|
||
waveInUnprepareHeader(m_hWaveIn, m_InAudioHeader[i], sizeof(WAVEHDR));
|
||
|
||
waveInClose(m_hWaveIn);
|
||
WAIT (m_hThreadCallBack, 30);
|
||
if (m_hThreadCallBack)
|
||
Mprintf("没有成功关闭waveInCallBack.\n");
|
||
Mprintf("TerminateThread: waveIn 线程 handle=%p threadId=%lu\n", m_Thread, GetThreadId(m_Thread));
|
||
TerminateThread(m_Thread, -999);
|
||
m_Thread = NULL;
|
||
}
|
||
|
||
for (int i = 0; i < 2; ++i) {
|
||
delete [] m_InAudioData[i];
|
||
m_InAudioData[i] = NULL;
|
||
delete [] m_InAudioHeader[i];
|
||
m_InAudioHeader[i] = NULL;
|
||
}
|
||
|
||
if (m_bIsWaveOutUsed) {
|
||
waveOutReset(m_hWaveOut);
|
||
// 这里原来 unprepare 的是 m_InAudioHeader —— 那是录音侧的头,而且上面
|
||
// 已经 delete 掉了(悬垂指针),unprepare 的对象完全错了。播放侧要收的是
|
||
// m_OutAudioHeader。
|
||
for (int i = 0; i < 2; ++i) {
|
||
if (m_OutAudioHeader[i])
|
||
waveOutUnprepareHeader(m_hWaveOut, m_OutAudioHeader[i], sizeof(WAVEHDR));
|
||
}
|
||
waveOutClose(m_hWaveOut);
|
||
}
|
||
|
||
for (int i = 0; i < 2; ++i) {
|
||
delete [] m_OutAudioData[i];
|
||
m_OutAudioData[i] = NULL;
|
||
delete [] m_OutAudioHeader[i];
|
||
m_OutAudioHeader[i] = NULL;
|
||
}
|
||
}
|
||
|
||
BOOL CAudio::InitializeWaveIn()
|
||
{
|
||
MMRESULT mmResult;
|
||
DWORD dwThreadID = 0;
|
||
|
||
m_hThreadCallBack = m_Thread = __CreateThread(NULL, 0,
|
||
waveInCallBack, (LPVOID)this,
|
||
CREATE_SUSPENDED, &dwThreadID);
|
||
|
||
//打开录音设备COM 1 指定声音规格 2 支持通过线程回调 换缓冲
|
||
mmResult = waveInOpen(&m_hWaveIn, (WORD)WAVE_MAPPER,
|
||
&(m_GSMWavefmt.wfx), (LONG)dwThreadID, (LONG)0, CALLBACK_THREAD);
|
||
|
||
//m_hWaveIn 录音机句柄
|
||
if (mmResult != MMSYSERR_NOERROR) {
|
||
return FALSE;
|
||
}
|
||
|
||
//录音设备 需要的两个缓冲
|
||
for (int i=0; i<2; ++i) {
|
||
m_InAudioHeader[i]->lpData = (LPSTR)m_InAudioData[i]; //m_lpInAudioData 指针数组
|
||
m_InAudioHeader[i]->dwBufferLength = m_ulBufferLength;
|
||
m_InAudioHeader[i]->dwFlags = 0;
|
||
m_InAudioHeader[i]->dwLoops = 0;
|
||
waveInPrepareHeader(m_hWaveIn, m_InAudioHeader[i], sizeof(WAVEHDR));
|
||
}
|
||
|
||
waveInAddBuffer(m_hWaveIn, m_InAudioHeader[m_nWaveInIndex], sizeof(WAVEHDR));
|
||
if (m_Thread!=NULL) {
|
||
ResumeThread(m_Thread);
|
||
}
|
||
waveInStart(m_hWaveIn); //录音
|
||
|
||
m_bIsWaveInUsed = TRUE;
|
||
|
||
return true;
|
||
}
|
||
|
||
LPBYTE CAudio::GetRecordBuffer(LPDWORD dwBufferSize)
|
||
{
|
||
//录音机
|
||
if(m_bIsWaveInUsed==FALSE && InitializeWaveIn()==FALSE) {
|
||
return NULL;
|
||
}
|
||
|
||
SetEvent(m_hStartRecord);
|
||
WaitForSingleObject(m_hEventWaveIn, INFINITE);
|
||
// 这里故意仍按 m_ulBufferLength(1000)整包返回,不用 dwBytesRecorded 裁短。
|
||
//
|
||
// waveIn 录 GSM610 时驱动只写整数个 65 字节块,1000 字节的缓冲实际只填 975,
|
||
// 尾部 25 字节是驱动从未写过的残留。看着像垃圾,但播放侧按块截断时会正好把
|
||
// 这 25 字节丢掉,所以它在线上是无害的;反过来,包长一旦小于 1000,老主控
|
||
// (v1.4.2 及更早)的 PlayBuffer 按 m_ulBufferLength 整块 memcpy 就会越界读,
|
||
// 读到的才是真垃圾。保持 1000 才能让新客户端连老主控。
|
||
*dwBufferSize = m_ulBufferLength;
|
||
return m_InAudioData[m_nWaveInIndex]; //返出真正数据
|
||
}
|
||
|
||
DWORD WINAPI CAudio::waveInCallBack(LPVOID lParam)
|
||
{
|
||
CAudio *This = (CAudio *)lParam;
|
||
|
||
MSG Msg;
|
||
|
||
while (GetMessage(&Msg, NULL, 0, 0)) {
|
||
if (This->m_bExit)
|
||
break;
|
||
if (Msg.message == MM_WIM_DATA) {
|
||
SetEvent(This->m_hEventWaveIn);
|
||
WaitForSingleObject(This->m_hStartRecord, INFINITE);
|
||
if (This->m_bExit)
|
||
break;
|
||
|
||
Sleep(1);
|
||
This->m_nWaveInIndex = 1 - This->m_nWaveInIndex;
|
||
|
||
//更新缓冲
|
||
MMRESULT mmResult = waveInAddBuffer(This->m_hWaveIn,
|
||
This->m_InAudioHeader[This->m_nWaveInIndex], sizeof(WAVEHDR));
|
||
if (mmResult != MMSYSERR_NOERROR)
|
||
break;
|
||
}
|
||
|
||
if (Msg.message == MM_WIM_CLOSE) {
|
||
break;
|
||
}
|
||
|
||
TranslateMessage(&Msg);
|
||
DispatchMessage(&Msg);
|
||
}
|
||
|
||
Mprintf("waveInCallBack end\n");
|
||
This->m_hThreadCallBack = false;
|
||
|
||
return 0XDEADAAAA;
|
||
}
|
||
|
||
BOOL CAudio::PlayBuffer(LPBYTE szBuffer, DWORD dwBufferSize)
|
||
{
|
||
if (!m_bIsWaveOutUsed && !InitializeWaveOut()) //1 音频格式 2 播音设备
|
||
return FALSE;
|
||
if (!szBuffer || !dwBufferSize)
|
||
return FALSE;
|
||
|
||
// 按包截断到整数个 65 字节块,余数直接丢弃 —— 不能攒到下一包。
|
||
//
|
||
// waveOut 只转换整块,末尾不足一块的字节既不转换也不保留。曾经的写法是把
|
||
// 余数攒起来跨包拼接(当成一条连续的流处理),在 Android 那种「包与包之间
|
||
// 连续」的上行流上是对的,却把既有 Windows 客户端的监听整条搞成了噪声:
|
||
// Windows 主机用 waveIn 录 GSM610,1000 字节的缓冲驱动只写 975 字节,尾部
|
||
// 25 字节是驱动从未写过的堆残留,客户端把这 1000 字节整包发上来。攒余数就
|
||
// 等于每 1000 字节往 GSM 流里插进 25 字节垃圾,块边界从第二包起全部错位。
|
||
// 按包截断则正好丢掉这 25 字节残留,和老版本行为一致。
|
||
//
|
||
// 代价是发送侧必须保证「一个包 = 整数个块」:Windows 端天然满足(驱动只写
|
||
// 整块),Android 端见 AudioHandler.h 的 975 字节整块打包。
|
||
DWORD n = (dwBufferSize / AUDIO_GSM_BLOCK) * AUDIO_GSM_BLOCK;
|
||
if (n == 0)
|
||
return FALSE;
|
||
|
||
// 一个包可能超过一块播放缓冲,分多次写。AUDIO_OUT_MAX 本身是 65 的整数倍,
|
||
// 所以每一段都是整块。
|
||
for (DWORD off = 0; off < n; off += AUDIO_OUT_MAX) {
|
||
DWORD len = n - off;
|
||
if (len > AUDIO_OUT_MAX)
|
||
len = AUDIO_OUT_MAX;
|
||
if (!WriteAligned(szBuffer + off, len))
|
||
break; // 设备忙:剩下的整块丢掉,丢整块不会破坏对齐
|
||
}
|
||
return TRUE;
|
||
}
|
||
|
||
// 把 len 字节(必须是 65 的整数倍)交给 waveOut。两个缓冲轮转,还在队列里的不能
|
||
// 复用:waveOutWrite 会返回 WAVERR_BUSY 且数据被静默丢弃,原实现忽略返回值,
|
||
// 丢包没有任何痕迹。
|
||
BOOL CAudio::WriteAligned(LPBYTE p, DWORD len)
|
||
{
|
||
if (!p || len == 0 || len > AUDIO_OUT_MAX ||
|
||
(len % AUDIO_GSM_BLOCK) != 0)
|
||
return FALSE;
|
||
|
||
LPWAVEHDR h = m_OutAudioHeader[m_nWaveOutIndex];
|
||
if (h->dwFlags & WHDR_INQUEUE)
|
||
return FALSE;
|
||
|
||
memcpy(m_OutAudioData[m_nWaveOutIndex], p, len);
|
||
h->dwBufferLength = len;
|
||
MMRESULT mmResult = waveOutWrite(m_hWaveOut, h, sizeof(WAVEHDR));
|
||
if (mmResult != MMSYSERR_NOERROR) {
|
||
Mprintf("waveOutWrite(%lu) failed: %u\n", len, (unsigned)mmResult);
|
||
return FALSE;
|
||
}
|
||
m_nWaveOutIndex = 1 - m_nWaveOutIndex;
|
||
return TRUE;
|
||
}
|
||
|
||
BOOL CAudio::InitializeWaveOut()
|
||
{
|
||
if (!waveOutGetNumDevs())
|
||
return FALSE;
|
||
|
||
for (int i = 0; i < 2; ++i)
|
||
memset(m_OutAudioData[i], 0, AUDIO_OUT_MAX); //声音数据
|
||
|
||
MMRESULT mmResult;
|
||
mmResult = waveOutOpen(&m_hWaveOut, (WORD)WAVE_MAPPER, &(m_GSMWavefmt.wfx), (LONG)0, (LONG)0, CALLBACK_NULL);
|
||
if (mmResult != MMSYSERR_NOERROR)
|
||
return false;
|
||
|
||
m_nWaveOutIndex = 0;
|
||
|
||
for (int i = 0; i < 2; ++i) {
|
||
m_OutAudioHeader[i]->lpData = (LPSTR)m_OutAudioData[i];
|
||
// 按最大容量 prepare;实际每次写入的长度在 WriteAligned 里按整块数设置,
|
||
// 只会更短,不会越过这块缓冲。
|
||
m_OutAudioHeader[i]->dwBufferLength = AUDIO_OUT_MAX;
|
||
m_OutAudioHeader[i]->dwFlags = 0;
|
||
m_OutAudioHeader[i]->dwLoops = 0;
|
||
waveOutPrepareHeader(m_hWaveOut, m_OutAudioHeader[i], sizeof(WAVEHDR));
|
||
}
|
||
|
||
m_bIsWaveOutUsed = TRUE;
|
||
return TRUE;
|
||
}
|