1use core::fmt;
11
12use zencodec::LimitKind;
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct MediaLimits {
18 pub max_packet_bytes: usize,
20 pub max_tracks: u32,
22 pub max_header_bytes: u64,
24 pub max_table_entries: u64,
26 pub max_queued_packets: usize,
28 pub max_queued_frames: usize,
30}
31
32impl MediaLimits {
33 pub const DEFAULT: Self = Self {
36 max_packet_bytes: 64 << 20,
37 max_tracks: 64,
38 max_header_bytes: 256 << 20,
39 max_table_entries: 4 << 20,
40 max_queued_packets: 64,
41 max_queued_frames: 8,
42 };
43
44 pub const fn with_max_packet_bytes(mut self, n: usize) -> Self {
46 self.max_packet_bytes = n;
47 self
48 }
49
50 pub const fn with_max_tracks(mut self, n: u32) -> Self {
52 self.max_tracks = n;
53 self
54 }
55
56 pub const fn with_max_header_bytes(mut self, n: u64) -> Self {
58 self.max_header_bytes = n;
59 self
60 }
61
62 pub const fn with_max_table_entries(mut self, n: u64) -> Self {
64 self.max_table_entries = n;
65 self
66 }
67
68 pub const fn with_max_queued_packets(mut self, n: usize) -> Self {
70 self.max_queued_packets = n;
71 self
72 }
73
74 pub const fn with_max_queued_frames(mut self, n: usize) -> Self {
76 self.max_queued_frames = n;
77 self
78 }
79
80 pub fn check_packet_bytes(&self, bytes: u64) -> Result<(), MediaLimitExceeded> {
86 MediaLimitExceeded::check(LimitKind::PacketBytes, bytes, self.max_packet_bytes as u64)
87 }
88
89 pub fn check_tracks(&self, tracks: u64) -> Result<(), MediaLimitExceeded> {
95 MediaLimitExceeded::check(LimitKind::Tracks, tracks, u64::from(self.max_tracks))
96 }
97
98 pub fn check_header_bytes(&self, bytes: u64) -> Result<(), MediaLimitExceeded> {
104 MediaLimitExceeded::check(LimitKind::HeaderBytes, bytes, self.max_header_bytes)
105 }
106
107 pub fn check_table_entries(&self, entries: u64) -> Result<(), MediaLimitExceeded> {
113 MediaLimitExceeded::check(LimitKind::TableEntries, entries, self.max_table_entries)
114 }
115}
116
117impl Default for MediaLimits {
118 fn default() -> Self {
119 Self::DEFAULT
120 }
121}
122
123#[derive(Clone, Copy, Debug, PartialEq, Eq)]
125#[non_exhaustive]
126pub struct MediaLimitExceeded {
127 pub kind: LimitKind,
129 pub requested: u64,
131 pub limit: u64,
133}
134
135impl MediaLimitExceeded {
136 pub const fn check(kind: LimitKind, requested: u64, limit: u64) -> Result<(), Self> {
142 if requested > limit {
143 Err(Self {
144 kind,
145 requested,
146 limit,
147 })
148 } else {
149 Ok(())
150 }
151 }
152}
153
154impl fmt::Display for MediaLimitExceeded {
155 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156 write!(
157 f,
158 "{:?} limit exceeded: {} > {}",
159 self.kind, self.requested, self.limit
160 )
161 }
162}
163
164impl core::error::Error for MediaLimitExceeded {}