medea 0.2.0

Medea media server
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
//! Implementation of [Control API] gRPC server.
//!
//! [Control API]: https://tinyurl.com/yxsqplq7

use std::{
    collections::HashMap,
    convert::{From, TryFrom},
    net::SocketAddr,
};

use actix::{Actor, Addr, Arbiter, Context, Handler, MailboxError, System};
use async_trait::async_trait;
use derive_more::{Display, From};
use failure::Fail;
use futures::channel::oneshot;
use medea_client_api_proto::MemberId;
use medea_control_api_proto::grpc::{
    api as proto,
    api::control_api_server::{
        ControlApi, ControlApiServer as TonicControlApiServer,
    },
};
use tonic::{
    transport::{self, Server},
    Status,
};

use crate::{
    api::control::{
        endpoints::{WebRtcPlayEndpoint, WebRtcPublishEndpoint},
        error_codes::{
            ErrorCode,
            ErrorCode::{
                ElementIdIsTooLong, ElementIdMismatch, UnimplementedCall,
            },
            ErrorResponse,
        },
        refs::{fid::ParseFidError, Fid, StatefulFid, ToMember, ToRoom},
        EndpointId, EndpointSpec, MemberSpec, RoomSpec, TryFromProtobufError,
    },
    log::prelude::*,
    shutdown::ShutdownGracefully,
    signalling::room_service::{
        ApplyMember, ApplyRoom, CreateEndpointInRoom, CreateMemberInRoom,
        CreateRoom, DeleteElements, Get, RoomService, RoomServiceError, Sids,
    },
    AppContext,
};

/// Errors which can happen while processing requests to gRPC [Control API].
///
/// [Control API]: https://tinyurl.com/yxsqplq7
#[derive(Debug, Display, Fail, From)]
pub enum GrpcControlApiError {
    /// Error while parsing [`Fid`] of element.
    Fid(ParseFidError),

    /// Error which can happen while converting protobuf objects into interior
    /// [medea] [Control API] objects.
    ///
    /// [Control API]: https://tinyurl.com/yxsqplq7
    /// [medea]: https://github.com/instrumentisto/medea
    TryFromProtobuf(TryFromProtobufError),

    /// [`MailboxError`] for [`RoomService`].
    #[display(fmt = "Room service mailbox error: {:?}", _0)]
    RoomServiceMailboxError(MailboxError),

    /// Wrapper around [`RoomServiceError`].
    RoomServiceError(RoomServiceError),
}

/// Service which provides gRPC [Control API] implementation.
struct ControlApiService(Addr<RoomService>);

impl ControlApiService {
    /// Implementation of `Create` method for [`Room`].
    ///
    /// [`Room`]: crate::signalling::room::Room
    async fn create_room(
        &self,
        spec: RoomSpec,
    ) -> Result<Sids, GrpcControlApiError> {
        Ok(self.0.send(CreateRoom { spec }).await??)
    }

    /// Implementation of `Create` method for [`Member`] element.
    ///
    /// [`Member`]: crate::signalling::elements::Member
    async fn create_member(
        &self,
        id: MemberId,
        parent_fid: Fid<ToRoom>,
        spec: MemberSpec,
    ) -> Result<Sids, GrpcControlApiError> {
        Ok(self
            .0
            .send(CreateMemberInRoom {
                id,
                parent_fid,
                spec,
            })
            .await??)
    }

    /// Implementation of `Create` method for `Endpoint` element.
    async fn create_endpoint(
        &self,
        id: EndpointId,
        parent_fid: Fid<ToMember>,
        spec: EndpointSpec,
    ) -> Result<Sids, GrpcControlApiError> {
        Ok(self
            .0
            .send(CreateEndpointInRoom {
                id,
                parent_fid,
                spec,
            })
            .await??)
    }

    /// Parses the provided [`proto::ApplyRequest`] and sends [`ApplyRoom`] or
    /// [`ApplyMember`] message to [`RoomService`].
    async fn apply_element(
        &self,
        req: proto::ApplyRequest,
    ) -> Result<Sids, ErrorResponse> {
        let unparsed_fid = req.parent_fid;
        let elem = if let Some(elem) = req.el {
            elem
        } else {
            return Err(ErrorResponse::new(
                ErrorCode::NoElement,
                &unparsed_fid,
            ));
        };

        let parent_fid = StatefulFid::try_from(unparsed_fid)?;
        match parent_fid {
            StatefulFid::Room(fid) => match elem {
                proto::apply_request::El::Room(_) => Ok(self
                    .0
                    .send(ApplyRoom {
                        id: fid.take_room_id(),
                        spec: RoomSpec::try_from(elem)?,
                    })
                    .await
                    .map_err(GrpcControlApiError::from)??),
                _ => Err(ErrorResponse::new(ElementIdMismatch, &fid)),
            },
            StatefulFid::Member(fid) => match elem {
                proto::apply_request::El::Member(member) => Ok(self
                    .0
                    .send(ApplyMember {
                        fid,
                        spec: MemberSpec::try_from(member)?,
                    })
                    .await
                    .map(|_| Sids::new())
                    .map_err(GrpcControlApiError::from)?),
                _ => Err(ErrorResponse::new(ElementIdMismatch, &fid)),
            },
            StatefulFid::Endpoint(_) => Err(ErrorResponse::with_explanation(
                UnimplementedCall,
                String::from(
                    "Apply method for Endpoints is not currently supported.",
                ),
                None,
            )),
        }
    }

    /// Creates element based on provided [`proto::CreateRequest`].
    async fn create_element(
        &self,
        req: proto::CreateRequest,
    ) -> Result<Sids, ErrorResponse> {
        let unparsed_parent_fid = req.parent_fid;
        let elem = if let Some(elem) = req.el {
            elem
        } else {
            return Err(ErrorResponse::new(
                ErrorCode::NoElement,
                &unparsed_parent_fid,
            ));
        };

        if unparsed_parent_fid.is_empty() {
            return Ok(self.create_room(RoomSpec::try_from(elem)?).await?);
        }

        let parent_fid = StatefulFid::try_from(unparsed_parent_fid)?;
        match parent_fid {
            StatefulFid::Room(parent_fid) => match elem {
                proto::create_request::El::Member(member) => {
                    let id: MemberId = member.id.clone().into();
                    let member_spec = MemberSpec::try_from(member)?;
                    Ok(self.create_member(id, parent_fid, member_spec).await?)
                }
                _ => Err(ErrorResponse::new(ElementIdMismatch, &parent_fid)),
            },
            StatefulFid::Member(parent_fid) => {
                let (endpoint_spec, id) = match elem {
                    proto::create_request::El::WebrtcPlay(play) => (
                        EndpointSpec::from(WebRtcPlayEndpoint::try_from(
                            &play,
                        )?),
                        play.id.into(),
                    ),
                    proto::create_request::El::WebrtcPub(publish) => (
                        EndpointSpec::from(WebRtcPublishEndpoint::from(
                            &publish,
                        )),
                        publish.id.into(),
                    ),
                    _ => {
                        return Err(ErrorResponse::new(
                            ElementIdMismatch,
                            &parent_fid,
                        ))
                    }
                };

                Ok(self.create_endpoint(id, parent_fid, endpoint_spec).await?)
            }
            StatefulFid::Endpoint(_) => {
                Err(ErrorResponse::new(ElementIdIsTooLong, &parent_fid))
            }
        }
    }

    /// Deletes element by [`proto::IdRequest`].
    async fn delete_element(
        &self,
        req: proto::IdRequest,
    ) -> Result<(), GrpcControlApiError> {
        let mut delete_elements_msg = DeleteElements::new();
        for id in req.fid {
            let fid = StatefulFid::try_from(id)?;
            delete_elements_msg.add_fid(fid);
        }
        self.0.send(delete_elements_msg.validate()?).await??;
        Ok(())
    }

    /// Returns requested by [`proto::IdRequest`] [`proto::Element`]s serialized
    /// to protobuf.
    async fn get_element(
        &self,
        req: proto::IdRequest,
    ) -> Result<HashMap<String, proto::Element>, GrpcControlApiError> {
        let mut fids = Vec::new();
        for id in req.fid {
            let fid = StatefulFid::try_from(id)?;
            fids.push(fid);
        }

        let elements = self.0.send(Get(fids)).await??;

        Ok(elements
            .into_iter()
            .map(|(id, value)| (id.to_string(), value))
            .collect())
    }
}

/// Converts [`Sids`] to a [`HashMap`] of [`String`]s for gRPC Control API
/// protocol.
fn proto_sids(sids: Sids) -> HashMap<String, String> {
    sids.into_iter()
        .map(|(id, sid)| (id.to_string(), sid.to_string()))
        .collect()
}

#[async_trait]
impl ControlApi for ControlApiService {
    /// Creates a new [`Element`] with a given ID.
    ///
    /// Not idempotent. Errors if an [`Element`] with the same ID already
    /// exists.
    ///
    /// Propagates request to [`ControlApiService::create_element`].
    ///
    /// [`Element`]: proto::create_request::El
    async fn create(
        &self,
        request: tonic::Request<proto::CreateRequest>,
    ) -> Result<tonic::Response<proto::CreateResponse>, Status> {
        debug!("Create gRPC Request: [{:?}]", request);
        let create_response =
            match self.create_element(request.into_inner()).await {
                Ok(sid) => proto::CreateResponse {
                    sid: proto_sids(sid),
                    error: None,
                },
                Err(e) => proto::CreateResponse {
                    sid: HashMap::new(),
                    error: Some(e.into()),
                },
            };
        Ok(tonic::Response::new(create_response))
    }

    /// Removes an [`Element`] by its ID.
    ///
    /// Allows referring multiple [`Element`]s on the last two levels.
    /// Idempotent. If no [`Element`]s with such IDs exist, then succeeds.
    ///
    /// Propagates request to [`ControlApiService::delete_element`].
    ///
    /// [`Element`]: proto::Element
    async fn delete(
        &self,
        request: tonic::Request<proto::IdRequest>,
    ) -> Result<tonic::Response<proto::Response>, Status> {
        debug!("Delete gRPC Request: [{:?}]", request);
        let response = match self.delete_element(request.into_inner()).await {
            Ok(_) => proto::Response { error: None },
            Err(e) => proto::Response {
                error: Some(ErrorResponse::from(e).into()),
            },
        };
        Ok(tonic::Response::new(response))
    }

    /// Returns an [`Element`] by its ID.
    ///
    /// Allows referring multiple [`Element`]s.
    /// If no ID specified, returns all the declared [`Element`]s.
    ///
    /// Propagates request to [`ControlApiService::get_element`].
    ///
    /// [`Element`]: proto::Element
    async fn get(
        &self,
        request: tonic::Request<proto::IdRequest>,
    ) -> Result<tonic::Response<proto::GetResponse>, Status> {
        debug!("Get gRPC Request: [{:?}]", request);
        let response = match self.get_element(request.into_inner()).await {
            Ok(elements) => proto::GetResponse {
                elements,
                error: None,
            },
            Err(e) => proto::GetResponse {
                elements: HashMap::new(),
                error: Some(ErrorResponse::from(e).into()),
            },
        };
        Ok(tonic::Response::new(response))
    }

    /// Applies the given spec to an [`Element`] by its ID.
    ///
    /// Idempotent. If no [`Element`] with such ID exists, then it will be
    /// created, otherwise it will be reconfigured. [`Element`]s that exist, but
    /// are not specified in the provided spec will be removed.
    ///
    /// Propagates request to [`ControlApiService::apply_element`].
    ///
    /// [`Element`]: proto::apply_request::El
    async fn apply(
        &self,
        request: tonic::Request<proto::ApplyRequest>,
    ) -> Result<tonic::Response<proto::CreateResponse>, Status> {
        debug!("Apply gRPC Request: [{:?}]", request);
        let response = match self.apply_element(request.into_inner()).await {
            Ok(sid) => proto::CreateResponse {
                sid: proto_sids(sid),
                error: None,
            },
            Err(e) => proto::CreateResponse {
                sid: HashMap::new(),
                error: Some(e.into()),
            },
        };
        Ok(tonic::Response::new(response))
    }
}

/// Actor wrapper for [`tonic`] gRPC server which provides dynamic [Control
/// API].
///
/// [Control API]: https://tinyurl.com/yxsqplq7
pub struct GrpcServer(Option<futures::channel::oneshot::Sender<()>>);

impl Actor for GrpcServer {
    type Context = Context<Self>;

    fn started(&mut self, _ctx: &mut Self::Context) {
        info!("gRPC Control API server started.");
    }
}

impl Handler<ShutdownGracefully> for GrpcServer {
    type Result = ();

    fn handle(
        &mut self,
        _: ShutdownGracefully,
        _: &mut Self::Context,
    ) -> Self::Result {
        info!(
            "gRPC Control API server received ShutdownGracefully message so \
             shutting down.",
        );
        if let Some(grpc_shutdown) = self.0.take() {
            let _ = grpc_shutdown.send(());
        }
    }
}

/// Run gRPC [Control API] server in actix actor. Returns [`Addr`] of
/// [`GrpcServer`] [`Actor`] and [`oneshot::Receiver`] for [`transport::Error`]
/// that may fire when initializing [`GrpcServer`].
///
/// [Control API]: https://tinyurl.com/yxsqplq7
pub fn run(
    room_service: Addr<RoomService>,
    app: &AppContext,
) -> (
    Addr<GrpcServer>,
    oneshot::Receiver<Result<(), transport::Error>>,
) {
    let bind_ip = app.config.server.control.grpc.bind_ip;
    let bind_port = app.config.server.control.grpc.bind_port;

    info!("Starting gRPC server on {}:{}", bind_ip, bind_port);

    let bind_addr = SocketAddr::from((bind_ip, bind_port));
    let (grpc_shutdown_tx, grpc_shutdown_rx) = oneshot::channel();
    let (tonic_server_tx, tonic_server_rx) = oneshot::channel();
    let grpc_actor_addr =
        GrpcServer::start_in_arbiter(&Arbiter::new(), move |_| {
            Arbiter::spawn(async move {
                let result = Server::builder()
                    .add_service(TonicControlApiServer::new(ControlApiService(
                        room_service,
                    )))
                    .serve_with_shutdown(bind_addr, async move {
                        let _ = grpc_shutdown_rx.await;
                    })
                    .await;

                if let Err(err) = tonic_server_tx.send(result) {
                    error!(
                        "gRPC server failed to start, and error could not \
                        be propagated. Error details: {:?}",
                        err
                    );
                    System::current().stop();
                };
            });

            GrpcServer(Some(grpc_shutdown_tx))
        });

    (grpc_actor_addr, tonic_server_rx)
}