| 1 | use calling::devices::shared_factory; |
| 2 | use calling::sdp::{LineRole, parse, stream_lines, to_teams_offer}; |
| 3 | use calling::video_layout::{SlotTable, add_video_lines, offer_plan}; |
| 4 | use libwebrtc::audio_source::native::NativeAudioSource; |
| 5 | use libwebrtc::data_channel::DataChannelInit; |
| 6 | use libwebrtc::peer_connection_factory::native::PeerConnectionFactoryExt; |
| 7 | use libwebrtc::prelude::*; |
| 8 | use libwebrtc::rtp_transceiver::{RtpTransceiverDirection, RtpTransceiverInit}; |
| 9 | |
| 10 | fn counter() -> impl FnMut() -> u32 { |
| 11 | let mut next = 5_000_000u32; |
| 12 | move || { |
| 13 | next += 1; |
| 14 | next |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | async fn browser_offer() -> String { |
| 19 | offer_with(false).await |
| 20 | } |
| 21 | |
| 22 | async fn offer_with(data_channel: bool) -> String { |
| 23 | let factory = shared_factory(); |
| 24 | let peer = factory.create_peer_connection(RtcConfiguration::default()).unwrap(); |
| 25 | let _channel = data_channel.then(|| peer.create_data_channel("main-channel", DataChannelInit::default()).unwrap()); |
| 26 | let source = NativeAudioSource::new(AudioSourceOptions::default(), 48_000, 1, 100); |
| 27 | let track = factory.create_audio_track("microphone", source); |
| 28 | peer.add_transceiver( |
| 29 | MediaStreamTrack::Audio(track), |
| 30 | RtpTransceiverInit { |
| 31 | direction: RtpTransceiverDirection::SendRecv, |
| 32 | stream_ids: vec!["microphone".into()], |
| 33 | send_encodings: Vec::new(), |
| 34 | }, |
| 35 | ) |
| 36 | .unwrap(); |
| 37 | add_video_lines(&peer, &factory).unwrap(); |
| 38 | peer.create_offer(OfferOptions { |
| 39 | offer_to_receive_audio: true, |
| 40 | offer_to_receive_video: true, |
| 41 | ..OfferOptions::default() |
| 42 | }) |
| 43 | .await |
| 44 | .unwrap() |
| 45 | .to_string() |
| 46 | } |
| 47 | |
| 48 | #[tokio::test] |
| 49 | async fn the_caption_data_channel_adds_one_x_data_line_after_the_video_lines() { |
| 50 | let offer = to_teams_offer(&offer_with(true).await, &offer_plan(), &mut counter()).unwrap(); |
| 51 | let parsed = parse(&offer.sdp).unwrap(); |
| 52 | let kinds: Vec<(&str, &str)> = parsed.media.iter().map(|media| (media.kind.as_str(), media.mid().unwrap_or_default())).collect(); |
| 53 | assert_eq!(kinds.len(), 13); |
| 54 | assert_eq!(kinds[12], ("x-data", "12")); |
| 55 | assert_eq!(&kinds[..12].iter().map(|(_, mid)| *mid).collect::<Vec<_>>(), &["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]); |
| 56 | assert_eq!(parsed.media[12].value("x-data-protocol"), Some("sctp")); |
| 57 | assert_eq!(offer.lines.iter().filter(|line| line.role == LineRole::Data).count(), 1); |
| 58 | let group = parsed.session.attributes.iter().find(|attribute| attribute.name == "group").and_then(|attribute| attribute.value.clone()); |
| 59 | assert_eq!(group.as_deref(), Some("BUNDLE 0 1 2 3 4 5 6 7 8 9 10 11 12")); |
| 60 | } |
| 61 | |
| 62 | #[tokio::test] |
| 63 | async fn the_meeting_layout_is_signaled_like_the_web_client_offers_it() { |
| 64 | let browser = browser_offer().await; |
| 65 | let offer = to_teams_offer(&browser, &offer_plan(), &mut counter()).unwrap(); |
| 66 | let parsed = parse(&offer.sdp).unwrap(); |
| 67 | let summary: Vec<(String, String, String)> = parsed |
| 68 | .media |
| 69 | .iter() |
| 70 | .map(|media| { |
| 71 | ( |
| 72 | media.mid().unwrap_or_default().to_owned(), |
| 73 | media.value("label").unwrap_or_default().to_owned(), |
| 74 | media.direction().unwrap_or_default().to_owned(), |
| 75 | ) |
| 76 | }) |
| 77 | .collect(); |
| 78 | let expected: Vec<(&str, &str, &str)> = vec![ |
| 79 | ("0", "main-audio", "sendrecv"), |
| 80 | ("1", "main-video", "sendrecv"), |
| 81 | ("2", "main-video", "recvonly"), |
| 82 | ("3", "main-video", "recvonly"), |
| 83 | ("4", "main-video", "recvonly"), |
| 84 | ("5", "main-video", "recvonly"), |
| 85 | ("6", "main-video", "recvonly"), |
| 86 | ("7", "main-video", "recvonly"), |
| 87 | ("8", "main-video", "recvonly"), |
| 88 | ("9", "main-video", "recvonly"), |
| 89 | ("10", "main-video", "recvonly"), |
| 90 | ("11", "applicationsharing-video", "sendrecv"), |
| 91 | ]; |
| 92 | let actual: Vec<(&str, &str, &str)> = summary.iter().map(|(a, b, c)| (a.as_str(), b.as_str(), c.as_str())).collect(); |
| 93 | assert_eq!(actual, expected); |
| 94 | for receive in &parsed.media[2..11] { |
| 95 | assert_eq!(receive.value("x-ssrc-range"), Some("1-1")); |
| 96 | assert_eq!(receive.value("x-signaling-fb"), Some("* x-message app send:src recv:src,vc")); |
| 97 | assert!(receive.values("rtpmap").all(|mapping| mapping.contains("H264") || mapping.contains("rtx")), "{:?}", receive.values("rtpmap").collect::<Vec<_>>()); |
| 98 | } |
| 99 | assert!(parsed.media[1].value("x-ssrc-range").is_some()); |
| 100 | let group = parsed.session.attributes.iter().find(|attribute| attribute.name == "group").and_then(|attribute| attribute.value.clone()); |
| 101 | assert_eq!(group.as_deref(), Some("BUNDLE 0 1 2 3 4 5 6 7 8 9 10 11")); |
| 102 | assert_eq!(offer.lines.iter().filter(|line| line.role == LineRole::ScreenShare).count(), 1); |
| 103 | } |
| 104 | |
| 105 | #[tokio::test] |
| 106 | async fn the_slots_come_from_the_source_stream_ids_of_an_answer() { |
| 107 | let browser = browser_offer().await; |
| 108 | let offer = to_teams_offer(&browser, &offer_plan(), &mut counter()).unwrap(); |
| 109 | let mut answer = String::from("v=0\r\no=- 1 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\n"); |
| 110 | let roles: Vec<&str> = ["main-audio"].into_iter().chain(["main-video"; 10]).chain(["applicationsharing-video"]).collect(); |
| 111 | for (index, role) in roles.iter().enumerate() { |
| 112 | let kind = if index == 0 { "audio" } else { "video" }; |
| 113 | answer.push_str(&format!("m={kind} 3478 RTP/SAVP 107\r\na=x-source-streamid:{}\r\na=label:{role}\r\na=mid:{index}\r\n", 201 + index)); |
| 114 | } |
| 115 | let streams = stream_lines(&answer, &offer.lines).unwrap(); |
| 116 | let table = SlotTable::from_streams(&streams); |
| 117 | let slots: Vec<(String, u32)> = table.people.iter().map(|slot| (slot.mid.clone(), slot.stream_id)).collect(); |
| 118 | let expected: Vec<(String, u32)> = (2..=10).map(|mid| (mid.to_string(), 201 + mid)).collect(); |
| 119 | assert_eq!(slots, expected); |
| 120 | assert_eq!(table.screen.map(|slot| (slot.mid, slot.stream_id)), Some(("11".to_owned(), 212))); |
| 121 | } |