#include "stream_session.h" #include "api_client.h" #include "app_config.h" #include "camera_mgr.h" #include "net_mgr.h" #include "power_mgr.h" static bool s_active = false; static char s_id[40] = {0}; static uint32_t s_end_ms = 0; static uint32_t s_next_ms = 0; static uint32_t s_period_ms = 100; static uint32_t s_frames = 0; static uint8_t s_prev_size = 0; static uint8_t s_fail_streak = 0; bool streamActive() { return s_active; } bool streamStart(const char* session_id, uint8_t mode_override, uint8_t framesize_override, uint16_t duration_override) { if (s_active) { log_w("stream: already running as %s", s_id); return false; } if (!g_cfg.stream_enabled) { log_w("stream: refused, streaming is disabled in config"); return false; } if (!netIsConnected() || !cameraReady()) { log_w("stream: refused, network or camera not ready"); return false; } const uint8_t mode = (mode_override == 0xFF) ? g_cfg.stream_mode : mode_override; const uint8_t fsize = framesize_override ? framesize_override : g_cfg.stream_frame_size; uint16_t duration = duration_override ? duration_override : g_cfg.stream_duration_s; if (duration > 600) duration = 600; // SPEC C9: always time-boxed strncpy(s_id, session_id, sizeof(s_id) - 1); s_id[sizeof(s_id) - 1] = '\0'; if (!apiStreamOpen(s_id, mode, duration)) return false; // Drop to the stream resolution for the session and restore it after, so // a stream never silently changes the resolution of stills. s_prev_size = g_cfg.frame_size; if (fsize != s_prev_size) cameraSetFrameSize(fsize); s_period_ms = 1000UL / (g_cfg.stream_fps ? g_cfg.stream_fps : 10); s_end_ms = millis() + duration * 1000UL; s_next_ms = millis(); s_frames = 0; s_fail_streak = 0; s_active = true; powerInhibit(INHIBIT_STREAM); // released in streamStop() powerNoteActivity(); log_i("stream: %s started, %us @ %ufps, size %u", s_id, duration, g_cfg.stream_fps, fsize); return true; } void streamTick() { if (!s_active) return; if ((int32_t)(s_end_ms - millis()) <= 0) { streamStop("duration reached"); return; } if (!netIsConnected()) { streamStop("wifi lost"); return; } if ((int32_t)(s_next_ms - millis()) > 0) return; s_next_ms += s_period_ms; // If we fell behind (a slow upload, a burst of uploads competing for the // radio) do not try to catch up by bursting — just resync to now. if ((int32_t)(s_next_ms - millis()) < 0) s_next_ms = millis() + s_period_ms; camera_fb_t* fb = cameraGrab(); if (!fb) { if (++s_fail_streak > 10) streamStop("camera keeps failing"); return; } bool sent = apiStreamSendFrame(fb->buf, fb->len, netEpochMs()); esp_camera_fb_return(fb); if (sent) { s_frames++; s_fail_streak = 0; powerNoteActivity(); } else if (++s_fail_streak > 5) { // The Pi closed the connection — a viewer navigated away, or the // session was cancelled server-side. Treat that as a normal end. streamStop("endpoint stopped accepting frames"); } } void streamStop(const char* reason) { if (!s_active) return; s_active = false; apiStreamClose(); if (s_prev_size && s_prev_size != g_cfg.stream_frame_size) { cameraSetFrameSize(s_prev_size); } powerRelease(INHIBIT_STREAM); powerNoteActivity(); // give the idle timer a fresh start before sleeping log_i("stream: %s ended after %u frames — %s", s_id, (unsigned)s_frames, reason); s_id[0] = '\0'; } uint32_t streamFramesSent() { return s_frames; } uint32_t streamSecondsLeft() { if (!s_active) return 0; int32_t left = (int32_t)(s_end_ms - millis()); return left > 0 ? (uint32_t)(left / 1000) : 0; }