/
webrtc_sink.rs
738 lines (633 loc) · 27.7 KB
/
webrtc_sink.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
use crate::cli;
use anyhow::{anyhow, Context, Result};
use gst::prelude::*;
use tokio::sync::mpsc::{self, WeakUnboundedSender};
use tracing::*;
use super::SinkInterface;
use crate::stream::manager::Manager;
use crate::stream::webrtc::signalling_protocol::{
Answer, BindAnswer, IceNegotiation, MediaNegotiation, Message, RTCIceCandidateInit,
RTCSessionDescription, Sdp,
};
use crate::stream::webrtc::signalling_server::WebRTCSessionManagementInterface;
use crate::stream::webrtc::turn_server::DEFAULT_TURN_ENDPOINT;
use crate::stream::webrtc::webrtcbin_interface::WebRTCBinInterface;
#[derive(Clone)]
pub struct WebRTCSinkWeakProxy {
bind: BindAnswer,
sender: WeakUnboundedSender<Result<Message>>,
}
#[derive(Debug)]
pub struct WebRTCSink {
pub queue: gst::Element,
pub webrtcbin: gst::Element,
pub webrtcbin_sink_pad: gst::Pad,
pub tee_src_pad: Option<gst::Pad>,
pub bind: BindAnswer,
/// MPSC channel's sender to send messages to the respective Websocket from Signaller server. Err can be used to end the WebSocket.
pub sender: mpsc::UnboundedSender<Result<Message>>,
pub end_reason: Option<String>,
}
impl SinkInterface for WebRTCSink {
#[instrument(level = "debug", skip(self, pipeline))]
fn link(
self: &mut WebRTCSink,
pipeline: &gst::Pipeline,
pipeline_id: &uuid::Uuid,
tee_src_pad: gst::Pad,
) -> Result<()> {
// Configure transceiver https://gstreamer.freedesktop.org/documentation/webrtclib/gstwebrtc-transceiver.html?gi-language=c
let webrtcbin_sink_pad = &self.webrtcbin_sink_pad;
let transceiver =
webrtcbin_sink_pad.property::<gst_webrtc::WebRTCRTPTransceiver>("transceiver");
transceiver.set_property(
"direction",
gst_webrtc::WebRTCRTPTransceiverDirection::Sendonly,
);
transceiver.set_property("do-nack", false);
transceiver.set_property("fec-type", gst_webrtc::WebRTCFECType::None);
// Link
let sink_id = &self.get_id();
// Set Tee's src pad
if self.tee_src_pad.is_some() {
return Err(anyhow!(
"Tee's src pad from WebRTCBin {sink_id} has already been configured"
));
}
self.tee_src_pad.replace(tee_src_pad);
let Some(tee_src_pad) = &self.tee_src_pad else {
unreachable!()
};
// Block data flow to prevent any data before set Playing, which would cause an error
let Some(tee_src_pad_data_blocker) = tee_src_pad
.add_probe(gst::PadProbeType::BLOCK_DOWNSTREAM, |_pad, _info| {
gst::PadProbeReturn::Ok
})
else {
let msg =
"Failed adding probe to Tee's src pad to block data before going to playing state"
.to_string();
error!(msg);
if let Some(parent) = tee_src_pad.parent_element() {
parent.release_request_pad(tee_src_pad)
}
return Err(anyhow!(msg));
};
// Add the Sink elements to the Pipeline
let elements = &[&self.queue, &self.webrtcbin];
if let Err(add_err) = pipeline.add_many(elements) {
let msg = format!("Failed to add WebRTCSink's elements to the Pipeline: {add_err:?}");
error!(msg);
if let Some(parent) = tee_src_pad.parent_element() {
parent.release_request_pad(tee_src_pad)
}
return Err(anyhow!(msg));
}
// Link the queue's src pad to the Sink's sink pad
let queue_src_pad = &self
.queue
.static_pad("src")
.expect("No sink pad found on Queue");
if let Err(link_err) = queue_src_pad.link(&self.webrtcbin_sink_pad) {
let msg =
format!("Failed to link Queue's src pad with WebRTCBin's sink pad: {link_err:?}");
error!(msg);
if let Some(parent) = tee_src_pad.parent_element() {
parent.release_request_pad(tee_src_pad)
}
if let Err(remove_err) = pipeline.remove_many(elements) {
error!("Failed to remove elements from pipeline: {remove_err:?}");
}
return Err(anyhow!(msg));
}
// Link the new Tee's src pad to the queue's sink pad
let queue_sink_pad = &self
.queue
.static_pad("sink")
.expect("No src pad found on Queue");
if let Err(link_err) = tee_src_pad.link(queue_sink_pad) {
let msg = format!("Failed to link Tee's src pad with Queue's sink pad: {link_err:?}");
error!(msg);
if let Err(unlink_err) = queue_src_pad.unlink(&self.webrtcbin_sink_pad) {
error!(
"Failed to unlink Queue's src pad from WebRTCBin's sink pad: {unlink_err:?}"
);
}
if let Some(parent) = tee_src_pad.parent_element() {
parent.release_request_pad(tee_src_pad)
}
if let Err(remove_err) = pipeline.remove_many(elements) {
error!("Failed to remove elements from pipeline: {remove_err:?}");
}
return Err(anyhow!(msg));
}
// Syncronize added and linked elements
if let Err(sync_err) = pipeline.sync_children_states() {
let msg = format!("Failed to synchronize children states: {sync_err:?}");
error!(msg);
if let Err(unlink_err) = queue_src_pad.unlink(&self.webrtcbin_sink_pad) {
error!(
"Failed to unlink Queue's src pad from WebRTCBin's sink pad: {unlink_err:?}"
);
}
if let Err(unlink_err) = tee_src_pad.unlink(queue_sink_pad) {
error!("Failed to unlink Tee's src pad from Queue's sink pad: {unlink_err:?}");
}
if let Some(parent) = tee_src_pad.parent_element() {
parent.release_request_pad(tee_src_pad)
}
if let Err(remove_err) = pipeline.remove_many(elements) {
error!("Failed to remove elements from pipeline: {remove_err:?}");
}
return Err(anyhow!(msg));
}
// Unblock data to go through this added Tee src pad
tee_src_pad.remove_probe(tee_src_pad_data_blocker);
// TODO: Workaround for bug: https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/issues/1539
// Reasoning: because we are not receiving the Disconnected | Failed | Closed of WebRTCPeerConnectionState,
// we are directly connecting to webrtcbin->transceiver->transport->connect_state_notify:
// When the bug is solved, we should remove this code and use WebRTCPeerConnectionState instead.
let webrtcbin_clone = self.webrtcbin.downgrade();
let bind_clone = self.bind.clone();
let rtp_sender = transceiver
.sender()
.context("Failed getting transceiver's RTP sender element")?;
rtp_sender.connect_notify(Some("transport"), move |rtp_sender, _pspec| {
let transport = rtp_sender.property::<gst_webrtc::WebRTCDTLSTransport>("transport");
let bind = bind_clone.clone();
let webrtcbin_clone = webrtcbin_clone.clone();
transport.connect_state_notify(move |transport| {
use gst_webrtc::WebRTCDTLSTransportState::*;
let bind = bind.clone();
let state = transport.state();
debug!("DTLS Transport Connection changed to {state:#?}");
match state {
Failed | Closed => {
if let Some(webrtcbin) = webrtcbin_clone.upgrade() {
if webrtcbin.current_state() == gst::State::Playing {
// Closing the channel from the same thread can cause a deadlock, so we are calling it from another one:
std::thread::Builder::new()
.name("DTLSKiller".to_string())
.spawn(move || {
let bind = &bind.clone();
if let Err(error) = Manager::remove_session(
bind,
format!(
"DTLS Transport connection closed with: {state:?}"
),
) {
error!("Failed removing session {bind:#?}: {error}");
}
})
.expect("Failed spawing DTLSKiller thread");
}
}
}
_ => (),
}
});
});
Ok(())
}
#[instrument(level = "debug", skip(self, pipeline))]
fn unlink(&self, pipeline: &gst::Pipeline, pipeline_id: &uuid::Uuid) -> Result<()> {
let Some(tee_src_pad) = &self.tee_src_pad else {
warn!("Tried to unlink Sink from a pipeline without a Tee src pad.");
return Ok(());
};
// Block data flow to prevent any data from holding the Pipeline elements alive
if tee_src_pad
.add_probe(gst::PadProbeType::BLOCK_DOWNSTREAM, |_pad, _info| {
gst::PadProbeReturn::Ok
})
.is_none()
{
warn!(
"Failed adding probe to Tee's src pad to block data before going to playing state"
);
}
// Unlink the Queue element from the source's pipeline Tee's src pad
let queue_sink_pad = self
.queue
.static_pad("sink")
.expect("No sink pad found on Queue");
if let Err(unlink_err) = tee_src_pad.unlink(&queue_sink_pad) {
warn!("Failed unlinking WebRTC's Queue element from Tee's src pad: {unlink_err:?}");
}
drop(queue_sink_pad);
// Release Tee's src pad
if let Some(parent) = tee_src_pad.parent_element() {
parent.release_request_pad(tee_src_pad)
}
// Remove the Sink's elements from the Source's pipeline
let elements = &[&self.queue, &self.webrtcbin];
if let Err(remove_err) = pipeline.remove_many(elements) {
warn!("Failed removing WebRTCBin's elements from pipeline: {remove_err:?}");
}
// Set Queue to null
if let Err(state_err) = self.queue.set_state(gst::State::Null) {
warn!("Failed to set Queue's state to NULL: {state_err:#?}");
}
// Set Sink to null
if let Err(state_err) = self.webrtcbin.set_state(gst::State::Null) {
warn!("Failed to set WebRTCBin's to NULL: {state_err:#?}");
}
Ok(())
}
#[instrument(level = "trace", skip(self))]
fn get_id(&self) -> uuid::Uuid {
self.bind.session_id
}
#[instrument(level = "trace", skip(self))]
fn get_sdp(&self) -> Result<gst_sdp::SDPMessage> {
Err(anyhow!(
"Not available: WebRTC Sink should only be connected by means of its Signalling protocol."
))
}
#[instrument(level = "debug", skip(self))]
fn start(&self) -> Result<()> {
Ok(())
}
#[instrument(level = "debug", skip(self))]
fn eos(&self) {
let webrtcbin_weak = self.webrtcbin.downgrade();
std::thread::spawn(move || {
let webrtcbin = webrtcbin_weak.upgrade().unwrap();
if let Err(error) = webrtcbin.post_message(gst::message::Eos::new()) {
error!("Failed posting Eos message into Sink bus. Reason: {error:?}");
}
});
}
}
impl WebRTCSink {
#[instrument(level = "debug", skip(sender))]
pub fn try_new(
bind: BindAnswer,
sender: mpsc::UnboundedSender<Result<Message>>,
) -> Result<Self> {
let queue = gst::ElementFactory::make("queue")
.property_from_str("leaky", "downstream") // Throw away any data
.property("silent", true)
.property("flush-on-eos", true)
.property("max-size-buffers", 0u32) // Disable buffers
.build()?;
// Workaround to have a better name for the threads created by the WebRTCBin element
let webrtcbin = {
let (tx, rx) = std::sync::mpsc::sync_channel(1);
std::thread::Builder::new()
.name("WebRTCBin".to_string())
.spawn(move || {
let webrtcbin = gst::ElementFactory::make("webrtcbin")
.property_from_str(
"name",
format!("webrtcbin-{}", bind.session_id).as_str(),
)
.property("async-handling", true)
.property("bundle-policy", gst_webrtc::WebRTCBundlePolicy::MaxBundle) // https://webrtcstandards.info/sdp-bundle/
.property("latency", 0u32)
.property_from_str(
"stun-server",
cli::manager::stun_server_address().as_str(),
)
.property_from_str("turn-server", DEFAULT_TURN_ENDPOINT)
.build();
tx.send(webrtcbin).unwrap();
})
.expect("Failed spawning leak_inside_webrtcbin thread");
rx.recv()??
};
// Configure RTP
let webrtcbin = webrtcbin.downcast::<gst::Bin>().unwrap();
webrtcbin
.iterate_elements()
.filter(|e| e.name().starts_with("rtpbin"))
.into_iter()
.for_each(|res| {
let Ok(rtp_bin) = res else { return };
// Use the pipeline clock time. This will ensure that the timestamps from the source are correct.
rtp_bin.set_property_from_str("ntp-time-source", "clock-time");
});
let webrtcbin = webrtcbin.upcast::<gst::Element>();
let webrtcbin_sink_pad = webrtcbin
.request_pad_simple("sink_%u")
.context("Failed requesting sink pad for webrtcsink")?;
sender.send(Ok(Message::from(Answer::StartSession(bind.clone()))))?;
let this = WebRTCSink {
queue,
webrtcbin,
webrtcbin_sink_pad,
tee_src_pad: None,
bind,
sender,
end_reason: None,
};
// Connect to on-negotiation-needed to handle sending an Offer
let weak_proxy = this.downgrade();
this.webrtcbin
.connect("on-negotiation-needed", false, move |values| {
let element = values[0].get::<gst::Element>().expect("Invalid argument");
if let Err(error) = weak_proxy.on_negotiation_needed(&element) {
error!("Failed to negotiate: {error:?}");
}
None
});
// Whenever there is a new ICE candidate, send it to the peer
let weak_proxy = this.downgrade();
this.webrtcbin
.connect("on-ice-candidate", false, move |values| {
let element = values[0].get::<gst::Element>().expect("Invalid argument");
let sdp_m_line_index = values[1].get::<u32>().expect("Invalid argument");
let candidate = values[2].get::<String>().expect("Invalid argument");
if let Err(error) =
weak_proxy.on_ice_candidate(&element, &sdp_m_line_index, &candidate)
{
debug!("Failed to send ICE candidate: {error}");
}
None
});
let weak_proxy = this.downgrade();
this.webrtcbin
.connect_notify(Some("connection-state"), move |webrtcbin, _pspec| {
let state =
webrtcbin.property::<gst_webrtc::WebRTCPeerConnectionState>("connection-state");
if let Err(error) = weak_proxy.on_connection_state_change(webrtcbin, &state) {
error!("Failed to processing connection-state: {error:?}");
}
});
let weak_proxy = this.downgrade();
this.webrtcbin
.connect_notify(Some("ice-connection-state"), move |webrtcbin, _pspec| {
let state = webrtcbin
.property::<gst_webrtc::WebRTCICEConnectionState>("ice-connection-state");
if let Err(error) = weak_proxy.on_ice_connection_state_change(webrtcbin, &state) {
error!("Failed to processing ice-connection-state: {error:?}");
}
});
let weak_proxy = this.downgrade();
this.webrtcbin
.connect_notify(Some("ice-gathering-state"), move |webrtcbin, _pspec| {
let state = webrtcbin
.property::<gst_webrtc::WebRTCICEGatheringState>("ice-gathering-state");
if let Err(error) = weak_proxy.on_ice_gathering_state_change(webrtcbin, &state) {
error!("Failed to processing ice-gathering-state: {error:?}");
}
});
Ok(this)
}
#[instrument(level = "debug", skip(self))]
fn downgrade(&self) -> WebRTCSinkWeakProxy {
WebRTCSinkWeakProxy {
bind: self.bind.clone(),
sender: self.sender.downgrade(),
}
}
#[instrument(level = "debug", skip(self))]
pub fn handle_sdp(&self, sdp: &gst_webrtc::WebRTCSessionDescription) -> Result<()> {
self.downgrade().handle_sdp(&self.webrtcbin, sdp)
}
#[instrument(level = "debug", skip(self))]
pub fn handle_ice(&self, sdp_m_line_index: &u32, candidate: &str) -> Result<()> {
self.downgrade()
.handle_ice(&self.webrtcbin, sdp_m_line_index, candidate)
}
}
impl WebRTCBinInterface for WebRTCSinkWeakProxy {
// 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
#[instrument(level = "debug", skip(self, webrtcbin))]
fn on_negotiation_needed(&self, webrtcbin: &gst::Element) -> Result<()> {
let this = self.clone();
let webrtcbin_weak = webrtcbin.downgrade();
let promise = gst::Promise::with_change_func(move |reply| {
let reply = match reply {
Ok(Some(reply)) => reply,
Ok(None) => {
error!("Offer creation future got no response");
return;
}
Err(error) => {
error!("Failed to send SDP offer: {error:?}");
return;
}
};
let offer = match reply.get_optional::<gst_webrtc::WebRTCSessionDescription>("offer") {
Ok(Some(offer)) => offer,
Ok(None) => {
error!("Response got no \"offer\"");
return;
}
Err(error) => {
error!("Failed to send SDP offer: {error:?}");
return;
}
};
if let Some(webrtcbin) = webrtcbin_weak.upgrade() {
if let Err(error) = this.on_offer_created(&webrtcbin, &offer) {
error!("Failed to send SDP offer: {error}");
}
}
});
webrtcbin.emit_by_name::<()>("create-offer", &[&None::<gst::Structure>, &promise]);
Ok(())
}
// Once webrtcbin has create the offer SDP for us, handle it by sending it to the peer via the
// WebSocket connection
#[instrument(level = "debug", skip(self, webrtcbin))]
fn on_offer_created(
&self,
webrtcbin: &gst::Element,
offer: &gst_webrtc::WebRTCSessionDescription,
) -> Result<()> {
// Recreate the SDP offer with our customized SDP
let offer =
gst_webrtc::WebRTCSessionDescription::new(offer.type_(), customize_sdp(&offer.sdp())?);
let Ok(sdp) = offer.sdp().as_text() else {
return Err(anyhow!("Failed reading the received SDP"));
};
// All good, then set local description
webrtcbin.emit_by_name::<()>("set-local-description", &[&offer, &None::<gst::Promise>]);
debug!("Sending SDP offer to peer. Offer:\n{sdp}");
let message = MediaNegotiation {
bind: self.bind.clone(),
sdp: RTCSessionDescription::Offer(Sdp { sdp }),
}
.into();
self.sender
.upgrade()
.context("Failed accessing MPSC Sender")?
.send(Ok(message))?;
Ok(())
}
// Once webrtcbin has create the answer SDP for us, handle it by sending it to the peer via the
// WebSocket connection
#[instrument(level = "debug", skip(self, _webrtcbin))]
fn on_answer_created(
&self,
_webrtcbin: &gst::Element,
answer: &gst_webrtc::WebRTCSessionDescription,
) -> Result<()> {
// Recreate the SDP answer with our customized SDP
let answer = gst_webrtc::WebRTCSessionDescription::new(
answer.type_(),
customize_sdp(&answer.sdp())?,
);
let Ok(sdp) = answer.sdp().as_text() else {
return Err(anyhow!("Failed reading the received SDP"));
};
debug!("Sending SDP answer to peer. Answer:\n{sdp}");
// All good, then set local description
let message = MediaNegotiation {
bind: self.bind.clone(),
sdp: RTCSessionDescription::Answer(Sdp { sdp }),
}
.into();
self.sender
.upgrade()
.context("Failed accessing MPSC Sender")?
.send(Ok(message))
.context("Failed to send SDP answer")?;
Ok(())
}
#[instrument(level = "debug", skip(self, _webrtcbin))]
fn on_ice_candidate(
&self,
_webrtcbin: &gst::Element,
sdp_m_line_index: &u32,
candidate: &str,
) -> Result<()> {
let message = IceNegotiation {
bind: self.bind.clone(),
ice: RTCIceCandidateInit {
candidate: Some(candidate.to_owned()),
sdp_mid: None,
sdp_m_line_index: Some(sdp_m_line_index.to_owned()),
username_fragment: None,
},
}
.into();
self.sender
.upgrade()
.context("Failed accessing MPSC Sender")?
.send(Ok(message))?;
debug!("ICE candidate created!");
Ok(())
}
#[instrument(level = "debug", skip(self, _webrtcbin))]
fn on_ice_gathering_state_change(
&self,
_webrtcbin: &gst::Element,
state: &gst_webrtc::WebRTCICEGatheringState,
) -> Result<()> {
if let gst_webrtc::WebRTCICEGatheringState::Complete = state {
debug!("ICE gathering complete")
}
Ok(())
}
#[instrument(level = "debug", skip(self, webrtcbin))]
fn on_ice_connection_state_change(
&self,
webrtcbin: &gst::Element,
state: &gst_webrtc::WebRTCICEConnectionState,
) -> Result<()> {
use gst_webrtc::WebRTCICEConnectionState::*;
debug!("ICE connection changed to {state:#?}");
match state {
Completed => {
let srcpads = webrtcbin.src_pads();
if let Some(srcpad) = srcpads.first() {
srcpad.send_event(
gst_video::UpstreamForceKeyUnitEvent::builder()
.all_headers(true)
.build(),
);
}
}
Failed | Closed | Disconnected => {
let bind = self.bind.clone();
let state = *state;
// Closing the channel from the same thread can cause a deadlock, so we are calling it from another one:
std::thread::Builder::new()
.name("ICEKiller".to_string())
.spawn(move || {
if let Err(error) =
Manager::remove_session(&bind, format!("ICE closed with: {state:?}"))
{
error!("Failed removing session {bind:#?}: {error}");
}
})
.expect("Failed spawing ICEKiller thread");
}
_ => (),
};
Ok(())
}
#[instrument(level = "debug", skip(self, _webrtcbin))]
fn on_connection_state_change(
&self,
_webrtcbin: &gst::Element,
state: &gst_webrtc::WebRTCPeerConnectionState,
) -> Result<()> {
use gst_webrtc::WebRTCPeerConnectionState::*;
debug!("Connection changed to {state:#?}");
match state {
// TODO: This would be the desired workflow, but it is not being detected, so we are using a workaround connecting direcly to the DTLS Transport connection state in the Session constructor.
Disconnected | Failed | Closed => {
warn!("For mantainers: Peer connection lost was detected by WebRTCPeerConnectionState, we should remove the workaround. State: {state:#?}");
// self.close("Connection lost"); // TODO: Keep this line commented until the forementioned bug is solved.
}
_ => (),
}
Ok(())
}
#[instrument(level = "debug", skip(self, webrtcbin))]
fn handle_sdp(
&self,
webrtcbin: &gst::Element,
sdp: &gst_webrtc::WebRTCSessionDescription,
) -> Result<()> {
webrtcbin.emit_by_name::<()>("set-remote-description", &[&sdp, &None::<gst::Promise>]);
Ok(())
}
#[instrument(level = "debug", skip(self, webrtcbin))]
fn handle_ice(
&self,
webrtcbin: &gst::Element,
sdp_m_line_index: &u32,
candidate: &str,
) -> Result<()> {
webrtcbin.emit_by_name::<()>("add-ice-candidate", &[&sdp_m_line_index, &candidate]);
Ok(())
}
}
/// Because GSTreamer's WebRTCBin often crashes when receiving an invalid SDP,
/// we use Mozzila's SDP parser to manipulate the SDP Message before giving it to GStreamer
#[instrument(level = "debug")]
fn customize_sdp(sdp: &gst_sdp::SDPMessage) -> Result<gst_sdp::SDPMessage> {
let mut sdp = webrtc_sdp::parse_sdp(sdp.as_text()?.as_str(), false)?;
for media in sdp.media.iter_mut() {
let attributes = media.get_attributes().to_vec(); // This clone is necessary to avoid imutable borrow after a mutable borrow
for attribute in attributes {
use webrtc_sdp::attribute_type::SdpAttribute::*;
match attribute {
// Filter out unsupported/unwanted attributes
Recvonly | Sendrecv | Inactive => {
media.remove_attribute((&attribute).into());
debug!("Removed unsupported/unwanted attribute: {attribute:?}");
}
// Customize FMTP
// Here we are lying to the peer about our profile-level-id (to constrained-baseline) so any browser can accept it
Fmtp(mut fmtp) => {
const CONSTRAINED_BASELINE_LEVEL_ID: u32 = 0x42e01f;
fmtp.parameters.profile_level_id = CONSTRAINED_BASELINE_LEVEL_ID;
fmtp.parameters.level_asymmetry_allowed = true;
let attribute = webrtc_sdp::attribute_type::SdpAttribute::Fmtp(fmtp);
debug!("FMTP attribute customized: {attribute:?}");
media.set_attribute(attribute)?;
}
_ => continue,
}
}
}
gst_sdp::SDPMessage::parse_buffer(sdp.to_string().as_bytes()).map_err(anyhow::Error::msg)
}