fan out camera devices

This commit is contained in:
Green Sky 2024-10-06 21:31:16 +02:00
parent bc0f21175b
commit 8ba8b6322f
No known key found for this signature in database
3 changed files with 59 additions and 48 deletions

View File

@ -7,34 +7,21 @@
SDLVideo2InputDevice::SDLVideo2InputDevice(void) { SDLVideo2InputDevice::SDLVideo2InputDevice(void) {
int devcount {0}; int devcount {0};
SDL_CameraID *devices = SDL_GetCameras(&devcount); SDL_CameraID *devices = SDL_GetCameras(&devcount);
std::cout << "SDLVID: SDL Camera Driver: " << SDL_GetCurrentCameraDriver() << "\n";
if (devices == nullptr || devcount < 1) { if (devices == nullptr || devcount < 1) {
throw int(2); // TODO: proper error code throw int(2); // TODO: proper error code
} }
std::cout << "SDLVID: found cameras:\n"; // pick the last (usually the newest device)
for (int i = 0; i < devcount; i++) { _dev = devices[devcount-1];
const SDL_CameraID device = devices[i];
const char *name = SDL_GetCameraName(device);
std::cout << " - Camera #" << i << ": " << name << "\n";
int speccount {0};
SDL_CameraSpec** specs = SDL_GetCameraSupportedFormats(device, &speccount);
if (specs == nullptr) {
std::cout << " - no supported spec\n";
} else {
for (int spec_i = 0; spec_i < speccount; spec_i++) {
std::cout << " - " << specs[spec_i]->width << "x" << specs[spec_i]->height << "@" << float(specs[spec_i]->framerate_numerator)/specs[spec_i]->framerate_denominator << "fps " << SDL_GetPixelFormatName(specs[spec_i]->format) << "\n";
}
SDL_free(specs);
}
}
SDL_free(devices); SDL_free(devices);
} }
SDLVideo2InputDevice::SDLVideo2InputDevice(const SDL_CameraID dev) : _dev(dev) {
// nothing else?
}
SDLVideo2InputDevice::~SDLVideo2InputDevice(void) { SDLVideo2InputDevice::~SDLVideo2InputDevice(void) {
} }
@ -44,19 +31,6 @@ std::shared_ptr<FrameStream2I<SDLVideoFrame>> SDLVideo2InputDevice::subscribe(vo
// there was previously no stream, we assume no thread // there was previously no stream, we assume no thread
// open device here? or on the thread? // open device here? or on the thread?
int devcount {0};
SDL_CameraID *devices = SDL_GetCameras(&devcount);
if (devices == nullptr || devcount < 1) {
_ref--;
// error/no devices, should we do this in the constructor?
SDL_free(devices);
return nullptr;
}
//auto device = devices[0];
auto device = devices[devcount-1];
SDL_CameraSpec spec { SDL_CameraSpec spec {
// FORCE a different pixel format // FORCE a different pixel format
SDL_PIXELFORMAT_UNKNOWN, SDL_PIXELFORMAT_UNKNOWN,
@ -71,7 +45,7 @@ std::shared_ptr<FrameStream2I<SDLVideoFrame>> SDLVideo2InputDevice::subscribe(vo
// choose a good spec, large res but <= 1080p // choose a good spec, large res but <= 1080p
int speccount {0}; int speccount {0};
SDL_CameraSpec** specs = SDL_GetCameraSupportedFormats(device, &speccount); SDL_CameraSpec** specs = SDL_GetCameraSupportedFormats(_dev, &speccount);
if (specs != nullptr) { if (specs != nullptr) {
spec = *specs[0]; spec = *specs[0];
for (int spec_i = 1; spec_i < speccount; spec_i++) { for (int spec_i = 1; spec_i < speccount; spec_i++) {
@ -104,10 +78,9 @@ std::shared_ptr<FrameStream2I<SDLVideoFrame>> SDLVideo2InputDevice::subscribe(vo
camera = { camera = {
//SDL_OpenCamera(device, nullptr), //SDL_OpenCamera(device, nullptr),
SDL_OpenCamera(device, &spec), SDL_OpenCamera(_dev, &spec),
&SDL_CloseCamera &SDL_CloseCamera
}; };
SDL_free(devices);
if (!camera) { if (!camera) {
std::cerr << "SDLVID error: failed opening camera device\n"; std::cerr << "SDLVID error: failed opening camera device\n";

View File

@ -15,11 +15,13 @@
// while a stream is subscribed, have the camera device open // while a stream is subscribed, have the camera device open
// and aquire and push frames from a thread // and aquire and push frames from a thread
struct SDLVideo2InputDevice : public FrameStream2MultiSource<SDLVideoFrame> { struct SDLVideo2InputDevice : public FrameStream2MultiSource<SDLVideoFrame> {
SDL_CameraID _dev {0};
std::atomic_uint _ref {0}; std::atomic_uint _ref {0};
std::thread _thread; std::thread _thread;
// TODO: device id
SDLVideo2InputDevice(void); SDLVideo2InputDevice(void);
SDLVideo2InputDevice(const SDL_CameraID dev);
virtual ~SDLVideo2InputDevice(void); virtual ~SDLVideo2InputDevice(void);
// we hook int multi source // we hook int multi source

View File

@ -181,21 +181,57 @@ MainScreen::MainScreen(SimpleConfigModel&& conf_, SDL_Renderer* renderer_, Theme
} }
if (SDL_InitSubSystem(SDL_INIT_CAMERA)) { if (SDL_InitSubSystem(SDL_INIT_CAMERA)) {
{ // video in std::cout << "MS: SDL Camera Driver: " << SDL_GetCurrentCameraDriver() << "\n";
ObjectHandle vsrc {os.registry(), os.registry().create()};
try {
vsrc.emplace<Components::FrameStream2Source<SDLVideoFrame>>(
std::make_unique<SDLVideo2InputDevice>()
);
vsrc.emplace<Components::StreamSource>(Components::StreamSource::create<SDLVideoFrame>("SDL Video Default Recording Device")); int devcount {0};
vsrc.emplace<Components::TagDefaultTarget>(); SDL_CameraID *devices = SDL_GetCameras(&devcount);
os.throwEventConstruct(vsrc); std::cout << "MS: found cameras:\n";
} catch (...) { if (devices != nullptr) {
std::cerr << "MS error: failed constructing default video input source\n"; ObjectHandle last_src{};
os.registry().destroy(vsrc); for (int i = 0; i < devcount; i++) {
const SDL_CameraID device = devices[i];
const char *name = SDL_GetCameraName(device);
std::cout << " - Camera #" << i << ": " << name << "\n";
int speccount {0};
SDL_CameraSpec** specs = SDL_GetCameraSupportedFormats(device, &speccount);
if (specs == nullptr) {
std::cout << " - no supported spec\n";
} else {
for (int spec_i = 0; spec_i < speccount; spec_i++) {
std::cout << " - " << specs[spec_i]->width << "x" << specs[spec_i]->height << "@" << float(specs[spec_i]->framerate_numerator)/specs[spec_i]->framerate_denominator << "fps " << SDL_GetPixelFormatName(specs[spec_i]->format) << "\n";
}
SDL_free(specs);
// create sink for device
ObjectHandle vsrc {os.registry(), os.registry().create()};
try {
vsrc.emplace<Components::FrameStream2Source<SDLVideoFrame>>(
std::make_unique<SDLVideo2InputDevice>(device)
);
vsrc.emplace<Components::StreamSource>(Components::StreamSource::create<SDLVideoFrame>("SDL Video '" + std::string(name) + "'"));
//vsrc.emplace<Components::TagDefaultTarget>();
os.throwEventConstruct(vsrc);
last_src = vsrc;
} catch (...) {
std::cerr << "MS error: failed constructing video input source\n";
os.registry().destroy(vsrc);
}
}
} }
//if (static_cast<bool>(last_src)) {
// last_src.emplace<Components::TagDefaultTarget>();
//}
SDL_free(devices);
} else {
std::cout << " none\n";
} }
} else { } else {
std::cerr << "MS warning: no sdl camera: " << SDL_GetError() << "\n"; std::cerr << "MS warning: no sdl camera: " << SDL_GetError() << "\n";