Skip to content

Commit

Permalink
Use structs for json handling
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmcd committed Jun 12, 2018
1 parent 38499be commit 1a4cb92
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
2 changes: 2 additions & 0 deletions sendrecv/gst-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ gstreamer-sdp-sys = { git = "https://github.com/sdroege/gstreamer-sys" }
gstreamer-webrtc = { git = "https://github.com/sdroege/gstreamer-rs", rev="f4d57a6" }
rand = "0.5"
serde_json = "1.0.19"
serde_derive = "1.0.66"
serde = "1.0.66"
ws = "0.7.6"
89 changes: 53 additions & 36 deletions sendrecv/gst-rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
extern crate clap;
extern crate failure;
extern crate glib;
extern crate gstreamer as gst;
extern crate gstreamer_sdp as gst_sdp;
extern crate gstreamer_webrtc as gst_webrtc;
extern crate rand;
extern crate ws;
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;
extern crate failure;
extern crate ws;

use failure::Error;
use gst::prelude::*;
Expand Down Expand Up @@ -392,6 +395,21 @@ impl WsClient {
}
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
enum JsonMsg {
Ice {
candidate: String,
#[serde(rename = "sdpMLineIndex")]
sdp_mline_index: u32,
},
Sdp {
#[serde(rename = "type")]
type_: String,
sdp: String,
},
}

impl ws::Handler for WsClient {
fn on_open(&mut self, _: ws::Handshake) -> ws::Result<()> {
self.update_state(AppState::ServerConnected);
Expand Down Expand Up @@ -452,41 +470,40 @@ impl ws::Handler for WsClient {
// TODO: signal & cleanup
}

let json_msg: serde_json::Value = serde_json::from_str(&msg_text).unwrap();
if json_msg.get("sdp").is_some() {
assert_eq!(
self.app_control.lock().unwrap().app_state,
AppState::PeerCallNegotiating
);

if !json_msg["sdp"].get("type").is_some() {
println!("ERROR: received SDP without 'type'");
return Ok(());
let json_msg: JsonMsg = serde_json::from_str(&msg_text).unwrap();
match json_msg {
JsonMsg::Sdp { type_, sdp } => {
assert_eq!(
self.app_control.lock().unwrap().app_state,
AppState::PeerCallNegotiating
);

assert_eq!(type_, "answer");
print!("Received answer:\n{}\n", sdp);

let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes()).unwrap();
let answer = gst_webrtc::WebRTCSessionDescription::new(
gst_webrtc::WebRTCSDPType::Answer,
ret,
);
let promise = gst::Promise::new();
self.webrtc
.as_ref()
.unwrap()
.emit("set-remote-description", &[&answer, &promise])
.unwrap();
self.update_state(AppState::PeerCallStarted);
}
JsonMsg::Ice {
sdp_mline_index,
candidate,
} => {
self.webrtc
.as_ref()
.unwrap()
.emit("add-ice-candidate", &[&sdp_mline_index, &candidate])
.unwrap();
}
let sdptype = &json_msg["sdp"]["type"];
assert_eq!(sdptype, "answer");
let text = &json_msg["sdp"]["sdp"];
print!("Received answer:\n{}\n", text.as_str().unwrap());

let ret = gst_sdp::SDPMessage::parse_buffer(text.as_str().unwrap().as_bytes()).unwrap();
let answer =
gst_webrtc::WebRTCSessionDescription::new(gst_webrtc::WebRTCSDPType::Answer, ret);
let promise = gst::Promise::new();
self.webrtc
.as_ref()
.unwrap()
.emit("set-remote-description", &[&answer, &promise])
.unwrap();
self.update_state(AppState::PeerCallStarted);
}
if json_msg.get("ice").is_some() {
let candidate = json_msg["ice"]["candidate"].as_str().unwrap();
let sdpmlineindex = json_msg["ice"]["sdpMLineIndex"].as_u64().unwrap() as u32;
self.webrtc
.as_ref()
.unwrap()
.emit("add-ice-candidate", &[&sdpmlineindex, &candidate])
.unwrap();
}

Ok(())
Expand Down

0 comments on commit 1a4cb92

Please sign in to comment.