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
use crate::error::{Error, Result};
use util::{marshal::*, Buffer};
use tokio::sync::mpsc;
pub const SRTP_BUFFER_SIZE: usize = 1000 * 1000;
pub const SRTCP_BUFFER_SIZE: usize = 100 * 1000;
#[derive(Debug)]
pub struct Stream {
ssrc: u32,
tx: mpsc::Sender<u32>,
pub(crate) buffer: Buffer,
is_rtp: bool,
}
impl Stream {
pub fn new(ssrc: u32, tx: mpsc::Sender<u32>, is_rtp: bool) -> Self {
Stream {
ssrc,
tx,
buffer: Buffer::new(
0,
if is_rtp {
SRTP_BUFFER_SIZE
} else {
SRTCP_BUFFER_SIZE
},
),
is_rtp,
}
}
pub fn get_ssrc(&self) -> u32 {
self.ssrc
}
pub fn is_rtp_stream(&self) -> bool {
self.is_rtp
}
pub async fn read(&self, buf: &mut [u8]) -> Result<usize> {
Ok(self.buffer.read(buf, None).await?)
}
pub async fn read_rtp(&self, buf: &mut [u8]) -> Result<(usize, rtp::header::Header)> {
if !self.is_rtp {
return Err(Error::InvalidRtpStream);
}
let n = self.buffer.read(buf, None).await?;
let mut b = &buf[..n];
let header = rtp::header::Header::unmarshal(&mut b)?;
Ok((n, header))
}
pub async fn read_rtcp(&self, buf: &mut [u8]) -> Result<(usize, rtcp::header::Header)> {
if self.is_rtp {
return Err(Error::InvalidRtcpStream);
}
let n = self.buffer.read(buf, None).await?;
let mut b = &buf[..n];
let header = rtcp::header::Header::unmarshal(&mut b)?;
Ok((n, header))
}
pub async fn close(&self) -> Result<()> {
self.buffer.close().await;
let _ = self.tx.send(self.ssrc).await;
Ok(())
}
}