OptrisOptrisDocumentation

Documentation/OTC SDK/OTC SDK — C++ API referencev12.0

optris::sdk::rec::RingBuffer

Stores raw ThermalFrame objects (pixel data + metadata) for pre-roll capture. When the buffer is full, the oldest frame is overwritten.

Thread safety: Thread safety All public methods are mutex-protected. Push() is called from the SDK callback thread; Flush() is called from the recording-start / recording controller thread.

Public functions

RingBuffer

RingBuffer(size_t capacity)

Parameters

Name Description
capacity Maximum number of frames to store.

Push

auto Push(const optris::sdk::ThermalFrame &frame) -> bool

Push a frame into the buffer.

If the buffer is full, the oldest frame is overwritten. The frame is copied (deep copy of pixel data).

A frame of a different size: A frame of a different size Buffered frames are flushed into one video stream, which is opened at one size, so a buffer holding two geometries cannot be written out -- the flush fails on the first frame of the wrong size and the whole pre-roll is lost. That is not hypothetical: a camera reports one geometry during startup calibration and another once it settles, and an application that armed pre-roll too early lost it every time.

So a frame whose size differs from what is stored empties the buffer first. The pre-roll is then short rather than gone, and it refills at the new size.

The abandoned frames' memory goes back: The abandoned frames' memory goes back The emptying releases those slots rather than counting them out. It used to reset two counters and nothing else, so every abandoned slot kept its old-geometry pixels until the ring refilled -- measured at 618,496 B returned of 98,304,000 B on a 160-frame 640x480 ring, which is one slot, the one the new frame was moved into. A 320-frame ring is about 200 MB held that way, the refill is as slow as the camera is, and a format settle is routine: InitState::Restart and the mode-switch path both cause one. It also applies after a Clear(), where the counters no longer describe what the slots hold -- the geometry is remembered separately so that case is not invisible.

The span, when one is set: The span, when one is set A frame older than SetSpanUs() behind the newest is dropped, so the buffer holds the time it was asked for whatever rate frames arrive at. See SetSpanUs() for why a frame count alone is not enough.

Returns: true when the buffer was emptied for that reason. The caller may want to say so; nothing breaks if it ignores it.

Push

auto Push(Buffered entry) -> bool

Push a frame together with the state it arrived with.

The overload the recording controller uses. The single-argument one above is this with an empty Buffered around it, and is what a caller driving a RingBuffer by hand wants. See Buffered for what is carried and why.

Flush

auto Flush() -> std::vector< Buffered >

Flush all buffered frames in chronological order.

Returns the frames oldest-first, each with the state it arrived with, and clears the buffer.

Clear

void Clear()

Clear all buffered frames.

Keeps their memory for a refill at the same geometry; see Release() for the method that hands it back. A refill at a different geometry can reuse none of it and releases it, so the kept allocations are only ever the ones a refill will use.

Release

void Release()

Clear all buffered frames and release their memory.

Clear() resets the counters and leaves every stored frame's pixels allocated, which is right for a buffer that is about to refill and wrong for one that is being turned off. Measured on a 640x480 at 32 Hz with 10 s armed: 320 frames cost +200 MB over the process's baseline, and a Clear() gave none of it back -- DisablePreRoll() called only Clear(), while its documentation promised to "stop keeping a pre-roll buffer and release its memory".

The buffer is usable afterwards, at the same capacity; it simply has to allocate again as it refills.

Size

auto Size() const -> size_t

Number of frames currently in the buffer.

Capacity

auto Capacity() const -> size_t

Maximum number of frames the buffer can hold.

SetSpanUs

void SetSpanUs(uint64_t span_us)

Bound the buffer by time as well as by frame count.

Parameters

Name Description
span_us How far back the buffer may reach, in microseconds of frame timestamp. Zero turns the bound off, which is the default and what a caller who only wants N frames gets.

Why a frame count is not enough: Why a frame count is not enough A capacity is derived from a rate, and the rate the ring was sized from is the sensor's -- GetDeviceInfo().frame_rate -- not the rate at which frames are delivered. Those differ whenever ProcessingConfig::subsampled_framerate is set: on a 382x288 camera at 27 Hz subsampled to 1 Hz, a 30 s request kept 810 frames, which is 13.5 minutes of history, while GetPreRoll() reported 30 s. A pre-roll that holds twenty-seven times what it says it holds is both a memory surprise and a recording whose first file is minutes of material nobody asked to keep.

Evicting by timestamp makes the span the thing that is enforced and leaves the capacity as what it should always have been: a memory bound. A camera that delivers faster than the sensor rate is still bounded by the capacity, so neither limit is redundant.

A frame whose timestamp is not ahead of the newest stored one -- zero, or a clock that stepped back -- evicts nothing, so a source with no usable stamps behaves exactly as it did before.

HeldSpanUs

auto HeldSpanUs() const -> uint64_t

The span the buffer currently holds, in microseconds.

The newest stored timestamp less the oldest. Zero when fewer than two frames are stored. This is the span actually held, which is the number SetSpanUs() exists to make equal to the one asked for.

HoldsFullSpan

auto HoldsFullSpan() const -> bool

Whether the buffer holds all the history it was asked for.

True once either bound has bitten: a frame has been evicted for being older than SetSpanUs(), or the buffer has reached its capacity. Either way nothing more can be held, so this is the honest answer to "is the pre-roll full".

Why it is not: Why it is not Size() == Capacity() The capacity is derived from the sensor's rate; the span is enforced against the rate frames are delivered at. On a camera that delivers more slowly -- subsampled, or restricted to 9 fps -- the count bound is never reached, so Size() == Capacity() is false for as long as the ring is armed while the ring holds its whole duration. An application waiting on that comparison waits for ever; the Python binding's PreRollState.full did exactly that, and its own documentation told callers to wait for it.

Why eviction and not a comparison of HeldSpanUs() against the span: a span is measured between the oldest and the newest stamp, so it tops out one delivered frame period short of the span asked for, and that period is the number a caller outside the SDK does not have. An eviction is exact and needs no tolerance.

Not sticky: Flush(), Clear(), Release() and Resize() all empty the buffer, and this goes false with them. A recording's start flushes the pre-roll, so a sticky answer would announce the next recording's pre-roll as full before a frame was back in it.

And SetSpanUs() clears it when the span grows, or when the bound is turned off: the eviction proves the buffer reaches as far back as the span it was bounded by then, which is no longer the span being asked for. A shorter span leaves it standing, because a buffer that held 5 s certainly holds 3 s. Enabling a pre-roll never meets this -- it resizes first -- so it is for a direct caller of this class.

A source whose timestamps are all zero evicts nothing, so this falls back to the capacity alone, exactly as the buffer behaved before the span bound existed.

Resize

void Resize(size_t new_capacity)

Resize the buffer capacity.

Clears all existing frames and releases their memory.