1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
mod config;
mod datachannel;
mod error;
mod peerconnection;

mod sys {
    use std::ffi::CStr;
    use std::os::raw::c_char;

    use datachannel_sys as sys;
    use lazy_static::lazy_static;

    lazy_static! {
        pub(super) static ref INIT_LOGGING: () = {
            let level = match log::max_level() {
                log::LevelFilter::Off => sys::rtcLogLevel_RTC_LOG_NONE,
                log::LevelFilter::Error => sys::rtcLogLevel_RTC_LOG_ERROR,
                log::LevelFilter::Warn => sys::rtcLogLevel_RTC_LOG_WARNING,
                log::LevelFilter::Info => sys::rtcLogLevel_RTC_LOG_INFO,
                log::LevelFilter::Debug => sys::rtcLogLevel_RTC_LOG_DEBUG,
                log::LevelFilter::Trace => sys::rtcLogLevel_RTC_LOG_VERBOSE,
            };
            unsafe { sys::rtcInitLogger(level, Some(log_callback)) };
        };
    }

    unsafe extern "C" fn log_callback(level: sys::rtcLogLevel, message: *const c_char) {
        let message = CStr::from_ptr(message).to_string_lossy();
        match level {
            sys::rtcLogLevel_RTC_LOG_NONE => (),
            sys::rtcLogLevel_RTC_LOG_ERROR => log::error!("{}", message),
            sys::rtcLogLevel_RTC_LOG_WARNING => log::warn!("{}", message),
            sys::rtcLogLevel_RTC_LOG_INFO => log::info!("{}", message),
            sys::rtcLogLevel_RTC_LOG_DEBUG => log::debug!("{}", message),
            sys::rtcLogLevel_RTC_LOG_VERBOSE => log::trace!("{}", message),
            _ => unreachable!(),
        }
    }
}

fn ensure_logging() {
    *sys::INIT_LOGGING;
}

/// An optional function to preload resources, otherwise they will be loaded lazily.
pub fn preload() {
    unsafe { datachannel_sys::rtcPreload() };
}

/// An optional resources cleanup function.
pub fn unload() {
    unsafe { datachannel_sys::rtcCleanup() };
}

pub use crate::config::Config;
pub use crate::datachannel::{DataChannel, MakeDataChannel, Reliability, RtcDataChannel};
pub use crate::peerconnection::{
    ConnectionState, DescriptionType, GatheringState, IceCandidate, PeerConnection,
    RtcPeerConnection, SessionDescription,
};

pub use webrtc_sdp as sdp;