lib-gst-meet 0.2.0

Connect GStreamer pipelines to Jitsi Meet conferences
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
use std::{collections::HashMap, convert::TryFrom, fmt, future::Future, pin::Pin, sync::Arc};

use anyhow::{anyhow, bail, Context, Result};
use async_trait::async_trait;
use futures::stream::StreamExt;
use glib::ObjectExt;
use gstreamer::prelude::{ElementExt, ElementExtManual, GstBinExt};
use once_cell::sync::Lazy;
use tokio::sync::{mpsc, oneshot, Mutex};
use tokio_stream::wrappers::ReceiverStream;
use tracing::{debug, error, info, trace, warn};
use xmpp_parsers::{
  disco::{DiscoInfoQuery, DiscoInfoResult, Feature},
  ecaps2::{self, ECaps2},
  hashes::Algo,
  iq::{Iq, IqType},
  jingle::{Action, Jingle},
  muc::{Muc, MucUser},
  ns,
  presence::{self, Presence},
  BareJid, Element, FullJid, Jid,
};

use crate::{jingle::JingleSession, source::MediaType, stanza_filter::StanzaFilter, xmpp};

static DISCO_INFO: Lazy<DiscoInfoResult> = Lazy::new(|| DiscoInfoResult {
  node: None,
  identities: vec![],
  features: vec![
    Feature::new(ns::JINGLE_RTP_AUDIO),
    Feature::new(ns::JINGLE_RTP_VIDEO),
    Feature::new(ns::JINGLE_ICE_UDP),
    Feature::new(ns::JINGLE_DTLS),
    // not supported yet: rtx
    // Feature::new("urn:ietf:rfc:4588"),

    // not supported yet: rtcp remb
    // Feature::new("http://jitsi.org/remb"),

    // not supported yet: transport-cc
    // Feature::new("http://jitsi.org/tcc"),

    // rtcp-mux
    Feature::new("urn:ietf:rfc:5761"),
    // rtp-bundle
    Feature::new("urn:ietf:rfc:5888"),
    // opus red
    Feature::new("http://jitsi.org/opus-red"),
  ],
  extensions: vec![],
});

#[derive(Debug, Clone, Copy)]
enum JitsiConferenceState {
  Discovering,
  JoiningMuc,
  Idle,
}

#[derive(Debug, Clone)]
pub struct JitsiConferenceConfig {
  pub muc: BareJid,
  pub focus: FullJid,
  pub nick: String,
  pub region: String,
  pub video_codec: String,
}

#[derive(Clone)]
pub struct JitsiConference {
  pub(crate) glib_main_context: glib::MainContext,
  pub(crate) jid: FullJid,
  pub(crate) xmpp_tx: mpsc::Sender<Element>,
  pub(crate) config: JitsiConferenceConfig,
  pub(crate) external_services: Vec<xmpp::extdisco::Service>,
  pub(crate) inner: Arc<Mutex<JitsiConferenceInner>>,
}

impl fmt::Debug for JitsiConference {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("JitsiConference")
      .field("jid", &self.jid)
      .field("config", &self.config)
      .field("inner", &self.inner)
      .finish()
  }
}

#[derive(Debug, Clone)]
pub struct Participant {
  pub jid: FullJid,
  pub muc_jid: FullJid,
  pub nick: Option<String>,
  bin: Option<gstreamer::Bin>,
}

pub(crate) struct JitsiConferenceInner {
  pub(crate) jingle_session: Option<JingleSession>,
  participants: HashMap<String, Participant>,
  on_participant: Option<
    Arc<
      dyn (Fn(Participant) -> Pin<Box<dyn Future<Output = Result<Option<gstreamer::Bin>>> + Send>>)
        + Send
        + Sync,
    >,
  >,
  state: JitsiConferenceState,
  connected_tx: Option<oneshot::Sender<()>>,
  connected_rx: Option<oneshot::Receiver<()>>,
}

impl fmt::Debug for JitsiConferenceInner {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("JitsiConferenceInner")
      .field("state", &self.state)
      .finish()
  }
}

impl JitsiConference {
  #[tracing::instrument(level = "debug", skip(xmpp_tx), err)]
  pub(crate) async fn new(
    glib_main_context: glib::MainContext,
    jid: FullJid,
    xmpp_tx: mpsc::Sender<Element>,
    config: JitsiConferenceConfig,
    external_services: Vec<xmpp::extdisco::Service>,
  ) -> Result<Self> {
    let (tx, rx) = oneshot::channel();
    Ok(Self {
      glib_main_context,
      jid,
      xmpp_tx,
      config,
      external_services,
      inner: Arc::new(Mutex::new(JitsiConferenceInner {
        state: JitsiConferenceState::Discovering,
        participants: HashMap::new(),
        on_participant: None,
        jingle_session: None,
        connected_tx: Some(tx),
        connected_rx: Some(rx),
      })),
    })
  }

  pub async fn connected(&self) -> Result<()> {
    let rx = {
      let mut locked_inner = self.inner.lock().await;
      locked_inner
        .connected_rx
        .take()
        .context("connected() called twice")?
    };
    rx.await?;
    Ok(())
  }

  #[tracing::instrument(level = "debug", err)]
  pub async fn leave(self) -> Result<()> {
    let mut inner = self.inner.lock().await;

    if let Some(jingle_session) = inner.jingle_session.take() {
      debug!("pausing all sinks");
      jingle_session.pause_all_sinks();

      debug!("setting pipeline state to NULL");
      if let Err(e) = jingle_session.pipeline().set_state(gstreamer::State::Null) {
        warn!("failed to set pipeline state to NULL: {:?}", e);
      }

      debug!("waiting for state change to complete");
      let _ = jingle_session.pipeline_stopped().await;
    }

    // should leave the XMPP muc gracefully, instead of just disconnecting

    Ok(())
  }

  fn jid_in_muc(&self) -> Result<FullJid> {
    let resource = self
      .jid
      .node
      .as_ref()
      .ok_or_else(|| anyhow!("invalid jid"))?
      .split('-')
      .next()
      .ok_or_else(|| anyhow!("invalid jid"))?;
    Ok(self.config.muc.clone().with_resource(resource))
  }

  pub(crate) fn focus_jid_in_muc(&self) -> Result<FullJid> {
    Ok(self.config.muc.clone().with_resource("focus"))
  }

  #[tracing::instrument(level = "debug", err)]
  async fn send_presence(&self, payloads: Vec<Element>) -> Result<()> {
    let mut presence = Presence::new(presence::Type::None).with_to(self.jid_in_muc()?);
    presence.payloads = payloads;
    self.xmpp_tx.send(presence.into()).await?;
    Ok(())
  }

  #[tracing::instrument(level = "debug", err)]
  pub async fn set_muted(&self, media_type: MediaType, muted: bool) -> Result<()> {
    self
      .send_presence(vec![
        Element::builder(media_type.jitsi_muted_presence_element_name(), "")
          .append(muted.to_string())
          .build(),
      ])
      .await
  }

  pub async fn pipeline(&self) -> Result<gstreamer::Pipeline> {
    Ok(
      self
        .inner
        .lock()
        .await
        .jingle_session
        .as_ref()
        .context("not connected (no jingle session)")?
        .pipeline(),
    )
  }

  #[tracing::instrument(level = "debug", err)]
  pub async fn add_bin(&self, bin: &gstreamer::Bin) -> Result<()> {
    let pipeline = self.pipeline().await?;
    pipeline.add(bin)?;
    bin.sync_state_with_parent()?;
    Ok(())
  }

  #[tracing::instrument(level = "debug", err)]
  pub async fn set_pipeline_state(&self, state: gstreamer::State) -> Result<()> {
    self.pipeline().await?.call_async(move |p| {
      if let Err(e) = p.set_state(state) {
        error!("pipeline set_state: {:?}", e);
      }
    });
    Ok(())
  }

  pub async fn audio_sink_element(&self) -> Result<gstreamer::Element> {
    Ok(
      self
        .inner
        .lock()
        .await
        .jingle_session
        .as_ref()
        .context("not connected (no jingle session)")?
        .audio_sink_element(),
    )
  }

  pub async fn video_sink_element(&self) -> Result<gstreamer::Element> {
    Ok(
      self
        .inner
        .lock()
        .await
        .jingle_session
        .as_ref()
        .context("not connected (no jingle session)")?
        .video_sink_element(),
    )
  }

  #[tracing::instrument(level = "trace", skip(f))]
  pub async fn on_participant(
    &self,
    f: impl (Fn(Participant) -> Pin<Box<dyn Future<Output = Result<Option<gstreamer::Bin>>> + Send>>)
    + Send
    + Sync
    + 'static,
  ) {
    let f = Arc::new(f);
    let f2 = f.clone();
    let existing_participants: Vec<_> = {
      let mut locked_inner = self.inner.lock().await;
      locked_inner.on_participant = Some(f2);
      locked_inner.participants.values().cloned().collect()
    };
    for participant in existing_participants {
      debug!(
        "calling on_participant with existing participant: {:?}",
        participant
      );
      match f(participant.clone()).await {
        Ok(Some(bin)) => {
          bin
            .set_property(
              "name",
              format!("participant_{}", participant.muc_jid.resource),
            )
            .unwrap();
          match self.add_bin(&bin).await {
            Ok(_) => {
              let mut locked_inner = self.inner.lock().await;
              if let Some(p) = locked_inner
                .participants
                .get_mut(&participant.muc_jid.resource)
              {
                p.bin = Some(bin);
              }
            },
            Err(e) => warn!("failed to add participant bin: {:?}", e),
          }
        },
        Ok(None) => {},
        Err(e) => warn!("on_participant failed: {:?}", e),
      }
    }
  }
}

#[async_trait]
impl StanzaFilter for JitsiConference {
  #[tracing::instrument(level = "trace")]
  fn filter(&self, element: &Element) -> bool {
    element.attr("from") == Some(self.config.focus.to_string().as_str())
      && element.is("iq", "jabber:client")
      || element
        .attr("from")
        .and_then(|from| from.parse::<BareJid>().ok())
        .map(|jid| jid == self.config.muc)
        .unwrap_or_default()
        && (element.is("presence", "jabber:client") || element.is("iq", "jabber:client"))
  }

  #[tracing::instrument(level = "trace", err)]
  async fn take(&self, element: Element) -> Result<()> {
    let mut locked_inner = self.inner.lock().await;

    use JitsiConferenceState::*;
    match locked_inner.state {
      Discovering => {
        let iq = Iq::try_from(element)?;
        if let IqType::Result(Some(element)) = iq.payload {
          let ready: bool = element
            .attr("ready")
            .ok_or_else(|| anyhow!("missing ready attribute on conference IQ"))?
            .parse()?;
          if !ready {
            bail!("focus reports room not ready");
          }
        }
        else {
          bail!("focus IQ failed");
        };

        let jitsi_disco_info = DiscoInfoResult {
          node: Some("http://jitsi.org/jitsimeet".to_string()),
          identities: vec![],
          features: vec![],
          extensions: vec![],
        };

        let jitsi_disco_hash =
          ecaps2::hash_ecaps2(&ecaps2::compute_disco(&jitsi_disco_info)?, Algo::Sha_256)?;
        self
          .send_presence(vec![
            Muc::new().into(),
            ECaps2::new(vec![jitsi_disco_hash]).into(),
            Element::builder("stats-id", "").append("gst-meet").build(),
            Element::builder("jitsi_participant_codecType", "")
              .append(self.config.video_codec.as_str())
              .build(),
            Element::builder("jitsi_participant_region", "")
              .append(self.config.region.as_str())
              .build(),
            Element::builder("audiomuted", "").append("false").build(),
            Element::builder("videomuted", "").append("false").build(),
            Element::builder("nick", "http://jabber.org/protocol/nick")
              .append(self.config.nick.as_str())
              .build(),
            Element::builder("region", "http://jitsi.org/jitsi-meet")
              .attr("id", &self.config.region)
              .build(),
          ])
          .await?;
        locked_inner.state = JoiningMuc;
      },
      JoiningMuc => {
        let presence = Presence::try_from(element)?;
        if BareJid::from(presence.from.as_ref().unwrap().clone()) == self.config.muc {
          debug!("Joined MUC: {}", self.config.muc);
          locked_inner.state = Idle;
        }
      },
      Idle => {
        if let Ok(iq) = Iq::try_from(element.clone()) {
          match iq.payload {
            IqType::Get(element) => {
              if let Ok(query) = DiscoInfoQuery::try_from(element) {
                debug!(
                  "Received disco info query from {} for node {:?}",
                  iq.from.as_ref().unwrap(),
                  query.node
                );
                if query.node.is_none() {
                  let iq = Iq::from_result(iq.id, Some(DISCO_INFO.clone()))
                    .with_from(Jid::Full(self.jid.clone()))
                    .with_to(iq.from.unwrap());
                  self.xmpp_tx.send(iq.into()).await?;
                }
                else {
                  panic!("don't know how to handle disco info node: {:?}", query.node);
                }
              }
            },
            IqType::Set(element) => {
              if let Ok(jingle) = Jingle::try_from(element) {
                if let Some(Jid::Full(from_jid)) = iq.from {
                  if jingle.action == Action::SessionInitiate {
                    if from_jid.resource == "focus" {
                      // Acknowledge the IQ
                      let result_iq = Iq::empty_result(Jid::Full(from_jid.clone()), iq.id.clone())
                        .with_from(Jid::Full(self.jid.clone()));
                      self.xmpp_tx.send(result_iq.into()).await?;

                      locked_inner.jingle_session =
                        Some(JingleSession::initiate(self, jingle).await?);
                    }
                    else {
                      debug!("Ignored Jingle session-initiate from {}", from_jid);
                    }
                  }
                  else if jingle.action == Action::SourceAdd {
                    debug!("Received Jingle source-add");

                    // Acknowledge the IQ
                    let result_iq = Iq::empty_result(Jid::Full(from_jid.clone()), iq.id.clone())
                      .with_from(Jid::Full(self.jid.clone()));
                    self.xmpp_tx.send(result_iq.into()).await?;

                    locked_inner
                      .jingle_session
                      .as_mut()
                      .context("not connected (no jingle session")?
                      .source_add(jingle)
                      .await?;
                  }
                }
                else {
                  debug!("Received Jingle IQ from invalid JID: {:?}", iq.from);
                }
              }
              else {
                debug!("Received non-Jingle IQ");
              }
            },
            IqType::Result(_) => {
              if let Some(jingle_session) = locked_inner.jingle_session.as_ref() {
                if Some(iq.id) == jingle_session.accept_iq_id {
                  let colibri_url = jingle_session.colibri_url.clone();

                  locked_inner.jingle_session.as_mut().unwrap().accept_iq_id = None;

                  debug!("Focus acknowledged session-accept");

                  if let Some(colibri_url) = colibri_url {
                    info!("Connecting Colibri WebSocket to {}", colibri_url);

                    let request =
                      tokio_tungstenite::tungstenite::http::Request::get(colibri_url).body(())?;
                    let (colibri_websocket, _response) =
                      tokio_tungstenite::connect_async(request).await?;
                    info!("Connected Colibri WebSocket");

                    let (colibri_sink, mut colibri_stream) = colibri_websocket.split();
                    let colibri_receive_task = tokio::spawn(async move {
                      while let Some(msg) = colibri_stream.next().await {
                        debug!("colibri: {:?}", msg);
                      }
                      Ok::<_, anyhow::Error>(())
                    });
                    let (colibri_tx, colibri_rx) = mpsc::channel(8);
                    locked_inner.jingle_session.as_mut().unwrap().colibri_tx = Some(colibri_tx);
                    let colibri_transmit_task = tokio::spawn(async move {
                      let stream = ReceiverStream::new(colibri_rx);
                      stream.forward(colibri_sink).await?;
                      Ok::<_, anyhow::Error>(())
                    });

                    tokio::spawn(async move {
                      tokio::select! {
                        res = colibri_receive_task => if let Ok(Err(e)) = res {
                          error!("colibri read loop: {:?}", e);
                        },
                        res = colibri_transmit_task => if let Ok(Err(e)) = res {
                          error!("colibri write loop: {:?}", e);
                        },
                      };
                    });
                  }

                  if let Some(connected_tx) = locked_inner.connected_tx.take() {
                    connected_tx.send(()).unwrap();
                  }
                }
              }
            },
            _ => {},
          }
        }
        else if let Ok(presence) = Presence::try_from(element) {
          if let Jid::Full(from) = presence
            .from
            .as_ref()
            .context("missing from in presence")?
            .clone()
          {
            let bare_from: BareJid = from.clone().into();
            if bare_from == self.config.muc && from.resource != "focus" {
              trace!("received MUC presence from {}", from.resource);
              for payload in presence.payloads {
                if !payload.is("x", ns::MUC_USER) {
                  continue;
                }
                let muc_user = MucUser::try_from(payload)?;
                debug!("MUC user presence: {:?}", muc_user);
                for item in muc_user.items {
                  if let Some(jid) = &item.jid {
                    if jid == &self.jid {
                      continue;
                    }
                    let participant = Participant {
                      jid: jid.clone(),
                      muc_jid: from.clone(),
                      nick: item.nick,
                      bin: None,
                    };
                    if locked_inner
                      .participants
                      .insert(from.resource.clone(), participant.clone())
                      .is_none()
                    {
                      debug!("new participant: {:?}", jid);
                      if let Some(f) = &locked_inner.on_participant {
                        debug!("calling on_participant with new participant");
                        match f(participant).await {
                          Ok(Some(bin)) => {
                            bin.set_property("name", format!("participant_{}", from.resource))?;
                            match self.add_bin(&bin).await {
                              Ok(_) => {
                                if let Some(p) = locked_inner.participants.get_mut(&from.resource) {
                                  p.bin = Some(bin);
                                }
                              },
                              Err(e) => warn!("failed to add participant bin: {:?}", e),
                            }
                          },
                          Ok(None) => {},
                          Err(e) => warn!("on_participant failed: {:?}", e),
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
    }
    Ok(())
  }
}