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