Skip to content

Commit

Permalink
Merge pull request #136 from binbat/feat/rtp-1200
Browse files Browse the repository at this point in the history
add whipinfo rtp `pkt_size` more than 1200
  • Loading branch information
a-wing authored May 14, 2024
2 parents f9017d5 + 28dcd44 commit 2501032
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,20 @@ OBS Studio whip | :tv: 3 | :shit: | :shit: | :star: | :star: | :shit: |
### whipinto
**NOTE: About `pkt_size=1200`**
WebRTC must need `pkt_size=1200`
If `pkt_size > 1200` (most tool `> 1200`, for example: `ffmpeg` default `1472`), we need to de-payload after re-payload
But now, We support re-size `pkt_size` in `VP8` and `VP9`, You can use any `pkt_size` value in `VP8` and `VP9`
Codec | `AV1` | `VP9` | `VP8` | `H264` | `OPUS` | `G722` |
----------------- | ------ | ------ | ------ | ------ | ------ | ------ |
`pkt_size > 1200` | :shit: | :star: | :star: | :shit: | :shit: | :shit: |
* * *
This tool is `rtp2whip`
Build
Expand Down
1 change: 1 addition & 0 deletions tools/whipinto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
clap = { version = "4.5.1", features = ["derive"] }
webrtc = { git = "https://github.com/webrtc-rs/webrtc", rev = "49140eabfe2bb18cb92ac595888be35ab3b6e5b9" }
bytes = "1.6.0"
anyhow = "1.0"
tokio = { version = "1.36", features = ["full"] }
cli = { path = "../../libs/cli" }
Expand Down
32 changes: 21 additions & 11 deletions tools/whipinto/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{sync::Arc, time::Duration};
use std::{sync::Arc, time::Duration, vec};

use anyhow::{anyhow, Result};
use clap::{ArgAction, Parser};
Expand All @@ -10,24 +10,25 @@ use tokio::{
net::UdpSocket,
sync::mpsc::{unbounded_channel, UnboundedSender},
};
use tracing::{info, trace, warn, Level};
use webrtc::ice_transport::ice_credential_type::RTCIceCredentialType;
use tracing::{debug, info, trace, warn, Level};
use webrtc::{
api::{interceptor_registry::register_default_interceptors, media_engine::*, APIBuilder},
ice_transport::ice_server::RTCIceServer,
ice_transport::{ice_credential_type::RTCIceCredentialType, ice_server::RTCIceServer},
interceptor::registry::Registry,
peer_connection::{
configuration::RTCConfiguration, peer_connection_state::RTCPeerConnectionState,
RTCPeerConnection,
},
rtp,
rtp::packet::Packet,
rtp_transceiver::rtp_codec::RTCRtpCodecCapability,
track::track_local::{
track_local_static_rtp::TrackLocalStaticRTP, TrackLocal, TrackLocalWriter,
},
util::Unmarshal,
};

mod payload;

const PREFIX_LIB: &str = "WEBRTC";

#[derive(Parser)]
Expand Down Expand Up @@ -196,11 +197,12 @@ async fn new_peer(
RTCPeerConnectionState::Closed => {
let _ = complete_tx.send(());
}
_ => {}
v => debug!("{}", v),
};
});
Box::pin(async {})
}));
let mime_type = codec.mime_type.clone();
let track = Arc::new(TrackLocalStaticRTP::new(
codec,
"webrtc".to_owned(),
Expand All @@ -211,14 +213,22 @@ async fn new_peer(
.await
.map_err(|error| anyhow!(format!("{:?}: {}", error, error)))?;
let (send, mut recv) = unbounded_channel::<Vec<u8>>();

tokio::spawn(async move {
let mut sequence_number: u16 = 0;
debug!("Codec is: {}", mime_type);
let mut handler: Box<dyn payload::RePayload + Send> = match mime_type.as_str() {
MIME_TYPE_VP8 => Box::new(payload::RePayloadVpx::new(mime_type)),
MIME_TYPE_VP9 => Box::new(payload::RePayloadVpx::new(mime_type)),
_ => Box::new(payload::Forward::new()),
};

while let Some(data) = recv.recv().await {
if let Ok(mut packet) = rtp::packet::Packet::unmarshal(&mut data.as_slice()) {
if let Ok(packet) = Packet::unmarshal(&mut data.as_slice()) {
trace!("received packet: {}", packet);
packet.header.sequence_number = sequence_number;
let _ = track.write_rtp(&packet).await;
sequence_number = sequence_number.wrapping_add(1);
for packet in handler.payload(packet) {
trace!("send packet: {}", packet);
let _ = track.write_rtp(&packet).await;
}
}
}
});
Expand Down
115 changes: 115 additions & 0 deletions tools/whipinto/src/payload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use bytes::Bytes;
use tracing::error;
use webrtc::{
api::media_engine::*,
rtp::{
codecs::*,
packet::Packet,
packetizer::{Depacketizer, Payloader},
},
};

/// https://github.com/webrtc-rs/webrtc/blob/dcfefd7b48dc2bb9ecf50ea66c304f62719a6c4a/webrtc/src/track/mod.rs#L10C12-L10C49
/// https://github.com/binbat/live777/issues/1200
/// WebRTC Build-in RTP must less 1200
const RTP_OUTBOUND_MTU: usize = 1200;

pub(crate) trait RePayload {
fn payload(&mut self, packet: Packet) -> Vec<Packet>;
}

pub(crate) struct Forward {}

impl Forward {
pub fn new() -> Forward {
Forward {}
}
}

impl RePayload for Forward {
fn payload(&mut self, packet: Packet) -> Vec<Packet> {
vec![packet]
}
}

pub(crate) struct RePayloadVpx {
buffer: Vec<Bytes>,
encoder: Box<dyn Payloader + Send>,
decoder: Box<dyn Depacketizer + Send>,
sequence_number: u16,
/// In order to verify that the sequence number is correct
/// If network have some error loss some packet, We need detect it
src_sequence_number: u16,
}

impl RePayloadVpx {
pub fn new(mime_type: String) -> RePayloadVpx {
RePayloadVpx {
buffer: Vec::new(),
decoder: match mime_type.as_str() {
MIME_TYPE_VP8 => Box::default() as Box<vp8::Vp8Packet>,
MIME_TYPE_VP9 => Box::default() as Box<vp9::Vp9Packet>,
_ => Box::default() as Box<vp8::Vp8Packet>,
},
encoder: match mime_type.as_str() {
MIME_TYPE_VP8 => Box::default() as Box<vp8::Vp8Payloader>,
MIME_TYPE_VP9 => Box::default() as Box<vp9::Vp9Payloader>,
_ => Box::default() as Box<vp8::Vp8Payloader>,
},
sequence_number: 0,
src_sequence_number: 0,
}
}
}

impl RePayload for RePayloadVpx {
fn payload(&mut self, packet: Packet) -> Vec<Packet> {
// verify the sequence number is linear
if self.src_sequence_number + 1 != packet.header.sequence_number
&& self.src_sequence_number != 0
{
error!(
"Should received sequence: {}. But received sequence: {}",
self.src_sequence_number + 1,
packet.header.sequence_number
);
}
self.src_sequence_number = packet.header.sequence_number;

match self.decoder.depacketize(&packet.payload) {
Ok(data) => self.buffer.push(data),
Err(e) => error!("{}", e),
};

if packet.header.marker {
let packets = match self
.encoder
.payload(RTP_OUTBOUND_MTU, &Bytes::from(self.buffer.concat()))
{
Ok(payloads) => {
let length = payloads.len();
payloads
.into_iter()
.enumerate()
.map(|(i, payload)| -> Packet {
let mut header = packet.clone().header;
header.sequence_number = self.sequence_number;
header.marker = matches!(i, x if x == length - 1);

self.sequence_number = self.sequence_number.wrapping_add(1);
Packet { header, payload }
})
.collect::<Vec<Packet>>()
}
Err(e) => {
error!("{}", e);
vec![]
}
};

self.buffer.clear();
return packets;
}
vec![]
}
}

0 comments on commit 2501032

Please sign in to comment.