Capture PS3 Eye pixels through direct driver
This commit is contained in:
parent
0d9c7824b8
commit
58e7af8a76
11 changed files with 593 additions and 1 deletions
|
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
project(mimir_camera_capture LANGUAGES CXX)
|
||||
project(mimir_camera_capture LANGUAGES C CXX)
|
||||
|
||||
add_library(mimir_camera_capture SHARED
|
||||
mimir_ks_camera_capture.cpp)
|
||||
|
|
@ -12,3 +12,45 @@ target_compile_definitions(mimir_camera_capture PRIVATE
|
|||
target_link_libraries(mimir_camera_capture PRIVATE
|
||||
setupapi
|
||||
ksuser)
|
||||
|
||||
set(PSMOVEAPI_ROOT "$ENV{TEMP}/mimir-driver-probes/psmoveapi" CACHE PATH "Root of a psmoveapi checkout with external/libusb-1.0 and external/PS3EYEDriver.")
|
||||
set(LIBUSB_ROOT "${PSMOVEAPI_ROOT}/external/libusb-1.0" CACHE PATH "libusb source root.")
|
||||
set(PS3EYE_ROOT "${PSMOVEAPI_ROOT}/external/PS3EYEDriver" CACHE PATH "PS3EYEDriver source root.")
|
||||
|
||||
add_library(mimir_libusb_static STATIC
|
||||
${LIBUSB_ROOT}/libusb/core.c
|
||||
${LIBUSB_ROOT}/libusb/descriptor.c
|
||||
${LIBUSB_ROOT}/libusb/hotplug.c
|
||||
${LIBUSB_ROOT}/libusb/io.c
|
||||
${LIBUSB_ROOT}/libusb/strerror.c
|
||||
${LIBUSB_ROOT}/libusb/sync.c
|
||||
${LIBUSB_ROOT}/libusb/os/events_windows.c
|
||||
${LIBUSB_ROOT}/libusb/os/threads_windows.c
|
||||
${LIBUSB_ROOT}/libusb/os/windows_common.c
|
||||
${LIBUSB_ROOT}/libusb/os/windows_usbdk.c
|
||||
${LIBUSB_ROOT}/libusb/os/windows_winusb.c)
|
||||
target_include_directories(mimir_libusb_static PUBLIC
|
||||
${LIBUSB_ROOT}/libusb
|
||||
${LIBUSB_ROOT}/libusb/os
|
||||
${LIBUSB_ROOT}/msvc)
|
||||
target_compile_definitions(mimir_libusb_static PRIVATE
|
||||
_CRT_SECURE_NO_WARNINGS
|
||||
_WIN32_WINNT=0x0601)
|
||||
target_link_libraries(mimir_libusb_static PRIVATE setupapi ole32 advapi32)
|
||||
|
||||
add_library(mimir_ps3eye_vendor STATIC
|
||||
${PS3EYE_ROOT}/src/ps3eye.cpp
|
||||
${PS3EYE_ROOT}/src/ps3eye_capi.cpp)
|
||||
target_include_directories(mimir_ps3eye_vendor PUBLIC
|
||||
${PS3EYE_ROOT}/src
|
||||
${LIBUSB_ROOT}/libusb)
|
||||
target_link_libraries(mimir_ps3eye_vendor PUBLIC mimir_libusb_static)
|
||||
|
||||
add_library(mimir_ps3eye_capture SHARED
|
||||
mimir_ps3eye_capture.cpp)
|
||||
target_compile_features(mimir_ps3eye_capture PRIVATE cxx_std_20)
|
||||
target_compile_definitions(mimir_ps3eye_capture PRIVATE
|
||||
WIN32_LEAN_AND_MEAN
|
||||
NOMINMAX)
|
||||
target_include_directories(mimir_ps3eye_capture PRIVATE ${PS3EYE_ROOT}/src)
|
||||
target_link_libraries(mimir_ps3eye_capture PRIVATE mimir_ps3eye_vendor)
|
||||
|
|
|
|||
215
native/camera_capture/mimir_ps3eye_capture.cpp
Normal file
215
native/camera_capture/mimir_ps3eye_capture.cpp
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "ps3eye_capi.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
struct MimirPs3EyeFrame
|
||||
{
|
||||
std::vector<unsigned char> data;
|
||||
std::int64_t timestampNs = 0;
|
||||
std::uint64_t sequence = 0;
|
||||
};
|
||||
|
||||
struct MimirPs3EyeCapture
|
||||
{
|
||||
ps3eye_t* eye = nullptr;
|
||||
std::thread worker;
|
||||
std::mutex mutex;
|
||||
std::condition_variable condition;
|
||||
std::deque<MimirPs3EyeFrame> queue;
|
||||
std::atomic<bool> running = false;
|
||||
int cameraIndex = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int fps = 0;
|
||||
std::uint64_t sequence = 0;
|
||||
};
|
||||
|
||||
std::mutex g_ps3eye_mutex;
|
||||
int g_ps3eye_users = 0;
|
||||
|
||||
long long nowNs()
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
void retainPs3Eye()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_ps3eye_mutex);
|
||||
if (g_ps3eye_users++ == 0)
|
||||
{
|
||||
ps3eye_init();
|
||||
}
|
||||
}
|
||||
|
||||
void releasePs3Eye()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_ps3eye_mutex);
|
||||
if (g_ps3eye_users > 0 && --g_ps3eye_users == 0)
|
||||
{
|
||||
ps3eye_uninit();
|
||||
}
|
||||
}
|
||||
|
||||
void pushFrame(MimirPs3EyeCapture& capture, const std::vector<unsigned char>& source)
|
||||
{
|
||||
MimirPs3EyeFrame frame;
|
||||
frame.data = source;
|
||||
frame.timestampNs = nowNs();
|
||||
frame.sequence = capture.sequence++;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(capture.mutex);
|
||||
capture.queue.push_back(std::move(frame));
|
||||
while (capture.queue.size() > 32)
|
||||
{
|
||||
capture.queue.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
capture.condition.notify_one();
|
||||
}
|
||||
|
||||
void captureThread(MimirPs3EyeCapture* capture)
|
||||
{
|
||||
std::vector<unsigned char> frame(static_cast<std::size_t>(capture->width) * static_cast<std::size_t>(capture->height));
|
||||
while (capture->running.load(std::memory_order_acquire))
|
||||
{
|
||||
ps3eye_grab_frame(capture->eye, frame.data());
|
||||
pushFrame(*capture, frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
__declspec(dllexport) MimirPs3EyeCapture* mimir_ps3eye_create(int cameraIndex, int width, int height, int fps)
|
||||
{
|
||||
if (cameraIndex < 0 || width <= 0 || height <= 0 || fps <= 0)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
retainPs3Eye();
|
||||
if (cameraIndex >= ps3eye_count_connected())
|
||||
{
|
||||
releasePs3Eye();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto* capture = new MimirPs3EyeCapture();
|
||||
capture->cameraIndex = cameraIndex;
|
||||
capture->width = width;
|
||||
capture->height = height;
|
||||
capture->fps = fps;
|
||||
capture->eye = ps3eye_open(cameraIndex, width, height, fps, PS3EYE_FORMAT_BAYER);
|
||||
if (!capture->eye)
|
||||
{
|
||||
delete capture;
|
||||
releasePs3Eye();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return capture;
|
||||
}
|
||||
|
||||
__declspec(dllexport) int mimir_ps3eye_start(MimirPs3EyeCapture* capture)
|
||||
{
|
||||
if (!capture || capture->running.exchange(true))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
capture->worker = std::thread(captureThread, capture);
|
||||
return 1;
|
||||
}
|
||||
|
||||
__declspec(dllexport) int mimir_ps3eye_read(
|
||||
MimirPs3EyeCapture* capture,
|
||||
int* width,
|
||||
int* height,
|
||||
int* stride,
|
||||
std::int64_t* timestampNs,
|
||||
std::uint64_t* sequence,
|
||||
unsigned char* destination,
|
||||
int destinationBytes)
|
||||
{
|
||||
if (!capture || !destination || destinationBytes <= 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
MimirPs3EyeFrame frame;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(capture->mutex);
|
||||
if (capture->queue.empty())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
frame = std::move(capture->queue.front());
|
||||
capture->queue.pop_front();
|
||||
}
|
||||
|
||||
if (frame.data.size() > static_cast<std::size_t>(destinationBytes))
|
||||
{
|
||||
return -static_cast<int>(frame.data.size());
|
||||
}
|
||||
|
||||
std::memcpy(destination, frame.data.data(), frame.data.size());
|
||||
if (width)
|
||||
{
|
||||
*width = capture->width;
|
||||
}
|
||||
if (height)
|
||||
{
|
||||
*height = capture->height;
|
||||
}
|
||||
if (stride)
|
||||
{
|
||||
*stride = capture->width;
|
||||
}
|
||||
if (timestampNs)
|
||||
{
|
||||
*timestampNs = frame.timestampNs;
|
||||
}
|
||||
if (sequence)
|
||||
{
|
||||
*sequence = frame.sequence;
|
||||
}
|
||||
|
||||
return static_cast<int>(frame.data.size());
|
||||
}
|
||||
|
||||
__declspec(dllexport) void mimir_ps3eye_destroy(MimirPs3EyeCapture* capture)
|
||||
{
|
||||
if (!capture)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
capture->running.store(false, std::memory_order_release);
|
||||
if (capture->worker.joinable())
|
||||
{
|
||||
capture->worker.join();
|
||||
}
|
||||
if (capture->eye)
|
||||
{
|
||||
ps3eye_close(capture->eye);
|
||||
}
|
||||
|
||||
delete capture;
|
||||
releasePs3Eye();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue