#![deny(missing_debug_implementations)]
use janus_plugin_sys as ffi;
use bitflags::bitflags;
pub use debug::LogLevel;
pub use debug::log;
pub use jansson::{JanssonDecodingFlags, JanssonEncodingFlags, JanssonValue, RawJanssonValue};
pub use session::SessionWrapper;
pub use ffi::events::janus_eventhandler as EventHandler;
pub use ffi::plugin::janus_callbacks as PluginCallbacks;
pub use ffi::plugin::janus_plugin as Plugin;
pub use ffi::plugin::janus_plugin_result as RawPluginResult;
pub use ffi::plugin::janus_plugin_session as PluginSession;
pub use ffi::plugin::janus_plugin_rtp as PluginRtpPacket;
pub use ffi::plugin::janus_plugin_rtcp as PluginRtcpPacket;
pub use ffi::plugin::janus_plugin_data as PluginDataPacket;
pub use ffi::plugin::janus_plugin_rtp_extensions as PluginRtpExtensions;
use ffi::plugin::janus_plugin_result_type as PluginResultType;
use std::error::Error;
use std::fmt;
use std::ffi::CStr;
use std::mem;
use std::ops::Deref;
use std::os::raw::{c_char, c_int};
use std::ptr;
pub mod debug;
pub mod rtcp;
pub mod sdp;
pub mod session;
pub mod jansson;
pub mod utils;
pub mod refcount;
bitflags! {
pub struct JanusEventType: u32 {
const JANUS_EVENT_TYPE_SESSION = 1 << 0;
const JANUS_EVENT_TYPE_HANDLE = 1 << 1;
const JANUS_EVENT_TYPE_JSEP = 1 << 3; const JANUS_EVENT_TYPE_WEBRTC = 1 << 4;
const JANUS_EVENT_TYPE_MEDIA = 1 << 5;
const JANUS_EVENT_TYPE_PLUGIN = 1 << 6;
const JANUS_EVENT_TYPE_TRANSPORT = 1 << 7;
const JANUS_EVENT_TYPE_CORE = 1 << 8;
}
}
#[derive(Debug, Clone, Copy)]
pub struct JanusError {
pub code: i32
}
pub type JanusResult = Result<(), JanusError>;
impl JanusError {
pub fn to_cstr(self) -> &'static CStr {
unsafe { CStr::from_ptr(ffi::janus_get_api_error(self.code)) }
}
pub fn from(val: i32) -> JanusResult {
match val {
0 => Ok(()),
e => Err(JanusError { code: e })
}
}
}
impl Error for JanusError {}
impl fmt::Display for JanusError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} (code: {})", self.to_cstr().to_str().unwrap()
, self.code)
}
}
#[derive(Debug)]
pub struct PluginResult {
ptr: *mut RawPluginResult,
}
impl PluginResult {
pub unsafe fn new(type_: PluginResultType, text: *const c_char, content: *mut RawJanssonValue) -> Self {
Self { ptr: ffi::plugin::janus_plugin_result_new(type_, text, content) }
}
pub fn ok(response: JanssonValue) -> Self {
unsafe { Self::new(PluginResultType::JANUS_PLUGIN_OK, ptr::null(), response.into_raw()) }
}
pub fn ok_wait(hint: Option<&'static CStr>) -> Self {
let hint_ptr = hint.map(|x| x.as_ptr()).unwrap_or_else(ptr::null);
unsafe { Self::new(PluginResultType::JANUS_PLUGIN_OK_WAIT, hint_ptr, ptr::null_mut()) }
}
pub fn error(msg: &'static CStr) -> Self {
unsafe { Self::new(PluginResultType::JANUS_PLUGIN_ERROR, msg.as_ptr(), ptr::null_mut()) }
}
pub fn into_raw(self) -> *mut RawPluginResult {
let ptr = self.ptr;
mem::forget(self);
ptr
}
}
impl Deref for PluginResult {
type Target = RawPluginResult;
fn deref(&self) -> &RawPluginResult {
unsafe { &*self.ptr }
}
}
impl Drop for PluginResult {
fn drop(&mut self) {
unsafe { ffi::plugin::janus_plugin_result_destroy(self.ptr) }
}
}
unsafe impl Send for PluginResult {}
#[derive(Debug)]
pub struct LibraryMetadata<'a> {
pub api_version: c_int,
pub version: c_int,
pub version_str: &'a CStr,
pub description: &'a CStr,
pub name: &'a CStr,
pub author: &'a CStr,
pub package: &'a CStr,
}
#[macro_export]
macro_rules! build_plugin {
($md:expr, $($cb:ident),*) => {{
extern "C" fn get_api_compatibility() -> c_int { $md.api_version }
extern "C" fn get_version() -> c_int { $md.version }
extern "C" fn get_version_string() -> *const c_char { $md.version_str.as_ptr() }
extern "C" fn get_description() -> *const c_char { $md.description.as_ptr() }
extern "C" fn get_name() -> *const c_char { $md.name.as_ptr() }
extern "C" fn get_author() -> *const c_char { $md.author.as_ptr() }
extern "C" fn get_package() -> *const c_char { $md.package.as_ptr() }
$crate::Plugin {
get_api_compatibility,
get_version,
get_version_string,
get_description,
get_name,
get_author,
get_package,
$($cb,)*
}
}}
}
#[macro_export]
macro_rules! export_plugin {
($pl:expr) => {
#[no_mangle]
pub extern "C" fn create() -> *const $crate::Plugin { $pl }
}
}
#[macro_export]
macro_rules! build_eventhandler {
($md:expr, $mask:expr, $($cb:ident),*) => {{
extern "C" fn get_api_compatibility() -> c_int { $md.api_version }
extern "C" fn get_version() -> c_int { $md.version }
extern "C" fn get_version_string() -> *const c_char { $md.version_str.as_ptr() }
extern "C" fn get_description() -> *const c_char { $md.description.as_ptr() }
extern "C" fn get_name() -> *const c_char { $md.name.as_ptr() }
extern "C" fn get_author() -> *const c_char { $md.author.as_ptr() }
extern "C" fn get_package() -> *const c_char { $md.package.as_ptr() }
$crate::EventHandler {
events_mask: $mask,
get_api_compatibility,
get_version,
get_version_string,
get_description,
get_name,
get_author,
get_package,
$($cb,)*
}
}}
}
#[macro_export]
macro_rules! export_eventhandler {
($evh:expr) => {
#[no_mangle]
pub extern "C" fn create() -> *const $crate::EventHandler { $evh }
}
}