rtci_rtcp/goodbye/mod.rs
1#[cfg(test)]
2mod goodbye_test;
3
4use crate::{header::*, packet::*, util::*};
5use shared::{
6 error::{Error, Result},
7 marshal::{Marshal, MarshalSize, Unmarshal},
8};
9
10use bytes::{Buf, BufMut, Bytes};
11use std::any::Any;
12use std::fmt;
13
14/// The Goodbye packet indicates that one or more sources are no longer active.
15#[derive(Debug, PartialEq, Eq, Default, Clone)]
16pub struct Goodbye {
17 /// The SSRC/CSRC identifiers that are no longer active
18 pub sources: Vec<u32>,
19 /// Optional text indicating the reason for leaving, e.g., "camera malfunction" or "RTP loop detected"
20 pub reason: Bytes,
21}
22
23impl fmt::Display for Goodbye {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 let mut out = "Goodbye:\n\tSources:\n".to_string();
26 for s in &self.sources {
27 out += format!("\t{}\n", *s).as_str();
28 }
29 out += format!("\tReason: {:?}\n", self.reason).as_str();
30
31 write!(f, "{out}")
32 }
33}
34
35impl Packet for Goodbye {
36 /// Header returns the Header associated with this packet.
37 fn header(&self) -> Header {
38 Header {
39 // The null octets after the reason are not the padding of the P bit (RFC 3550 6.6).
40 padding: false,
41 count: self.sources.len() as u8,
42 packet_type: PacketType::Goodbye,
43 length: ((self.marshal_size() / 4) - 1) as u16,
44 }
45 }
46
47 /// destination_ssrc returns an array of SSRC values that this packet refers to.
48 fn destination_ssrc(&self) -> Vec<u32> {
49 self.sources.to_vec()
50 }
51
52 fn raw_size(&self) -> usize {
53 let srcs_length = self.sources.len() * SSRC_LENGTH;
54 // The reason is optional: without one there is no length octet either.
55 let reason_length = if self.reason.is_empty() {
56 0
57 } else {
58 self.reason.len() + 1
59 };
60
61 HEADER_LENGTH + srcs_length + reason_length
62 }
63
64 fn as_any(&self) -> &dyn Any {
65 self
66 }
67
68 fn equal(&self, other: &dyn Packet) -> bool {
69 other.as_any().downcast_ref::<Goodbye>() == Some(self)
70 }
71
72 fn cloned(&self) -> Box<dyn Packet> {
73 Box::new(self.clone())
74 }
75}
76
77impl MarshalSize for Goodbye {
78 fn marshal_size(&self) -> usize {
79 let l = self.raw_size();
80 // align to 32-bit boundary
81 l + get_padding_size(l)
82 }
83}
84
85impl Marshal for Goodbye {
86 /// marshal_to encodes the packet in binary.
87 fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
88 if self.sources.len() > COUNT_MAX {
89 return Err(Error::TooManySources);
90 }
91
92 if self.reason.len() > SDES_MAX_OCTET_COUNT {
93 return Err(Error::ReasonTooLong);
94 }
95
96 if buf.remaining_mut() < self.marshal_size() {
97 return Err(Error::BufferTooShort);
98 }
99
100 /*
101 * 0 1 2 3
102 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
103 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 * |V=2|P| SC | PT=BYE=203 | length |
105 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 * | SSRC/CSRC |
107 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 * : ... :
109 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
110 * (opt) | length | reason for leaving ...
111 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 */
113
114 let h = self.header();
115 let n = h.marshal_to(buf)?;
116 buf = &mut buf[n..];
117
118 for source in &self.sources {
119 buf.put_u32(*source);
120 }
121
122 if !self.reason.is_empty() {
123 buf.put_u8(self.reason.len() as u8);
124 buf.put(self.reason.clone());
125 // "If the string fills the packet to the next 32-bit boundary, the string is not
126 // null terminated. If not, the BYE packet MUST be padded with null octets to the
127 // next 32-bit boundary. This padding is separate from that indicated by the P bit"
128 buf.put_bytes(0, get_padding_size(self.raw_size()));
129 }
130
131 Ok(self.marshal_size())
132 }
133}
134
135impl Unmarshal for Goodbye {
136 fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
137 where
138 Self: Sized,
139 B: Buf,
140 {
141 /*
142 * 0 1 2 3
143 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
144 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 * |V=2|P| SC | PT=BYE=203 | length |
146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
147 * | SSRC/CSRC |
148 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149 * : ... :
150 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
151 * (opt) | length | reason for leaving ...
152 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
153 */
154 let (header, mut body) = read_packet(raw_packet)?;
155 if header.packet_type != PacketType::Goodbye {
156 return Err(Error::WrongType);
157 }
158
159 // SC sources, four octets each.
160 if body.len() < header.count as usize * SSRC_LENGTH {
161 return Err(Error::PacketTooShort);
162 }
163 let mut sources = Vec::with_capacity(header.count as usize);
164 for _ in 0..header.count {
165 sources.push(body.get_u32());
166 }
167
168 // Then, optionally, a length octet and that many octets of reason. What is left after
169 // them pads the packet to a 32-bit boundary.
170 let reason = if body.has_remaining() {
171 let reason_len = body.get_u8() as usize;
172 if reason_len > body.len() {
173 return Err(Error::PacketTooShort);
174 }
175 body.split_to(reason_len)
176 } else {
177 Bytes::new()
178 };
179
180 Ok(Goodbye { sources, reason })
181 }
182}