diff --git a/Cargo.toml b/Cargo.toml index 234b772..f256475 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ description = "Rust bindings for creating Janus plugins." [dependencies] chrono = "0.4" colored = "1.5" +jansson-sys = { path = "jansson-sys" } janus-plugin-sys = { path = "janus-plugin-sys" } \ No newline at end of file diff --git a/janus-plugin-sys/Cargo.toml b/janus-plugin-sys/Cargo.toml index bfd344a..eb3adca 100644 --- a/janus-plugin-sys/Cargo.toml +++ b/janus-plugin-sys/Cargo.toml @@ -2,4 +2,7 @@ name = "janus-plugin-sys" version = "0.1.0" authors = ["Marshall Quander "] -description = "Native bindings to the Janus plugin API." \ No newline at end of file +description = "Native bindings to the Janus plugin API." + +[dependencies] +jansson-sys = { path = "../jansson-sys" } diff --git a/janus-plugin-sys/src/lib.rs b/janus-plugin-sys/src/lib.rs index c8e8437..734403a 100644 --- a/janus-plugin-sys/src/lib.rs +++ b/janus-plugin-sys/src/lib.rs @@ -1,5 +1,8 @@ #![allow(non_camel_case_types)] + +extern crate jansson_sys; use std::os::raw::{c_char, c_int, c_void}; +use jansson_sys::json_t; /// The Janus API version this library's plugins are compatible with. pub const JANUS_PLUGIN_API_VERSION: c_int = 8; @@ -82,31 +85,19 @@ pub struct janus_plugin { pub query_session: unsafe extern "C" fn(handle: *mut janus_plugin_session) -> *mut json_t, } -/// The type of a RapidJSON value. -#[repr(u32)] -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub enum json_type { - JSON_OBJECT = 0, - JSON_ARRAY = 1, - JSON_STRING = 2, - JSON_INTEGER = 3, - JSON_REAL = 4, - JSON_TRUE = 5, - JSON_FALSE = 6, - JSON_NULL = 7, -} - -/// A RapidJSON value. -#[repr(C)] -#[derive(Debug)] -pub struct json_t { - pub type_: json_type, - pub refcount: usize, -} +/// An opaque representation of an SDP offer. +pub enum janus_sdp { } extern "C" { pub static janus_log_timestamps: c_int; + pub fn janus_sdp_free(sdp: *mut janus_sdp); + pub fn janus_sdp_parse(sdp: *const c_char, error: *const c_char, errlen: usize) -> *mut janus_sdp; + pub fn janus_sdp_generate_answer(sdp: *mut janus_sdp, ...) -> *mut janus_sdp; + pub fn janus_sdp_write(sdp: *mut janus_sdp) -> *mut c_char; pub fn janus_plugin_result_new(type_: janus_plugin_result_type, text: *const c_char, content: *mut json_t) -> *mut janus_plugin_result; + pub fn janus_plugin_result_destroy(result: *mut janus_plugin_result); + + /// Writes an entry to the Janus log. The entry is copied synchronously from format into the log buffer + /// and flushed asynchronously to disk/stdout. pub fn janus_vprintf(format: *const c_char, ...); - pub fn json_object() -> *mut json_t; } diff --git a/src/lib.rs b/src/lib.rs index 0f7bfdf..a1a05cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ extern crate chrono; extern crate colored; -extern crate janus_plugin_sys as internal; +extern crate janus_plugin_sys as janus; +extern crate jansson_sys as jansson; use chrono::Local; use colored::{Color, Colorize}; @@ -8,13 +9,13 @@ use std::fmt; use std::fmt::Write; use std::ffi::CString; use std::os::raw::{c_char, c_int}; -pub use internal::JANUS_PLUGIN_API_VERSION as API_VERSION; -pub use internal::janus_callbacks as PluginCallbacks; -pub use internal::janus_plugin as Plugin; -pub use internal::janus_plugin_result as PluginResult; -pub use internal::janus_plugin_result_type as PluginResultType; -pub use internal::janus_plugin_session as PluginSession; -pub use internal::json_t as Json; +pub use janus::JANUS_PLUGIN_API_VERSION as API_VERSION; +pub use janus::janus_callbacks as PluginCallbacks; +pub use janus::janus_plugin as Plugin; +pub use janus::janus_plugin_result as PluginResult; +pub use janus::janus_plugin_result_type as PluginResultType; +pub use janus::janus_plugin_session as PluginSession; +pub use jansson::json_t as Json; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum LogLevel { @@ -48,16 +49,27 @@ impl fmt::Display for LogLevel { } } +/// Writes a message at the given log level to the Janus log. pub fn log(level: LogLevel, message: &str) { let mut output = String::new(); - if unsafe { internal::janus_log_timestamps == 1 } { + if unsafe { janus::janus_log_timestamps == 1 } { write!(output, "{} ", Local::now().format("[%a %b %e %T %Y]")).unwrap() } if level >= LogLevel::Warn { write!(output, "{} ", level).unwrap(); } output.push_str(message); - unsafe { internal::janus_vprintf(CString::new(output).unwrap().as_ptr()) } + unsafe { janus::janus_vprintf(CString::new(output).unwrap().as_ptr()) } +} + +/// Allocates a Janus plugin result. Should be destroyed with destroy_result. +pub fn create_result(type_: PluginResultType, text: *const c_char, content: *mut Json) -> Box { + unsafe { Box::from_raw(janus::janus_plugin_result_new(type_, text, content)) } +} + +/// Destroys a Janus plugin result. +pub fn destroy_result(result: Box) { + unsafe { janus::janus_plugin_result_destroy(Box::into_raw(result)) } } /// Represents metadata about this plugin which Janus can query at runtime.