test_app.rs

  1use std::time::Duration;
  2
  3use futures::StreamExt;
  4use gpui::{actions, keymap_matcher::Binding, Menu, MenuItem};
  5use live_kit_client::{
  6    LocalAudioTrack, LocalVideoTrack, RemoteAudioTrackUpdate, RemoteVideoTrackUpdate, Room,
  7};
  8use live_kit_server::token::{self, VideoGrant};
  9use log::LevelFilter;
 10use simplelog::SimpleLogger;
 11
 12actions!(capture, [Quit]);
 13
 14fn main() {
 15    SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
 16
 17    gpui::App::new(()).unwrap().run(|cx| {
 18        #[cfg(any(test, feature = "test-support"))]
 19        println!("USING TEST LIVEKIT");
 20
 21        #[cfg(not(any(test, feature = "test-support")))]
 22        println!("USING REAL LIVEKIT");
 23
 24        cx.platform().activate(true);
 25        cx.add_global_action(quit);
 26
 27        cx.add_bindings([Binding::new("cmd-q", Quit, None)]);
 28        cx.set_menus(vec![Menu {
 29            name: "Zed",
 30            items: vec![MenuItem::Action {
 31                name: "Quit",
 32                action: Box::new(Quit),
 33                os_action: None,
 34            }],
 35        }]);
 36
 37        let live_kit_url = std::env::var("LIVE_KIT_URL").unwrap_or("http://localhost:7880".into());
 38        let live_kit_key = std::env::var("LIVE_KIT_KEY").unwrap_or("devkey".into());
 39        let live_kit_secret = std::env::var("LIVE_KIT_SECRET").unwrap_or("secret".into());
 40
 41        cx.spawn(|cx| async move {
 42            let user_a_token = token::create(
 43                &live_kit_key,
 44                &live_kit_secret,
 45                Some("test-participant-1"),
 46                VideoGrant::to_join("test-room"),
 47            )
 48            .unwrap();
 49            let room_a = Room::new();
 50            room_a.connect(&live_kit_url, &user_a_token).await.unwrap();
 51
 52            let user2_token = token::create(
 53                &live_kit_key,
 54                &live_kit_secret,
 55                Some("test-participant-2"),
 56                VideoGrant::to_join("test-room"),
 57            )
 58            .unwrap();
 59            let room_b = Room::new();
 60            room_b.connect(&live_kit_url, &user2_token).await.unwrap();
 61
 62            let mut audio_track_updates = room_b.remote_audio_track_updates();
 63            let audio_track = LocalAudioTrack::create();
 64            let audio_track_publication = room_a.publish_audio_track(&audio_track).await.unwrap();
 65
 66            if let RemoteAudioTrackUpdate::Subscribed(track) =
 67                audio_track_updates.next().await.unwrap()
 68            {
 69                let remote_tracks = room_b.remote_audio_tracks("test-participant-1");
 70                assert_eq!(remote_tracks.len(), 1);
 71                assert_eq!(remote_tracks[0].publisher_id(), "test-participant-1");
 72                assert_eq!(track.publisher_id(), "test-participant-1");
 73            } else {
 74                panic!("unexpected message");
 75            }
 76
 77            println!("Pausing for 5 seconds to test audio, make some noise!");
 78            let timer = cx.background().timer(Duration::from_secs(5));
 79            timer.await;
 80
 81            let remote_audio_track = room_b
 82                .remote_audio_tracks("test-participant-1")
 83                .pop()
 84                .unwrap();
 85            room_a.unpublish_track(audio_track_publication);
 86            if let RemoteAudioTrackUpdate::Unsubscribed {
 87                publisher_id,
 88                track_id,
 89            } = audio_track_updates.next().await.unwrap()
 90            {
 91                assert_eq!(publisher_id, "test-participant-1");
 92                assert_eq!(remote_audio_track.sid(), track_id);
 93                assert_eq!(room_b.remote_audio_tracks("test-participant-1").len(), 0);
 94            } else {
 95                panic!("unexpected message");
 96            }
 97
 98            let mut video_track_updates = room_b.remote_video_track_updates();
 99            let displays = room_a.display_sources().await.unwrap();
100            let display = displays.into_iter().next().unwrap();
101
102            let local_video_track = LocalVideoTrack::screen_share_for_display(&display);
103            let local_video_track_publication = room_a
104                .publish_video_track(&local_video_track)
105                .await
106                .unwrap();
107
108            if let RemoteVideoTrackUpdate::Subscribed(track) =
109                video_track_updates.next().await.unwrap()
110            {
111                let remote_video_tracks = room_b.remote_video_tracks("test-participant-1");
112                assert_eq!(remote_video_tracks.len(), 1);
113                assert_eq!(remote_video_tracks[0].publisher_id(), "test-participant-1");
114                assert_eq!(track.publisher_id(), "test-participant-1");
115            } else {
116                panic!("unexpected message");
117            }
118
119            let remote_video_track = room_b
120                .remote_video_tracks("test-participant-1")
121                .pop()
122                .unwrap();
123            room_a.unpublish_track(local_video_track_publication);
124            if let RemoteVideoTrackUpdate::Unsubscribed {
125                publisher_id,
126                track_id,
127            } = video_track_updates.next().await.unwrap()
128            {
129                assert_eq!(publisher_id, "test-participant-1");
130                assert_eq!(remote_video_track.sid(), track_id);
131                assert_eq!(room_b.remote_video_tracks("test-participant-1").len(), 0);
132            } else {
133                panic!("unexpected message");
134            }
135
136            cx.platform().quit();
137        })
138        .detach();
139    });
140}
141
142fn quit(_: &Quit, cx: &mut gpui::AppContext) {
143    cx.platform().quit();
144}