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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#[cfg(not(any(feature = "log", feature = "tracing")))]
compile_error!("one of the features ['log', 'tracing'] must be enabled");

#[cfg(all(feature = "log", feature = "tracing"))]
compile_error!("only one of ['log', 'tracing'] can be enabled");

use std::sync::Once;

mod config;
mod datachannel;
mod error;
mod logger;
mod peerconnection;
mod track;

pub use error::{Error, Result};

static INIT_LOGGING: Once = Once::new();

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

    use datachannel_sys as sys;

    use crate::logger;

    pub(crate) 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 | sys::rtcLogLevel_RTC_LOG_FATAL => {
                logger::error!("{}", message)
            }
            sys::rtcLogLevel_RTC_LOG_WARNING => logger::warn!("{}", message),
            sys::rtcLogLevel_RTC_LOG_INFO => logger::info!("{}", message),
            sys::rtcLogLevel_RTC_LOG_DEBUG => logger::debug!("{}", message),
            sys::rtcLogLevel_RTC_LOG_VERBOSE => logger::trace!("{}", message),
            _ => unreachable!(),
        }
    }
}

#[cfg(feature = "log")]
fn ensure_logging() {
    INIT_LOGGING.call_once(|| {
        let level = match log::max_level() {
            log::LevelFilter::Off => datachannel_sys::rtcLogLevel_RTC_LOG_NONE,
            log::LevelFilter::Error => datachannel_sys::rtcLogLevel_RTC_LOG_ERROR,
            log::LevelFilter::Warn => datachannel_sys::rtcLogLevel_RTC_LOG_WARNING,
            log::LevelFilter::Info => datachannel_sys::rtcLogLevel_RTC_LOG_INFO,
            log::LevelFilter::Debug => datachannel_sys::rtcLogLevel_RTC_LOG_DEBUG,
            log::LevelFilter::Trace => datachannel_sys::rtcLogLevel_RTC_LOG_VERBOSE,
        };
        unsafe { datachannel_sys::rtcInitLogger(level, Some(sys::log_callback)) };
    });
}

fn ffi_string(ffi: &[u8]) -> crate::error::Result<String> {
    use std::ffi::CStr;
    let bytes = CStr::to_bytes(CStr::from_bytes_with_nul(ffi)?);
    Ok(String::from_utf8(bytes.to_vec())?)
}

/// An optional function to enable libdatachannel logging via `tracing`, otherwise it will be disabled.
#[cfg(feature = "tracing")]
pub fn configure_logging(level: tracing::Level) {
    INIT_LOGGING.call_once(|| {
        let level = match level {
            tracing::Level::ERROR => datachannel_sys::rtcLogLevel_RTC_LOG_ERROR,
            tracing::Level::WARN => datachannel_sys::rtcLogLevel_RTC_LOG_WARNING,
            tracing::Level::INFO => datachannel_sys::rtcLogLevel_RTC_LOG_INFO,
            tracing::Level::DEBUG => datachannel_sys::rtcLogLevel_RTC_LOG_DEBUG,
            tracing::Level::TRACE => datachannel_sys::rtcLogLevel_RTC_LOG_VERBOSE,
        };

        unsafe { datachannel_sys::rtcInitLogger(level, Some(sys::log_callback)) };
    });
}

/// 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 cleanup() {
    unsafe { datachannel_sys::rtcCleanup() };
}

pub use crate::config::{CertificateType, RtcConfig, TransportPolicy};
pub use crate::datachannel::{DataChannelHandler, DataChannelInit, Reliability, RtcDataChannel};
pub use crate::peerconnection::{
    fmt_sdp, serde_sdp, CandidatePair, ConnectionState, GatheringState, IceCandidate,
    PeerConnectionHandler, RtcPeerConnection, SdpType, SessionDescription, SignalingState,
};
pub use crate::track::{Codec, Direction, RtcTrack, TrackHandler, TrackInit};
pub use webrtc_sdp as sdp;