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

Commit d4165e4

Browse files
committed
Add peer-id and server flags, clean up some mutex use, general cleanup
1 parent e778ca1 commit d4165e4

File tree

6 files changed

+68
-47
lines changed

6 files changed

+68
-47
lines changed

docker-compose.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
version: '3'
22

33
services:
4+
# uncomment the sendrecv you would like to use
5+
#
46
# sendrecv-gst:
57
# build: ./sendrecv/gst
68
sendrecv-gst-rust:
79
build: ./sendrecv/gst-rust
8-
command: sleep 10000
9-
volumes:
10-
- ./sendrecv/gst-rust/src:/opt/src
1110
sendrecv-js:
1211
build: ./sendrecv/js
1312
ports:

sendrecv/gst-rust/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

sendrecv/gst-rust/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ version = "0.1.0"
44
authors = ["maxmcd <max.t.mcdonnell@gmail.com>"]
55

66
[dependencies]
7+
clap = "2.31.2"
8+
glib = { git = "https://github.com/gtk-rs/glib" }
79
gstreamer = { git = "https://github.com/sdroege/gstreamer-rs", features = ["v1_14"] }
810
gstreamer-rtsp-server = { git = "https://github.com/sdroege/gstreamer-rs" }
9-
gstreamer-webrtc = { git = "https://github.com/sdroege/gstreamer-rs" }
1011
gstreamer-sdp = { git = "https://github.com/sdroege/gstreamer-rs" }
1112
gstreamer-sdp-sys = { git = "https://github.com/sdroege/gstreamer-sys" }
12-
13-
glib = { git = "https://github.com/gtk-rs/glib" }
13+
gstreamer-webrtc = { git = "https://github.com/sdroege/gstreamer-rs" }
14+
rand = "0.5"
1415
serde_json = "1.0.19"
1516
ws = "0.7.6"
16-
rand = "0.5"

sendrecv/gst-rust/Dockerfile

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ FROM maxmcd/gstreamer:1.14-buster
22

33
RUN apt-get install -y curl
44
RUN wget -O rustup.sh https://sh.rustup.rs && sh ./rustup.sh -y
5-
65
ENV PATH=$PATH:/root/.cargo/bin/
76

87
WORKDIR /opt/
9-
RUN mkdir -p /opt/src && echo 'fn main() {println!("");}' > /opt/src/main.rs
10-
COPY Cargo.* /opt/
11-
RUN cargo install
12-
RUN cargo build
138
COPY . /opt/
9+
RUN cargo build
1410

15-
CMD cargo run
16-
11+
CMD echo "Waiting a few seconds for you to open the browser at localhost:8080" \
12+
&& sleep 10 \
13+
&& /opt/target/debug/gst-rust --peer-id=1 --server=ws://signalling:8443

sendrecv/gst-rust/src/main.rs

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
extern crate clap;
12
extern crate glib;
23
extern crate gstreamer as gst;
34
extern crate gstreamer_sdp;
@@ -86,20 +87,26 @@ fn check_plugins() -> bool {
8687
}
8788

8889
fn setup_call(app_control: &Arc<Mutex<AppControl>>) -> AppState {
89-
app_control.lock().unwrap().app_state = AppState::PeerConnecting;
90-
println!("Setting up signalling server call with 1");
91-
app_control.lock().unwrap().out.send("SESSION 1").unwrap();
90+
let mut app_control = app_control.lock().unwrap();
91+
app_control.app_state = AppState::PeerConnecting;
92+
println!(
93+
"Setting up signalling server call with {}",
94+
app_control.peer_id
95+
);
96+
app_control
97+
.ws_sender
98+
.send(format!("SESSION {}", app_control.peer_id))
99+
.unwrap();
92100
return AppState::PeerConnecting;
93101
}
94102

95103
fn register_with_server(app_control: &Arc<Mutex<AppControl>>) -> AppState {
96-
app_control.lock().unwrap().app_state = AppState::ServerRegistering;
104+
let mut app_control = app_control.lock().unwrap();
105+
app_control.app_state = AppState::ServerRegistering;
97106
let our_id = rand::thread_rng().gen_range(10, 10_000);
98107
println!("Registering id {} with server", our_id);
99108
app_control
100-
.lock()
101-
.unwrap()
102-
.out
109+
.ws_sender
103110
.send(format!("HELLO {}", our_id))
104111
.unwrap();
105112
return AppState::ServerRegistering;
@@ -117,7 +124,8 @@ fn send_sdp_offer(
117124
app_control: &Arc<Mutex<AppControl>>,
118125
offer: gstreamer_webrtc::WebRTCSessionDescription,
119126
) {
120-
if app_control.lock().unwrap().app_state < AppState::PeerCallNegotiating {
127+
let app_control = app_control.lock().unwrap();
128+
if app_control.app_state < AppState::PeerCallNegotiating {
121129
// TODO signal and cleanup
122130
panic!("Can't send offer, not in call");
123131
};
@@ -127,12 +135,7 @@ fn send_sdp_offer(
127135
"sdp": sdp_message_as_text(offer).unwrap(),
128136
}
129137
});
130-
app_control
131-
.lock()
132-
.unwrap()
133-
.out
134-
.send(message.to_string())
135-
.unwrap();
138+
app_control.ws_sender.send(message.to_string()).unwrap();
136139
}
137140

138141
fn on_offer_created(
@@ -260,7 +263,8 @@ fn send_ice_candidate_message(
260263
app_control: &Arc<Mutex<AppControl>>,
261264
values: &[glib::Value],
262265
) -> Option<glib::Value> {
263-
if app_control.lock().unwrap().app_state < AppState::PeerCallNegotiating {
266+
let app_control = app_control.lock().unwrap();
267+
if app_control.app_state < AppState::PeerCallNegotiating {
264268
panic!("Can't send ICE, not in call");
265269
}
266270

@@ -273,12 +277,7 @@ fn send_ice_candidate_message(
273277
"sdpMLineIndex": mlineindex,
274278
}
275279
});
276-
app_control
277-
.lock()
278-
.unwrap()
279-
.out
280-
.send(message.to_string())
281-
.unwrap();
280+
app_control.ws_sender.send(message.to_string()).unwrap();
282281
None
283282
}
284283

@@ -326,7 +325,8 @@ struct WsClient {
326325

327326
struct AppControl {
328327
app_state: AppState,
329-
out: ws::Sender,
328+
ws_sender: ws::Sender,
329+
peer_id: String,
330330
}
331331

332332
impl WsClient {
@@ -338,7 +338,6 @@ impl WsClient {
338338
impl ws::Handler for WsClient {
339339
fn on_open(&mut self, _: ws::Handshake) -> ws::Result<()> {
340340
self.update_state(AppState::ServerConnected);
341-
// TODO: replace with random
342341
self.update_state(register_with_server(&self.app_control.clone()));
343342
Ok(())
344343
}
@@ -348,7 +347,6 @@ impl ws::Handler for WsClient {
348347
let msg_text = msg.into_text().unwrap();
349348
if msg_text == "HELLO" {
350349
if self.app_control.lock().unwrap().app_state != AppState::ServerRegistering {
351-
// TODO: signal and cleanup
352350
panic!("ERROR: Received HELLO when not registering");
353351
}
354352
self.update_state(AppState::ServerRegistered);
@@ -357,7 +355,6 @@ impl ws::Handler for WsClient {
357355
}
358356
if msg_text == "SESSION_OK" {
359357
if self.app_control.lock().unwrap().app_state != AppState::PeerConnecting {
360-
// TODO: signal and cleanup
361358
panic!("ERROR: Received SESSION_OK when not calling");
362359
}
363360
self.update_state(AppState::PeerConnected);
@@ -391,7 +388,7 @@ impl ws::Handler for WsClient {
391388
self.app_control
392389
.lock()
393390
.unwrap()
394-
.out
391+
.ws_sender
395392
.close(ws::CloseCode::Normal)
396393
.unwrap();
397394
panic!("Got websocket error {:?}", error);
@@ -440,25 +437,53 @@ impl ws::Handler for WsClient {
440437

441438
Ok(())
442439
}
440+
441+
fn on_close(&mut self, _code: ws::CloseCode, _reason: &str) {
442+
self.app_control.lock().unwrap().app_state = AppState::ServerClosed;
443+
}
443444
}
444445

445-
fn connect_to_websocket_server_async() {
446-
ws::connect("ws://signalling:8443", |out| WsClient {
446+
fn connect_to_websocket_server_async(peer_id: &str, server: &str) {
447+
println!("Connecting to server {}", server);
448+
ws::connect(server, |ws_sender| WsClient {
447449
webrtc: None,
448450
app_control: Arc::new(Mutex::new(AppControl {
449-
out: out,
451+
ws_sender: ws_sender,
452+
peer_id: peer_id.to_string(),
450453
app_state: AppState::ServerConnecting,
451454
})),
452455
}).unwrap();
453456
}
454457

455458
fn main() {
459+
let matches = clap::App::new("Sendrcv rust")
460+
.arg(
461+
clap::Arg::with_name("peer-id")
462+
.help("String ID of the peer to connect to")
463+
.long("peer-id")
464+
.required(true)
465+
.takes_value(true),
466+
)
467+
.arg(
468+
clap::Arg::with_name("server")
469+
.help("Signalling server to connect to")
470+
.long("server")
471+
.required(false)
472+
.takes_value(true),
473+
)
474+
.get_matches();
475+
456476
gst::init().unwrap();
457477

458478
if !check_plugins() {
459479
return;
460480
}
461481
let main_loop = glib::MainLoop::new(None, false);
462-
connect_to_websocket_server_async();
482+
connect_to_websocket_server_async(
483+
matches.value_of("peer-id").unwrap(),
484+
matches
485+
.value_of("server")
486+
.unwrap_or("wss://webrtc.nirbheek.in:8443"),
487+
);
463488
main_loop.run();
464489
}

sendrecv/gst/Dockerfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ RUN sed -i 's/strict_ssl = TRUE/strict_ssl = FALSE/g' \
1414

1515
RUN make
1616

17-
# ENV GST_DEBUG=5
18-
CMD echo "Waiting a few seconds for you to open the browser" \
19-
&& sleep 7 \
17+
CMD echo "Waiting a few seconds for you to open the browser at localhost:8080" \
18+
&& sleep 10 \
2019
&& ./webrtc-sendrecv --peer-id=1
2120

0 commit comments

Comments
 (0)