Skip to content

Commit

Permalink
add RTP sender and receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Feb 19, 2024
1 parent cc726cf commit 211a17c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
8 changes: 3 additions & 5 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use log::{error, info};
use sfu::{EndpointId, SessionId};
use std::io::Write;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc::UnboundedReceiver;
use tokio::sync::Notify;
use webrtc::api::interceptor_registry::register_default_interceptors;
Expand All @@ -26,13 +25,12 @@ use webrtc::rtp_transceiver::rtp_codec::RTCRtpCodecCapability;
use webrtc::rtp_transceiver::rtp_sender::RTCRtpSender;
use webrtc::rtp_transceiver::rtp_transceiver_direction::RTCRtpTransceiverDirection;
use webrtc::rtp_transceiver::RTCRtpTransceiverInit;
use webrtc::track::track_local::track_local_static_sample::TrackLocalStaticSample;
use webrtc::track::track_local::track_local_static_rtp::TrackLocalStaticRTP;
use webrtc::track::track_local::TrackLocal;
use webrtc::track::track_remote::TrackRemote;

pub const HOST: &'static str = "127.0.0.1";
pub const SIGNAL_PORT: u16 = 8080;
pub const OGG_PAGE_DURATION: Duration = Duration::from_millis(20);

fn pretty_sdp(input: &str) -> String {
input.replace("\\r\\n", "\n")
Expand Down Expand Up @@ -349,9 +347,9 @@ pub async fn add_track(
mime_type: &str,
track_id: &str,
direction: RTCRtpTransceiverDirection,
) -> Result<(Arc<RTCRtpSender>, Arc<TrackLocalStaticSample>)> {
) -> Result<(Arc<RTCRtpSender>, Arc<TrackLocalStaticRTP>)> {
// Create a video track
let track = Arc::new(TrackLocalStaticSample::new(
let track = Arc::new(TrackLocalStaticRTP::new(
RTCRtpCodecCapability {
mime_type: mime_type.to_owned(),
..Default::default()
Expand Down
78 changes: 73 additions & 5 deletions tests/play_from_disk_vpx_test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::common::{HOST, SIGNAL_PORT};
use bytes::Bytes;
use log::{error, info};
use rand::random;
use sfu::SessionId;
//use shared::error::Error;
use shared::error::Error;
use webrtc::api::media_engine::MIME_TYPE_VP8;
use webrtc::ice_transport::ice_server::RTCIceServer;
use webrtc::peer_connection::configuration::RTCConfiguration;
use webrtc::peer_connection::sdp::sdp_type::RTCSdpType;
use webrtc::rtp::header::{Extension, Header};
use webrtc::rtp_transceiver::rtp_transceiver_direction::RTCRtpTransceiverDirection;
use webrtc::track::track_local::TrackLocalWriter;

// importing common module.
mod common;
Expand Down Expand Up @@ -48,7 +51,7 @@ async fn test_play_from_disk_vpx_1to1() -> anyhow::Result<()> {
data_channels.push((data_channel_tx, data_channel_rx));
}

let (rtp_sender, _track_local) = match common::add_track(
let (rtp_sender, track_local) = match common::add_track(
&peer_connections[0].1,
MIME_TYPE_VP8,
"video_track",
Expand All @@ -73,7 +76,7 @@ async fn test_play_from_disk_vpx_1to1() -> anyhow::Result<()> {
}
});

let _track_remote_rx = match common::on_track(&peer_connections[1].1).await {
let mut track_remote_rx = match common::on_track(&peer_connections[1].1).await {
Ok(ok) => ok,
Err(err) => {
error!("{}/{}: error {}", session_id, peer_connections[1].0, err);
Expand Down Expand Up @@ -114,16 +117,81 @@ async fn test_play_from_disk_vpx_1to1() -> anyhow::Result<()> {
assert!(false);
}

let mut send_rtp_packet = webrtc::rtp::packet::Packet {
header: Header {
version: 2,
padding: false,
extension: true,
marker: true,
payload_type: 96,
sequence_number: 0,
timestamp: 3653407706,
ssrc: 476325762,
csrc: vec![],
extension_profile: 1,
extensions: vec![Extension {
id: 0,
payload: Bytes::from_static(&[0xFF, 0xFF, 0xFF, 0xFF]),
}],
..Default::default()
},
payload: Bytes::from_static(&[0x98, 0x36, 0xbe, 0x88, 0x9e]),
};

if let Err(err) = track_local.write_rtp(&send_rtp_packet).await {
error!("write_sample: {err}");
assert!(false);
return Err(err.into());
}

// waiting for track_remote for endpoint 1
/*let _track_remote = match track_remote_rx.recv().await {
let track_remote = match track_remote_rx.recv().await {
Some(track_remote) => track_remote,
None => {
assert!(false);
return Err(Error::Other("track remote rx close".to_string()).into());
}
};*/
};

match track_remote.read_rtp().await {
Ok((recv_rtp_packet, _)) => {
assert_eq!(
send_rtp_packet.header.sequence_number,
recv_rtp_packet.header.sequence_number
);
assert_eq!(send_rtp_packet.payload, recv_rtp_packet.payload);
}
Err(err) => {
error!("read_rtp error {:?}", err);
assert!(false);
return Err(err.into());
}
}

// Verify track_local and track_remote match
for i in 1..100 {
send_rtp_packet.header.sequence_number = i;
if let Err(err) = track_local.write_rtp(&send_rtp_packet).await {
error!("write_sample: {err}");
assert!(false);
return Err(err.into());
}

match track_remote.read_rtp().await {
Ok((recv_rtp_packet, _)) => {
assert_eq!(
send_rtp_packet.header.sequence_number,
recv_rtp_packet.header.sequence_number
);
assert_eq!(send_rtp_packet.payload, recv_rtp_packet.payload);
}
Err(err) => {
error!("read_rtp error {:?}", err);
assert!(false);
return Err(err.into());
}
}
}

match common::teardown_peer_connections(peer_connections).await {
Ok(ok) => ok,
Expand Down

0 comments on commit 211a17c

Please sign in to comment.