prod.rs

  1use anyhow::{anyhow, Context, Result};
  2use core_foundation::{
  3    array::{CFArray, CFArrayRef},
  4    base::{CFRelease, CFRetain, TCFType},
  5    string::{CFString, CFStringRef},
  6};
  7use futures::{
  8    channel::{mpsc, oneshot},
  9    Future,
 10};
 11pub use media::core_video::CVImageBuffer;
 12use media::core_video::CVImageBufferRef;
 13use parking_lot::Mutex;
 14use postage::watch;
 15use std::{
 16    ffi::c_void,
 17    sync::{Arc, Weak},
 18};
 19
 20// SAFETY: Most live kit types are threadsafe:
 21// https://github.com/livekit/client-sdk-swift#thread-safety
 22macro_rules! pointer_type {
 23    ($pointer_name:ident) => {
 24        #[repr(transparent)]
 25        #[derive(Copy, Clone, Debug)]
 26        pub struct $pointer_name(pub *const std::ffi::c_void);
 27        unsafe impl Send for $pointer_name {}
 28    };
 29}
 30
 31mod swift {
 32    pointer_type!(Room);
 33    pointer_type!(LocalAudioTrack);
 34    pointer_type!(RemoteAudioTrack);
 35    pointer_type!(LocalVideoTrack);
 36    pointer_type!(RemoteVideoTrack);
 37    pointer_type!(LocalTrackPublication);
 38    pointer_type!(RemoteTrackPublication);
 39    pointer_type!(MacOSDisplay);
 40    pointer_type!(RoomDelegate);
 41}
 42
 43extern "C" {
 44    fn LKRoomDelegateCreate(
 45        callback_data: *mut c_void,
 46        on_did_disconnect: extern "C" fn(callback_data: *mut c_void),
 47        on_did_subscribe_to_remote_audio_track: extern "C" fn(
 48            callback_data: *mut c_void,
 49            publisher_id: CFStringRef,
 50            track_id: CFStringRef,
 51            remote_track: swift::RemoteAudioTrack,
 52            remote_publication: swift::RemoteTrackPublication,
 53        ),
 54        on_did_unsubscribe_from_remote_audio_track: extern "C" fn(
 55            callback_data: *mut c_void,
 56            publisher_id: CFStringRef,
 57            track_id: CFStringRef,
 58        ),
 59        on_mute_changed_from_remote_audio_track: extern "C" fn(
 60            callback_data: *mut c_void,
 61            track_id: CFStringRef,
 62            muted: bool,
 63        ),
 64        on_active_speakers_changed: extern "C" fn(
 65            callback_data: *mut c_void,
 66            participants: CFArrayRef,
 67        ),
 68        on_did_subscribe_to_remote_video_track: extern "C" fn(
 69            callback_data: *mut c_void,
 70            publisher_id: CFStringRef,
 71            track_id: CFStringRef,
 72            remote_track: swift::RemoteVideoTrack,
 73        ),
 74        on_did_unsubscribe_from_remote_video_track: extern "C" fn(
 75            callback_data: *mut c_void,
 76            publisher_id: CFStringRef,
 77            track_id: CFStringRef,
 78        ),
 79    ) -> swift::RoomDelegate;
 80
 81    fn LKRoomCreate(delegate: swift::RoomDelegate) -> swift::Room;
 82    fn LKRoomConnect(
 83        room: swift::Room,
 84        url: CFStringRef,
 85        token: CFStringRef,
 86        callback: extern "C" fn(*mut c_void, CFStringRef),
 87        callback_data: *mut c_void,
 88    );
 89    fn LKRoomDisconnect(room: swift::Room);
 90    fn LKRoomPublishVideoTrack(
 91        room: swift::Room,
 92        track: swift::LocalVideoTrack,
 93        callback: extern "C" fn(*mut c_void, swift::LocalTrackPublication, CFStringRef),
 94        callback_data: *mut c_void,
 95    );
 96    fn LKRoomPublishAudioTrack(
 97        room: swift::Room,
 98        track: swift::LocalAudioTrack,
 99        callback: extern "C" fn(*mut c_void, swift::LocalTrackPublication, CFStringRef),
100        callback_data: *mut c_void,
101    );
102    fn LKRoomUnpublishTrack(room: swift::Room, publication: swift::LocalTrackPublication);
103
104    fn LKRoomAudioTracksForRemoteParticipant(
105        room: swift::Room,
106        participant_id: CFStringRef,
107    ) -> CFArrayRef;
108
109    fn LKRoomAudioTrackPublicationsForRemoteParticipant(
110        room: swift::Room,
111        participant_id: CFStringRef,
112    ) -> CFArrayRef;
113
114    fn LKRoomVideoTracksForRemoteParticipant(
115        room: swift::Room,
116        participant_id: CFStringRef,
117    ) -> CFArrayRef;
118
119    fn LKVideoRendererCreate(
120        callback_data: *mut c_void,
121        on_frame: extern "C" fn(callback_data: *mut c_void, frame: CVImageBufferRef) -> bool,
122        on_drop: extern "C" fn(callback_data: *mut c_void),
123    ) -> *const c_void;
124
125    fn LKRemoteAudioTrackGetSid(track: swift::RemoteAudioTrack) -> CFStringRef;
126    fn LKVideoTrackAddRenderer(track: swift::RemoteVideoTrack, renderer: *const c_void);
127    fn LKRemoteVideoTrackGetSid(track: swift::RemoteVideoTrack) -> CFStringRef;
128
129    fn LKDisplaySources(
130        callback_data: *mut c_void,
131        callback: extern "C" fn(
132            callback_data: *mut c_void,
133            sources: CFArrayRef,
134            error: CFStringRef,
135        ),
136    );
137    fn LKCreateScreenShareTrackForDisplay(display: swift::MacOSDisplay) -> swift::LocalVideoTrack;
138    fn LKLocalAudioTrackCreateTrack() -> swift::LocalAudioTrack;
139
140    fn LKLocalTrackPublicationSetMute(
141        publication: swift::LocalTrackPublication,
142        muted: bool,
143        on_complete: extern "C" fn(callback_data: *mut c_void, error: CFStringRef),
144        callback_data: *mut c_void,
145    );
146
147    fn LKRemoteTrackPublicationSetEnabled(
148        publication: swift::RemoteTrackPublication,
149        enabled: bool,
150        on_complete: extern "C" fn(callback_data: *mut c_void, error: CFStringRef),
151        callback_data: *mut c_void,
152    );
153
154    fn LKRemoteTrackPublicationIsMuted(publication: swift::RemoteTrackPublication) -> bool;
155    fn LKRemoteTrackPublicationGetSid(publication: swift::RemoteTrackPublication) -> CFStringRef;
156}
157
158pub type Sid = String;
159
160#[derive(Clone, Eq, PartialEq)]
161pub enum ConnectionState {
162    Disconnected,
163    Connected { url: String, token: String },
164}
165
166pub struct Room {
167    native_room: Mutex<swift::Room>,
168    connection: Mutex<(
169        watch::Sender<ConnectionState>,
170        watch::Receiver<ConnectionState>,
171    )>,
172    remote_audio_track_subscribers: Mutex<Vec<mpsc::UnboundedSender<RemoteAudioTrackUpdate>>>,
173    remote_video_track_subscribers: Mutex<Vec<mpsc::UnboundedSender<RemoteVideoTrackUpdate>>>,
174    _delegate: Mutex<RoomDelegate>,
175}
176
177trait AssertSendSync: Send {}
178impl AssertSendSync for Room {}
179
180impl Room {
181    pub fn new() -> Arc<Self> {
182        Arc::new_cyclic(|weak_room| {
183            let delegate = RoomDelegate::new(weak_room.clone());
184            Self {
185                native_room: Mutex::new(unsafe { LKRoomCreate(delegate.native_delegate) }),
186                connection: Mutex::new(watch::channel_with(ConnectionState::Disconnected)),
187                remote_audio_track_subscribers: Default::default(),
188                remote_video_track_subscribers: Default::default(),
189                _delegate: Mutex::new(delegate),
190            }
191        })
192    }
193
194    pub fn status(&self) -> watch::Receiver<ConnectionState> {
195        self.connection.lock().1.clone()
196    }
197
198    pub fn connect(self: &Arc<Self>, url: &str, token: &str) -> impl Future<Output = Result<()>> {
199        let url = CFString::new(url);
200        let token = CFString::new(token);
201        let (did_connect, tx, rx) = Self::build_done_callback();
202        unsafe {
203            LKRoomConnect(
204                *self.native_room.lock(),
205                url.as_concrete_TypeRef(),
206                token.as_concrete_TypeRef(),
207                did_connect,
208                tx,
209            )
210        }
211
212        let this = self.clone();
213        let url = url.to_string();
214        let token = token.to_string();
215        async move {
216            rx.await.unwrap().context("error connecting to room")?;
217            *this.connection.lock().0.borrow_mut() = ConnectionState::Connected { url, token };
218            Ok(())
219        }
220    }
221
222    fn did_disconnect(&self) {
223        *self.connection.lock().0.borrow_mut() = ConnectionState::Disconnected;
224    }
225
226    pub fn display_sources(self: &Arc<Self>) -> impl Future<Output = Result<Vec<MacOSDisplay>>> {
227        extern "C" fn callback(tx: *mut c_void, sources: CFArrayRef, error: CFStringRef) {
228            unsafe {
229                let tx = Box::from_raw(tx as *mut oneshot::Sender<Result<Vec<MacOSDisplay>>>);
230
231                if sources.is_null() {
232                    let _ = tx.send(Err(anyhow!("{}", CFString::wrap_under_get_rule(error))));
233                } else {
234                    let sources = CFArray::wrap_under_get_rule(sources)
235                        .into_iter()
236                        .map(|source| MacOSDisplay::new(swift::MacOSDisplay(*source)))
237                        .collect();
238
239                    let _ = tx.send(Ok(sources));
240                }
241            }
242        }
243
244        let (tx, rx) = oneshot::channel();
245
246        unsafe {
247            LKDisplaySources(Box::into_raw(Box::new(tx)) as *mut _, callback);
248        }
249
250        async move { rx.await.unwrap() }
251    }
252
253    pub fn publish_video_track(
254        self: &Arc<Self>,
255        track: LocalVideoTrack,
256    ) -> impl Future<Output = Result<LocalTrackPublication>> {
257        let (tx, rx) = oneshot::channel::<Result<LocalTrackPublication>>();
258        extern "C" fn callback(
259            tx: *mut c_void,
260            publication: swift::LocalTrackPublication,
261            error: CFStringRef,
262        ) {
263            let tx =
264                unsafe { Box::from_raw(tx as *mut oneshot::Sender<Result<LocalTrackPublication>>) };
265            if error.is_null() {
266                let _ = tx.send(Ok(LocalTrackPublication::new(publication)));
267            } else {
268                let error = unsafe { CFString::wrap_under_get_rule(error).to_string() };
269                let _ = tx.send(Err(anyhow!(error)));
270            }
271        }
272        unsafe {
273            LKRoomPublishVideoTrack(
274                *self.native_room.lock(),
275                track.0,
276                callback,
277                Box::into_raw(Box::new(tx)) as *mut c_void,
278            );
279        }
280        async { rx.await.unwrap().context("error publishing video track") }
281    }
282
283    pub fn publish_audio_track(
284        self: &Arc<Self>,
285        track: LocalAudioTrack,
286    ) -> impl Future<Output = Result<LocalTrackPublication>> {
287        let (tx, rx) = oneshot::channel::<Result<LocalTrackPublication>>();
288        extern "C" fn callback(
289            tx: *mut c_void,
290            publication: swift::LocalTrackPublication,
291            error: CFStringRef,
292        ) {
293            let tx =
294                unsafe { Box::from_raw(tx as *mut oneshot::Sender<Result<LocalTrackPublication>>) };
295            if error.is_null() {
296                let _ = tx.send(Ok(LocalTrackPublication::new(publication)));
297            } else {
298                let error = unsafe { CFString::wrap_under_get_rule(error).to_string() };
299                let _ = tx.send(Err(anyhow!(error)));
300            }
301        }
302        unsafe {
303            LKRoomPublishAudioTrack(
304                *self.native_room.lock(),
305                track.0,
306                callback,
307                Box::into_raw(Box::new(tx)) as *mut c_void,
308            );
309        }
310        async { rx.await.unwrap().context("error publishing audio track") }
311    }
312
313    pub fn unpublish_track(&self, publication: LocalTrackPublication) {
314        unsafe {
315            LKRoomUnpublishTrack(*self.native_room.lock(), publication.0);
316        }
317    }
318
319    pub fn remote_video_tracks(&self, participant_id: &str) -> Vec<Arc<RemoteVideoTrack>> {
320        unsafe {
321            let tracks = LKRoomVideoTracksForRemoteParticipant(
322                *self.native_room.lock(),
323                CFString::new(participant_id).as_concrete_TypeRef(),
324            );
325
326            if tracks.is_null() {
327                Vec::new()
328            } else {
329                let tracks = CFArray::wrap_under_get_rule(tracks);
330                tracks
331                    .into_iter()
332                    .map(|native_track| {
333                        let native_track = swift::RemoteVideoTrack(*native_track);
334                        let id =
335                            CFString::wrap_under_get_rule(LKRemoteVideoTrackGetSid(native_track))
336                                .to_string();
337                        Arc::new(RemoteVideoTrack::new(
338                            native_track,
339                            id,
340                            participant_id.into(),
341                        ))
342                    })
343                    .collect()
344            }
345        }
346    }
347
348    pub fn remote_audio_tracks(&self, participant_id: &str) -> Vec<Arc<RemoteAudioTrack>> {
349        unsafe {
350            let tracks = LKRoomAudioTracksForRemoteParticipant(
351                *self.native_room.lock(),
352                CFString::new(participant_id).as_concrete_TypeRef(),
353            );
354
355            if tracks.is_null() {
356                Vec::new()
357            } else {
358                let tracks = CFArray::wrap_under_get_rule(tracks);
359                tracks
360                    .into_iter()
361                    .map(|native_track| {
362                        let native_track = swift::RemoteAudioTrack(*native_track);
363                        let id =
364                            CFString::wrap_under_get_rule(LKRemoteAudioTrackGetSid(native_track))
365                                .to_string();
366                        Arc::new(RemoteAudioTrack::new(
367                            native_track,
368                            id,
369                            participant_id.into(),
370                        ))
371                    })
372                    .collect()
373            }
374        }
375    }
376
377    pub fn remote_audio_track_publications(
378        &self,
379        participant_id: &str,
380    ) -> Vec<Arc<RemoteTrackPublication>> {
381        unsafe {
382            let tracks = LKRoomAudioTrackPublicationsForRemoteParticipant(
383                *self.native_room.lock(),
384                CFString::new(participant_id).as_concrete_TypeRef(),
385            );
386
387            if tracks.is_null() {
388                Vec::new()
389            } else {
390                let tracks = CFArray::wrap_under_get_rule(tracks);
391                tracks
392                    .into_iter()
393                    .map(|native_track_publication| {
394                        let native_track_publication =
395                            swift::RemoteTrackPublication(*native_track_publication);
396                        Arc::new(RemoteTrackPublication::new(native_track_publication))
397                    })
398                    .collect()
399            }
400        }
401    }
402
403    pub fn remote_audio_track_updates(&self) -> mpsc::UnboundedReceiver<RemoteAudioTrackUpdate> {
404        let (tx, rx) = mpsc::unbounded();
405        self.remote_audio_track_subscribers.lock().push(tx);
406        rx
407    }
408
409    pub fn remote_video_track_updates(&self) -> mpsc::UnboundedReceiver<RemoteVideoTrackUpdate> {
410        let (tx, rx) = mpsc::unbounded();
411        self.remote_video_track_subscribers.lock().push(tx);
412        rx
413    }
414
415    fn did_subscribe_to_remote_audio_track(
416        &self,
417        track: RemoteAudioTrack,
418        publication: RemoteTrackPublication,
419    ) {
420        let track = Arc::new(track);
421        let publication = Arc::new(publication);
422        self.remote_audio_track_subscribers.lock().retain(|tx| {
423            tx.unbounded_send(RemoteAudioTrackUpdate::Subscribed(
424                track.clone(),
425                publication.clone(),
426            ))
427            .is_ok()
428        });
429    }
430
431    fn did_unsubscribe_from_remote_audio_track(&self, publisher_id: String, track_id: String) {
432        self.remote_audio_track_subscribers.lock().retain(|tx| {
433            tx.unbounded_send(RemoteAudioTrackUpdate::Unsubscribed {
434                publisher_id: publisher_id.clone(),
435                track_id: track_id.clone(),
436            })
437            .is_ok()
438        });
439    }
440
441    fn mute_changed_from_remote_audio_track(&self, track_id: String, muted: bool) {
442        self.remote_audio_track_subscribers.lock().retain(|tx| {
443            tx.unbounded_send(RemoteAudioTrackUpdate::MuteChanged {
444                track_id: track_id.clone(),
445                muted,
446            })
447            .is_ok()
448        });
449    }
450
451    // A vec of publisher IDs
452    fn active_speakers_changed(&self, speakers: Vec<String>) {
453        self.remote_audio_track_subscribers
454            .lock()
455            .retain(move |tx| {
456                tx.unbounded_send(RemoteAudioTrackUpdate::ActiveSpeakersChanged {
457                    speakers: speakers.clone(),
458                })
459                .is_ok()
460            });
461    }
462
463    fn did_subscribe_to_remote_video_track(&self, track: RemoteVideoTrack) {
464        let track = Arc::new(track);
465        self.remote_video_track_subscribers.lock().retain(|tx| {
466            tx.unbounded_send(RemoteVideoTrackUpdate::Subscribed(track.clone()))
467                .is_ok()
468        });
469    }
470
471    fn did_unsubscribe_from_remote_video_track(&self, publisher_id: String, track_id: String) {
472        self.remote_video_track_subscribers.lock().retain(|tx| {
473            tx.unbounded_send(RemoteVideoTrackUpdate::Unsubscribed {
474                publisher_id: publisher_id.clone(),
475                track_id: track_id.clone(),
476            })
477            .is_ok()
478        });
479    }
480
481    fn build_done_callback() -> (
482        extern "C" fn(*mut c_void, CFStringRef),
483        *mut c_void,
484        oneshot::Receiver<Result<()>>,
485    ) {
486        let (tx, rx) = oneshot::channel();
487        extern "C" fn done_callback(tx: *mut c_void, error: CFStringRef) {
488            let tx = unsafe { Box::from_raw(tx as *mut oneshot::Sender<Result<()>>) };
489            if error.is_null() {
490                let _ = tx.send(Ok(()));
491            } else {
492                let error = unsafe { CFString::wrap_under_get_rule(error).to_string() };
493                let _ = tx.send(Err(anyhow!(error)));
494            }
495        }
496        (
497            done_callback,
498            Box::into_raw(Box::new(tx)) as *mut c_void,
499            rx,
500        )
501    }
502}
503
504impl Drop for Room {
505    fn drop(&mut self) {
506        unsafe {
507            let native_room = &*self.native_room.lock();
508            LKRoomDisconnect(*native_room);
509            CFRelease(native_room.0);
510        }
511    }
512}
513
514struct RoomDelegate {
515    native_delegate: swift::RoomDelegate,
516    _weak_room: Weak<Room>,
517}
518
519impl RoomDelegate {
520    fn new(weak_room: Weak<Room>) -> Self {
521        let native_delegate = unsafe {
522            LKRoomDelegateCreate(
523                weak_room.as_ptr() as *mut c_void,
524                Self::on_did_disconnect,
525                Self::on_did_subscribe_to_remote_audio_track,
526                Self::on_did_unsubscribe_from_remote_audio_track,
527                Self::on_mute_change_from_remote_audio_track,
528                Self::on_active_speakers_changed,
529                Self::on_did_subscribe_to_remote_video_track,
530                Self::on_did_unsubscribe_from_remote_video_track,
531            )
532        };
533        Self {
534            native_delegate,
535            _weak_room: weak_room,
536        }
537    }
538
539    extern "C" fn on_did_disconnect(room: *mut c_void) {
540        let room = unsafe { Weak::from_raw(room as *mut Room) };
541        if let Some(room) = room.upgrade() {
542            room.did_disconnect();
543        }
544        let _ = Weak::into_raw(room);
545    }
546
547    extern "C" fn on_did_subscribe_to_remote_audio_track(
548        room: *mut c_void,
549        publisher_id: CFStringRef,
550        track_id: CFStringRef,
551        track: swift::RemoteAudioTrack,
552        publication: swift::RemoteTrackPublication,
553    ) {
554        let room = unsafe { Weak::from_raw(room as *mut Room) };
555        let publisher_id = unsafe { CFString::wrap_under_get_rule(publisher_id).to_string() };
556        let track_id = unsafe { CFString::wrap_under_get_rule(track_id).to_string() };
557        let track = RemoteAudioTrack::new(track, track_id, publisher_id);
558        let publication = RemoteTrackPublication::new(publication);
559        if let Some(room) = room.upgrade() {
560            room.did_subscribe_to_remote_audio_track(track, publication);
561        }
562        let _ = Weak::into_raw(room);
563    }
564
565    extern "C" fn on_did_unsubscribe_from_remote_audio_track(
566        room: *mut c_void,
567        publisher_id: CFStringRef,
568        track_id: CFStringRef,
569    ) {
570        let room = unsafe { Weak::from_raw(room as *mut Room) };
571        let publisher_id = unsafe { CFString::wrap_under_get_rule(publisher_id).to_string() };
572        let track_id = unsafe { CFString::wrap_under_get_rule(track_id).to_string() };
573        if let Some(room) = room.upgrade() {
574            room.did_unsubscribe_from_remote_audio_track(publisher_id, track_id);
575        }
576        let _ = Weak::into_raw(room);
577    }
578
579    extern "C" fn on_mute_change_from_remote_audio_track(
580        room: *mut c_void,
581        track_id: CFStringRef,
582        muted: bool,
583    ) {
584        let room = unsafe { Weak::from_raw(room as *mut Room) };
585        let track_id = unsafe { CFString::wrap_under_get_rule(track_id).to_string() };
586        if let Some(room) = room.upgrade() {
587            room.mute_changed_from_remote_audio_track(track_id, muted);
588        }
589        let _ = Weak::into_raw(room);
590    }
591
592    extern "C" fn on_active_speakers_changed(room: *mut c_void, participants: CFArrayRef) {
593        if participants.is_null() {
594            return;
595        }
596
597        let room = unsafe { Weak::from_raw(room as *mut Room) };
598        let speakers = unsafe {
599            CFArray::wrap_under_get_rule(participants)
600                .into_iter()
601                .map(
602                    |speaker: core_foundation::base::ItemRef<'_, *const c_void>| {
603                        CFString::wrap_under_get_rule(*speaker as CFStringRef).to_string()
604                    },
605                )
606                .collect()
607        };
608
609        if let Some(room) = room.upgrade() {
610            room.active_speakers_changed(speakers);
611        }
612        let _ = Weak::into_raw(room);
613    }
614
615    extern "C" fn on_did_subscribe_to_remote_video_track(
616        room: *mut c_void,
617        publisher_id: CFStringRef,
618        track_id: CFStringRef,
619        track: swift::RemoteVideoTrack,
620    ) {
621        let room = unsafe { Weak::from_raw(room as *mut Room) };
622        let publisher_id = unsafe { CFString::wrap_under_get_rule(publisher_id).to_string() };
623        let track_id = unsafe { CFString::wrap_under_get_rule(track_id).to_string() };
624        let track = RemoteVideoTrack::new(track, track_id, publisher_id);
625        if let Some(room) = room.upgrade() {
626            room.did_subscribe_to_remote_video_track(track);
627        }
628        let _ = Weak::into_raw(room);
629    }
630
631    extern "C" fn on_did_unsubscribe_from_remote_video_track(
632        room: *mut c_void,
633        publisher_id: CFStringRef,
634        track_id: CFStringRef,
635    ) {
636        let room = unsafe { Weak::from_raw(room as *mut Room) };
637        let publisher_id = unsafe { CFString::wrap_under_get_rule(publisher_id).to_string() };
638        let track_id = unsafe { CFString::wrap_under_get_rule(track_id).to_string() };
639        if let Some(room) = room.upgrade() {
640            room.did_unsubscribe_from_remote_video_track(publisher_id, track_id);
641        }
642        let _ = Weak::into_raw(room);
643    }
644}
645
646impl Drop for RoomDelegate {
647    fn drop(&mut self) {
648        unsafe {
649            CFRelease(self.native_delegate.0);
650        }
651    }
652}
653
654pub struct LocalAudioTrack(swift::LocalAudioTrack);
655
656impl LocalAudioTrack {
657    pub fn create() -> Self {
658        Self(unsafe { LKLocalAudioTrackCreateTrack() })
659    }
660}
661
662impl Drop for LocalAudioTrack {
663    fn drop(&mut self) {
664        unsafe { CFRelease(self.0 .0) }
665    }
666}
667
668pub struct LocalVideoTrack(swift::LocalVideoTrack);
669
670impl LocalVideoTrack {
671    pub fn screen_share_for_display(display: &MacOSDisplay) -> Self {
672        Self(unsafe { LKCreateScreenShareTrackForDisplay(display.0) })
673    }
674}
675
676impl Drop for LocalVideoTrack {
677    fn drop(&mut self) {
678        unsafe { CFRelease(self.0 .0) }
679    }
680}
681
682pub struct LocalTrackPublication(swift::LocalTrackPublication);
683
684impl LocalTrackPublication {
685    pub fn new(native_track_publication: swift::LocalTrackPublication) -> Self {
686        unsafe {
687            CFRetain(native_track_publication.0);
688        }
689        Self(native_track_publication)
690    }
691
692    pub fn set_mute(&self, muted: bool) -> impl Future<Output = Result<()>> {
693        let (tx, rx) = futures::channel::oneshot::channel();
694
695        extern "C" fn complete_callback(callback_data: *mut c_void, error: CFStringRef) {
696            let tx = unsafe { Box::from_raw(callback_data as *mut oneshot::Sender<Result<()>>) };
697            if error.is_null() {
698                tx.send(Ok(())).ok();
699            } else {
700                let error = unsafe { CFString::wrap_under_get_rule(error).to_string() };
701                tx.send(Err(anyhow!(error))).ok();
702            }
703        }
704
705        unsafe {
706            LKLocalTrackPublicationSetMute(
707                self.0,
708                muted,
709                complete_callback,
710                Box::into_raw(Box::new(tx)) as *mut c_void,
711            )
712        }
713
714        async move { rx.await.unwrap() }
715    }
716}
717
718impl Drop for LocalTrackPublication {
719    fn drop(&mut self) {
720        unsafe { CFRelease(self.0 .0) }
721    }
722}
723
724pub struct RemoteTrackPublication {
725    native_publication: Mutex<swift::RemoteTrackPublication>,
726}
727
728impl RemoteTrackPublication {
729    pub fn new(native_track_publication: swift::RemoteTrackPublication) -> Self {
730        unsafe {
731            CFRetain(native_track_publication.0);
732        }
733        Self {
734            native_publication: Mutex::new(native_track_publication),
735        }
736    }
737
738    pub fn sid(&self) -> String {
739        unsafe {
740            CFString::wrap_under_get_rule(LKRemoteTrackPublicationGetSid(
741                *self.native_publication.lock(),
742            ))
743            .to_string()
744        }
745    }
746
747    pub fn is_muted(&self) -> bool {
748        unsafe { LKRemoteTrackPublicationIsMuted(*self.native_publication.lock()) }
749    }
750
751    pub fn set_enabled(&self, enabled: bool) -> impl Future<Output = Result<()>> {
752        let (tx, rx) = futures::channel::oneshot::channel();
753
754        extern "C" fn complete_callback(callback_data: *mut c_void, error: CFStringRef) {
755            let tx = unsafe { Box::from_raw(callback_data as *mut oneshot::Sender<Result<()>>) };
756            if error.is_null() {
757                tx.send(Ok(())).ok();
758            } else {
759                let error = unsafe { CFString::wrap_under_get_rule(error).to_string() };
760                tx.send(Err(anyhow!(error))).ok();
761            }
762        }
763
764        unsafe {
765            LKRemoteTrackPublicationSetEnabled(
766                *self.native_publication.lock(),
767                enabled,
768                complete_callback,
769                Box::into_raw(Box::new(tx)) as *mut c_void,
770            )
771        }
772
773        async move { rx.await.unwrap() }
774    }
775}
776
777impl Drop for RemoteTrackPublication {
778    fn drop(&mut self) {
779        unsafe { CFRelease((*self.native_publication.lock()).0) }
780    }
781}
782
783#[derive(Debug)]
784pub struct RemoteAudioTrack {
785    native_track: Mutex<swift::RemoteAudioTrack>,
786    sid: Sid,
787    publisher_id: String,
788}
789
790impl RemoteAudioTrack {
791    fn new(native_track: swift::RemoteAudioTrack, sid: Sid, publisher_id: String) -> Self {
792        unsafe {
793            CFRetain(native_track.0);
794        }
795        Self {
796            native_track: Mutex::new(native_track),
797            sid,
798            publisher_id,
799        }
800    }
801
802    pub fn sid(&self) -> &str {
803        &self.sid
804    }
805
806    pub fn publisher_id(&self) -> &str {
807        &self.publisher_id
808    }
809
810    pub fn enable(&self) -> impl Future<Output = Result<()>> {
811        async { Ok(()) }
812    }
813
814    pub fn disable(&self) -> impl Future<Output = Result<()>> {
815        async { Ok(()) }
816    }
817}
818
819impl Drop for RemoteAudioTrack {
820    fn drop(&mut self) {
821        unsafe { CFRelease(self.native_track.lock().0) }
822    }
823}
824
825#[derive(Debug)]
826pub struct RemoteVideoTrack {
827    native_track: Mutex<swift::RemoteVideoTrack>,
828    sid: Sid,
829    publisher_id: String,
830}
831
832impl RemoteVideoTrack {
833    fn new(native_track: swift::RemoteVideoTrack, sid: Sid, publisher_id: String) -> Self {
834        unsafe {
835            CFRetain(native_track.0);
836        }
837        Self {
838            native_track: Mutex::new(native_track),
839            sid,
840            publisher_id,
841        }
842    }
843
844    pub fn sid(&self) -> &str {
845        &self.sid
846    }
847
848    pub fn publisher_id(&self) -> &str {
849        &self.publisher_id
850    }
851
852    pub fn frames(&self) -> async_broadcast::Receiver<Frame> {
853        extern "C" fn on_frame(callback_data: *mut c_void, frame: CVImageBufferRef) -> bool {
854            unsafe {
855                let tx = Box::from_raw(callback_data as *mut async_broadcast::Sender<Frame>);
856                let buffer = CVImageBuffer::wrap_under_get_rule(frame);
857                let result = tx.try_broadcast(Frame(buffer));
858                let _ = Box::into_raw(tx);
859                match result {
860                    Ok(_) => true,
861                    Err(async_broadcast::TrySendError::Closed(_))
862                    | Err(async_broadcast::TrySendError::Inactive(_)) => {
863                        log::warn!("no active receiver for frame");
864                        false
865                    }
866                    Err(async_broadcast::TrySendError::Full(_)) => {
867                        log::warn!("skipping frame as receiver is not keeping up");
868                        true
869                    }
870                }
871            }
872        }
873
874        extern "C" fn on_drop(callback_data: *mut c_void) {
875            unsafe {
876                let _ = Box::from_raw(callback_data as *mut async_broadcast::Sender<Frame>);
877            }
878        }
879
880        let (tx, rx) = async_broadcast::broadcast(64);
881        unsafe {
882            let renderer = LKVideoRendererCreate(
883                Box::into_raw(Box::new(tx)) as *mut c_void,
884                on_frame,
885                on_drop,
886            );
887            LKVideoTrackAddRenderer(*self.native_track.lock(), renderer);
888            rx
889        }
890    }
891}
892
893impl Drop for RemoteVideoTrack {
894    fn drop(&mut self) {
895        unsafe { CFRelease(self.native_track.lock().0) }
896    }
897}
898
899pub enum RemoteVideoTrackUpdate {
900    Subscribed(Arc<RemoteVideoTrack>),
901    Unsubscribed { publisher_id: Sid, track_id: Sid },
902}
903
904pub enum RemoteAudioTrackUpdate {
905    ActiveSpeakersChanged { speakers: Vec<Sid> },
906    MuteChanged { track_id: Sid, muted: bool },
907    Subscribed(Arc<RemoteAudioTrack>, Arc<RemoteTrackPublication>),
908    Unsubscribed { publisher_id: Sid, track_id: Sid },
909}
910
911pub struct MacOSDisplay(swift::MacOSDisplay);
912
913impl MacOSDisplay {
914    fn new(ptr: swift::MacOSDisplay) -> Self {
915        unsafe {
916            CFRetain(ptr.0);
917        }
918        Self(ptr)
919    }
920}
921
922impl Drop for MacOSDisplay {
923    fn drop(&mut self) {
924        unsafe { CFRelease(self.0 .0) }
925    }
926}
927
928#[derive(Clone)]
929pub struct Frame(CVImageBuffer);
930
931impl Frame {
932    pub fn width(&self) -> usize {
933        self.0.width()
934    }
935
936    pub fn height(&self) -> usize {
937        self.0.height()
938    }
939
940    pub fn image(&self) -> CVImageBuffer {
941        self.0.clone()
942    }
943}