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
#![allow(dead_code)]

extern crate lazy_static;

#[macro_use]
mod macros;

#[macro_use]
pub mod connection;
pub mod audio_device;
mod enums;
pub mod log;
pub mod publisher;
pub mod session;
pub mod stream;
pub mod subscriber;
pub mod video_capturer;
pub mod video_frame;

pub use crate::enums::{IntoResult, OtcError, OtcResult};

use std::{ptr, sync::atomic::AtomicBool, sync::atomic::Ordering};

#[cfg(feature = "gstreamer-utils")]
pub mod utils;

static INITIALIZED: AtomicBool = AtomicBool::new(false);

/// Initializes the library. You must call this function before
/// the execution of any other code using the library.
pub fn init() -> OtcResult {
    unsafe { ffi::otc_init(ptr::null_mut()) }
        .into_result()
        .map(move |_| {
            INITIALIZED.store(true, Ordering::Relaxed);
        })
}

/// Destroys the library engine. You should call this function when you are done
/// executing code that uses the library.
pub fn deinit() -> OtcResult {
    audio_device::AudioDevice::stop();
    for (_, publisher) in publisher::INSTANCES.lock().unwrap().drain() {
        let _ = publisher.unpublish();
    }
    for (_, subscriber) in subscriber::INSTANCES.lock().unwrap().drain() {
        let _ = subscriber.unsubscribe();
    }
    for (_, session) in session::INSTANCES.lock().unwrap().drain() {
        let _ = session.disconnect();
    }
    unsafe { ffi::otc_destroy() }.into_result()
}

/// Check if the underlying OpenTok library was successfully initialized or not.
pub fn is_initialized() -> bool {
    INITIALIZED.load(Ordering::Relaxed)
}