sew-call-client 0.9.6

Sew's native voice engine: LiveKit-protocol signalling and reconnect against Sew's media server, str0m transport with TURN over UDP, TCP and TLS (relay-only when asked), MLS call keys with the mandatory E2EE checks over the call credential and the call socket, the device store, and the kessel-audio bridge.
Documentation
//! `sew-call-client`: Sew's native voice engine. Everything below the UI for one call:
//!
//! - LiveKit-protocol signalling against Sew's media server (the `/rtc/v1` single-PC session from
//!   `sew-lk-compat`) with resume, full reconnect inside a 20 s window and re-join through the
//!   host's [`JoinProvider`];
//! - str0m transport with TURN over UDP, TCP and TLS, relay-only when the join answer or the
//!   engine asks (`media.relay_only` / `force_relay`), production TLS trust (the platform's);
//! - the frame path: kessel-audio's encoded Opus frames are encrypted through the call's keys
//!   ([`e2ee::KeySession`]) and sent; remote frames are decrypted and verified before kessel-audio
//!   ever sees them (no plaintext mode exists);
//! - mute and deafen, participants, speaking levels, and the call credential's renewal;
//! - the call-stats beacon (Sew's `POST /api/v1/me/voice/stats`, when the join answer carries
//!   its terms) and the same per-window numbers for the host ([`stats`], [`Call::stats`]);
//! - recording consent (Sew 0.7.3): a join into a recorded room is held at
//!   [`CallState::ConsentRequired`] until the host answers the notice ([`Call::consent`]); the
//!   call's recording ([`Call::recording`], [`CallEvent::Recording`]) feeds the keys, which
//!   release this device's key to the recorder only once Sew says `granted`;
//!   [`Call::withdraw_consent`] is "Stop recording me". Consent travels through the host's
//!   [`JoinProvider`] with the person's own session.
//!
//! Sew is a self-hosted chat and calls server from Alakazam Labs; this crate is its native call
//! client. It needs a Sew server to join calls on.
//!
//! ```text
//! let client = CallClient::with_mls(ClientConfig::new("com.example.app", data_dir));
//! let call = client.join(provider, JoinRequest::new(target, room)).await?;
//! let audio = AudioBridge::start(&call, kessel_audio::EngineConfig::default())?; // feature `audio`
//! let mut events = call.events();
//! ```
//!
//! The host implements [`JoinProvider`] (its documentation says which crates that needs and has
//! a complete example). The engine runs on Tokio: call [`CallClient::join`] from inside a Tokio
//! runtime.
//!
//! The E2EE layer plugs in through [`e2ee::E2eeProvider`]; `CallClient` cannot be built without
//! one. The real one, and the default ([`CallClient::with_mls`]), is [`e2ee::MlsKeys`]: MLS
//! through `sew-e2ee`, the call socket ([`gateway`]), the device store ([`device`]), the
//! mandatory checks and sew-lk-compat's LkGcm. The only other provider is the test-only
//! `StaticKeys` (feature `testing`). [`CallClient`] says when to use `with_mls` and when
//! [`CallClient::new`].
//!
//! Two labels in these docs: **K2** is Alakazam Labs' platform, which can host Sew
//! (`kessel-audio` and `kessel-video`, the audio and video pipelines used here, are K2's crates;
//! nothing here needs K2). A **Sew version** ("Sew 0.7.3") is a version of the Sew server: next
//! to a feature, the first server version that has it; in the CHANGELOG's headings, the server
//! version that release of this crate is tested against.
//!
//! The [README](https://github.com/Alakazam-211/sew-crates/tree/main/sew-call-client#features)
//! has the cargo features and the platforms each one builds on.
// Test code may unwrap and expect: the packaged crate has no clippy.toml (whose
// allow-unwrap-in-tests and allow-expect-in-tests say so in this repository).
#![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used))]

mod api;
#[cfg(feature = "audio-core")]
mod audio;
mod call;
mod config;
pub mod device;
pub mod e2ee;
mod engine;
pub mod gateway;
mod noise;
mod pc;
mod provider;
#[cfg(feature = "rust-crypto")]
mod rust_crypto;
mod signal;
pub mod stats;
mod transport;
mod video;

#[cfg(feature = "audio")]
pub use audio::platform_audio_config;
#[cfg(feature = "audio-core")]
pub use audio::{
    AUDIO_BACKEND, AudioBridge, AudioBridgeError, AudioSession, BridgeLevels, SessionEvent,
};
pub use call::{
    Call, CallClient, CallCounters, CallError, CallErrorEvent, CallEvent, CallLeaver, CallMoved,
    CallRecording, CallSecurity, CallSnapshot, CallState, EndReason, FrameSender, FrameStream,
    OpusFrame, Participant, RecordingConsent, RecordingMember, RecvItem, RecvOpus, ServerControls,
    Speaking,
};
#[cfg(feature = "testing")]
pub use config::TestingOverrides;
pub use config::{ClientConfig, default_video_h264};
/// kessel-audio, re-exported for hosts that configure the audio engine.
#[cfg(feature = "audio-core")]
pub use kessel_audio;
/// kessel-video, re-exported for hosts that pick or configure the video backend.
#[cfg(feature = "video-core")]
pub use kessel_video;
/// The noise removal setting of the capture path and what runs; with `audio-core`, the control
/// that builds the capture stage ([`AudioBridge::start_with_noise`]).
#[cfg(feature = "audio-core")]
pub use noise::NoiseRemovalControl;
/// The device bench of the noise removal stage (feature `noise-bench`; debug builds only).
#[cfg(feature = "noise-bench")]
pub use noise::bench as noise_bench;
/// The probe of the noise removal stage (feature `noise-probe`; measurement builds only).
#[cfg(feature = "noise-probe")]
pub use noise::probe as noise_probe;
pub use noise::{
    CaptureDspSched, CaptureDspSchedError, CaptureDspSchedFallback, CaptureDspSchedMode,
    CaptureDspScheduling, CaptureStageClock, CaptureTransient, NoiseDfn3State, NoiseModel,
    NoiseRemoval, NoiseRemovalReason, NoiseRemovalState, NoiseStepDownReason, PhoneAudioMode,
};
/// What loading DeepFilterNet3's weights cost, and how long the model stays after the last
/// call by default (feature `dfn3`).
#[cfg(feature = "dfn3")]
pub use noise::{DFN3_IDLE_UNLOAD, Dfn3LoadInfo};
pub use provider::{
    AnswerError, BoxFuture, CONSENT_UNSUPPORTED, CallCredential, ConsentAnswer, ConsentRequired,
    DmCall, JoinAnswer, JoinProvider, JoinRequest, ProviderError, RECORDING_UNSUPPORTED,
    RecordingControl, RecordingRequest, RejoinReason,
};
/// rustls, re-exported for hosts that pass their own crypto provider
/// ([`ClientConfig::with_crypto_provider`]).
pub use rustls;
/// Why DeepFilterNet3's weights were refused (feature `dfn3`).
#[cfg(feature = "dfn3")]
pub use sew_noise::dfn3::LoadError as Dfn3LoadError;
/// The DeepFilterNet3 weights file's name, as an app ships it (feature `dfn3`).
#[cfg(feature = "dfn3")]
pub use sew_noise::dfn3::WEIGHTS_FILE as DFN3_WEIGHTS_FILE;
/// Sew's recording types the engine's recording API carries (from `sew-proto`).
pub use sew_proto::recording::{
    ConsentPrompt, RecordUnavailable, Recording, RecordingNotice, RecordingRef, RecordingTrigger,
};
pub use stats::{
    BeaconOutcome, BeforeFirstWindow, CallStatsReport, PlayoutSource, PlayoutStats, PlayoutTotals,
    StatsWindow,
};
/// TEST ONLY (feature `testing`, off Apple): the stand-in preview handle.
#[cfg(all(
    feature = "video-core",
    not(sew_apple_preview),
    any(test, feature = "testing")
))]
#[doc(hidden)]
pub use video::FakePreview;
/// The zero-hop self-view preview's handle (macOS and iOS with `video`: kessel-video's
/// `ApplePreview`), as `CameraTapItem::PreviewReady` carries it.
#[cfg(all(feature = "video-core", any(sew_preview_events, test)))]
pub use video::PreviewHandle;
/// The per-device video codec preference and the run-time H.264 check it rests on.
#[cfg(feature = "video-core")]
pub use video::h264_encode_detection;
/// Video publishing: the engine's frame API ([`Call::publish_video`]) and its types.
pub use video::{
    BackupSpec, EncodedVideo, LayerRate, LayerSpec, LocalVideo, VideoCaps, VideoControl,
    VideoError, VideoPublication, VideoPublish, VideoSendStats, VideoSender, plan_layers,
    plan_screen_layers,
};
/// Raw camera frames for a native self-view (`video-core`): [`VideoCapture::camera_tap`].
#[cfg(feature = "video-core")]
pub use video::{CameraFeed, CameraFrame, CameraTap, CameraTapItem, CameraTapSender};
/// The video bridge: a kessel-video backend's cameras, screens and encoders into a call, with
/// the in-call self-view.
#[cfg(feature = "video-core")]
#[allow(deprecated)]
pub use video::{
    CameraRequest, CameraState, CodecPlan, PlannedCodec, SelfViewFrames, SelfViewItem, SourceFeed,
    SourceNote, VideoBridge, choose_codec, choose_codecs,
};
/// Video capture (`video-core`): cameras, screens, the OS picker, screen thumbnails and the
/// pre-join preview over a kessel-video backend, as the sources the publish side takes.
#[cfg(feature = "video-core")]
pub use video::{
    CameraSource, Capture, CaptureEnd, CaptureError, CaptureEvent, CaptureKind, CapturePause,
    LocalFrame, PREVIEW_BITRATE_BPS, PREVIEW_FPS, PREVIEW_HEIGHT, PREVIEW_QUEUE, PREVIEW_WIDTH,
    Preview, PreviewFrames, PreviewItem, ScreenChoice, ScreenPick, ScreenSource,
    THUMBNAIL_MAX_WIDTH, VideoCapture, preview_codec, screen_constraints, thumbnail_png,
};
pub use video::{
    CodecPreferenceReason, H264Detection, VideoCodecPreference, effective_publish_codec,
    effective_publish_codec_under_policy,
};
/// A kessel-video backend replaying pre-encoded VP8 and H.264 simulcast streams (test only).
#[cfg(all(feature = "testing", feature = "video-core"))]
pub use video::{FixtureBackend, FixtureError, FixtureH264, FixtureOptions};
pub use video::{
    VideoCodec, VideoFrame, VideoFrameStream, VideoLayerInfo, VideoPause, VideoRecvItem, VideoSink,
    VideoSinkSpec, VideoSource, VideoTrack, VideoTrackView,
};

// aws-lc-rs is never built for the phones; their DTLS/SRTP is the pure-Rust provider.
#[cfg(all(
    any(target_os = "ios", target_os = "android"),
    not(feature = "rust-crypto")
))]
compile_error!(
    "sew-call-client on iOS and Android needs the `mobile` feature (or `rust-crypto`): aws-lc-rs is not built for phones"
);

/// The call peer connection's DTLS/SRTP provider in this build: `"rust-crypto"` (features
/// `rust-crypto`, `mobile`: dimpl's RustCrypto backend and RustCrypto SRTP) or `"aws-lc-rs"`.
pub const CRYPTO_BACKEND: &str = if cfg!(feature = "rust-crypto") {
    "rust-crypto"
} else {
    "aws-lc-rs"
};

/// Android only: hands the app's JNI env and `android.content.Context` to the platform TLS
/// verifier (rustls-platform-verifier), which needs the JVM to reach Android's trust store.
///
/// **Call it once, before the first [`CallClient::join`]** (from `JNI_OnLoad` or the
/// `Application.onCreate` bridge), with the application Context. Without it the first TLS
/// connection (signalling, TURN-TLS, the key routes) panics inside the verifier. Later calls are
/// no-ops. The Android build side is the host's: the app must include the verifier's Kotlin
/// component (the `rustls-platform-verifier-android` package, as that crate's README
/// describes) and keep its classes from the Android code shrinker (ProGuard rules).
///
/// [`jni`] is re-exported so the host passes the version the verifier takes.
#[cfg(target_os = "android")]
pub fn android_init(
    env: &mut jni::JNIEnv<'_>,
    context: jni::objects::JObject<'_>,
) -> Result<(), jni::errors::Error> {
    rustls_platform_verifier::android::init_with_env(env, context)
}

/// The `jni` crate [`android_init`] takes (Android only).
#[cfg(target_os = "android")]
pub use jni;

/// The engine's name and version as Sew sees it (`ClientInfo`, the caps' `sdk_version`).
pub const ENGINE_VERSION: &str = concat!("sew-call-client/", env!("CARGO_PKG_VERSION"));

/// The user agent of the key routes and the call socket (no personal data).
pub const USER_AGENT: &str = concat!(
    "sew-call-client/",
    env!("CARGO_PKG_VERSION"),
    " (+https://github.com/Alakazam-211)"
);

/// Wall-clock unix milliseconds, or a test clock.
pub type Clock = std::sync::Arc<dyn Fn() -> u64 + Send + Sync>;

/// The system clock.
pub fn system_clock() -> Clock {
    use sew_proto::clock::Clock as _;
    std::sync::Arc::new(|| sew_proto::clock::SystemClock.now_unix_ms())
}

/// README.md's examples, compiled and run as doc tests (with the `audio-core` feature: the example starts the audio bridge).
#[cfg(all(doctest, feature = "audio-core"))]
#[doc = include_str!("../README.md")]
struct ReadmeExamples;