#include "frame_ring.h" #include static RingFrame* s_ring = nullptr; static size_t s_depth = 0; static size_t s_head = 0; // next slot to write static size_t s_count = 0; static uint32_t s_seq = 0; static SemaphoreHandle_t s_mtx = nullptr; // Slots start at a plausible SVGA JPEG size and grow if a frame does not fit. // Growing costs one realloc; shrinking is never worth the churn. static const size_t kInitialSlotBytes = 48 * 1024; bool ringBegin(uint8_t depth) { ringEnd(); if (depth == 0) return false; s_mtx = xSemaphoreCreateMutex(); if (!s_mtx) return false; s_ring = (RingFrame*)heap_caps_calloc(depth, sizeof(RingFrame), MALLOC_CAP_SPIRAM); if (!s_ring) { log_e("ring: cannot allocate slot table in PSRAM"); vSemaphoreDelete(s_mtx); s_mtx = nullptr; return false; } for (size_t i = 0; i < depth; i++) { s_ring[i].data = (uint8_t*)heap_caps_malloc(kInitialSlotBytes, MALLOC_CAP_SPIRAM); if (!s_ring[i].data) { log_e("ring: PSRAM exhausted at slot %u of %u", (unsigned)i, depth); // Run with the slots we got rather than failing outright — a // shallower ring is still far better than none. depth = i; break; } s_ring[i].cap = kInitialSlotBytes; } if (depth == 0) { ringEnd(); return false; } s_depth = depth; s_head = s_count = 0; s_seq = 0; log_i("ring: %u slots x %uKB = %uKB PSRAM (free %uKB)", (unsigned)s_depth, (unsigned)(kInitialSlotBytes / 1024), (unsigned)(s_depth * kInitialSlotBytes / 1024), (unsigned)(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024)); return true; } void ringEnd() { if (s_mtx) xSemaphoreTake(s_mtx, portMAX_DELAY); if (s_ring) { for (size_t i = 0; i < s_depth; i++) free(s_ring[i].data); free(s_ring); s_ring = nullptr; } s_depth = s_head = s_count = 0; s_seq = 0; if (s_mtx) { xSemaphoreGive(s_mtx); vSemaphoreDelete(s_mtx); s_mtx = nullptr; } } bool ringPush(const uint8_t* data, size_t len, int64_t epoch_ms) { if (!s_ring || !data || len == 0) return false; if (xSemaphoreTake(s_mtx, pdMS_TO_TICKS(100)) != pdTRUE) return false; RingFrame& f = s_ring[s_head]; if (len > f.cap) { uint8_t* bigger = (uint8_t*)heap_caps_realloc(f.data, len, MALLOC_CAP_SPIRAM); if (!bigger) { xSemaphoreGive(s_mtx); log_w("ring: cannot grow slot to %u bytes, dropping frame", (unsigned)len); return false; } f.data = bigger; f.cap = len; } memcpy(f.data, data, len); f.len = len; f.at_ms = millis(); f.at_epoch = epoch_ms; f.seq = ++s_seq; s_head = (s_head + 1) % s_depth; if (s_count < s_depth) s_count++; xSemaphoreGive(s_mtx); return true; } size_t ringTakeNewest(size_t n, FrameCopy* out, size_t out_cap) { if (!s_ring || !out || out_cap == 0) return 0; if (xSemaphoreTake(s_mtx, pdMS_TO_TICKS(500)) != pdTRUE) return 0; if (n > s_count) n = s_count; if (n > out_cap) n = out_cap; size_t written = 0; for (size_t i = 0; i < n; i++) { // Walk oldest-of-the-newest-n forward to newest. size_t back = n - i; // 1 == newest size_t idx = (s_head + s_depth - back) % s_depth; RingFrame& f = s_ring[idx]; if (f.seq == 0 || f.len == 0) continue; // Copies live in PSRAM. A ten-frame event is ~450 KB, which would // exhaust internal RAM and starve the WiFi stack exactly when it is // about to do the most work. uint8_t* buf = (uint8_t*)heap_caps_malloc(f.len, MALLOC_CAP_SPIRAM); if (!buf) { log_w("ring: out of memory copying frame for upload"); break; } memcpy(buf, f.data, f.len); out[written++] = FrameCopy{buf, f.len, f.seq, f.at_ms, f.at_epoch}; } xSemaphoreGive(s_mtx); return written; } uint32_t ringSeq() { return s_seq; } size_t ringCount() { return s_count; } size_t ringBytesUsed() { if (!s_ring) return 0; size_t total = 0; for (size_t i = 0; i < s_depth; i++) total += s_ring[i].cap; return total; }