#include "win_camera.h" #define WIN32_LEAN_AND_MEAN #include #include #include #include #include #include #include "trace_log.h" namespace kinect_tracker { namespace { template 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 enumerate_cameras() { std::vector 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 enumerate_resolutions(const std::wstring& symbolic_link) { std::vector 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(w) && r.height == static_cast(h)) { seen = true; break; } } if (!seen) out.push_back({static_cast(w), static_cast(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) { // The symbolic link we enumerated earlier no longer resolves to a live // device - the usual cause is the selected camera having been unplugged // (or claimed exclusively by another app) between enumeration and Start. TraceLog("camera open FAILED: no media source for '%s' - camera disconnected " "or in use by another application", Narrow(symbolic_link.c_str()).c_str()); return false; } IMFAttributes* ra = nullptr; MFCreateAttributes(&ra, 1); ra->SetUINT32(MF_SOURCE_READER_ENABLE_ADVANCED_VIDEO_PROCESSING, TRUE); // Cut the source reader's internal frame buffering so ReadSample returns the // freshest frame instead of a queued (stale) one - critical for low latency. ra->SetUINT32(MF_LOW_LATENCY, 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: RGB32 %dx%d rejected (hr=0x%08lX); retrying RGB32 default size", width, height, static_cast(hr)); IMFMediaType* mt2 = nullptr; MFCreateMediaType(&mt2); mt2->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video); mt2->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_RGB32); hr = reader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, nullptr, mt2); SafeRelease(&mt2); } if (FAILED(hr)) { TraceLog("camera open FAILED: no RGB32 output available hr=0x%08lX", static_cast(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(w); height = static_cast(h); } UINT32 s = 0; if (SUCCEEDED(cur->GetUINT32(MF_MT_DEFAULT_STRIDE, &s))) { stride = static_cast(s); } SafeRelease(&cur); } if (stride == 0) stride = width * 4; TraceLog("camera open OK: negotiated %dx%d default_stride=%ld (%s)", width, height, stride, stride < 0 ? "bottom-up -> flipped" : "top-down"); reader_ = reader; width_ = width; height_ = height; stride_ = static_cast(stride); return true; } bool Camera::read_frame(std::vector& bgra, int& width, int& height) { if (!reader_) return false; auto* reader = static_cast(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 row_bytes = static_cast(width_) * 4u; const size_t need = row_bytes * height_; const int abs_stride = stride_ < 0 ? -stride_ : stride_; static unsigned logged = 0; if (logged < 3) { TraceLog("read_frame: buffer=%lu bytes, need=%zu (%dx%d*4) stride=%d", static_cast(cur_len), need, width_, height_, stride_); ++logged; } if (cur_len >= static_cast(static_cast(abs_stride) * height_)) { bgra.resize(need); for (int y = 0; y < height_; ++y) { const int src_row = stride_ < 0 ? (height_ - 1 - y) : y; std::memcpy(bgra.data() + static_cast(y) * row_bytes, data + static_cast(src_row) * abs_stride, row_bytes); } width = width_; height = height_; ok = true; } buffer->Unlock(); } SafeRelease(&buffer); } SafeRelease(&sample); return ok; } void Camera::close() { if (reader_) { static_cast(reader_)->Release(); reader_ = nullptr; } width_ = height_ = 0; } }