getstream 0.1.0-preview.2

Official Rust SDK for Stream Video (server REST + SFU WebRTC).
Documentation
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! A lower-level participant client (`RtcClient`) constructed from an API key
//! and a **user token**, mirroring videosdk / the JS `StreamVideoClient` style.
//!
//! Most callers should use [`crate::Stream`] + [`crate::Call::join`], which mints
//! the user token server-side. `RtcClient` is handy for tests and for callers
//! that already hold a user token and only need the participant path.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use crate::client::{Client, ClientConfig, NetworkLimits};
use crate::error::Result as CrateResult;
use crate::token::{self, TokenOptions};

use super::error::{Result, RtcError};
use super::join::{CallEvent, CallStateSnapshot, CallingState, JoinCallData, RtcCore};
use super::local_track::{LocalAudioTrack, LocalTrack, LocalVideoTrack};
use super::proto::models::TrackType;
use super::remote_track::{RemoteParticipant, RemoteTrack};
use super::subscriptions::{SubscriptionConfig, SubscriptionTarget};

/// Boxed future returned by an RTC [`TokenProvider`].
pub type TokenFuture = Pin<Box<dyn Future<Output = CrateResult<String>> + Send + 'static>>;

/// Asynchronous user-token provider used to recover from Stream error code 40.
///
/// The provider is called once before the initial join and at most once after a
/// token-expired response. Concurrent provider calls are serialized by the
/// owning call.
pub trait TokenProvider: Send + Sync {
    /// Load a user JWT.
    fn load_token(&self) -> TokenFuture;
}

impl<F, Fut> TokenProvider for F
where
    F: Fn() -> Fut + Send + Sync,
    Fut: Future<Output = CrateResult<String>> + Send + 'static,
{
    fn load_token(&self) -> TokenFuture {
        Box::pin((self)())
    }
}

#[derive(Clone)]
pub(crate) enum UserTokenSource {
    Static(String),
    Provider(Arc<dyn TokenProvider>),
    ServerMinted {
        client: Arc<Client>,
        user_id: String,
        call_cid: String,
        expiration: Duration,
    },
}

impl std::fmt::Debug for UserTokenSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Static(_) => f.debug_tuple("Static").field(&"<redacted>").finish(),
            Self::Provider(_) => f.debug_tuple("Provider").field(&"<provider>").finish(),
            Self::ServerMinted {
                user_id,
                call_cid,
                expiration,
                ..
            } => f
                .debug_struct("ServerMinted")
                .field("user_id", user_id)
                .field("call_cid", call_cid)
                .field("expiration", expiration)
                .finish_non_exhaustive(),
        }
    }
}

impl UserTokenSource {
    pub(crate) async fn load(&self, expected_user_id: &str) -> Result<String> {
        let token = match self {
            Self::Static(token) => token.clone(),
            Self::Provider(provider) => provider.load_token().await.map_err(RtcError::from)?,
            Self::ServerMinted {
                client,
                user_id,
                call_cid,
                expiration,
            } => token::create_user_token(
                client.api_secret(),
                user_id,
                &TokenOptions {
                    expiration: Some(*expiration),
                    call_cids: Some(vec![call_cid.clone()]),
                    ..Default::default()
                },
            )
            .map_err(RtcError::from)?,
        };
        token::validate_operational_token(&token, expected_user_id).map_err(RtcError::from)?;
        Ok(token)
    }

    pub(crate) async fn load_with_expiry_retry(&self, expected_user_id: &str) -> Result<String> {
        match self.load(expected_user_id).await {
            Err(error) if error.is_token_expired() && self.can_refresh() => {
                self.load(expected_user_id).await
            }
            result => result,
        }
    }

    pub(crate) fn can_refresh(&self) -> bool {
        !matches!(self, Self::Static(_))
    }

    pub(crate) fn refreshes_before_full_reconnect(&self) -> bool {
        matches!(self, Self::ServerMinted { .. })
    }
}

/// A participant-only client bound to a single user token.
#[derive(Clone)]
pub struct RtcClient {
    client: Arc<Client>,
    token_source: UserTokenSource,
    disconnection_timeout: Duration,
}

impl std::fmt::Debug for RtcClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RtcClient")
            .field("client", &self.client)
            .field("token_source", &self.token_source)
            .field("disconnection_timeout", &self.disconnection_timeout)
            .finish()
    }
}

impl RtcClient {
    /// Build from an API key and a user JWT, using the default coordinator URL
    /// and connection settings.
    pub fn new(api_key: impl Into<String>, user_token: impl Into<String>) -> CrateResult<Self> {
        Self::with_config(api_key, user_token, ClientConfig::default())
    }

    /// Build with a custom [`ClientConfig`] (e.g. an alternate base URL).
    pub fn with_config(
        api_key: impl Into<String>,
        user_token: impl Into<String>,
        config: ClientConfig,
    ) -> CrateResult<Self> {
        // The participant path never mints the server token, but the shared
        // HTTP client needs a non-empty secret to construct; pass a placeholder
        // that is never used to sign participant requests (they use the user
        // token). This keeps a single connection-pool implementation.
        let client = Client::new(api_key.into(), "webrtc-user".to_owned(), config)?;
        Ok(Self {
            client: Arc::new(client),
            token_source: UserTokenSource::Static(user_token.into()),
            disconnection_timeout: Duration::ZERO,
        })
    }

    /// Build with custom connection settings and payload limits.
    pub fn with_config_and_limits(
        api_key: impl Into<String>,
        user_token: impl Into<String>,
        config: ClientConfig,
        limits: NetworkLimits,
    ) -> CrateResult<Self> {
        let client =
            Client::new_with_limits(api_key.into(), "webrtc-user".to_owned(), config, limits)?;
        Ok(Self {
            client: Arc::new(client),
            token_source: UserTokenSource::Static(user_token.into()),
            disconnection_timeout: Duration::ZERO,
        })
    }

    /// Build with an asynchronous token provider.
    ///
    /// Provider tokens are validated for user identity and time claims before
    /// operational use. If Stream rejects a token as expired, the provider is
    /// called once more and the failed join is retried once.
    pub fn with_token_provider<P>(
        api_key: impl Into<String>,
        token_provider: P,
    ) -> CrateResult<Self>
    where
        P: TokenProvider + 'static,
    {
        Self::with_token_provider_and_limits(api_key, token_provider, NetworkLimits::default())
    }

    /// Build with an asynchronous token provider and custom payload limits.
    pub fn with_token_provider_and_limits<P>(
        api_key: impl Into<String>,
        token_provider: P,
        limits: NetworkLimits,
    ) -> CrateResult<Self>
    where
        P: TokenProvider + 'static,
    {
        let client = Client::new_with_limits(
            api_key.into(),
            "webrtc-user".to_owned(),
            ClientConfig::default(),
            limits,
        )?;
        Ok(Self {
            client: Arc::new(client),
            token_source: UserTokenSource::Provider(Arc::new(token_provider)),
            disconnection_timeout: Duration::ZERO,
        })
    }

    /// Set the maximum reconnect duration. Zero keeps reconnecting indefinitely.
    #[must_use]
    pub fn with_disconnection_timeout(mut self, timeout: Duration) -> Self {
        self.disconnection_timeout = timeout;
        self
    }

    /// Join `<call_type>:<call_id>` and return a live [`RtcCall`] handle.
    pub async fn join(
        &self,
        call_type: impl Into<String>,
        call_id: impl Into<String>,
        data: JoinCallData,
    ) -> Result<RtcCall> {
        let core = RtcCore::new(self.client.clone(), call_type.into(), call_id.into());
        core.set_disconnection_timeout(self.disconnection_timeout);
        core.join_with_token_source(self.token_source.clone(), data)
            .await?;
        Ok(RtcCall { core })
    }
}

/// A joined call handle from [`RtcClient::join`].
#[derive(Clone)]
pub struct RtcCall {
    core: Arc<RtcCore>,
}

impl RtcCall {
    /// Subscribe to the typed SFU event stream.
    pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<CallEvent> {
        self.core.subscribe()
    }

    /// Register a callback for typed call events.
    pub fn on<F>(&self, callback: F) -> tokio::task::AbortHandle
    where
        F: Fn(CallEvent) + Send + 'static,
    {
        self.core.on(callback)
    }

    /// Remove a callback registered with [`Self::on`].
    pub fn off(&self, handler: &tokio::task::AbortHandle) {
        self.core.off(handler);
    }

    /// The current calling state.
    pub fn calling_state(&self) -> CallingState {
        self.core.state()
    }

    /// The live session id.
    pub async fn session_id(&self) -> Option<String> {
        self.core.session_id().await
    }

    /// A snapshot of the participants currently known to the SFU.
    pub fn participants(&self) -> Vec<RemoteParticipant> {
        self.core.participants()
    }

    /// Return the latest participant, count, pin, grant, and session snapshot.
    pub fn call_state(&self) -> CallStateSnapshot {
        self.core.call_state()
    }

    /// Register the callback fired for every inbound media track.
    pub fn on_track<F>(&self, callback: F)
    where
        F: Fn(RemoteTrack) + Send + Sync + 'static,
    {
        self.core.on_track(callback);
    }

    /// Publish a local audio track.
    pub async fn publish_audio(&self, track: LocalAudioTrack) -> Result<()> {
        self.core.publish(LocalTrack::Audio(track)).await
    }

    /// Publish a local video track.
    pub async fn publish_video(&self, track: LocalVideoTrack) -> Result<()> {
        self.core
            .publish(LocalTrack::Video {
                track,
                track_type: TrackType::Video,
            })
            .await
    }

    /// Publish a local screen-share track.
    pub async fn publish_screen_share(&self, track: LocalVideoTrack) -> Result<()> {
        self.core
            .publish(LocalTrack::Video {
                track,
                track_type: TrackType::ScreenShare,
            })
            .await
    }

    /// Publish an audio track associated with a screen share.
    pub async fn publish_screen_share_audio(&self, track: LocalAudioTrack) -> Result<()> {
        self.core.publish(LocalTrack::ScreenShareAudio(track)).await
    }

    /// Temporarily mute a published track kind while preserving its sender.
    pub async fn mute_track(&self, track_type: TrackType) -> Result<()> {
        self.core.set_track_muted(track_type, true).await
    }

    /// Resume a temporarily muted published track kind.
    pub async fn unmute_track(&self, track_type: TrackType) -> Result<()> {
        self.core.set_track_muted(track_type, false).await
    }

    /// Enable SFU-side noise cancellation.
    pub async fn start_noise_cancellation(&self) -> Result<()> {
        self.core.start_noise_cancellation().await
    }

    /// Disable SFU-side noise cancellation.
    pub async fn stop_noise_cancellation(&self) -> Result<()> {
        self.core.stop_noise_cancellation().await
    }

    /// Stop publishing a local media track.
    pub async fn stop_publish(&self, track: LocalTrack) -> Result<()> {
        self.core.stop_publish(track).await
    }

    /// Update the remote media subscription policy.
    pub async fn update_subscriptions(&self, config: SubscriptionConfig) -> Result<()> {
        self.core.update_subscriptions(config).await
    }

    /// Subscribe to an exact set of participant-session tracks.
    pub async fn update_subscription_targets(
        &self,
        targets: Vec<SubscriptionTarget>,
    ) -> Result<()> {
        self.core.update_subscription_targets(targets).await
    }

    /// Enable or disable incoming video from every remote participant.
    pub async fn set_incoming_video_enabled(&self, enabled: bool) -> Result<()> {
        self.core.set_incoming_video_enabled(enabled).await
    }

    /// Leave the call, closing the SFU connection and PeerConnections.
    pub async fn leave(&self) -> Result<()> {
        self.core.leave("user requested leave").await
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};

    use base64::Engine;
    use base64::engine::general_purpose::URL_SAFE_NO_PAD;
    use serde_json::json;

    use super::*;
    use crate::error::TokenError;

    fn inspected_token(claims: serde_json::Value) -> String {
        let header = URL_SAFE_NO_PAD.encode(br#"{"alg":"HS256","typ":"JWT"}"#);
        let payload =
            URL_SAFE_NO_PAD.encode(serde_json::to_vec(&claims).expect("serialize claims"));
        let signature = URL_SAFE_NO_PAD.encode([0_u8; 32]);
        format!("{header}.{payload}.{signature}")
    }

    #[test]
    fn client_debug_redacts_static_token() {
        let client = RtcClient::new("key", "rtc-token-must-not-leak").expect("participant client");
        let debug = format!("{client:?}");
        assert!(!debug.contains("rtc-token-must-not-leak"));
        assert!(debug.contains("<redacted>"));
    }

    #[tokio::test]
    async fn static_expired_token_surfaces_typed_error() {
        let source = UserTokenSource::Static(inspected_token(json!({
            "user_id": "user",
            "iat": 1,
            "exp": 2
        })));
        let error = source.load("user").await.expect_err("expired token");
        assert!(matches!(
            error,
            RtcError::TokenValidation(TokenError::Expired { exp: 2, .. })
        ));
    }

    #[tokio::test]
    async fn provider_loads_fresh_tokens() {
        let loads = Arc::new(AtomicUsize::new(0));
        let provider_loads = loads.clone();
        let token = inspected_token(json!({"user_id": "user"}));
        let source = UserTokenSource::Provider(Arc::new(move || {
            provider_loads.fetch_add(1, Ordering::SeqCst);
            let token = token.clone();
            async move { Ok(token) }
        }));

        source.load("user").await.expect("first token");
        source.load("user").await.expect("refreshed token");
        assert_eq!(loads.load(Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn provider_reloads_once_when_first_token_is_expired() {
        let loads = Arc::new(AtomicUsize::new(0));
        let provider_loads = loads.clone();
        let expired = inspected_token(json!({"user_id": "user", "exp": 1}));
        let fresh = inspected_token(json!({"user_id": "user"}));
        let source = UserTokenSource::Provider(Arc::new(move || {
            let load = provider_loads.fetch_add(1, Ordering::SeqCst);
            let token = if load == 0 {
                expired.clone()
            } else {
                fresh.clone()
            };
            async move { Ok(token) }
        }));

        source
            .load_with_expiry_retry("user")
            .await
            .expect("refreshed token");
        assert_eq!(loads.load(Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn server_minted_tokens_are_finite_and_call_scoped() {
        let client = Arc::new(
            Client::new(
                "key".to_owned(),
                "secret".to_owned(),
                ClientConfig::default(),
            )
            .expect("client"),
        );
        let source = UserTokenSource::ServerMinted {
            client: client.clone(),
            user_id: "user".to_owned(),
            call_cid: "default:call".to_owned(),
            expiration: Duration::from_secs(600),
        };

        let token = source.load("user").await.expect("mint token");
        let claims = token::decode_token(client.api_secret(), &token).expect("verify token");
        assert_eq!(claims.call_cids, Some(vec!["default:call".to_owned()]));
        assert_eq!(
            claims.exp.zip(claims.iat).map(|(exp, iat)| exp - iat),
            Some(600)
        );
    }
}