OptrisOptrisDocumentation

Documentation/OTC SDK/Optris.Sdk — C# API referencev12.0

RecordingSession

public sealed class RecordingSession : IDisposable

Several cameras into one file, on one clock.

An operator watching both sides of a furnace, or three points on a line, wants the dashboard recorded and replayed as it was seen: every camera in step, one file to archive and to send. One recording per camera with a manifest beside them does not do that, and the reason is worth stating because it is not obvious.

Each recording's presentation times are measured from its own first frame. Two cameras are never started in the same microsecond, so the two files disagree from the start; and a camera that reports no capture stamp of its own (which a PI over USB does, on every frame) has its times synthesised from its nominal rate, so a camera running at 9.8 Hz and calling itself 10 Hz drifts by 2 % of elapsed time, without bound, for as long as the recording runs. Measured on a ten-second two-camera run: at nine seconds in, the two files showed 9000 ms and 9184 ms at the same point on the transport bar.

A session fixes that by taking the timing decision away from the cameras. One clock stamps every frame of every source, so "what did camera 1 see at the moment camera 0 saw this" has an answer in the file rather than in an application's guess.

Frames in, not cameras in. You hand each camera's frames to the session as they arrive, one call per frame. A session that took cameras and subscribed to them would take the frames away from the code drawing your dashboard, and on a live canvas that is the one thing it must not do.

This is not a replacement for the per-camera recorder and does not change it. ThermalCamera.StartRecording(…) behaves exactly as it did, single-camera recordings open exactly as they did, and a session file is an ordinary Matroska file that any tool can probe.

Example

using var session = RecordingSession.Create("dashboard.mkv");

int north = session.AddSource(new SessionSource
{
    Width = camera.Width,
    Height = camera.Height,
    FrameRate = 32.0f,
    Name = "Furnace north",
    Serial = camera.Serial,
});
session.Open();

await foreach (ThermalFrame frame in camera.FramesAsync(token))
{
    session.WriteFrame(north, frame);
}

RecordingSession: properties

public ulong BytesWritten { get; }

Total bytes the session has written, across every segment.

  • Throws ObjectDisposedException: The session has been disposed.
public TimeSpan Elapsed { get; }

Session time now, since the session opened.

  • Throws ObjectDisposedException: The session has been disposed.
public int SourceCount { get; }

How many sources this session has.

  • Throws ObjectDisposedException: The session has been disposed.
public Nullable<DateTimeOffset> StartedAt { get; }

When the session started, as a date.

  • Throws ObjectDisposedException: The session has been disposed.

RecordingSession: methods

public int AddSource(SessionSource source)

Add one camera to the session.

  • source: The camera's dimensions, rate and identity.

Returns: The source index (0, 1, …) to pass to every other call about this camera.

  • Throws ArgumentNullException: source is null.
  • Throws SdkConfigurationException: A zero width or height.
  • Throws InvalidOperationException: The session is already open.
  • Throws ObjectDisposedException: The session has been disposed.

Must be called before RecordingSession.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.

public static RecordingSession Create(string outputPath, SessionOptions? options = null)

Create a session. Add sources next, then open it.

  • outputPath: Where to write. .mkv recommended; every segment is named from it. Ask for a segment limit and every part carries a suffix, the first one included -- run.mkv writes run_part000.mkv, run_part001.mkv and so on, and nothing at run.mkv. That changed in API version 24 and it breaks an application that passed a path and then reopened it: take the name from the RecordingPhase.SegmentCompleted event, which reports every finished file including the first, or open _part000 itself. With no segment limit outputPath is used exactly as given, so only sessions that asked to roll over are affected.
  • options: Session-wide settings, or null for the defaults.

Returns: The session.

  • Throws ArgumentException: outputPath is null or empty.
  • Throws SdkConfigurationException: A setting the SDK will not accept: a queue depth of zero.
public void Dispose()

Finish the session and release it.

Disposing twice is safe.

public void EndSource(int source, string? reason = null)

This camera is finished; the rest of the session carries on without it.

  • source: The index RecordingSession.AddSource(…) returned.

  • reason: Why it ended. Written into the file and carried on the event.

  • Throws ArgumentOutOfRangeException: source is not one of this session's sources.

  • Throws ObjectDisposedException: The session has been disposed.

The track 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. Use RecordingSession.MarkSourceLost(…) instead for a camera that may come back; this one is final, and frames written for the source afterwards are refused.

public SessionSourceState GetSourceState(int source)

What one source is doing right now.

Returns: See SessionSourceState.

  • Throws ArgumentOutOfRangeException: source is not one of this session's sources.
  • Throws ObjectDisposedException: The session has been disposed.
public SessionSourceStatistics GetStatistics(int source)

What one source has done.

Returns: Its counts and spans.

  • Throws ArgumentOutOfRangeException: source is not one of this session's sources.
  • Throws ObjectDisposedException: The session has been disposed.

Read these after stopping the session when the numbers have to be exact. While it runs they describe what the writer thread has finished, which can be a whole queue behind what the application has handed over.

public void MarkSourceLost(int source, string? reason = null)

This camera has dropped out. Keep its track open and record the outage.

  • source: The index RecordingSession.AddSource(…) returned.

  • reason: What happened, in whatever words you have: "USB device removed", "no frame for 5 s". Written into the file and carried on the event.

  • Throws ArgumentOutOfRangeException: source is not one of this session's sources.

  • Throws ObjectDisposedException: The session has been disposed.

The difference between this and RecordingSession.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.

Ended is a finished track. Nothing more will be written to it, ever.

A record goes into the file 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 and 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.

Calling it for a source already lost does nothing. Calling RecordingSession.MarkSourceResumed(…) is optional: a frame arriving is what "it is back" means, and the outage closes either way.

public void MarkSourceResumed(int source)

This camera is back. Its track continues where the session clock now is.

  • source: The index RecordingSession.AddSource(…) returned.

  • Throws ArgumentOutOfRangeException: source is not one of this session's sources.

  • Throws ObjectDisposedException: The session has been disposed.

Optional: the first frame after an outage resumes the source by itself. 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 on a camera that calibrates on connect is most of a shutter cycle later.

public void OnEvent(Action<RecordingEvent>? handler)

Be told that the session started or stopped, that a segment finished, that a write failed, or that one camera was lost, came back or finished.

  • handler: What to call, or null to unregister.

  • Throws ObjectDisposedException: The session has been disposed.

RecordingEvent.Source names the camera, or is −1 for an event about the session as a whole. Before this existed a session's failure event carried no source at all, so an application with five cameras learned that a write had failed and could not find out which.

The handler runs on the session's writer thread. Do not dispose the session from it; set a flag and act on a thread of your own. Register before RecordingSession.Open; pass null to stop delivery.

public void Open()

Open the output file and write its header now.

  • Throws SdkIOException: The path cannot be written.
  • Throws InvalidOperationException: No source has been added.
  • Throws ObjectDisposedException: The session has been disposed.

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 session time means "since the file opened" rather than "since some camera happened to produce something".

public void Stop()

Finish the session: drain the queue, flush every encoder, write the trailer and close the file.

  • Throws ObjectDisposedException: The session has been disposed.

When this returns the file is complete on disk, and the statistics describe it rather than however far behind the writer thread was. Called for you by RecordingSession.Dispose; calling it twice is harmless.

public void WriteColorFrame(int source, ReadOnlySpan<byte> rgb, uint width, uint height, string? annotation = null)

Write one visual camera's picture into the session.

  • source: The index RecordingSession.AddSource(…) returned, for a source added with SessionSourceKind.Color.

  • rgb: Packed RGB24, row-major from the top-left, three bytes a pixel and no row padding. Copied.

  • width: The picture's width.

  • height: The picture's height.

  • annotation: Per-frame region record as JSON, or null.

  • Throws SdkConfigurationException: An unknown source, a thermal source, a zero width or height, or a span that is not exactly width × height × 3 bytes. A buffer larger than the picture is a caller who thinks the stride is padded, and copying its first bytes would put a sheared picture in the file rather than an error here.

  • Throws InvalidOperationException: The session is not open, has been stopped, or this source has ended.

  • Throws ObjectDisposedException: The session has been disposed.

Identical to RecordingSession.WriteFrame(…) in every respect that matters: the same session clock, stamped on this thread, the same queue, the same statistics. A visual camera recorded this way is in step with the thermal ones because it is on the same clock, not because anything aligned it afterwards.

public void WriteFrame(int source, ThermalFrame frame, string? annotation = null)

Write one thermal camera's frame into the session.

  • source: The index RecordingSession.AddSource(…) returned.

  • frame: The camera's frame. Copied; you may reuse or dispose it the moment this returns.

  • annotation: Per-frame region record as JSON, or null.

  • Throws ArgumentNullException: frame is null.

  • Throws SdkConfigurationException: An unknown source, a frame that is not the size the source was declared with, or a visual source; use RecordingSession.WriteColorFrame(…) for one of those.

  • Throws SdkIOException: The hand-over queue was full under SessionBacklogPolicy.DropNewest, so the frame is not in the file. The running count is in SessionSourceStatistics.FramesDroppedBacklog.

  • Throws InvalidOperationException: The session is not open, has been stopped, or this source has ended.

  • Throws ObjectDisposedException: The session has been disposed.

Stamps the frame with the session clock now, on this thread, then hands it to the session's writer. Safe to call from several threads at once; it has to be, because N cameras deliver on N threads.