| 1 | use libwebrtc::peer_connection::PeerConnection; |
| 2 | use libwebrtc::prelude::MediaStreamTrack; |
| 3 | use libwebrtc::rtp_receiver::RtpSource; |
| 4 | |
| 5 | /// How long after its last packet a contributing source still counts as speaking. |
| 6 | const RECENT_MS: i64 = 400; |
| 7 | /// Audio level in -dBov at or below which a source counts as speaking (0 loudest, 127 silence). |
| 8 | const SPEAKING_LEVEL: u8 = 60; |
| 9 | |
| 10 | pub fn audio_sources(peer: &PeerConnection) -> Vec<RtpSource> { |
| 11 | peer.receivers() |
| 12 | .into_iter() |
| 13 | .filter(|receiver| matches!(receiver.track(), Some(MediaStreamTrack::Audio(_)))) |
| 14 | .flat_map(|receiver| receiver.sources()) |
| 15 | .collect() |
| 16 | } |
| 17 | |
| 18 | /// The source ids that are speaking now, like Teams web's `getContributingSources()` reading of the mixed meeting audio. |
| 19 | /// A mixer lists the people it mixes in as CSRCs; a direct call has only the peer's SSRC with its level. |
| 20 | pub fn speaking_sources(sources: &[RtpSource]) -> Vec<u32> { |
| 21 | let Some(newest) = sources.iter().map(|source| source.timestamp_ms).max() else { |
| 22 | return Vec::new(); |
| 23 | }; |
| 24 | let has_contributors = sources.iter().any(|source| source.contributing); |
| 25 | let mut speaking: Vec<u32> = sources |
| 26 | .iter() |
| 27 | .filter(|source| source.contributing == has_contributors) |
| 28 | .filter(|source| newest - source.timestamp_ms <= RECENT_MS) |
| 29 | .filter(|source| match source.audio_level { |
| 30 | Some(level) => level <= SPEAKING_LEVEL, |
| 31 | None => source.contributing, |
| 32 | }) |
| 33 | .map(|source| source.source_id) |
| 34 | .collect(); |
| 35 | speaking.sort_unstable(); |
| 36 | speaking.dedup(); |
| 37 | speaking |
| 38 | } |
| 39 | |
| 40 | #[cfg(test)] |
| 41 | mod tests { |
| 42 | use super::*; |
| 43 | |
| 44 | fn source(source_id: u32, contributing: bool, audio_level: Option<u8>, timestamp_ms: i64) -> RtpSource { |
| 45 | RtpSource { source_id, contributing, audio_level, timestamp_ms } |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn mixer_contributors_seen_just_now_are_speaking() { |
| 50 | let sources = [ |
| 51 | source(900, false, Some(30), 10_000), |
| 52 | source(7, true, None, 9_900), |
| 53 | source(8, true, Some(40), 10_000), |
| 54 | source(9, true, None, 9_000), |
| 55 | ]; |
| 56 | assert_eq!(speaking_sources(&sources), vec![7, 8]); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn a_quiet_contributor_is_not_speaking() { |
| 61 | let sources = [source(900, false, Some(30), 10_000), source(8, true, Some(100), 10_000)]; |
| 62 | assert!(speaking_sources(&sources).is_empty()); |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn a_direct_call_uses_the_peer_level() { |
| 67 | assert_eq!(speaking_sources(&[source(42, false, Some(35), 5_000)]), vec![42]); |
| 68 | assert!(speaking_sources(&[source(42, false, Some(127), 5_000)]).is_empty()); |
| 69 | assert!(speaking_sources(&[source(42, false, None, 5_000)]).is_empty()); |
| 70 | assert!(speaking_sources(&[]).is_empty()); |
| 71 | } |
| 72 | } |