feat(tracker-native): native Windows app - MF camera + Win32 GUI + shm publish
win_camera: Media Foundation device + native-resolution enumeration and RGB32 capture. win_main: plain Win32 GUI (device/resolution dropdowns, Start/Stop, StretchDIBits live preview with mask+landmark overlay) + CreateFileMappingW on kinect_virtual_frames + a worker thread wiring capture -> PoseEstimator (bootstrap+track) -> FramePipeline -> publish. build_win.sh cross-compiles a static kinect-tracker.exe with MinGW-w64 x64 (ORT header needs -D_stdcall=__stdcall; x64 ignores the calling convention so the win ORT DLL ABI matches). PoseEstimator path made ORTCHAR_T/wchar-portable; M_PI -> local const. Builds clean; exe is PE32+ GUI x86-64.
This commit is contained in:
1 parent
41534b07d1
commit
31178f1675
6 files changed
+643
-6
No files matched your search
@@ -0,0 +1,195 @@
|
||||
#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>
|
||||
|
||||
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)) {
|
||||
SafeRelease(&reader);
|
||||
return false;
|
||||
}
|
||||
|
||||
IMFMediaType* cur = nullptr;
|
||||
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);
|
||||
}
|
||||
SafeRelease(&cur);
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user