-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtp_parser.rs
52 lines (46 loc) · 1.67 KB
/
rtp_parser.rs
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
use anyhow::{Context, Result};
use data_pipeline_rs::data_handler::{DataTransformer, SomeDataHandler};
use rtp_parse::rtp::rtp_packet::read_rtp_packet;
use crate::{
packet_info::{PacketInfo, SomePacket},
stream_information_store::PayloadTypes,
util::LiveStateReader,
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MediaType {
Audio,
Video,
}
pub struct RtpParser {
payload_types: LiveStateReader<PayloadTypes>,
}
impl RtpParser {
pub fn new(payload_types: LiveStateReader<PayloadTypes>) -> Self {
Self { payload_types }
}
}
impl DataTransformer<PacketInfo> for RtpParser {
fn transform(&mut self, mut data: PacketInfo) -> Result<PacketInfo> {
match data.packet {
SomePacket::UnparsedPacket(packet_data) => {
let rtp_packet = read_rtp_packet(packet_data).context("rtp parse")?;
// println!("parsed rtp packet: {rtp_packet:?}");
match self.payload_types.value().get(&rtp_packet.payload_type()) {
Some(MediaType::Audio) => data.packet = SomePacket::AudioRtpPacket(rtp_packet),
Some(MediaType::Video) => data.packet = SomePacket::VideoRtpPacket(rtp_packet),
None => panic!(
"Unable to find media type for payload type {}",
rtp_packet.payload_type()
),
}
Ok(data)
}
_ => panic!("RTP parser got unexpected packet type: {:x?}", data.packet),
}
}
}
impl From<RtpParser> for SomeDataHandler<PacketInfo> {
fn from(val: RtpParser) -> Self {
SomeDataHandler::Transformer(Box::new(val))
}
}