Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit 2d45dd9

Browse files
committed
Fix various clippy warnings in the Rust sendrecv demo
1 parent 0a7aaf6 commit 2d45dd9

File tree

1 file changed

+52
-54
lines changed

1 file changed

+52
-54
lines changed

sendrecv/gst-rust/src/main.rs

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::sync::{mpsc, Arc, Mutex};
2222
use std::thread;
2323
use websocket::message::OwnedMessage;
2424

25-
const STUN_SERVER: &'static str = "stun://stun.l.google.com:19302 ";
25+
const STUN_SERVER: &str = "stun://stun.l.google.com:19302 ";
2626
lazy_static! {
2727
static ref RTP_CAPS_OPUS: gst::GstRc<gst::CapsRef> = {
2828
gst::Caps::new_simple(
@@ -46,9 +46,9 @@ lazy_static! {
4646
};
4747
}
4848

49-
#[derive(PartialEq, PartialOrd, Eq, Debug, Clone, Ord)]
49+
#[derive(PartialEq, PartialOrd, Eq, Debug, Copy, Clone, Ord)]
5050
enum AppState {
51-
AppStateErr = 1,
51+
Error = 1,
5252
ServerConnected,
5353
ServerRegistering = 2000,
5454
ServerRegisteringError,
@@ -76,7 +76,7 @@ enum JsonMsg {
7676
},
7777
}
7878

79-
#[derive(Debug)]
79+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8080
enum MediaType {
8181
Audio,
8282
Video,
@@ -138,14 +138,14 @@ fn check_plugins() -> Result<(), Error> {
138138
missing.push(plugin_name)
139139
}
140140
}
141-
if missing.len() > 0 {
141+
if !missing.is_empty() {
142142
Err(MissingElements(missing))?
143143
} else {
144144
Ok(())
145145
}
146146
}
147147

148-
fn send_sdp_offer(app_control: &AppControl, offer: gst_webrtc::WebRTCSessionDescription) {
148+
fn send_sdp_offer(app_control: &AppControl, offer: &gst_webrtc::WebRTCSessionDescription) {
149149
if app_control.app_state_cmp(
150150
AppState::PeerCallNegotiating,
151151
"Can't send offer, not in call",
@@ -162,7 +162,7 @@ fn send_sdp_offer(app_control: &AppControl, offer: gst_webrtc::WebRTCSessionDesc
162162

163163
fn on_offer_created(
164164
app_control: &AppControl,
165-
webrtc: gst::Element,
165+
webrtc: &gst::Element,
166166
promise: &gst::Promise,
167167
) -> Result<(), Error> {
168168
if !app_control.app_state_eq(
@@ -180,7 +180,7 @@ fn on_offer_created(
180180
.expect("Invalid argument");
181181
webrtc.emit("set-local-description", &[&offer, &None::<gst::Promise>])?;
182182

183-
send_sdp_offer(&app_control, offer);
183+
send_sdp_offer(&app_control, &offer);
184184
Ok(())
185185
}
186186

@@ -190,7 +190,7 @@ fn on_negotiation_needed(app_control: &AppControl, values: &[glib::Value]) -> Re
190190
let webrtc_clone = webrtc.clone();
191191
let app_control_clone = app_control.clone();
192192
let promise = gst::Promise::new_with_change_func(move |promise| {
193-
on_offer_created(&app_control_clone, webrtc, promise).unwrap();
193+
on_offer_created(&app_control_clone, &webrtc, promise).unwrap();
194194
});
195195
webrtc_clone.emit("create-offer", &[&None::<gst::Structure>, &promise])?;
196196
Ok(())
@@ -252,20 +252,21 @@ fn on_incoming_decodebin_stream(
252252

253253
let caps = pad.get_current_caps().unwrap();
254254
let name = caps.get_structure(0).unwrap().get_name();
255-
match if name.starts_with("video") {
255+
256+
let handled = if name.starts_with("video") {
256257
handle_media_stream(&pad, &pipe, MediaType::Video)
257258
} else if name.starts_with("audio") {
258259
handle_media_stream(&pad, &pipe, MediaType::Audio)
259260
} else {
260261
println!("Unknown pad {:?}, ignoring", pad);
261262
Ok(())
262-
} {
263-
Ok(()) => return None,
264-
Err(err) => {
265-
app_control.send_bus_error(format!("Error adding pad with caps {} {:?}", name, err));
266-
return None;
267-
}
268263
};
264+
265+
if let Err(err) = handled {
266+
app_control.send_bus_error(&format!("Error adding pad with caps {} {:?}", name, err));
267+
}
268+
269+
None
269270
}
270271

271272
fn on_incoming_stream(
@@ -302,7 +303,7 @@ fn send_ice_candidate_message(app_control: &AppControl, values: &[glib::Value])
302303
let mlineindex = values[1].get::<u32>().expect("Invalid argument");
303304
let candidate = values[2].get::<String>().expect("Invalid argument");
304305
let message = serde_json::to_string(&JsonMsg::Ice {
305-
candidate: candidate,
306+
candidate,
306307
sdp_mline_index: mlineindex,
307308
}).unwrap();
308309
app_control.send_text_msg(message.to_string());
@@ -374,7 +375,7 @@ fn add_audio_source(pipeline: &gst::Pipeline, webrtcbin: &gst::Element) -> Resul
374375
impl AppControl {
375376
fn app_state_eq(&self, state: AppState, error_msg: &'static str) -> bool {
376377
if { self.0.lock().unwrap().app_state != state } {
377-
self.send_bus_error(String::from(error_msg));
378+
self.send_bus_error(error_msg);
378379
false
379380
} else {
380381
true
@@ -384,14 +385,14 @@ impl AppControl {
384385
fn app_state_cmp(&self, state: AppState, error_msg: &'static str) -> Ordering {
385386
match { self.0.lock().unwrap().app_state.cmp(&state) } {
386387
Ordering::Less => {
387-
self.send_bus_error(String::from(error_msg));
388+
self.send_bus_error(error_msg);
388389
Ordering::Less
389390
}
390391
_foo => _foo,
391392
}
392393
}
393394

394-
fn send_bus_error(&self, body: String) {
395+
fn send_bus_error(&self, body: &str) {
395396
let mbuilder =
396397
gst::Message::new_application(gst::Structure::new("error", &[("body", &body)]));
397398
let _ = self.0.lock().unwrap().bus.post(&mbuilder.build());
@@ -422,7 +423,8 @@ impl AppControl {
422423

423424
fn start_pipeline(&self) -> Result<(), Error> {
424425
let pipe = self.construct_pipeline()?;
425-
let webrtc = pipe.clone()
426+
let webrtc = pipe
427+
.clone()
426428
.dynamic_cast::<gst::Bin>()
427429
.unwrap()
428430
.get_by_name("sendrecv")
@@ -495,20 +497,21 @@ impl AppControl {
495497
AppState::ServerRegisteringError => AppState::ServerRegisteringError,
496498
AppState::PeerConnectionError => AppState::PeerConnectionError,
497499
AppState::PeerCallError => AppState::PeerCallError,
498-
AppState::AppStateErr => AppState::AppStateErr,
499-
AppState::ServerConnected => AppState::AppStateErr,
500-
AppState::ServerRegistered => AppState::AppStateErr,
501-
AppState::PeerCallStarted => AppState::AppStateErr,
500+
AppState::Error => AppState::Error,
501+
AppState::ServerConnected => AppState::Error,
502+
AppState::ServerRegistered => AppState::Error,
503+
AppState::PeerCallStarted => AppState::Error,
502504
};
503-
return Err(WsError(error))?;
505+
506+
Err(WsError(error))?
504507
}
505-
fn handle_sdp(&self, type_: String, sdp: String) {
508+
fn handle_sdp(&self, type_: &str, sdp: &str) {
506509
if !self.app_state_eq(AppState::PeerCallNegotiating, "Not ready to handle sdp") {
507510
return;
508511
}
509512

510513
if type_ != "answer" {
511-
self.send_bus_error(String::from("Sdp type is not \"anser\""));
514+
self.send_bus_error("Sdp type is not \"answer\"");
512515
return;
513516
}
514517

@@ -527,7 +530,7 @@ impl AppControl {
527530
.unwrap();
528531
app_control.app_state = AppState::PeerCallStarted;
529532
}
530-
fn handle_ice(&self, sdp_mline_index: u32, candidate: String) {
533+
fn handle_ice(&self, sdp_mline_index: u32, candidate: &str) {
531534
let app_control = self.0.lock().unwrap();
532535
app_control
533536
.webrtc
@@ -536,7 +539,7 @@ impl AppControl {
536539
.emit("add-ice-candidate", &[&sdp_mline_index, &candidate])
537540
.unwrap();
538541
}
539-
fn on_message(&mut self, msg: String) -> Result<(), Error> {
542+
fn on_message(&mut self, msg: &str) -> Result<(), Error> {
540543
if msg == "HELLO" {
541544
return self.handle_hello();
542545
}
@@ -548,18 +551,18 @@ impl AppControl {
548551
println!("Got error message! {}", msg);
549552
return self.handle_error();
550553
}
551-
let json_msg: JsonMsg = serde_json::from_str(&msg)?;
554+
let json_msg: JsonMsg = serde_json::from_str(msg)?;
552555
match json_msg {
553-
JsonMsg::Sdp { type_, sdp } => self.handle_sdp(type_, sdp),
556+
JsonMsg::Sdp { type_, sdp } => self.handle_sdp(&type_, &sdp),
554557
JsonMsg::Ice {
555558
sdp_mline_index,
556559
candidate,
557-
} => self.handle_ice(sdp_mline_index, candidate),
560+
} => self.handle_ice(sdp_mline_index, &candidate),
558561
};
559562
Ok(())
560563
}
561564

562-
fn close_and_quit(&self, err: Error) {
565+
fn close_and_quit(&self, err: &Error) {
563566
let app_control = self.0.lock().unwrap();
564567
println!("{}\nquitting", err);
565568
app_control
@@ -614,13 +617,12 @@ fn send_loop(
614617
return;
615618
}
616619
};
617-
match msg {
618-
OwnedMessage::Close(_) => {
619-
let _ = sender.send_message(&msg);
620-
return;
621-
}
622-
_ => (),
620+
621+
if let OwnedMessage::Close(_) = msg {
622+
let _ = sender.send_message(&msg);
623+
return;
623624
}
625+
624626
match sender.send_message(&msg) {
625627
Ok(()) => (),
626628
Err(err) => println!("Error sending {:?}", err),
@@ -683,7 +685,7 @@ fn handle_application_msg(
683685
let msg = struc.get_value("body").unwrap();
684686
app_control.on_message(msg.get().unwrap())
685687
}
686-
"ws-error" => Err(WsError(app_control.0.lock().unwrap().app_state.clone()))?,
688+
"ws-error" => Err(WsError(app_control.0.lock().unwrap().app_state))?,
687689
"error" => {
688690
let msg: String = struc.get_value("body").unwrap().get().unwrap();
689691
Err(BusError(msg))?
@@ -697,12 +699,9 @@ fn handle_application_msg(
697699

698700
fn main() {
699701
gst::init().unwrap();
700-
match check_plugins() {
701-
Err(err) => {
702-
println!("{:?}", err);
703-
return;
704-
}
705-
_ => {}
702+
if let Err(err) = check_plugins() {
703+
println!("{:?}", err);
704+
return;
706705
}
707706

708707
let (server, peer_id) = parse_args();
@@ -733,8 +732,8 @@ fn main() {
733732

734733
let app_control = AppControl(Arc::new(Mutex::new(AppControlInner {
735734
webrtc: None,
736-
pipeline: pipeline,
737-
send_msg_tx: send_msg_tx,
735+
pipeline,
736+
send_msg_tx,
738737
bus: bus.clone(),
739738
main_loop: main_loop.clone(),
740739
peer_id: peer_id.to_string(),
@@ -746,16 +745,15 @@ fn main() {
746745
let mut app_control = app_control.clone();
747746
use gst::message::MessageView;
748747
match msg.view() {
749-
MessageView::Error(err) => app_control.close_and_quit(Error::from(err.get_error())),
748+
MessageView::Error(err) => app_control.close_and_quit(&Error::from(err.get_error())),
750749
MessageView::Warning(warning) => {
751750
println!("Warning: \"{}\"", warning.get_debug().unwrap());
752751
}
753752
MessageView::Application(a) => {
754753
let struc = a.get_structure().unwrap();
755-
match handle_application_msg(&mut app_control, struc) {
756-
Err(err) => app_control.close_and_quit(err),
757-
_ => {}
758-
};
754+
if let Err(err) = handle_application_msg(&mut app_control, struc) {
755+
app_control.close_and_quit(&err)
756+
}
759757
}
760758
_ => {}
761759
};

0 commit comments

Comments
 (0)