Skip to content

Commit

Permalink
fan out camera devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Oct 6, 2024
1 parent bc0f211 commit 8ba8b63
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 48 deletions.
43 changes: 8 additions & 35 deletions src/frame_streams/sdl/sdl_video_frame_stream2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,21 @@
SDLVideo2InputDevice::SDLVideo2InputDevice(void) {
int devcount {0};
SDL_CameraID *devices = SDL_GetCameras(&devcount);
std::cout << "SDLVID: SDL Camera Driver: " << SDL_GetCurrentCameraDriver() << "\n";

if (devices == nullptr || devcount < 1) {
throw int(2); // TODO: proper error code
}

std::cout << "SDLVID: found cameras:\n";
for (int i = 0; i < devcount; i++) {
const SDL_CameraID device = devices[i];
// pick the last (usually the newest device)
_dev = devices[devcount-1];

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);
}

SDLVideo2InputDevice::SDLVideo2InputDevice(const SDL_CameraID dev) : _dev(dev) {
// nothing else?
}

SDLVideo2InputDevice::~SDLVideo2InputDevice(void) {
}

Expand All @@ -44,19 +31,6 @@ std::shared_ptr<FrameStream2I<SDLVideoFrame>> SDLVideo2InputDevice::subscribe(vo
// there was previously no stream, we assume no 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 {
// FORCE a different pixel format
SDL_PIXELFORMAT_UNKNOWN,
Expand All @@ -71,7 +45,7 @@ std::shared_ptr<FrameStream2I<SDLVideoFrame>> SDLVideo2InputDevice::subscribe(vo

// choose a good spec, large res but <= 1080p
int speccount {0};
SDL_CameraSpec** specs = SDL_GetCameraSupportedFormats(device, &speccount);
SDL_CameraSpec** specs = SDL_GetCameraSupportedFormats(_dev, &speccount);
if (specs != nullptr) {
spec = *specs[0];
for (int spec_i = 1; spec_i < speccount; spec_i++) {
Expand Down Expand Up @@ -104,10 +78,9 @@ std::shared_ptr<FrameStream2I<SDLVideoFrame>> SDLVideo2InputDevice::subscribe(vo

camera = {
//SDL_OpenCamera(device, nullptr),
SDL_OpenCamera(device, &spec),
SDL_OpenCamera(_dev, &spec),
&SDL_CloseCamera
};
SDL_free(devices);

if (!camera) {
std::cerr << "SDLVID error: failed opening camera device\n";
Expand Down
4 changes: 3 additions & 1 deletion src/frame_streams/sdl/sdl_video_frame_stream2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
// while a stream is subscribed, have the camera device open
// and aquire and push frames from a thread
struct SDLVideo2InputDevice : public FrameStream2MultiSource<SDLVideoFrame> {
SDL_CameraID _dev {0};

std::atomic_uint _ref {0};
std::thread _thread;

// TODO: device id
SDLVideo2InputDevice(void);
SDLVideo2InputDevice(const SDL_CameraID dev);
virtual ~SDLVideo2InputDevice(void);

// we hook int multi source
Expand Down
60 changes: 48 additions & 12 deletions src/main_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,57 @@ MainScreen::MainScreen(SimpleConfigModel&& conf_, SDL_Renderer* renderer_, Theme
}

if (SDL_InitSubSystem(SDL_INIT_CAMERA)) {
{ // video in
ObjectHandle vsrc {os.registry(), os.registry().create()};
try {
vsrc.emplace<Components::FrameStream2Source<SDLVideoFrame>>(
std::make_unique<SDLVideo2InputDevice>()
);
std::cout << "MS: SDL Camera Driver: " << SDL_GetCurrentCameraDriver() << "\n";

vsrc.emplace<Components::StreamSource>(Components::StreamSource::create<SDLVideoFrame>("SDL Video Default Recording Device"));
vsrc.emplace<Components::TagDefaultTarget>();
int devcount {0};
SDL_CameraID *devices = SDL_GetCameras(&devcount);

os.throwEventConstruct(vsrc);
} catch (...) {
std::cerr << "MS error: failed constructing default video input source\n";
os.registry().destroy(vsrc);
std::cout << "MS: found cameras:\n";
if (devices != nullptr) {
ObjectHandle last_src{};
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 {
std::cerr << "MS warning: no sdl camera: " << SDL_GetError() << "\n";
Expand Down

0 comments on commit 8ba8b63

Please sign in to comment.