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
//! [`Endpoint`] definitions.

pub mod web_rtc_play;
pub mod web_rtc_publish;

use derive_more::{AsRef, Display, From, Into};
use ref_cast::RefCast;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[doc(inline)]
pub use self::{web_rtc_play::WebRtcPlay, web_rtc_publish::WebRtcPublish};

/// Media [`Element`] flowing one or more media data streams through itself.
///
/// [`Element`]: crate::Element
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Endpoint {
    /// ID of this [`Endpoint`] media [`Element`].
    ///
    /// [`Element`]: crate::Element
    pub id: Id,

    /// [`Spec`] of this [`Endpoint`] media [`Element`].
    ///
    /// [`Element`]: crate::Element
    pub spec: Spec,
}

impl From<WebRtcPlay> for Endpoint {
    fn from(play: WebRtcPlay) -> Self {
        Self {
            id: play.id.into(),
            spec: play.spec.into(),
        }
    }
}

impl From<WebRtcPublish> for Endpoint {
    fn from(publish: WebRtcPublish) -> Self {
        Self {
            id: publish.id.into(),
            spec: publish.spec.into(),
        }
    }
}

/// Spec of an [`Endpoint`] media [`Element`].
///
/// [`Element`]: crate::Element
#[derive(Clone, Debug, Eq, From, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind", content = "spec"))]
pub enum Spec {
    /// [`WebRtcPublish`] media [`Element`] spec.
    ///
    /// [`Element`]: crate::Element
    WebRtcPublishEndpoint(web_rtc_publish::Spec),

    /// [`WebRtcPlay`] media [`Element`] spec.
    ///
    /// [`Element`]: crate::Element
    WebRtcPlayEndpoint(web_rtc_play::Spec),
}

/// ID of an [`Endpoint`] media [`Element`].
///
/// [`Element`]: crate::Element
#[derive(
    AsRef,
    Clone,
    Debug,
    Display,
    Eq,
    From,
    Hash,
    Into,
    Ord,
    PartialEq,
    PartialOrd,
    RefCast,
)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[from(types("&str", String, web_rtc_play::Id, web_rtc_publish::Id))]
#[into(owned(types(String, web_rtc_play::Id, web_rtc_publish::Id)))]
#[repr(transparent)]
pub struct Id(Box<str>);

impl AsRef<web_rtc_play::Id> for Id {
    fn as_ref(&self) -> &web_rtc_play::Id {
        web_rtc_play::Id::ref_cast(&self.0)
    }
}

impl AsRef<web_rtc_publish::Id> for Id {
    fn as_ref(&self) -> &web_rtc_publish::Id {
        web_rtc_publish::Id::ref_cast(&self.0)
    }
}