tgcalls 0.2.0-beta1

An elegant Rust client for Telegram voice and video calls.
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use ferogram::tl;
use ntgcalls::{
    CallType, ConnectionInfo, ConnectionMode, ConnectionState, FrameData, MediaDescription,
    MediaSegmentPartStatus, NTgCalls, SsrcGroup, StreamDevice, StreamMode, StreamType,
    VideoDescription,
};
use tokio::sync::oneshot;
use tracing::{debug, info, warn};

use crate::{error::TgCallsError, signaling};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CallState {
    Idle,
    Joining,
    Joined,
    Leaving,
}

/// Filled by `join()` right before `connect()`, drained by the
/// `on_connection_change` callback once the state leaves `Connecting`.
type ConnectSlot = Arc<Mutex<Option<oneshot::Sender<Result<(), ntgcalls::Error>>>>>;

const JOIN_MAX_RETRIES: u32 = 3;
const JOIN_RETRY_BASE_DELAY: Duration = Duration::from_millis(500);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParticipantAction {
    Joined,
    Left,
    Updated,
}

/// Everything a bot might want to react to about this call, beyond the
/// return values of `Call`'s own methods.
#[derive(Debug, Clone)]
pub enum CallEvent {
    /// A stream (or a recording target) reached EOF.
    StreamEnded(StreamType, StreamDevice),
    /// A participant joined, left, or changed state (mute, video, etc).
    ParticipantUpdate {
        user_id: i64,
        action: ParticipantAction,
        is_self: bool,
    },
    /// You were removed from (or otherwise left) the call without calling
    /// `leave()` yourself. `Call`'s state is already reset to `Idle` by
    /// the time this fires.
    Left,
    /// The voice chat was ended for everyone.
    Ended,
}

type EventHandler = Arc<dyn Fn(CallEvent) + Send + Sync>;

fn participant_action(p: &tl::types::GroupCallParticipant) -> ParticipantAction {
    if p.just_joined {
        ParticipantAction::Joined
    } else if p.left {
        ParticipantAction::Left
    } else {
        ParticipantAction::Updated
    }
}

#[derive(Default, Clone)]
struct VideoSubs {
    camera_endpoint: Option<String>,
    presentation_endpoint: Option<String>,
}

#[derive(Clone, Copy)]
enum VideoKind {
    Camera,
    Presentation,
}

pub struct Call {
    client: ferogram::Client,
    chat_id: i64,
    ntg: NTgCalls,
    connect_slot: ConnectSlot,
    state: CallState,
    active_call: Option<ferogram::tl::enums::InputGroupCall>,
    presentation_active: bool,
    video_subs: Arc<Mutex<HashMap<i64, VideoSubs>>>,
    event_handler: Option<EventHandler>,
}

/// Whether a join() failure looks transient and worth retrying: flood
/// waits, timeouts, and Telegram's internal (-500) errors. Anything else
/// (bad chat, no active call, auth issues) fails fast instead.
fn is_transient(err: &TgCallsError) -> bool {
    if !matches!(err, TgCallsError::Ferogram(_) | TgCallsError::NtgCalls(_)) {
        return false;
    }
    let msg = err.to_string().to_uppercase();
    msg.contains("FLOOD_WAIT")
        || msg.contains("TIMEOUT")
        || msg.contains("-500")
        || msg.contains("INTERNAL")
        || msg.contains("CONNECTION")
}

fn peer_user_id(peer: &tl::enums::Peer) -> Option<i64> {
    match peer {
        tl::enums::Peer::User(u) => Some(u.user_id),
        _ => None,
    }
}

/// Pulls `(endpoint, ssrc_groups)` out of a participant's camera or
/// presentation field, or `None` if absent/paused.
fn extract_video(
    v: &Option<tl::enums::GroupCallParticipantVideo>,
) -> Option<(String, Vec<SsrcGroup>)> {
    let tl::enums::GroupCallParticipantVideo::GroupCallParticipantVideo(v) = v.as_ref()?;
    if v.paused {
        return None;
    }
    Some((v.endpoint.clone(), to_ssrc_groups(&v.source_groups)))
}

fn to_ssrc_groups(groups: &[tl::enums::GroupCallParticipantVideoSourceGroup]) -> Vec<SsrcGroup> {
    groups
        .iter()
        .map(|g| {
            let tl::enums::GroupCallParticipantVideoSourceGroup::GroupCallParticipantVideoSourceGroup(g) = g;
            SsrcGroup {
                semantics: g.semantics.clone(),
                ssrcs: g.sources.iter().map(|&s| s as u32).collect(),
            }
        })
        .collect()
}

// Telegram supergroup IDs come in as -100xxxxxxxxxx; strip the -100 prefix
// to get the raw channel_id that ntgcalls expects.
fn ntg_chat_id(chat_id: i64) -> i64 {
    if chat_id < 0 {
        let abs = -chat_id;
        if abs > 1_000_000_000_000 {
            abs - 1_000_000_000_000
        } else {
            abs
        }
    } else {
        chat_id
    }
}

impl Call {
    pub fn new(client: ferogram::Client, chat_id: i64) -> Self {
        let mut ntg = NTgCalls::new();
        let connect_slot: ConnectSlot = Arc::new(Mutex::new(None));

        {
            let connect_slot = connect_slot.clone();
            ntg.on_connection_change(move |_chat_id, info: ConnectionInfo| {
                let mut slot = connect_slot.lock().unwrap();
                match info.state {
                    ConnectionState::Connected => {
                        debug!(target: "tgcalls", "connection established");
                        if let Some(tx) = slot.take() {
                            let _ = tx.send(Ok(()));
                        }
                    }
                    ConnectionState::Failed
                    | ConnectionState::Timeout
                    | ConnectionState::Closed => {
                        warn!(target: "tgcalls", "connection ended: {:?}", info.state);
                        if let Some(tx) = slot.take() {
                            let _ = tx.send(Err(ntgcalls::Error {
                                kind: ntgcalls::ErrorKind::Connection,
                                message: format!("connection ended: {:?}", info.state),
                            }));
                        }
                    }
                    ConnectionState::Connecting => {
                        debug!(target: "tgcalls", "connecting...");
                    }
                }
            });
        }

        Self {
            client,
            chat_id,
            ntg,
            connect_slot,
            state: CallState::Idle,
            active_call: None,
            presentation_active: false,
            video_subs: Arc::new(Mutex::new(HashMap::new())),
            event_handler: None,
        }
    }

    /// Registers a handler for everything in [`CallEvent`] - stream end,
    /// participant changes, unexpected removal, the call ending. Call
    /// before `join()`.
    ///
    /// Must be called from inside a Tokio runtime (it is - every caller
    /// runs from inside `rt.block_on` or `#[tokio::main]`). ntgcalls fires
    /// `on_stream_end` (and every other `on_*` callback) synchronously on
    /// its own native WebRTC thread, never on a Tokio thread, so we can't
    /// just call `handler` in place: if `handler` does anything that needs
    /// a runtime (`tokio::spawn`, `.await`, a timer, ...) it aborts with
    /// "there is no reactor running". We capture this thread's `Handle`
    /// now and `spawn` onto it from the callback instead, so `handler`
    /// always runs as a proper task on the runtime that owns this `Call`.
    pub fn on_event(&mut self, handler: impl Fn(CallEvent) + Send + Sync + 'static) {
        let handler: EventHandler = Arc::new(handler);
        let stream_end_handler = handler.clone();
        let rt_handle = tokio::runtime::Handle::current();
        self.ntg
            .on_stream_end(move |_chat_id, stream_type, device| {
                let stream_end_handler = stream_end_handler.clone();
                let event = CallEvent::StreamEnded(stream_type, device);
                // Hop back onto the Tokio runtime before running user code.
                rt_handle.spawn(async move {
                    stream_end_handler(event);
                });
            });
        self.event_handler = Some(handler);
    }

    fn emit(&self, event: CallEvent) {
        if let Some(handler) = &self.event_handler {
            handler(event);
        }
    }

    pub async fn join(&mut self, media: MediaDescription) -> Result<(), TgCallsError> {
        self.join_inner(media, false).await
    }

    /// Like `join`, but starts a new voice chat first if none is active
    /// yet, instead of failing with `NoActiveGroupCall`.
    pub async fn create_and_join(&mut self, media: MediaDescription) -> Result<(), TgCallsError> {
        self.join_inner(media, true).await
    }

    async fn join_inner(
        &mut self,
        media: MediaDescription,
        auto_start: bool,
    ) -> Result<(), TgCallsError> {
        if self.state != CallState::Idle {
            return Err(TgCallsError::AlreadyJoined);
        }

        let mut attempt = 0;
        loop {
            match self.try_join(media.clone(), auto_start).await {
                Ok(()) => return Ok(()),
                Err(e) if attempt < JOIN_MAX_RETRIES && is_transient(&e) => {
                    attempt += 1;
                    let delay = JOIN_RETRY_BASE_DELAY * 2u32.pow(attempt - 1);
                    warn!(
                        "tgcalls: join attempt {} failed ({}), retrying in {:?}",
                        attempt, e, delay
                    );
                    self.state = CallState::Idle;
                    self.active_call = None;
                    tokio::time::sleep(delay).await;
                }
                Err(e) => {
                    self.state = CallState::Idle;
                    return Err(e);
                }
            }
        }
    }

    async fn try_join(
        &mut self,
        media: MediaDescription,
        auto_start: bool,
    ) -> Result<(), TgCallsError> {
        self.state = CallState::Joining;

        let call = if auto_start {
            signaling::resolve_or_create_call(&self.client, self.chat_id).await?
        } else {
            signaling::resolve_call(&self.client, self.chat_id).await?
        };
        let ntg_id = ntg_chat_id(self.chat_id);

        let params_json = self.ntg.create_call(ntg_id).await?;
        debug!(
            "tgcalls: create params ({} bytes): {}",
            params_json.len(),
            params_json
        );

        let (tx, rx) = oneshot::channel();
        *self.connect_slot.lock().unwrap() = Some(tx);

        let transport_json = signaling::join_call(&self.client, call.clone(), &params_json).await?;
        debug!(
            "tgcalls: transport received ({} bytes): {}",
            transport_json.len(),
            transport_json
        );

        self.ntg.connect(ntg_id, &transport_json, false).await?;

        // connect() only dispatches the SDP; Connected arrives via the callback.
        rx.await.expect("connection callback never fired")?;

        self.ntg
            .set_stream_sources(ntg_id, StreamMode::Capture, &media)
            .await?;

        self.active_call = Some(call);
        self.state = CallState::Joined;
        info!("tgcalls: joined chat {}", self.chat_id);
        debug!("tgcalls: stream sources configured");
        Ok(())
    }

    pub async fn leave(&mut self) -> Result<(), TgCallsError> {
        if self.state != CallState::Joined {
            return Err(TgCallsError::NotJoined);
        }
        self.state = CallState::Leaving;

        if self.presentation_active {
            if let Err(e) = self.stop_presentation().await {
                warn!("tgcalls: stop_presentation error (ignored): {}", e);
            }
        }

        if let Err(e) = self.ntg.stop(ntg_chat_id(self.chat_id)).await {
            warn!("tgcalls: ntg stop error (ignored): {}", e);
        }

        let leave_result = match self.active_call.take() {
            Some(call) => signaling::leave_call(&self.client, call, 0).await,
            None => Ok(()),
        };

        // Reset unconditionally, even on a signaling failure: the media
        // stream is already stopped above either way, and getting stuck in
        // `Leaving` forever - unable to join() or leave() again - would be
        // strictly worse than surfacing this error while still recovering.
        self.video_subs.lock().unwrap().clear();
        self.state = CallState::Idle;

        if let Err(e) = leave_result {
            warn!(
                "tgcalls: leaveGroupCall signaling failed (state still reset, safe to retry): {}",
                e
            );
            return Err(e);
        }

        info!("tgcalls: left chat {}", self.chat_id);
        Ok(())
    }

    pub async fn join_presentation(
        &mut self,
        screen: VideoDescription,
    ) -> Result<(), TgCallsError> {
        self.require_joined()?;
        if self.presentation_active {
            return Err(TgCallsError::AlreadyJoined);
        }
        let ntg_id = ntg_chat_id(self.chat_id);
        let params_json = self.ntg.init_presentation(ntg_id).await?;
        debug!("tgcalls: presentation params ({} bytes)", params_json.len());

        let call = self
            .active_call
            .as_ref()
            .ok_or(TgCallsError::NotJoined)?
            .clone();
        let transport_json = signaling::join_presentation(&self.client, call, &params_json).await?;
        debug!(
            "tgcalls: presentation transport received ({} bytes)",
            transport_json.len()
        );

        self.ntg.connect(ntg_id, &transport_json, true).await?;

        let desc = MediaDescription {
            microphone: None,
            speaker: None,
            camera: None,
            screen: Some(screen),
        };
        self.ntg
            .set_stream_sources(ntg_id, StreamMode::Capture, &desc)
            .await?;

        self.presentation_active = true;
        info!("tgcalls: presentation started in chat {}", self.chat_id);
        Ok(())
    }

    pub async fn stop_presentation(&mut self) -> Result<(), TgCallsError> {
        self.require_joined()?;
        if !self.presentation_active {
            return Ok(());
        }
        let ntg_id = ntg_chat_id(self.chat_id);
        if let Err(e) = self.ntg.stop_presentation(ntg_id).await {
            warn!("tgcalls: ntg stop_presentation error (ignored): {}", e);
        }
        let call = self
            .active_call
            .as_ref()
            .ok_or(TgCallsError::NotJoined)?
            .clone();
        let leave_result = signaling::leave_presentation(&self.client, call).await;

        // Same reasoning as leave(): the ntg-level stop already happened
        // above, so don't leave presentation_active stuck true on a
        // signaling failure - that would incorrectly block a future
        // join_presentation() until this is retried.
        self.presentation_active = false;

        leave_result?;
        info!("tgcalls: presentation stopped in chat {}", self.chat_id);
        Ok(())
    }

    pub fn is_presentation_active(&self) -> bool {
        self.presentation_active
    }

    /// `user_id` is the participant whose video you want to subscribe to.
    pub async fn add_incoming_video(
        &mut self,
        user_id: i64,
        endpoint: &str,
        ssrc_groups: &[SsrcGroup],
    ) -> Result<u32, TgCallsError> {
        self.require_joined()?;
        Ok(self
            .ntg
            .add_incoming_video(ntg_chat_id(self.chat_id), user_id, endpoint, ssrc_groups)
            .await?)
    }

    pub async fn remove_incoming_video(&mut self, endpoint: &str) -> Result<bool, TgCallsError> {
        self.require_joined()?;
        Ok(self
            .ntg
            .remove_incoming_video(ntg_chat_id(self.chat_id), endpoint)
            .await?)
    }

    pub async fn send_external_frame(
        &mut self,
        device: StreamDevice,
        frame: &[u8],
        frame_data: &FrameData,
    ) -> Result<(), TgCallsError> {
        self.require_joined()?;
        Ok(self
            .ntg
            .send_external_frame(ntg_chat_id(self.chat_id), device, frame, frame_data)
            .await?)
    }

    pub async fn send_broadcast_timestamp(&mut self, timestamp: i64) -> Result<(), TgCallsError> {
        self.require_joined()?;
        Ok(self
            .ntg
            .send_broadcast_timestamp(ntg_chat_id(self.chat_id), timestamp)
            .await?)
    }

    pub async fn send_broadcast_part(
        &mut self,
        segment_id: i64,
        part_id: i32,
        status: MediaSegmentPartStatus,
        quality_update: bool,
        frame: &[u8],
    ) -> Result<(), TgCallsError> {
        self.require_joined()?;
        Ok(self
            .ntg
            .send_broadcast_part(
                ntg_chat_id(self.chat_id),
                segment_id,
                part_id,
                status,
                quality_update,
                Some(frame),
            )
            .await?)
    }

    pub async fn set_stream_sources(
        &mut self,
        stream_mode: StreamMode,
        media: &MediaDescription,
    ) -> Result<(), TgCallsError> {
        self.require_joined()?;
        self.ntg
            .set_stream_sources(ntg_chat_id(self.chat_id), stream_mode, media)
            .await?;
        Ok(())
    }

    /// Starts recording using a [`Media::record_audio`]/[`Media::record_video`]/
    /// [`Media::record_screen`] description. Shorthand for
    /// `set_stream_sources(StreamMode::Playback, media)`.
    pub async fn record(&mut self, media: &MediaDescription) -> Result<(), TgCallsError> {
        self.set_stream_sources(StreamMode::Playback, media).await
    }

    pub async fn cpu_usage(&mut self) -> Result<f64, TgCallsError> {
        Ok(self.ntg.cpu_usage().await?)
    }

    pub async fn call_type(&mut self) -> Result<CallType, TgCallsError> {
        self.require_joined()?;
        Ok(self.ntg.get_call_type(ntg_chat_id(self.chat_id)).await?)
    }

    pub async fn connection_mode(&mut self) -> Result<ConnectionMode, TgCallsError> {
        self.require_joined()?;
        Ok(self
            .ntg
            .get_connection_mode(ntg_chat_id(self.chat_id))
            .await?)
    }

    pub async fn pause(&mut self) -> Result<(), TgCallsError> {
        self.require_joined()?;
        self.ntg.pause(ntg_chat_id(self.chat_id)).await?;
        Ok(())
    }

    pub async fn resume(&mut self) -> Result<(), TgCallsError> {
        self.require_joined()?;
        self.ntg.resume(ntg_chat_id(self.chat_id)).await?;
        Ok(())
    }

    /// Seconds of the given stream played so far.
    pub async fn played_seconds(&mut self, stream_mode: StreamMode) -> Result<u64, TgCallsError> {
        self.require_joined()?;
        Ok(self
            .ntg
            .time(ntg_chat_id(self.chat_id), stream_mode)
            .await?)
    }

    pub async fn mute(&mut self) -> Result<(), TgCallsError> {
        self.require_joined()?;
        let call = self
            .active_call
            .as_ref()
            .ok_or(TgCallsError::NotJoined)?
            .clone();
        let _ = self.ntg.mute(ntg_chat_id(self.chat_id)).await;
        signaling::set_muted(&self.client, call, true).await
    }

    pub async fn unmute(&mut self) -> Result<(), TgCallsError> {
        self.require_joined()?;
        let call = self
            .active_call
            .as_ref()
            .ok_or(TgCallsError::NotJoined)?
            .clone();
        let _ = self.ntg.unmute(ntg_chat_id(self.chat_id)).await;
        signaling::set_muted(&self.client, call, false).await
    }

    /// Sets how loud `user_id` sounds to *you* in this call. 0 = muted for
    /// you, 10000 = 100% (default), up to 20000 = 200%. This is a
    /// per-listener preference, not a broadcast-wide control.
    pub async fn set_volume(&self, user_id: i64, volume: i32) -> Result<(), TgCallsError> {
        self.require_joined()?;
        let call = self
            .active_call
            .as_ref()
            .ok_or(TgCallsError::NotJoined)?
            .clone();
        let access_hash = signaling::get_user_access_hash(&self.client, user_id).await?;
        signaling::set_volume(&self.client, call, user_id, access_hash, volume).await
    }

    /// Fetches every current participant in the group call (paginated
    /// internally).
    pub async fn get_participants(
        &self,
    ) -> Result<Vec<tl::types::GroupCallParticipant>, TgCallsError> {
        self.require_joined()?;
        let call = self
            .active_call
            .as_ref()
            .ok_or(TgCallsError::NotJoined)?
            .clone();
        signaling::get_participants(&self.client, call).await
    }

    /// The voice chat was discarded for everyone - `discarded_id` is the
    /// group call's raw ID from the `GroupCallDiscarded` update. No-op if
    /// it isn't this call's ID.
    pub fn handle_call_ended(&mut self, discarded_id: i64) {
        if self.group_call_id() != Some(discarded_id) {
            return;
        }
        self.emit(CallEvent::Ended);
        self.state = CallState::Idle;
        self.active_call = None;
    }

    /// Auto subscribes/unsubscribes incoming video as participants' camera
    /// or screen-share sources change. [`crate::Calls`] wires this for you;
    /// call it directly only if you're managing `Call` yourself.
    pub async fn handle_participants_update(
        &mut self,
        participants: &[tl::enums::GroupCallParticipant],
    ) -> Result<(), TgCallsError> {
        if self.state != CallState::Joined {
            return Ok(());
        }

        for p in participants {
            let tl::enums::GroupCallParticipant::GroupCallParticipant(p) = p;
            let Some(user_id) = peer_user_id(&p.peer) else {
                continue;
            };

            self.emit(CallEvent::ParticipantUpdate {
                user_id,
                action: participant_action(p),
                is_self: p.is_self,
            });

            if p.is_self {
                if p.left {
                    self.emit(CallEvent::Left);
                    self.state = CallState::Idle;
                    self.active_call = None;
                    return Ok(());
                }
                continue;
            }

            let camera = if p.left {
                None
            } else {
                extract_video(&p.video)
            };
            let presentation = if p.left {
                None
            } else {
                extract_video(&p.presentation)
            };

            self.sync_video_source(user_id, VideoKind::Camera, camera)
                .await;
            self.sync_video_source(user_id, VideoKind::Presentation, presentation)
                .await;
        }
        Ok(())
    }

    /// Adds/removes an incoming video subscription for one participant's
    /// camera or presentation source, only when the endpoint actually
    /// changed. Never holds the tracking lock across an await.
    async fn sync_video_source(
        &mut self,
        user_id: i64,
        kind: VideoKind,
        wanted: Option<(String, Vec<SsrcGroup>)>,
    ) {
        let current = {
            let subs = self.video_subs.lock().unwrap();
            subs.get(&user_id).and_then(|e| match kind {
                VideoKind::Camera => e.camera_endpoint.clone(),
                VideoKind::Presentation => e.presentation_endpoint.clone(),
            })
        };
        let wanted_endpoint = wanted.as_ref().map(|(e, _)| e.clone());
        if current == wanted_endpoint {
            return;
        }

        if let Some(old) = current {
            let _ = self.remove_incoming_video(&old).await;
        }

        match wanted {
            Some((endpoint, groups)) => {
                if self
                    .add_incoming_video(user_id, &endpoint, &groups)
                    .await
                    .is_err()
                {
                    return;
                }
                let mut subs = self.video_subs.lock().unwrap();
                let entry = subs.entry(user_id).or_default();
                match kind {
                    VideoKind::Camera => entry.camera_endpoint = Some(endpoint),
                    VideoKind::Presentation => entry.presentation_endpoint = Some(endpoint),
                }
            }
            None => {
                let mut subs = self.video_subs.lock().unwrap();
                if let Some(entry) = subs.get_mut(&user_id) {
                    match kind {
                        VideoKind::Camera => entry.camera_endpoint = None,
                        VideoKind::Presentation => entry.presentation_endpoint = None,
                    }
                    if entry.camera_endpoint.is_none() && entry.presentation_endpoint.is_none() {
                        subs.remove(&user_id);
                    }
                }
            }
        }
    }

    pub fn state(&self) -> CallState {
        self.state
    }

    /// The active group call's raw ID, used by [`crate::Calls`] to route
    /// `GroupCallParticipants` updates back to the right chat. `None` if
    /// not currently joined.
    pub fn group_call_id(&self) -> Option<i64> {
        match self.active_call.as_ref()? {
            tl::enums::InputGroupCall::InputGroupCall(c) => Some(c.id),
            _ => None,
        }
    }

    pub fn is_joined(&self) -> bool {
        self.state == CallState::Joined
    }

    pub async fn media_state(&mut self) -> Result<ntgcalls::MediaState, TgCallsError> {
        self.require_joined()?;
        Ok(self.ntg.get_state(ntg_chat_id(self.chat_id)).await?)
    }

    fn require_joined(&self) -> Result<(), TgCallsError> {
        if self.state != CallState::Joined {
            return Err(TgCallsError::NotJoined);
        }
        Ok(())
    }
}

impl Drop for Call {
    fn drop(&mut self) {
        if self.state != CallState::Joined {
            return;
        }
        warn!("tgcalls: Call dropped while still joined - stopping stream");

        // Drop can't await; ntg.stop() must run via block_on since NTgCalls
        // isn't Sync and can't cross Handle::spawn's Send bound.
        let ntg = std::mem::take(&mut self.ntg);
        let chat_id = ntg_chat_id(self.chat_id);
        if let Ok(handle) = tokio::runtime::Handle::try_current() {
            let handle2 = handle.clone();
            handle.spawn_blocking(move || {
                handle2.block_on(async move {
                    let _ = ntg.stop(chat_id).await;
                });
            });
        }
    }
}