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

Commit b392f4b

Browse files
committed
Cleanup and more idiomatic error handling for missing elements
1 parent 03bf984 commit b392f4b

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

sendrecv/gst-rust/src/main.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ extern crate lazy_static;
1717

1818
use failure::Error;
1919
use gst::prelude::*;
20-
use gst::{BinExt, ElementExt};
2120
use rand::Rng;
2221
use std::sync::{mpsc, Arc, Mutex};
2322
use std::thread;
@@ -49,7 +48,6 @@ lazy_static! {
4948

5049
#[derive(PartialEq, PartialOrd, Eq, Debug)]
5150
enum AppState {
52-
// AppStateUnknown = 0,
5351
AppStateErr = 1,
5452
ServerConnected,
5553
ServerRegistering = 2000,
@@ -116,7 +114,11 @@ struct PadLinkError {
116114
right: gst::PadLinkReturn,
117115
}
118116

119-
fn check_plugins() -> bool {
117+
#[derive(Debug, Fail)]
118+
#[fail(display = "Missing elements {:?}", _0)]
119+
struct MissingElements(Vec<&'static str>);
120+
121+
fn check_plugins() -> Result<(), Error> {
120122
let needed = vec![
121123
"opus",
122124
"vpx",
@@ -129,15 +131,18 @@ fn check_plugins() -> bool {
129131
"audiotestsrc",
130132
];
131133
let registry = gst::Registry::get();
132-
let mut ret = true;
134+
let mut missing: Vec<&'static str> = Vec::new();
133135
for plugin_name in needed {
134136
let plugin = registry.find_plugin(&plugin_name.to_string());
135137
if plugin.is_none() {
136-
println!("Required gstreamer plugin '{}' not found", plugin_name);
137-
ret = false;
138+
missing.push(plugin_name)
138139
}
139140
}
140-
ret
141+
if missing.len() > 0 {
142+
Err(MissingElements(missing))?
143+
} else {
144+
Ok(())
145+
}
141146
}
142147

143148
fn send_sdp_offer(app_control: &AppControl, offer: gst_webrtc::WebRTCSessionDescription) {
@@ -425,7 +430,6 @@ impl AppControl {
425430
.unwrap();
426431
let app_control_clone = self.clone();
427432
webrtc.connect("on-negotiation-needed", false, move |values| {
428-
// handle
429433
on_negotiation_needed(&app_control_clone, values).unwrap();
430434
None
431435
})?;
@@ -557,7 +561,6 @@ impl AppControl {
557561
}
558562

559563
fn close_and_quit(&self, err: Error) {
560-
// self.update_state(AppState::ServerClosed);
561564
let app_control = self.0.lock().unwrap();
562565
println!("{}\nquitting", err);
563566
app_control
@@ -646,16 +649,13 @@ fn receive_loop(
646649
let _ = send_msg_tx.send(OwnedMessage::Close(None));
647650
return;
648651
}
649-
OwnedMessage::Ping(data) => {
650-
match send_msg_tx.send(OwnedMessage::Pong(data)) {
651-
// Send a pong in response
652-
Ok(()) => (),
653-
Err(e) => {
654-
println!("Receive Loop error: {:?}", e);
655-
return;
656-
}
652+
OwnedMessage::Ping(data) => match send_msg_tx.send(OwnedMessage::Pong(data)) {
653+
Ok(()) => (),
654+
Err(e) => {
655+
println!("Receive Loop error: {:?}", e);
656+
return;
657657
}
658-
}
658+
},
659659
OwnedMessage::Text(msg) => {
660660
let mbuilder = gst::Message::new_application(gst::Structure::new(
661661
"ws-message",
@@ -674,8 +674,12 @@ fn receive_loop(
674674

675675
fn main() {
676676
gst::init().unwrap();
677-
if !check_plugins() {
678-
return;
677+
match check_plugins() {
678+
Err(err) => {
679+
println!("{:?}", err);
680+
return;
681+
}
682+
_ => {}
679683
}
680684

681685
let (server, peer_id) = parse_args();

0 commit comments

Comments
 (0)