xref: /xiu/protocol/rtsp/src/rtp/rtcp/rtcp_app.rs (revision 8e71d710)
1 use super::errors::RtcpError;
2 use super::rtcp_header::RtcpHeader;
3 use crate::rtp::utils::Marshal;
4 use crate::rtp::utils::Unmarshal;
5 use byteorder::BigEndian;
6 use bytes::BytesMut;
7 use bytesio::bytes_reader::BytesReader;
8 use bytesio::bytes_writer::BytesWriter;
9 
10 //  0                   1                   2                   3
11 //  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
12 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13 // |V=2|P|    ST   |   PT=APP=204  |             length            |
14 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
15 // |                           SSRC/CSRC                           |
16 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
17 // |                          name (ASCII)                         |
18 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
19 // |                   application-dependent data                ...
20 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
21 #[derive(Debug, Clone, Default)]
22 pub struct RtcpApp {
23     pub header: RtcpHeader,
24     pub ssrc: u32,
25     pub name: BytesMut,
26     pub app_data: BytesMut,
27 }
28 
29 impl Unmarshal<BytesMut, Result<Self, RtcpError>> for RtcpApp {
unmarshal(data: BytesMut) -> Result<Self, RtcpError> where Self: Sized,30     fn unmarshal(data: BytesMut) -> Result<Self, RtcpError>
31     where
32         Self: Sized,
33     {
34         let mut reader = BytesReader::new(data);
35 
36         let mut rtcp_app = RtcpApp::default();
37         rtcp_app.header = RtcpHeader::unmarshal(&mut reader)?;
38 
39         rtcp_app.ssrc = reader.read_u32::<BigEndian>()?;
40         rtcp_app.name = reader.read_bytes(4)?;
41         rtcp_app.app_data = reader.read_bytes(rtcp_app.header.length as usize * 4)?;
42 
43         Ok(rtcp_app)
44     }
45 }
46 
47 impl Marshal<Result<BytesMut, RtcpError>> for RtcpApp {
marshal(&self) -> Result<BytesMut, RtcpError>48     fn marshal(&self) -> Result<BytesMut, RtcpError> {
49         let mut writer = BytesWriter::default();
50 
51         let header_bytesmut = self.header.marshal()?;
52         writer.write(&header_bytesmut[..])?;
53 
54         writer.write_u32::<BigEndian>(self.ssrc)?;
55         writer.write(&self.name[..])?;
56         writer.write(&self.app_data[..])?;
57 
58         Ok(writer.extract_current_bytes())
59     }
60 }
61