Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HamGuy/5849939 to your computer and use it in GitHub Desktop.
Save HamGuy/5849939 to your computer and use it in GitHub Desktop.
#include "pch.h"
#include "NativeCameraInterface.h"
using namespace Microsoft::WRL;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Phone::Media::Capture;
// Called each time a preview frame is available
void NativeCameraInterface::Native::CameraCapturePreviewSink::OnFrameAvailable(
DXGI_FORMAT format,
UINT width,
UINT height,
BYTE* pixels
)
{
OutputDebugString(L"Preview!\n");
// Insert your own code to process each preview frame here
}
// Called each time a captured frame is available
void NativeCameraInterface::Native::CameraCaptureSampleSink::OnSampleAvailable(
ULONGLONG hnsPresentationTime,
ULONGLONG hnsSampleDuration,
DWORD cbSample,
BYTE* pSample)
{
OutputDebugString(L"Full!\n");
// Insert your own code to process each captured frame here
}
NativeCameraInterface::Native::NativeCapture::NativeCapture()
{
// Set the capture dimensions
Size captureDimensions;
captureDimensions.Width = 1920;
captureDimensions.Height = 1080;
// Open the AudioVideoCaptureDevice for video only
IAsyncOperation<AudioVideoCaptureDevice^> ^openOperation = AudioVideoCaptureDevice::OpenForVideoOnlyAsync(CameraSensorLocation::Back, captureDimensions);
openOperation->Completed = ref new AsyncOperationCompletedHandler<AudioVideoCaptureDevice^>(
[this] (IAsyncOperation<AudioVideoCaptureDevice^> ^operation, Windows::Foundation::AsyncStatus status)
{
if (status == Windows::Foundation::AsyncStatus::Completed)
{
auto captureDevice = operation->GetResults();
// Save the reference to the opened video capture device
pAudioVideoCaptureDevice = captureDevice;
// Retrieve the native ICameraCaptureDeviceNative interface from the managed video capture device
ICameraCaptureDeviceNative *iCameraCaptureDeviceNative = NULL;
HRESULT hr = reinterpret_cast<IUnknown*>(captureDevice)->QueryInterface(__uuidof(ICameraCaptureDeviceNative), (void**) &iCameraCaptureDeviceNative);
// Save the pointer to the native interface
pCameraCaptureDeviceNative = iCameraCaptureDeviceNative;
// Initialize the preview dimensions (see the accompanying article at )
// The aspect ratio of the capture and preview resolution must be equal,
// 4:3 for capture => 4:3 for preview, and 16:9 for capture => 16:9 for preview.
Size previewDimensions;
previewDimensions.Width = 800;
previewDimensions.Height = 448;
IAsyncAction^ setPreviewResolutionAction = pAudioVideoCaptureDevice->SetPreviewResolutionAsync(previewDimensions);
setPreviewResolutionAction->Completed = ref new AsyncActionCompletedHandler(
[this](IAsyncAction^ action, Windows::Foundation::AsyncStatus status)
{
HResult hr = action->ErrorCode;
if (status == Windows::Foundation::AsyncStatus::Completed)
{
// Create the sink
MakeAndInitialize<CameraCapturePreviewSink>(&pCameraCapturePreviewSink);
pCameraCaptureDeviceNative->SetPreviewSink(pCameraCapturePreviewSink);
// Set the preview format
pCameraCaptureDeviceNative->SetPreviewFormat(DXGI_FORMAT::DXGI_FORMAT_B8G8R8A8_UNORM);
}
}
);
// Retrieve IAudioVideoCaptureDeviceNative native interface from managed projection.
IAudioVideoCaptureDeviceNative *iAudioVideoCaptureDeviceNative = NULL;
hr = reinterpret_cast<IUnknown*>(captureDevice)->QueryInterface(__uuidof(IAudioVideoCaptureDeviceNative), (void**) &iAudioVideoCaptureDeviceNative);
// Save the pointer to the IAudioVideoCaptureDeviceNative native interface
pAudioVideoCaptureDeviceNative = iAudioVideoCaptureDeviceNative;
// Set sample encoding format to ARGB. See the documentation for further values.
pAudioVideoCaptureDevice->VideoEncodingFormat = CameraCaptureVideoFormat::Argb;
// Initialize and set the CameraCaptureSampleSink class as sink for captures samples
MakeAndInitialize<CameraCaptureSampleSink>(&pCameraCaptureSampleSink);
pAudioVideoCaptureDeviceNative->SetVideoSampleSink(pCameraCaptureSampleSink);
// Start recording (only way to receive samples using the ICameraCaptureSampleSink interface
pAudioVideoCaptureDevice->StartRecordingToSinkAsync();
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment