use crate::video_room::params::*;
use crate::video_room::responses::*;
use crate::JanusId;
use jarust_core::prelude::*;
use jarust_interface::japrotocol::Jsep;
use jarust_rt::JaTask;
use serde_json::json;
use serde_json::Value;
use std::ops::Deref;
use std::time::Duration;
pub struct VideoRoomHandle {
handle: JaHandle,
task: Option<JaTask>,
}
impl VideoRoomHandle {
#[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
pub async fn create_room(
&self,
room: Option<JanusId>,
timeout: Duration,
) -> Result<VideoRoomCreatedRsp, jarust_interface::Error> {
self.create_room_with_config(
VideoRoomCreateParams {
room,
..Default::default()
},
timeout,
)
.await
}
#[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
pub async fn create_room_with_config(
&self,
params: VideoRoomCreateParams,
timeout: Duration,
) -> Result<VideoRoomCreatedRsp, jarust_interface::Error> {
tracing::info!(plugin = "videoroom", "Sending create");
let mut message: Value = params.try_into()?;
message["request"] = "create".into();
self.handle
.send_waiton_rsp::<VideoRoomCreatedRsp>(message, timeout)
.await
}
#[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
pub async fn edit_room(
&self,
params: VideoRoomEditParams,
timeout: Duration,
) -> Result<VideoRoomEditedRsp, jarust_interface::Error> {
tracing::info!(plugin = "videoroom", "Sending edit");
let mut message: Value = params.try_into()?;
message["request"] = "edit".into();
self.handle
.send_waiton_rsp::<VideoRoomEditedRsp>(message, timeout)
.await
}
#[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
pub async fn destroy_room(
&self,
params: VideoRoomDestroyParams,
timeout: Duration,
) -> Result<VideoRoomDestroyedRsp, jarust_interface::Error> {
tracing::info!(plugin = "videoroom", "Sending destroy");
let mut message: Value = params.try_into()?;
message["request"] = "destroy".into();
self.handle
.send_waiton_rsp::<VideoRoomDestroyedRsp>(message, timeout)
.await
}
#[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
pub async fn exists(
&self,
params: VideoRoomExistsParams,
timeout: Duration,
) -> Result<bool, jarust_interface::Error> {
tracing::info!(plugin = "videoroom", "Sending exists");
let mut message: Value = params.try_into()?;
message["request"] = "exists".into();
let response = self
.handle
.send_waiton_rsp::<VideoRoomExistsRsp>(message, timeout)
.await?;
Ok(response.exists)
}
#[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
pub async fn list_rooms(
&self,
timeout: Duration,
) -> Result<Vec<Room>, jarust_interface::Error> {
tracing::info!(plugin = "videoroom", "Sending list");
let response = self
.handle
.send_waiton_rsp::<VideoRoomListRoomsRsp>(json!({"request": "list"}), timeout)
.await?;
Ok(response.list)
}
#[cfg(feature = "__experimental")]
#[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
pub async fn allowed(
&self,
params: VideoRoomAllowedParams,
timeout: Duration,
) -> Result<VideoRoomAccessRsp, jarust_interface::Error> {
if (params.action == VideoRoomAllowedAction::Enable
|| params.action == VideoRoomAllowedAction::Disable)
&& !params.allowed.is_empty()
{
return Err(jarust_interface::Error::InvalidJanusRequest {
reason: "An enable or disable 'allowed' request cannot have its allowed array set"
.to_string(),
});
}
tracing::info!(plugin = "videoroom", "Sending allowed");
let mut message: Value = params.try_into()?;
message["request"] = "allowed".into();
self.handle
.send_waiton_rsp::<VideoRoomAccessRsp>(message, timeout)
.await
}
#[cfg(feature = "__experimental")]
#[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
pub async fn kick(
&self,
params: VideoRoomKickParams,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
tracing::info!(plugin = "videoroom", "Sending kick");
let mut message: Value = params.try_into()?;
message["request"] = "kick".into();
self.handle.send_waiton_rsp::<()>(message, timeout).await
}
#[cfg(feature = "__experimental")]
#[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
pub async fn enable_recording(
&self,
params: VideoRoomEnableRecordingParams,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
tracing::info!(plugin = "videoroom", "Sending enable recording");
let mut message: Value = params.try_into()?;
message["request"] = "enable_recording".into();
self.handle.send_waiton_rsp::<()>(message, timeout).await
}
#[cfg(feature = "__experimental")]
#[tracing::instrument(level = tracing::Level::DEBUG, skip_all)]
pub async fn list_participants(
&self,
params: VideoRoomListParticipantsParams,
timeout: Duration,
) -> Result<ListParticipantsRsp, jarust_interface::Error> {
tracing::info!(plugin = "videoroom", "Sending list participants");
let mut message: Value = params.try_into()?;
message["request"] = "listparticipants".into();
self.handle
.send_waiton_rsp::<ListParticipantsRsp>(message, timeout)
.await
}
#[cfg(feature = "__experimental")]
pub async fn moderate(
&self,
params: VideoRoomModerateParams,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
let mut message = serde_json::to_value(params)?;
message["request"] = "moderate".into();
self.handle.send_waiton_rsp::<()>(message, timeout).await
}
#[cfg(feature = "__experimental")]
pub async fn list_forwarders(
&self,
params: VideoRoomListForwardersParams,
timeout: Duration,
) -> Result<VideoRoomListForwardersRsp, jarust_interface::Error> {
let mut message = serde_json::to_value(params)?;
message["request"] = "list_forwarders".into();
self.handle
.send_waiton_rsp::<VideoRoomListForwardersRsp>(message, timeout)
.await
}
#[cfg(feature = "__experimental")]
pub async fn rtp_forward(
&self,
params: VideoRoomRtpForwardParams,
timeout: Duration,
) -> Result<VideoRoomRtpForwardRsp, jarust_interface::Error> {
let mut message = serde_json::to_value(params)?;
message["request"] = "rtp_forward".into();
self.handle
.send_waiton_rsp::<VideoRoomRtpForwardRsp>(message, timeout)
.await
}
#[cfg(feature = "__experimental")]
pub async fn stop_rtp_forward(
&self,
params: VideoRoomStopRtpForward,
timeout: Duration,
) -> Result<VideoRoomStopRtpForwardRsp, jarust_interface::Error> {
let mut message = serde_json::to_value(params)?;
message["request"] = "stop_rtp_forward".into();
self.handle
.send_waiton_rsp::<VideoRoomStopRtpForwardRsp>(message, timeout)
.await
}
}
impl VideoRoomHandle {
pub async fn join_as_publisher(
&self,
params: VideoRoomPublisherJoinParams,
jsep: Option<Jsep>,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
let mut message: Value = params.try_into()?;
message["request"] = "join".into();
message["ptype"] = "publisher".into();
match jsep {
None => self.handle.send_waiton_ack(message, timeout).await?,
Some(jsep) => {
self.handle
.send_waiton_ack_with_jsep(message, jsep, timeout)
.await?
}
};
Ok(())
}
#[cfg(feature = "__experimental")]
pub async fn join_as_subscriber(
&self,
params: VideoRoomSubscriberJoinParams,
jsep: Option<Jsep>,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
let mut message: Value = params.try_into()?;
message["request"] = "join".into();
message["ptype"] = "subscriber".into();
match jsep {
None => self.handle.send_waiton_ack(message, timeout).await?,
Some(ep) => {
self.handle
.send_waiton_ack_with_jsep(message, ep, timeout)
.await?
}
};
Ok(())
}
pub async fn configure_publisher(
&self,
params: VideoRoomPublisherConfigureParams,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
let mut message: Value = params.try_into()?;
message["request"] = "configure".into();
self.handle.send_waiton_ack(message, timeout).await?;
Ok(())
}
#[cfg(feature = "__experimental")]
pub async fn configure_subscriber(
&self,
params: VideoRoomConfigureSubscriberParams,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
let mut message: Value = params.try_into()?;
message["request"] = "configure".into();
self.handle.send_waiton_ack(message, timeout).await?;
Ok(())
}
pub async fn publisher_join_and_configure(
&self,
params: VideoRoomPublisherJoinAndConfigureParams,
jsep: Option<Jsep>,
timeout: Duration,
) -> Result<String, jarust_interface::Error> {
let mut message: Value = params.try_into()?;
message["request"] = "joinandconfigure".into();
message["ptype"] = "publisher".into();
match jsep {
None => self.handle.send_waiton_ack(message, timeout).await,
Some(jsep) => {
self.handle
.send_waiton_ack_with_jsep(message, jsep, timeout)
.await
}
}
}
#[cfg(feature = "__experimental")]
pub async fn publish(
&self,
params: VideoRoomPublishParams,
jsep: Jsep,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
let mut message: Value = params.try_into()?;
message["request"] = "publish".into();
self.handle
.send_waiton_ack_with_jsep(message, jsep, timeout)
.await?;
Ok(())
}
#[cfg(feature = "__experimental")]
pub async fn unpublish(&self, timeout: Duration) -> Result<(), jarust_interface::Error> {
self.handle
.send_waiton_ack(json!({"request": "unpublish"}), timeout)
.await?;
Ok(())
}
pub async fn start(
&self,
jsep: Jsep,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
self.handle
.send_waiton_ack_with_jsep(json!({"request": "start"}), jsep, timeout)
.await?;
Ok(())
}
#[cfg(feature = "__experimental")]
pub async fn subscribe(
&self,
params: VideoRoomSubscribeParams,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
let mut message = serde_json::to_value(params)?;
message["request"] = "subscribe".into();
self.handle.send_waiton_ack(message, timeout).await?;
Ok(())
}
#[cfg(feature = "__experimental")]
pub async fn unsubscribe(
&self,
params: VideoRoomUnsubscribeParams,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
let mut message = serde_json::to_value(params)?;
message["request"] = "unsubscribe".into();
self.handle.send_waiton_ack(message, timeout).await?;
Ok(())
}
#[cfg(feature = "__experimental")]
pub async fn update(
&self,
params: VideoRoomCombinedUpdateParams,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
let mut message = serde_json::to_value(params)?;
message["request"] = "update".into();
self.handle.send_waiton_ack(message, timeout).await?;
Ok(())
}
#[cfg(feature = "__experimental")]
pub async fn pause(&self, timeout: Duration) -> Result<(), jarust_interface::Error> {
self.handle
.send_waiton_ack(json!({"request": "pause"}), timeout)
.await?;
Ok(())
}
#[cfg(feature = "__experimental")]
pub async fn switch(
&self,
params: VideoRoomSwitchParams,
timeout: Duration,
) -> Result<(), jarust_interface::Error> {
let mut message = serde_json::to_value(params)?;
message["request"] = "switch".into();
self.handle.send_waiton_ack(message, timeout).await?;
Ok(())
}
#[cfg(feature = "__experimental")]
pub async fn leave(&self, timeout: Duration) -> Result<(), jarust_interface::Error> {
self.handle
.send_waiton_ack(json!({"request": "leave"}), timeout)
.await?;
Ok(())
}
}
impl PluginTask for VideoRoomHandle {
fn assign_task(&mut self, task: JaTask) {
self.task = Some(task);
}
fn cancel_task(&mut self) {
if let Some(task) = self.task.take() {
task.cancel()
};
}
}
impl From<JaHandle> for VideoRoomHandle {
fn from(handle: JaHandle) -> Self {
Self { handle, task: None }
}
}
impl Deref for VideoRoomHandle {
type Target = JaHandle;
fn deref(&self) -> &Self::Target {
&self.handle
}
}
impl Drop for VideoRoomHandle {
fn drop(&mut self) {
self.cancel_task();
}
}