tauri_plugin_sew_voice/lib.rs
1//! `tauri-plugin-sew-voice`: Sew native voice calls in a Tauri 2 app.
2//!
3//! The webview draws the UI and sends eight commands (`plugin:sew-voice|join`, `leave`, `mute`,
4//! `devices`, `set_device`, `state`, and for recording consent `consent` and
5//! `withdraw_consent`); the plugin answers and emits six events (`sew-voice://state`,
6//! `participants`, `speaking`, `errors`, `devices`, `recording`). Sign-in stays in the host's
7//! Rust: the host registers a [`JoinProvider`] that obtains Sew's join answer (and sends the
8//! person's recording consent with the person's own session), so no token ever reaches the
9//! webview.
10//!
11//! ```ignore
12//! tauri::Builder::default()
13//! .plugin(tauri_plugin_sew_voice::Builder::new(MyProvider::new()).build())
14//! .run(tauri::generate_context!())?;
15//! ```
16//!
17//! The engine is `sew-call-client` ([`real::CallClientEngine`], features `audio-core`/`audio`);
18//! [`fake::FakeEngine`] stands in for it in tests. Without the `tauri` feature the crate is the
19//! plugin's core ([`VoiceService`], the JSON shapes in [`wire`], the engine interface in
20//! [`engine`], the real and the fake engine), which builds and tests without Tauri's system
21//! libraries. The crate's
22//! [README](https://github.com/Alakazam-211/sew-crates/tree/main/tauri-plugin-sew-voice#readme)
23//! is the guide: setup, the capability file, every command with its permission set, and
24//! shipping DeepFilterNet3's weights. The
25//! [native voice SDK guide](https://github.com/Alakazam-211/sew-crates/blob/main/docs/native-voice-sdk.md)
26//! has each command's arguments, answers and errors; the exact JSON is pinned in the package's
27//! `contracts/` folder.
28//!
29//! Sew is Alakazam Labs' self-hosted chat and calls server; the plugin needs one to join calls
30//! on. **K2** is Alakazam Labs' platform, which can host Sew (the audio and video pipelines
31//! `kessel-audio` and `kessel-video` are K2's crates; nothing here needs K2). A **Sew version**
32//! ("Sew 0.9.0") is a version of the Sew server: next to a feature, the first server version
33//! that has it; in the CHANGELOG's headings, the server version that release of this plugin is
34//! tested against. A plain version ("0.4.1") is the plugin's own.
35//!
36//! **Call details (0.4.1).** Beside the frozen eight, the `call_details`, `ring` and
37//! `ring_ended` commands (permission set `sew-voice:call-details`) and the `security`, `roster`,
38//! `quality` and `incoming` events give a call UI the key state and security code, each media
39//! participant's engine identity and label, the connection quality and an incoming DM call
40//! ([`details`]).
41//!
42//! **Record and Stop (Sew 0.9.0).** The `start_recording` and `stop_recording` commands
43//! ([`RECORDING_COMMANDS`], permission set `sew-voice:recording`) send the person's Record and
44//! Stop through the host's provider ([`JoinProvider::recording`]) with the person's own
45//! session, as consent goes.
46//!
47//! **Which buttons (0.6.1).** The host tells the plugin the person's rights in the call's room
48//! with `set_recording_rights` ([`RECORDING_RIGHTS_COMMANDS`], in `sew-voice:recording`;
49//! [`RecordingRights`]); the plugin then says whether Sew would take a Record or a Stop now,
50//! in the `recording_controls` event ([`RecordingControls`]) and the `recording` event's
51//! `can_record` and `can_stop`, and updates them as the call changes.
52//!
53//! **Record and Stop from Sew (0.7.0).** A Sew server that computes each person's Record and
54//! Stop sends them to the call; the plugin then passes them on in the same events, with
55//! `source: "sew"` ([`ControlsSource`]) and Sew's `record_unavailable` hint, and the host's
56//! rights are not used. `set_recording_rights` stays for Sew servers that don't send recording
57//! controls.
58//!
59//! **Phones (0.3.0).** A CallKit or Telecom shim registers a [`VoiceListener`]
60//! (`Builder::listener`), which sees the call whether or not a webview is awake, and acts
61//! through [`VoiceControl`] (Tauri state): join, leave, mute, consent, the snapshot on wake,
62//! `network_changed` and the audio-session hooks it forwards to kessel-audio. On iOS and
63//! Android the plugin lists no devices between calls (the OS owns the route) and has no
64//! keyring backend. It has no Swift or Kotlin of its own: on mobile it registers exactly as on
65//! desktop (`.plugin(Builder::new(provider).build())` in the app's mobile entry point).
66//!
67//! **Video.** More commands ([`VIDEO_COMMANDS`] and the codec setting's
68//! [`VIDEO_CODEC_COMMANDS`], the `sew-voice:video` permission set) and four events (`sew-voice://video`, `local_video`, `video_devices`,
69//! `policy`). Remote and self-view frames reach the webview over a Tauri raw `Channel` per
70//! element, a 32-byte header ([`video::FrameHeader`]) then the encoded frame, decoded by the
71//! webview's WebCodecs; keys never leave Rust. The engine side is in [`video`]: every video
72//! method of [`VoiceEngine`] and [`CallHandle`] defaults to "no video", so an engine without it
73//! is unchanged.
74// Test code may unwrap and expect: the packaged crate has no clippy.toml (whose
75// allow-unwrap-in-tests and allow-expect-in-tests say so in this repository).
76#![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used))]
77
78pub mod details;
79pub mod engine;
80pub mod fake;
81#[cfg(feature = "video-core")]
82pub mod kessel;
83pub mod listener;
84#[cfg(feature = "video-core")]
85pub mod native;
86#[cfg(feature = "noise-probe")]
87mod probe;
88#[cfg(feature = "audio-core")]
89pub mod real;
90mod rights;
91/// The iPhone screen share's ring (feature `ios-screen-ring` with `video-core`; iOS, and Linux
92/// for tests off the device).
93#[cfg(all(
94 feature = "ios-screen-ring",
95 feature = "video-core",
96 any(target_os = "ios", target_os = "linux")
97))]
98pub mod screen_ring;
99pub mod service;
100mod sink;
101pub mod video;
102pub mod wire;
103
104#[cfg(feature = "tauri")]
105mod commands;
106#[cfg(feature = "tauri")]
107mod plugin;
108
109pub use details::{
110 CallDetails, DETAIL_COMMANDS, DETAIL_EVENTS, IncomingCall, QualityInfo, RingEndedArgs,
111 RosterEntry, SecurityInfo,
112};
113pub use engine::{
114 AnswerError, AppScene, AudioSessionEvent, BoxFuture, CONSENT_UNSUPPORTED, CallCredential,
115 CallEvent, CallHandle, CallSnapshot, ConsentAnswer, ConsentRequired, DmCall, EngineError,
116 JoinAnswer, JoinProvider, JoinRequest, ProviderError, RECORDING_UNSUPPORTED, RecordingControl,
117 RecordingRequest, RejoinReason, ServerControls, VideoSnapshot, VoiceEngine, app_server_answer,
118};
119pub use listener::{VoiceControl, VoiceListener};
120#[cfg(feature = "tauri")]
121pub use plugin::{Builder, TauriSink};
122pub use service::{EventSink, SPEAKING_MIN_INTERVAL, Spawn, VoiceService};
123/// The engine crate, for hosts that build [`real::CallClientEngine`] themselves.
124pub use sew_call_client;
125/// The simulator's and x86 emulator's software stand-in (feature `sim-software-key`).
126#[cfg(feature = "sim-software-key")]
127pub use sew_call_client::device::SimSoftwareKey;
128/// The host's own device key store (`Builder::device_key_store`), re-exported, with the
129/// record's public half (sew-e2ee's `StoredDevice`), so a host needs no sew-e2ee dependency.
130pub use sew_call_client::device::{
131 DeviceError, DeviceKeyId, DeviceKeyRecord, DeviceKeyStore, MemoryDeviceKeyStore, StoredDevice,
132};
133/// The hardware device key (`Builder::hardware_device_key`: Secure Enclave, StrongBox/TEE) and
134/// the enrolment platform (`Builder::device_platform`), re-exported.
135pub use sew_call_client::device::{DevicePlatform, HardwareDeviceKey};
136/// A probed call's phase, by how the audio pipeline's processing thread is scheduled in it
137/// (feature `noise-probe`; measurement builds only).
138#[cfg(feature = "noise-probe")]
139pub use sew_call_client::noise_probe::NoiseProbePhase;
140/// What the audio pipeline's processing thread asks the OS for
141/// ([`VoiceControl::set_dsp_sched`]): the setting, its mode, why a setting is refused, and why
142/// a call's thread fell back to the default mode (sew-call-client's types, under the plugin's
143/// names).
144pub use sew_call_client::{
145 CaptureDspSched as DspSched, CaptureDspSchedError as DspSchedError,
146 CaptureDspSchedFallback as DspSchedFallback, CaptureDspSchedMode as DspSchedMode,
147};
148/// The consent notice in the Sew App kit's words (`ConsentPrompt::title_en`, `text_en`,
149/// `choices_en`), from `sew-proto`.
150pub use sew_proto::recording::{ConsentAsk, ConsentChoice};
151pub use sink::FrameChannel;
152pub use video::{
153 CameraRequest, DecodeFailure, DecodedFormat, DecodedFrame, DecoderSpec, DecoderWake,
154 EncodedVideoFrame, FrameDecoder, FrameHeader, ParentWindow, PreviewRequest, ScreenRequest,
155 TrackSettings, VideoFrames, VideoRecvItem,
156};
157pub use wire::{
158 CallState, ConsentArgs, ConsentPrompt, ControlsSource, DEFAULT_DEVICE, Device, DeviceChoice,
159 DeviceKeyInfo, DeviceList, ErrorInfo, ForgetServerArgs, JoinArgs, JoinOptions, JoinResult,
160 MuteArgs, Participant, PluginEvent, RecordingControls, RecordingInfo, RecordingParticipant,
161 RecordingRights, SelfMute, SetDeviceArgs, Speaking, StateEvent, StateSnapshot,
162};
163pub use wire::{
164 Camera, CameraArgs, CodecPreferenceInfo, CodecPreferenceReason, Facing, LocalVideo,
165 LocalVideoState, PermissionState, PreviewAnswer, PreviewArgs, ScreenArgs, ScreenCaps,
166 ScreenKind, ScreenSource, SetVideoCodecPreferenceArgs, SetVideoDeviceArgs, SinkIdArgs,
167 SinkMode, SinkStats, Tile, VideoCaps, VideoCodec, VideoCodecPreference, VideoDevices,
168 VideoPaused, VideoPermissions, VideoSinkAnswer, VideoSinkArgs, VideoSinkUpdateArgs,
169 VideoSource, VideoTrack,
170};
171/// The microphone's noise removal (`set_noise_removal`, `noise_removal`, the `noise_removal`
172/// event).
173pub use wire::{
174 CaptureDspScheduling, CaptureStageClock, CaptureTransient, NoiseDfn3State, NoiseModel,
175 NoiseRemoval, NoiseRemovalReason, NoiseRemovalState, NoiseStepDownReason, PhoneAudioMode,
176 SetNoiseRemovalArgs, UNAVAILABLE_NOISE_MODES,
177};
178
179// On phones the device key is the host's (Builder::device_key_store, or a hardware key):
180// keyring's backends are desktop-only.
181#[cfg(all(
182 feature = "keyring-store",
183 any(target_os = "ios", target_os = "android")
184))]
185compile_error!(
186 "tauri-plugin-sew-voice: `keyring-store` is desktop-only; on iOS and Android pass the \
187 host's store with Builder::device_key_store"
188);
189
190/// The plugin's name: commands are `plugin:sew-voice|<command>`, permissions `sew-voice:*`.
191pub const PLUGIN_NAME: &str = "sew-voice";
192
193/// The eight commands, as the webview invokes them after `plugin:sew-voice|`.
194pub const COMMANDS: [&str; 8] = [
195 "join",
196 "leave",
197 "mute",
198 "devices",
199 "set_device",
200 "state",
201 "consent",
202 "withdraw_consent",
203];
204
205/// The device-key commands (0.4.1), outside `sew-voice:default`: a window gets them only with
206/// an explicit permission (`sew-voice:allow-forget-server`, `sew-voice:allow-known-device-keys`,
207/// or the set `sew-voice:device-keys`). `forget_server {install_id}` deletes the devices of one
208/// Sew install (a sign-out) and answers the removed keys; `known_device_keys` lists the keys
209/// the plugin's device store holds.
210pub const DEVICE_KEY_COMMANDS: [&str; 2] = ["forget_server", "known_device_keys"];
211
212/// The recording commands (Sew 0.9.0), outside `sew-voice:default`: a window gets them with
213/// the set `sew-voice:recording` or each one's own `sew-voice:allow-<command>`.
214/// `start_recording` (Record) and `stop_recording` (Stop) are the person's own acts, sent by
215/// the host's provider with the person's session ([`JoinProvider::recording`]); Sew decides
216/// who may.
217pub const RECORDING_COMMANDS: [&str; 2] = ["start_recording", "stop_recording"];
218
219/// The recording rights' command (plugin 0.6.1), in the `sew-voice:recording` set after
220/// [`RECORDING_COMMANDS`] (its own permission `sew-voice:allow-set-recording-rights`):
221/// `set_recording_rights {room, my_perms, dm?, recording_enabled?, recording_dm_enabled?,
222/// record_capped?, agent?}` ([`RecordingRights`]) gives the plugin what it needs for
223/// [`RecordingControls`]. It answers `null`, or `invalid_args` when `my_perms` is not a
224/// decimal number.
225pub const RECORDING_RIGHTS_COMMANDS: [&str; 1] = ["set_recording_rights"];
226
227/// The video commands, outside `sew-voice:default`: a window gets them with the set
228/// `sew-voice:video` (all thirteen, and [`VIDEO_CODEC_COMMANDS`]) or each one's own
229/// `sew-voice:allow-<command>`. With an engine
230/// without video, `video_caps` answers "nothing" and the others reject `video_unsupported`.
231pub const VIDEO_COMMANDS: [&str; 13] = [
232 "video_caps",
233 "video_devices",
234 "set_video_device",
235 "camera",
236 "screen_sources",
237 "screen",
238 "preview",
239 "video_sink",
240 "video_sink_update",
241 "video_sink_close",
242 "request_keyframe",
243 "camera_permission",
244 "request_camera_permission",
245];
246
247/// The video codec setting's command, in the `sew-voice:video` set after [`VIDEO_COMMANDS`]
248/// (its own permission `sew-voice:allow-set-video-codec-preference`):
249/// `set_video_codec_preference {preference}` (`"automatic"`, `"prefer_h264"`, `"prefer_vp8"`)
250/// sets this device's video codec preference and answers `video_caps.codec_preference`
251/// (`{setting, effective, reason}`). The engine keeps it until the process ends; persisting it
252/// is the host's job.
253pub const VIDEO_CODEC_COMMANDS: [&str; 1] = ["set_video_codec_preference"];
254
255/// The noise removal commands, outside `sew-voice:default`: a window gets them with the set
256/// `sew-voice:noise-removal` or each one's own `sew-voice:allow-<command>`.
257/// `set_noise_removal {mode}` (`"off"`, `"standard"`) sets the microphone's noise removal,
258/// before or during a call, and answers `{mode, active, reason?}`; `"strong"` answers
259/// `noise_mode_unavailable` in this version. `noise_removal` answers the same shape; the
260/// `noise_removal` event carries each change. The engine keeps the setting until the process
261/// ends; persisting it is the host's job.
262pub const NOISE_COMMANDS: [&str; 2] = ["set_noise_removal", "noise_removal"];
263
264/// README.md's examples, compiled and run as doc tests (with the `tauri` feature).
265#[cfg(all(doctest, feature = "tauri"))]
266#[doc = include_str!("../README.md")]
267struct ReadmeExamples;