opentalk-roomserver-module-livekit 0.0.34

OpenTalk RoomServer LiveKit signaling module
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
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: OpenTalk Team <mail@opentalk.eu>

use std::{
    collections::{BTreeMap, BTreeSet, btree_map::Entry},
    sync::Arc,
};

use anyhow::Context as _;
use futures::FutureExt as _;
use livekit_api::{
    access_token::{AccessToken, VideoGrants},
    services::{ServiceError, TwirpError, TwirpErrorCode, room::RoomClient},
};
use livekit_protocol::TrackSource;
use opentalk_roomserver_signaling::{
    module_context::{ChannelDroppedError, ModuleContext},
    signaling_module::{ModuleJoinData, PeerDataMap, SignalingModule},
};
use opentalk_roomserver_types::{
    connection_id::ConnectionId, room_kind::RoomKind, signaling::module_error::SignalingModuleError,
};
use opentalk_roomserver_types_livekit::{
    Credentials, LiveKitError, LiveKitEvent, LiveKitSettings, LiveKitState,
    MicrophoneRestrictionError, MicrophoneRestrictionErrorKind, MicrophoneRestrictionState,
};
use opentalk_types_common::rooms::RoomId;
use opentalk_types_signaling::ParticipantId;
use tokio::sync::{Mutex, oneshot};

use crate::{
    ACCESS_TOKEN_TTL, LIVEKIT_MEDIA_SOURCES, LiveKitModule, build_livekit_participant_id, loopback,
};

#[derive(Debug, Clone)]
pub struct LiveKitSubroom {
    /// The default screenshare permission. If the moderator didn't explicitly set a policy,
    /// this will be used to grant or deny screensharing privileges.
    ///
    /// `True` - all participants are allowed to screenshare
    /// `False` - only moderators are allowed to screenshare
    default_screenshare_permission: bool,

    settings: Arc<LiveKitSettings>,

    /// LiveKit API client used to communicate with the LiveKit server
    livekit_client: Arc<RoomClient>,
    /// The record of all issued tokens for each participant.
    token_identities: BTreeMap<(ParticipantId, ConnectionId), BTreeSet<String>>,

    /// Activating the microphone can be restricted by the moderator. A subset of users might still
    /// be allowed to unmute.
    microphone_restrictions: MicrophoneRestrictionState,

    /// There must only be one background task updating the microphone restrictions at any given
    /// time.
    ongoing_microphone_restrictions: Arc<Mutex<()>>,

    subroom_id: String,
}

impl LiveKitSubroom {
    pub fn new(
        ctx: &mut ModuleContext<'_, LiveKitModule>,
        default_screenshare_permission: bool,
        settings: Arc<LiveKitSettings>,
        livekit_client: Arc<RoomClient>,
        room_id: RoomId,
        room_kind: RoomKind,
    ) -> Self {
        let subroom_id = build_subroom_id(room_id, room_kind);
        {
            let subroom_id = subroom_id.clone();
            let livekit_client = Arc::clone(&livekit_client);
            ctx.spawn(loopback::create_room(livekit_client, subroom_id));
        }

        Self {
            default_screenshare_permission,
            settings,
            livekit_client,
            token_identities: BTreeMap::new(),
            microphone_restrictions: MicrophoneRestrictionState::Disabled,
            ongoing_microphone_restrictions: Arc::new(Mutex::new(())),
            subroom_id,
        }
    }

    pub fn join_info(
        &mut self,
        ctx: &mut ModuleContext<'_, LiveKitModule>,
        participant_id: ParticipantId,
        connection_id: ConnectionId,
    ) -> Result<ModuleJoinData<LiveKitModule>, SignalingModuleError<LiveKitError>> {
        let credentials = self.create_new_access_token(ctx, participant_id, connection_id)?;
        Ok(ModuleJoinData {
            join_success: Some(LiveKitState {
                credentials,
                microphone_restriction_state: self.microphone_restrictions.clone(),
            }),
            peer_events: PeerDataMap::default(),
            peer_data: PeerDataMap::default(),
        })
    }

    pub fn identifier(&self) -> &str {
        &self.subroom_id
    }

    #[tracing::instrument(level = "debug", skip(self, ctx), fields(room = self.subroom_id))]
    pub fn create_new_access_token(
        &mut self,
        ctx: &mut ModuleContext<'_, LiveKitModule>,
        participant: ParticipantId,
        connection: ConnectionId,
    ) -> Result<Credentials, SignalingModuleError<<LiveKitModule as SignalingModule>::Error>> {
        let mut available_sources = LIVEKIT_MEDIA_SOURCES.to_vec();
        if let MicrophoneRestrictionState::Enabled {
            unrestricted_participants,
        } = &self.microphone_restrictions
            && !ctx.is_moderator(participant)
            && !unrestricted_participants.contains(&participant)
        {
            available_sources.retain(|s| s != &TrackSource::Microphone);
        }

        if !self.default_screenshare_permission && !ctx.is_moderator(participant) {
            available_sources
                .retain(|s| s != &TrackSource::ScreenShare && s != &TrackSource::ScreenShareAudio);
        }

        let can_publish_sources = available_sources
            .into_iter()
            .map(|s| TrackSource::as_str_name(&s).to_lowercase())
            .collect();

        let identity = build_livekit_participant_id(participant, connection);

        let hidden = !ctx
            .participant_state(participant)
            .with_context(|| format!("Participant '{participant}' has no state"))?
            .is_visible();

        let token = AccessToken::with_api_key(&self.settings.api_key, &self.settings.api_secret)
            .with_name(&identity)
            .with_identity(&identity)
            .with_grants(VideoGrants {
                room_create: true,
                room_list: false,
                room_record: false,
                room_admin: false,
                room_join: true,
                room: self.identifier().to_string(),
                can_publish: true,
                can_subscribe: true,
                can_publish_data: false,
                can_publish_sources,
                can_update_own_metadata: false,
                ingress_admin: false,
                hidden,
                recorder: false,
                destination_room: String::new(),
            })
            .with_ttl(ACCESS_TOKEN_TTL)
            .to_jwt()
            .context("Failed to create LiveKit access-token")?;
        self.token_identities
            .entry((participant, connection))
            .or_default()
            .insert(identity);

        Ok(Credentials {
            room: self.identifier().to_string(),
            token,
            public_url: self.settings.public_url.clone(),
            service_url: None,
        })
    }

    #[tracing::instrument(level = "debug", skip(self), fields(room = self.subroom_id))]
    pub fn note_revoked_tokens(
        &mut self,
        revoked_token_identities: BTreeSet<String>,
        participant_id: ParticipantId,
        connection_id: ConnectionId,
    ) -> Result<(), SignalingModuleError<LiveKitError>> {
        let entry = self.token_identities.entry((participant_id, connection_id));

        if let Entry::Occupied(mut occupied) = entry {
            occupied
                .get_mut()
                .retain(|item| !revoked_token_identities.contains(item));
            if occupied.get().is_empty() {
                occupied.remove();
            }
        }
        Ok(())
    }

    #[tracing::instrument(level = "debug", skip(self, ctx), fields(room = self.subroom_id))]
    pub fn issue_popout_stream_access_token(
        &mut self,
        ctx: &mut ModuleContext<'_, LiveKitModule>,
        participant_id: ParticipantId,
        connection_id: ConnectionId,
    ) -> Result<(), SignalingModuleError<LiveKitError>> {
        let identity = format!(
            "{}-popout{}",
            build_livekit_participant_id(participant_id, connection_id),
            self.token_identities
                .get(&(participant_id, connection_id))
                .map(BTreeSet::len)
                .unwrap_or_default()
        );

        let token = AccessToken::with_api_key(&self.settings.api_key, &self.settings.api_secret)
            .with_name(&identity)
            .with_identity(&identity)
            .with_grants(VideoGrants {
                room_create: false,
                room_list: false,
                room_record: false,
                room_admin: false,
                room_join: true,
                room: self.identifier().to_string(),
                can_publish: false,
                can_subscribe: true,
                can_publish_data: false,
                can_publish_sources: vec![],
                can_update_own_metadata: false,
                ingress_admin: false,
                hidden: true,
                recorder: false,
                destination_room: String::new(),
            })
            .with_ttl(ACCESS_TOKEN_TTL)
            .to_jwt()
            .map_err(|err| {
                tracing::error!("failed to create popout stream access token: {}", err);
                LiveKitError::LivekitUnavailable
            })?;

        self.token_identities
            .entry((participant_id, connection_id))
            .or_default()
            .insert(identity);

        ctx.send_ws_message(
            [participant_id],
            LiveKitEvent::PopoutStreamAccessToken { token },
        )?;

        Ok(())
    }

    #[tracing::instrument(level = "debug", skip(self, ctx), fields(room = self.subroom_id))]
    pub fn update_microphone_restrictions(
        &mut self,
        ctx: &mut ModuleContext<'_, LiveKitModule>,
        sender: ParticipantId,
        new_state: MicrophoneRestrictionState,
        return_channel: oneshot::Sender<
            Result<MicrophoneRestrictionState, MicrophoneRestrictionError>,
        >,
    ) -> Result<(), ChannelDroppedError> {
        let local_lock = Arc::clone(&self.ongoing_microphone_restrictions);
        let Ok(guard) = local_lock.try_lock_owned() else {
            tracing::debug!(
                "Received microphone restriction request during ongoing restriction update"
            );
            return return_channel
                .send(Err(MicrophoneRestrictionError {
                    sender,
                    error: MicrophoneRestrictionErrorKind::ConflictingTask,
                }))
                .map_err(|_| ChannelDroppedError);
        };

        let room = self.identifier().to_string();
        let livekit_client = Arc::clone(&self.livekit_client);
        let connections = ctx.participants.connections();

        // update the state now so that the rule is already applied to new participants.
        self.microphone_restrictions = new_state.clone();

        ctx.spawn_optional({
            loopback::update_restricted_microphones(
                livekit_client,
                room,
                sender,
                new_state,
                connections,
            )
            .map(move |res| {
                drop(guard);
                if return_channel.send(res).is_err() {
                    tracing::error!("Channel dropped when sending microphone restriction result");
                }
                None
            })
        });

        Ok(())
    }

    fn notify_unknown_participants(
        ctx: &mut ModuleContext<'_, LiveKitModule>,
        unknown_participants: BTreeSet<ParticipantId>,
        sender: ParticipantId,
    ) -> Result<(), SignalingModuleError<LiveKitError>> {
        if !unknown_participants.is_empty() {
            ctx.send_ws_message(
                [sender],
                LiveKitError::UnknownParticipant {
                    participant: unknown_participants,
                }
                .into(),
            )?;
        }
        Ok(())
    }

    #[tracing::instrument(level = "debug", skip(self, ctx), fields(room = self.subroom_id))]
    pub(crate) fn start_revoke_participant_access(
        &self,
        ctx: &mut ModuleContext<'_, LiveKitModule>,
        participant_id: ParticipantId,
        connection_id: ConnectionId,
    ) {
        // When a participant leaves, we revoke their access to livekit.
        // The identities will be removed from the set when the loopback task was successful.
        let Some(token_identities) = self
            .token_identities
            .get(&(participant_id, connection_id))
            .cloned()
        else {
            tracing::warn!("No livekit token identities found");
            return;
        };
        let livekit_client = self.livekit_client.clone();

        ctx.spawn(loopback::revoke_token(
            livekit_client,
            participant_id,
            connection_id,
            self.identifier().to_string(),
            token_identities,
        ));
    }

    #[tracing::instrument(level = "debug", skip(self, ctx), fields(room = self.subroom_id))]
    pub(crate) fn set_screenshare_permissions(
        &self,
        ctx: &mut ModuleContext<'_, LiveKitModule>,
        sender: ParticipantId,
        participants: BTreeSet<ParticipantId>,
        grant: bool,
    ) -> Result<(), SignalingModuleError<LiveKitError>> {
        if !ctx.is_moderator(sender) {
            tracing::debug!(
                "Participant has insufficient permission to grant screen sharing rights: {sender}"
            );
            return Err(LiveKitError::InsufficientPermissions.into());
        }

        // check that we know all participants and query their connection ids
        let known_participants: BTreeSet<_> = ctx
            .participants
            .connected()
            .ids()
            .filter(|p| participants.contains(p))
            .collect();
        let unknown_participants: BTreeSet<_> = participants
            .difference(&known_participants)
            .copied()
            .collect();

        let mut connections = ctx.participants.connections();
        connections.retain(|p, _| known_participants.contains(p));
        Self::notify_unknown_participants(ctx, unknown_participants, sender)?;

        ctx.spawn(loopback::set_screenshare_permissions(
            Arc::clone(&self.livekit_client),
            self.identifier().to_string(),
            sender,
            connections,
            grant,
        ));

        Ok(())
    }

    #[tracing::instrument(level = "debug", skip_all, fields(room = self.subroom_id))]
    pub async fn cleanup_room(self) {
        match self.livekit_client.delete_room(self.identifier()).await {
            Ok(()) => {
                tracing::debug!("Destroyed livekit room");
            }
            Err(ServiceError::Twirp(TwirpError::Twirp(code)))
                if code.code == TwirpErrorCode::NOT_FOUND =>
            {
                tracing::debug!("Livekit room was already destroyed");
            }
            Err(e) => {
                tracing::error!("Failed to destroy livekit room: {}", e);
            }
        }
    }
}

fn build_subroom_id(room_id: RoomId, room_kind: RoomKind) -> String {
    match room_kind {
        RoomKind::Breakout(breakout_id) => format!("{room_id}:{breakout_id}"),
        RoomKind::Main => format!("{room_id}:main"),
    }
}

/// Information about a participant's connection to a LiveKit room.
pub struct LiveKitConnection {
    pub participant_id: ParticipantId,
    pub livekit_participant_id: String,
    pub livekit_room: String,
}

impl LiveKitConnection {
    pub fn new(
        participant_id: ParticipantId,
        connection_id: ConnectionId,
        room_id: RoomId,
        room_kind: RoomKind,
    ) -> LiveKitConnection {
        Self {
            participant_id,
            livekit_participant_id: build_livekit_participant_id(participant_id, connection_id),
            livekit_room: build_subroom_id(room_id, room_kind),
        }
    }
}