IRSOL
C++ code implementing socket server for interacting with Baumer camera.
collector.hpp
Go to the documentation of this file.
1#pragma once
2
6#include "irsol/types.hpp"
7#include "irsol/utils.hpp"
8
9#include <atomic>
10#include <condition_variable>
11#include <deque>
12#include <map>
13#include <memory>
14#include <thread>
15#include <unordered_map>
16#include <vector>
17
29namespace irsol {
30namespace server {
31namespace frame_collector {
32
70{
71public:
73
83 static std::shared_ptr<frame_queue_t> makeQueuePtr();
84
91 constexpr static irsol::types::duration_t SLACK = std::chrono::milliseconds(50);
92
99
104
110 void start();
111
117 void stop();
118
124 bool isBusy() const;
125
140 void registerClient(
142 double fps,
143 std::shared_ptr<frame_queue_t> queue,
144 int64_t frameCount = -1);
145
155
156private:
165 void run();
166
176
186 std::pair<std::vector<irsol::types::client_id_t>, std::vector<irsol::types::timepoint_t>>
188
194 void cleanUpSchedule(const std::vector<irsol::types::timepoint_t> schedules);
195
202 std::optional<std::pair<FrameMetadata, std::vector<irsol::types::byte_t>>> grabImageData() const;
203
212 bool schedule(const irsol::types::client_id_t& clientId, irsol::types::timepoint_t nextFrameDue);
213
215
216 std::mutex m_clientsMutex;
217 std::unordered_map<irsol::types::client_id_t, ClientCollectionParams>
219 std::map<irsol::types::timepoint_t, std::vector<irsol::types::client_id_t>>
221
222 std::condition_variable m_scheduleCondition;
224
225 std::atomic<bool> m_stop{
226 false};
227};
228
229} // namespace frame_collector
230} // namespace server
231} // namespace irsol
High-level wrapper around the NeoAPI camera for synchronized access.
Definition interface.hpp:61
Coordinates frame acquisition from a camera and distributes frames to registered clients.
Definition collector.hpp:70
std::atomic< bool > m_stop
Indicates whether the collector is stopping due to an external request.
void run()
Runs the frame distribution loop in a background thread.
void deregisterClient(irsol::types::client_id_t clientId)
Deregisters a client and stops frame delivery.
void start()
Starts the frame collection and distribution thread.
Definition collector.cpp:32
void deregisterClientNonThreadSafe(irsol::types::client_id_t clientId)
Deregisters a client and stops frame delivery (not thread-safe).
std::thread m_distributorThread
Thread responsible for frame distribution.
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.
std::map< irsol::types::timepoint_t, std::vector< irsol::types::client_id_t > > m_scheduleMap
Maps timestamps to client IDs scheduled at that time.
~FrameCollector()
Destructor. Stops any running threads and cleans up resources.
Definition collector.cpp:26
std::condition_variable m_scheduleCondition
Signals when a new client is scheduled.
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.
Definition collector.cpp:58
irsol::camera::Interface & m_cam
Reference to the camera interface used for capturing.
bool schedule(const irsol::types::client_id_t &clientId, irsol::types::timepoint_t nextFrameDue)
Schedules the next frame delivery for a client.
bool isBusy() const
Checks whether the collector is currently serving any clients.
Definition collector.cpp:52
std::unordered_map< irsol::types::client_id_t, ClientCollectionParams > m_clients
Stores parameters for each registered client.
static constexpr irsol::types::duration_t SLACK
Slack window for batching frame delivery to clients.
Definition collector.hpp:91
std::optional< std::pair< FrameMetadata, std::vector< irsol::types::byte_t > > > grabImageData() const
Captures an image and returns it along with associated metadata.
void stop()
Stops the frame collector and joins worker threads.
Definition collector.cpp:43
std::mutex m_clientsMutex
Protects access to m_clients and m_scheduleMap.
static std::shared_ptr< frame_queue_t > makeQueuePtr()
Utility static function to create a shared pointer to a frame queue.
Definition collector.cpp:16
void cleanUpSchedule(const std::vector< irsol::types::timepoint_t > schedules)
Removes schedules from the schedule map.
A thread-safe, optionally bounded queue with blocking push and pop operations.
Definition queue.hpp:66
High-level wrapper around NeoAPI camera control for the irsol library.
clock_t::duration duration_t
Alias for a duration of time as defined by clock_t.
Definition types.hpp:128
std::string client_id_t
Represents a unique client identifier. Typically used to identify connected clients by string IDs.
Definition types.hpp:55
clock_t::time_point timepoint_t
Alias for a point in time as defined by clock_t.
Definition types.hpp:120
irsol::utils::SafeQueue< std::unique_ptr< Frame > > frame_queue_t
Definition params.hpp:17
Core type definitions for networking, time handling, and protocol values used throughout the irsol li...
General utility functions used throughout the irsol library.