#janus #web-rtc #janus-gateway #async

jarust_make_plugin

Rust的janus客户端SDK

14个版本

0.2.7 2024年4月25日
0.2.6 2024年4月7日
0.2.4 2024年2月28日
0.1.5 2024年2月2日
0.1.4 2024年1月28日

6#janus-gateway 中排名

41 每月下载次数

MIT 许可证

8KB
58

Jarust Make Plugin

一个辅助宏,用于创建一个用于扩展 JaSession 的trait。

要求

make_plugin 将生成包含 async_traittokio(具有 sync 功能)的代码。当然,我们还需要 jarust

[dependencies]
async-trait = "<version>"
jarust = "<version>"
tokio = { version = "<version>", features = ["sync"] }

用法

use jarust::prelude::*;
use jarust_make_plugin::make_plugin;

make_plugin!(EchoTest, "janus.plugin.echotest");

这将生成这个trait

#[async_trait::async_trait]
pub trait EchoTest: Attach {
    type Event: Send + Sync + 'static;
    type Handle: From<JaHandle> + std::ops::Deref<Target = JaHandle> + PluginTask;

    fn parse_echo_test_message(message: JaResponse) -> JaResult<Self::Event>;

    async fn attach_echo_test(
        &self,
    ) -> JaResult<(Self::Handle, tokio::sync::mpsc::Receiver<Self::Event>)> {
        let (handle, mut receiver) = self.attach("janus.plugin.echotest").await?;
        let (tx, rx) = tokio::sync::mpsc::channel(BUFFER_SIZE);
        let join_handle = tokio::spawn(async move {
            while let Some(msg) = receiver.recv().await {
                let msg = Self::parse_echo_test_message(msg)?;
                let _ = tx.send(msg).await;
            }
            Ok::<(), JaError>(())
        });
        let abort_handle = join_handle.abort_handle();
        let mut handle: Self::Handle = handle.into();
        handle.assign_abort(abort_handle);
        Ok((handle, rx))
    }
}

我们将从创建 EchoTestHandle 开始。

pub struct EchoTestHandle {
    handle: JaHandle,
    abort_handle: Option<AbortHandle>,
}

然后我们可以实现 EchoTestHandle 所需的trait,以便将其用作插件句柄。

// This trait is used to assign an abort handle so we can abort the task on drop
impl PluginTask for EchoTestHandle {
    fn assign_abort(&mut self, abort_handle: AbortHandle) {
        self.abort_handle = Some(abort_handle);
    }

    fn abort_plugin(&mut self) {
        if let Some(abort_handle) = self.abort_handle.take() {
            abort_handle.abort();
        };
    }
}

// Create EchoTestHandle from JaHandle
impl From<JaHandle> for EchoTestHandle {
    fn from(handle: JaHandle) -> Self {
        Self {
            handle,
            abort_handle: None,
        }
    }
}

// Dereference EchoTestHandle to JaHandle to get the existing fucntionalities from JaHandle
impl Deref for EchoTestHandle {
    type Target = JaHandle;

    fn deref(&self) -> &Self::Target {
        &self.handle
    }
}

// On drop abort task
impl Drop for EchoTestHandle {
    fn drop(&mut self) {
        self.abort_plugin();
    }
}

并添加 EchoTestHandle 特定的请求。(例如,音频桥可以具有 muteunmute 请求)

// EchoTestHandle specific requests
impl EchoTestHandle {
    pub async fn start(&self, request: EchoTestStartMsg) -> JaResult<()> {
        self.handle.message(serde_json::to_value(request)?).await
    }
}

最后,我们在 JaSession 上实现从 make_plugin! 宏创建的 EchoTest trait,以扩展 JaSession 的功能。并提供句柄类型、事件类型以及如何解析传入的事件。

impl EchoTest for JaSession {
    type Event = EchoTestPluginEvent;
    type Handle = EchoTestHandle;

    fn parse_echo_test_message(message: JaResponse) -> JaResult<Self::Event> {
        let msg = match message.janus {
            JaResponseProtocol::Event(JaEventProtocol::Event { plugin_data, .. }) => {
                serde_json::from_value::<EchoTestPluginData>(plugin_data)?.event
            }
            _ => {
                tracing::error!("unexpected response");
                return Err(JaError::UnexpectedResponse);
            }
        };
        Ok(msg)
    }
}

依赖项

~0.9–1.4MB
~26K SLoC