Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is the C++ code:

C++
#include <atlbase.h>

#include <atlimage.h>

#include <Dshow.h>
#pragma comment(lib, "Strmiids.lib")

#include "Qedit.h"

	void init_capture();

	CComPtr<IGraphBuilder> pGraph; //менеджер графа фильтров
	CComPtr<ICaptureGraphBuilder2> pBuild; //граф фильтра аудио\видео захвата

	CComPtr<IBaseFilter> pCapture; //аудиофильтр
	CComPtr<IBaseFilter> pAudioRenderer; //аудиофильтр

	CComPtr<ICreateDevEnum> pDevEnum;
	CComPtr<IEnumMoniker> pEnum;
	CComPtr<IMoniker> pMoniker;

	CComPtr<IMediaControl> pMediaControl;
	CComPtr<IMediaEventEx> pEvent;

	CComPtr<IBaseFilter> pAudioGrabberF;
    CComPtr<ISampleGrabber> pAudioGrabber;


C++
void CCapture_Audio_MFCDlg::init_capture()
{
	HWND parameter_hwnd = m_hWnd;



	HRESULT hr;

	{
		//Создание графа-фильтров - Create the Capture Graph Builder
		hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**)&pBuild );
		if (SUCCEEDED(hr))
		{
			//Создание менеджера графа фильтров - Create the Filter Graph Manager
			hr = CoCreateInstance(CLSID_FilterGraph, 0, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&pGraph);
			if (SUCCEEDED(hr))
			{
				//Initialize the Capture Graph Builder
				pBuild->SetFiltergraph(pGraph);

				// Find system device enumerator to find a video capture device.
				hr = CoCreateInstance(CLSID_SystemDeviceEnum, 0, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (LPVOID*)&pDevEnum);

				if(SUCCEEDED(hr))
				{

					//создает счетчик для определенной категории устройств
					hr = pDevEnum->CreateClassEnumerator(CLSID_AudioInputDeviceCategory, &pEnum, 0);

					if(SUCCEEDED(hr))
					{
						if( pEnum->Next(1, &pMoniker,0) == S_OK )
						{
							hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void **)&pCapture);

							if(SUCCEEDED(hr))
							{
								pGraph->AddFilter(pCapture, L"Capture Filter");

								//	CLSID_NullRenderer
								hr = CoCreateInstance(CLSID_AudioRender, 0, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&pAudioRenderer);
								if (SUCCEEDED(hr))
								{
									//	L"Null Filter"
									pGraph->AddFilter(pAudioRenderer, L"Audio Renderer Filter");

									// Create the Sample Grabber filter.
									hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
										IID_PPV_ARGS(&pAudioGrabberF));
									if (SUCCEEDED(hr))
									{
										hr = pGraph->AddFilter(pAudioGrabberF, L"Sample Grabber");
										if (SUCCEEDED(hr))
										{
											hr = pAudioGrabberF->QueryInterface(IID_ISampleGrabber, (void**)&pAudioGrabber);

											if (SUCCEEDED(hr))
											{
												AM_MEDIA_TYPE mt;
												ZeroMemory(&mt, sizeof(mt));
												mt.majortype = MEDIATYPE_Audio;
												mt.subtype = MEDIASUBTYPE_PCM;

												hr = pAudioGrabber->SetMediaType(&mt);
												
												if (SUCCEEDED(hr))
												{
													hr = pAudioGrabber->SetOneShot(TRUE);
													if (SUCCEEDED(hr))
													{

														hr = pAudioGrabber->SetBufferSamples(TRUE);
														if (SUCCEEDED(hr))
														{



															hr = pBuild->RenderStream(/*&PIN_CATEGORY_PREVIEW*/NULL, NULL/*&MEDIATYPE_Video*/, pCapture, pAudioGrabberF, pAudioRenderer);  //3,4,5 Параметры - фильтры, которые объединены в граф
															//hr = pBuild->RenderStream(/*&PIN_CATEGORY_PREVIEW*/NULL, NULL/*&MEDIATYPE_Video*/, pCapture, NULL, pAudioRenderer);  //3,4,5 Параметры - фильтры, которые объединены в граф

															HWND hWnd = parameter_hwnd;

															// Set the owner window to receive event notices.
															hr = pGraph->QueryInterface(IID_IMediaEventEx, (void **)&pEvent);
															hr = pEvent->SetNotifyWindow((OAHWND)hWnd, 0, 0);

															// Run the graph.
															hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl);
															hr = pMediaControl->Run();

															SaveGraphFile(pGraph, L"c:\\temp\\audio_capture.grf");
														}
													}
												}
												_FreeMediaType(mt);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}


Audio Capture does not work in the MFC application but works in GraphStudioNext.

Code with
C++
hr = pBuild->RenderStream(/*&PIN_CATEGORY_PREVIEW*/NULL, NULL/*&MEDIATYPE_Video*/, pCapture, NULL, pAudioRenderer);  //3,4,5 Параметры - фильтры, которые объединены в граф

instead of
C++
hr = pBuild->RenderStream(/*&PIN_CATEGORY_PREVIEW*/NULL, NULL/*&MEDIATYPE_Video*/, pCapture, pAudioGrabberF, pAudioRenderer);  //3,4,5 Параметры - фильтры, которые объединены в граф

works both in the application and in GraphStudioNext.
So with samplagrabber does not work.

init_capture(); is called in
C++
BOOL CCapture_Audio_MFCDlg::OnInitDialog()


C++
// Release the format block for a media type.

void _FreeMediaType(AM_MEDIA_TYPE& mt)
{
    if (mt.cbFormat != 0)
    {
        CoTaskMemFree((PVOID)mt.pbFormat);
        mt.cbFormat = 0;
        mt.pbFormat = NULL;
    }
    if (mt.pUnk != NULL)
    {
        // pUnk should not be used.
        mt.pUnk->Release();
        mt.pUnk = NULL;
    }
}


What is wrong in the code?
Posted
Updated 23-Dec-15 7:37am
v2
Comments
Сергей Козлов 23-Dec-15 12:25pm    
No errors are detected with "hr" variable.
There is no sound played, when i speak to microphone.
Сергей Козлов 23-Dec-15 13:41pm    
This is all the code of the application.
You are able to compile it easy and try to help me.
Of course if you are familiar with directshow and SampleGraber filter.
Сергей Козлов 23-Dec-15 13:42pm    
Here is the file Qedit.h:

///////////////////////////////////////////////////////////////////////////////////

#ifndef __qedit_h__
#define __qedit_h__

///////////////////////////////////////////////////////////////////////////////////

#pragma once

///////////////////////////////////////////////////////////////////////////////////

interface
ISampleGrabberCB
:
public IUnknown
{
virtual STDMETHODIMP SampleCB( double SampleTime, IMediaSample *pSample ) = 0;
virtual STDMETHODIMP BufferCB( double SampleTime, BYTE *pBuffer, long BufferLen ) = 0;
};

///////////////////////////////////////////////////////////////////////////////////

static
const
IID IID_ISampleGrabberCB = { 0x0579154A, 0x2B53, 0x4994, { 0xB0, 0xD0, 0xE7, 0x73, 0x14, 0x8E, 0xFF, 0x85 } };

///////////////////////////////////////////////////////////////////////////////////

interface
ISampleGrabber
:
public IUnknown
{
virtual HRESULT STDMETHODCALLTYPE SetOneShot( BOOL OneShot ) = 0;
virtual HRESULT STDMETHODCALLTYPE SetMediaType( const AM_MEDIA_TYPE *pType ) = 0;
virtual HRESULT STDMETHODCALLTYPE GetConnectedMediaType( AM_MEDIA_TYPE *pType ) = 0;
virtual HRESULT STDMETHODCALLTYPE SetBufferSamples( BOOL BufferThem ) = 0;
virtual HRESULT STDMETHODCALLTYPE GetCurrentBuffer( long *pBufferSize, long *pBuffer ) = 0;
virtual HRESULT STDMETHODCALLTYPE GetCurrentSample( IMediaSample **ppSample ) = 0;
virtual HRESULT STDMETHODCALLTYPE SetCallback( ISampleGrabberCB *pCallback, long WhichMethodToCallback ) = 0;
};

///////////////////////////////////////////////////////////////////////////////////

static
const
IID IID_ISampleGrabber = { 0x6B652FFF, 0x11FE, 0x4fce, { 0x92, 0xAD, 0x02, 0x66, 0xB5, 0xD7, 0xC7, 0x8F } };

///////////////////////////////////////////////////////////////////////////////////

static
const
CLSID CLSID_SampleGrabber = { 0xC1F400A0, 0x3F08, 0x11d3, { 0x9F, 0x0B, 0x00, 0x60, 0x08, 0x03, 0x9E, 0x37 } };

///////////////////////////////////////////////////////////////////////////////////

static
const
CLSID CLSID_NullRenderer = { 0xC1F400A4, 0x3F08, 0x11d3, { 0x9F, 0x0B, 0x00, 0x60, 0x08, 0x03, 0x9E, 0x37 } };

///////////////////////////////////////////////////////////////////////////////////

static
const
CLSID CLSID_VideoEffects1Category = { 0xcc7bfb42, 0xf175, 0x11d1, { 0xa3, 0x92, 0x0, 0xe0, 0x29, 0x1f, 0x39, 0x59 } };

///////////////////////////////////////////////////////////////////////////////////

static
const
CLSID CLSID_VideoEffects2Category = { 0xcc7bfb43, 0xf175, 0x11d1, { 0xa3, 0x92, 0x0, 0xe0, 0x29, 0x1f, 0x39, 0x59 } };

///////////////////////////////////////////////////////////////////////////////////

static
const
CLSID CLSID_AudioEffects1Category = { 0xcc7bfb44, 0xf175, 0x11d1, { 0xa3, 0x92, 0x0, 0xe0, 0x29, 0x1f, 0x39, 0x59 } };

///////////////////////////////////////////////////////////////////////////////////

static
const
CLSID CLSID_AudioEffects2Category = { 0xcc7bfb45, 0xf175, 0x11d1, { 0xa3, 0x92, 0x0, 0xe0, 0x29, 0x1f, 0x39, 0x59 } };

///////////////////////////////////////////////////////////////////////////////////

#endif

///////////////////////////////////////////////////////////////////////////////////
Сергей Козлов 23-Dec-15 13:43pm    
I use Visual Studio 2012 (version 11) with Windows 10 Professional operating system having installed Visual Studio 2015 Community.

1 solution

Here is the solution:
One should use FALSE as argument to this function to do not stop the graph to capture all the audio frames.
C++
hr = pAudioGrabber->SetOneShot(FALSE);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900