jarust_plugins 1.9.3

A janus client SDK in Rust
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
use crate::video_room::params::*;
use crate::video_room::responses::*;
use crate::JanusId;
use jarust_core::prelude::*;
use jarust_interface::japrotocol::Jsep;
use jarust_rt::JaTask;
use serde_json::json;
use serde_json::Value;
use std::ops::Deref;
use std::time::Duration;

pub struct VideoRoomHandle {
    handle: JaHandle,
    task: Option<JaTask>,
}

//
// synchronous methods
//
impl VideoRoomHandle {
    /// Create a new video room dynamically with the given room number,
    /// as an alternative to using the configuration file
    ///
    /// ### Note:
    /// Random room number will be used if `room` is `None`
    #[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
    pub async fn create_room(
        &self,
        room: Option<JanusId>,
        timeout: Duration,
    ) -> Result<VideoRoomCreatedRsp, jarust_interface::Error> {
        self.create_room_with_config(
            VideoRoomCreateParams {
                room,
                ..Default::default()
            },
            timeout,
        )
        .await
    }

    /// Create a new video room dynamically with the given configuration,
    /// as an alternative to using the configuration file
    ///
    /// ### Note:
    /// Random room number will be used if `room` is `None`
    #[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
    pub async fn create_room_with_config(
        &self,
        params: VideoRoomCreateParams,
        timeout: Duration,
    ) -> Result<VideoRoomCreatedRsp, jarust_interface::Error> {
        tracing::info!(plugin = "videoroom", "Sending create");
        let mut message: Value = params.try_into()?;
        message["request"] = "create".into();

        self.handle
            .send_waiton_rsp::<VideoRoomCreatedRsp>(message, timeout)
            .await
    }

    /// Allows you to dynamically edit some room properties (e.g., the PIN)
    ///
    /// ### Note:
    /// You won't be able to modify other more static properties,
    /// like the room ID, the sampling rate, the extensions-related stuff and so on.
    #[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
    pub async fn edit_room(
        &self,
        params: VideoRoomEditParams,
        timeout: Duration,
    ) -> Result<VideoRoomEditedRsp, jarust_interface::Error> {
        tracing::info!(plugin = "videoroom", "Sending edit");
        let mut message: Value = params.try_into()?;
        message["request"] = "edit".into();

        self.handle
            .send_waiton_rsp::<VideoRoomEditedRsp>(message, timeout)
            .await
    }

    // Destroy an existing video room, whether created dynamically or statically
    #[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
    pub async fn destroy_room(
        &self,
        params: VideoRoomDestroyParams,
        timeout: Duration,
    ) -> Result<VideoRoomDestroyedRsp, jarust_interface::Error> {
        tracing::info!(plugin = "videoroom", "Sending destroy");
        let mut message: Value = params.try_into()?;
        message["request"] = "destroy".into();

        self.handle
            .send_waiton_rsp::<VideoRoomDestroyedRsp>(message, timeout)
            .await
    }

    /// Check whether a room exists
    #[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
    pub async fn exists(
        &self,
        params: VideoRoomExistsParams,
        timeout: Duration,
    ) -> Result<bool, jarust_interface::Error> {
        tracing::info!(plugin = "videoroom", "Sending exists");
        let mut message: Value = params.try_into()?;
        message["request"] = "exists".into();
        let response = self
            .handle
            .send_waiton_rsp::<VideoRoomExistsRsp>(message, timeout)
            .await?;
        Ok(response.exists)
    }

    /// Get a list of the available rooms
    #[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
    pub async fn list_rooms(
        &self,
        timeout: Duration,
    ) -> Result<Vec<Room>, jarust_interface::Error> {
        tracing::info!(plugin = "videoroom", "Sending list");
        let response = self
            .handle
            .send_waiton_rsp::<VideoRoomListRoomsRsp>(json!({"request": "list"}), timeout)
            .await?;

        Ok(response.list)
    }

    /// Allows you to edit who's allowed to join a room via ad-hoc tokens
    #[cfg(feature = "__experimental")]
    #[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
    pub async fn allowed(
        &self,
        params: VideoRoomAllowedParams,
        timeout: Duration,
    ) -> Result<VideoRoomAccessRsp, jarust_interface::Error> {
        if (params.action == VideoRoomAllowedAction::Enable
            || params.action == VideoRoomAllowedAction::Disable)
            && !params.allowed.is_empty()
        {
            return Err(jarust_interface::Error::InvalidJanusRequest {
                reason: "An enable or disable 'allowed' request cannot have its allowed array set"
                    .to_string(),
            });
        }

        tracing::info!(plugin = "videoroom", "Sending allowed");
        let mut message: Value = params.try_into()?;
        message["request"] = "allowed".into();

        self.handle
            .send_waiton_rsp::<VideoRoomAccessRsp>(message, timeout)
            .await
    }

    /// Kicks a participants out of a room
    #[cfg(feature = "__experimental")]
    #[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
    pub async fn kick(
        &self,
        params: VideoRoomKickParams,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        tracing::info!(plugin = "videoroom", "Sending kick");
        let mut message: Value = params.try_into()?;
        message["request"] = "kick".into();

        self.handle.send_waiton_rsp::<()>(message, timeout).await
    }

    /// Enable or disable recording on all participants while the conference is in progress
    #[cfg(feature = "__experimental")]
    #[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
    pub async fn enable_recording(
        &self,
        params: VideoRoomEnableRecordingParams,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        tracing::info!(plugin = "videoroom", "Sending enable recording");
        let mut message: Value = params.try_into()?;
        message["request"] = "enable_recording".into();

        self.handle.send_waiton_rsp::<()>(message, timeout).await
    }

    /// Get a list of the participants in a specific room
    #[cfg(feature = "__experimental")]
    #[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
    pub async fn list_participants(
        &self,
        params: VideoRoomListParticipantsParams,
        timeout: Duration,
    ) -> Result<ListParticipantsRsp, jarust_interface::Error> {
        tracing::info!(plugin = "videoroom", "Sending list participants");
        let mut message: Value = params.try_into()?;
        message["request"] = "listparticipants".into();
        self.handle
            .send_waiton_rsp::<ListParticipantsRsp>(message, timeout)
            .await
    }

    #[cfg(feature = "__experimental")]
    pub async fn moderate(
        &self,
        params: VideoRoomModerateParams,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        let mut message = serde_json::to_value(params)?;
        message["request"] = "moderate".into();

        self.handle.send_waiton_rsp::<()>(message, timeout).await
    }

    #[cfg(feature = "__experimental")]
    pub async fn list_forwarders(
        &self,
        params: VideoRoomListForwardersParams,
        timeout: Duration,
    ) -> Result<VideoRoomListForwardersRsp, jarust_interface::Error> {
        let mut message = serde_json::to_value(params)?;
        message["request"] = "list_forwarders".into();

        self.handle
            .send_waiton_rsp::<VideoRoomListForwardersRsp>(message, timeout)
            .await
    }

    #[cfg(feature = "__experimental")]
    pub async fn rtp_forward(
        &self,
        params: VideoRoomRtpForwardParams,
        timeout: Duration,
    ) -> Result<VideoRoomRtpForwardRsp, jarust_interface::Error> {
        let mut message = serde_json::to_value(params)?;
        message["request"] = "rtp_forward".into();

        self.handle
            .send_waiton_rsp::<VideoRoomRtpForwardRsp>(message, timeout)
            .await
    }

    #[cfg(feature = "__experimental")]
    pub async fn stop_rtp_forward(
        &self,
        params: VideoRoomStopRtpForward,
        timeout: Duration,
    ) -> Result<VideoRoomStopRtpForwardRsp, jarust_interface::Error> {
        let mut message = serde_json::to_value(params)?;
        message["request"] = "stop_rtp_forward".into();
        self.handle
            .send_waiton_rsp::<VideoRoomStopRtpForwardRsp>(message, timeout)
            .await
    }
}

//
// asynchronous methods
//
impl VideoRoomHandle {
    /// Join a room as a publishers
    ///
    /// In a VideoRoom, publishers are those participant handles that are able (although may choose not to)
    /// publish media in the room, and as such become feeds that you can subscribe to.
    /// To specify that a handle will be associated with a publisher, you must use the `join_as_publisher` request
    /// (note that you can also use [`VideoRoomHandle::join_and_configure`] for the purpose).
    ///
    /// A successful join will result in a [`VideoRoomEvent::RoomJoined`](super::events::VideoRoomEvent::RoomJoined) event,
    /// which will contain a list of the currently active (as in publishing via WebRTC) publishers,
    /// and optionally a list of passive attendees (but only if the room was configured with notify_joining set to TRUE)
    pub async fn join_as_publisher(
        &self,
        params: VideoRoomPublisherJoinParams,
        jsep: Option<Jsep>,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        let mut message: Value = params.try_into()?;
        message["request"] = "join".into();
        message["ptype"] = "publisher".into();

        match jsep {
            None => self.handle.send_waiton_ack(message, timeout).await?,
            Some(jsep) => {
                self.handle
                    .send_waiton_ack_with_jsep(message, jsep, timeout)
                    .await?
            }
        };
        Ok(())
    }

    /// Join a room as a subscriber
    ///
    /// In a VideoRoom, subscribers are NOT participants, but simply handles that will be used exclusively to
    /// receive media from one or more publishers in the room. Since they're not participants per se,
    /// they're basically streams that can be (and typically are) associated to publisher handles
    /// as the ones we introduced in the previous section, whether active or not.
    /// In fact, the typical use case is publishers being notified about new participants becoming active in the room,
    /// and as a result new subscriber sessions being created to receive their media streams;
    /// as soon as the publisher goes away, other participants are notified so that the related subscriber handles
    /// can be removed/updated accordingly as well. As such, these subscriber sessions are dependent on feedback
    /// obtained by publishers, and can't exist on their own, unless you feed them the right info out of band
    /// (which is impossible in rooms configured with require_pvtid).
    #[cfg(feature = "__experimental")]
    pub async fn join_as_subscriber(
        &self,
        params: VideoRoomSubscriberJoinParams,
        jsep: Option<Jsep>,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        let mut message: Value = params.try_into()?;
        message["request"] = "join".into();
        message["ptype"] = "subscriber".into();

        match jsep {
            None => self.handle.send_waiton_ack(message, timeout).await?,
            Some(ep) => {
                self.handle
                    .send_waiton_ack_with_jsep(message, ep, timeout)
                    .await?
            }
        };
        Ok(())
    }

    /// Tweak some of the properties of an active publisher session
    ///
    /// It's basically the same properties as those listed for publish , with the addition of a `streams` array that can be used
    /// to tweak individual streams (which is not available when publishing since in that case the stream doesn't exist yet).
    /// Notice that the configure request can also be used in renegotiations, to provide an updated SDP with changes to the published media.
    pub async fn configure_publisher(
        &self,
        params: VideoRoomPublisherConfigureParams,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        let mut message: Value = params.try_into()?;
        message["request"] = "configure".into();
        self.handle.send_waiton_ack(message, timeout).await?;
        Ok(())
    }

    /// This request allows subscribers to dynamically change some properties associated to their media subscription,
    /// e.g., in terms of what should and should not be sent at a specific time.
    #[cfg(feature = "__experimental")]
    pub async fn configure_subscriber(
        &self,
        params: VideoRoomConfigureSubscriberParams,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        let mut message: Value = params.try_into()?;
        message["request"] = "configure".into();
        self.handle.send_waiton_ack(message, timeout).await?;
        Ok(())
    }

    /// A combination of [VideoRoomHandle::join_as_publisher()] and [VideoRoomHandle::configure_publisher()]
    pub async fn publisher_join_and_configure(
        &self,
        params: VideoRoomPublisherJoinAndConfigureParams,
        jsep: Option<Jsep>,
        timeout: Duration,
    ) -> Result<String, jarust_interface::Error> {
        let mut message: Value = params.try_into()?;
        message["request"] = "joinandconfigure".into();
        message["ptype"] = "publisher".into();
        match jsep {
            None => self.handle.send_waiton_ack(message, timeout).await,
            Some(jsep) => {
                self.handle
                    .send_waiton_ack_with_jsep(message, jsep, timeout)
                    .await
            }
        }
    }

    /// Start publishing in a room
    ///
    /// This request MUST be accompanied by a JSEP SDP offer to negotiate a new PeerConnection.
    /// The plugin will match it to the room configuration (e.g., to make sure the codecs you negotiated are allowed in the room),
    /// and will reply with a JSEP SDP answer to close the circle and complete the setup of the PeerConnection.
    /// As soon as the PeerConnection has been established, the publisher will become active, and a new active feed other participants can subscribe to.
    #[cfg(feature = "__experimental")]
    pub async fn publish(
        &self,
        params: VideoRoomPublishParams,
        jsep: Jsep,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        let mut message: Value = params.try_into()?;
        message["request"] = "publish".into();
        self.handle
            .send_waiton_ack_with_jsep(message, jsep, timeout)
            .await?;
        Ok(())
    }

    /// Stop publishing and tear down the related PeerConnection
    ///
    /// This request requires no arguments as the context is implicit.
    #[cfg(feature = "__experimental")]
    pub async fn unpublish(&self, timeout: Duration) -> Result<(), jarust_interface::Error> {
        self.handle
            .send_waiton_ack(json!({"request": "unpublish"}), timeout)
            .await?;
        Ok(())
    }

    /// Complete the setup of the PeerConnection for a subscriber
    ///
    /// The subscriber is supposed to send a JSEP SDP answer back to the plugin by the means of this request,
    /// which in this case MUST be associated with a JSEP SDP answer but otherwise requires no arguments.
    pub async fn start(
        &self,
        jsep: Jsep,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        self.handle
            .send_waiton_ack_with_jsep(json!({"request": "start"}), jsep, timeout)
            .await?;
        Ok(())
    }

    #[cfg(feature = "__experimental")]
    pub async fn subscribe(
        &self,
        params: VideoRoomSubscribeParams,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        let mut message = serde_json::to_value(params)?;
        message["request"] = "subscribe".into();
        self.handle.send_waiton_ack(message, timeout).await?;
        Ok(())
    }

    #[cfg(feature = "__experimental")]
    pub async fn unsubscribe(
        &self,
        params: VideoRoomUnsubscribeParams,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        let mut message = serde_json::to_value(params)?;
        message["request"] = "unsubscribe".into();
        self.handle.send_waiton_ack(message, timeout).await?;
        Ok(())
    }

    #[cfg(feature = "__experimental")]
    pub async fn update(
        &self,
        params: VideoRoomCombinedUpdateParams,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        let mut message = serde_json::to_value(params)?;
        message["request"] = "update".into();
        self.handle.send_waiton_ack(message, timeout).await?;
        Ok(())
    }

    #[cfg(feature = "__experimental")]
    pub async fn pause(&self, timeout: Duration) -> Result<(), jarust_interface::Error> {
        self.handle
            .send_waiton_ack(json!({"request": "pause"}), timeout)
            .await?;
        Ok(())
    }

    #[cfg(feature = "__experimental")]
    pub async fn switch(
        &self,
        params: VideoRoomSwitchParams,
        timeout: Duration,
    ) -> Result<(), jarust_interface::Error> {
        let mut message = serde_json::to_value(params)?;
        message["request"] = "switch".into();
        self.handle.send_waiton_ack(message, timeout).await?;
        Ok(())
    }

    #[cfg(feature = "__experimental")]
    pub async fn leave(&self, timeout: Duration) -> Result<(), jarust_interface::Error> {
        self.handle
            .send_waiton_ack(json!({"request": "leave"}), timeout)
            .await?;
        Ok(())
    }
}

impl PluginTask for VideoRoomHandle {
    fn assign_task(&mut self, task: JaTask) {
        self.task = Some(task);
    }

    fn cancel_task(&mut self) {
        if let Some(task) = self.task.take() {
            task.cancel()
        };
    }
}

impl From<JaHandle> for VideoRoomHandle {
    fn from(handle: JaHandle) -> Self {
        Self { handle, task: None }
    }
}

impl Deref for VideoRoomHandle {
    type Target = JaHandle;

    fn deref(&self) -> &Self::Target {
        &self.handle
    }
}

impl Drop for VideoRoomHandle {
    fn drop(&mut self) {
        self.cancel_task();
    }
}