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
use serde::{Deserialize, Serialize};

use crate::common::{PhantomId, SerializableId, SocketInfo};
use crate::prelude::{PeerId, Token};

/// Identifier for source socket of media
#[derive(Serialize, Deserialize, Debug, Clone, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct MediaId(pub String);

impl MediaId {
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    pub fn new(media_id: impl Into<String>) -> Self {
        MediaId(media_id.into())
    }
}

impl SerializableId for MediaId {
    fn try_create(id: Option<String>) -> Option<Self>
    where
        Self: Sized,
    {
        id.map(|id| MediaId(id))
    }

    fn as_str(&self) -> &str {
        self.0.as_str()
    }

    fn id(&self) -> String {
        self.0.clone()
    }

    fn key(&self) -> &'static str {
        "media_id"
    }
}

/// Identifier for source socket of rtcp
#[derive(Serialize, Deserialize, Debug, Clone, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct RtcpId(pub String);

impl RtcpId {
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    pub fn new(rtcp_id: impl Into<String>) -> Self {
        RtcpId(rtcp_id.into())
    }
}

impl SerializableId for RtcpId {
    fn try_create(id: Option<String>) -> Option<Self>
    where
        Self: Sized,
    {
        id.map(|id| RtcpId(id))
    }

    fn as_str(&self) -> &str {
        self.0.as_str()
    }

    fn id(&self) -> String {
        self.0.clone()
    }

    fn key(&self) -> &'static str {
        "rtcp_id"
    }
}

/// Identifier for MediaConnection
#[derive(Serialize, Deserialize, Debug, Clone, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct MediaConnectionId(pub String);

impl MediaConnectionId {
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    pub fn new(media_connection_id: impl Into<String>) -> Self {
        MediaConnectionId(media_connection_id.into())
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub(crate) struct CreateMediaOptions {
    pub is_video: bool,
}

/// Query parameter for POST /media/connections
///
/// [API](http://35.200.46.204/#/3.media/media_connection_create)
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct CallQuery {
    /// to identify which PeerObject calls to neighbour
    pub peer_id: PeerId,
    /// to show that this program has permission to control PeerObject
    pub token: Token,
    /// connect to the neighbour which has this PeerId
    pub target_id: PeerId,
    /// Parameters for MediaConnection
    /// It contains source socket. If the field is None, this MediaConnection works as RecvOnly.
    pub constraints: Option<Constraints>,
    /// Shows destiation socket to which received data is redirected
    /// If this field is not set, DataConnection works as SendOnly.
    pub redirect_params: Option<RedirectParameters>,
}

/// Parameters for MediaConnection
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[allow(non_snake_case)]
pub struct Constraints {
    /// Shows whether this connection sends video or not
    pub video: bool,
    /// Shows whether this connection receives video or not
    pub videoReceiveEnabled: Option<bool>,
    /// Shows whether this connection sends audio or not
    pub audio: bool,
    /// Shows whether this connection receives audio or not
    pub audioReceiveEnabled: Option<bool>,
    /// Parameters for sending video
    pub video_params: Option<MediaParams>,
    /// Parameters for sending audio
    pub audio_params: Option<MediaParams>,
}

/// Parameters for sending media
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct MediaParams {
    /// band width between Peers
    pub band_width: usize,
    /// Codec which caller side want to use. Video: `"H264"` or `"VP8"`, Audio: `"OPUS"` or `"G711"`. It will be used in SDP.
    pub codec: String,
    /// Identify which media should be redirected
    pub media_id: MediaId,
    /// Identify which rtcp should be redirected
    pub rtcp_id: Option<RtcpId>,
    /// Payload type which caller side want to use. It will be used in SDP.
    pub payload_type: Option<u16>,
    /// Sampling rate which media uses
    pub sampling_rate: Option<usize>,
}

/// Shows to which socket media should be redirected.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct RedirectParameters {
    /// video is redirected to this socket
    pub video: Option<SocketInfo<PhantomId>>,
    /// video rtcp is redirected to this socket
    pub video_rtcp: Option<SocketInfo<PhantomId>>,
    /// audio is redirected to this socket
    pub audio: Option<SocketInfo<PhantomId>>,
    /// audio rtcp is redirected to this socket
    pub audio_rtcp: Option<SocketInfo<PhantomId>>,
}

/// Response from POST /media/connections
///
/// [API](http://35.200.46.204/#/3.media/media_connection_create)
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct CallResponse {
    /// Fixed value as `"PEERS_CALL"`.
    pub command_type: String,
    /// Identifier for MediaConnection
    pub params: MediaConnectionIdWrapper,
}

/// Wrapper for serializing JSON
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, PartialOrd)]
pub struct MediaConnectionIdWrapper {
    /// Identifier for MediaConnection
    pub media_connection_id: MediaConnectionId,
}

/// Query parameter for POST /media/connections
///
/// [API](http://35.200.46.204/#/3.media/media_connection_answer)
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct AnswerQuery {
    /// Parameters for MediaConnection
    /// It contains source socket. If the field is None, this MediaConnection works as RecvOnly.
    pub constraints: Constraints,
    /// Shows destiation socket to which received data is redirected
    /// If this field is not set, DataConnection works as SendOnly.
    pub redirect_params: Option<RedirectParameters>,
}

/// Response from POST /media/connections
///
/// [API](http://35.200.46.204/#/3.media/media_connection_answer)
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct AnswerResponse {
    /// Fixed value as `"MEDIA_CONNECTION_ANSWER"`.
    pub command_type: String,
    /// Shows media_ids used in this MediaConnection
    pub params: AnswerResponseParams,
}

/// Shows media_ids used in this MediaConnection
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct AnswerResponseParams {
    pub video_id: Option<MediaId>,
    pub audio_id: Option<MediaId>,
}

/// Events from GET /media/events API.
/// It includes TIMEOUT, but the event is not needed for end-user-programs.
/// So it's used internally.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(tag = "event")]
pub(crate) enum EventEnum {
    READY,
    STREAM,
    CLOSE,
    ERROR { error_message: String },
    TIMEOUT,
}

/// Status of MediaConnection
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct MediaConnectionStatus {
    /// Metadata associated with the connection, passed in by whoever initiated the connection.
    pub metadata: String,
    /// Shows whether this MediaConnection is working or not.
    pub open: bool,
    /// Shows neighbour id
    pub remote_id: PeerId,
    /// Shows ssrc(Synchrozination Source) information
    pub ssrc: Vec<SsrcPair>,
}

/// Shows ssrc(Synchrozination Source) information
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct SsrcPair {
    /// Identify Media
    pub media_id: MediaId,
    /// SSRC
    pub ssrc: usize,
}