Skip to content

Commit

Permalink
Merge pull request webrtc-rs#3 from webrtc-rs/v0.3.0
Browse files Browse the repository at this point in the history
pull v0.3.0beta to main
  • Loading branch information
rainliu authored Nov 8, 2021
2 parents af408c9 + e4eb3b6 commit c2b4e02
Show file tree
Hide file tree
Showing 31 changed files with 941 additions and 956 deletions.
19 changes: 9 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "examples"
version = "0.2.1"
version = "0.3.0"
authors = ["Rain Liu <yliu@webrtc.rs>"]
edition = "2018"
description = "Examples of WebRTC.rs stack"
Expand All @@ -15,8 +15,7 @@ repository = "https://github.com/webrtc-rs/examples"


[dev-dependencies]
util = { package = "webrtc-util", version = "0.5.0" }
webrtc = "0.2.2"
webrtc = "0.3.0"
tokio = { version = "1.12.0", features = ["full"] }
env_logger = "0.9.0"
clap = "2"
Expand All @@ -36,6 +35,11 @@ console-subscriber = { git = "https://github.com/tokio-rs/console", branch = "ma
[profile.dev]
opt-level = 0

[[example]]
name = "rc-cycle"
path = "examples/rc-cycle/rc-cycle.rs"
bench = false

[[example]]
name = "broadcast"
path = "examples/broadcast/broadcast.rs"
Expand Down Expand Up @@ -77,13 +81,8 @@ path = "examples/insertable-streams/insertable-streams.rs"
bench = false

[[example]]
name = "play-from-disk-vp8"
path = "examples/play-from-disk-vp8/play-from-disk-vp8.rs"
bench = false

[[example]]
name = "play-from-disk-vp9"
path = "examples/play-from-disk-vp9/play-from-disk-vp9.rs"
name = "play-from-disk-vpx"
path = "examples/play-from-disk-vpx/play-from-disk-vpx.rs"
bench = false

[[example]]
Expand Down
3 changes: 1 addition & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ All examples are ported from [Pion](https://github.com/pion/webrtc/tree/master/e

#### Media API
- [x] [Reflect](reflect): The reflect example demonstrates how to have webrtc-rs send back to the user exactly what it receives using the same PeerConnection.
- [x] [Play from Disk VP8](play-from-disk-vp8): The play-from-disk-vp8 example demonstrates how to send VP8 video to your browser from a file saved to disk.
- [x] [Play from Disk VP9](play-from-disk-vp9): The play-from-disk-vp9 example demonstrates how to send VP9 video to your browser from a file saved to disk.
- [x] [Play from Disk VPx](play-from-disk-vpx): The play-from-disk-vp8 example demonstrates how to send VP8/VP9 video to your browser from a file saved to disk.
- [x] [Play from Disk H264](play-from-disk-h264): The play-from-disk-h264 example demonstrates how to send H264 video to your browser from a file saved to disk.
- [x] [Play from Disk Renegotiation](play-from-disk-renegotiation): The play-from-disk-renegotiation example is an extension of the play-from-disk example, but demonstrates how you can add/remove video tracks from an already negotiated PeerConnection.
- [x] [Insertable Streams](insertable-streams): The insertable-streams example demonstrates how webrtc-rs can be used to send E2E encrypted video and decrypt via insertable streams in the browser.
Expand Down
34 changes: 19 additions & 15 deletions examples/broadcast/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use tokio::time::Duration;
use webrtc::api::interceptor_registry::register_default_interceptors;
use webrtc::api::media_engine::MediaEngine;
use webrtc::api::APIBuilder;
use webrtc::ice_transport::ice_server::RTCIceServer;
use webrtc::interceptor::registry::Registry;
use webrtc::media::rtp::rtp_codec::RTPCodecType;
use webrtc::media::rtp::rtp_receiver::RTCRtpReceiver;
use webrtc::media::track::track_local::track_local_static_rtp::TrackLocalStaticRTP;
use webrtc::media::track::track_local::{TrackLocal, TrackLocalWriter};
use webrtc::media::track::track_remote::TrackRemote;
use webrtc::peer::configuration::RTCConfiguration;
use webrtc::peer::ice::ice_server::RTCIceServer;
use webrtc::peer::peer_connection_state::RTCPeerConnectionState;
use webrtc::peer::sdp::session_description::RTCSessionDescription;
use webrtc::peer_connection::configuration::RTCConfiguration;
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;
use webrtc::peer_connection::sdp::session_description::RTCSessionDescription;
use webrtc::rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication;
use webrtc::rtp_transceiver::rtp_codec::RTPCodecType;
use webrtc::rtp_transceiver::rtp_receiver::RTCRtpReceiver;
use webrtc::track::track_local::track_local_static_rtp::TrackLocalStaticRTP;
use webrtc::track::track_local::{TrackLocal, TrackLocalWriter};
use webrtc::track::track_remote::TrackRemote;
use webrtc::Error;

#[tokio::main]
Expand Down Expand Up @@ -96,7 +96,7 @@ async fn main() -> Result<()> {
let mut registry = Registry::new();

// Use the default set of Interceptors
registry = register_default_interceptors(registry, &mut m)?;
registry = register_default_interceptors(registry, &mut m).await?;

// Create the API object with the MediaEngine
let api = APIBuilder::new()
Expand Down Expand Up @@ -127,15 +127,15 @@ async fn main() -> Result<()> {
let local_track_chan_tx = Arc::new(local_track_chan_tx);
// Set a handler for when a new remote track starts, this handler copies inbound RTP packets,
// replaces the SSRC and sends them back
let pc = Arc::clone(&peer_connection);
let pc = Arc::downgrade(&peer_connection);
peer_connection
.on_track(Box::new(
move |track: Option<Arc<TrackRemote>>, _receiver: Option<Arc<RTCRtpReceiver>>| {
if let Some(track) = track {
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
// This is a temporary fix until we implement incoming RTCP events, then we would push a PLI only when a viewer requests it
let media_ssrc = track.ssrc();
let pc2 = Arc::clone(&pc);
let pc2 = pc.clone();
tokio::spawn(async move {
let mut result = Result::<usize>::Ok(0);
while result.is_ok() {
Expand All @@ -144,10 +144,14 @@ async fn main() -> Result<()> {

tokio::select! {
_ = timeout.as_mut() =>{
result = pc2.write_rtcp(&PictureLossIndication{
if let Some(pc) = pc2.upgrade(){
result = pc.write_rtcp(&[Box::new(PictureLossIndication{
sender_ssrc: 0,
media_ssrc,
}).await.map_err(Into::into);
})]).await.map_err(Into::into);
}else{
break;
}
}
};
}
Expand Down Expand Up @@ -237,7 +241,7 @@ async fn main() -> Result<()> {
let mut registry = Registry::new();

// Use the default set of Interceptors
registry = register_default_interceptors(registry, &mut m)?;
registry = register_default_interceptors(registry, &mut m).await?;

// Create the API object with the MediaEngine
let api = APIBuilder::new()
Expand Down
29 changes: 19 additions & 10 deletions examples/data-channels-close/data-channels-close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use tokio::time::Duration;
use webrtc::api::interceptor_registry::register_default_interceptors;
use webrtc::api::media_engine::MediaEngine;
use webrtc::api::APIBuilder;
use webrtc::data::data_channel::data_channel_message::DataChannelMessage;
use webrtc::data::data_channel::RTCDataChannel;
use webrtc::data_channel::data_channel_message::DataChannelMessage;
use webrtc::data_channel::RTCDataChannel;
use webrtc::ice_transport::ice_server::RTCIceServer;
use webrtc::interceptor::registry::Registry;
use webrtc::peer::configuration::RTCConfiguration;
use webrtc::peer::ice::ice_server::RTCIceServer;
use webrtc::peer::peer_connection_state::RTCPeerConnectionState;
use webrtc::peer::sdp::session_description::RTCSessionDescription;
use webrtc::util::math_rand_alpha;
use webrtc::peer_connection::configuration::RTCConfiguration;
use webrtc::peer_connection::math_rand_alpha;
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;
use webrtc::peer_connection::sdp::session_description::RTCSessionDescription;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -92,7 +92,7 @@ async fn main() -> Result<()> {
let mut registry = Registry::new();

// Use the default set of Interceptors
registry = register_default_interceptors(registry, &mut m)?;
registry = register_default_interceptors(registry, &mut m).await?;

// Create the API object with the MediaEngine
let api = APIBuilder::new()
Expand All @@ -112,6 +112,8 @@ async fn main() -> Result<()> {
// Create a new RTCPeerConnection
let peer_connection = Arc::new(api.new_peer_connection(config).await?);

let (done_tx, mut done_rx) = tokio::sync::mpsc::channel::<()>(1);

// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
peer_connection
Expand All @@ -123,7 +125,7 @@ async fn main() -> Result<()> {
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
println!("Peer Connection has gone to failed exiting");
std::process::exit(0);
let _ = done_tx.try_send(());
}

Box::pin(async {})
Expand Down Expand Up @@ -226,7 +228,14 @@ async fn main() -> Result<()> {
}

println!("Press ctrl-c to stop");
tokio::signal::ctrl_c().await.unwrap();
tokio::select! {
_ = done_rx.recv() => {
println!("received done signal!");
}
_ = tokio::signal::ctrl_c() => {
println!("");
}
};

peer_connection.close().await?;

Expand Down
27 changes: 18 additions & 9 deletions examples/data-channels-create/data-channels-create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use tokio::time::Duration;
use webrtc::api::interceptor_registry::register_default_interceptors;
use webrtc::api::media_engine::MediaEngine;
use webrtc::api::APIBuilder;
use webrtc::data::data_channel::data_channel_message::DataChannelMessage;
use webrtc::data_channel::data_channel_message::DataChannelMessage;
use webrtc::ice_transport::ice_server::RTCIceServer;
use webrtc::interceptor::registry::Registry;
use webrtc::peer::configuration::RTCConfiguration;
use webrtc::peer::ice::ice_server::RTCIceServer;
use webrtc::peer::peer_connection_state::RTCPeerConnectionState;
use webrtc::peer::sdp::session_description::RTCSessionDescription;
use webrtc::util::math_rand_alpha;
use webrtc::peer_connection::configuration::RTCConfiguration;
use webrtc::peer_connection::math_rand_alpha;
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;
use webrtc::peer_connection::sdp::session_description::RTCSessionDescription;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -74,7 +74,7 @@ async fn main() -> Result<()> {
let mut registry = Registry::new();

// Use the default set of Interceptors
registry = register_default_interceptors(registry, &mut m)?;
registry = register_default_interceptors(registry, &mut m).await?;

// Create the API object with the MediaEngine
let api = APIBuilder::new()
Expand All @@ -97,6 +97,8 @@ async fn main() -> Result<()> {
// Create a datachannel with label 'data'
let data_channel = peer_connection.create_data_channel("data", None).await?;

let (done_tx, mut done_rx) = tokio::sync::mpsc::channel::<()>(1);

// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
peer_connection
Expand All @@ -108,7 +110,7 @@ async fn main() -> Result<()> {
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
println!("Peer Connection has gone to failed exiting");
std::process::exit(0);
let _ = done_tx.try_send(());
}

Box::pin(async {})
Expand Down Expand Up @@ -180,7 +182,14 @@ async fn main() -> Result<()> {
peer_connection.set_remote_description(answer).await?;

println!("Press ctrl-c to stop");
tokio::signal::ctrl_c().await.unwrap();
tokio::select! {
_ = done_rx.recv() => {
println!("received done signal!");
}
_ = tokio::signal::ctrl_c() => {
println!("");
}
};

peer_connection.close().await?;

Expand Down
29 changes: 19 additions & 10 deletions examples/data-channels-detach-create/data-channels-detach-create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use webrtc::api::interceptor_registry::register_default_interceptors;
use webrtc::api::media_engine::MediaEngine;
use webrtc::api::setting_engine::SettingEngine;
use webrtc::api::APIBuilder;
use webrtc::ice_transport::ice_server::RTCIceServer;
use webrtc::interceptor::registry::Registry;
use webrtc::peer::configuration::RTCConfiguration;
use webrtc::peer::ice::ice_server::RTCIceServer;
use webrtc::peer::peer_connection_state::RTCPeerConnectionState;
use webrtc::peer::sdp::session_description::RTCSessionDescription;
use webrtc::util::math_rand_alpha;
use webrtc::peer_connection::configuration::RTCConfiguration;
use webrtc::peer_connection::math_rand_alpha;
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;
use webrtc::peer_connection::sdp::session_description::RTCSessionDescription;

const MESSAGE_SIZE: usize = 1500;

Expand Down Expand Up @@ -77,7 +77,7 @@ async fn main() -> Result<()> {
let mut registry = Registry::new();

// Use the default set of Interceptors
registry = register_default_interceptors(registry, &mut m)?;
registry = register_default_interceptors(registry, &mut m).await?;

// Since this behavior diverges from the WebRTC API it has to be
// enabled using a settings engine. Mixing both detached and the
Expand Down Expand Up @@ -109,6 +109,8 @@ async fn main() -> Result<()> {
// Create a datachannel with label 'data'
let data_channel = peer_connection.create_data_channel("data", None).await?;

let (done_tx, mut done_rx) = tokio::sync::mpsc::channel::<()>(1);

// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
peer_connection
Expand All @@ -120,7 +122,7 @@ async fn main() -> Result<()> {
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
println!("Peer Connection has gone to failed exiting");
std::process::exit(0);
let _ = done_tx.try_send(());
}

Box::pin(async {})
Expand Down Expand Up @@ -189,15 +191,22 @@ async fn main() -> Result<()> {
peer_connection.set_remote_description(answer).await?;

println!("Press ctrl-c to stop");
tokio::signal::ctrl_c().await.unwrap();
tokio::select! {
_ = done_rx.recv() => {
println!("received done signal!");
}
_ = tokio::signal::ctrl_c() => {
println!("");
}
};

peer_connection.close().await?;

Ok(())
}

// read_loop shows how to read from the datachannel directly
async fn read_loop(d: Arc<webrtc::webrtc_data::data_channel::DataChannel>) -> Result<()> {
async fn read_loop(d: Arc<webrtc::data::data_channel::DataChannel>) -> Result<()> {
let mut buffer = vec![0u8; MESSAGE_SIZE];
loop {
let n = match d.read(&mut buffer).await {
Expand All @@ -216,7 +225,7 @@ async fn read_loop(d: Arc<webrtc::webrtc_data::data_channel::DataChannel>) -> Re
}

// write_loop shows how to write to the datachannel directly
async fn write_loop(d: Arc<webrtc::webrtc_data::data_channel::DataChannel>) -> Result<()> {
async fn write_loop(d: Arc<webrtc::data::data_channel::DataChannel>) -> Result<()> {
let mut result = Result::<usize>::Ok(0);
while result.is_ok() {
let timeout = tokio::time::sleep(Duration::from_secs(5));
Expand Down
Loading

0 comments on commit c2b4e02

Please sign in to comment.