I try to make a simple VoIP app for windows phone8 using WASAPI.In some run of the project By Calling
hr = m_pCaptureClient->GetBuffer(&pbData, &nFrames, &dwFlags, nullptr, nullptr)
after some minutes dwFlags repeatedly set to 1 (AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY) and returned packet become smaller and smaller and stutter become more and more during the time but in some run of the project everything is fine and the voice is completely clear.
Can anybody help about it?
//MAX_RAW_BUFFER_SIZE 320
void BackEndAudio::CaptureThread(Windows::Foundation::IAsyncAction^ operation)
{
uint8_t amrEncodedData[13];
uint8_t SendingData[14];
HRESULT hr = m_pDefaultCaptureDevice->Start();
BYTE *pLocalBuffer = new BYTE[MAX_RAW_BUFFER_SIZE];
HANDLE eventHandles[] = {
hCaptureEvent, // WAIT_OBJECT0
hShutdownEvent // WAIT_OBJECT0 + 1
};
if (SUCCEEDED(hr) && pLocalBuffer)
{
unsigned int uAccumulatedBytes = 0;
while (SUCCEEDED(hr))
{
DWORD waitResult = WaitForMultipleObjectsEx(SIZEOF_ARRAY(eventHandles), eventHandles, FALSE, INFINITE, FALSE);
if (WAIT_OBJECT_0 == waitResult)
{
BYTE* pbData = nullptr;
UINT32 nFrames = 0;
DWORD dwFlags = 0;
if (SUCCEEDED(hr))
{
hr = m_pCaptureClient->GetBuffer(&pbData, &nFrames, &dwFlags, nullptr, nullptr);//Retrieves a pointer to the next available packet of data in the capture endpoint buffer.
unsigned int incomingBufferSize = nFrames * m_sourceFrameSizeInBytes;
if (*SendFrameNo==(uint8_t)256)
*SendFrameNo=0;
if(dwFlags==1)
glitch++;
while(MAX_RAW_BUFFER_SIZE - uAccumulatedBytes < incomingBufferSize)
{
memcpy(pLocalBuffer + uAccumulatedBytes, pbData,MAX_RAW_BUFFER_SIZE-uAccumulatedBytes);
if (transportController)
{
short *tempbuffer=new short[160];
for(int i=0;i<160;i++)
{
BToS.buf[0]=pLocalBuffer[i*2];
BToS.buf[1]=pLocalBuffer[i*2+1];
tempbuffer[i]=BToS.shbuf;
}
bool bRet = EasyVAD_IsSilence(hVAD,tempbuffer,160);
if(!bRet)
{
Encoder_Interface_Encode(amren, MR475, (const short *)pLocalBuffer, amrEncodedData, 0);
memcpy(SendingData,SendFrameNo,1);
memcpy(SendingData+1,amrEncodedData,13);
transportController->Write(SendingData, 14);
(*SendFrameNo)++;
}
}
pbData+=(MAX_RAW_BUFFER_SIZE-uAccumulatedBytes);
incomingBufferSize-=(MAX_RAW_BUFFER_SIZE-uAccumulatedBytes);
uAccumulatedBytes=0;
}
if(MAX_RAW_BUFFER_SIZE - uAccumulatedBytes == incomingBufferSize)
{
memcpy(pLocalBuffer + uAccumulatedBytes, pbData, incomingBufferSize);
if (transportController)
{
short *tempbuffer=new short[160];
for(int i=0;i<160;i++)
{
BToS.buf[0]=pLocalBuffer[i*2];
BToS.buf[1]=pLocalBuffer[i*2+1];
tempbuffer[i]=BToS.shbuf;
}
bool bRet = EasyVAD_IsSilence(hVAD,tempbuffer,160);
if(!bRet)
{
Encoder_Interface_Encode(amren, MR475, (const short *)pLocalBuffer, amrEncodedData, 0);
memcpy(SendingData,SendFrameNo,1);
memcpy(SendingData+1,amrEncodedData,13);
transportController->Write(SendingData, 14);
(*SendFrameNo)++;
}
}
// Reset our counter
uAccumulatedBytes = 0;
}
else if(MAX_RAW_BUFFER_SIZE - uAccumulatedBytes > incomingBufferSize)
{
memcpy(pLocalBuffer + uAccumulatedBytes, pbData, incomingBufferSize);
uAccumulatedBytes += incomingBufferSize;
}
}
if (SUCCEEDED(hr))
{
hr = m_pCaptureClient->ReleaseBuffer(nFrames);
}
}
else if (WAIT_OBJECT_0 + 1 == waitResult)
{
// We're being asked to shutdown
break;
}
else
{
// Unknown return value
DbgRaiseAssertionFailure();
}
}
}
delete[] pLocalBuffer;
}
I know that the code does to much work between each GetBuffer
call and this is probably the main issue of time glitches but my question is why sometimes it happens and sometimes not?