1

我尝试使用 WASAPI 为 windows phone8 制作一个简单的 VoIP 应用程序。在项目的某些运行中通过调用

hr = m_pCaptureClient->GetBuffer(&pbData, &nFrames, &dwFlags, nullptr, nullptr)

几分钟后 dwFlags 反复设置为 1 (AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY) 并且返回的数据包变得越来越小,并且在此期间口吃变得越来越多,但在项目的某些运行中,一切都很好,声音完全清晰。

有人可以帮忙吗?

  //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;
}

我知道代码在每次GetBuffer调用之间都做了很多工作,这可能是时间故障的主要问题,但我的问题是为什么有时会发生有时不会发生?

4

0 回答 0