![]() |
IRSOL
C++ code implementing socket server for interacting with Baumer camera.
|
Coordinates frame acquisition from a camera and distributes frames to registered clients. More...
#include <collector.hpp>
Public Types | |
using | frame_queue_t = ClientCollectionParams::frame_queue_t |
Public Member Functions | |
FrameCollector (irsol::camera::Interface &camera) | |
Constructs a FrameCollector for the given camera interface. | |
~FrameCollector () | |
Destructor. Stops any running threads and cleans up resources. | |
void | start () |
Starts the frame collection and distribution thread. | |
void | stop () |
Stops the frame collector and joins worker threads. | |
bool | isBusy () const |
Checks whether the collector is currently serving any clients. | |
void | registerClient (irsol::types::client_id_t clientId, double fps, std::shared_ptr< frame_queue_t > queue, int64_t frameCount=-1) |
Registers a client to receive frames at a specified frame rate. | |
void | deregisterClient (irsol::types::client_id_t clientId) |
Deregisters a client and stops frame delivery. | |
Static Public Member Functions | |
static std::shared_ptr< frame_queue_t > | makeQueuePtr () |
Utility static function to create a shared pointer to a frame queue. | |
Static Public Attributes | |
static constexpr irsol::types::duration_t | SLACK = std::chrono::milliseconds(50) |
Slack window for batching frame delivery to clients. | |
Private Member Functions | |
void | run () |
Runs the frame distribution loop in a background thread. | |
void | deregisterClientNonThreadSafe (irsol::types::client_id_t clientId) |
Deregisters a client and stops frame delivery (not thread-safe). | |
std::pair< std::vector< irsol::types::client_id_t >, std::vector< irsol::types::timepoint_t > > | collectReadyClients (irsol::types::timepoint_t now, irsol::types::duration_t slack) const |
Collects clients who are scheduled to receive a frame at the given time. | |
void | cleanUpSchedule (const std::vector< irsol::types::timepoint_t > schedules) |
Removes schedules from the schedule map. | |
std::optional< std::pair< FrameMetadata, std::vector< irsol::types::byte_t > > > | grabImageData () const |
Captures an image and returns it along with associated metadata. | |
bool | schedule (const irsol::types::client_id_t &clientId, irsol::types::timepoint_t nextFrameDue) |
Schedules the next frame delivery for a client. | |
Private Attributes | |
irsol::camera::Interface & | m_cam |
Reference to the camera interface used for capturing. | |
std::mutex | m_clientsMutex |
Protects access to m_clients and m_scheduleMap. | |
std::unordered_map< irsol::types::client_id_t, ClientCollectionParams > | m_clients |
Stores parameters for each registered client. | |
std::map< irsol::types::timepoint_t, std::vector< irsol::types::client_id_t > > | m_scheduleMap |
Maps timestamps to client IDs scheduled at that time. | |
std::condition_variable | m_scheduleCondition |
Signals when a new client is scheduled. | |
std::thread | m_distributorThread |
Thread responsible for frame distribution. | |
std::atomic< bool > | m_stop |
Indicates whether the collector is stopping due to an external request. | |
Coordinates frame acquisition from a camera and distributes frames to registered clients.
The FrameCollector class is responsible for orchestrating the acquisition of frames from a camera device and distributing those frames to multiple registered clients. Each client can specify its desired frame rate and the number of frames it wishes to receive. The collector ensures that frames are delivered to clients according to their schedules, batching requests within a configurable "slack" window to optimize camera usage and minimize redundant captures. For instance, if multiple clients are scheduled to receive frames at similar times, a single frame image is requested from the camera and distributed to all those clients.
The collector maintains a background thread that monitors client schedules, captures frames just-in-time, and pushes them into client-specific irsol::utils::SafeQueue. Clients can consume frames from these queues at their own pace. When a client has received its requested number of frames, it is automatically deregistered and its queue is marked as complete.
The scheduling strategy is as follows:
Thread safety: All public methods are thread-safe unless otherwise noted.
Definition at line 69 of file collector.hpp.
using irsol::server::frame_collector::FrameCollector::frame_queue_t = ClientCollectionParams::frame_queue_t |
Definition at line 72 of file collector.hpp.
irsol::server::frame_collector::FrameCollector::FrameCollector | ( | irsol::camera::Interface & | camera | ) |
Constructs a FrameCollector for the given camera interface.
camera | Reference to a camera interface for capturing frames. |
Definition at line 21 of file collector.cpp.
irsol::server::frame_collector::FrameCollector::~FrameCollector | ( | ) |
Destructor. Stops any running threads and cleans up resources.
Definition at line 26 of file collector.cpp.
|
private |
Removes schedules from the schedule map.
schedules | Vector of schedule times to clean up. |
Definition at line 342 of file collector.cpp.
|
private |
Collects clients who are scheduled to receive a frame at the given time.
now | Current timestamp. |
slack | Allowed slack between now and client's schedule for considering a client to be ready for receiving data. |
Definition at line 314 of file collector.cpp.
void irsol::server::frame_collector::FrameCollector::deregisterClient | ( | irsol::types::client_id_t | clientId | ) |
Deregisters a client and stops frame delivery.
clientId | The client's unique identifier. |
Definition at line 106 of file collector.cpp.
|
private |
Deregisters a client and stops frame delivery (not thread-safe).
clientId | The client's unique identifier. |
Definition at line 264 of file collector.cpp.
|
private |
Captures an image and returns it along with associated metadata.
std::nullopt
otherwise. Definition at line 351 of file collector.cpp.
bool irsol::server::frame_collector::FrameCollector::isBusy | ( | ) | const |
Checks whether the collector is currently serving any clients.
Definition at line 52 of file collector.cpp.
|
static |
Utility static function to create a shared pointer to a frame queue.
Clients may use this to obtain a queue prior to registering with the collector.
Definition at line 16 of file collector.cpp.
void irsol::server::frame_collector::FrameCollector::registerClient | ( | irsol::types::client_id_t | clientId, |
double | fps, | ||
std::shared_ptr< frame_queue_t > | queue, | ||
int64_t | frameCount = -1 |
||
) |
Registers a client to receive frames at a specified frame rate.
clientId | Unique identifier for the client. |
fps | Desired frame rate (frames per second). If fps <= 0 and frameCount == 1, the client will receive a single frame immediately. Tipically used in gi command |
queue | Shared pointer to the client's frame queue. The collector will push frame data into this queue at the schedule defined by the client. |
frameCount | Number of frames to deliver; -1 means unlimited. After the last frame has been pushed to the client, the client is automatically deregistered and its queue marked as complete. |
Definition at line 58 of file collector.cpp.
|
private |
Runs the frame distribution loop in a background thread.
This method monitors client schedules, captures frames as needed, and distributes them to all clients whose scheduled delivery times fall within the current slack window. It also handles automatic deregistration of clients who have received their requested number of frames.
Definition at line 113 of file collector.cpp.
|
private |
Schedules the next frame delivery for a client.
clientId | The client to schedule. |
nextFrameDue | The next timestamp when the client should receive a frame. |
Definition at line 384 of file collector.cpp.
void irsol::server::frame_collector::FrameCollector::start | ( | ) |
Starts the frame collection and distribution thread.
If the collector is already running, this call is ignored.
Definition at line 32 of file collector.cpp.
void irsol::server::frame_collector::FrameCollector::stop | ( | ) |
Stops the frame collector and joins worker threads.
After calling stop(), no further frames will be delivered to clients.
Definition at line 43 of file collector.cpp.
|
private |
Reference to the camera interface used for capturing.
Definition at line 214 of file collector.hpp.
|
private |
Stores parameters for each registered client.
Definition at line 218 of file collector.hpp.
|
private |
Protects access to m_clients and m_scheduleMap.
Definition at line 216 of file collector.hpp.
|
private |
Thread responsible for frame distribution.
Definition at line 223 of file collector.hpp.
|
private |
Signals when a new client is scheduled.
Definition at line 222 of file collector.hpp.
|
private |
Maps timestamps to client IDs scheduled at that time.
Definition at line 220 of file collector.hpp.
|
private |
Indicates whether the collector is stopping due to an external request.
Definition at line 225 of file collector.hpp.
|
staticconstexpr |
Slack window for batching frame delivery to clients.
Clients whose scheduled delivery times fall within this window of a captured frame will all receive the same frame. This reduces redundant captures and improves efficiency.
Definition at line 91 of file collector.hpp.