Changing sample rate for microphone/speakers in Windows 7 - c++

I need to change sample rate for microphone and speakers.
I know how to change volume levels for speakers and microphone using IMMDevice, but how can I set sample rate?
bool SoundDeviceControler::setVolume(EDataFlow dataFlow,float volume )
{
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
return false
IMMDeviceEnumerator *deviceEnumerator = NULL;
hr = CoCreateInstance( __uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&deviceEnumerator) );
if (FAILED(hr))
return false;
IMMDeviceCollection *deviceCollection = NULL;
hr = deviceEnumerator->EnumAudioEndpoints( dataFlow, DEVICE_STATE_ACTIVE | DEVICE_STATE_UNPLUGGED, &deviceCollection );
if (FAILED(hr))
return false;
UINT deviceCount;
hr = deviceCollection->GetCount(&deviceCount);
if (FAILED(hr))
return false;
IMMDevice *device = NULL;
for (UINT i = 0 ; i < deviceCount ; i += 1)
{
device = NULL;
hr = deviceCollection->Item(i, &device);
if (FAILED(hr))
return false;
IAudioEndpointVolume *endpointVolume = NULL;
hr = device->Activate( __uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, reinterpret_cast<void **>(&endpointVolume) );
if (FAILED(hr))
retur false;
hr=endpointVolume->SetMasterVolumeLevelScalar(volume,NULL);
if (FAILED(hr))
return false;
hr=endpointVolume->SetMute(false,NULL);
if (FAILED(hr))
return false;
}
return true;
}

Related

Getting bytes from GraphFilter SampleGrabber to Audio raw data WinApi

I am having a big issue getting raw audio data from the SampleGrabber GraphFilter and writing it to an audio file in the proper format.
What i'm trying to achieve is:
grabbing the data from: Microphone->SampleGrabber->Null Rendrer
Write the raw data captured to a proper audio file (MP3\AVI\WAVE)
Some questions:
Am I setting the proper MediaType for the SampleGrabber?
How can you compress the audio data to a proper audio file?
My current code below:
struct sRecortdingData {
std::vector<BYTE> recordingData;
double totalRecordingTime;
};
class CFakeCallback : public ISampleGrabberCB
{
public:
sRecortdingData sRecording;
STDMETHODIMP_(ULONG) AddRef() { return 2; }
STDMETHODIMP_(ULONG) Release() { return 1; }
STDMETHODIMP_(HRESULT __stdcall) QueryInterface(REFIID riid, void** ppv)
{
if (riid == IID_ISampleGrabberCB || riid == IID_IUnknown)
{
*ppv = (void*) static_cast<ISampleGrabberCB*>(this);
return NOERROR;
}
return E_NOINTERFACE;
}
STDMETHODIMP_(HRESULT __stdcall) SampleCB(double SampleTime, IMediaSample* pSample)
{
return S_OK;
}
STDMETHODIMP_(HRESULT __stdcall) BufferCB(double SampleTime, BYTE* pBuffer, long BufferLen)
{
sRecording.recordingData.push_back(*pBuffer);
sRecording.totalRecordingTime = SampleTime;
return S_OK;
}
};
void Microphone::RecordAudio()
{
HRESULT hr;
AM_MEDIA_TYPE mt;
long recordingEventCode;
IMediaControl* pMediaControl = NULL;
ISampleGrabber* pISampleGrabber = NULL;
IBaseFilter* pSampleGrabberFilter;
IGraphBuilder* pGraphBuilder = NULL;
IMediaEventEx* pEvent = NULL;
IBaseFilter* pMicrophoneFilter = NULL;
ISampleGrabber* pSampleGrabber = NULL;
ICreateDevEnum* pDeviceEnum = NULL;
IBaseFilter* pNullRender = NULL;
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
{
HR_Failed(hr);
return;
}
// Filter graph
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pGraphBuilder));
if (FAILED(hr))
{
HR_Failed(hr);
return;
}
// Device enum
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDeviceEnum));
if (FAILED(hr))
{
HR_Failed(hr);
return;
}
// Null renderer
hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pNullRender));
if (FAILED(hr))
{
HR_Failed(hr);
return;
}
// Get the even control
hr = pGraphBuilder->QueryInterface(IID_PPV_ARGS(&pEvent));
if (FAILED(hr))
{
HR_Failed(hr);
return;
}
// Sample Grabber
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSampleGrabberFilter));
if (FAILED(hr))
{
HR_Failed(hr);
return;
}
// Get Media Control
hr = pGraphBuilder->QueryInterface(IID_PPV_ARGS(&pMediaControl));
if (FAILED(hr))
{
HR_Failed(hr);
return;
}
// Setup input device filter
pMicrophoneFilter = Microphone::SetupDeviceFilter(CLSID_AudioInputDeviceCategory, pGraphBuilder, pDeviceEnum,
Utils::s2ws("My Microphone");
if (pMicrophoneFilter == NULL) {
HR_Failed(hr);
return;
}
// Setup sample grabber filter
pSampleGrabberFilter = Microphone::SetupDeviceFilter(CLSID_LegacyAmFilterCategory,
pGraphBuilder, pDeviceEnum, L"SampleGrabber");
if (pSampleGrabberFilter == NULL) {
HR_Failed(hr);
return;
}
// Connect both pins together
Device_Connect(pMicrophoneFilter, pSampleGrabberFilter);
// Setup null renderer filter
pNullRender = Microphone::SetupDeviceFilter(CLSID_LegacyAmFilterCategory,
pGraphBuilder, pDeviceEnum, L"Null Renderer");
if (pNullRender == NULL) {
HR_Failed(hr);
return;
}
// Connect both pins together
Device_Connect(pSampleGrabberFilter, pNullRender);
// Get the ISampleGranner interface
hr = pSampleGrabberFilter->QueryInterface(IID_ISampleGrabber, (LPVOID*)&pISampleGrabber);
if (FAILED(hr))
{
HR_Failed(hr);
return;
}
hr = pISampleGrabber->SetCallback(&CB, 1);
ZeroMemory(&mt, sizeof(mt));
mt.majortype = MEDIATYPE_Audio;
mt.subtype = MEDIASUBTYPE_PCM;
// Set the media type
hr = pISampleGrabber->SetMediaType(&mt);
if (FAILED(hr))
{
HR_Failed(hr);
return;
}
pISampleGrabber->SetBufferSamples(FALSE);
if (FAILED(hr))
{
HR_Failed(hr);
return;
}
hr = pMediaControl->Run();
if (FAILED(hr))
{
HR_Failed(hr);
return;
}
DWORD recordingTime = 20000;
hr = pEvent->WaitForCompletion(recordingTime, &recordingEventCode);
std::string str(CB.sRecording.recordingData.begin(), CB.sRecording.recordingData.end());
// Do something with the data from callback and write it to audio file
}

Why do I get a success code when I start a task, but it does nothing?

Please help me to solve a problem. I'm trying to copy a system RegIdleBackup task and launch the copied task. I successfully copied task with ITaskService api, and change some settings in it. And after all i tried to Run this task, and I got return code S_OK, but task info didn't changed in Task Scheduler window and backup files didn't update. I can manually run this task from task sheduler and my copied task works good. My problem is with launch method, but i can't find it. Thank you.
Here is my code
HRESULT GetTask(ITaskService* pService, const std::wstring& fullTaskName, IRegisteredTask** pTask)
{
ITaskFolder* pFolder = nullptr;
HRESULT hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
return hr;
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
if (FAILED(hr))
return hr;
hr = pFolder->GetTask(SysAllocString(L"RegIdleBackup"), pTask);
pFolder->Release();
return hr;
}
HRESULT CreateTaskCopy(ITaskService* pService, IRegisteredTask* pTask, const std::wstring& fullTaskName)
{
ITaskFolder* pFolder = nullptr;
HRESULT hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
return hr;
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
ITaskDefinition* pTaskDef = nullptr;
ITaskSettings* pTaskSettings = nullptr;
IRegisteredTask* iTask = nullptr;
hr = pTask->get_Definition(&pTaskDef);
if (FAILED(hr))
goto exit;
hr = pTaskDef->get_Settings(&pTaskSettings);
if (FAILED(hr))
goto exit;
hr = pTaskSettings->put_AllowDemandStart(_variant_t(TRUE));
if (FAILED(hr))
goto exit;
hr = pTaskSettings->put_MultipleInstances(TASK_INSTANCES_PARALLEL);
if (FAILED(hr))
goto exit;
hr = pTaskDef->put_Settings(pTaskSettings);
if (FAILED(hr))
goto exit;
pTaskSettings->Release();
pTaskSettings = nullptr;
hr = pFolder->DeleteTask(_bstr_t(taskName.c_str()), 0);
hr = pFolder->RegisterTaskDefinition(_bstr_t(taskName.c_str()),
pTaskDef,
TASK_CREATE_OR_UPDATE,
_variant_t(),
_variant_t(),
TASK_LOGON_SERVICE_ACCOUNT,
_variant_t(L""),
&iTask);
exit:
if (pTaskSettings)
pTaskSettings->Release();
if (iTask)
iTask->Release();
if (pTaskDef)
pTaskDef->Release();
return hr;
}
HRESULT CopyTask(const std::wstring& fullTaskName, const std::wstring& copyName)
{
ITaskService* pService = nullptr;
IRegisteredTask* pTask = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
hr = GetTask(pService, fullTaskName, &pTask);
if (FAILED(hr))
goto exit;
hr = CreateTaskCopy(pService, pTask, copyName);
exit:
if(pService)
pService->Release();
if (pTask)
pTask->Release();
return hr;
}
HRESULT StartTask(const std::wstring& fullTaskName)
{
ITaskService* pService = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
{
pService->Release();
return hr;
}
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
ITaskFolder* pFolder = nullptr;
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
IRegisteredTask* pTask = nullptr;
pFolder->GetTask(_bstr_t(taskName.c_str()), &pTask);
pFolder->Release();
IRunningTask* pRunningTask = nullptr;
hr = pTask->RunEx(_variant_t(),0,0, _bstr_t(L"S-1-5-18"), &pRunningTask);
pTask->Release();
return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr))
return hr;
hr = CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, 0, nullptr);
if (FAILED(hr))
return hr;
hr = CopyTask(L"\\Microsoft\\Windows\\Registry\\RegIdleBackup", L"\\Microsoft\\Windows\\Registry\\MyTask");
hr = StartTask(L"\\Microsoft\\Windows\\Registry\\MyTask");
CoUninitialize();
return hr;
}
Today morning I tried to run task with replacing launch trigger, and same time fixed some task settings. And all worked good, and after all I started to test my program on Win 7 and all were great, but on Win10 i got some interesting issue, task created with another last launch time (on Win7 field empty, on Win 10 message - "Task never launched before") and i tried to use IRegisteredTask::Run method, and it's worked! I tested on other OSes and run method working. I paste my code sample, maybe it canbe usefull for somebody.
HRESULT GetTaskDefinition(ITaskService* pService, const std::wstring& fullTaskName, ITaskDefinition** pTask)
{
ITaskFolder* pFolder = nullptr;
IRegisteredTask* pRegTask = nullptr;
HRESULT hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
return hr;
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
if (FAILED(hr))
return hr;
hr = pFolder->GetTask(SysAllocString(L"RegIdleBackup"), &pRegTask);
pFolder->Release();
if (FAILED(hr))
return hr;
hr = pRegTask->get_Definition(pTask);
return hr;
}
HRESULT ChangeTaskSettings(ITaskDefinition* pTaskDef)
{
ITaskSettings* pTaskSettings = nullptr;
HRESULT hr = pTaskDef->get_Settings(&pTaskSettings);
if (FAILED(hr))
return hr;
hr = pTaskSettings->put_AllowDemandStart(VARIANT_TRUE);
hr = pTaskSettings->put_MultipleInstances(TASK_INSTANCES_PARALLEL);
hr = pTaskSettings->put_RunOnlyIfIdle(VARIANT_FALSE);
hr = pTaskSettings->put_StartWhenAvailable(VARIANT_TRUE);
hr = pTaskSettings->put_StopIfGoingOnBatteries(VARIANT_FALSE);
hr = pTaskSettings->put_Enabled(VARIANT_FALSE);
hr = pTaskSettings->put_DisallowStartIfOnBatteries(VARIANT_FALSE);
hr = pTaskDef->put_Settings(pTaskSettings);
pTaskSettings->Release();
return hr;
}
FILETIME AddSecondsToFIleTime(FILETIME ft, unsigned seconds)
{
FILETIME res;
#define _SECONDS ((__int64) 10000000) //100 ns intervals in second
ULONGLONG tmp = (static_cast<ULONGLONG>(ft.dwHighDateTime) << 32) + ft.dwLowDateTime + seconds*_SECONDS;
res.dwLowDateTime = static_cast<DWORD>(tmp & 0xFFFFFFFF);
res.dwHighDateTime = static_cast<DWORD>(tmp >> 32);
return res;
}
std::wstring GetTaskLaunchTimeWstring(SYSTEMTIME st, unsigned wait_interval)
{
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
ft = AddSecondsToFIleTime(ft, wait_interval);
FileTimeToSystemTime(&ft, &st);
std::stringstream ss;
ss << std::setfill('0') << std::setw(2) << st.wYear << "-" <<
std::setfill('0') << std::setw(2) << st.wMonth << "-" <<
std::setfill('0') << std::setw(2) << st.wDay << "T" <<
std::setfill('0') << std::setw(2) << st.wHour << ":" <<
std::setfill('0') << std::setw(2) << st.wMinute << ":" <<
std::setfill('0') << std::setw(2) << st.wSecond;
std::string str = ss.str();
return std::wstring(str.begin(), str.end());
}
HRESULT ChangeTaskTriggers(ITaskDefinition* pTaskDef, const std::wstring& LaunchTimeStr)
{
ITriggerCollection* pTriggersCollection = nullptr;
HRESULT hr = pTaskDef->get_Triggers(&pTriggersCollection);
hr = pTriggersCollection->Clear();
ITrigger* pTrigger = nullptr;
hr = pTriggersCollection->Create(TASK_TRIGGER_TIME, &pTrigger);
pTriggersCollection->Release();
if (FAILED(hr))
return hr;
ITimeTrigger* pTimeTrigger = nullptr;
hr = pTrigger->QueryInterface(IID_ITimeTrigger, reinterpret_cast<void**>(&pTimeTrigger));
pTrigger->Release();
if (FAILED(hr))
return hr;
hr = pTimeTrigger->put_Id(_bstr_t(L"Trigger"));
hr = pTimeTrigger->put_Enabled(VARIANT_TRUE);
hr = pTimeTrigger->put_StartBoundary(_bstr_t(LaunchTimeStr.c_str()));
pTimeTrigger->Release();
return hr;
}
HRESULT RegisterTask(ITaskService* pService, ITaskDefinition* pTaskDef, const std::wstring& fullTaskName)
{
ITaskFolder* pFolder = nullptr;
HRESULT hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
return hr;
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
IRegisteredTask* pRegisteredTAsk = nullptr;
hr = pFolder->DeleteTask(_bstr_t(taskName.c_str()), 0);
hr = pFolder->RegisterTaskDefinition(_bstr_t(taskName.c_str()),
pTaskDef,
TASK_CREATE_OR_UPDATE,
_variant_t(),
_variant_t(),
TASK_LOGON_SERVICE_ACCOUNT,
_variant_t(L""),
&pRegisteredTAsk);
if (SUCCEEDED(hr))
{
pRegisteredTAsk->put_Enabled(VARIANT_TRUE);
pRegisteredTAsk->Release();
}
return hr;
}
HRESULT CreateModifiedTaskCopy(const std::wstring& fullTaskName, const std::wstring& copyName, unsigned seconds_to_start)
{
ITaskService* pService = nullptr;
ITaskDefinition* pTaskDef = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
hr = GetTaskDefinition(pService, fullTaskName, &pTaskDef);
if (FAILED(hr))
{
pService->Release();
return hr;
}
hr = ChangeTaskSettings(pTaskDef);
SYSTEMTIME st;
GetLocalTime(&st);
const std::wstring launchStr = GetTaskLaunchTimeWstring(st, seconds_to_start);
hr = ChangeTaskTriggers(pTaskDef, launchStr);
hr = RegisterTask(pService, pTaskDef, copyName);
pTaskDef->Release();
pService->Release();
return hr;
}
HRESULT WaitUntilTaskCompleted(const std::wstring& fullTaskName, unsigned taskTimeout, unsigned maxWaitTime)
{
ITaskService* pService = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
{
pService->Release();
return hr;
}
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
ITaskFolder* pFolder = nullptr;
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
IRegisteredTask* pTask = nullptr;
hr = pFolder->GetTask(_bstr_t(taskName.c_str()), &pTask);
if (FAILED(hr))
{
pFolder->Release();
return hr;
}
DATE lastRun;
unsigned tries = 0;
do
{
Sleep(1000);
hr = pTask->get_LastRunTime(&lastRun);
tries++;
if (tries > maxWaitTime)
break;
} while (hr == SCHED_S_TASK_HAS_NOT_RUN);
return tries > maxWaitTime? SCHED_E_INVALID_TASK :S_OK;
}
HRESULT DeleteTask(const std::wstring& fullTaskName)
{
ITaskService* pService = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
{
pService->Release();
return hr;
}
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
ITaskFolder* pFolder = nullptr;
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
hr = pFolder->DeleteTask(_bstr_t(taskName.c_str()), 0);
pFolder->Release();
return hr;
}
HRESULT RunTask(const std::wstring& fullTaskName)
{
ITaskService* pService = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
{
pService->Release();
return hr;
}
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
ITaskFolder* pFolder = nullptr;
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
IRegisteredTask* pTask = nullptr;
hr = pFolder->GetTask(_bstr_t(taskName.c_str()), &pTask);
pFolder->Release();
IRunningTask* pRunTask = nullptr;
pTask->Run(_variant_t(), &pRunTask);
pTask->Release();
pRunTask->Release();
return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr))
return hr;
hr = CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, 0, nullptr);
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
const unsigned seconds_to_start = 30;
hr = CreateModifiedTaskCopy(L"\\Microsoft\\Windows\\Registry\\RegIdleBackup", L"\\Microsoft\\Windows\\Registry\\MyTask", seconds_to_start);
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
RunTask(L"\\Microsoft\\Windows\\Registry\\MyTask");
hr = WaitUntilTaskCompleted(L"\\Microsoft\\Windows\\Registry\\MyTask", seconds_to_start, seconds_to_start * 4);
hr = DeleteTask(L"\\Microsoft\\Windows\\Registry\\MyTask");
CoUninitialize();
printf("All ok!\r\n");
return hr;
}

How to grab all frames from an avi file - how to modify MS Sample Grabber sample

I have created a sample based on this MS link:
https://learn.microsoft.com/en-us/windows/win32/directshow/using-the-sample-grabber
Code appears below.
But the code only grabs one frame. I think the very first frame in the video But I want to grab every frame available in the video file. How do I do that?
/*
for this sample to work you have to have
"C:\\temp\\FlickAnimation.avi"
and it will output the first frame to grab1.bmp
work out how to do all frames.
qedit.h from here:
https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/2ab5c212-5824-419d-b5d9-7f5db82f57cd/qedith-missing-in-current-windows-sdk-v70?forum=windowsdirectshowdevelopment
dshowutil.h from here:
https://raw.githubusercontent.com/microsoft/Windows-classic-samples/master/Samples/Win7Samples/multimedia/directshow/common/dshowutil.h
*/
#include <windows.h>
#include <dshow.h> // DirectShow main header
#include "qedit.h" // from Microsoft
#include <strmif.h> // for IMediaSample
#include <combaseapi.h> // IID_PPV_ARGS
#include "dshowutil.h" // from Microsoft
#pragma comment(lib, "strmiids.lib")
#pragma comment(lib, "dxguid.lib")
template <class T> void SafeRelease(T **ppT)
{
if (*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}
HRESULT WriteBitmap(PCWSTR, BITMAPINFOHEADER*, size_t, BYTE*, size_t);
HRESULT GrabVideoBitmap(PCWSTR pszVideoFile, PCWSTR pszBitmapFile)
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEventEx *pEvent = NULL;
IBaseFilter *pGrabberF = NULL;
ISampleGrabber *pGrabber = NULL;
IBaseFilter *pSourceF = NULL;
IEnumPins *pEnum = NULL;
IPin *pPin = NULL;
IBaseFilter *pNullF = NULL;
BYTE *pBuffer = NULL;
HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pGraph));
if (FAILED(hr))
{
goto done;
}
hr = pGraph->QueryInterface(IID_PPV_ARGS(&pControl));
if (FAILED(hr))
{
goto done;
}
hr = pGraph->QueryInterface(IID_PPV_ARGS(&pEvent));
if (FAILED(hr))
{
goto done;
}
// Create the Sample Grabber filter.
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pGrabberF));
if (FAILED(hr))
{
goto done;
}
hr = pGraph->AddFilter(pGrabberF, L"Sample Grabber");
if (FAILED(hr))
{
goto done;
}
hr = pGrabberF->QueryInterface(IID_PPV_ARGS(&pGrabber));
if (FAILED(hr))
{
goto done;
}
AM_MEDIA_TYPE mt;
ZeroMemory(&mt, sizeof(mt));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
hr = pGrabber->SetMediaType(&mt);
if (FAILED(hr))
{
goto done;
}
hr = pGraph->AddSourceFilter(pszVideoFile, L"Source", &pSourceF);
if (FAILED(hr))
{
goto done;
}
hr = pSourceF->EnumPins(&pEnum);
if (FAILED(hr))
{
goto done;
}
while (S_OK == pEnum->Next(1, &pPin, NULL))
{
hr = ConnectFilters(pGraph, pPin, pGrabberF);
SafeRelease(&pPin);
if (SUCCEEDED(hr))
{
break;
}
}
if (FAILED(hr))
{
goto done;
}
hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pNullF));
if (FAILED(hr))
{
goto done;
}
hr = pGraph->AddFilter(pNullF, L"Null Filter");
if (FAILED(hr))
{
goto done;
}
hr = ConnectFilters(pGraph, pGrabberF, pNullF);
if (FAILED(hr))
{
goto done;
}
hr = pGrabber->SetOneShot(FALSE); // TRUE);
if (FAILED(hr))
{
goto done;
}
hr = pGrabber->SetBufferSamples(TRUE);
if (FAILED(hr))
{
goto done;
}
hr = pControl->Run();
if (FAILED(hr))
{
goto done;
}
long evCode;
hr = pEvent->WaitForCompletion(INFINITE, &evCode);
// Find the required buffer size.
long cbBuffer;
hr = pGrabber->GetCurrentBuffer(&cbBuffer, NULL);
if (FAILED(hr))
{
goto done;
}
pBuffer = (BYTE*)CoTaskMemAlloc(cbBuffer);
if (!pBuffer)
{
hr = E_OUTOFMEMORY;
goto done;
}
hr = pGrabber->GetCurrentBuffer(&cbBuffer, (long*)pBuffer);
if (FAILED(hr))
{
goto done;
}
hr = pGrabber->GetConnectedMediaType(&mt);
if (FAILED(hr))
{
goto done;
}
// Examine the format block.
if ((mt.formattype == FORMAT_VideoInfo) &&
(mt.cbFormat >= sizeof(VIDEOINFOHEADER)) &&
(mt.pbFormat != NULL))
{
VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)mt.pbFormat;
hr = WriteBitmap(pszBitmapFile, &pVih->bmiHeader,
mt.cbFormat - SIZE_PREHEADER, pBuffer, cbBuffer);
}
else
{
// Invalid format.
hr = VFW_E_INVALIDMEDIATYPE;
}
FreeMediaType(mt);
done:
CoTaskMemFree(pBuffer);
SafeRelease(&pPin);
SafeRelease(&pEnum);
SafeRelease(&pNullF);
SafeRelease(&pSourceF);
SafeRelease(&pGrabber);
SafeRelease(&pGrabberF);
SafeRelease(&pControl);
SafeRelease(&pEvent);
SafeRelease(&pGraph);
return hr;
};
// Writes a bitmap file
// pszFileName: Output file name.
// pBMI: Bitmap format information (including pallete).
// cbBMI: Size of the BITMAPINFOHEADER, including palette, if present.
// pData: Pointer to the bitmap bits.
// cbData Size of the bitmap, in bytes.
HRESULT WriteBitmap(PCWSTR pszFileName, BITMAPINFOHEADER *pBMI, size_t cbBMI,
BYTE *pData, size_t cbData)
{
HANDLE hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, 0, NULL);
if (hFile == NULL)
{
return HRESULT_FROM_WIN32(GetLastError());
}
BITMAPFILEHEADER bmf = {};
bmf.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
bmf.bfSize = cbBMI + cbData + sizeof(bmf);
bmf.bfOffBits = sizeof(bmf) + cbBMI;
DWORD cbWritten = 0;
BOOL result = WriteFile(hFile, &bmf, sizeof(bmf), &cbWritten, NULL);
if (result)
{
result = WriteFile(hFile, pBMI, cbBMI, &cbWritten, NULL);
}
if (result)
{
result = WriteFile(hFile, pData, cbData, &cbWritten, NULL);
}
HRESULT hr = result ? S_OK : HRESULT_FROM_WIN32(GetLastError());
CloseHandle(hFile);
return hr;
}
int main() {
// Initialize the COM library.
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return 1;
}
GrabVideoBitmap(L"C:\\temp\\FlickAnimation.avi", L"grab1.bmp");
CoUninitialize();
}
Idea behind solution:
Try opening the video with OpenCV, then use it's helper functions to read it frame-by-frame, saving the frame to images.
Code sample:
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(){
// Create a VideoCapture object and open the input file
// If the input is the web camera, pass 0 instead of the video file name
VideoCapture cap("chaplin.mp4");
// Check if camera opened successfully
if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
int counter = 0;
while(1){
Mat frame;
// Capture frame-by-frame
cap >> frame;
// If the frame is empty, break immediately
if (frame.empty())
break;
// Display the resulting frame
//imshow( "Frame", frame );
//Save the resulting frame
imwrite( "FilePathAndName" + std::to_string( counter ), frame );
counter++;
// Press ESC on keyboard to exit
char c=(char)waitKey(25);
if(c==27)
break;
}
// When everything done, release the video capture object
cap.release();
// Closes all the frames
destroyAllWindows();
return 0;
}
Hope this helps! ;)

What is this Sidetone Setting?

I'm using Window's Core Audio API (https://learn.microsoft.com/en-us/windows/desktop/CoreAudio/core-audio-apis-in-windows-vista) for controlling audio settings for my audio processing/analysis application. Below is my modified version of WalkTreeBackwardsFromPart. I can control the sidetone setting from the API, but I don't know what it actually does. If it does what I think it does, it could be useful to me. It does not seem to do the exact same thing as "listen to this device" setting. See line 204 for where I turn off the setting. Thanks.
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <mmdeviceapi.h>
#include <devicetopology.h>
#include <vector>
using namespace std;
struct AudioNode
{
LPWSTR name;
AudioNode *parent;
vector<AudioNode *>children;
};
AudioNode *newAudioNode(LPWSTR name) {
AudioNode *temp = new AudioNode;
temp->name = name;
temp->parent = NULL;
return temp;
}
AudioNode *audioTreeHead;
//AudioNode *audioTreeCurrNode;
HRESULT WalkTreeBackwardsFromPart(IPart *pPart, EDataFlow deviceType, AudioNode *audioTreeCurrNode, int iTabLevel = 0);
//HRESULT DisplayVolume(IAudioVolumeLevel *pVolume, int iTabLevel);
HRESULT DisplayMute(IAudioMute *pMute, int iTabLevel);
int SetDeviceVolume(EDataFlow deviceType, float vol);
void Tab(int iTabLevel);
int __cdecl main(void) {
//float volumes[] = {0, 5, 10, 15}
printf("capture\n");
int result = SetDeviceVolume(eCapture, -20);
printf("render\n");
result = SetDeviceVolume(eRender, -20);
system("pause");
return 0;
}
//HRESULT DisplayVolume(IAudioVolumeLevel *pVolume, int iTabLevel) {
//
//}
int SetDeviceVolume(EDataFlow deviceType, float vol) {
audioTreeHead = newAudioNode(L"");
AudioNode *audioTreeCurrNode = audioTreeHead;
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr)) {
printf("Failed CoInitializeEx: hr = 0x%08x\n", hr);
return __LINE__;
}
// get default render endpoint
IMMDeviceEnumerator *pEnum = NULL;
hr = CoCreateInstance(
__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
(void**)&pEnum
);
if (FAILED(hr)) {
printf("Couldn't get device enumerator: hr = 0x%08x\n", hr);
CoUninitialize();
return __LINE__;
}
IMMDevice *pDevice = NULL;
hr = pEnum->GetDefaultAudioEndpoint(deviceType, eConsole, &pDevice);
if (FAILED(hr)) {
printf("Couldn't get default capture device: hr = 0x%08x\n", hr);
pEnum->Release();
CoUninitialize();
return __LINE__;
}
pEnum->Release();
// get device topology object for that endpoint
IDeviceTopology *pDT = NULL;
hr = pDevice->Activate(__uuidof(IDeviceTopology), CLSCTX_ALL, NULL, (void**)&pDT);
if (FAILED(hr)) {
printf("Couldn't get device topology object: hr = 0x%08x\n", hr);
pDevice->Release();
CoUninitialize();
return __LINE__;
}
pDevice->Release();
// get the single connector for that endpoint
IConnector *pConnEndpoint = NULL;
hr = pDT->GetConnector(0, &pConnEndpoint);
if (FAILED(hr)) {
printf("Couldn't get the connector on the endpoint: hr = 0x%08x\n", hr);
pDT->Release();
CoUninitialize();
return __LINE__;
}
pDT->Release();
// get the connector on the device that is
// connected to
// the connector on the endpoint
IConnector *pConnDevice = NULL;
hr = pConnEndpoint->GetConnectedTo(&pConnDevice);
if (FAILED(hr)) {
printf("Couldn't get the connector on the device: hr = 0x%08x\n", hr);
pConnEndpoint->Release();
CoUninitialize();
return __LINE__;
}
pConnEndpoint->Release();
// QI on the device's connector for IPart
IPart *pPart = NULL;
hr = pConnDevice->QueryInterface(__uuidof(IPart), (void**)&pPart);
if (FAILED(hr)) {
printf("Couldn't get the part: hr = 0x%08x\n", hr);
pConnDevice->Release();
CoUninitialize();
return __LINE__;
}
pConnDevice->Release();
// all the real work is done in this function
hr = WalkTreeBackwardsFromPart(pPart, deviceType, audioTreeCurrNode);
if (FAILED(hr)) {
printf("Couldn't walk the tree: hr = 0x%08x\n", hr);
pPart->Release();
CoUninitialize();
return __LINE__;
}
pPart->Release();
CoUninitialize();
return 0;
}
HRESULT WalkTreeBackwardsFromPart(IPart *pPart, EDataFlow deviceType, AudioNode *audioTreeCurrNode, int iTabLevel /* = 0 */) {
HRESULT hr = S_OK;
LPWSTR pwszPartName = NULL;
hr = pPart->GetName(&pwszPartName);
if (FAILED(hr)) {
Tab(iTabLevel);
printf("Could not get part name: hr = 0x%08x\n", hr);
return hr;
}
Tab(iTabLevel);
printf("Part name: %ws\n", *pwszPartName ? pwszPartName : L"(Unnamed)");
AudioNode * tempNode = newAudioNode(pwszPartName);
//CoTaskMemFree(pwszPartName);
tempNode->parent = audioTreeCurrNode;
audioTreeCurrNode->children.push_back(tempNode);
audioTreeCurrNode = tempNode;
audioTreeCurrNode->name = pwszPartName;
if (audioTreeCurrNode->parent != NULL) {
Tab(iTabLevel);
printf("my parent: %ws\n", audioTreeCurrNode->parent->name);
if (audioTreeCurrNode->parent->parent != NULL) {
Tab(iTabLevel);
printf("my parent of parent: %ws\n", audioTreeCurrNode->parent->parent->name);
}
}
else {
Tab(iTabLevel);
printf("parent is NULL!\n");
}
if (wcscmp(audioTreeCurrNode->name, L"Mute") == 0 &&
audioTreeCurrNode->parent != NULL && audioTreeCurrNode->parent->parent != NULL &&
wcscmp(audioTreeCurrNode->parent->name, L"Volume") == 0 &&
wcscmp(audioTreeCurrNode->parent->parent->name, L"SuperMix") == 0) {
Tab(iTabLevel);
printf("found the mute I want!\n");
Tab(iTabLevel);
printf("parent: %ws\n", audioTreeCurrNode->parent->name);
Tab(iTabLevel);
printf("parent of parent: %ws\n", audioTreeCurrNode->parent->parent->name);
Tab(iTabLevel);
printf("muting passthrough...\n");
const IID IID_IAudioMute = __uuidof(IAudioMute);
IAudioMute *muteControl = NULL;
hr = pPart->Activate(CLSCTX_ALL, IID_IAudioMute, (void**)&muteControl);
if (E_NOINTERFACE == hr) {
Tab(iTabLevel);
printf("NO MUTE CONTROL\n");
}
else if (FAILED(hr)) {
Tab(iTabLevel);
printf("Unexpected failure trying to activate IAudioMute : hr = 0x%08x\n", hr);
return hr;
}
else {
Tab(iTabLevel);
printf("HAS MUTE CONTROL\n");
//LPCGUID pguidEventContext;
BOOL muted;
muteControl->SetMute(TRUE, NULL);
muteControl->GetMute(&muted);
Tab(iTabLevel);
printf("%s\n", muted ? "MUTED" : "NOT MUTED");
}
}
// Check AGC settings
const IID IID_IAudioAutoGainControl = __uuidof(IAudioAutoGainControl);
IAudioAutoGainControl *aGCcontrol = NULL;
hr = pPart->Activate(CLSCTX_ALL, IID_IAudioAutoGainControl, (void**)&aGCcontrol);
if (E_NOINTERFACE == hr) {
Tab(iTabLevel);
printf("NO AGC CONTROL\n");
// not a Microphone node
}
else if (FAILED(hr)) {
Tab(iTabLevel);
printf("Unexpected failure trying to activate IAudioAutoGainControl : hr = 0x%08x\n", hr);
return hr;
}
else {
// it's an AGC node...
Tab(iTabLevel);
printf("HAS AGC CONTROL\n");
aGCcontrol->SetEnabled(0, NULL); //Disable it
if (FAILED(hr)) {
Tab(iTabLevel);
printf("AGC Failed: hr = 0x%08x", hr);
aGCcontrol->Release();
return hr;
}
aGCcontrol->Release();
}
// Check Volume Settings
const IID IID_IAudioVolumeLevel = __uuidof(IAudioVolumeLevel);
IAudioVolumeLevel *volControl = NULL;
hr = pPart->Activate(CLSCTX_ALL, IID_IAudioVolumeLevel, (void**)&volControl);
if (E_NOINTERFACE == hr) {
Tab(iTabLevel);
printf("NO VOLUME CONTROL\n");
// not a volume control
}
else if (FAILED(hr)) {
Tab(iTabLevel);
printf("Unexpected failure trying to activate IAudioVolumeLevel : hr = 0x%08x\n", hr);
return hr;
}
else {
// it's a volume control node
Tab(iTabLevel);
printf("HAS VOLUME CONTROL\n");
UINT numChannels;
float pfLevelDB;
float minLevel;
float maxLevel;
float stepLevel;
volControl->GetChannelCount(&numChannels);
for (int i = 0; i < numChannels; i++) {
volControl->GetLevel(i + 1, &pfLevelDB);
volControl->GetLevelRange(i, &minLevel, &maxLevel, &stepLevel);
Tab(iTabLevel);
printf("Volume Level on %d: %f\n", i, pfLevelDB);
Tab(iTabLevel);
printf("Volume range: %f to %f steps of %f\n", minLevel, maxLevel, stepLevel);
}
}
// get the list of incoming parts
IPartsList *pOutgoingParts = NULL;
if (deviceType == eCapture) {
hr = pPart->EnumPartsOutgoing(&pOutgoingParts);
}
else {
hr = pPart->EnumPartsIncoming(&pOutgoingParts);
}
if (E_NOTFOUND == hr) {
return hr;
// not an error... we've just reached the end of the path
//MessageBox("No incoming parts at this part\n", MB_OK);
}
if (FAILED(hr)) {
return hr;
//MessageBox("Couldn't enum outgoing parts", MB_OK);
}
UINT nParts = 0;
hr = pOutgoingParts->GetCount(&nParts);
if (FAILED(hr)) {
//MessageBox("Couldn't get count of outgoing parts", MB_OK);
pOutgoingParts->Release();
return hr;
}
// walk the tree on each incoming part recursively
for (UINT n = 0; n < nParts; n++) {
IPart *pOutgoingPart = NULL;
hr = pOutgoingParts->GetPart(n, &pOutgoingPart);
if (FAILED(hr)) {
//MessageBox("Couldn't get part ", MB_OK);
pOutgoingParts->Release();
return hr;
}
hr = WalkTreeBackwardsFromPart(pOutgoingPart, deviceType, audioTreeCurrNode, iTabLevel + 1);
if (FAILED(hr)) {
//MessageBox("Couldn't walk tree on part", MB_OK);
pOutgoingPart->Release();
pOutgoingParts->Release();
return hr;
}
pOutgoingPart->Release();
}
pOutgoingParts->Release();
return S_OK;
}
void Tab(int iTabLevel) {
if (0 >= iTabLevel) { return; }
printf("\t");
Tab(iTabLevel - 1);
}
Here is my original post: (What Is This Sidetone Setting on My Playback Device?)
It was flagged for not being on topic because it's not programming related.

IMMDevice::Activate() with IBaseFilter return REGDB_E_CLASSNOTREG Class not registered

I want to get interface IBaseFilter system mixer, but get an error
REGDB_E_CLASSNOTREG Class not registered in:
hr = pEndpoint->Activate(__uuidof(IBaseFilter), CLSCTX_ALL, /*&var*/ NULL, (void**)ppFilter);
Earlier this code to work.
Fully code:
filterName = "Stereo mixer (IDT High Definition Audio CODEC)"
HRESULT CCapture::GetAudioCaptureFilterByName(wstring *filterName, IBaseFilter **ppFilter)
{
HRESULT hr = S_OK;
IMMDeviceEnumerator *pEnumerator = 0;
IMMDeviceCollection *pCollection = 0;
IMMDevice *pEndpoint = 0;
IPropertyStore *pProps = 0;
LPWSTR pwszID = 0;
hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pEnumerator);
if (FAILED(hr)) return hr;
hr = pEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &pCollection);
if (FAILED(hr))
{
if (pEnumerator) pEnumerator->Release();
return hr;
}
UINT count;
hr = pCollection->GetCount(&count);
if (FAILED(hr))
{
if (pCollection) pCollection->Release();
if (pEnumerator) pEnumerator->Release();
return hr;
}
if (count == 0)
{
if (pCollection) pCollection->Release();
if (pEnumerator) pEnumerator->Release();
return hr;
}
for (ULONG i = 0; i < count; i++)
{
hr = pCollection->Item(i, &pEndpoint);
if (FAILED(hr))
{
if (pCollection) pCollection->Release();
if (pEnumerator) pEnumerator->Release();
return hr;
}
// Получаем ID устройства.
hr = pEndpoint->GetId(&pwszID);
if (FAILED(hr))
{
if (pEndpoint) pEndpoint->Release();
if (pCollection) pCollection->Release();
if (pEnumerator) pEnumerator->Release();
return hr;
}
hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
if (FAILED(hr))
{
CoTaskMemFree(pwszID);
if (pEndpoint) pEndpoint->Release();
if (pCollection) pCollection->Release();
if (pEnumerator) pEnumerator->Release();
return hr;
}
PROPVARIANT varName;
PropVariantInit(&varName);
// Получаем дружественное имя устройства.
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
if (FAILED(hr))
{
CoTaskMemFree(pwszID);
if (pProps) pProps->Release();
if (pEndpoint) pEndpoint->Release();
if (pCollection) pCollection->Release();
if (pEnumerator) pEnumerator->Release();
return hr;
}
wstring *wstr = new wstring(varName.pwszVal);
if (wstr->compare(*filterName) == 0)
{
DIRECTX_AUDIO_ACTIVATION_PARAMS daap;
daap.cbDirectXAudioActivationParams = sizeof(daap);
daap.guidAudioSession = guidAudioSessionId;
daap.dwAudioStreamFlags = AUDCLNT_STREAMFLAGS_CROSSPROCESS;
PROPVARIANT var;
PropVariantInit(&var);
var.vt = VT_BLOB;
var.blob.cbSize = sizeof(daap);
var.blob.pBlobData = (BYTE*)&daap;
hr = pEndpoint->Activate(__uuidof(IBaseFilter), CLSCTX_ALL, /*&var*/ NULL, (void**)ppFilter);
delete wstr;
CoTaskMemFree(pwszID);
PropVariantClear(&varName);
if (pProps) pProps->Release();
if (pEndpoint) pEndpoint->Release();
if (pCollection) pCollection->Release();
if (pEnumerator) pEnumerator->Release();
return hr;
}
delete wstr;
CoTaskMemFree(pwszID);
pwszID = NULL;
PropVariantClear(&varName);
if (pProps) pProps->Release();
if (pEndpoint) pEndpoint->Release();
pProps = 0;
pEndpoint = 0;
}
hr = E_FAIL;
if (pProps) pProps->Release();
if (pEndpoint) pEndpoint->Release();
if (pCollection) pCollection->Release();
if (pEnumerator) pEnumerator->Release();
return hr;
}
The problem is caused by conflict with installed DirectShowSpy tool.
MMDevice's Activate for IBaseFilter presumably does the following:
if(interface is IBaseFilter)
{
IMMDeviceActivator pMmDeviceActivator;
CoCreateInstace(CLSID_SystemDeviceEnum, ..., &pMmDeviceActivator);
return pMmDeviceActivator->Activate(pMmDevice, ...)
}
Implementation of IMMDeviceActivator by System Device Enumerator CLSID_SystemDeviceEnum is undocumented (the interface is reserved for internal use) and DirectShowSpy did not handle it well. Since version 1.0.0.2106 it does it right.