| 1 | use std::sync::Arc; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use calling::video_frame::{VideoHub, VideoKey}; |
| 5 | use calling::video_layout::VideoRouter; |
| 6 | use calling::video_pattern::{PatternKind, spawn_pattern}; |
| 7 | use calling::video_send::LocalSink; |
| 8 | use calling::video_receive::{VideoCounters, spawn_video_pump}; |
| 9 | use calling::{CallUpdate, devices::shared_factory}; |
| 10 | use libwebrtc::video_source::native::NativeVideoSource; |
| 11 | use libwebrtc::peer_connection_factory::native::PeerConnectionFactoryExt; |
| 12 | use libwebrtc::prelude::*; |
| 13 | use libwebrtc::rtp_transceiver::{RtpTransceiverDirection, RtpTransceiverInit}; |
| 14 | use libwebrtc::video_source::VideoResolution; |
| 15 | use tokio::sync::mpsc; |
| 16 | use tokio::time::{sleep, timeout}; |
| 17 | |
| 18 | fn h264_only(capabilities: Vec<RtpCodecCapability>) -> Vec<RtpCodecCapability> { |
| 19 | capabilities |
| 20 | .into_iter() |
| 21 | .filter(|codec| matches!(codec.mime_type.to_ascii_lowercase().as_str(), "video/h264" | "video/rtx")) |
| 22 | .collect() |
| 23 | } |
| 24 | |
| 25 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 26 | async fn a_generated_h264_stream_survives_the_pipeline_to_bgra_pictures() { |
| 27 | let factory = shared_factory(); |
| 28 | let sender = factory.create_peer_connection(RtcConfiguration::default()).unwrap(); |
| 29 | let receiver = factory.create_peer_connection(RtcConfiguration::default()).unwrap(); |
| 30 | |
| 31 | let (to_receiver, mut for_receiver) = mpsc::unbounded_channel::<IceCandidate>(); |
| 32 | let (to_sender, mut for_sender) = mpsc::unbounded_channel::<IceCandidate>(); |
| 33 | sender.on_ice_candidate(Some(Box::new(move |candidate| { |
| 34 | let _ = to_receiver.send(candidate); |
| 35 | }))); |
| 36 | receiver.on_ice_candidate(Some(Box::new(move |candidate| { |
| 37 | let _ = to_sender.send(candidate); |
| 38 | }))); |
| 39 | let (track_sender, mut tracks) = mpsc::unbounded_channel::<RtcVideoTrack>(); |
| 40 | receiver.on_track(Some(Box::new(move |event| { |
| 41 | if let MediaStreamTrack::Video(track) = event.track { |
| 42 | let _ = track_sender.send(track); |
| 43 | } |
| 44 | }))); |
| 45 | |
| 46 | let (width, height) = PatternKind::Camera.size(); |
| 47 | let source = NativeVideoSource::new(VideoResolution { width, height }, false); |
| 48 | let track = factory.create_video_track("camera", source.clone()); |
| 49 | let transceiver = sender |
| 50 | .add_transceiver( |
| 51 | MediaStreamTrack::Video(track), |
| 52 | RtpTransceiverInit { |
| 53 | direction: RtpTransceiverDirection::SendOnly, |
| 54 | stream_ids: vec!["camera".into()], |
| 55 | send_encodings: Vec::new(), |
| 56 | }, |
| 57 | ) |
| 58 | .unwrap(); |
| 59 | transceiver |
| 60 | .set_codec_preferences(h264_only(factory.get_rtp_sender_capabilities(MediaType::Video).codecs)) |
| 61 | .unwrap(); |
| 62 | let pattern = spawn_pattern(LocalSink::new(source, None), PatternKind::Camera, None); |
| 63 | |
| 64 | let offer = sender.create_offer(OfferOptions::default()).await.unwrap(); |
| 65 | sender.set_local_description(offer.clone()).await.unwrap(); |
| 66 | receiver.set_remote_description(offer).await.unwrap(); |
| 67 | let answer = receiver.create_answer(AnswerOptions::default()).await.unwrap(); |
| 68 | receiver.set_local_description(answer.clone()).await.unwrap(); |
| 69 | sender.set_remote_description(answer).await.unwrap(); |
| 70 | |
| 71 | let relay = tokio::spawn(async move { |
| 72 | loop { |
| 73 | tokio::select! { |
| 74 | Some(candidate) = for_receiver.recv() => { let _ = receiver.add_ice_candidate(candidate).await; } |
| 75 | Some(candidate) = for_sender.recv() => { let _ = sender.add_ice_candidate(candidate).await; } |
| 76 | else => break, |
| 77 | } |
| 78 | } |
| 79 | (sender, receiver) |
| 80 | }); |
| 81 | |
| 82 | let remote_track = timeout(Duration::from_secs(15), tracks.recv()).await.expect("video track").expect("track"); |
| 83 | let (updates, mut ready) = mpsc::unbounded_channel(); |
| 84 | let hub = VideoHub::new(updates); |
| 85 | let router = VideoRouter::default(); |
| 86 | router.set("0", Some(VideoKey::Person("8:orgid:loopback".into()))); |
| 87 | let counters = Arc::new(VideoCounters::default()); |
| 88 | let pump = spawn_video_pump(remote_track, "0".into(), router, hub.clone(), counters.clone()); |
| 89 | |
| 90 | let mut pictures = 0; |
| 91 | let mut last = None; |
| 92 | let collect = async { |
| 93 | while pictures < 20 { |
| 94 | let Some(CallUpdate::VideoReady) = ready.recv().await else { break }; |
| 95 | for (key, picture) in hub.drain() { |
| 96 | assert_eq!(key, VideoKey::Person("8:orgid:loopback".into())); |
| 97 | pictures += 1; |
| 98 | last = Some(picture); |
| 99 | } |
| 100 | } |
| 101 | }; |
| 102 | timeout(Duration::from_secs(20), collect).await.expect("pictures within 20 s"); |
| 103 | sleep(Duration::from_millis(200)).await; |
| 104 | let picture = last.unwrap(); |
| 105 | assert_eq!((picture.width, picture.height), (width, height)); |
| 106 | assert_eq!(picture.bgra.len(), (width * height * 4) as usize); |
| 107 | let bright = picture.bgra.as_chunks::<4>().0.iter().filter(|pixel| pixel[0] > 150 && pixel[1] > 150 && pixel[2] > 150).count(); |
| 108 | assert!(bright > 1000, "the moving square is visible ({bright} bright pixels)"); |
| 109 | assert!(counters.received.load(std::sync::atomic::Ordering::Relaxed) >= 20); |
| 110 | pump.abort(); |
| 111 | pattern.abort(); |
| 112 | relay.abort(); |
| 113 | } |