Я пытаюсь получить предварительный просмотр камеры с помощью Media FOundation, который я обнаружил на YT, работающем.Я уже перечислил свои устройства.Я могу получить предварительный просмотр с моей камеры, но мой формат данных - NV12.Мне нужны мои данные в формате RGB32.Это класс, который содержит информацию об устройстве.
class Media : public IMFSourceReaderCallback
{
CRITICAL_SECTION criticalSection;
long referenceCount;
WCHAR *wSymbolicLink;
UINT32 cchSymbolicLink;
IMFSourceReader* sourceReader;
public:
LONG stride;
int bytesPerPixel;
GUID videoFormat;
UINT height;
UINT width;
WCHAR deviceNameString[2048];
BYTE* rawData;
HRESULT CreateCaptureDevice();
HRESULT SetSourceReader(IMFActivate *device);
HRESULT IsMediaTypeSupported(IMFMediaType* type);
HRESULT GetDefaultStride(IMFMediaType *pType, LONG *plStride);
HRESULT Close();
Media();
~Media();
// the class must implement the methods from IUnknown
STDMETHODIMP QueryInterface(REFIID iid, void** ppv);
STDMETHODIMP_(ULONG) AddRef();
STDMETHODIMP_(ULONG) Release();
// the class must implement the methods from IMFSourceReaderCallback
STDMETHODIMP OnReadSample(HRESULT status, DWORD streamIndex, DWORD streamFlags, LONGLONG timeStamp, IMFSample *sample);
STDMETHODIMP OnEvent(DWORD, IMFMediaEvent *);
STDMETHODIMP OnFlush(DWORD);
};
Это метод, который создает устройство:
HRESULT Media::CreateCaptureDevice()
{
HRESULT hr = S_OK;
//this is important!!
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);//COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
UINT32 count = 0;
IMFAttributes *attributes = NULL;
IMFActivate **devices = NULL;
if (FAILED(hr)) { CLEAN_ATTRIBUTES() }
// Create an attribute store to specify enumeration parameters.
hr = MFCreateAttributes(&attributes, 1);
if (FAILED(hr)) { CLEAN_ATTRIBUTES() }
//The attribute to be requested is devices that can capture video
hr = attributes->SetGUID(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID
);
if (FAILED(hr)) { CLEAN_ATTRIBUTES() }
//Enummerate the video capture devices
hr = MFEnumDeviceSources(attributes, &devices, &count);
if (FAILED(hr)) { CLEAN_ATTRIBUTES() }
//if there are any available devices
if (count > 0)
{
/*If you actually need to select one of the available devices
this is the place to do it. For this example the first device
is selected
*/
//Get a source reader from the first available device
SetSourceReader(devices[0]);
WCHAR *nameString = NULL;
// Get the human-friendly name of the device
UINT32 cchName;
hr = devices[0]->GetAllocatedString(
MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME,
&nameString, &cchName);
if (SUCCEEDED(hr))
{
//allocate a byte buffer for the raw pixel data
bytesPerPixel = abs(stride) / width;
rawData = new BYTE[width*height * bytesPerPixel];
wcscpy(deviceNameString,nameString);
}
CoTaskMemFree(nameString);
}
//clean
CLEAN_ATTRIBUTES()
}
И это метод, который устанавливает устройство:
HRESULT Media::SetSourceReader(IMFActivate *device)
{
HRESULT hr = S_OK;
IMFMediaSource *source = NULL;
IMFAttributes *attributes = NULL;
IMFMediaType *mediaType = NULL;
EnterCriticalSection(&criticalSection);
hr = device->ActivateObject(__uuidof(IMFMediaSource), (void**)&source);
//get symbolic link for the device
if(SUCCEEDED(hr))
hr = device->GetAllocatedString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &wSymbolicLink, &cchSymbolicLink);
//Allocate attributes
if (SUCCEEDED(hr))
hr = MFCreateAttributes(&attributes, 2);
//get attributes
if (SUCCEEDED(hr))
hr = attributes->SetUINT32(MF_READWRITE_DISABLE_CONVERTERS, TRUE);
// Set the callback pointer.
if (SUCCEEDED(hr))
hr = attributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK,this);
//Create the source reader
if (SUCCEEDED(hr))
hr = MFCreateSourceReaderFromMediaSource(source,attributes,&sourceReader);
// Try to find a suitable output type.
if (SUCCEEDED(hr))
{
for (DWORD i = 0; ; i++)
{
hr = sourceReader->GetNativeMediaType((DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM,i,&mediaType);
if (FAILED(hr)) { break; }
hr = IsMediaTypeSupported(mediaType);
if (FAILED(hr)) { break; }
//Get width and height
MFGetAttributeSize(mediaType, MF_MT_FRAME_SIZE, &width, &height);
if (mediaType)
{ mediaType->Release(); mediaType = NULL; }
if (SUCCEEDED(hr))// Found an output type.
break;
}
}
if (SUCCEEDED(hr))
{
// Ask for the first sample.
hr = sourceReader->ReadSample((DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, NULL, NULL,NULL,NULL);
}
if (FAILED(hr))
{
if (source)
{
source->Shutdown();
}
Close();
}
if (source) { source->Release(); source = NULL; }
if (attributes) { attributes->Release(); attributes = NULL; }
if (mediaType) { mediaType->Release(); mediaType = NULL; }
LeaveCriticalSection(&criticalSection);
return hr;
}