Verified Commit 3c965472 authored by Martin Löffler's avatar Martin Löffler
Browse files

Refactor ProduceType, WebSocket produce events

parent 6aca9252
......@@ -10,7 +10,7 @@ use tokio::sync::{
RwLock,
};
use super::user::User;
use super::user::{ProduceType, User};
use crate::{api::ApiError, rtc::get_worker_pool};
pub mod users;
......@@ -20,6 +20,8 @@ pub use users::RoomUsers;
pub enum RoomEvent {
UserJoined(String),
UserLeft(String),
UserStartProduce(String, ProduceType),
UserStopProduce(String, ProduceType),
RoomDelete,
}
......
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::{str::FromStr, sync::Arc};
use mediasoup::producer::Producer;
use mediasoup::rtp_parameters::MediaKind;
use super::room::{Room, RoomEvent};
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
pub enum ProduceType {
#[serde(rename = "audio")]
Audio,
#[serde(rename = "video")]
Video,
#[serde(rename = "saudio")]
#[serde(alias = "screenshareaudio")]
ScreenshareAudio,
#[serde(rename = "svideo")]
#[serde(alias = "screensharevideo")]
ScreenshareVideo,
}
impl ProduceType {
pub fn into_kind(self) -> MediaKind {
match self {
ProduceType::Audio | ProduceType::ScreenshareAudio => MediaKind::Audio,
ProduceType::Video | ProduceType::ScreenshareVideo => MediaKind::Video,
}
}
}
impl From<ProduceType> for MediaKind {
fn from(produce_type: ProduceType) -> MediaKind {
produce_type.into_kind()
}
}
impl FromStr for ProduceType {
......@@ -63,6 +89,7 @@ impl User {
pub fn get_producer(&self, produce_type: ProduceType) -> Option<&Producer> {
let producer = match produce_type {
ProduceType::Audio => &self.audio,
_ => todo!(),
};
producer.as_ref()
......@@ -78,6 +105,7 @@ impl User {
}
let producer = match produce_type {
ProduceType::Audio => &mut self.audio,
_ => todo!(),
};
*producer = Some(new_producer);
......
......@@ -14,7 +14,7 @@ mod error;
mod types;
use error::WSCloseType;
use types::{WSCommand, WSCommandType, WSReply, WSReplyType, WSEvent};
use types::{WSCommand, WSCommandType, WSEvent, WSReply, WSReplyType};
pub fn route() -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Copy {
warp::ws::ws().map(|ws: Ws| ws.on_upgrade(on_connection))
......@@ -129,12 +129,28 @@ async fn event_loop(
if id == user_id {
return Err(WSCloseType::Kicked);
}
let event = WSEvent::UserLeft { id };
ws_sink
.send(Message::text(serde_json::to_string(&event)?))
.await?;
},
RoomEvent::UserStartProduce(id, produce_type) => {
if id != user_id {
let event = WSEvent::UserStartProduce { id, produce_type };
ws_sink
.send(Message::text(serde_json::to_string(&event)?))
.await?;
}
},
RoomEvent::UserStopProduce(id, produce_type) => {
if id != user_id {
let event = WSEvent::UserStopProduce { id, produce_type };
ws_sink
.send(Message::text(serde_json::to_string(&event)?))
.await?;
}
}
RoomEvent::RoomDelete => {
return Err(WSCloseType::RoomClosed);
},
......
......@@ -9,37 +9,7 @@ use mediasoup::{
srtp_parameters::{SrtpCryptoSuite, SrtpParameters},
};
use crate::state::user::UserInfo;
#[derive(Serialize, Deserialize, Clone, Copy)]
pub enum ProduceType {
#[serde(rename = "audio")]
Audio,
#[serde(rename = "video")]
Video,
#[serde(rename = "saudio")]
#[serde(alias = "screenshareaudio")]
ScreenshareAudio,
#[serde(rename = "svideo")]
#[serde(alias = "screensharevideo")]
ScreenshareVideo,
}
impl ProduceType {
pub fn into_kind(self) -> MediaKind {
match self {
ProduceType::Audio | ProduceType::ScreenshareAudio => MediaKind::Audio,
ProduceType::Video | ProduceType::ScreenshareVideo => MediaKind::Video,
}
}
}
impl From<ProduceType> for MediaKind {
fn from(produce_type: ProduceType) -> MediaKind {
produce_type.into_kind()
}
}
use crate::state::user::{ProduceType, UserInfo};
#[derive(Deserialize, IntoStaticStr)]
#[serde(tag = "type", content = "data")]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment