gst-plugin-webrtc 0.15.0

GStreamer plugin for high level WebRTC elements and a simple signaling server
Documentation
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
// Copyright (C) 2022, Asymptotic Inc.
//      Author: Sanchayan Maity <sanchayan@asymptotic.io>
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at
// <https://mozilla.org/MPL/2.0/>.
//
// SPDX-License-Identifier: MPL-2.0

use crate::RUNTIME;
use crate::signaller::{Signallable, SignallableImpl};
use crate::utils::{
    WaitError, build_reqwest_client, parse_redirect_location, set_ice_servers, wait, wait_async,
};
use bytes::Bytes;
use futures::future;
use gst::glib::RustClosure;
use gst::{glib, prelude::*, subclass::prelude::*};
use gst_sdp::*;
use gst_webrtc::*;
use reqwest::StatusCode;
use reqwest::header::{HeaderMap, HeaderValue};
use std::sync::LazyLock;
use std::sync::Mutex;

static CAT: LazyLock<gst::DebugCategory> = LazyLock::new(|| {
    gst::DebugCategory::new(
        "whep-client-signaller",
        gst::DebugColorFlags::empty(),
        Some("WHEP Client Signaller"),
    )
});

const MAX_REDIRECTS: u8 = 10;
const DEFAULT_TIMEOUT: u32 = 15;
const WHEP_CLIENT_OFFER: &str = "whep-client-offer";
const WHEP_SSO: &str = "whep-server-sent-offer";

#[derive(Debug, Clone)]
struct Settings {
    whep_endpoint: Option<String>,
    auth_token: Option<String>,
    use_link_headers: bool,
    timeout: u32,
}

#[allow(clippy::derivable_impls)]
impl Default for Settings {
    fn default() -> Self {
        Self {
            whep_endpoint: None,
            auth_token: None,
            use_link_headers: false,
            timeout: DEFAULT_TIMEOUT,
        }
    }
}

#[derive(Debug, Clone)]
enum PatchType {
    // Answer to the counter offer sent by the WHEP Server
    AnswerCounterOffer { whep_resource: String },
    // TODO: :
    // IceRestart,
    // IceTrickle,
}

#[derive(Debug, Default, Clone)]
enum State {
    #[default]
    Stopped,
    Post {
        redirects: u8,
    },
    Running {
        whep_resource: String,
    },
    Patch {
        patch: PatchType,
    },
}

impl std::fmt::Display for State {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            State::Stopped => write!(f, "Stopped"),
            State::Post { redirects } => write!(f, "Post (redirects: {redirects})"),
            State::Running { whep_resource } => {
                write!(f, "Running (whep_resource: {whep_resource})")
            }
            State::Patch { patch } => match patch {
                PatchType::AnswerCounterOffer { whep_resource } => write!(
                    f,
                    "Patch AnswerCounterOffer (whep_resource {whep_resource})"
                ),
            },
        }
    }
}

pub struct WhepClient {
    settings: Mutex<Settings>,
    state: Mutex<State>,
    canceller: Mutex<Option<future::AbortHandle>>,
    client: reqwest::Client,
}

impl Default for WhepClient {
    fn default() -> Self {
        // We'll handle redirects manually since the default redirect handler does not
        // reuse the authentication token on the redirected server
        let pol = reqwest::redirect::Policy::none();
        let client = build_reqwest_client(pol);

        Self {
            settings: Mutex::new(Settings::default()),
            state: Mutex::new(State::default()),
            canceller: Mutex::new(None),
            client,
        }
    }
}

impl ObjectImpl for WhepClient {
    fn properties() -> &'static [glib::ParamSpec] {
        static PROPERTIES: LazyLock<Vec<glib::ParamSpec>> = LazyLock::new(|| {
            vec![
                glib::ParamSpecString::builder("whep-endpoint")
                    .nick("WHEP Endpoint")
                    .blurb("The WHEP server endpoint to POST SDP offer to.")
                    .build(),
                glib::ParamSpecBoolean::builder("use-link-headers")
                    .nick("Use Link Headers")
                    .blurb("Use link headers to configure STUN/TURN servers if present in WHEP endpoint response.")
                    .build(),
                glib::ParamSpecString::builder("auth-token")
                    .nick("Authorization Token")
                    .blurb("Authentication token to use, will be sent in the HTTP Header as 'Bearer <auth-token>'")
                    .build(),
                glib::ParamSpecUInt::builder("timeout")
                    .nick("Timeout")
                    .blurb("Value in seconds to timeout WHEP endpoint requests (0 = No timeout).")
                    .maximum(3600)
                    .default_value(DEFAULT_TIMEOUT)
                    .readwrite()
                    .build(),
                glib::ParamSpecBoolean::builder("manual-sdp-munging")
                    .nick("Manual SDP munging")
                    .blurb("Whether the signaller manages SDP munging itself")
                    .default_value(false)
                    .read_only()
                    .build(),
            ]
        });
        PROPERTIES.as_ref()
    }

    fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
        match pspec.name() {
            "whep-endpoint" => {
                let mut settings = self.settings.lock().unwrap();
                settings.whep_endpoint = value.get().expect("WHEP endpoint should be a string");
            }
            "use-link-headers" => {
                let mut settings = self.settings.lock().unwrap();
                settings.use_link_headers = value
                    .get()
                    .expect("use-link-headers should be a boolean value");
            }
            "auth-token" => {
                let mut settings = self.settings.lock().unwrap();
                settings.auth_token = value.get().expect("Auth token should be a string");
            }
            "timeout" => {
                let mut settings = self.settings.lock().unwrap();
                settings.timeout = value.get().expect("type checked upstream");
            }
            _ => unimplemented!(),
        }
    }

    fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
        match pspec.name() {
            "whep-endpoint" => {
                let settings = self.settings.lock().unwrap();
                settings.whep_endpoint.to_value()
            }
            "use-link-headers" => {
                let settings = self.settings.lock().unwrap();
                settings.use_link_headers.to_value()
            }
            "auth-token" => {
                let settings = self.settings.lock().unwrap();
                settings.auth_token.to_value()
            }
            "timeout" => {
                let settings = self.settings.lock().unwrap();
                settings.timeout.to_value()
            }
            "manual-sdp-munging" => false.to_value(),
            _ => unimplemented!(),
        }
    }
}

#[glib::object_subclass]
impl ObjectSubclass for WhepClient {
    const NAME: &'static str = "GstWhepClientSignaller";
    type Type = super::WhepClientSignaller;
    type ParentType = glib::Object;
    type Interfaces = (Signallable,);
}

impl WhepClient {
    fn raise_error(&self, msg: String) {
        self.obj()
            .emit_by_name::<()>("error", &[&format!("Error: {msg}")]);
    }

    fn handle_future_error(&self, err: WaitError) {
        match err {
            WaitError::FutureAborted => {
                gst::warning!(CAT, imp = self, "Future aborted")
            }
            WaitError::FutureError(err) => self.raise_error(err.to_string()),
        };
    }

    fn sdp_message_parse(&self, sdp_bytes: Bytes, server_sent_offer: bool) {
        let sdp = match sdp_message::SDPMessage::parse_buffer(&sdp_bytes) {
            Ok(sdp) => sdp,
            Err(_) => {
                self.raise_error("Could not parse answer SDP".to_string());
                return;
            }
        };

        let remote_sdp = WebRTCSessionDescription::new(
            if server_sent_offer {
                WebRTCSDPType::Offer
            } else {
                WebRTCSDPType::Answer
            },
            sdp,
        );

        if !server_sent_offer {
            self.obj()
                .emit_by_name::<()>("session-description", &[&WHEP_CLIENT_OFFER, &remote_sdp]);
        } else {
            // end the current session
            if self
                .obj()
                .emit_by_name::<bool>("session-ended", &[&WHEP_CLIENT_OFFER])
            {
                // request a new session for the server sent offer
                gst::debug!(CAT, imp = self, "Requesting new session");
                self.obj()
                    .emit_by_name::<()>("session-started", &[&WHEP_SSO, &WHEP_SSO]);

                gst::debug!(CAT, imp = self, "Sending new session description as Offer");
                self.obj()
                    .emit_by_name::<()>("session-description", &[&WHEP_SSO, &remote_sdp]);
            }
        }
    }

    async fn parse_endpoint_response(
        &self,
        sess_desc: WebRTCSessionDescription,
        resp: reqwest::Response,
        redirects: u8,
        webrtcbin: gst::Element,
    ) {
        let endpoint;
        let use_link_headers;

        {
            let settings = self.settings.lock().unwrap();
            endpoint =
                reqwest::Url::parse(settings.whep_endpoint.as_ref().unwrap().as_str()).unwrap();
            use_link_headers = settings.use_link_headers;
            drop(settings);
        }

        let status = resp.status();

        match status {
            StatusCode::OK | StatusCode::NO_CONTENT => {
                gst::info!(CAT, imp = self, "SDP offer successfully sent");
            }

            StatusCode::CREATED | StatusCode::NOT_ACCEPTABLE => {
                gst::debug!(CAT, imp = self, "Response headers: {:?}", resp.headers());

                if use_link_headers && let Err(e) = set_ice_servers(&webrtcbin, resp.headers()) {
                    self.raise_error(e.to_string());
                    return;
                };

                /* See section 4.2 of the WHEP specification */
                let location = match resp.headers().get(reqwest::header::LOCATION) {
                    Some(location) => location,
                    None => {
                        self.raise_error(
                            "Location header field should be present for WHEP resource URL"
                                .to_string(),
                        );
                        return;
                    }
                };

                let location = match location.to_str() {
                    Ok(loc) => loc,
                    Err(e) => {
                        self.raise_error(format!("Failed to convert location to string: {e}"));
                        return;
                    }
                };

                let url = reqwest::Url::parse(endpoint.as_str()).unwrap();

                gst::debug!(CAT, imp = self, "WHEP resource: {:?}", location);

                let url = match url.join(location) {
                    Ok(joined_url) => joined_url,
                    Err(err) => {
                        self.raise_error(format!("URL join operation failed: {err:?}"));
                        return;
                    }
                };

                let server_sent_offer = status == StatusCode::NOT_ACCEPTABLE;

                match resp.bytes().await {
                    Ok(sdp) => {
                        let mut state = self.state.lock().unwrap();
                        *state = match *state {
                            State::Post { redirects: _r } => {
                                if server_sent_offer {
                                    State::Patch {
                                        patch: PatchType::AnswerCounterOffer {
                                            whep_resource: url.to_string(),
                                        },
                                    }
                                } else {
                                    State::Running {
                                        whep_resource: url.to_string(),
                                    }
                                }
                            }
                            _ => {
                                self.raise_error("Expected to be in POST state".to_string());
                                return;
                            }
                        };
                        drop(state);

                        self.sdp_message_parse(sdp, server_sent_offer)
                    }
                    Err(err) => self.raise_error(err.to_string()),
                }
            }

            status if status.is_redirection() => {
                if redirects < MAX_REDIRECTS {
                    match parse_redirect_location(resp.headers(), &endpoint) {
                        Ok(redirect_url) => {
                            {
                                let mut state = self.state.lock().unwrap();
                                *state = match *state {
                                    State::Post { redirects: _r } => State::Post {
                                        redirects: redirects + 1,
                                    },
                                    /*
                                     * As per section 4.6 of the specification, redirection is
                                     * not required to be supported for the PATCH and DELETE
                                     * requests to the final WHEP resource URL. Only the initial
                                     * POST request may support redirection.
                                     */
                                    State::Running { .. } => {
                                        self.raise_error(
                                            "Unexpected redirection in RUNNING state".to_string(),
                                        );
                                        return;
                                    }
                                    _ => unreachable!(),
                                };
                                drop(state);
                            }

                            gst::warning!(
                                CAT,
                                imp = self,
                                "Redirecting endpoint to {}",
                                redirect_url.as_str()
                            );

                            Box::pin(self.do_post(sess_desc, webrtcbin, redirect_url)).await
                        }
                        Err(e) => self.raise_error(e.to_string()),
                    }
                } else {
                    self.raise_error("Too many redirects. Unable to connect.".to_string());
                }
            }

            s => {
                match resp.bytes().await {
                    Ok(r) => {
                        let res = r.escape_ascii().to_string();

                        // FIXME: Check and handle 'Retry-After' header in case of server error
                        self.raise_error(format!("Unexpected response: {} - {}", s.as_str(), res));
                    }
                    Err(err) => self.raise_error(err.to_string()),
                }
            }
        }
    }

    async fn whep_offer(&self, webrtcbin: gst::Element) {
        let local_desc =
            webrtcbin.property::<Option<WebRTCSessionDescription>>("local-description");

        let sess_desc = match local_desc {
            None => {
                self.raise_error("Local description is not set".to_string());
                return;
            }
            Some(mut local_desc) => {
                local_desc.set_type(WebRTCSDPType::Offer);
                local_desc
            }
        };

        gst::debug!(
            CAT,
            imp = self,
            "Sending offer SDP: {}",
            sess_desc.sdp().to_string()
        );

        let timeout;
        let endpoint;

        {
            let settings = self.settings.lock().unwrap();
            timeout = settings.timeout;
            endpoint =
                reqwest::Url::parse(settings.whep_endpoint.as_ref().unwrap().as_str()).unwrap();
            drop(settings);
        }

        if let Err(e) = wait_async(
            &self.canceller,
            self.do_post(sess_desc, webrtcbin, endpoint),
            timeout,
        )
        .await
        {
            self.handle_future_error(e);
        }
    }

    // SDP answer to the counter offser sent by the server
    async fn answer_sso(&self, webrtcbin: gst::Element) {
        let local_desc =
            webrtcbin.property::<Option<WebRTCSessionDescription>>("local-description");
        let timeout = self.settings.lock().unwrap().timeout;
        let sess_desc = match local_desc {
            None => {
                self.raise_error("Local description is not set".to_string());
                return;
            }
            Some(mut local_desc) => {
                local_desc.set_type(WebRTCSDPType::Answer);
                local_desc
            }
        };

        gst::debug!(
            CAT,
            imp = self,
            "Sending answer SDP: {}",
            sess_desc.sdp().to_string()
        );

        if let Err(e) = wait_async(&self.canceller, self.do_patch(sess_desc), timeout).await {
            self.handle_future_error(e);
        }
    }

    async fn do_patch(&self, offer: WebRTCSessionDescription) -> Result<(), gst::ErrorMessage> {
        let resource_url = {
            let state = self.state.lock().unwrap();
            match &*state {
                State::Patch { patch: p } => match p {
                    PatchType::AnswerCounterOffer { whep_resource } => whep_resource.clone(),
                },
                State::Running { whep_resource: _ } => {
                    return Err(gst::error_msg!(
                        gst::ResourceError::Failed,
                        ["Trying to do PATCH in Running state"]
                    ));
                }
                _ => {
                    gst::warning!(
                        CAT,
                        imp = self,
                        "Not allowed to do PATCH request in {} state",
                        &*state
                    );
                    return Ok(());
                }
            }
        };

        let auth_token = self.settings.lock().unwrap().auth_token.clone();

        let sdp = offer.sdp();
        let body = sdp.as_text().unwrap();

        let mut headermap = HeaderMap::new();
        headermap.insert(
            reqwest::header::CONTENT_TYPE,
            HeaderValue::from_static("application/sdp"),
        );

        if let Some(token) = auth_token.as_ref() {
            let bearer_token = format!("Bearer {token}");
            headermap.insert(
                reqwest::header::AUTHORIZATION,
                HeaderValue::from_str(bearer_token.as_str())
                    .expect("Failed to set auth token to header"),
            );
        }

        gst::debug!(
            CAT,
            imp = self,
            "Url for HTTP PATCH request: {resource_url}",
        );

        let resp = self
            .client
            .request(reqwest::Method::PATCH, resource_url)
            .headers(headermap)
            .body(body)
            .send()
            .await;

        match resp {
            Ok(r) => match r.status() {
                StatusCode::OK | StatusCode::NO_CONTENT => {
                    let mut state = self.state.lock().unwrap();
                    if let State::Patch { patch } = &mut *state {
                        match patch {
                            PatchType::AnswerCounterOffer { whep_resource } => {
                                *state = State::Running {
                                    whep_resource: whep_resource.clone(),
                                };
                            }
                        }
                    }
                    gst::debug!(CAT, imp = self, "Server accepted SDP answer",);
                }
                _ => {
                    return Err(gst::error_msg!(
                        gst::ResourceError::Failed,
                        ["Unexpected response from the server: {}", r.status()]
                    ));
                }
            },
            Err(err) => return Err(gst::error_msg!(gst::ResourceError::Failed, ["{err}"])),
        }
        Ok(())
    }

    async fn do_post(
        &self,
        offer: WebRTCSessionDescription,
        webrtcbin: gst::Element,
        endpoint: reqwest::Url,
    ) {
        let redirects = {
            let state = self.state.lock().unwrap();
            match *state {
                State::Post { redirects } => redirects,
                State::Running { whep_resource: _ } => {
                    let err = "Trying to do POST in Running state".to_string();
                    self.raise_error(err);
                    return;
                }
                _ => {
                    gst::warning!(CAT, imp = self, "Not allowed to POST in {} state", &*state);
                    return;
                }
            }
        };

        let auth_token = self.settings.lock().unwrap().auth_token.clone();

        let sdp = offer.sdp();
        let body = sdp.as_text().unwrap();

        gst::info!(CAT, imp = self, "Using endpoint {}", endpoint.as_str());

        let mut headermap = HeaderMap::new();
        headermap.insert(
            reqwest::header::CONTENT_TYPE,
            HeaderValue::from_static("application/sdp"),
        );

        if let Some(token) = auth_token.as_ref() {
            let bearer_token = "Bearer ".to_owned() + token.as_str();
            headermap.insert(
                reqwest::header::AUTHORIZATION,
                HeaderValue::from_str(bearer_token.as_str())
                    .expect("Failed to set auth token to header"),
            );
        }

        gst::debug!(
            CAT,
            imp = self,
            "Url for HTTP POST request: {}",
            endpoint.as_str()
        );

        let resp = self
            .client
            .request(reqwest::Method::POST, endpoint.clone())
            .headers(headermap)
            .body(body)
            .send()
            .await;

        match resp {
            Ok(r) => {
                self.parse_endpoint_response(offer, r, redirects, webrtcbin)
                    .await
            }
            Err(err) => self.raise_error(err.to_string()),
        }
    }

    fn terminate_session(&self, resource_url: &String) {
        let settings = self.settings.lock().unwrap();
        let timeout = settings.timeout;

        let mut headermap = HeaderMap::new();
        if let Some(token) = &settings.auth_token {
            let bearer_token = "Bearer ".to_owned() + token.as_str();
            headermap.insert(
                reqwest::header::AUTHORIZATION,
                HeaderValue::from_str(bearer_token.as_str())
                    .expect("Failed to set auth token to header"),
            );
        }

        drop(settings);

        gst::debug!(CAT, imp = self, "DELETE request on {}", resource_url);

        /* DELETE request goes to the WHEP resource URL. See section 3 of the specification. */
        let client = build_reqwest_client(reqwest::redirect::Policy::default());
        let future = async {
            client
                .delete(resource_url.clone())
                .headers(headermap)
                .send()
                .await
                .map_err(|err| {
                    gst::error_msg!(
                        gst::ResourceError::Failed,
                        ["DELETE request failed {}: {:?}", resource_url, err]
                    )
                })
        };

        let res = wait(&self.canceller, future, timeout);
        match res {
            Ok(r) => {
                gst::debug!(CAT, imp = self, "Response to DELETE : {}", r.status());
            }
            Err(e) => match e {
                WaitError::FutureAborted => {
                    gst::warning!(CAT, imp = self, "DELETE request aborted")
                }
                WaitError::FutureError(e) => {
                    gst::error!(CAT, imp = self, "Error on DELETE request : {}", e)
                }
            },
        };
    }

    pub fn on_webrtcbin_ready(&self) -> RustClosure {
        glib::closure!(|signaller: &super::WhepClientSignaller,
                        _consumer_identifier: &str,
                        webrtcbin: &gst::Element| {
            webrtcbin.connect_notify(
                Some("ice-gathering-state"),
                glib::clone!(
                    #[weak]
                    signaller,
                    move |webrtcbin, _pspec| {
                        let state =
                            webrtcbin.property::<WebRTCICEGatheringState>("ice-gathering-state");

                        match state {
                            WebRTCICEGatheringState::Gathering => {
                                gst::info!(CAT, obj = signaller, "ICE gathering started");
                            }
                            WebRTCICEGatheringState::Complete => {
                                gst::info!(CAT, obj = signaller, "ICE gathering complete");

                                let webrtcbin = webrtcbin.clone();

                                RUNTIME.spawn(async move {
                                    signaller.imp().on_ice_gathering_complete(webrtcbin).await
                                });
                            }
                            _ => (),
                        }
                    }
                ),
            );
        })
    }

    async fn on_ice_gathering_complete(&self, webrtcbin: gst::Element) {
        let state = self.state.lock().unwrap().clone();

        // Reading the state here only to decide what request (POST/PATCH) to make.
        // We again check the current state before doing the request to ensure the
        // session is not terminated
        match state {
            State::Patch { patch: p } => match p.clone() {
                PatchType::AnswerCounterOffer { whep_resource: _ } => {
                    self.answer_sso(webrtcbin).await
                }
            },
            State::Post { redirects: _ } => self.whep_offer(webrtcbin).await,
            _ => {}
        }
    }
}

impl SignallableImpl for WhepClient {
    fn start(&self) {
        if self.settings.lock().unwrap().whep_endpoint.is_none() {
            self.raise_error("WHEP endpoint URL must be set".to_string());
            return;
        }

        let mut state = self.state.lock().unwrap();
        *state = State::Post { redirects: 0 };
        drop(state);

        let this_weak = self.downgrade();

        // the state lock will be already held at this point in the `maybe_start_signaller` of BaseWebRTCSrc
        // and the `start_session` will also need the state mutex to check for existing sessions with same id
        // so spawn a new thread and emit the signals inside it, so that there won't be a deadlock
        RUNTIME.spawn(async move {
            if let Some(this) = this_weak.upgrade() {
                this.obj().emit_by_name::<()>(
                    "session-started",
                    &[&WHEP_CLIENT_OFFER, &WHEP_CLIENT_OFFER],
                );

                this.obj().emit_by_name::<()>(
                    "session-requested",
                    &[
                        &WHEP_CLIENT_OFFER,
                        &WHEP_CLIENT_OFFER,
                        &None::<gst_webrtc::WebRTCSessionDescription>,
                    ],
                );
            }
        });
    }

    fn stop(&self) {}

    fn end_session(&self, session_id: &str) {
        gst::debug!(CAT, imp = self, " ending session {session_id}");
        // Interrupt requests in progress, if any
        if let Some(canceller) = &*self.canceller.lock().unwrap() {
            canceller.abort();
        }

        let mut state = self.state.lock().unwrap();
        match &*state {
            State::Running { whep_resource } => {
                // Release server-side resources
                self.terminate_session(whep_resource);
                *state = State::Stopped;
            }
            State::Patch { patch } => match patch {
                PatchType::AnswerCounterOffer { whep_resource } => {
                    if session_id == WHEP_SSO {
                        self.terminate_session(whep_resource);
                        *state = State::Stopped;
                    }
                }
            },
            _ => {}
        };
    }
}