|
| 1 | +use std::io::Write; |
| 2 | + |
| 3 | +use util::Error; |
| 4 | + |
| 5 | +use super::errors::*; |
| 6 | +use super::packet::*; |
| 7 | +use super::source_description::*; |
| 8 | + |
| 9 | +#[cfg(test)] |
| 10 | +mod compound_packet_test; |
| 11 | + |
| 12 | +// A CompoundPacket is a collection of RTCP packets transmitted as a single packet with |
| 13 | +// the underlying protocol (for example UDP). |
| 14 | +// |
| 15 | +// To maximize the resolution of receiption statistics, the first Packet in a CompoundPacket |
| 16 | +// must always be either a SenderReport or a ReceiverReport. This is true even if no data |
| 17 | +// has been sent or received, in which case an empty ReceiverReport must be sent, and even |
| 18 | +// if the only other RTCP packet in the compound packet is a Goodbye. |
| 19 | +// |
| 20 | +// Next, a SourceDescription containing a CNAME item must be included in each CompoundPacket |
| 21 | +// to identify the source and to begin associating media for purposes such as lip-sync. |
| 22 | +// |
| 23 | +// Other RTCP packet types may follow in any order. Packet types may appear more than once. |
| 24 | +#[derive(Debug, Clone)] |
| 25 | +pub struct CompoundPacket(pub Vec<Packet>); |
| 26 | + |
| 27 | +impl CompoundPacket { |
| 28 | + // Validate returns an error if this is not an RFC-compliant CompoundPacket. |
| 29 | + pub fn validate(&self) -> Result<(), Error> { |
| 30 | + if self.0.is_empty() { |
| 31 | + return Err(ERR_EMPTY_COMPOUND.clone()); |
| 32 | + } |
| 33 | + |
| 34 | + // SenderReport and ReceiverReport are the only types that |
| 35 | + // are allowed to be the first packet in a compound datagram |
| 36 | + match &self.0[0] { |
| 37 | + Packet::SenderReport(_) | Packet::ReceiverReport(_) => {} |
| 38 | + _ => return Err(ERR_BAD_FIRST_PACKET.clone()), |
| 39 | + }; |
| 40 | + |
| 41 | + for pkt in &self.0[1..] { |
| 42 | + match pkt { |
| 43 | + // If the number of RecetpionReports exceeds 31 additional ReceiverReports |
| 44 | + // can be included here. |
| 45 | + Packet::ReceiverReport(_) => {} |
| 46 | + |
| 47 | + // A SourceDescription containing a CNAME must be included in every |
| 48 | + // CompoundPacket. |
| 49 | + Packet::SourceDescription(p) => { |
| 50 | + let mut has_cname = false; |
| 51 | + for c in &p.chunks { |
| 52 | + for it in &c.items { |
| 53 | + if it.sdes_type == SDESType::SDESCNAME { |
| 54 | + has_cname = true |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if !has_cname { |
| 60 | + return Err(ERR_MISSING_CNAME.clone()); |
| 61 | + } |
| 62 | + |
| 63 | + return Ok(()); |
| 64 | + } |
| 65 | + // Other packets are not permitted before the CNAME |
| 66 | + _ => return Err(ERR_PACKET_BEFORE_CNAME.clone()), |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + // CNAME never reached |
| 71 | + Err(ERR_MISSING_CNAME.clone()) |
| 72 | + } |
| 73 | + |
| 74 | + //CNAME returns the CNAME that *must* be present in every CompoundPacket |
| 75 | + pub fn cname(&self) -> Result<String, Error> { |
| 76 | + if self.0.is_empty() { |
| 77 | + return Err(ERR_EMPTY_COMPOUND.clone()); |
| 78 | + } |
| 79 | + |
| 80 | + for pkt in &self.0[1..] { |
| 81 | + match pkt { |
| 82 | + Packet::ReceiverReport(_) => {} |
| 83 | + |
| 84 | + // A SourceDescription containing a CNAME must be included in every |
| 85 | + // CompoundPacket. |
| 86 | + Packet::SourceDescription(p) => { |
| 87 | + for c in &p.chunks { |
| 88 | + for it in &c.items { |
| 89 | + if it.sdes_type == SDESType::SDESCNAME { |
| 90 | + return Ok(it.text.clone()); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + _ => return Err(ERR_PACKET_BEFORE_CNAME.clone()), |
| 97 | + }; |
| 98 | + } |
| 99 | + Err(ERR_MISSING_CNAME.clone()) |
| 100 | + } |
| 101 | + |
| 102 | + // Marshal encodes the CompoundPacket as binary. |
| 103 | + pub fn marshal<W: Write>(&self, writer: &mut W) -> Result<(), Error> { |
| 104 | + self.validate()?; |
| 105 | + |
| 106 | + // use packet::marshal function |
| 107 | + marshal(&self.0, writer) |
| 108 | + } |
| 109 | + |
| 110 | + // destination_ssrc returns the synchronization sources associated with this |
| 111 | + // CompoundPacket's reception report. |
| 112 | + pub fn destination_ssrc(&self) -> Vec<u32> { |
| 113 | + if self.0.is_empty() { |
| 114 | + vec![] |
| 115 | + } else { |
| 116 | + match &self.0[0] { |
| 117 | + Packet::SenderReport(p) => p.destination_ssrc(), |
| 118 | + Packet::ReceiverReport(p) => p.destination_ssrc(), |
| 119 | + _ => vec![], |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments