File
Blob: firmware/vendor/str0m/tests/vla.rs
| 1 | use std::time::Instant; |
| 2 | |
| 3 | use str0m::media::{MediaKind, MediaTime}; |
| 4 | use str0m::rtp::vla::{ResolutionAndFramerate, Serializer as VlaSerializer}; |
| 5 | use str0m::rtp::vla::{SimulcastStreamAllocation, SpatialLayerAllocation}; |
| 6 | use str0m::rtp::vla::{TemporalLayerAllocation, URI as VLA_URI, VideoLayersAllocation}; |
| 7 | use str0m::rtp::{Extension, ExtensionValues, RtpWrite, Ssrc}; |
| 8 | use str0m::{Event, Rtc, RtcError}; |
| 9 | |
| 10 | mod common; |
| 11 | use common::{connect_l_r_with_rtc, init_crypto_default, init_log, progress}; |
| 12 | |
| 13 | #[test] |
| 14 | pub fn vla_rtp_mode() -> Result<(), RtcError> { |
| 15 | init_log(); |
| 16 | init_crypto_default(); |
| 17 | |
| 18 | let vla_ext = Extension::with_serializer(VLA_URI, VlaSerializer); |
| 19 | |
| 20 | let now = Instant::now(); |
| 21 | let rtc_l = Rtc::builder() |
| 22 | .set_rtp_mode(true) |
| 23 | .set_extension(14, vla_ext.clone()) |
| 24 | .build(now); |
| 25 | let rtc_r = Rtc::builder() |
| 26 | .set_rtp_mode(true) |
| 27 | .set_extension(14, vla_ext) |
| 28 | .build(now); |
| 29 | |
| 30 | let (mut l, mut r) = connect_l_r_with_rtc(rtc_l, rtc_r); |
| 31 | |
| 32 | let mid = "vid".into(); |
| 33 | let ssrc_tx: Ssrc = 1000.into(); |
| 34 | |
| 35 | l.direct_api().declare_media(mid, MediaKind::Video); |
| 36 | l.direct_api().declare_stream_tx(ssrc_tx, None, mid, None); |
| 37 | r.direct_api().declare_media(mid, MediaKind::Video); |
| 38 | |
| 39 | let max = l.last.max(r.last); |
| 40 | l.last = max; |
| 41 | r.last = max; |
| 42 | |
| 43 | let params = l.params_vp8(); |
| 44 | let pt = params.pt(); |
| 45 | let ssrc = l.direct_api().stream_tx_by_mid(mid, None).unwrap().ssrc(); |
| 46 | |
| 47 | let vla = VideoLayersAllocation { |
| 48 | current_simulcast_stream_index: 0, |
| 49 | simulcast_streams: vec![SimulcastStreamAllocation { |
| 50 | spatial_layers: vec![SpatialLayerAllocation { |
| 51 | temporal_layers: vec![TemporalLayerAllocation { |
| 52 | cumulative_kbps: 500, |
| 53 | }], |
| 54 | resolution_and_framerate: Some(ResolutionAndFramerate { |
| 55 | width: 640, |
| 56 | height: 480, |
| 57 | framerate: 30, |
| 58 | }), |
| 59 | }], |
| 60 | }], |
| 61 | }; |
| 62 | |
| 63 | let mut exts = ExtensionValues { |
| 64 | mid: Some(mid), |
| 65 | ..Default::default() |
| 66 | }; |
| 67 | exts.user_values.set(vla.clone()); |
| 68 | |
| 69 | let last = l.last; |
| 70 | let mut direct = l.direct_api(); |
| 71 | let stream = direct.stream_tx(&ssrc).unwrap(); |
| 72 | stream.write_rtp(RtpWrite::new(pt, 1000_u64.into(), 0, last, [0xAA; 10]).ext_vals(exts)); |
| 73 | |
| 74 | let mut vla_received = false; |
| 75 | for _ in 0..100 { |
| 76 | progress(&mut l, &mut r)?; |
| 77 | |
| 78 | for (_, event) in &r.events { |
| 79 | if let Event::RtpPacket(packet) = event { |
| 80 | if let Some(v) = packet |
| 81 | .header |
| 82 | .ext_vals |
| 83 | .user_values |
| 84 | .get::<VideoLayersAllocation>() |
| 85 | { |
| 86 | assert_eq!(v, &vla); |
| 87 | assert_eq!( |
| 88 | v.simulcast_streams[0].spatial_layers[0].temporal_layers[0].cumulative_kbps, |
| 89 | 500 |
| 90 | ); |
| 91 | assert_eq!( |
| 92 | v.simulcast_streams[0].spatial_layers[0] |
| 93 | .resolution_and_framerate |
| 94 | .as_ref() |
| 95 | .unwrap() |
| 96 | .width, |
| 97 | 640 |
| 98 | ); |
| 99 | vla_received = true; |
| 100 | } else { |
| 101 | println!( |
| 102 | "Packet received but no VLA. Exts: {:?}", |
| 103 | packet.header.ext_vals |
| 104 | ); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | if vla_received { |
| 109 | break; |
| 110 | } |
| 111 | r.events.clear(); |
| 112 | } |
| 113 | |
| 114 | assert!(vla_received); |
| 115 | |
| 116 | Ok(()) |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | pub fn vla_frame_mode() -> Result<(), RtcError> { |
| 121 | init_log(); |
| 122 | init_crypto_default(); |
| 123 | |
| 124 | let vla_ext = Extension::with_serializer(VLA_URI, VlaSerializer); |
| 125 | |
| 126 | let now = Instant::now(); |
| 127 | let rtc_l = Rtc::builder().set_extension(14, vla_ext.clone()).build(now); |
| 128 | let rtc_r = Rtc::builder().set_extension(14, vla_ext).build(now); |
| 129 | |
| 130 | let (mut l, mut r) = connect_l_r_with_rtc(rtc_l, rtc_r); |
| 131 | |
| 132 | let mid = "vid".into(); |
| 133 | let ssrc_tx: Ssrc = 1000.into(); |
| 134 | |
| 135 | l.direct_api().declare_media(mid, MediaKind::Video); |
| 136 | l.direct_api().declare_stream_tx(ssrc_tx, None, mid, None); |
| 137 | r.direct_api().declare_media(mid, MediaKind::Video); |
| 138 | |
| 139 | let max = l.last.max(r.last); |
| 140 | l.last = max; |
| 141 | r.last = max; |
| 142 | |
| 143 | let vla = VideoLayersAllocation { |
| 144 | current_simulcast_stream_index: 0, |
| 145 | simulcast_streams: vec![SimulcastStreamAllocation { |
| 146 | spatial_layers: vec![SpatialLayerAllocation { |
| 147 | temporal_layers: vec![TemporalLayerAllocation { |
| 148 | cumulative_kbps: 500, |
| 149 | }], |
| 150 | resolution_and_framerate: Some(ResolutionAndFramerate { |
| 151 | width: 640, |
| 152 | height: 480, |
| 153 | framerate: 30, |
| 154 | }), |
| 155 | }], |
| 156 | }], |
| 157 | }; |
| 158 | |
| 159 | let pt = l.params_vp8().pt(); |
| 160 | let last = l.last; |
| 161 | |
| 162 | let writer = l.writer(mid).unwrap(); |
| 163 | |
| 164 | writer |
| 165 | .user_extension_value(vla.clone()) |
| 166 | // Create ~3-4 packets to make sure vla is propagated properly to the receiver |
| 167 | .write(pt, last, MediaTime::ZERO, vec![0xAA; 5000])?; |
| 168 | |
| 169 | let mut vla_received = false; |
| 170 | for _ in 0..100 { |
| 171 | progress(&mut l, &mut r)?; |
| 172 | |
| 173 | for (_, event) in &r.events { |
| 174 | if let Event::MediaData(data) = event { |
| 175 | if let Some(v) = data.ext_vals.user_values.get::<VideoLayersAllocation>() { |
| 176 | assert_eq!(v, &vla); |
| 177 | assert_eq!( |
| 178 | v.simulcast_streams[0].spatial_layers[0].temporal_layers[0].cumulative_kbps, |
| 179 | 500 |
| 180 | ); |
| 181 | assert_eq!( |
| 182 | v.simulcast_streams[0].spatial_layers[0] |
| 183 | .resolution_and_framerate |
| 184 | .as_ref() |
| 185 | .unwrap() |
| 186 | .width, |
| 187 | 640 |
| 188 | ); |
| 189 | vla_received = true; |
| 190 | } else { |
| 191 | println!("MediaData received but no VLA. Exts: {:?}", data.ext_vals); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | if vla_received { |
| 196 | break; |
| 197 | } |
| 198 | r.events.clear(); |
| 199 | } |
| 200 | |
| 201 | assert!(vla_received); |
| 202 | |
| 203 | Ok(()) |
| 204 | } |