jarust_core/lib.rs
1//! # Jarust
2//!
3//! Jarust is a Rust adapter for [Janus WebRTC server](https://github.com/meetecho/janus-gateway).
4//!
5//! It provides a high-level API to interact with the Janus server.
6//!
7//! You can use it to connect with the Janus server, create a session,
8//! attach a plugin, send messages to the plugin, and handle the incoming messages.
9//!
10//! ## Customizability
11//!
12//! Janus supports multiple transports, each transport has a different API to interact with.
13//!
14//! Jarust was built in a modular manner to support the variations Janus provides. It also has its customizations like the transaction generation strategy.
15//!
16//! ## Runtime
17//!
18//! We currently only support the Tokio runtime and are planning to support more runtimes in the future. For that, we've abstracted the runtime-specific code in the [`jarust_rt`] crate.
19//!
20//! ## Plugins
21//!
22//! We have a separate crate for Janus plugins, [`jarust_plugins`](https://crates.io/crates/jarust_plugins).
23//!
24
25pub mod jaconfig;
26pub mod jaconnection;
27pub mod jahandle;
28mod jakeepalive;
29pub mod japlugin;
30pub mod jasession;
31pub mod prelude;
32
33pub use jarust_interface::tgenerator::GenerateTransaction;
34
35use jaconfig::JaConfig;
36use jaconfig::JanusAPI;
37use jaconnection::JaConnection;
38use jarust_interface::janus_interface::ConnectionParams;
39use jarust_interface::janus_interface::JanusInterface;
40use jarust_interface::restful::RestfulInterface;
41use jarust_interface::websocket::WebSocketInterface;
42use tracing::Level;
43
44/// Creates a new connection with janus server from the provided configs.
45///
46/// ## Example:
47///
48/// ```rust
49/// let config = JaConfig::builder()
50/// .url("ws://localhost:8188/ws")
51/// .capacity(32)
52/// .build();
53/// let mut connection = jarust_core::connect(config, ApiInterface::WebSocket, RandomTransactionGenerator).await.unwrap();
54/// ```
55#[cfg(not(target_family = "wasm"))]
56pub async fn connect(
57 jaconfig: JaConfig,
58 api_interface: JanusAPI,
59 transaction_generator: impl GenerateTransaction,
60) -> Result<JaConnection, jarust_interface::Error> {
61 let conn_params = ConnectionParams {
62 url: jaconfig.url,
63 capacity: jaconfig.capacity,
64 apisecret: jaconfig.apisecret,
65 server_root: jaconfig.server_root,
66 };
67 match api_interface {
68 JanusAPI::WebSocket => {
69 custom_connect(
70 WebSocketInterface::make_interface(conn_params, transaction_generator).await?,
71 )
72 .await
73 }
74 JanusAPI::Restful => {
75 custom_connect(
76 RestfulInterface::make_interface(conn_params, transaction_generator).await?,
77 )
78 .await
79 }
80 }
81}
82
83/// Creates a new connection with janus server from the provided configs
84#[cfg(target_family = "wasm")]
85pub async fn connect(
86 jaconfig: JaConfig,
87 api_interface: JanusAPI,
88 transaction_generator: impl GenerateTransaction,
89) -> Result<JaConnection, jarust_interface::Error> {
90 todo!("WASM is not supported yet")
91}
92
93/// Creates a new customized connection with janus servers.
94#[tracing::instrument(level = Level::TRACE, skip_all)]
95pub async fn custom_connect(
96 interface: impl JanusInterface,
97) -> Result<JaConnection, jarust_interface::Error> {
98 JaConnection::open(interface).await
99}