| 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(), 8); |
| 54 | assert_eq!(kinds[7], ("x-data", "7")); |
| 55 | assert_eq!(&kinds[..7].iter().map(|(_, mid)| *mid).collect::<Vec<_>>(), &["0", "1", "2", "3", "4", "5", "6"]); |
| 56 | assert_eq!(parsed.media[7].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")); |
| 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", "applicationsharing-video", "sendrecv"), |
| 86 | ]; |
| 87 | let actual: Vec<(&str, &str, &str)> = summary.iter().map(|(a, b, c)| (a.as_str(), b.as_str(), c.as_str())).collect(); |
| 88 | assert_eq!(actual, expected); |
| 89 | for receive in &parsed.media[2..6] { |
| 90 | assert_eq!(receive.value("x-ssrc-range"), Some("1-1")); |
| 91 | assert_eq!(receive.value("x-signaling-fb"), Some("* x-message app send:src recv:src,vc")); |
| 92 | assert!(receive.values("rtpmap").all(|mapping| mapping.contains("H264") || mapping.contains("rtx")), "{:?}", receive.values("rtpmap").collect::<Vec<_>>()); |
| 93 | } |
| 94 | assert!(parsed.media[1].value("x-ssrc-range").is_some()); |
| 95 | let group = parsed.session.attributes.iter().find(|attribute| attribute.name == "group").and_then(|attribute| attribute.value.clone()); |
| 96 | assert_eq!(group.as_deref(), Some("BUNDLE 0 1 2 3 4 5 6")); |
| 97 | assert_eq!(offer.lines.iter().filter(|line| line.role == LineRole::ScreenShare).count(), 1); |
| 98 | } |
| 99 | |
| 100 | #[tokio::test] |
| 101 | async fn the_slots_come_from_the_source_stream_ids_of_an_answer() { |
| 102 | let browser = browser_offer().await; |
| 103 | let offer = to_teams_offer(&browser, &offer_plan(), &mut counter()).unwrap(); |
| 104 | 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"); |
| 105 | for (index, role) in ["main-audio", "main-video", "main-video", "main-video", "main-video", "main-video", "applicationsharing-video"].iter().enumerate() { |
| 106 | let kind = if index == 0 { "audio" } else { "video" }; |
| 107 | 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)); |
| 108 | } |
| 109 | let streams = stream_lines(&answer, &offer.lines).unwrap(); |
| 110 | let table = SlotTable::from_streams(&streams); |
| 111 | let slots: Vec<(String, u32)> = table.people.iter().map(|slot| (slot.mid.clone(), slot.stream_id)).collect(); |
| 112 | assert_eq!(slots, vec![("2".to_owned(), 203), ("3".to_owned(), 204), ("4".to_owned(), 205), ("5".to_owned(), 206)]); |
| 113 | assert_eq!(table.screen.map(|slot| (slot.mid, slot.stream_id)), Some(("6".to_owned(), 207))); |
| 114 | } |