feat(match): 同步比赛计时与装甲板扣血许可
构建客户端 / 检查(类型 + 单元测试) (push) Canceled after 0s
构建客户端 / Linux x64(deb + AppImage) (push) Canceled after 0s
构建客户端 / Windows x64(NSIS 安装包) (push) Canceled after 0s
构建客户端 / macOS 通用(.app + .dmg) (push) Canceled after 0s

- feat(client): 增加裁判比赛计时、技术暂停与选手端局域网同步
- feat(armor): 红蓝共用通用固件,按客户端选队并显示网络连接状态
- feat(armor): 仅在对局进行中允许扣血,其他阶段保留受击闪烁
- feat(client): 将计时置于血条中间并增加开赛前五秒全屏动画
- fix(client): 修复连续血条、对手镜像和图传断连灰度遮罩
- fix(client): 统一界面中文字体并整理各页面文案
- fix(maintenance): 备份迁移装甲板分区,恢复 SDK 构建与固件打包
- refactor(repo): 按 armor、vt 和 client 整理工程与脚本,移除旧参考实现
- test: 补充线路、受击许可、回环 UDP、维护和 HUD 验证
- docs: 同步构建、协议、比赛控制和硬件验证边界

验证:前端构建与 19 项相关测试通过;核心 91 项单元测试与 2 项回环 UDP 集成测试通过;装甲板规则 25 项、线路 26 项、Tauri 51 项测试通过;独立探测器 5 项与目录回归 4 项通过。完成 C3 两组阈值实际编译、导出与 ZIP 校验,检查 900×600 / 1280×720 页面、倒计时和随包中文字体。
未执行烧录、板上运行、跨电脑广播或硬件稳定性验证。
This commit is contained in:
sloray committed 2026-10-22 17:08:49 +08:00
1 parent 72901939c6
commit f7ca246b68
581 files changed
+6596 -7683

No files matched your search

+429
View File
@@ -0,0 +1,429 @@
//! Thin safe wrapper over the `cam_wrapper` C component (see
//! `components/cam_wrapper`).
//!
//! The pin map matches the XIAO ESP32S3 Sense camera connector, which is the
//! common OV2640/OV3660 wiring on an ESP32-S3-N16R8. Adjust `PINS` at the
//! bottom of this file if your board wires the DVP bus differently.
use std::ffi::CStr;
use anyhow::{anyhow, Result};
use esp_idf_svc::sys;
// bindgen names C enum variants `<enum type>_<VARIANT>`, so alias them to
// something readable here instead of scattering those prefixes around.
use esp_idf_svc::sys::camera::{
cam_wrapper_fb_location_t_CAM_WRAPPER_FB_IN_PSRAM as FB_IN_PSRAM,
cam_wrapper_framesize_t_CAM_WRAPPER_FRAMESIZE_CIF as FRAMESIZE_CIF,
cam_wrapper_framesize_t_CAM_WRAPPER_FRAMESIZE_HVGA as FRAMESIZE_HVGA,
cam_wrapper_framesize_t_CAM_WRAPPER_FRAMESIZE_QQVGA as FRAMESIZE_QQVGA,
cam_wrapper_framesize_t_CAM_WRAPPER_FRAMESIZE_QVGA as FRAMESIZE_QVGA,
cam_wrapper_framesize_t_CAM_WRAPPER_FRAMESIZE_SVGA as FRAMESIZE_SVGA,
cam_wrapper_framesize_t_CAM_WRAPPER_FRAMESIZE_SXGA as FRAMESIZE_SXGA,
cam_wrapper_framesize_t_CAM_WRAPPER_FRAMESIZE_UXGA as FRAMESIZE_UXGA,
cam_wrapper_framesize_t_CAM_WRAPPER_FRAMESIZE_VGA as FRAMESIZE_VGA,
cam_wrapper_framesize_t_CAM_WRAPPER_FRAMESIZE_XGA as FRAMESIZE_XGA,
cam_wrapper_grab_mode_t_CAM_WRAPPER_GRAB_LATEST as GRAB_LATEST,
cam_wrapper_pixel_format_t_CAM_WRAPPER_PIXFORMAT_JPEG as PIXFORMAT_JPEG,
cam_wrapper_sensor_t_CAM_WRAPPER_SENSOR_AUTO as SENSOR_AUTO,
cam_wrapper_sensor_t_CAM_WRAPPER_SENSOR_OV2640 as SENSOR_OV2640,
cam_wrapper_sensor_t_CAM_WRAPPER_SENSOR_OV3660 as SENSOR_OV3660,
cam_wrapper_sensor_t_CAM_WRAPPER_SENSOR_OV5640 as SENSOR_OV5640,
};
/// Capture resolution.
///
/// Anything above CIF needs PSRAM, which the N16R8 has; the bandwidth cost of
/// the bigger sizes is real, so start at VGA and only go higher if the link
/// keeps up.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Resolution {
Qqvga,
Qvga,
Cif,
Hvga,
Vga,
Svga,
Xga,
Sxga,
Uxga,
}
impl Resolution {
fn framesize(self) -> sys::camera::cam_wrapper_framesize_t {
match self {
Resolution::Qqvga => FRAMESIZE_QQVGA,
Resolution::Qvga => FRAMESIZE_QVGA,
Resolution::Cif => FRAMESIZE_CIF,
Resolution::Hvga => FRAMESIZE_HVGA,
Resolution::Vga => FRAMESIZE_VGA,
Resolution::Svga => FRAMESIZE_SVGA,
Resolution::Xga => FRAMESIZE_XGA,
Resolution::Sxga => FRAMESIZE_SXGA,
Resolution::Uxga => FRAMESIZE_UXGA,
}
}
}
impl std::str::FromStr for Resolution {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"qqvga" => Ok(Resolution::Qqvga),
"qvga" => Ok(Resolution::Qvga),
"cif" => Ok(Resolution::Cif),
"hvga" => Ok(Resolution::Hvga),
"vga" => Ok(Resolution::Vga),
"svga" => Ok(Resolution::Svga),
"xga" => Ok(Resolution::Xga),
"sxga" => Ok(Resolution::Sxga),
"uxga" => Ok(Resolution::Uxga),
other => Err(anyhow!("unknown resolution {other:?}")),
}
}
}
use log::info;
/// Sensor override. `Auto` lets esp32-camera probe the SCCB bus, which is what
/// you want unless the probe is ambiguous.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SensorHint {
Auto,
Ov2640,
Ov3660,
Ov5640,
}
impl From<SensorHint> for sys::camera::cam_wrapper_sensor_t {
fn from(hint: SensorHint) -> Self {
match hint {
SensorHint::Auto => SENSOR_AUTO,
SensorHint::Ov2640 => SENSOR_OV2640,
SensorHint::Ov3660 => SENSOR_OV3660,
SensorHint::Ov5640 => SENSOR_OV5640,
}
}
}
/// One captured JPEG frame. Holds the driver frame buffer, so the data is only
/// valid until the `Frame` is dropped.
pub struct Frame {
fb: sys::camera::cam_wrapper_fb_t,
}
impl Frame {
pub fn captured_at_us(&self) -> u64 {
self.fb.capture_started_us.max(0) as u64
}
pub fn skipped_before(&self) -> u32 {
self.fb.skipped_before
}
pub fn data(&self) -> &[u8] {
if self.fb.data.is_null() || self.fb.len == 0 {
return &[];
}
// SAFETY: `data` points at `len` bytes owned by esp32-camera, which
// stays valid until this Frame is dropped (cam_wrapper_fb_release).
unsafe { std::slice::from_raw_parts(self.fb.data, self.fb.len) }
}
/// Size of the JPEG in bytes.
///
/// Reported in the sender's statistics, which is how a frame rate problem gets
/// attributed to the capture side or the transport side.
#[allow(dead_code)]
pub fn len(&self) -> usize {
self.fb.len
}
/// Capture width in pixels, as the sensor actually produced it (which can be
/// smaller than the requested frame size).
pub fn width(&self) -> usize {
self.fb.width
}
/// Capture height in pixels.
pub fn height(&self) -> usize {
self.fb.height
}
}
impl Drop for Frame {
fn drop(&mut self) {
unsafe { sys::camera::cam_wrapper_fb_release(&mut self.fb) }
}
}
/// The driver could not hand out a frame.
#[derive(Debug, Clone, Copy)]
pub struct FrameError;
impl std::fmt::Display for FrameError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("camera returned no frame")
}
}
impl std::error::Error for FrameError {}
/// An initialized camera.
///
/// Held for the lifetime of the program: esp32-camera keeps a task and the
/// frame buffers running, and tearing them down would only be needed to power
/// the sensor down, which this program never does.
pub struct Camera {
sensor: SensorHint,
}
impl Camera {
/// Brings up the sensor and blocks until it has produced frames.
///
/// `vsync_invert` should be true for a directly connected DVP VSYNC (the
/// esp32-camera default) and false if the board inverts the line in hardware.
pub fn init(
sensor: SensorHint,
resolution: Resolution,
jpeg_quality: u8,
vsync_invert: bool,
) -> Result<Self> {
if jpeg_quality > 63 {
return Err(anyhow!("jpeg_quality must be 0-63, got {jpeg_quality}"));
}
let config = sys::camera::cam_wrapper_config_t {
pin_pwdn: -1,
pin_reset: pins::RESET,
pin_xclk: pins::XCLK,
pin_sccb_sda: pins::SIOD,
pin_sccb_scl: pins::SIOC,
pin_d: pins::DATA,
pin_vsync: pins::VSYNC,
pin_href: pins::HREF,
pin_pclk: pins::PCLK,
// 20 MHz is what the OV5640 datasheet calls for, and esp32-camera's
// OV5640 driver is only PLL-tuned for it; raising this without
// retuning the sensor's own PLL just corrupts frames.
xclk_freq_hz: 20_000_000,
ledc_timer: 0,
ledc_channel: 0,
pixel_format: PIXFORMAT_JPEG,
frame_size: resolution.framesize(),
jpeg_quality: jpeg_quality as i32,
// Three buffers tolerate socket stalls. Two reduce capture queue
// depth, but may miss a VSYNC while the caller holds a buffer.
// Keep this selectable so latency and delivered FPS can be measured
// together instead of assuming the smallest pool always wins.
fb_count: {
let count: u32 = option_env!("CAM_FB_COUNT").unwrap_or("3").parse()?;
if !(2..=3).contains(&count) {
return Err(anyhow!("CAM_FB_COUNT must be 2 or 3"));
}
count as _
},
fb_location: FB_IN_PSRAM,
// LATEST keeps the newest frame in the queue instead of handing out
// whatever was captured first, which matters once a client is slow
// enough for frames to pile up.
grab_mode: GRAB_LATEST,
sccb_i2c_port: 0,
// esp32-camera inverts VSYNC by default, which is right for the
// large majority of DVP sensors. A board that inverts the line
// itself needs this cleared: a mismatch here does not fail, it
// makes the driver latch frames on the wrong edge and the frame rate
// collapses while the pictures still look plausible.
vsync_invert: if vsync_invert { 1 } else { 0 },
};
let rc = unsafe { sys::camera::cam_wrapper_init(&config, sensor.into()) };
if rc != 0 {
return Err(anyhow!(
"camera init failed (esp_err 0x{:x}) - check the DVP/SCCB wiring and that \
octal PSRAM is enabled",
rc
));
}
info!("camera ready: {}", Self::sensor_name());
Ok(Self { sensor })
}
pub fn sensor_name() -> String {
unsafe {
let ptr = sys::camera::cam_wrapper_sensor_name();
if ptr.is_null() {
String::from("unknown")
} else {
CStr::from_ptr(ptr).to_string_lossy().into_owned()
}
}
}
pub fn set_grayscale(&self, enabled: bool) -> Result<()> {
let rc = unsafe { sys::camera::cam_wrapper_set_grayscale(i32::from(enabled)) };
if rc != 0 {
return Err(anyhow!("sensor grayscale failed (esp_err 0x{rc:x})"));
}
Ok(())
}
/// A complete PLL profile; preserve HTS/VTS and the active readout window.
/// 610 MHz VCO, 61 MHz system clock, 15.25 MHz DVP PCLK at XCLK 20 MHz.
pub fn set_clock_profile(&self, profile: &str) -> Result<()> {
if profile == "default" {
return Ok(());
}
if profile != "vga30" {
return Err(anyhow!("CAM_CLOCK_PROFILE must be default or vga30"));
}
unsafe {
let sensor = sys::camera::esp_camera_sensor_get();
if sensor.is_null() || (*sensor).id.PID != 0x5640 {
return Err(anyhow!("vga30 requires OV5640"));
}
let set = (*sensor)
.set_pll
.ok_or_else(|| anyhow!("sensor PLL control unavailable"))?;
// sensor_t callback order differs from the driver's private helper:
// bypass, multiplier, sys_div, root_2x, pre_div, pclk_root_div,
// manual_pclk, pclk_div.
if set(sensor, 0, 61, 1, 0, 2, 2, 1, 4) != 0 {
return Err(anyhow!("OV5640 clock profile failed"));
}
std::thread::sleep(std::time::Duration::from_millis(300));
info!("OV5640 vga30 clock profile: VCO 610 MHz, SYSCLK 61 MHz, PCLK 15.25 MHz; HTS/VTS unchanged");
}
Ok(())
}
/// Fine AE target adjustment without changing the exposure ceiling or AWB.
pub fn set_ae_level(&self, level: i32) -> Result<()> {
if !(-5..=5).contains(&level) {
return Err(anyhow!("CAM_AE_LEVEL must be between -5 and 5"));
}
unsafe {
let sensor = sys::camera::esp_camera_sensor_get();
if sensor.is_null() || (*sensor).id.PID != 0x5640 {
return Err(anyhow!("CAM_AE_LEVEL requires OV5640"));
}
let set = (*sensor)
.set_ae_level
.ok_or_else(|| anyhow!("sensor AE target control unavailable"))?;
if set(sensor, level) != 0 {
return Err(anyhow!("OV5640 AE target adjustment failed"));
}
}
info!(
"OV5640 AE target override: driver level {level}, target {}/55; AEC/AGC retained",
(level + 5) * 10 + 5
);
Ok(())
}
pub fn probe_frame_rate(&self) -> f32 {
unsafe { sys::camera::cam_wrapper_probe_frame_rate() }
}
pub fn set_quality(&self, quality: u8) -> Result<()> {
let rc = unsafe { sys::camera::cam_wrapper_set_quality(u32::from(quality)) };
if rc != 0 {
return Err(anyhow!("sensor JPEG quality failed (esp_err 0x{rc:x})"));
}
Ok(())
}
pub fn set_gain_ceiling(&self, gain: u8) -> Result<()> {
if gain == 0 {
return Ok(());
}
if gain > 63 {
return Err(anyhow!("CAM_GAIN_CEILING must be 0 or 1-63"));
}
// OV5640 gain is a 6.4 fixed-point value. Keep AEC/AGC automatic;
// change its limit, rather than forcing the live analogue gain.
unsafe {
let sensor = sys::camera::esp_camera_sensor_get();
if sensor.is_null() || (*sensor).id.PID != 0x5640 {
return Err(anyhow!("CAM_GAIN_CEILING requires OV5640"));
}
let write = (*sensor)
.set_reg
.ok_or_else(|| anyhow!("sensor register writes unavailable"))?;
let read = (*sensor)
.get_reg
.ok_or_else(|| anyhow!("sensor register reads unavailable"))?;
let raw = i32::from(gain) * 16 - 1;
if write(sensor, 0x3a18, 0x03, raw >> 8) != 0
|| write(sensor, 0x3a19, 0xff, raw & 0xff) != 0
|| read(sensor, 0x3a18, 0x03ff) != raw
{
return Err(anyhow!("could not set OV5640 gain ceiling"));
}
info!("OV5640 automatic gain ceiling: {:.2}x", raw as f32 / 16.0);
}
Ok(())
}
/// Keep capture memory at three buffers, but never send a FIFO backlog.
pub fn latest_frame(&self) -> Result<Frame, FrameError> {
let mut fb: sys::camera::cam_wrapper_fb_t = unsafe { std::mem::zeroed() };
let rc = unsafe { sys::camera::cam_wrapper_fb_get_latest(&mut fb, 100) };
if rc != 0 || fb.data.is_null() || fb.len == 0 {
unsafe { sys::camera::cam_wrapper_fb_release(&mut fb) };
return Err(FrameError);
}
Ok(Frame { fb })
}
/// Grabs the next frame.
///
/// This blocks until the sensor has one, which is what paces the stream.
/// `Err` means the driver could not hand out a frame at all; the camera is
/// still usable afterwards, so callers should keep going rather than treat
/// it as fatal.
pub fn next_frame(&self) -> Result<Frame, FrameError> {
let mut fb: sys::camera::cam_wrapper_fb_t = unsafe { std::mem::zeroed() };
let rc = unsafe { sys::camera::cam_wrapper_fb_get(&mut fb, self.sensor.into()) };
if rc != 0 || fb.data.is_null() || fb.len == 0 {
// Nothing was handed out, but be defensive in case a future
// driver version sets a partial frame.
unsafe { sys::camera::cam_wrapper_fb_release(&mut fb) };
return Err(FrameError);
}
Ok(Frame { fb })
}
}
/// Camera connector of the Freenove ESP32-S3-WROOM CAM board.
///
/// Every pin here is one the ESP32-S3 leaves free: 26..37 are bonded to the
/// module's SPI flash and octal PSRAM, 19/20 are the native USB pair this
/// firmware logs over, and 43/44 are the console UART. Touching IO_MUX on any
/// of the flash/PSRAM pins hangs the CPU mid-transfer, which is what happens if
/// you reuse a pin map written for a different board.
///
/// The data bus is the sensor's Y2..Y9, i.e. the low byte of the 10-bit pixel
/// word, so Y2 is D0 and Y9 is D7.
mod pins {
pub const RESET: i32 = 0;
pub const XCLK: i32 = 15;
pub const PCLK: i32 = 13;
pub const VSYNC: i32 = 6;
pub const HREF: i32 = 7;
pub const SIOD: i32 = 4;
pub const SIOC: i32 = 5;
/// D0..D7 == Y2..Y9, in the order `camera_config_t` expects.
pub const DATA: [i32; 8] = [11, 9, 8, 10, 12, 18, 17, 16];
}
+102
View File
@@ -0,0 +1,102 @@
//! Nonblocking Wi-Fi recovery. Association and DHCP have separate deadlines.
#[derive(Debug, PartialEq)]
pub enum Action {
Connect,
AbortAttempt,
}
pub struct Recovery {
next_attempt_ms: u64,
attempt_deadline_ms: Option<u64>,
lease_started_ms: Option<u64>,
backoff_ms: u64,
}
impl Recovery {
pub fn new(now_ms: u64) -> Self {
Self {
next_attempt_ms: now_ms,
attempt_deadline_ms: None,
lease_started_ms: None,
backoff_ms: 1000,
}
}
fn failed(&mut self, now_ms: u64) {
self.attempt_deadline_ms = None;
self.lease_started_ms = None;
self.next_attempt_ms = now_ms.saturating_add(self.backoff_ms);
self.backoff_ms = (self.backoff_ms * 2).min(30_000);
}
pub fn disconnected(&mut self, now_ms: u64) {
if self.attempt_deadline_ms.is_some() {
self.failed(now_ms);
}
}
pub fn poll(&mut self, now_ms: u64, associated: bool) -> Option<Action> {
if associated {
let start = *self.lease_started_ms.get_or_insert(now_ms);
if now_ms.saturating_sub(start) >= 15_000 {
self.failed(now_ms);
return Some(Action::AbortAttempt);
}
return None;
}
self.lease_started_ms = None;
if self
.attempt_deadline_ms
.is_some_and(|deadline| now_ms >= deadline)
{
self.failed(now_ms);
return Some(Action::AbortAttempt);
}
if self.attempt_deadline_ms.is_none() && now_ms >= self.next_attempt_ms {
self.attempt_deadline_ms = Some(now_ms.saturating_add(15_000));
return Some(Action::Connect);
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn first_attempt_is_immediate_but_does_not_restart_active_authentication() {
let mut r = Recovery::new(100);
assert_eq!(r.poll(100, false), Some(Action::Connect));
assert_eq!(r.poll(14_999, false), None);
assert_eq!(r.poll(15_100, false), Some(Action::AbortAttempt));
assert_eq!(r.poll(16_099, false), None);
assert_eq!(r.poll(16_100, false), Some(Action::Connect));
}
#[test]
fn failure_events_back_off_with_a_thirty_second_limit() {
let mut r = Recovery::new(0);
let mut now = 0;
for delay in [1000, 2000, 4000, 8000, 16000, 30000, 30000] {
assert_eq!(r.poll(now, false), Some(Action::Connect));
r.disconnected(now + 100);
assert_eq!(r.poll(now + 99 + delay, false), None);
now += 100 + delay;
}
}
#[test]
fn dhcp_is_allowed_time_but_cannot_hang_forever() {
let mut r = Recovery::new(0);
assert_eq!(r.poll(0, false), Some(Action::Connect));
assert_eq!(r.poll(500, true), None);
assert_eq!(r.poll(15_499, true), None);
assert_eq!(r.poll(15_500, true), Some(Action::AbortAttempt));
// The expected disconnect after abort must not double the backoff.
r.disconnected(15_501);
assert_eq!(r.poll(16_500, false), Some(Action::Connect));
}
#[test]
fn a_new_outage_resets_backoff_and_deadlines() {
let mut r = Recovery::new(0);
r.poll(0, false);
r.disconnected(100);
let mut next = Recovery::new(200);
assert_eq!(next.poll(200, false), Some(Action::Connect));
}
}
+652
View File
@@ -0,0 +1,652 @@
//! Camera firmware for an ESP32-S3-N16R8 with an OV2640 / OV3660 / OV5640 sensor,
//! written in Rust on top of ESP-IDF.
//!
//! Captures JPEG frames and streams them as UDP datagrams to any number of
//! clients. The wire format lives in the `protocol` crate, shared with the desktop
//! client in `client/`; see that crate's documentation for why the transport is
//! UDP rather than TCP.
//!
//! WiFi credentials come from the `WIFI_SSID` / `WIFI_PASS` environment variables
//! at build time, so no secrets end up in the source.
use std::sync::{
atomic::{AtomicU32, Ordering},
Arc,
};
use std::time::{Duration, Instant};
use anyhow::{anyhow, Result};
use embedded_svc::wifi::{
AccessPointInfo, AuthMethod, ClientConfiguration, Configuration, PmfConfiguration,
};
use esp_idf_svc::hal::peripherals::Peripherals;
use esp_idf_svc::wifi::{BlockingWifi, EspWifi, WifiEvent};
use esp_idf_svc::{eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition};
use log::{error, info, warn};
mod camera;
mod link_control;
mod stream_control;
mod udp;
use camera::{Camera, Resolution, SensorHint};
use stream_control::{Pacer, QualityController, ThermalLimiter};
use udp::UdpSender;
const SSID: &str = env!("WIFI_SSID");
const PASSWORD: &str = env!("WIFI_PASS");
/// Authentication mode to assume, overriding whatever the scan reports.
///
/// Usually left unset: the scan tells us the AP's real mode. Pin it with
/// `WIFI_AUTH=wpa2` (or `wpa3`, `wpa2wpa3`, `wpa`, `none`) when the scan cannot
/// see the AP - a hidden SSID, for instance.
fn auth_override() -> Option<AuthMethod> {
let name = option_env!("WIFI_AUTH")?;
Some(match name {
"none" => AuthMethod::None,
"wep" => AuthMethod::WEP,
"wpa" => AuthMethod::WPA,
"wpa2" => AuthMethod::WPA2Personal,
"wpawpa2" => AuthMethod::WPAWPA2Personal,
"wpa3" => AuthMethod::WPA3Personal,
"wpa2wpa3" => AuthMethod::WPA2WPA3Personal,
"wapi" => AuthMethod::WAPIPersonal,
other => panic!(
"WIFI_AUTH={other} is not one of: \
none, wep, wpa, wpa2, wpawpa2, wpa3, wpa2wpa3, wapi"
),
})
}
/// Capture resolution, overridable at build time: `CAM_RES=vga ./scripts/build.sh`
const RESOLUTION: &str = match option_env!("CAM_RES") {
Some(value) => value,
None => "vga",
};
/// JPEG quality 0-63 (lower is better), overridable: `CAM_QUALITY=15 ./scripts/build.sh`
const QUALITY: &str = match option_env!("CAM_QUALITY") {
Some(value) => value,
None => "12",
};
/// Whether the driver inverts VSYNC, overridable so polarity can be A/B tested
/// without editing code: `CAM_VSYNC_INVERT=0 ./scripts/build.sh`.
///
/// A function rather than a `const` because matching on a `str` is not yet
/// allowed in const context on this toolchain.
///
/// `true` is esp32-camera's default and correct for most DVP sensors. `false` is
/// what a board that inverts VSYNC in hardware needs.
fn vsync_invert() -> bool {
!matches!(option_env!("CAM_VSYNC_INVERT"), Some("0"))
}
fn main() -> Result<()> {
// Must run once so the esp-idf-sys runtime patches link correctly.
esp_idf_svc::sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();
let resolution: Resolution = RESOLUTION.parse()?;
let quality: u8 = QUALITY.parse()?;
let quality_ceiling: u8 = option_env!("CAM_QUALITY_MAX").unwrap_or("0").parse()?;
if quality_ceiling != 0 && !(quality..=63).contains(&quality_ceiling) {
return Err(anyhow!(
"CAM_QUALITY_MAX must be 0 (fixed quality) or CAM_QUALITY..63"
));
}
let max_fps: u32 = option_env!("CAM_MAX_FPS").unwrap_or("0").parse()?;
if max_fps > 60 {
return Err(anyhow!("CAM_MAX_FPS must be 0 (uncapped) or 1-60"));
}
let grayscale = match option_env!("CAM_GRAYSCALE").unwrap_or("0") {
"0" => false,
"1" => true,
_ => return Err(anyhow!("CAM_GRAYSCALE must be 0 or 1")),
};
let thermal_limit: f32 = option_env!("CAM_THERMAL_LIMIT_C").unwrap_or("75").parse()?;
if !thermal_limit.is_finite()
|| (thermal_limit != 0.0 && !(40.0..=95.0).contains(&thermal_limit))
{
return Err(anyhow!("CAM_THERMAL_LIMIT_C must be 0 (disabled) or 40-95"));
}
info!("reset reason: {}", unsafe {
esp_idf_svc::sys::esp_reset_reason()
});
info!("vsync invert: {}", vsync_invert());
let camera = Camera::init(SensorHint::Auto, resolution, quality, vsync_invert())?;
camera.set_clock_profile(option_env!("CAM_CLOCK_PROFILE").unwrap_or("default"))?;
camera.set_grayscale(grayscale)?;
let exposure_ev: i32 = option_env!("CAM_EXPOSURE_EV").unwrap_or("0").parse()?;
esp_idf_svc::sys::esp!(unsafe {
esp_idf_svc::sys::camera::cam_wrapper_set_exposure_bias(exposure_ev)
})?;
if let Some(level) = option_env!("CAM_AE_LEVEL") {
if exposure_ev != 0 {
return Err(anyhow!(
"Use CAM_AE_LEVEL or a nonzero CAM_EXPOSURE_EV, not both"
));
}
camera.set_ae_level(level.parse()?)?;
}
let horizontal_total: u32 = option_env!("CAM_HTS").unwrap_or("0").parse()?;
esp_idf_svc::sys::esp!(unsafe {
esp_idf_svc::sys::camera::cam_wrapper_set_horizontal_timing(horizontal_total)
})?;
let shutter_lines: u32 = option_env!("CAM_SHUTTER_LINES").unwrap_or("0").parse()?;
let exposure_limit: u32 = option_env!("CAM_EXPOSURE_MAX_LINES")
.unwrap_or("0")
.parse()?;
if shutter_lines != 0 && exposure_limit != 0 {
return Err(anyhow!(
"Use CAM_SHUTTER_LINES or CAM_EXPOSURE_MAX_LINES, not both"
));
}
esp_idf_svc::sys::esp!(unsafe {
esp_idf_svc::sys::camera::cam_wrapper_set_shutter_lines(shutter_lines)
})?;
esp_idf_svc::sys::esp!(unsafe {
esp_idf_svc::sys::camera::cam_wrapper_set_exposure_limit(exposure_limit)
})?;
camera.set_gain_ceiling(option_env!("CAM_GAIN_CEILING").unwrap_or("0").parse()?)?;
info!("stream profile: target {max_fps} fps, grayscale {grayscale}, quality {quality}, thermal limit {thermal_limit:.1} C");
info!(
"configured capture probe: {:.2} fps",
camera.probe_frame_rate()
);
let temperature_available = unsafe { esp_idf_svc::sys::camera::cam_wrapper_temperature_init() };
if temperature_available != 0 {
warn!("chip temperature unavailable (esp_err 0x{temperature_available:x}); fixed frame cap remains active");
}
let peripherals = Peripherals::take()?;
let sys_loop = EspSystemEventLoop::take()?;
let nvs = EspDefaultNvsPartition::take()?;
let network_epoch = Arc::new(AtomicU32::new(0));
let event_epoch = network_epoch.clone();
let _wifi_events = sys_loop.subscribe::<WifiEvent, _>(move |event| {
if matches!(event, WifiEvent::StaDisconnected(_)) {
event_epoch.fetch_add(1, Ordering::Relaxed);
}
})?;
let mut wifi = BlockingWifi::wrap(
EspWifi::new(peripherals.modem, sys_loop.clone(), Some(nvs))?,
sys_loop,
)?;
// `connect` starts the driver, then scans and associates, so that an SSID on
// a 5 GHz network (which this radio cannot see at all) is reported as such
// instead of a bare association timeout.
if let Err(err) = connect(&mut wifi) {
error!("could not join \"{SSID}\": {err}");
error!("check that the SSID is a 2.4 GHz network and the password is right");
return Err(err);
}
let ip = wifi.wifi().sta_netif().get_ip_info()?.ip;
configure_radio()?;
// The board identifies itself by an id derived from its MAC, not its IP: DHCP
// hands out a different address across reboots, and a client that saved its
// binding to one would have to be reconfigured every time.
let sender_id = udp::local_sender_id();
let mut sender = UdpSender::new(sender_id)?;
info!(
"RMIC::2026 firmware {} (build {}) on {ip}, wire protocol {}",
udp::FIRMWARE_VERSION,
env!("RMIC_BUILD"),
protocol::PROTOCOL_VERSION
);
info!("clients will find it by broadcast; no configuration needed on this side");
let mut last_report = Instant::now();
let mut last_stats = sender.stats();
let mut pacer = Pacer::new(max_fps);
let mut thermal = ThermalLimiter::new(max_fps, thermal_limit);
let mut current_quality = quality;
let mut quality_control = QualityController::new(
quality,
if quality_ceiling == 0 {
quality
} else {
quality_ceiling
},
);
let loop_start = Instant::now();
let mut recovery = link_control::Recovery::new(0);
let mut seen_epoch = network_epoch.load(Ordering::Relaxed);
let mut link_up = true;
let mut active_ip = ip;
let mut next_link_check = 0u64;
let mut outage_started = 0u64;
let test_drop_ms = option_env!("WIFI_TEST_DROP_AFTER_S")
.unwrap_or("0")
.parse::<u64>()?
.saturating_mul(1000);
let mut test_injected = false;
let mut test_hold_until = 0u64;
loop {
let now_ms = loop_start.elapsed().as_millis() as u64;
if test_drop_ms > 0 && !test_injected && now_ms >= test_drop_ms {
test_injected = true;
test_hold_until = now_ms + 5000;
warn!("Wi-Fi recovery self-test: intentional disconnect for 5000 ms");
wifi.wifi_mut().disconnect().ok();
next_link_check = 0;
}
if now_ms >= next_link_check {
next_link_check = now_ms + 100;
let epoch = network_epoch.load(Ordering::Relaxed);
let associated = wifi.wifi().is_connected().unwrap_or(false);
let lease = wifi
.wifi()
.sta_netif()
.get_ip_info()
.ok()
.map(|v| v.ip)
.filter(|ip| !ip.is_unspecified());
let ready = associated
&& wifi.wifi().is_up().unwrap_or(false)
&& lease.is_some()
&& now_ms >= test_hold_until;
let address_changed = ready && lease.is_some_and(|address| address != active_ip);
if link_up && (!ready || epoch != seen_epoch || address_changed) {
link_up = false;
outage_started = now_ms;
recovery = link_control::Recovery::new(now_ms);
sender.network_lost();
warn!("Wi-Fi link unavailable or changed; UDP suspended, waiting for association and DHCP");
} else if epoch != seen_epoch {
recovery.disconnected(now_ms);
}
seen_epoch = epoch;
if !link_up && ready {
match sender.restore_network() {
Ok(()) => {
active_ip = lease.unwrap();
if let Err(err) = configure_radio() {
warn!("radio profile restore: {err}");
}
// Discard any queued image from before recovery.
for _ in 0..2 {
if let Ok(frame) = camera.next_frame() {
drop(frame);
}
}
link_up = true;
last_report = Instant::now();
last_stats = sender.stats();
info!("Wi-Fi recovery complete after {} ms; IP {active_ip}; UDP recreated, camera state preserved",now_ms-outage_started);
}
Err(err) => warn!("UDP restore failed, will retry: {err}"),
}
} else if !link_up && now_ms >= test_hold_until {
match recovery.poll(now_ms, associated) {
Some(link_control::Action::Connect) => {
info!("Wi-Fi reconnect attempt");
if let Err(err) = wifi.wifi_mut().connect() {
warn!("Wi-Fi reconnect rejected: {err}");
recovery.disconnected(now_ms);
}
}
Some(link_control::Action::AbortAttempt) => {
warn!("Wi-Fi association/DHCP deadline elapsed; cancelling attempt before backoff");
wifi.wifi_mut().disconnect().ok();
}
None => {}
}
}
}
if !link_up {
std::thread::sleep(Duration::from_millis(50));
continue;
}
sender.housekeeping();
let now_us = unsafe { esp_idf_svc::sys::esp_timer_get_time() } as u64;
let wait_us = pacer.delay_us(now_us);
if wait_us > 0 {
std::thread::sleep(Duration::from_micros(wait_us));
}
pacer.started(unsafe { esp_idf_svc::sys::esp_timer_get_time() } as u64);
udp::pump(&camera, &mut sender)?;
let now = Instant::now();
if now.duration_since(last_report) >= Duration::from_secs(5) {
// Deltas rather than totals: the totals alone cannot distinguish "the
// sensor is slow" from "the sender is not keeping up", which is the
// distinction that matters when the frame rate is wrong.
let s = sender.stats();
let elapsed = now.duration_since(last_report).as_secs_f32();
let attempted = s.frames_attempted - last_stats.frames_attempted;
let dropped = s.peer_frames_dropped - last_stats.peer_frames_dropped;
let send_ms = (s.send_time_us - last_stats.send_time_us) as f32
/ attempted.max(1) as f32
/ 1000.0;
if let Some(next) = quality_control.observe(
u64::from(attempted) * sender.peer_count() as u64,
dropped,
send_ms,
) {
match camera.set_quality(next) {
Ok(()) => {
info!("JPEG load control: quality {current_quality} -> {next}, {dropped} peer drops / {attempted} attempts, {send_ms:.2} ms/send");
current_quality = next;
}
Err(err) => {
warn!("JPEG load control disabled after sensor error: {err}");
quality_control = QualityController::new(current_quality, current_quality);
}
}
}
let mut ap: esp_idf_svc::sys::wifi_ap_record_t = unsafe { std::mem::zeroed() };
let ap_ok = unsafe { esp_idf_svc::sys::esp_wifi_sta_get_ap_info(&mut ap) } == 0;
let mut tx_power = 0;
let tx_power_ok = unsafe {
esp_idf_svc::sys::esp_wifi_get_max_tx_power(&mut tx_power)
} == 0;
let mut celsius = f32::NAN;
let temp_ok = temperature_available == 0
&& unsafe { esp_idf_svc::sys::camera::cam_wrapper_temperature_read(&mut celsius) }
== 0;
let previous_fps = thermal.fps();
let target_fps = thermal.observe(if temp_ok { Some(celsius) } else { None });
if previous_fps != target_fps {
pacer.set_fps(
target_fps,
unsafe { esp_idf_svc::sys::esp_timer_get_time() } as u64,
);
info!("thermal load limit changed: {previous_fps} -> {target_fps} fps at {celsius:.1} C");
}
info!(
"{:.1} fps submitted, {:.1} fps attempted, {:.1} datagrams/s, {:.1} kB/s, \
{} send failures, {} retries, errno {}, {} peer frames dropped, \
{:.2} ms/send, {} clients subscribed, {} frames skipped (no client), \
internal heap {} free / {} largest bytes, RSSI {} dBm channel {}, \
chip temp {:.1} C, target {} fps, thermal_limited {}, capture errors {}, exposure {} lines, jpeg quality {}, gain {:.2}x, \
capture queue skipped {}, stale frames dropped {}, deadline peer drops {}, max submitted capture age {:.2} ms, max send hold {:.2} ms, max TX power {:.2} dBm",
(s.frames_sent - last_stats.frames_sent) as f32 / elapsed,
(s.frames_attempted - last_stats.frames_attempted) as f32 / elapsed,
(s.fragments_sent - last_stats.fragments_sent) as f32 / elapsed,
(s.bytes_sent - last_stats.bytes_sent) as f32 / elapsed / 1024.0,
s.errors - last_stats.errors,
s.retries - last_stats.retries,
s.last_errno,
s.peer_frames_dropped - last_stats.peer_frames_dropped,
(s.send_time_us - last_stats.send_time_us) as f32
/ (s.frames_attempted - last_stats.frames_attempted).max(1) as f32
/ 1000.0,
sender.peer_count(),
s.skipped_no_peer - last_stats.skipped_no_peer,
unsafe {
esp_idf_svc::sys::heap_caps_get_free_size(esp_idf_svc::sys::MALLOC_CAP_INTERNAL)
},
unsafe {
esp_idf_svc::sys::heap_caps_get_largest_free_block(
esp_idf_svc::sys::MALLOC_CAP_INTERNAL,
)
},
if ap_ok { ap.rssi } else { 0 },
if ap_ok { ap.primary } else { 0 },
if temp_ok { celsius } else { f32::NAN },
target_fps,
thermal.limited(),
s.capture_errors - last_stats.capture_errors,
unsafe { esp_idf_svc::sys::camera::cam_wrapper_exposure_lines() },
current_quality,
unsafe { esp_idf_svc::sys::camera::cam_wrapper_gain_raw() } as f32 / 16.0,
s.capture_queue_skipped - last_stats.capture_queue_skipped,
s.stale_frames_dropped - last_stats.stale_frames_dropped,
s.deadline_peer_drops - last_stats.deadline_peer_drops,
s.max_submitted_age_us as f32 / 1000.0,
s.max_send_hold_us as f32 / 1000.0,
if tx_power_ok { tx_power as f32 / 4.0 } else { f32::NAN },
);
last_stats = s;
last_report = now;
}
}
}
fn configure_radio() -> Result<()> {
// Modem power save trades latency for current, and for a video stream that
// trade is backwards: with PS on, an idle beacon interval costs hundreds of
// milliseconds, which shows up as stutter and a ping of 200-300 ms on an
// otherwise good link. The board is mains-powered anyway.
//
// This one binds as a bare `esp_err_t` rather than a `Result`.
let ps_rc =
unsafe { esp_idf_svc::sys::esp_wifi_set_ps(esp_idf_svc::sys::wifi_ps_type_t_WIFI_PS_NONE) };
if ps_rc == 0 {
info!("WiFi power save disabled");
} else {
warn!("could not disable WiFi power save (esp_err 0x{ps_rc:x})");
}
// Apply the selected power cap both at startup and after reconnecting.
// Congestion/thermal controllers must not lower it: they adjust JPEG
// quality or frame delivery instead. The SDK quantizes this setting.
if let Some(value) = option_env!("WIFI_TX_POWER_DBM") {
let dbm: u8 = value
.parse()
.map_err(|_| anyhow!("WIFI_TX_POWER_DBM must be an integer from 2 to 20"))?;
if !(2..=20).contains(&dbm) {
return Err(anyhow!("WIFI_TX_POWER_DBM must be an integer from 2 to 20"));
}
esp_idf_svc::sys::esp!(unsafe {
esp_idf_svc::sys::esp_wifi_set_max_tx_power((dbm * 4) as i8)
})?;
info!("WiFi requested max TX power: {dbm} dBm");
}
let mut ap: esp_idf_svc::sys::wifi_ap_record_t = unsafe { std::mem::zeroed() };
let mut phy = 0;
let mut tx_power = 0;
unsafe {
esp_idf_svc::sys::esp_wifi_sta_get_ap_info(&mut ap);
esp_idf_svc::sys::esp_wifi_sta_get_negotiated_phymode(&mut phy);
}
esp_idf_svc::sys::esp!(unsafe {
esp_idf_svc::sys::esp_wifi_get_max_tx_power(&mut tx_power)
})?;
info!(
"WiFi link: negotiated PHY {phy}, pairwise cipher {}, max TX power {:.2} dBm",
ap.pairwise_cipher,
tx_power as f32 / 4.0
);
Ok(())
}
/// Joins `SSID`, scanning first and retrying the plausible authentication modes.
///
/// The scan is not decoration. This radio is 2.4 GHz only, so a 5 GHz SSID never
/// shows up at all and a bare "association timed out" tells you nothing.
///
/// The retries are because the AP's advertised mode is not the same thing as the
/// mode it will accept. Routers commonly run WPA/WPA2 mixed, WPA2/WPA3 mixed, or
/// WPA3 with PMF mandatory, and a client pinned to the wrong one fails the 4-way
/// handshake *after* it has already associated - the failure looks identical to
/// a wrong password, so it is worth walking the list instead of guessing.
fn connect(wifi: &mut BlockingWifi<EspWifi<'static>>) -> Result<()> {
let pinned_auth = auth_override();
// A configuration has to exist before start(), which is what decides
// station vs AP mode; the real one is picked after the scan.
wifi.set_configuration(&client_config(
pinned_auth.unwrap_or_else(default_auth_method),
)?)?;
wifi.start()?;
info!("wifi started");
info!("scanning for 2.4 GHz networks...");
let advertised = match wifi.wifi_mut().scan() {
Ok(aps) => {
report_scan(&aps);
advertised_auth(&aps)
}
Err(err) => {
warn!("scan failed: {err}");
None
}
};
for auth in candidates(advertised, pinned_auth) {
info!("trying {auth:?}...");
wifi.set_configuration(&client_config(auth)?)?;
// Driver-level (non-blocking) connect: `BlockingWifi::connect()` waits
// out the driver's own ~20 s association timeout, which would make
// walking the list of modes take minutes.
wifi.wifi_mut().connect()?;
// Bounded, so one bad mode costs a few seconds instead of the timeout.
let associated = wifi
.wifi_wait_while(
|| wifi.wifi().is_connected().map(|c| !c),
Some(Duration::from_secs(5)),
)
.is_ok();
let got_ip = associated
&& wifi
.ip_wait_while(
|| wifi.wifi().is_up().map(|up| !up),
Some(Duration::from_secs(4)),
)
.is_ok();
if got_ip {
let ip_info = wifi.wifi().sta_netif().get_ip_info()?;
if !ip_info.ip.is_unspecified() {
info!("wifi connected with {auth:?}: {ip_info:?}");
return Ok(());
}
}
warn!(
"{auth:?} did not complete the handshake ({})",
if associated {
"associated but no DHCP address"
} else {
"no association"
}
);
wifi.wifi_mut().disconnect().ok();
}
Err(anyhow!(
"no authentication mode joined \"{SSID}\" (tried {:?}); \
association succeeded but the 4-way handshake did not, \
which almost always means the password is wrong",
candidates(advertised, pinned_auth)
))
}
/// Authentication modes to try, most likely first.
///
/// A mode pinned with `WIFI_AUTH` is the only one tried - if it does not work,
/// trying others would just be guessing at a value the builder already stated.
fn candidates(advertised: Option<AuthMethod>, pinned: Option<AuthMethod>) -> Vec<AuthMethod> {
if let Some(auth) = pinned {
return vec![auth];
}
// "WPA" proper is a pre-2004 mode no current router speaks, and "None" is
// only meaningful without a password, so neither is worth a 10 s attempt.
let mut modes = vec![advertised.unwrap_or_else(default_auth_method)];
modes.extend([
AuthMethod::WPA2Personal,
AuthMethod::WPA2WPA3Personal,
AuthMethod::WPA3Personal,
]);
if PASSWORD.is_empty() {
modes.push(AuthMethod::None);
}
let mut seen = Vec::new();
modes.retain(|m| {
let fresh = !seen.contains(m);
seen.push(*m);
fresh
});
modes
}
fn client_config(auth_method: AuthMethod) -> Result<Configuration> {
Ok(Configuration::Client(ClientConfiguration {
ssid: SSID
.try_into()
.map_err(|_| anyhow!("WIFI_SSID too long (max 32 bytes)"))?,
bssid: None,
auth_method,
password: PASSWORD
.try_into()
.map_err(|_| anyhow!("WIFI_PASS too long (max 64 bytes)"))?,
channel: None,
// Advertising PMF keeps routers that offer WPA2+WPA3 mixed mode from
// dropping the handshake, while `required: false` still lets a WPA2-only
// AP through. Leaving this at NotCapable fails on the former.
pmf_cfg: PmfConfiguration::Capable { required: false },
..Default::default()
}))
}
fn default_auth_method() -> AuthMethod {
if PASSWORD.is_empty() {
AuthMethod::None
} else {
AuthMethod::WPA2Personal
}
}
/// What `SSID` advertises, if the scan saw it.
///
/// `None` means the scan did not find this network at all, which on its own is
/// the most useful diagnostic there is: on a 2.4 GHz-only radio it almost always
/// means the SSID belongs to a 5 GHz network, or the AP is on a channel this
/// region's regulatory domain forbids.
fn advertised_auth(aps: &[AccessPointInfo]) -> Option<AuthMethod> {
aps.iter()
.find(|ap| ap.ssid.as_str() == SSID)
.and_then(|ap| ap.auth_method)
}
/// Logs the visible networks, marking the one we are trying to join.
fn report_scan(aps: &[AccessPointInfo]) {
if aps.is_empty() {
warn!("scan found no networks at all");
return;
}
for ap in aps {
info!(
" {:<32} ch {:>2} {:>4} dBm {:?}{}",
ap.ssid,
ap.channel,
ap.signal_strength,
ap.auth_method,
if ap.ssid.as_str() == SSID {
" <- target"
} else {
""
}
);
}
if advertised_auth(aps).is_none() {
error!("\"{SSID}\" was not in the scan results");
error!("this radio is 2.4 GHz only, so the AP is likely 5 GHz or on a forbidden channel");
}
}
+287
View File
@@ -0,0 +1,287 @@
//! Frame pacing and a hysteretic, configurable thermal load limit.
//! Times are monotonic microseconds so the scheduling policy is host-testable.
pub struct Pacer {
period_us: u64,
next_due_us: u64,
}
impl Pacer {
pub fn new(fps: u32) -> Self {
Self::with_gap_us(period(fps))
}
pub fn with_gap_us(period_us: u64) -> Self {
Self {
period_us,
next_due_us: 0,
}
}
pub fn delay_us(&self, now_us: u64) -> u64 {
self.next_due_us.saturating_sub(now_us)
}
pub fn started(&mut self, now_us: u64) {
// An overrun starts a new period; never burst to repay missed slots.
self.next_due_us = now_us.saturating_add(self.period_us);
}
pub fn set_fps(&mut self, fps: u32, now_us: u64) {
self.period_us = period(fps);
self.started(now_us);
}
}
pub const MAX_SEND_HOLD_US: u64 = 25_000;
pub const MAX_CAPTURE_AGE_US: u64 = 100_000;
pub const DEFAULT_RETRY_BUDGET_US: u64 = 8_000;
/// One current frame, no pending-send FIFO. A short retry budget is shared by
/// all fragments/peers; success never renews the frame's absolute deadline.
pub struct FrameBudget {
deadline_us: u64,
retry_left_us: u64,
}
impl FrameBudget {
pub fn new(now_us: u64, captured_us: u64, retry_us: u64) -> Self {
Self {
deadline_us: now_us
.saturating_add(MAX_SEND_HOLD_US)
.min(captured_us.min(now_us).saturating_add(MAX_CAPTURE_AGE_US)),
retry_left_us: retry_us.min(MAX_SEND_HOLD_US),
}
}
pub fn expired(&self, now_us: u64) -> bool {
now_us >= self.deadline_us
}
pub fn allow_retry(&mut self, now_us: u64, wait_us: u64) -> bool {
if wait_us == 0
|| wait_us > self.retry_left_us
|| now_us.saturating_add(wait_us) >= self.deadline_us
{
return false;
}
self.retry_left_us -= wait_us;
true
}
}
fn period(fps: u32) -> u64 {
if fps == 0 {
0
} else {
1_000_000u64.div_ceil(fps as u64)
}
}
/// Adjust JPEG compression from five-second submission windows. The sensor's
/// quality number increases with compression; restore detail more slowly than
/// we shed load, and do not interpret absent subscribers as a healthy link.
pub struct QualityController {
base: u8,
ceiling: u8,
current: u8,
healthy_windows: u8,
}
impl QualityController {
pub fn new(base: u8, ceiling: u8) -> Self {
Self {
base,
ceiling,
current: base,
healthy_windows: 0,
}
}
pub fn observe(&mut self, peer_frames: u64, dropped: u32, send_ms: f32) -> Option<u8> {
if peer_frames < 5 || !send_ms.is_finite() {
self.healthy_windows = 0;
return None;
}
let previous = self.current;
if u64::from(dropped) * 100 >= peer_frames.saturating_mul(2) || send_ms >= 8.0 {
self.current = self.current.saturating_add(3).min(self.ceiling);
self.healthy_windows = 0;
} else if dropped == 0 && send_ms < 4.0 {
self.healthy_windows += 1;
if self.healthy_windows >= 3 {
self.current = self.current.saturating_sub(1).max(self.base);
self.healthy_windows = 0;
}
} else {
self.healthy_windows = 0;
}
(self.current != previous).then_some(self.current)
}
}
pub struct ThermalLimiter {
normal_fps: u32,
limit_c: f32,
limited: bool,
}
impl ThermalLimiter {
pub fn new(normal_fps: u32, limit_c: f32) -> Self {
Self {
normal_fps,
limit_c,
limited: false,
}
}
pub fn observe(&mut self, temperature: Option<f32>) -> u32 {
if self.limit_c > 0.0 {
if let Some(t) = temperature.filter(|t| t.is_finite()) {
if t >= self.limit_c {
self.limited = true;
} else if t <= self.limit_c - 5.0 {
self.limited = false;
}
}
}
self.fps()
}
pub fn fps(&self) -> u32 {
if !self.limited {
self.normal_fps
} else if self.normal_fps == 0 {
8
} else {
self.normal_fps.div_ceil(2)
}
}
pub fn limited(&self) -> bool {
self.limited
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pacing_caps_rate_without_catching_up_after_an_overrun() {
let mut pacer = Pacer::new(15);
assert_eq!(pacer.delay_us(100), 0);
pacer.started(100);
assert_eq!(pacer.delay_us(101), 66_666);
assert_eq!(pacer.delay_us(300_000), 0);
pacer.started(300_000);
assert_eq!(pacer.delay_us(300_001), 66_666);
}
#[test]
fn unlimited_and_runtime_rate_changes_do_not_burst() {
let mut pacer = Pacer::new(0);
pacer.started(1_000);
assert_eq!(pacer.delay_us(1_001), 0);
pacer.set_fps(10, 1_001);
assert_eq!(pacer.delay_us(1_002), 99_999);
}
#[test]
fn thermal_limit_has_five_degree_hysteresis() {
let mut limit = ThermalLimiter::new(15, 75.0);
assert_eq!(limit.observe(Some(74.9)), 15);
assert_eq!(limit.observe(Some(75.0)), 8);
assert_eq!(limit.observe(Some(72.0)), 8);
assert_eq!(limit.observe(None), 8);
assert_eq!(limit.observe(Some(f32::NAN)), 8);
assert_eq!(limit.observe(Some(70.0)), 15);
}
#[test]
fn thermal_guard_can_be_disabled_or_used_with_uncapped_streams() {
assert_eq!(ThermalLimiter::new(15, 0.0).observe(Some(90.0)), 15);
assert_eq!(ThermalLimiter::new(0, 75.0).observe(Some(80.0)), 8);
assert_eq!(ThermalLimiter::new(1, 75.0).observe(Some(80.0)), 1);
}
#[test]
fn packet_spacing_starts_from_actual_submission_without_catch_up() {
let mut pacer = Pacer::with_gap_us(1000);
pacer.started(100);
assert_eq!(pacer.delay_us(900), 200);
assert_eq!(pacer.delay_us(5000), 0);
pacer.started(5000);
assert_eq!(pacer.delay_us(5001), 999);
}
#[test]
fn frame_deadline_is_not_renewed_by_successful_fragments_or_other_peers() {
let frame = FrameBudget::new(50_000, 20_000, 8_000);
assert!(!frame.expired(74_999));
assert!(frame.expired(75_000));
}
#[test]
fn an_old_capture_cannot_gain_a_fresh_send_lifetime() {
let frame = FrameBudget::new(99_000, 0, 8_000);
assert!(!frame.expired(99_999));
assert!(frame.expired(100_000));
assert!(FrameBudget::new(100_001, 0, 8_000).expired(100_001));
}
#[test]
fn retry_allowance_is_shared_by_all_fragments_and_stops_before_deadline() {
let mut frame = FrameBudget::new(50_000, 20_000, 2_000);
assert!(frame.allow_retry(51_000, 1000));
assert!(frame.allow_retry(52_000, 1000));
assert!(!frame.allow_retry(53_000, 1000));
let mut late = FrameBudget::new(50_000, 20_000, 8_000);
assert!(!late.allow_retry(74_000, 1000));
}
#[test]
fn zero_retry_mode_still_allows_immediate_sends_and_abandons_backpressure() {
let mut frame = FrameBudget::new(50_000, 20_000, 0);
assert!(!frame.expired(50_001));
assert!(!frame.allow_retry(50_001, 1000));
}
#[test]
fn compression_sheds_congestion_load_quickly_and_stays_in_bounds() {
let mut control = QualityController::new(18, 24);
assert_eq!(control.observe(100, 2, 3.0), Some(21));
assert_eq!(control.observe(100, 0, 8.0), Some(24));
assert_eq!(control.observe(100, 20, 20.0), None);
let mut disabled = QualityController::new(18, 18);
assert_eq!(disabled.observe(100, 50, 20.0), None);
}
#[test]
fn compression_recovers_only_after_three_consecutive_healthy_windows() {
let mut control = QualityController::new(18, 24);
control.observe(100, 10, 10.0);
assert_eq!(control.observe(100, 0, 2.0), None);
assert_eq!(control.observe(100, 0, 2.0), None);
assert_eq!(control.observe(100, 1, 5.0), None);
assert_eq!(control.observe(100, 0, 2.0), None);
assert_eq!(control.observe(100, 0, 2.0), None);
assert_eq!(control.observe(100, 0, 2.0), Some(20));
for _ in 0..20 {
control.observe(100, 0, 2.0);
}
assert_eq!(control.current, 18);
}
#[test]
fn missing_traffic_or_invalid_timing_does_not_restore_quality() {
let mut control = QualityController::new(18, 24);
control.observe(100, 10, 10.0);
control.observe(100, 0, 2.0);
control.observe(100, 0, 2.0);
assert_eq!(control.observe(0, 0, 0.0), None);
assert_eq!(control.observe(100, 0, f32::NAN), None);
assert_eq!(control.observe(100, 0, 2.0), None);
assert_eq!(control.current, 21);
}
}
+678
View File
@@ -0,0 +1,678 @@
//! UDP 视频发送器 + 设备发现。
//!
//! # 为什么不是 HTTP
//!
//! 这里替换掉过一个 MJPEG-over-HTTP 的实现,它在板子上的实测表现很差:传感器产出
//! 22 fps,而 TCP 流只送出 1.9 fps,且只占用了链路能力的 1%——链路是空的,帧却堵在
//! TCP 的队头阻塞后面等一个实时画面等不起的重传计时器。
//!
//! 每一帧改为独立的一串数据报。没有重传也没有确认:缺了分片的帧整个丢弃,下一帧
//! 顶上。这让实测帧率跟上了传感器,也把本来就有的带宽真正用了起来。
//!
//! # 发现与推流
//!
//! 双向,两个方向各司其职:
//!
//! - **板子**每 [`HELLO_INTERVAL`] 广播一次 [`MsgType::Hello`],让客户端知道有谁
//! 在线、id 是多少。这是单向的,客户端什么都不用做。
//! - **客户端**广播 [`MsgType::Subscribe`],板子收到后记住这个客户端的地址并开始
//! 定向推流。
//!
//! 两个方向都需要对方先开口,这正是必须双向的原因:只有板子广播的话,板子无从知道
//! "有谁要看我"。
//!
//! 板子用 MAC 派生的 [`sender_id`](见 [`protocol::id_from_mac`])而不是 IP 标识
//! 自己,因为客户端要能把绑定存进配置文件并在重启后继续有效——DHCP 换 IP 是常态,
//! MAC 不会变。
use std::net::{Ipv4Addr, SocketAddrV4};
use anyhow::{anyhow, Result};
use esp_idf_svc::sys as ffi;
use log::{debug, info, warn};
use crate::camera::{Camera, Frame};
use crate::stream_control::{
FrameBudget, Pacer, DEFAULT_RETRY_BUDGET_US, MAX_CAPTURE_AGE_US, MAX_SEND_HOLD_US,
};
/// 单个数据报的最大字节数。
///
/// 与 `protocol::MAX_FRAGMENT_PAYLOAD + protocol::HEADER_LEN` 一致。放在这里而不是
/// 从协议 crate 取,是因为这个值决定了发送缓冲的大小,必须在分配时就确定。
const SEND_BUFFER_SIZE: usize = protocol::HEADER_LEN + protocol::MAX_FRAGMENT_PAYLOAD;
/// 板子广播自身存在的间隔。
///
/// 比客户端的订阅间隔宽松:客户端的存在由板子的推流直接体现,而"有谁在线"这条
/// 信息需要快速到达新上线的客户端。
/// 编译进去的固件版本,形如 `"0.2.0"`。
///
/// 由 `build.rs` 从仓库根的 `version.json` 注入——那个文件由 `scripts/version.mjs`
/// 从 git 历史算出来。手工写死一个常量的话,第一次忘记同步就会让界面上的版本和
/// 实际烧进去的固件分叉,而且不会有任何东西报错。
pub const FIRMWARE_VERSION: &str = env!("RMIC_VERSION");
/// 打包的语义化版本:`major << 16 | minor << 8 | patch`。
fn firmware_version_packed() -> u32 {
// build.rs 保证这是十进制数字;解析不出来只可能是有人手改了它的输出。
env!("RMIC_VERSION_PACKED").parse().unwrap_or(0)
}
/// 自增构建号(git 提交数)。
fn firmware_build() -> u16 {
env!("RMIC_BUILD").parse().unwrap_or(0)
}
const HELLO_INTERVAL_MS: i64 = 2_000;
/// 同时向多少个客户端推流。
///
/// 每个对端每帧要一次 `sendto`,所以这是帧 CPU 成本的倍数。三台客户端(两队 + 裁判)
/// 够用,且不让一个陈旧的地址条目拖慢每一帧。
const MAX_PEERS: usize = 4;
// A frame must not spend a capture period waiting for network buffers. Retry
// transient driver backpressure briefly, then abandon that peer's old frame.
const TX_RETRY_WAIT_US: i64 = 1_000_000 / ffi::CONFIG_FREERTOS_HZ as i64;
/// 把 Rust 的 socket 地址转成 lwIP 期望的 C 结构。
///
/// 必须逐字段构造而不是 transmute:Rust 的 `SocketAddrV4` 是紧凑的 6 字节(端口在前、
/// 主机字节序),C 的 `sockaddr_in` 是 16 字节且地址是网络字节序——指针强转会发到
/// 一个乱地址,而且不会报错。
fn to_sockaddr(addr: SocketAddrV4) -> ffi::sockaddr_in {
let octets = addr.ip().octets();
ffi::sockaddr_in {
sin_len: std::mem::size_of::<ffi::sockaddr_in>() as u8,
sin_family: ffi::AF_INET as ffi::sa_family_t,
// 用 lwIP 的 htons,字节序不必在 Rust 侧推理。
sin_port: unsafe { ffi::lwip_htons(addr.port()) },
sin_addr: ffi::in_addr {
s_addr: u32::from_le_bytes(octets),
},
sin_zero: [0; 8],
}
}
/// 一个已知客户端。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Peer {
addr: SocketAddrV4,
client_id: u32,
/// 最近一次收到它订阅的时刻。
last_seen_us: i64,
}
/// 发送器的统计。
///
/// 与"传感器产出了多少"分开计数:两者的差就是这条链路的全部故事。
#[derive(Debug, Clone, Copy, Default)]
pub struct SendStats {
pub capture_errors: u32,
/// 所有分片均交给网络的帧(至少一个对端;不代表无线接收确认)。
pub frames_sent: u32,
pub frames_attempted: u32,
pub peer_frames_sent: u32,
pub peer_frames_dropped: u32,
/// 已发出的数据报。
pub fragments_sent: u32,
/// 已发出的字节,含头。
pub bytes_sent: u64,
/// `sendto` 失败次数。
pub errors: u32,
pub retries: u32,
pub last_errno: i32,
pub send_time_us: u64,
/// 因没有客户端而未发送的帧。
pub skipped_no_peer: u32,
/// 收到的订阅数。
pub subscriptions: u32,
/// 已广播的 Hello 数。
pub hellos_sent: u32,
pub capture_queue_skipped: u32,
pub stale_frames_dropped: u32,
pub deadline_peer_drops: u32,
pub max_submitted_age_us: u64,
pub max_send_hold_us: u64,
}
/// 向已订阅的客户端推流,并广播自身存在。
pub struct UdpSender {
socket: i32,
/// 本机标识,MAC 派生,重启后不变。
sender_id: u32,
peers: Vec<Peer>,
next_frame_id: u32,
stats: SendStats,
/// 读客户端订阅的暂存。内容无关紧要——只有源地址有用——所以刻意很小。
announce_buf: [u8; 64],
last_hello_us: i64,
datagram: Vec<u8>,
packet_pacer: Pacer,
retry_budget_us: u64,
}
/// 本机标识:由 MAC 派生,重启后不变。
pub fn local_sender_id() -> u32 {
let mut mac = [0u8; 6];
// SAFETY: `mac` 有 6 字节空间,函数会把 6 字节写满。
let rc = unsafe { ffi::esp_read_mac(mac.as_mut_ptr(), ffi::esp_mac_type_t_ESP_MAC_WIFI_STA) };
if rc != 0 {
warn!("could not read the WiFi MAC (esp_err {rc}); using a fixed id");
return 0xF1F0_0001;
}
let id = protocol::id_from_mac(&mac);
info!("this board's id is 0x{id:08X}, from MAC {mac:02X?}");
debug!("raw MAC bytes: {mac:02X?}");
id
}
impl UdpSender {
/// 打开 socket,绑定到视频端口以便客户端能找到它。
///
/// socket 既收也发:客户端广播订阅进来,板子往它看到的源地址推流。
pub fn new(sender_id: u32) -> Result<Self> {
let packet_gap_us: u64 = option_env!("UDP_PACKET_GAP_US").unwrap_or("0").parse()?;
if packet_gap_us > 5_000 || packet_gap_us % TX_RETRY_WAIT_US as u64 != 0 {
return Err(anyhow!(
"UDP_PACKET_GAP_US must be 0..5000 in scheduler-tick increments"
));
}
let retry_budget_us: u64 = option_env!("UDP_RETRY_BUDGET_US")
.map(str::parse)
.transpose()?
.unwrap_or(DEFAULT_RETRY_BUDGET_US);
if retry_budget_us > MAX_SEND_HOLD_US || retry_budget_us % TX_RETRY_WAIT_US as u64 != 0 {
return Err(anyhow!(
"UDP_RETRY_BUDGET_US must be 0..25000 in scheduler-tick increments"
));
}
info!("UDP packet gap: {packet_gap_us} us; retries {retry_budget_us} us/frame (0=fail fast), hold limit {MAX_SEND_HOLD_US} us, capture age limit {MAX_CAPTURE_AGE_US} us; newest completed frame only");
// SAFETY: 纯 socket 设置,没有指向 Rust 内存的指针。
let socket = unsafe {
let fd = ffi::lwip_socket(ffi::AF_INET as i32, ffi::SOCK_DGRAM as i32, 0);
if fd < 0 {
// lwip_errno 没有单独导出,__errno 是同一个值,新libc 以 int 指针暴露。
return Err(anyhow!(
"could not create UDP socket: errno {}",
*ffi::__errno()
));
}
let enable: i32 = 1;
ffi::lwip_setsockopt(
fd,
ffi::SOL_SOCKET as i32,
ffi::SO_REUSEADDR as i32,
&enable as *const i32 as *const std::ffi::c_void,
std::mem::size_of::<i32>() as u32,
);
// ESP-IDF maps IP precedence 4 to WMM AC_VI. Video should use the
// video queue, which retains AMPDU, rather than best-effort bursts.
let video_priority: i32 = 4 << 5;
if ffi::lwip_setsockopt(
fd,
ffi::IPPROTO_IP as i32,
ffi::IP_TOS as i32,
&video_priority as *const i32 as *const std::ffi::c_void,
std::mem::size_of::<i32>() as u32,
) < 0
{
warn!(
"could not set video Wi-Fi priority: errno {}",
*ffi::__errno()
);
}
// 绑定到视频端口的所有接口。地址留 0,lwIP 视作 INADDR_ANY。
let addr = ffi::sockaddr_in {
sin_len: std::mem::size_of::<ffi::sockaddr_in>() as u8,
sin_family: ffi::AF_INET as ffi::sa_family_t,
sin_port: ffi::lwip_htons(protocol::DEFAULT_PORT),
sin_addr: ffi::in_addr { s_addr: 0 },
sin_zero: [0; 8],
};
let rc = ffi::lwip_bind(
fd,
&addr as *const ffi::sockaddr_in as *const ffi::sockaddr,
std::mem::size_of::<ffi::sockaddr_in>() as ffi::socklen_t,
);
// 非阻塞,这一条不是可选项。同一个 socket 用来在两帧之间查订阅,
// 阻塞的 recvfrom 会把采集循环一直挂到某个客户端发东西为止——那样一来,
// 没有客户端连着时相机会在第一帧后就停住,而不是跑在正常帧率上。
//
// lwIP 的 fcntl 在 C 里是可变参数;bindgen 把第三个参数标成普通 int,
// 对 fcntl 实际要的标志字来说这样正好。
if rc == 0 {
let flags = ffi::lwip_fcntl(fd, ffi::F_GETFL as i32, 0);
if flags < 0
|| ffi::lwip_fcntl(fd, ffi::F_SETFL as i32, flags | ffi::O_NONBLOCK as i32) < 0
{
let err = *ffi::__errno();
ffi::lwip_close(fd);
return Err(anyhow!(
"could not set the UDP socket non-blocking: errno {err}"
));
}
}
if rc < 0 {
let err = *ffi::__errno();
ffi::lwip_close(fd);
return Err(anyhow!(
"could not bind UDP port {}: errno {err}",
protocol::DEFAULT_PORT
));
}
fd
};
info!(
"board 0x{sender_id:08X} listening on UDP {}, broadcasting presence every {HELLO_INTERVAL_MS} ms",
protocol::DEFAULT_PORT
);
Ok(Self {
socket,
sender_id,
peers: Vec::new(),
next_frame_id: 1,
stats: SendStats::default(),
announce_buf: [0u8; 64],
last_hello_us: 0,
datagram: Vec::with_capacity(SEND_BUFFER_SIZE),
packet_pacer: Pacer::with_gap_us(packet_gap_us),
retry_budget_us,
})
}
pub fn stats(&self) -> SendStats {
self.stats
}
pub fn network_lost(&mut self) {
if self.socket >= 0 {
unsafe {
ffi::lwip_close(self.socket);
}
self.socket = -1;
}
self.peers.clear();
}
pub fn restore_network(&mut self) -> Result<()> {
// Keep frame numbering and telemetry across a short radio outage.
let next_frame_id = self.next_frame_id;
let stats = self.stats;
let fresh = Self::new(self.sender_id)?;
*self = fresh;
self.next_frame_id = next_frame_id;
self.stats = stats;
self.broadcast_hello();
self.last_hello_us = now_us();
Ok(())
}
/// 广播一次自身存在。
///
/// 客户端靠它发现有谁在线,所以即使当前没有订阅者也照发——这正是"板子主动
/// 开口"的意义。
pub fn broadcast_hello(&mut self) {
let hello = protocol::Hello {
sender_id: self.sender_id,
// 旧的两字节字段:老客户端还在读它。取值仍是 1("第一代图传板"),
// 新客户端一律看下面的 version / build / protocol。
firmware: 1,
version: firmware_version_packed(),
build: firmware_build(),
// 板子自报的协议版本。客户端据此判断"这块板子我能不能看懂",
// 不匹配时能说出原因,而不是只表现为没有画面。
protocol: protocol::PROTOCOL_VERSION,
};
let buf = hello.encode(protocol::MsgType::Hello);
// 广播地址 255.255.255.255。
let sa = to_sockaddr(SocketAddrV4::new(
protocol::BROADCAST_ADDR,
protocol::DEFAULT_PORT,
));
// SAFETY: 缓冲区与地址在调用期间都有效,sendto 会复制而不是持有指针。
let rc = unsafe {
ffi::lwip_sendto(
self.socket,
buf.as_ptr() as *const std::ffi::c_void,
buf.len(),
ffi::MSG_DONTWAIT as i32,
&sa as *const ffi::sockaddr_in as *const ffi::sockaddr,
std::mem::size_of::<ffi::sockaddr_in>() as ffi::socklen_t,
)
};
if rc >= 0 {
self.stats.hellos_sent += 1;
} else {
warn!("could not broadcast Hello: errno {}", unsafe {
*ffi::__errno()
});
}
}
/// 处理收到的订阅。
///
/// 新的订阅地址会被加入对端列表;已知地址只刷新时间戳,这样客户端可以靠重复
/// 订阅维持推流,而不需要任何超时淘汰。
pub fn handle_incoming(&mut self) {
loop {
let mut from = ffi::sockaddr_in {
sin_len: 0,
sin_family: 0,
sin_port: 0,
sin_addr: ffi::in_addr { s_addr: 0 },
sin_zero: [0; 8],
};
let mut from_len = std::mem::size_of::<ffi::sockaddr_in>() as ffi::socklen_t;
// SAFETY: `from` 是可写的 sockaddr,`from_len` 是 recvfrom 可以写入真实
// 长度的有效指针,接收缓冲有 `len` 字节空间。
let rc = unsafe {
ffi::lwip_recvfrom(
self.socket,
self.announce_buf.as_mut_ptr() as *mut std::ffi::c_void,
self.announce_buf.len(),
ffi::MSG_DONTWAIT as i32,
&mut from as *mut ffi::sockaddr_in as *mut ffi::sockaddr,
&mut from_len,
)
};
if rc < 0 {
// 没有待处理的数据,这是常态。
let err = unsafe { *ffi::__errno() };
if err != 11
/* EWOULDBLOCK */
{
warn!("recvfrom on the subscribe socket failed: errno {err}");
}
return;
}
if from.sin_family != ffi::AF_INET as ffi::sa_family_t {
continue;
}
let Some(sub) = protocol::Subscribe::decode(&self.announce_buf[..rc as usize]) else {
// 不是订阅:可能是另一块板子的 Hello,或者将来的其它消息。安静跳过,
// 每包都打日志没有意义。客户端的身份由它的源地址决定,订阅里带的
// id 用于识别新客户端的一次接收会话。
continue;
};
let addr = SocketAddrV4::new(
Ipv4Addr::from(from.sin_addr.s_addr.to_le_bytes()),
// lwIP 交回的端口是网络字节序,和它进来时一样。
u16::from_be_bytes(from.sin_port.to_ne_bytes()),
);
self.stats.subscriptions += 1;
self.upsert_peer(addr, sub.client_id);
}
}
/// 加入对端,或刷新已知地址的时间戳。
fn upsert_peer(&mut self, addr: SocketAddrV4, client_id: u32) {
let now = now_us();
// New clients use a random session ID. NAT/TUN source-port changes must
// move that session, not create duplicate video destinations. Legacy
// clients all used 0x0C110001, so retain address-based behavior for them.
let session_id = client_id != 0 && client_id != 0x0C11_0001;
if let Some(peer) = self.peers.iter_mut().find(|p| {
p.addr == addr || (session_id && p.client_id == client_id && p.addr.ip() == addr.ip())
}) {
if peer.addr != addr {
info!(
"client session 0x{client_id:08X} moved from {} to {addr}",
peer.addr
);
}
peer.addr = addr;
peer.client_id = client_id;
peer.last_seen_us = now;
return;
}
if self.peers.len() >= MAX_PEERS {
warn!("peer limit ({MAX_PEERS}) reached, ignoring {addr}");
return;
}
info!("client at {addr} subscribed; streaming to it");
self.peers.push(Peer {
addr,
client_id,
last_seen_us: now,
});
}
/// 移除超过 [`PEER_TIMEOUT_MS`] 未续订的对端。
///
/// 由客户端周期性续订,所以超时即代表它离开了——不需要任何探测包,也就避免了
/// 往客户端的接收端口发无意义的数据。
fn drop_stale_peers(&mut self) {
let cutoff = now_us() - PEER_TIMEOUT_MS * 1_000;
self.peers.retain(|p| {
let keep = p.last_seen_us >= cutoff;
if !keep {
info!("client {} stopped subscribing; dropping it", p.addr);
} else {
debug!("client {} still subscribed", p.addr);
}
keep
});
}
/// 把一帧发给每个对端。
///
/// 帧缓冲在返回时就交还驱动——多占着只会增加延迟。
pub fn send(&mut self, frame: &Frame) -> Result<()> {
if frame.data().is_empty() {
return Ok(());
}
if self.peers.is_empty() {
self.stats.skipped_no_peer += 1;
return Ok(());
}
let started = now_us() as u64;
let mut budget = FrameBudget::new(started, frame.captured_at_us(), self.retry_budget_us);
if budget.expired(started) {
self.stats.stale_frames_dropped += 1;
return Ok(());
}
let frame_id = self.next_frame_id;
self.next_frame_id = self.next_frame_id.wrapping_add(1);
self.stats.frames_attempted += 1;
let mut active = [true; MAX_PEERS];
// Borrow the PSRAM JPEG in place. fragment_frame() copies every payload
// into small internal-RAM allocations, competing with Wi-Fi TX buffers.
let jpeg = frame.data();
let frag_count = jpeg.len().div_ceil(protocol::MAX_FRAGMENT_PAYLOAD);
for (frag_index, payload) in jpeg.chunks(protocol::MAX_FRAGMENT_PAYLOAD).enumerate() {
if !active[..self.peers.len()].iter().any(|&peer| peer) {
break;
}
let header = protocol::Header {
sender_id: self.sender_id,
frame_id,
frag_index: frag_index as u16,
frag_count: frag_count as u16,
total_len: jpeg.len() as u32,
width: frame.width() as u16,
height: frame.height() as u16,
..Default::default()
};
self.datagram.clear();
protocol::encode_header_into(&header, &mut self.datagram);
self.datagram.extend_from_slice(payload);
for (index, peer) in self.peers.iter().enumerate() {
if !active[index] {
continue;
}
let sa = to_sockaddr(peer.addr);
loop {
if budget.expired(now_us() as u64) {
active[index] = false;
self.stats.peer_frames_dropped += 1;
self.stats.deadline_peer_drops += 1;
break;
}
let wait_us = self.packet_pacer.delay_us(now_us() as u64);
if wait_us > 0 {
unsafe {
ffi::vTaskDelay(
wait_us.div_ceil(TX_RETRY_WAIT_US as u64) as ffi::TickType_t
)
};
// vTaskDelay wakes on a tick boundary and can return
// before this microsecond deadline; recheck it.
continue;
}
// SAFETY: buffers remain valid until sendto has copied them.
let rc = unsafe {
ffi::lwip_sendto(
self.socket,
self.datagram.as_ptr() as *const std::ffi::c_void,
self.datagram.len(),
ffi::MSG_DONTWAIT as i32,
&sa as *const ffi::sockaddr_in as *const ffi::sockaddr,
std::mem::size_of::<ffi::sockaddr_in>() as ffi::socklen_t,
)
};
if rc == self.datagram.len() as isize {
self.packet_pacer.started(now_us() as u64);
self.stats.fragments_sent += 1;
self.stats.bytes_sent += rc as u64;
break;
}
self.stats.errors += 1;
let errno = unsafe { *ffi::__errno() };
self.stats.last_errno = errno;
if matches!(errno, 11 | 12 | 105)
&& budget.allow_retry(now_us() as u64, TX_RETRY_WAIT_US as u64)
{
self.stats.retries += 1;
// Give the network tasks and TX completion callbacks a
// scheduler tick instead of repeatedly allocating pbufs.
unsafe { ffi::vTaskDelay(1) };
} else {
active[index] = false;
self.stats.peer_frames_dropped += 1;
break;
}
}
}
}
let delivered = active[..self.peers.len()].iter().filter(|&&ok| ok).count();
self.stats.peer_frames_sent += delivered as u32;
self.stats.frames_sent += u32::from(delivered > 0);
let ended = now_us() as u64;
self.stats.send_time_us += ended.saturating_sub(started);
self.stats.max_send_hold_us = self
.stats
.max_send_hold_us
.max(ended.saturating_sub(started));
if delivered > 0 {
self.stats.max_submitted_age_us = self
.stats
.max_submitted_age_us
.max(ended.saturating_sub(frame.captured_at_us()));
}
Ok(())
}
/// 周期性的housekeeping:广播 Hello、处理订阅、清理过期对端。
pub fn housekeeping(&mut self) {
let now = now_us();
if now - self.last_hello_us >= HELLO_INTERVAL_MS * 1_000 {
self.broadcast_hello();
self.last_hello_us = now;
}
self.handle_incoming();
self.drop_stale_peers();
}
/// 有多少个客户端在接收。
pub fn peer_count(&self) -> usize {
self.peers.len()
}
}
/// 客户端超过这个时间没有续订就被视为已离开。
///
/// 是订阅间隔的数倍,所以偶发丢一两个包不会把人踢掉。
const PEER_TIMEOUT_MS: i64 = 6_000;
impl Drop for UdpSender {
fn drop(&mut self) {
// SAFETY: socket 由本值拥有,且只关闭一次。
unsafe {
if self.socket >= 0 {
ffi::lwip_close(self.socket);
}
}
}
}
/// 单调微秒计数。
fn now_us() -> i64 {
// SAFETY: 无参数、无返回缓冲的纯查询。
unsafe { esp_idf_svc::sys::esp_timer_get_time() }
}
/// 采集循环:处理订阅,然后取一帧推出去。
///
/// 顺序是有意的:先处理订阅,一个新客户端就不必多等一整帧才被注意到。
pub fn pump(camera: &Camera, sender: &mut UdpSender) -> Result<()> {
sender.housekeeping();
let frame = match camera.latest_frame() {
Ok(frame) => frame,
Err(_) => {
sender.stats.capture_errors += 1;
// 丢帧是可恢复的——驱动会重新同步——这里没什么可做的。
return Ok(());
}
};
sender.stats.capture_queue_skipped += frame.skipped_before();
sender.send(&frame)
}