examples: webrtc: rust: Fix a couple of minor clippy warnings

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3928>
This commit is contained in:
Sebastian Dröge 2023-02-10 13:16:21 +02:00 committed by GStreamer Marge Bot
parent 28ab612a88
commit fc5bad5f75
3 changed files with 38 additions and 46 deletions

View file

@ -161,7 +161,7 @@ fn check_plugins() -> Result<(), anyhow::Error> {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if !missing.is_empty() { if !missing.is_empty() {
bail!("Missing plugins: {:?}", missing); bail!("Missing plugins: {missing:?}");
} else { } else {
Ok(()) Ok(())
} }

View file

@ -162,9 +162,7 @@ impl App {
audiotestsrc wave=silence is-live=true ! audio-mixer. \ audiotestsrc wave=silence is-live=true ! audio-mixer. \
audiomixer name=audio-mixer sink_0::mute=true ! audioconvert ! audioresample ! autoaudiosink \ audiomixer name=audio-mixer sink_0::mute=true ! audioconvert ! audioresample ! autoaudiosink \
videotestsrc pattern=black ! capsfilter caps=video/x-raw,width=1,height=1 ! video-mixer. \ videotestsrc pattern=black ! capsfilter caps=video/x-raw,width=1,height=1 ! video-mixer. \
compositor name=video-mixer background=black sink_0::alpha=0.0 ! capsfilter caps=video/x-raw,width={width},height={height} ! videoconvert ! autovideosink", compositor name=video-mixer background=black sink_0::alpha=0.0 ! capsfilter caps=video/x-raw,width={VIDEO_WIDTH},height={VIDEO_HEIGHT} ! videoconvert ! autovideosink",
width=VIDEO_WIDTH,
height=VIDEO_HEIGHT,
))?; ))?;
// Downcast from gst::Element to gst::Pipeline // Downcast from gst::Element to gst::Pipeline
@ -222,7 +220,7 @@ impl App {
// Handle WebSocket messages, both our own as well as WebSocket protocol messages // Handle WebSocket messages, both our own as well as WebSocket protocol messages
fn handle_websocket_message(&self, msg: &str) -> Result<(), anyhow::Error> { fn handle_websocket_message(&self, msg: &str) -> Result<(), anyhow::Error> {
if msg.starts_with("ERROR") { if msg.starts_with("ERROR") {
bail!("Got error message: {}", msg); bail!("Got error message: {msg}");
} }
if let Some(msg) = msg.strip_prefix("ROOM_PEER_MSG ") { if let Some(msg) = msg.strip_prefix("ROOM_PEER_MSG ") {
@ -236,7 +234,7 @@ impl App {
let peers = self.peers.lock().unwrap(); let peers = self.peers.lock().unwrap();
let peer = peers let peer = peers
.get(&peer_id) .get(&peer_id)
.ok_or_else(|| anyhow!("Can't find peer {}", peer_id))? .ok_or_else(|| anyhow!("Can't find peer {peer_id}"))?
.clone(); .clone();
drop(peers); drop(peers);
@ -297,11 +295,11 @@ impl App {
// Add this new peer and if requested, send the offer to it // Add this new peer and if requested, send the offer to it
fn add_peer(&self, peer: &str, offer: bool) -> Result<(), anyhow::Error> { fn add_peer(&self, peer: &str, offer: bool) -> Result<(), anyhow::Error> {
println!("Adding peer {}", peer); println!("Adding peer {peer}");
let peer_id = str::parse::<u32>(peer).context("Can't parse peer id")?; let peer_id = str::parse::<u32>(peer).context("Can't parse peer id")?;
let mut peers = self.peers.lock().unwrap(); let mut peers = self.peers.lock().unwrap();
if peers.contains_key(&peer_id) { if peers.contains_key(&peer_id) {
bail!("Peer {} already called", peer_id); bail!("Peer {peer_id} already called");
} }
let peer_bin = gst::parse_bin_from_description( let peer_bin = gst::parse_bin_from_description(
@ -492,7 +490,7 @@ impl App {
// Remove this peer // Remove this peer
fn remove_peer(&self, peer: &str) -> Result<(), anyhow::Error> { fn remove_peer(&self, peer: &str) -> Result<(), anyhow::Error> {
println!("Removing peer {}", peer); println!("Removing peer {peer}");
let peer_id = str::parse::<u32>(peer).context("Can't parse peer id")?; let peer_id = str::parse::<u32>(peer).context("Can't parse peer id")?;
let mut peers = self.peers.lock().unwrap(); let mut peers = self.peers.lock().unwrap();
if let Some(peer) = peers.remove(&peer_id) { if let Some(peer) = peers.remove(&peer_id) {
@ -559,8 +557,6 @@ impl App {
(1, 1) (1, 1)
} else if npads <= 4 { } else if npads <= 4 {
(2, 2) (2, 2)
} else if npads <= 16 {
(4, 4)
} else { } else {
// FIXME: we don't support more than 16 streams for now // FIXME: we don't support more than 16 streams for now
(4, 4) (4, 4)
@ -637,7 +633,7 @@ impl Peer {
bail!("Offer creation future got no reponse"); bail!("Offer creation future got no reponse");
} }
Err(err) => { Err(err) => {
bail!("Offer creation future got error reponse: {:?}", err); bail!("Offer creation future got error reponse: {err:?}");
} }
}; };
@ -684,7 +680,7 @@ impl Peer {
bail!("Answer creation future got no reponse"); bail!("Answer creation future got no reponse");
} }
Err(err) => { Err(err) => {
bail!("Answer creation future got error reponse: {:?}", err); bail!("Answer creation future got error reponse: {err:?}");
} }
}; };
@ -722,7 +718,7 @@ impl Peer {
// Handle incoming SDP answers from the peer // Handle incoming SDP answers from the peer
fn handle_sdp(&self, type_: &str, sdp: &str) -> Result<(), anyhow::Error> { fn handle_sdp(&self, type_: &str, sdp: &str) -> Result<(), anyhow::Error> {
if type_ == "answer" { if type_ == "answer" {
print!("Received answer:\n{}\n", sdp); print!("Received answer:\n{sdp}\n");
let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes()) let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes())
.map_err(|_| anyhow!("Failed to parse SDP answer"))?; .map_err(|_| anyhow!("Failed to parse SDP answer"))?;
@ -734,7 +730,7 @@ impl Peer {
Ok(()) Ok(())
} else if type_ == "offer" { } else if type_ == "offer" {
print!("Received offer:\n{}\n", sdp); print!("Received offer:\n{sdp}\n");
let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes()) let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes())
.map_err(|_| anyhow!("Failed to parse SDP offer"))?; .map_err(|_| anyhow!("Failed to parse SDP offer"))?;
@ -774,7 +770,7 @@ impl Peer {
Ok(()) Ok(())
} else { } else {
bail!("Sdp type is not \"answer\" but \"{}\"", type_) bail!("Sdp type is not \"answer\" but \"{type_}\"")
} }
} }
@ -820,14 +816,12 @@ impl Peer {
let media_type = s let media_type = s
.get_optional::<&str>("media") .get_optional::<&str>("media")
.expect("Invalid type") .expect("Invalid type")
.ok_or_else(|| anyhow!("no media type in caps {:?}", caps))?; .ok_or_else(|| anyhow!("no media type in caps {caps:?}"))?;
let conv = if media_type == "video" { let conv = if media_type == "video" {
gst::parse_bin_from_description( gst::parse_bin_from_description(
&format!( &format!(
"decodebin name=dbin ! queue ! videoconvert ! videoscale ! capsfilter name=src caps=video/x-raw,width={width},height={height},pixel-aspect-ratio=1/1", "decodebin name=dbin ! queue ! videoconvert ! videoscale ! capsfilter name=src caps=video/x-raw,width={VIDEO_WIDTH},height={VIDEO_HEIGHT},pixel-aspect-ratio=1/1"
width=VIDEO_WIDTH,
height=VIDEO_HEIGHT
), ),
false, false,
)? )?
@ -837,7 +831,7 @@ impl Peer {
false, false,
)? )?
} else { } else {
println!("Unknown pad {:?}, ignoring", pad); println!("Unknown pad {pad:?}, ignoring");
return Ok(()); return Ok(());
}; };
@ -855,10 +849,10 @@ impl Peer {
self.bin.add(&conv).unwrap(); self.bin.add(&conv).unwrap();
conv.sync_state_with_parent() conv.sync_state_with_parent()
.with_context(|| format!("can't start sink for stream {:?}", caps))?; .with_context(|| format!("can't start sink for stream {caps:?}"))?;
pad.link(&sinkpad) pad.link(&sinkpad)
.with_context(|| format!("can't link sink for stream {:?}", caps))?; .with_context(|| format!("can't link sink for stream {caps:?}"))?;
// And then add a new ghost pad to the peer bin that proxies the source pad we added above // And then add a new ghost pad to the peer bin that proxies the source pad we added above
if media_type == "video" { if media_type == "video" {
@ -912,7 +906,7 @@ async fn run(
WsMessage::Binary(_) => None, WsMessage::Binary(_) => None,
WsMessage::Text(text) => { WsMessage::Text(text) => {
if let Err(err) = app.handle_websocket_message(&text) { if let Err(err) = app.handle_websocket_message(&text) {
println!("Failed to parse message: {}", err); println!("Failed to parse message: {err}");
} }
None None
}, },
@ -970,7 +964,7 @@ fn check_plugins() -> Result<(), anyhow::Error> {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if !missing.is_empty() { if !missing.is_empty() {
bail!("Missing plugins: {:?}", missing); bail!("Missing plugins: {missing:?}");
} else { } else {
Ok(()) Ok(())
} }
@ -991,9 +985,8 @@ async fn async_main() -> Result<(), anyhow::Error> {
// Say HELLO to the server and see if it replies with HELLO // Say HELLO to the server and see if it replies with HELLO
let our_id = rand::thread_rng().gen_range(10..10_000); let our_id = rand::thread_rng().gen_range(10..10_000);
println!("Registering id {} with server", our_id); println!("Registering id {our_id} with server");
ws.send(WsMessage::Text(format!("HELLO {}", our_id))) ws.send(WsMessage::Text(format!("HELLO {our_id}"))).await?;
.await?;
let msg = ws let msg = ws
.next() .next()
@ -1015,14 +1008,14 @@ async fn async_main() -> Result<(), anyhow::Error> {
let peers_str = if let WsMessage::Text(text) = &msg { let peers_str = if let WsMessage::Text(text) = &msg {
if !text.starts_with("ROOM_OK") { if !text.starts_with("ROOM_OK") {
bail!("server error: {:?}", text); bail!("server error: {text:?}");
} }
println!("Joined room {}", args.room_id); println!("Joined room {}", args.room_id);
&text["ROOM_OK ".len()..] &text["ROOM_OK ".len()..]
} else { } else {
bail!("server error: {:?}", msg); bail!("server error: {msg:?}");
}; };
// Collect the ids of already existing peers // Collect the ids of already existing peers

View file

@ -242,7 +242,7 @@ impl App {
// Handle WebSocket messages, both our own as well as WebSocket protocol messages // Handle WebSocket messages, both our own as well as WebSocket protocol messages
fn handle_websocket_message(&self, msg: &str) -> Result<(), anyhow::Error> { fn handle_websocket_message(&self, msg: &str) -> Result<(), anyhow::Error> {
if msg.starts_with("ERROR") { if msg.starts_with("ERROR") {
bail!("Got error message: {}", msg); bail!("Got error message: {msg}");
} }
if msg == "OFFER_REQUEST" { if msg == "OFFER_REQUEST" {
@ -333,7 +333,7 @@ impl App {
bail!("Offer creation future got no response"); bail!("Offer creation future got no response");
} }
Err(err) => { Err(err) => {
bail!("Offer creation future got error response: {:?}", err); bail!("Offer creation future got error response: {err:?}");
} }
}; };
@ -377,7 +377,7 @@ impl App {
bail!("Answer creation future got no response"); bail!("Answer creation future got no response");
} }
Err(err) => { Err(err) => {
bail!("Answer creation future got error response: {:?}", err); bail!("Answer creation future got error response: {err:?}", err);
} }
}; };
@ -490,7 +490,7 @@ impl App {
// Handle incoming SDP answers from the peer // Handle incoming SDP answers from the peer
fn handle_sdp(&self, type_: &str, sdp: &str) -> Result<(), anyhow::Error> { fn handle_sdp(&self, type_: &str, sdp: &str) -> Result<(), anyhow::Error> {
if type_ == "answer" { if type_ == "answer" {
print!("Received answer:\n{}\n", sdp); print!("Received answer:\n{sdp}\n");
let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes()) let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes())
.map_err(|_| anyhow!("Failed to parse SDP answer"))?; .map_err(|_| anyhow!("Failed to parse SDP answer"))?;
@ -502,7 +502,7 @@ impl App {
Ok(()) Ok(())
} else if type_ == "offer" { } else if type_ == "offer" {
print!("Received offer:\n{}\n", sdp); print!("Received offer:\n{sdp}\n");
let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes()) let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes())
.map_err(|_| anyhow!("Failed to parse SDP offer"))?; .map_err(|_| anyhow!("Failed to parse SDP offer"))?;
@ -556,7 +556,7 @@ impl App {
Ok(()) Ok(())
} else { } else {
bail!("Sdp type is not \"answer\" but \"{}\"", type_) bail!("Sdp type is not \"answer\" but \"{type_}\"")
} }
} }
@ -633,17 +633,17 @@ impl App {
true, true,
)? )?
} else { } else {
println!("Unknown pad {:?}, ignoring", pad); println!("Unknown pad {pad:?}, ignoring");
return Ok(()); return Ok(());
}; };
self.pipeline.add(&sink).unwrap(); self.pipeline.add(&sink).unwrap();
sink.sync_state_with_parent() sink.sync_state_with_parent()
.with_context(|| format!("can't start sink for stream {:?}", caps))?; .with_context(|| format!("can't start sink for stream {caps:?}"))?;
let sinkpad = sink.static_pad("sink").unwrap(); let sinkpad = sink.static_pad("sink").unwrap();
pad.link(&sinkpad) pad.link(&sinkpad)
.with_context(|| format!("can't link sink for stream {:?}", caps))?; .with_context(|| format!("can't link sink for stream {caps:?}"))?;
Ok(()) Ok(())
} }
@ -741,7 +741,7 @@ fn check_plugins() -> Result<(), anyhow::Error> {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if !missing.is_empty() { if !missing.is_empty() {
bail!("Missing plugins: {:?}", missing); bail!("Missing plugins: {missing:?}");
} else { } else {
Ok(()) Ok(())
} }
@ -764,9 +764,8 @@ async fn async_main() -> Result<(), anyhow::Error> {
let our_id = args let our_id = args
.our_id .our_id
.unwrap_or_else(|| rand::thread_rng().gen_range(10..10_000)); .unwrap_or_else(|| rand::thread_rng().gen_range(10..10_000));
println!("Registering id {} with server", our_id); println!("Registering id {our_id} with server");
ws.send(WsMessage::Text(format!("HELLO {}", our_id))) ws.send(WsMessage::Text(format!("HELLO {our_id}"))).await?;
.await?;
let msg = ws let msg = ws
.next() .next()
@ -778,10 +777,10 @@ async fn async_main() -> Result<(), anyhow::Error> {
} }
if let Some(peer_id) = args.peer_id { if let Some(peer_id) = args.peer_id {
println!("Setting up call with peer id {}", peer_id); println!("Setting up call with peer id {peer_id}");
// Join the given session // Join the given session
ws.send(WsMessage::Text(format!("SESSION {}", peer_id))) ws.send(WsMessage::Text(format!("SESSION {peer_id}")))
.await?; .await?;
let msg = ws let msg = ws
@ -790,7 +789,7 @@ async fn async_main() -> Result<(), anyhow::Error> {
.ok_or_else(|| anyhow!("didn't receive anything"))??; .ok_or_else(|| anyhow!("didn't receive anything"))??;
if msg != WsMessage::Text("SESSION_OK".into()) { if msg != WsMessage::Text("SESSION_OK".into()) {
bail!("server error: {:?}", msg); bail!("server error: {msg:?}");
} }
// If we expect the peer to create the offer request it now // If we expect the peer to create the offer request it now