/// 设置音频信息 BOOL CAudioCapture::SetAudioFormat(ENUM_FREQUENCY_TYPE enFrequency, ENUM_CHANNEL_TYPE enChannel, ENUM_SAMPLE_TYPE enSample) { if(NULL != m_pCaptureFilter) { BOOL bResult = FALSE; do { IPin* pOutPin = GetOutputPin(m_pCaptureFilter, (uint16_t)0); if(NULL != pOutPin) { IAMBufferNegotiation *pNeg = NULL; IAMStreamConfig *pCfg = NULL; // Get buffer negotiation interface HRESULT hr = pOutPin->QueryInterface(IID_IAMBufferNegotiation, (void **)&pNeg); if (FAILED(hr)) { pOutPin->Release(); break; } // Find number of bytes in one second long lBytesPerSecond = (long) (enSample * enFrequency * enChannel); // 针对FAAC编码器 做出的调整 long lBufferSize = 1024 * enSample * enChannel; // Set the buffer size based on selected settings ALLOCATOR_PROPERTIES prop={0}; prop.cbBuffer = lBufferSize; prop.cBuffers = 6; prop.cbAlign = enSample * enChannel; hr = pNeg->SuggestAllocatorProperties(&prop); pNeg->Release(); // Now set the actual format of the audio data hr = pOutPin->QueryInterface(IID_IAMStreamConfig, (void **)&pCfg); if (FAILED(hr)) { pOutPin->Release(); break; } // Read current media type/format AM_MEDIA_TYPE *pmt={0}; hr = pCfg->GetFormat(&pmt); if (SUCCEEDED(hr)) { // Fill in values for the new format WAVEFORMATEX *pWF = (WAVEFORMATEX *) pmt->pbFormat; pWF->nChannels = (WORD) enChannel; pWF->nSamplesPerSec = enFrequency; pWF->nAvgBytesPerSec = lBytesPerSecond; pWF->wBitsPerSample = (WORD) (enSample * 8); pWF->nBlockAlign = (WORD) (enSample * enChannel); // Set the new formattype for the output pin hr = pCfg->SetFormat(pmt); UtilDeleteMediaType(pmt); } // Release interfaces pCfg->Release(); pOutPin->Release(); bResult = TRUE; } }while(FALSE); return bResult; } else { m_enFrequency = enFrequency; m_enChannel = enChannel; m_enSample = enSample; return TRUE; } }
BOOL CBoxView::APlaying() { if (m_pGraph == NULL) { return FALSE; } CComPtr<IBaseFilter> pAudioInputFilter; HRESULT hr = S_OK; //查找audio filter并加入graph hr = FindInputFilters((void**)&pAudioInputFilter, CLSID_AudioInputDeviceCategory); if (NULL == pAudioInputFilter) { TRACE(L"[SVC] CBoxView:: Could not create the Filter AudioInputFilter"); return FALSE; } CComPtr<IPin> pinIn; CComPtr<IAMAudioInputMixer> pPinMixer; GetUnconnectedPin(pAudioInputFilter, PINDIR_INPUT, &pinIn); hr = pinIn->QueryInterface(IID_IAMAudioInputMixer, (void **)&pPinMixer); if (SUCCEEDED(hr)) { hr = pPinMixer->put_Enable(TRUE); } hr = m_pGraph->AddFilter(pAudioInputFilter, L"ACapture"); //创建render filter并加入graph CComPtr <IBaseFilter> pAudioRenderer = NULL; hr = CoCreateInstance(CLSID_AudioRender, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pAudioRenderer); hr = m_pGraph->AddFilter(pAudioRenderer, L"Audio Renderer"); if (pAudioRenderer == NULL) { TRACE(L"[SVC] CBoxView:: Could not create the Filter AudioRenderer"); return FALSE; } //获取麦克风输出脚 CComPtr<IPin> pAudioOutput; CComPtr<IEnumPins> pEnum; pAudioInputFilter->EnumPins(&pEnum); hr = pEnum->Reset(); hr = pEnum->Next(1, &pAudioOutput, NULL); //设置麦克风输出脚 IAMStreamConfig *pCfg = NULL; hr = pAudioOutput->QueryInterface(IID_IAMStreamConfig, (void **)&pCfg); // Read current media type/format AM_MEDIA_TYPE *pmt = { 0 }; hr = pCfg->GetFormat(&pmt); WAVEFORMATEX *pWF = (WAVEFORMATEX *)pmt->pbFormat; // Release interfaces pCfg->Release(); //设置麦克风输出脚缓冲 IAMBufferNegotiation *pNeg; pAudioOutput->QueryInterface(IID_IAMBufferNegotiation, (void **)&pNeg); ALLOCATOR_PROPERTIES prop = { 0 }; prop.cbBuffer = pWF->nAvgBytesPerSec * 50 / 1000; prop.cBuffers = -1; prop.cbAlign = -1; prop.cbPrefix = -1; hr = pNeg->SuggestAllocatorProperties(&prop); pNeg->Release(); #if 0 //method 1:Render RUN hr = m_pCGB->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Audio, pAudioInputFilter, NULL, NULL); #else //method 2:Connect RUN hr = ConnectFilters(m_pGraph, pAudioOutput, pAudioRenderer); #endif return FAILED(hr); }