void start_record_audio() { APP_LOG("<<<<<<start === record audio xp >>>>>>>>>>>>>>>>>>>");
pGraph_xp = NULL; HRESULT h_com_ret = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph_xp);
if(pGraph_xp == NULL) { APP_LOG("pGraph_xp is NULL ...."); return ; }
ICaptureGraphBuilder2 * pBuilder = NULL; h_com_ret = CoCreateInstance(CLSID_CaptureGraphBuilder2,0,CLSCTX_INPROC_SERVER,IID_ICaptureGraphBuilder2,(void**)&pBuilder); if(pBuilder) pBuilder->SetFiltergraph(pGraph_xp); else { APP_LOG("pBuilder is NULL ....%d",h_com_ret); return ; }
pMediaControl_xp = NULL; pGraph_xp->QueryInterface(IID_IMediaControl,(void**)&pMediaControl_xp); if(NULL == pMediaControl_xp) { APP_LOG("pMediaControl_xp is NULL ...."); return ; }
ICreateDevEnum *pDevEnum = NULL; h_com_ret = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **)&pDevEnum);
IEnumMoniker *pClassEnum = NULL; pDevEnum->CreateClassEnumerator(CLSID_AudioInputDeviceCategory, &pClassEnum, 0); IMoniker *pMoniker = NULL; ULONG cFetched; IBaseFilter *pSrc = NULL; if (pClassEnum && (pClassEnum->Next(1, &pMoniker, &cFetched) == S_OK)) { if(pMoniker) { pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSrc); pMoniker->Release(); } else { APP_LOG("pClassEnum is NULL ...."); } } else { APP_LOG("pClassEnum is NULL ...."); }
if(pClassEnum) pClassEnum->Release();
IBaseFilter *pWaveDest = NULL ,*pWriter = NULL; h_com_ret = CoCreateInstance(CLSID_WavDest, NULL, CLSCTX_ALL, IID_IBaseFilter, (void **)&pWaveDest); if(SUCCEEDED(h_com_ret)) {} else { APP_LOG("CoCreateInstance failed ...... %u",h_com_ret); }
h_com_ret = CoCreateInstance(CLSID_FileWriter, NULL, CLSCTX_ALL, IID_IBaseFilter, (void **)&pWriter); if(SUCCEEDED(h_com_ret)) {} else { APP_LOG("CoCreateInstance failed ...... %d",h_com_ret); }
if(pSrc) pGraph_xp->AddFilter(pSrc,L"Wav"); else { APP_LOG("pSrc is NULL ...."); }
if(pWaveDest) pGraph_xp->AddFilter(pWaveDest,L"WavDest"); else { APP_LOG("pWaveDest is NULL ...."); }
if(pWriter) pGraph_xp->AddFilter(pWriter,L"FileWriter"); else { APP_LOG("pWriter is NULL ...."); }
IFileSinkFilter2* pSink = NULL; pWriter->QueryInterface(IID_IFileSinkFilter2,(void**)&pSink);
if(pSink) { CString audio_fname = convert_string_ex(m_cap_fname_ex); pSink->SetFileName(audio_fname,NULL); pSink->SetMode(AM_FILE_OVERWRITE); } else { APP_LOG("pSink is NULL ...."); }
AM_MEDIA_TYPE type; type.majortype = MEDIATYPE_Stream; type.subtype =MEDIASUBTYPE_WAVE; type.formattype = FORMAT_None; type.bFixedSizeSamples = FALSE; type.bTemporalCompression = FALSE; type.pUnk = NULL;
// source filter, output pin IPin* pOutpin = FindPin(pSrc,PINDIR_OUTPUT);
IPin* pInpin = NULL,*pOut = NULL;
// transform filter, output pin pOut = FindPin_xp(pWaveDest,PINDIR_OUTPUT);
// transform filter, input pin pInpin = FindPin_xp(pWaveDest,PINDIR_INPUT);
// render filter, input pin IPin* pInpin1= FindPin_xp(pWriter,PINDIR_INPUT);
if(pOutpin && pInpin) pGraph_xp->ConnectDirect(pOutpin,pInpin,NULL); else { APP_LOG("pOutpin pInpin is NULL ...."); }
if(pOut && pInpin1) pGraph_xp->ConnectDirect(pOut,pInpin1,NULL); else { APP_LOG("pOut pInpin1 is NULL ...."); }
{ APP_LOG("START RECORD AV FILE ......"); } pMediaControl_xp->Run(); }
IPin* FindPin(IBaseFilter *pFilter, PIN_DIRECTION dir) { if(NULL == pFilter) return NULL;
IEnumPins* pEnumPins; IPin* pOutpin = NULL; PIN_DIRECTION pDir; pFilter->EnumPins(&pEnumPins); if(NULL == pEnumPins) return NULL;
while (pEnumPins->Next(1,&pOutpin,NULL)==S_OK) { if(pOutpin) pOutpin->QueryDirection(&pDir);
if (pDir == dir) { return pOutpin; } } return NULL; }