This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathmain.rs
878 lines (734 loc) · 29.4 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
extern crate clap;
#[macro_use]
extern crate failure;
extern crate glib;
#[macro_use]
extern crate gstreamer as gst;
extern crate gstreamer_sdp as gst_sdp;
extern crate gstreamer_webrtc as gst_webrtc;
extern crate rand;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate tokio;
extern crate websocket;
#[macro_use]
extern crate lazy_static;
use failure::Error;
use gst::prelude::*;
use rand::Rng;
use std::sync::{Arc, Mutex, Weak};
use tokio::prelude::*;
use tokio::sync::mpsc;
use websocket::message::OwnedMessage;
const STUN_SERVER: &str = "stun://stun.l.google.com:19302";
lazy_static! {
static ref RTP_CAPS_OPUS: gst::Caps = {
gst::Caps::new_simple(
"application/x-rtp",
&[
("media", &"audio"),
("encoding-name", &"OPUS"),
("payload", &(97i32)),
],
)
};
static ref RTP_CAPS_VP8: gst::Caps = {
gst::Caps::new_simple(
"application/x-rtp",
&[
("media", &"video"),
("encoding-name", &"VP8"),
("payload", &(96i32)),
],
)
};
}
#[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,
},
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum MediaType {
Audio,
Video,
}
// Strong reference to our application state
#[derive(Debug, Clone)]
struct App(Arc<AppInner>);
// Weak reference to our application state
#[derive(Debug, Clone)]
struct AppWeak(Weak<AppInner>);
// Actual application state
#[derive(Debug)]
struct AppInner {
// None if we wait for a peer to appear
peer_id: Option<String>,
pipeline: gst::Pipeline,
webrtcbin: gst::Element,
send_msg_tx: Mutex<mpsc::UnboundedSender<OwnedMessage>>,
rtx: bool,
}
// Various error types for the different errors that can happen here
#[derive(Debug, Fail)]
#[fail(display = "WebSocket error: {:?}", _0)]
struct WebSocketError(websocket::WebSocketError);
#[derive(Debug, Fail)]
#[fail(display = "GStreamer error: {:?}", _0)]
struct GStreamerError(String);
#[derive(Debug, Fail)]
#[fail(display = "Peer error: {:?}", _0)]
struct PeerError(String);
#[derive(Debug, Fail)]
#[fail(display = "Missing elements {:?}", _0)]
struct MissingElements(Vec<&'static str>);
// upgrade weak reference or return
#[macro_export]
macro_rules! upgrade_weak {
($x:ident, $r:expr) => {{
match $x.upgrade() {
Some(o) => o,
None => return $r,
}
}};
($x:ident) => {
upgrade_weak!($x, ())
};
}
impl AppWeak {
// Try upgrading a weak reference to a strong one
fn upgrade(&self) -> Option<App> {
self.0.upgrade().map(App)
}
}
impl Drop for AppInner {
fn drop(&mut self) {
// When dropping we need to ensure that the final pipeline state is actually Null
self.pipeline.set_state(gst::State::Null).unwrap();
}
}
impl App {
// Downgrade the strong reference to a weak reference
fn downgrade(&self) -> AppWeak {
AppWeak(Arc::downgrade(&self.0))
}
// Post an error message on the bus to asynchronously handle anything that goes
// wrong on GStreamer threads
fn post_error(&self, msg: &str) {
gst_element_error!(self.0.pipeline, gst::LibraryError::Failed, (msg));
}
// Send a plain text message asynchronously over the WebSocket connection. This can
// be called from any thread at any time and would send the actual message from the
// IO threads of the runtime
fn send_text_msg(&self, msg: String) -> Result<(), Error> {
self.0
.send_msg_tx
.lock()
.unwrap()
.try_send(OwnedMessage::Text(msg))
.map_err(|_| {
WebSocketError(websocket::WebSocketError::IoError(std::io::Error::new(
std::io::ErrorKind::BrokenPipe,
"Connection Closed",
)))
.into()
})
}
// Send our SDP offer via the WebSocket connection to the peer as JSON message
fn send_sdp_offer(&self, offer: &gst_webrtc::WebRTCSessionDescription) -> Result<(), Error> {
let message = serde_json::to_string(&JsonMsg::Sdp {
type_: "offer".to_string(),
sdp: offer.get_sdp().as_text().unwrap(),
})
.unwrap();
println!("Sending SDP offer to peer: {}", message);
self.send_text_msg(message)
}
// Once webrtcbin has create the offer SDP for us, handle it by sending it to the peer via the
// WebSocket connection
fn on_offer_created(&self, promise: &gst::Promise) -> Result<(), Error> {
let reply = match promise.wait() {
gst::PromiseResult::Replied => promise.get_reply().unwrap(),
err => {
return Err(GStreamerError(format!(
"Offer creation future got no reponse: {:?}",
err
))
.into());
}
};
let offer = reply
.get_value("offer")
.unwrap()
.get::<gst_webrtc::WebRTCSessionDescription>()
.expect("Invalid argument");
self.0
.webrtcbin
.emit("set-local-description", &[&offer, &None::<gst::Promise>])
.unwrap();
self.send_sdp_offer(&offer)
}
// Send our SDP answer via the WebSocket connection to the peer as JSON message
fn send_sdp_answer(&self, offer: &gst_webrtc::WebRTCSessionDescription) -> Result<(), Error> {
let message = serde_json::to_string(&JsonMsg::Sdp {
type_: "answer".to_string(),
sdp: offer.get_sdp().as_text().unwrap(),
})
.unwrap();
println!("Sending SDP answer to peer: {}", message);
self.send_text_msg(message)
}
// Once webrtcbin has create the answer SDP for us, handle it by sending it to the peer via the
// WebSocket connection
fn on_answer_created(&self, promise: &gst::Promise) -> Result<(), Error> {
let reply = match promise.wait() {
gst::PromiseResult::Replied => promise.get_reply().unwrap(),
err => {
return Err(GStreamerError(format!(
"Offer creation future got no reponse: {:?}",
err
))
.into());
}
};
let answer = reply
.get_value("answer")
.unwrap()
.get::<gst_webrtc::WebRTCSessionDescription>()
.expect("Invalid argument");
self.0
.webrtcbin
.emit("set-local-description", &[&answer, &None::<gst::Promise>])
.unwrap();
self.send_sdp_answer(&answer)
}
// Whenever webrtcbin tells us that (re-)negotiation is needed, simply ask
// for a new offer SDP from webrtcbin without any customization and then
// asynchronously send it to the peer via the WebSocket connection
fn on_negotiation_needed(&self) -> Result<(), Error> {
println!("Starting negotiation");
let app_clone = self.downgrade();
let promise = gst::Promise::new_with_change_func(move |promise| {
let app = upgrade_weak!(app_clone);
if let Err(err) = app.on_offer_created(promise) {
app.post_error(format!("Failed to send SDP offer: {:?}", err).as_str());
}
});
self.0
.webrtcbin
.emit("create-offer", &[&None::<gst::Structure>, &promise])
.unwrap();
Ok(())
}
// Handle a newly decoded stream from decodebin, i.e. one of the streams that the peer is
// sending to us. Connect it to newly create sink elements and converters.
fn handle_media_stream(&self, pad: &gst::Pad, media_type: MediaType) -> Result<(), Error> {
println!("Trying to handle stream {:?}", media_type);
let (q, conv, sink) = match media_type {
MediaType::Audio => {
let q = gst::ElementFactory::make("queue", None).unwrap();
let conv = gst::ElementFactory::make("audioconvert", None).unwrap();
let sink = gst::ElementFactory::make("autoaudiosink", None).unwrap();
let resample = gst::ElementFactory::make("audioresample", None).unwrap();
self.0
.pipeline
.add_many(&[&q, &conv, &resample, &sink])
.unwrap();
gst::Element::link_many(&[&q, &conv, &resample, &sink])?;
resample.sync_state_with_parent()?;
(q, conv, sink)
}
MediaType::Video => {
let q = gst::ElementFactory::make("queue", None).unwrap();
let conv = gst::ElementFactory::make("videoconvert", None).unwrap();
let sink = gst::ElementFactory::make("autovideosink", None).unwrap();
self.0.pipeline.add_many(&[&q, &conv, &sink]).unwrap();
gst::Element::link_many(&[&q, &conv, &sink])?;
(q, conv, sink)
}
};
q.sync_state_with_parent()?;
conv.sync_state_with_parent()?;
sink.sync_state_with_parent()?;
let qpad = q.get_static_pad("sink").unwrap();
pad.link(&qpad)?;
Ok(())
}
// Handle a newly decoded decodebin stream and depending on its type, create the relevant
// elements or simply ignore it
fn on_incoming_decodebin_stream(&self, pad: &gst::Pad) -> Result<(), Error> {
let caps = pad.get_current_caps().unwrap();
let name = caps.get_structure(0).unwrap().get_name();
if name.starts_with("video/") {
self.handle_media_stream(&pad, MediaType::Video)
} else if name.starts_with("audio/") {
self.handle_media_stream(&pad, MediaType::Audio)
} else {
println!("Unknown pad {:?}, ignoring", pad);
Ok(())
}
}
// Whenever there's a new incoming, encoded stream from the peer create a new decodebin
fn on_incoming_stream(&self, pad: &gst::Pad) -> Result<(), Error> {
// Early return for the source pads we're adding ourselves
if pad.get_direction() != gst::PadDirection::Src {
return Ok(());
}
let decodebin = gst::ElementFactory::make("decodebin", None).unwrap();
let app_clone = self.downgrade();
decodebin.connect_pad_added(move |_decodebin, pad| {
let app = upgrade_weak!(app_clone);
if let Err(err) = app.on_incoming_decodebin_stream(pad) {
app.post_error(format!("Failed to handle decoded stream: {:?}", err).as_str());
}
});
self.0.pipeline.add(&decodebin).unwrap();
decodebin.sync_state_with_parent()?;
let sinkpad = decodebin.get_static_pad("sink").unwrap();
pad.link(&sinkpad).unwrap();
Ok(())
}
// Asynchronously send ICE candidates to the peer via the WebSocket connection as a JSON
// message
fn send_ice_candidate_message(&self, mlineindex: u32, candidate: String) -> Result<(), Error> {
let message = serde_json::to_string(&JsonMsg::Ice {
candidate,
sdp_mline_index: mlineindex,
})
.unwrap();
self.send_text_msg(message)
}
// Create a video test source plus encoder for the video stream we send to the peer
fn add_video_source(&self) -> Result<(), Error> {
let videotestsrc = gst::ElementFactory::make("videotestsrc", None).unwrap();
let videoconvert = gst::ElementFactory::make("videoconvert", None).unwrap();
let queue = gst::ElementFactory::make("queue", None).unwrap();
let vp8enc = gst::ElementFactory::make("vp8enc", None).unwrap();
videotestsrc.set_property_from_str("pattern", "ball");
videotestsrc.set_property("is-live", &true).unwrap();
vp8enc.set_property("deadline", &1i64).unwrap();
let rtpvp8pay = gst::ElementFactory::make("rtpvp8pay", None).unwrap();
let queue2 = gst::ElementFactory::make("queue", None).unwrap();
self.0
.pipeline
.add_many(&[
&videotestsrc,
&videoconvert,
&queue,
&vp8enc,
&rtpvp8pay,
&queue2,
])
.unwrap();
gst::Element::link_many(&[
&videotestsrc,
&videoconvert,
&queue,
&vp8enc,
&rtpvp8pay,
&queue2,
])?;
queue2.link_filtered(&self.0.webrtcbin, Some(&*RTP_CAPS_VP8))?;
Ok(())
}
// Create a audio test source plus encoders for the audio stream we send to the peer
fn add_audio_source(&self) -> Result<(), Error> {
let audiotestsrc = gst::ElementFactory::make("audiotestsrc", None).unwrap();
let queue = gst::ElementFactory::make("queue", None).unwrap();
let audioconvert = gst::ElementFactory::make("audioconvert", None).unwrap();
let audioresample = gst::ElementFactory::make("audioresample", None).unwrap();
let queue2 = gst::ElementFactory::make("queue", None).unwrap();
let opusenc = gst::ElementFactory::make("opusenc", None).unwrap();
let rtpopuspay = gst::ElementFactory::make("rtpopuspay", None).unwrap();
let queue3 = gst::ElementFactory::make("queue", None).unwrap();
audiotestsrc.set_property_from_str("wave", "red-noise");
audiotestsrc.set_property("is-live", &true).unwrap();
self.0
.pipeline
.add_many(&[
&audiotestsrc,
&queue,
&audioconvert,
&audioresample,
&queue2,
&opusenc,
&rtpopuspay,
&queue3,
])
.unwrap();
gst::Element::link_many(&[
&audiotestsrc,
&queue,
&audioconvert,
&audioresample,
&queue2,
&opusenc,
&rtpopuspay,
&queue3,
])?;
queue3.link_filtered(&self.0.webrtcbin, Some(&*RTP_CAPS_OPUS))?;
Ok(())
}
// Finish creating our pipeline and actually start it once the connection with the peer is
// there
fn setup_pipeline(&self) -> Result<(), Error> {
println!("Start pipeline");
// Whenever (re-)negotiation is needed, do so but this is only needed if
// we send the initial offer
if self.0.peer_id.is_some() {
let app_clone = self.downgrade();
self.0
.webrtcbin
.connect("on-negotiation-needed", false, move |values| {
let _webrtc = values[0].get::<gst::Element>().unwrap();
let app = upgrade_weak!(app_clone, None);
if let Err(err) = app.on_negotiation_needed() {
app.post_error(format!("Failed to start negotiation: {:?}", err).as_str());
}
None
})
.unwrap();
}
// Whenever there is a new ICE candidate, send it to the peer
let app_clone = self.downgrade();
self.0
.webrtcbin
.connect("on-ice-candidate", false, move |values| {
let _webrtc = values[0].get::<gst::Element>().expect("Invalid argument");
let mlineindex = values[1].get::<u32>().expect("Invalid argument");
let candidate = values[2].get::<String>().expect("Invalid argument");
let app = upgrade_weak!(app_clone, None);
if let Err(err) = app.send_ice_candidate_message(mlineindex, candidate) {
app.post_error(format!("Failed to send ICE candidate: {:?}", err).as_str());
}
None
})
.unwrap();
// Whenever there is a new stream incoming from the peer, handle it
let app_clone = self.downgrade();
self.0.webrtcbin.connect_pad_added(move |_webrtc, pad| {
let app = upgrade_weak!(app_clone);
if let Err(err) = app.on_incoming_stream(pad) {
app.post_error(format!("Failed to handle incoming stream: {:?}", err).as_str());
}
});
// Create our audio/video sources we send to the peer
self.add_video_source()?;
self.add_audio_source()?;
// Enable RTX only for video, Chrome etc al fail SDP negotiation otherwise
if self.0.rtx {
let transceiver = self
.0
.webrtcbin
.emit("get-transceiver", &[&0i32])
.unwrap()
.unwrap()
.get::<glib::Object>()
.unwrap();
transceiver.set_property("do-nack", &true).unwrap();
}
Ok(())
}
// Send ID of the peer we want to talk to via the WebSocket connection
fn setup_call(&self, peer_id: &str) -> Result<OwnedMessage, Error> {
println!("Setting up signalling server call with {}", peer_id);
Ok(OwnedMessage::Text(format!("SESSION {}", peer_id)))
}
// Once we got the HELLO message from the WebSocket connection, start setting up the call
fn handle_hello(&self) -> Result<Option<OwnedMessage>, Error> {
if let Some(ref peer_id) = self.0.peer_id {
self.setup_call(peer_id).map(Some)
} else {
// Wait for a peer to appear
Ok(None)
}
}
// Once the session is set up correctly we start our pipeline
fn handle_session_ok(&self) -> Result<Option<OwnedMessage>, Error> {
self.setup_pipeline()?;
// And finally asynchronously start our pipeline
let app_clone = self.downgrade();
self.0.pipeline.call_async(move |pipeline| {
let app = upgrade_weak!(app_clone);
if let Err(err) = pipeline.set_state(gst::State::Playing) {
app.post_error(format!("Failed to set pipeline to Playing: {:?}", err).as_str());
}
});
Ok(None)
}
// Handle errors from the peer send to us via the WebSocket connection
fn handle_error(&self, msg: &str) -> Result<Option<OwnedMessage>, Error> {
println!("Got error message! {}", msg);
Err(PeerError(msg.into()).into())
}
// Handle incoming SDP answers from the peer
fn handle_sdp(&self, type_: &str, sdp: &str) -> Result<Option<OwnedMessage>, Error> {
if type_ == "answer" {
print!("Received answer:\n{}\n", sdp);
let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes())
.map_err(|_| GStreamerError("Failed to parse SDP answer".into()))?;
let answer =
gst_webrtc::WebRTCSessionDescription::new(gst_webrtc::WebRTCSDPType::Answer, ret);
self.0
.webrtcbin
.emit("set-remote-description", &[&answer, &None::<gst::Promise>])
.unwrap();
Ok(None)
} else if type_ == "offer" {
print!("Received offer:\n{}\n", sdp);
// FIXME: We need to do negotiation here based on what the peer offers us in the SDP
// and what we can produce. For example all RTCP or RTP header extensions we don't
// understand have to be removed, and similarly we have to negotiate the codecs.
// Need to start the pipeline as a first step here
self.setup_pipeline()?;
let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes())
.map_err(|_| GStreamerError("Failed to parse SDP offer".into()))?;
// And then asynchronously start our pipeline and do the next steps. The
// pipeline needs to be started before we can create an answer
let app_clone = self.downgrade();
self.0.pipeline.call_async(move |pipeline| {
let app = upgrade_weak!(app_clone);
if let Err(err) = pipeline.set_state(gst::State::Playing) {
app.post_error(
format!("Failed to set pipeline to Playing: {:?}", err).as_str(),
);
return;
}
let offer = gst_webrtc::WebRTCSessionDescription::new(
gst_webrtc::WebRTCSDPType::Offer,
ret,
);
app.0
.webrtcbin
.emit("set-remote-description", &[&offer, &None::<gst::Promise>])
.unwrap();
let app_clone = app.downgrade();
let promise = gst::Promise::new_with_change_func(move |promise| {
let app = upgrade_weak!(app_clone);
if let Err(err) = app.on_answer_created(promise) {
app.post_error(format!("Failed to send SDP answer: {:?}", err).as_str());
}
});
app.0
.webrtcbin
.emit("create-answer", &[&None::<gst::Structure>, &promise])
.unwrap();
});
Ok(None)
} else {
Err(PeerError(format!("Sdp type is not \"answer\" but \"{}\"", type_)).into())
}
}
// Handle incoming ICE candidates from the peer by passing them to webrtcbin
fn handle_ice(
&self,
sdp_mline_index: u32,
candidate: &str,
) -> Result<Option<OwnedMessage>, Error> {
self.0
.webrtcbin
.emit("add-ice-candidate", &[&sdp_mline_index, &candidate])
.unwrap();
Ok(None)
}
// Handle messages we got from the peer via the WebSocket connection
fn on_message(&self, msg: &str) -> Result<Option<OwnedMessage>, Error> {
match msg {
"HELLO" => self.handle_hello(),
"SESSION_OK" => self.handle_session_ok(),
x if x.starts_with("ERROR") => self.handle_error(msg),
_ => {
let json_msg: JsonMsg = serde_json::from_str(msg)?;
match json_msg {
JsonMsg::Sdp { type_, sdp } => self.handle_sdp(&type_, &sdp),
JsonMsg::Ice {
sdp_mline_index,
candidate,
} => self.handle_ice(sdp_mline_index, &candidate),
}
}
}
}
// Handle WebSocket messages, both our own as well as WebSocket protocol messages
fn handle_websocket_message(
&self,
message: OwnedMessage,
) -> Result<Option<OwnedMessage>, Error> {
match message {
OwnedMessage::Close(_) => Ok(Some(OwnedMessage::Close(None))),
OwnedMessage::Ping(data) => Ok(Some(OwnedMessage::Pong(data))),
OwnedMessage::Text(msg) => self.on_message(&msg),
OwnedMessage::Binary(_) => Ok(None),
OwnedMessage::Pong(_) => Ok(None),
}
}
// Handle GStreamer messages coming from the pipeline
fn handle_pipeline_message(
&self,
message: &gst::Message,
) -> Result<Option<OwnedMessage>, Error> {
use gst::message::MessageView;
match message.view() {
MessageView::Error(err) => Err(GStreamerError(format!(
"Error from element {}: {} ({})",
err.get_src()
.map(|s| String::from(s.get_path_string()))
.unwrap_or_else(|| String::from("None")),
err.get_error(),
err.get_debug().unwrap_or_else(|| String::from("None")),
))
.into()),
MessageView::Warning(warning) => {
println!("Warning: \"{}\"", warning.get_debug().unwrap());
Ok(None)
}
_ => Ok(None),
}
}
// Entry-point to start everything
fn register_with_server(&self) {
let our_id = rand::thread_rng().gen_range(10, 10_000);
println!("Registering id {} with server", our_id);
self.send_text_msg(format!("HELLO {}", our_id)).unwrap();
}
}
fn parse_args() -> (String, Option<String>, bool) {
let matches = clap::App::new("Sendrecv rust")
.arg(
clap::Arg::with_name("peer-id")
.help("String ID of the peer to connect to")
.long("peer-id")
.required(false)
.takes_value(true),
)
.arg(
clap::Arg::with_name("server")
.help("Signalling server to connect to")
.long("server")
.required(false)
.takes_value(true),
)
.arg(
clap::Arg::with_name("rtx")
.help("Enable retransmissions (RTX)")
.long("rtx")
.required(false),
)
.get_matches();
let server = matches
.value_of("server")
.unwrap_or("wss://webrtc.nirbheek.in:8443");
let peer_id = matches.value_of("peer-id");
let rtx = matches.is_present("rtx");
(server.to_string(), peer_id.map(String::from), rtx)
}
fn check_plugins() -> Result<(), Error> {
let needed = [
"opus",
"vpx",
"nice",
"webrtc",
"dtls",
"srtp",
"rtpmanager",
"videotestsrc",
"audiotestsrc",
];
let registry = gst::Registry::get();
let missing = needed
.iter()
.filter(|n| registry.find_plugin(n).is_none())
.cloned()
.collect::<Vec<_>>();
if !missing.is_empty() {
Err(MissingElements(missing))?
} else {
Ok(())
}
}
fn main() {
gst::init().unwrap();
if let Err(err) = check_plugins() {
println!("{:?}", err);
return;
}
let (server, peer_id, rtx) = parse_args();
let mut runtime = tokio::runtime::Runtime::new().unwrap();
println!("Connecting to server {}", server);
let res = runtime.block_on(
websocket::client::ClientBuilder::new(&server)
.unwrap()
.async_connect(None)
.map_err(|err| Error::from(WebSocketError(err)))
.and_then(move |(stream, _)| {
println!("connected");
// Create basic pipeline
let pipeline = gst::Pipeline::new(Some("main"));
let webrtcbin = gst::ElementFactory::make("webrtcbin", None).unwrap();
pipeline.add(&webrtcbin).unwrap();
webrtcbin.set_property_from_str("stun-server", STUN_SERVER);
webrtcbin.set_property_from_str("bundle-policy", "max-bundle");
let bus = pipeline.get_bus().unwrap();
// Send our bus messages via a futures channel to be handled asynchronously
let (send_gst_msg_tx, send_gst_msg_rx) = mpsc::unbounded_channel::<gst::Message>();
let send_gst_msg_tx = Mutex::new(send_gst_msg_tx);
bus.set_sync_handler(move |_, msg| {
let _ = send_gst_msg_tx.lock().unwrap().try_send(msg.clone());
gst::BusSyncReply::Pass
});
// Create our application control logic
let (send_ws_msg_tx, send_ws_msg_rx) = mpsc::unbounded_channel::<OwnedMessage>();
let app = App(Arc::new(AppInner {
peer_id,
pipeline,
webrtcbin,
send_msg_tx: Mutex::new(send_ws_msg_tx),
rtx,
}));
// Start registration process with the server. This will insert a
// message into the send_ws_msg channel that will then be sent later
app.register_with_server();
// Split the stream into the receive part (stream) and send part (sink)
let (sink, stream) = stream.split();
// Pass the WebSocket messages to our application control logic
// and convert them into potential messages to send out
let app_clone = app.clone();
let ws_messages = stream
.map_err(|err| Error::from(WebSocketError(err)))
.and_then(move |msg| app_clone.handle_websocket_message(msg))
.filter_map(|msg| msg);
// Pass the GStreamer messages to the application control logic
// and convert them into potential messages to send out
let app_clone = app.clone();
let gst_messages = send_gst_msg_rx
.map_err(Error::from)
.and_then(move |msg| app_clone.handle_pipeline_message(&msg))
.filter_map(|msg| msg);
// Merge the two outgoing message streams
let sync_outgoing_messages = gst_messages.select(ws_messages);
// And here collect all the asynchronous outgoing messages that come
// from other threads
let async_outgoing_messages = send_ws_msg_rx.map_err(Error::from);
// Merge both outgoing messages streams and send them out directly
sink.sink_map_err(|err| Error::from(WebSocketError(err)))
.send_all(sync_outgoing_messages.select(async_outgoing_messages))
.map(|_| ())
}),
);
if let Err(err) = res {
println!("Error: {:?}", err);
}
// And now shut down the runtime
runtime.shutdown_now().wait().unwrap();
}