61 lines
1.9 KiB
C++
61 lines
1.9 KiB
C++
#ifndef CMVR_ES_GRPC_CAMERA_STREAM_POLICY_H
|
|
#define CMVR_ES_GRPC_CAMERA_STREAM_POLICY_H
|
|
|
|
#pragma once
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
|
|
namespace cmvr::service {
|
|
|
|
inline constexpr size_t kDefaultCameraStreamMaxPendingFrames = 2;
|
|
inline constexpr uint32_t kDefaultCameraStreamMaxFrameAgeMs = 250;
|
|
|
|
struct CameraStreamLowLatencyConfig {
|
|
size_t max_pending_frames{kDefaultCameraStreamMaxPendingFrames};
|
|
std::chrono::milliseconds max_frame_age{
|
|
kDefaultCameraStreamMaxFrameAgeMs};
|
|
};
|
|
|
|
inline CameraStreamLowLatencyConfig makeCameraStreamLowLatencyConfig(
|
|
const uint32_t max_pending_frames,
|
|
const uint32_t max_frame_age_ms) noexcept {
|
|
CameraStreamLowLatencyConfig config;
|
|
config.max_pending_frames = max_pending_frames == 0
|
|
? kDefaultCameraStreamMaxPendingFrames
|
|
: static_cast<size_t>(max_pending_frames);
|
|
config.max_frame_age = std::chrono::milliseconds(
|
|
max_frame_age_ms == 0
|
|
? kDefaultCameraStreamMaxFrameAgeMs
|
|
: max_frame_age_ms);
|
|
return config;
|
|
}
|
|
|
|
inline std::optional<uint64_t> cameraFrameAgeNs(
|
|
const uint64_t capture_time_ns,
|
|
const uint64_t now_ns) noexcept {
|
|
if (capture_time_ns == 0 || now_ns < capture_time_ns) {
|
|
return std::nullopt;
|
|
}
|
|
return now_ns - capture_time_ns;
|
|
}
|
|
|
|
inline bool cameraFrameExceedsAgeLimit(
|
|
const uint64_t capture_time_ns,
|
|
const uint64_t now_ns,
|
|
const std::chrono::milliseconds max_frame_age) noexcept {
|
|
const auto age_ns = cameraFrameAgeNs(capture_time_ns, now_ns);
|
|
if (!age_ns || max_frame_age.count() <= 0) {
|
|
return false;
|
|
}
|
|
const auto max_age_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
max_frame_age).count();
|
|
return *age_ns > static_cast<uint64_t>(max_age_ns);
|
|
}
|
|
|
|
} // namespace cmvr::service
|
|
|
|
#endif // CMVR_ES_GRPC_CAMERA_STREAM_POLICY_H
|