Quantcast
Channel: Media Foundation Development for Windows Desktop forum
Viewing all articles
Browse latest Browse all 1079

How to capture raw format image using media foundation?

$
0
0

Hello,

I am new to MediaFoundation's video capture API. But I have an app that performs a video capture preview of a webcam. I picked up most of the ideas from this sample code 

CaptureEngine video capture sample

I am facing problem in saving the video buffer as raw format from Video stream. Iam trying this following way.

Using IMFCapturePhotoSink, I captured one frame from video stream.There is no raw container support in 
capture engine.So,i registered the callback using IMFCapturePhotoSink->SetSampleCallback() to retrieve 
buffer from video source.

After registered the callback,IMFCaptureEngineOnSampleCallback::OnSample() method receives a sample.
OnSample method is not called when MJPG video streaming. But I received a sample when streaming in YUY2 format. Below code for your reference.

HRESULT CTakePhoto()
{
    IMFCaptureSink *pSink = NULL;
    IMFCapturePhotoSink *pPhoto = NULL;
    IMFCaptureSource *pSource;
    IMFMediaType *pMediaType = 0;

    // Get a pointer to the photo sink.
    HRESULT hr = m_pEngine->GetSink(MF_CAPTURE_ENGINE_SINK_TYPE_PHOTO, &pSink);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pSink->QueryInterface(IID_PPV_ARGS(&pPhoto));
    if (FAILED(hr))
    {
        goto done;
    }

    hr = m_pEngine->GetSource(&pSource);
    if (FAILED(hr))
    {
		PrintDebug(L"GetSource : GetLastError() = %d\r\n",GetLastError());
        goto done;
    }

	hr = pSource->GetCurrentDeviceMediaType(1, &pMediaType);     		// 1 is image stream index
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pPhoto->RemoveAllStreams();
    if (FAILED(hr))
    {
		PrintDebug(L"RemoveAllStreams failed 0x%x %d\r\n",hr,GetLastError());
        goto done;
    }

    DWORD dwSinkStreamIndex;
	// Try to connect the first still image stream to the photo sink
	hr = pPhoto->AddStream((DWORD)MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_PHOTO,  pMediaType,NULL, &dwSinkStreamIndex);
	if(FAILED(hr))
	{
		goto done;
	}

	//Register the callback
	hr = pPhoto->SetSampleCallback(&SampleCb);
	if (FAILED(hr))
	{
		goto done;
	}

    hr = m_pEngine->TakePhoto();
    if (FAILED(hr))
    {
        goto done;
    }

done:
    SafeRelease(&pSink);
    SafeRelease(&pPhoto);
    SafeRelease(&pSource);
    SafeRelease(&pMediaType);
    return hr;
}

//Its a class
STDMETHODIMP CSampleCallback::OnSample(_In_ IMFSample *pSample)
{
	if(pSample != NULL)
	{

		hr = pSample->ConvertToContiguousBuffer(&pMediaBuffer);
		if(FAILED(hr))
		{
			OutputDebugString(L"ConvertToContiguousBuffer failed\r\n");
			return S_FALSE;
		}

		hr = pMediaBuffer->Lock(&pBuffer,0,&dwBufLen);
		if(FAILED(hr))
		{
			OutputDebugString(L"Lock failed\r\n");
			return S_FALSE;
		}

		hr = pMediaBuffer->Unlock();
		if(FAILED(hr))
		{
			OutputDebugString(L"Unlock failed\r\n");
			return S_FALSE;
		}

		SafeRelease(&pMediaBuffer);
	}
	return S_OK;
}

Why cant i receive a sample when MJPG video streaming?Why this behaviour occurs?
Did i miss anything to configure to receive a sample?Can you help me to sort out this problem?

Thanks in advance.

Regards,

Ambika



Viewing all articles
Browse latest Browse all 1079

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>