Skip to main content

rusty_esp_video_core/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![forbid(unsafe_code)]
3//! `rusty_esp_video-core` — the pure heart of `rusty_esp_video`.
4//!
5//! Espressif's video stack (`esp_video`, `esp_h264`, the CameraWebServer
6//! stream, the ESP-WebRTC capture pipeline) remade as one seam and a set of
7//! packetizers: a frame goes into a [`VideoEncoder`] and comes out as a
8//! [`MediaPacket`]; a packetizer turns packets into bytes a browser, `rff`,
9//! a Pi hub or the mesh can read; a [`PacketSink`] is wherever those bytes go.
10//!
11//! | Module | Holds |
12//! |---|---|
13//! | [`packet`] | [`MediaPacket`], [`Codec`] — the one packet type the family shares |
14//! | [`annexb`] | H.264 Annex-B NAL unit scanning (start codes, IDR and AUD detection) |
15//! | [`encoder`] | [`VideoEncoder`], [`EncoderConfig`], the zero-copy JPEG [`Passthrough`] |
16//! | [`sink`] | [`PacketSink`] and the host/test sinks |
17//! | [`source`] | [`PacketSource`] and [`EncodedSource`] — an `ImageSource` joined to a `VideoEncoder`, paced |
18//! | [`mjpeg_http`] | `multipart/x-mixed-replace` — what a browser opens |
19//! | [`mjpeg_reader`] | the receiving side: a streaming multipart parser over a caller buffer |
20//! | [`rtp`] | RTP headers, RFC 6184 H.264 (single NAL + FU-A) and RFC 2435 JPEG payloaders |
21//! | [`mpegts`] | an MPEG-2 transport stream mux (PAT, PMT, PES, PCR) for H.264 — what `rff -i udp://` reads today |
22//! | [`udp`] | a tiny framing for raw datagrams with a matching reassembler |
23//! | [`pacer`] | a frame-rate cap with drop counters |
24//!
25//! Rules (from the package plan): encoders write into caller buffers;
26//! packetizers stream MTU-sized pieces into a sink without an intermediate
27//! `Vec`; everything is testable on the host and gated against `rff` and
28//! `ffmpeg` where a bitstream is produced.
29
30#[cfg(feature = "alloc")]
31extern crate alloc;
32
33pub mod annexb;
34pub mod encoder;
35#[cfg(feature = "h264")]
36pub mod h264;
37pub mod http;
38#[cfg(feature = "jpeg")]
39pub mod jpeg;
40pub mod mjpeg_http;
41pub mod mjpeg_reader;
42pub mod mpegts;
43pub mod pacer;
44pub mod packet;
45pub mod policy;
46pub mod rtp;
47pub mod sink;
48pub mod source;
49pub mod udp;
50
51pub use encoder::{EncoderConfig, Passthrough, VideoEncoder};
52#[cfg(feature = "h264")]
53pub use h264::H264;
54pub use pacer::Pacer;
55pub use packet::{Codec, MediaPacket};
56pub use policy::{Choice, H264Path, Job, codec_for};
57pub use rusty_esp_core as esp_core;
58pub use sink::PacketSink;
59pub use source::{EncodedSource, PacketSource};
60
61/// The names a sketch or firmware wants in scope.
62pub mod prelude {
63    pub use crate::encoder::{EncoderConfig, Passthrough, VideoEncoder};
64    pub use crate::mjpeg_http::Multipart;
65    pub use crate::mpegts::Mux;
66    pub use crate::pacer::Pacer;
67    pub use crate::packet::{Codec, MediaPacket};
68    pub use crate::sink::PacketSink;
69    pub use crate::source::{EncodedSource, PacketSource};
70    pub use rusty_esp_core::prelude::*;
71}
72
73/// Crate version, for capability manifests and logs.
74pub const VERSION: &str = env!("CARGO_PKG_VERSION");
75
76/// Write a decimal `u64` into `buf`; returns the digits as a `&str`. No `alloc`.
77/// Public for the backend crate's HTTP responder.
78pub fn fmt_u64_pub(v: u64, buf: &mut [u8; 20]) -> &str {
79    fmt_u64(v, buf)
80}
81
82/// Write a decimal `u64` into `buf`; returns the digits as a `&str`. No `alloc`.
83pub(crate) fn fmt_u64(mut v: u64, buf: &mut [u8; 20]) -> &str {
84    let mut i = buf.len();
85    loop {
86        i -= 1;
87        buf[i] = b'0' + (v % 10) as u8;
88        v /= 10;
89        if v == 0 {
90            break;
91        }
92    }
93    // Only ASCII digits were written.
94    core::str::from_utf8(&buf[i..]).unwrap_or("0")
95}
96
97#[cfg(test)]
98mod tests {
99    #[test]
100    fn fmt_u64_edges() {
101        let mut b = [0u8; 20];
102        assert_eq!(super::fmt_u64(0, &mut b), "0");
103        assert_eq!(super::fmt_u64(7, &mut b), "7");
104        assert_eq!(super::fmt_u64(1234567890, &mut b), "1234567890");
105        assert_eq!(super::fmt_u64(u64::MAX, &mut b), "18446744073709551615");
106    }
107}