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
//! [`ControlApi`] definitions.
//!
//! [`ControlApi`]: Api

pub mod endpoint;
pub mod member;
pub mod room;

use std::{collections::HashMap, str::FromStr};

use async_trait::async_trait;
use derive_more::{Display, Error, From};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub use std::collections::HashMap as Pipeline;

#[doc(inline)]
pub use self::{endpoint::Endpoint, member::Member, room::Room};

/// API allowing to control a media server dynamically, by creating, updating
/// and destroying pipelines of media [`Element`]s on it.
///
/// Both API client and API server should implement this trait.
#[async_trait]
pub trait Api {
    /// Error returned by this [`ControlApi`].
    ///
    /// [`ControlApi`]: Api
    type Error;

    /// Creates a new [`Element`] on the media server.
    ///
    /// # Non-idempotent
    ///
    /// Errors if an [`Element`] with such ID already exists.
    ///
    /// # Errors
    ///
    /// - If the [`Element`]'s parent [`Element`] (identified by a [`Fid`])
    ///   doesn't exist.
    /// - If an [`Element`] with such ID already exists.
    /// - If the media server failed to perform this request.
    async fn create(&self, req: Request) -> Result<member::Sids, Self::Error>;

    /// Applies changes to an existing [`Element`] on the media server, or
    /// creates a new one in case there is no [`Element`] with such ID.
    ///
    /// # Idempotent
    ///
    /// If no [`Element`] with such ID exists, then it will be created,
    /// otherwise it will be reconfigured. [`Element`]s that exist on the same
    /// hierarchy level, but are not specified in the provided [`Request`], will
    /// be removed.
    ///
    /// # Errors
    ///
    /// - If the [`Element`]'s parent [`Element`] (identified by a [`Fid`])
    ///   doesn't exist.
    /// - If the media server failed to perform this request.
    async fn apply(&self, req: Request) -> Result<member::Sids, Self::Error>;

    /// Removes [`Element`]s from the media server.
    ///
    /// Allows referring multiple [`Element`]s on the last two levels of a
    /// [`Fid`].
    ///
    /// # Idempotent
    ///
    /// If no [`Element`]s with such [`Fid`]s exist, then succeeds.
    ///
    /// # Errors
    ///
    /// - If no [`Fid`]s were specified.
    /// - If any [`Fid`] contains multiple [`room::Id`]s.
    /// - If the media server failed to perform this request.
    async fn delete(&self, fids: &[Fid]) -> Result<(), Self::Error>;

    /// Lookups [`Element`]s by their [`Fid`]s on the media server.
    ///
    /// If no [`Fid`]s are specified, then returns all the current [`Element`]s
    /// on the media server.
    ///
    /// If no [`Element`] exists for some [`Fid`], then it won't be present in
    /// the returned [`Elements`] collection.
    ///
    /// # Errors
    ///
    /// - If the media server failed to perform this request.
    async fn get(&self, fids: &[Fid]) -> Result<Elements, Self::Error>;

    /// Checks healthiness of the media server.
    ///
    /// Caller should assert that the returned [`Pong`] has the same nonce as
    /// the sent [`Ping`].
    ///
    /// # Errors
    ///
    /// - If the media server failed to perform this request.
    async fn healthz(&self, ping: Ping) -> Result<Pong, Self::Error>;
}

/// Request for creating or applying an [`Element`] on a media server.
#[derive(Clone, Debug)]
pub enum Request {
    /// [`Room`] to be created or to apply changes to.
    Room {
        /// ID of the created [`Room`].
        id: room::Id,

        /// Spec of the created [`Room`].
        spec: room::Spec,
    },

    /// [`Member`] to be created or to apply changes to.
    Member {
        /// ID of the created [`Member`].
        id: member::Id,

        /// ID of the [`Room`] this [`Member`] participates in.
        room_id: room::Id,

        /// Spec of the created [`Member`].
        spec: Box<member::Spec>,
    },

    /// [`Endpoint`] to be created or to apply changes to.
    Endpoint {
        /// ID of the created [`Endpoint`].
        id: endpoint::Id,

        /// ID of the [`Room`] this [`Endpoint`] belongs to.
        room_id: room::Id,

        /// ID of the [`Member`] this [`Endpoint`] belongs to.
        member_id: member::Id,

        /// Spec of the created [`Endpoint`].
        spec: endpoint::Spec,
    },
}

/// All possible media elements of [`ControlApi`].
///
/// [`ControlApi`]: Api
#[derive(Clone, Debug, From)]
pub enum Element {
    /// [`Room`] media element.
    Room(Room),

    /// [`Member`] media element.
    Member(Box<Member>),

    /// [`Endpoint`] media element.
    Endpoint(Endpoint),
}

/// Collection of uniquely identified [`Element`]s.
pub type Elements = HashMap<Fid, Element>;

/// Possible [`Element`]s allowed to act as a root of [`ControlApi`] static
/// spec.
///
/// [`ControlApi`]: Api
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind"))]
pub enum RootElement {
    /// [`Room`] media [`Element`].
    Room(Room),
}

/// FID (Full ID) is a composition of media [`Element`] IDs referring to some
/// [`Element`] on a whole media server uniquely.
#[derive(Clone, Debug, Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Fid {
    /// FID of a [`Room`].
    #[display(fmt = "{}", id)]
    Room {
        /// Unique ID of the [`Room`].
        id: room::Id,
    },

    /// FID of a [`Member`].
    #[display(fmt = "{}/{}", room_id, id)]
    Member {
        /// ID of the [`Member`] in the [`Room`].
        id: member::Id,

        /// Unique ID of the [`Room`].
        room_id: room::Id,
    },

    /// FID of an [`Endpoint`].
    #[display(fmt = "{}/{}/{}", room_id, member_id, id)]
    Endpoint {
        /// ID of the [`Endpoint`] of the [`Member`].
        id: endpoint::Id,

        /// Unique ID of the [`Room`].
        room_id: room::Id,

        /// ID of the [`Member`] in the [`Room`].
        member_id: member::Id,
    },
}

impl FromStr for Fid {
    type Err = ParseFidError;

    fn from_str(val: &str) -> Result<Self, Self::Err> {
        if val.is_empty() {
            return Err(ParseFidError::Empty);
        }

        let mut splitted = val.split('/');

        let room_id = splitted.next().ok_or(ParseFidError::Empty)?;
        if room_id.is_empty() {
            return Err(ParseFidError::MissingPath(val.into()));
        }

        let Some(member_id) = splitted.next() else {
            return Ok(Self::Room { id: room_id.into() });
        };
        if member_id.is_empty() {
            return Err(ParseFidError::MissingPath(val.into()));
        }

        let Some(endpoint_id) = splitted.next() else {
            return Ok(Self::Member {
                id: member_id.into(),
                room_id: room_id.into(),
            });
        };
        if endpoint_id.is_empty() {
            return Err(ParseFidError::MissingPath(val.into()));
        }

        if splitted.next().is_some() {
            Err(ParseFidError::TooManyPaths(val.into()))
        } else {
            Ok(Self::Endpoint {
                id: endpoint_id.into(),
                room_id: room_id.into(),
                member_id: member_id.into(),
            })
        }
    }
}

/// Possible errors of parsing a [`Fid`].
#[derive(Debug, Display, Error)]
pub enum ParseFidError {
    /// [`Fid`] is empty.
    #[display(fmt = "FID is empty")]
    Empty,

    /// [`Fid`] has too many paths.
    #[display(fmt = "FID has too many paths: {}", _0)]
    TooManyPaths(#[error(not(source))] Box<str>),

    /// [`Fid`] has missing paths.
    #[display(fmt = "FID has missing paths: {}", _0)]
    MissingPath(#[error(not(source))] Box<str>),
}

/// [`Ping`] message received by a media server periodically for probing its
/// healthiness.
///
/// Each new [`Ping`] should increment its nonce, starting with `0`.
#[derive(
    Clone, Copy, Debug, Display, Eq, From, Hash, Ord, PartialEq, PartialOrd,
)]
pub struct Ping(pub u32);

/// [`Pong`] message sent by a media server in response to a received [`Ping`]
/// message.
///
/// Contains nonce of the answered [`Ping`] message.
#[derive(
    Clone, Copy, Debug, Display, Eq, From, Hash, Ord, PartialEq, PartialOrd,
)]
pub struct Pong(pub u32);

#[cfg(all(feature = "serde", test))]
mod serialization {
    use super::{
        endpoint::{
            web_rtc_play::{self, LocalSrcUri},
            web_rtc_publish::{self, AudioSettings, P2pMode, VideoSettings},
        },
        member::{self, Credentials},
        room, Room, RootElement,
    };

    // language=YAML
    const SPEC: &str = r#"
kind: Room
id: test-call
spec:
  pipeline:
    caller:
      kind: Member
      spec:
        credentials:
          plain: test
        pipeline:
          publish:
            kind: WebRtcPublishEndpoint
            spec:
              p2p: Always
    some-member:
      kind: Member
      spec:
        credentials:
          plain: test
        pipeline:
          publish:
            kind: WebRtcPublishEndpoint
            spec:
              p2p: Always
    responder:
      kind: Member
      spec:
        credentials:
          plain: test
        pipeline:
          play:
            kind: WebRtcPlayEndpoint
            spec:
              src: "local://test-call/caller/publish"
          play2:
            kind: WebRtcPlayEndpoint
            spec:
              src: "local://test-call/some-member/publish"
    "#;

    #[test]
    fn spec() {
        assert_eq!(
            serde_yaml::from_str::<RootElement>(SPEC)
                .unwrap_or_else(|e| panic!("{e}")),
            RootElement::Room(Room {
                id: "test-call".into(),
                spec: room::Spec {
                    pipeline: [
                        (
                            "caller".into(),
                            member::Spec {
                                pipeline: [(
                                    "publish".into(),
                                    web_rtc_publish::Spec {
                                        p2p: P2pMode::Always,
                                        force_relay: false,
                                        audio_settings: AudioSettings::default(
                                        ),
                                        video_settings: VideoSettings::default(
                                        ),
                                    }
                                    .into(),
                                )]
                                .into_iter()
                                .collect(),
                                credentials: Some(Credentials::Plain(
                                    "test".into(),
                                )),
                                on_join: None,
                                on_leave: None,
                                idle_timeout: None,
                                reconnect_timeout: None,
                                ping_interval: None,
                            }
                            .into(),
                        ),
                        (
                            "some-member".into(),
                            member::Spec {
                                pipeline: [(
                                    "publish".into(),
                                    web_rtc_publish::Spec {
                                        p2p: P2pMode::Always,
                                        force_relay: false,
                                        audio_settings: AudioSettings::default(
                                        ),
                                        video_settings: VideoSettings::default(
                                        ),
                                    }
                                    .into(),
                                )]
                                .into_iter()
                                .collect(),
                                credentials: Some(Credentials::Plain(
                                    "test".into()
                                )),
                                on_join: None,
                                on_leave: None,
                                idle_timeout: None,
                                reconnect_timeout: None,
                                ping_interval: None,
                            }
                            .into(),
                        ),
                        (
                            "responder".into(),
                            member::Spec {
                                pipeline: [
                                    (
                                        "play".into(),
                                        web_rtc_play::Spec {
                                            src: LocalSrcUri {
                                                room_id: "test-call".into(),
                                                member_id: "caller".into(),
                                                endpoint_id: "publish".into(),
                                            },
                                            force_relay: false,
                                        }
                                        .into(),
                                    ),
                                    (
                                        "play2".into(),
                                        web_rtc_play::Spec {
                                            src: LocalSrcUri {
                                                room_id: "test-call".into(),
                                                member_id: "some-member".into(),
                                                endpoint_id: "publish".into(),
                                            },
                                            force_relay: false,
                                        }
                                        .into(),
                                    )
                                ]
                                .into_iter()
                                .collect(),
                                credentials: Some(Credentials::Plain(
                                    "test".into(),
                                )),
                                on_join: None,
                                on_leave: None,
                                idle_timeout: None,
                                reconnect_timeout: None,
                                ping_interval: None,
                            }
                            .into(),
                        ),
                    ]
                    .into_iter()
                    .collect(),
                }
            })
        );
    }
}