Skip to content

Commit f576333

Browse files
committed
add connected check in play-from-disk-renegotiation.rs
1 parent c265b0f commit f576333

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ check [Pion Examples](https://github.com/pion/webrtc/tree/master/examples#readme
2929
your browser from a file saved to disk.
3030
-[Play from Disk H26x](play-from-disk-h26x): The play-from-disk-h26x example demonstrates how to send H26x video to
3131
your browser from a file saved to disk.
32-
- 🚧 [Play from Disk Renegotiation](play-from-disk-renegotiation): The play-from-disk-renegotiation example is an
32+
- [Play from Disk Renegotiation](play-from-disk-renegotiation): The play-from-disk-renegotiation example is an
3333
extension of the play-from-disk example, but demonstrates how you can add/remove video tracks from an already
3434
negotiated PeerConnection.
3535
- 🚧 [Insertable Streams](insertable-streams): The insertable-streams example demonstrates how webrtc-rs can be used to

examples/play-from-disk-renegotiation/play-from-disk-renegotiation.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ use rtc::peer_connection::configuration::interceptor_registry::register_default_
1313
use rtc::peer_connection::configuration::media_engine::{MIME_TYPE_VP8, MediaEngine};
1414
use rtc::peer_connection::sdp::RTCSessionDescription;
1515
use rtc::peer_connection::state::RTCSignalingState;
16-
use rtc::peer_connection::transport::RTCIceServer;
1716
use rtc::rtp_transceiver::RTCRtpSenderId;
1817
use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
1918
use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodingParameters, RTCRtpEncodingParameters};
2019
use std::collections::HashMap;
2120
use std::net::SocketAddr;
2221
use std::path::Path;
2322
use std::sync::Arc;
23+
use std::sync::atomic::{AtomicBool, Ordering};
2424
use std::time::Duration;
2525
use std::{
2626
fs::{File, OpenOptions},
@@ -106,6 +106,7 @@ async fn do_signaling(
106106
Err(err) => panic!("{}", err),
107107
};
108108

109+
println!("offer: {}", offer);
109110
if let Err(err) = state.peer_connection.set_remote_description(offer).await {
110111
panic!("{}", err);
111112
}
@@ -132,6 +133,7 @@ async fn do_signaling(
132133
}
133134

134135
let payload = if let Some(local_desc) = state.peer_connection.local_description().await {
136+
println!("answer: {}", local_desc);
135137
match serde_json::to_string(&local_desc) {
136138
Ok(p) => p,
137139
Err(err) => panic!("{}", err),
@@ -165,9 +167,9 @@ async fn create_peer_connection(
165167
async fn add_video(r: Request<Body>, state: Arc<AppState>) -> Result<Response<Body>, hyper::Error> {
166168
let video_track: Arc<TrackLocalStaticSample> = Arc::new(
167169
TrackLocalStaticSample::new(MediaStreamTrack::new(
168-
format!("webrtc-rs-stream-id-{}", RtpCodecKind::Video),
169-
format!("webrtc-rs-track-id-{}", RtpCodecKind::Video),
170-
format!("webrtc-rs-track-label-{}", RtpCodecKind::Video),
170+
format!("webrtc-rs-stream-id-{}", rand::random::<u32>()),
171+
format!("webrtc-rs-track-id-{}", rand::random::<u32>()),
172+
format!("webrtc-rs-track-label-{}", rand::random::<u32>()),
171173
RtpCodecKind::Video,
172174
vec![RTCRtpEncodingParameters {
173175
rtp_coding_parameters: RTCRtpCodingParameters {
@@ -282,6 +284,7 @@ struct Cli {
282284
struct TestHandler {
283285
gather_complete_tx: Sender<()>,
284286
done_tx: Sender<()>,
287+
connected: Arc<AtomicBool>,
285288
connection_notify: Notify,
286289
}
287290

@@ -298,8 +301,10 @@ impl PeerConnectionEventHandler for TestHandler {
298301
match state {
299302
RTCPeerConnectionState::Connected => {
300303
println!("Peer Connection State has gone to connected!");
304+
self.connected.store(true, Ordering::SeqCst);
301305
}
302306
RTCPeerConnectionState::Failed => {
307+
self.connected.store(false, Ordering::SeqCst);
303308
let _ = self.done_tx.try_send(());
304309
}
305310
_ => {}
@@ -308,7 +313,7 @@ impl PeerConnectionEventHandler for TestHandler {
308313

309314
async fn on_signaling_state_change(&self, state: RTCSignalingState) {
310315
println!("Signaling State has changed: {state}");
311-
if state == RTCSignalingState::Stable {
316+
if state == RTCSignalingState::Stable && self.connected.load(Ordering::SeqCst) {
312317
self.connection_notify.notify_waiters();
313318
}
314319
}
@@ -376,11 +381,14 @@ async fn async_main() -> Result<()> {
376381
let registry = register_default_interceptors(Registry::new(), &mut media_engine)?;
377382

378383
// Create RTC peer connection configuration
384+
379385
let config = RTCConfigurationBuilder::new()
386+
/*TODO: Fix localhost ip 127.0.0.1 takes too long to recv RTCIceGatheringState::Complete,
387+
when stun:stun.l.google.com:19302 is set #778
380388
.with_ice_servers(vec![RTCIceServer {
381389
urls: vec!["stun:stun.l.google.com:19302".to_string()],
382390
..Default::default()
383-
}])
391+
}])*/
384392
.build();
385393

386394
// Create the API object with the MediaEngine
@@ -398,6 +406,7 @@ async fn async_main() -> Result<()> {
398406
let handler = Arc::new(TestHandler {
399407
gather_complete_tx,
400408
done_tx: done_tx.clone(),
409+
connected: Arc::new(AtomicBool::new(false)),
401410
connection_notify: connection_notify.clone(),
402411
});
403412

@@ -408,7 +417,7 @@ async fn async_main() -> Result<()> {
408417
.with_interceptor_registry(registry)
409418
.with_handler(handler)
410419
.with_runtime(runtime.clone())
411-
.with_udp_addrs(vec![format!("{}:0", signal::get_local_ip())])
420+
.with_udp_addrs(vec![format!("127.0.0.1:0")])
412421
.build()
413422
.await?,
414423
);

0 commit comments

Comments
 (0)