Adds a ShimLog-style trace log next to the exe (startup, camera enumeration, negotiated capture format + MF_MT_DEFAULT_STRIDE, per-N-frame presence/tracked, shm CreateFileMapping result, errors) so the first real-hardware run is diagnosable instead of blind - mirrors how kinect_shim.log drove the shim fixes. Logs the camera stride so a top-down/bottom-up RGB32 image-flip is immediately visible.
213 lines
6.3 KiB
C++
213 lines
6.3 KiB
C++
#include "win_camera.h"
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <windows.h>
|
|
#include <mfapi.h>
|
|
#include <mfidl.h>
|
|
#include <mfreadwrite.h>
|
|
#include <mferror.h>
|
|
|
|
#include <cstring>
|
|
|
|
#include "trace_log.h"
|
|
|
|
namespace kinect_tracker {
|
|
namespace {
|
|
|
|
template <class T>
|
|
void SafeRelease(T** p) {
|
|
if (*p) {
|
|
(*p)->Release();
|
|
*p = nullptr;
|
|
}
|
|
}
|
|
|
|
std::string Narrow(const wchar_t* w) {
|
|
if (!w) return {};
|
|
const int n = WideCharToMultiByte(CP_UTF8, 0, w, -1, nullptr, 0, nullptr, nullptr);
|
|
std::string s(n > 0 ? n - 1 : 0, '\0');
|
|
if (n > 0) WideCharToMultiByte(CP_UTF8, 0, w, -1, s.data(), n, nullptr, nullptr);
|
|
return s;
|
|
}
|
|
|
|
IMFMediaSource* CreateSource(const std::wstring& symlink) {
|
|
IMFAttributes* attr = nullptr;
|
|
if (FAILED(MFCreateAttributes(&attr, 2))) return nullptr;
|
|
attr->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
|
|
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
|
|
attr->SetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK,
|
|
symlink.c_str());
|
|
IMFMediaSource* src = nullptr;
|
|
MFCreateDeviceSource(attr, &src);
|
|
SafeRelease(&attr);
|
|
return src;
|
|
}
|
|
|
|
}
|
|
|
|
std::vector<CameraDevice> enumerate_cameras() {
|
|
std::vector<CameraDevice> out;
|
|
IMFAttributes* attr = nullptr;
|
|
if (FAILED(MFCreateAttributes(&attr, 1))) return out;
|
|
attr->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
|
|
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
|
|
IMFActivate** devices = nullptr;
|
|
UINT32 count = 0;
|
|
if (SUCCEEDED(MFEnumDeviceSources(attr, &devices, &count))) {
|
|
for (UINT32 i = 0; i < count; ++i) {
|
|
CameraDevice dev;
|
|
wchar_t* link = nullptr;
|
|
UINT32 len = 0;
|
|
if (SUCCEEDED(devices[i]->GetAllocatedString(
|
|
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &link, &len))) {
|
|
dev.symbolic_link = link;
|
|
CoTaskMemFree(link);
|
|
}
|
|
wchar_t* name = nullptr;
|
|
if (SUCCEEDED(devices[i]->GetAllocatedString(
|
|
MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, &name, &len))) {
|
|
dev.name = Narrow(name);
|
|
CoTaskMemFree(name);
|
|
}
|
|
out.push_back(std::move(dev));
|
|
SafeRelease(&devices[i]);
|
|
}
|
|
CoTaskMemFree(devices);
|
|
}
|
|
SafeRelease(&attr);
|
|
return out;
|
|
}
|
|
|
|
std::vector<CameraResolution> enumerate_resolutions(const std::wstring& symbolic_link) {
|
|
std::vector<CameraResolution> out;
|
|
IMFMediaSource* src = CreateSource(symbolic_link);
|
|
if (!src) return out;
|
|
IMFSourceReader* reader = nullptr;
|
|
if (SUCCEEDED(MFCreateSourceReaderFromMediaSource(src, nullptr, &reader))) {
|
|
for (DWORD i = 0;; ++i) {
|
|
IMFMediaType* type = nullptr;
|
|
if (FAILED(reader->GetNativeMediaType(
|
|
MF_SOURCE_READER_FIRST_VIDEO_STREAM, i, &type))) {
|
|
break;
|
|
}
|
|
UINT32 w = 0, h = 0;
|
|
if (SUCCEEDED(MFGetAttributeSize(type, MF_MT_FRAME_SIZE, &w, &h))) {
|
|
bool seen = false;
|
|
for (const auto& r : out) {
|
|
if (r.width == static_cast<int>(w) && r.height == static_cast<int>(h)) {
|
|
seen = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!seen) out.push_back({static_cast<int>(w), static_cast<int>(h)});
|
|
}
|
|
SafeRelease(&type);
|
|
}
|
|
SafeRelease(&reader);
|
|
}
|
|
SafeRelease(&src);
|
|
return out;
|
|
}
|
|
|
|
Camera::~Camera() { close(); }
|
|
|
|
bool Camera::open(const std::wstring& symbolic_link, int width, int height) {
|
|
close();
|
|
IMFMediaSource* src = CreateSource(symbolic_link);
|
|
if (!src) return false;
|
|
|
|
IMFAttributes* ra = nullptr;
|
|
MFCreateAttributes(&ra, 1);
|
|
ra->SetUINT32(MF_SOURCE_READER_ENABLE_ADVANCED_VIDEO_PROCESSING, TRUE);
|
|
IMFSourceReader* reader = nullptr;
|
|
HRESULT hr = MFCreateSourceReaderFromMediaSource(src, ra, &reader);
|
|
SafeRelease(&ra);
|
|
SafeRelease(&src);
|
|
if (FAILED(hr)) return false;
|
|
|
|
IMFMediaType* mt = nullptr;
|
|
MFCreateMediaType(&mt);
|
|
mt->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
|
|
mt->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_RGB32);
|
|
MFSetAttributeSize(mt, MF_MT_FRAME_SIZE, width, height);
|
|
hr = reader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, nullptr, mt);
|
|
SafeRelease(&mt);
|
|
if (FAILED(hr)) {
|
|
TraceLog("camera open FAILED: SetCurrentMediaType RGB32 %dx%d hr=0x%08lX",
|
|
width, height, static_cast<unsigned long>(hr));
|
|
SafeRelease(&reader);
|
|
return false;
|
|
}
|
|
|
|
IMFMediaType* cur = nullptr;
|
|
LONG stride = 0;
|
|
if (SUCCEEDED(reader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, &cur))) {
|
|
UINT32 w = 0, h = 0;
|
|
if (SUCCEEDED(MFGetAttributeSize(cur, MF_MT_FRAME_SIZE, &w, &h))) {
|
|
width = static_cast<int>(w);
|
|
height = static_cast<int>(h);
|
|
}
|
|
UINT32 s = 0;
|
|
if (SUCCEEDED(cur->GetUINT32(MF_MT_DEFAULT_STRIDE, &s))) {
|
|
stride = static_cast<LONG>(s);
|
|
}
|
|
SafeRelease(&cur);
|
|
}
|
|
|
|
TraceLog("camera open OK: negotiated %dx%d default_stride=%ld (expect top-down +%d)",
|
|
width, height, stride, width * 4);
|
|
reader_ = reader;
|
|
width_ = width;
|
|
height_ = height;
|
|
return true;
|
|
}
|
|
|
|
bool Camera::read_frame(std::vector<uint8_t>& bgra, int& width, int& height) {
|
|
if (!reader_) return false;
|
|
auto* reader = static_cast<IMFSourceReader*>(reader_);
|
|
DWORD stream_flags = 0;
|
|
LONGLONG timestamp = 0;
|
|
IMFSample* sample = nullptr;
|
|
HRESULT hr = reader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, nullptr,
|
|
&stream_flags, ×tamp, &sample);
|
|
if (FAILED(hr)) return false;
|
|
if (!sample) return false;
|
|
|
|
IMFMediaBuffer* buffer = nullptr;
|
|
bool ok = false;
|
|
if (SUCCEEDED(sample->ConvertToContiguousBuffer(&buffer))) {
|
|
BYTE* data = nullptr;
|
|
DWORD max_len = 0, cur_len = 0;
|
|
if (SUCCEEDED(buffer->Lock(&data, &max_len, &cur_len))) {
|
|
const size_t need = static_cast<size_t>(width_) * height_ * 4u;
|
|
static unsigned logged = 0;
|
|
if (logged < 3) {
|
|
TraceLog("read_frame: buffer=%lu bytes, need=%zu (%dx%d*4)",
|
|
static_cast<unsigned long>(cur_len), need, width_, height_);
|
|
++logged;
|
|
}
|
|
if (cur_len >= need) {
|
|
bgra.resize(need);
|
|
std::memcpy(bgra.data(), data, need);
|
|
width = width_;
|
|
height = height_;
|
|
ok = true;
|
|
}
|
|
buffer->Unlock();
|
|
}
|
|
SafeRelease(&buffer);
|
|
}
|
|
SafeRelease(&sample);
|
|
return ok;
|
|
}
|
|
|
|
void Camera::close() {
|
|
if (reader_) {
|
|
static_cast<IMFSourceReader*>(reader_)->Release();
|
|
reader_ = nullptr;
|
|
}
|
|
width_ = height_ = 0;
|
|
}
|
|
|
|
}
|