optris::sdk::rec::SessionRecorder
Records N cameras into one Matroska file on one clock.
Thread safety: Thread safety WriteFrame(), EndSource() and every getter are safe to call from any thread, and WriteFrame() is safe to call from N threads at once -- which is the point, since N cameras deliver on N callback threads. This is the one thing a session does that rec::Recorder deliberately refuses to do; rec::Recorder is single-writer and enforces it, so the session owns a writer thread of its own and the camera threads hand frames to it.
The stamp is taken on the calling thread, inside WriteFrame(), before the frame is queued. It is not taken when the writer thread gets to it. That distinction is the whole feature: a queue that stamps at the far end would put the session's own scheduling jitter into the recorded times, which is the drift this exists to remove, reintroduced one layer up.
AddSource() and Open() are not thread-safe against anything and are not meant to be -- they run once, while the session is being set up.
Public static functions
Create
static auto Create(const SessionConfig &config) -> std::unique_ptr< SessionRecorder >
Create a session. Add sources next, then Open().
Exceptions
| Name | Description |
|---|---|
InvalidArgumentException |
for an empty output path or a queue_depth of zero. |
IoException |
when the container cannot be allocated. |
Public functions
~SessionRecorder
~SessionRecorder()
SessionRecorder
SessionRecorder(const SessionRecorder &)=delete
operator=
SessionRecorder & operator=(const SessionRecorder &)=delete
AddSource
auto AddSource(const SessionSourceConfig &config) -> int
Add one camera to the session.
Must be called before Open() and before the first frame: every track of a Matroska file is declared in its header, so a camera that joins after the header is down cannot be added to this file.
Parameters
| Name | Description |
|---|---|
config |
The camera's dimensions, rate and identity. |
Returns: The source index (0, 1, ...) to pass to WriteFrame().
Exceptions
| Name | Description |
|---|---|
InvalidStateException |
when the session is already open, so no track can be added. |
InvalidArgumentException |
for a zero width or height. |
Open
void Open()
Open the output file and write its header now.
Worth calling rather than letting the first frame do it: until the file is opened, an unwritable path is indistinguishable from a good one, and by the first frame the caller who could have been told is gone. Calling it twice does nothing.
The session clock's origin is taken here, so the first source to deliver after it starts at a small positive time rather than at zero. Session time therefore means "since the file opened", not "since some camera happened to produce something".
Exceptions
| Name | Description |
|---|---|
InvalidStateException |
when no source has been added -- a session with no sources has nothing to record. Also when the session has been stopped: its file is finished and it cannot be opened again. Create a new SessionRecorder for a new recording. |
IoException |
naming the reason the file could not be opened. |
WriteFrame
void WriteFrame(int source, const optris::sdk::ThermalFrame &frame, const std::string &annotation={}, const std::optional< RenderMeta > &render={})
Write one camera's frame into the session.
Stamps the frame with the session clock now, on this thread, then hands it to the session's writer. Returns without waiting for the encode unless SessionConfig::backlog_policy is SessionBacklogPolicy::kBlock and the queue is full.
Parameters
| Name | Description |
|---|---|
source |
Index from AddSource(). |
frame |
The camera's frame. Copied; the caller keeps ownership and may reuse it the moment this returns. |
annotation |
Per-frame region record, exactly as Recorder::WriteFrame() takes it. |
render |
What the colours meant for this frame, exactly as Recorder::WriteFrame() takes it. |
Exceptions
| Name | Description |
|---|---|
InvalidArgumentException |
for an unknown source, a source that is a visual camera, or a dimension mismatch. |
InvalidStateException |
for a session that is not open, that has been stopped, or whose track has already been ended with EndSource(). |
IoException |
when the hand-over queue is full under SessionBacklogPolicy::kDropNewest: the frame is not in the file and SessionSourceStats::frames_dropped_backlog counts it. Under SessionBacklogPolicy::kBlock this call waits instead and raises nothing. |
Note: A write failure that happens later, on the writer thread, cannot be raised here; it lands in SessionSourceStats::last_error and in the callback set by OnEvent().
WriteColorFrame
void WriteColorFrame(int source, const optris::sdk::RgbFrame &frame, const std::string &annotation={})
Write one visual camera's picture into the session.
The colour counterpart of WriteFrame(), and identical to it in every respect that matters: the same session clock, stamped on this thread before the frame is queued, the same hand-over queue, the same per-source statistics. A visual camera recorded this way is in step with the thermal ones because it is on the same clock, not because anything tried to align it afterwards.
The source must have been added with SessionSourceKind::kColor. A thermal source refuses this call.
Parameters
| Name | Description |
|---|---|
source |
Index from AddSource(). |
frame |
Packed RGB24 pixels, at the size the source was declared with. Copied; the caller may reuse it the moment this returns. |
annotation |
Per-frame region record, as WriteFrame() takes it. |
Exceptions
| Name | Description |
|---|---|
As |
WriteFrame(), with the source's kind reversed: a thermal source refuses this call. |
WriteColorFrame
void WriteColorFrame(int source, const uint8_t *rgb, size_t rgb_size, uint32_t width, uint32_t height, const std::string &annotation={})
Write one visual camera's picture from packed RGB24 bytes.
The same call, for a caller that has pixels rather than an RgbFrame -- a plugin, a V4L2 buffer, a decoder's output, which is where a visual camera's frames come from. It saves building an RgbFrame to have it copied straight back out again, and it is what the C entry point uses.
Parameters
| Name | Description |
|---|---|
source |
Index from AddSource(). |
rgb |
Packed RGB24, row-major from the top-left, three bytes a pixel and no row padding. Copied. |
rgb_size |
Bytes at rgb. Must be exactly width * height * 3; a short buffer is refused rather than read past the end. |
width, height |
The picture's size, which must be the size the source was declared with. |
annotation |
Per-frame region record, as WriteFrame() takes it. |
Exceptions
| Name | Description |
|---|---|
As |
the overload above, plus InvalidArgumentException for a null pointer, a zero dimension, or an rgb_size that is not exactly width * height * 3. |
MarkSourceLost
void MarkSourceLost(int source, const std::string &reason={})
This camera has dropped out. Keep its track open and record the outage.
The difference between this and EndSource() is the whole of what an application can say about a camera that stopped, and the two are not interchangeable:
- Lost is a hole. The source stays open, its track is not closed, and the moment frames arrive again they continue that same track at wherever the session clock has got to. Nothing is padded across the hole and no frame is invented, so the file shows the outage as the absence it was.
- Ended is a finished track. Nothing more will be written to it, ever.
Before this existed the only answer was EndSource(), so a camera that dropped for four seconds and came back lost the rest of the session.
What goes into the file: What goes into the file A record on this source's telemetry track at the moment of the call, saying the source was lost and why. That is what lets a player tell a disconnection from an ordinary gap -- a busy disk, a shutter cycle, a camera that was idle all look the same in the track itself -- and therefore what lets it show a disconnected picture across the span instead of holding the last frame. A held last frame reads as a live scene, which on a furnace is the wrong thing to show somebody.
Calling it for a source already lost does nothing: one outage is one outage however many times the transport reports it.
Parameters
| Name | Description |
|---|---|
source |
Index from AddSource(). |
reason |
What happened, in whatever words the application has -- "USB device removed", "no frame for 5 s". Written into the file and carried on the event. May be empty. |
MarkSourceResumed
void MarkSourceResumed(int source)
This camera is back. Its track continues where the session clock now is.
Optional: the first frame after an outage resumes the source by itself, because a frame arriving is what "it is back" means. Calling it is how an application that knows the transport reconnected says so before the first frame -- so the outage recorded in the file ends at the reconnection rather than at the first frame after it, which can be most of a shutter cycle later.
Does nothing for a source that is not lost.
EndSource
void EndSource(int source, const std::string &reason={})
This camera is finished; the rest of the session carries on without it.
The deliberate answer to "a camera dies mid-recording". The track simply ends where the camera stopped: nothing is padded, no frame is invented, and the other cameras are not slowed to it or stopped with it. A dashboard that loses one of two cameras at minute three is still worth the remaining seven minutes, and a file that says so is worth more than one that quietly repeats a dead camera's last frame for seven minutes.
Use MarkSourceLost() instead for a camera that may come back; this one is final.
Frames written for an ended source after this are refused. A RecordingPhase::kSourceEnded event is emitted naming the source and reason -- before this, EndSource() emitted nothing at all and an application found out by noticing that WriteFrame() had begun failing.
Parameters
| Name | Description |
|---|---|
source |
Index from AddSource(). |
reason |
Why it ended. Written into the file and carried on the event. May be empty. |
GetSourceCount
auto GetSourceCount() const -> int
How many sources this session has.
GetSourceState
auto GetSourceState(int source) const -> SessionSourceState
What one source is doing right now. See SessionSourceState.
GetStatistics
auto GetStatistics(int source) const -> SessionSourceStats
What one source has done. Index out of range gives a zeroed struct.
GetElapsedUs
auto GetElapsedUs() const -> uint64_t
Session time now, in microseconds since Open().
Zero before Open(). Monotonic, by construction of the clock; it is a stopwatch, not a date.
GetStartHostTimeUs
auto GetStartHostTimeUs() const -> uint64_t
When the session started, as a date: microseconds since the Unix epoch, from std::chrono::system_clock.
Read once, at Open(), in the same breath as the monotonic origin -- the convention the rest of the SDK keeps for a pair like this (see AlarmEvent::host_time_us and ShutterEvent::host_time_us). Neither is derived from the other, and this one is not monotonic: NTP can step it. Use the session clock to measure, and this to say when.
Zero before Open(), or if the system clock could not be read.
GetBytesWritten
auto GetBytesWritten() const -> uint64_t
Total bytes the session has written, across every segment.
OnEvent
void OnEvent(std::function< void(const RecordingEvent &)> handler)
Be told that a segment finished or that a write failed.
Delivered on the session's writer thread. Failure is reported once per source per session, not once per lost frame: a full disk fails every frame, and one notification per frame per camera is a flood rather than a report. The running counts are in GetStatistics().
Register before Open(). Pass nullptr to stop delivery.
Stop
void Stop()
Finish the session: drain the queue, flush every encoder, write the trailer and close the file.
When this returns the file is complete on disk. Called by the destructor; calling it twice is harmless.