|
23 | 23 | import static org.junit.jupiter.api.Assertions.fail; |
24 | 24 |
|
25 | 25 | import java.nio.ByteBuffer; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.concurrent.CopyOnWriteArrayList; |
26 | 29 | import java.util.concurrent.CountDownLatch; |
27 | 30 | import java.util.concurrent.TimeUnit; |
28 | 31 | import java.util.concurrent.atomic.AtomicInteger; |
29 | 32 | import java.util.concurrent.atomic.AtomicReference; |
30 | 33 |
|
| 34 | +import dev.onvoid.webrtc.media.SyncClock; |
31 | 35 | import dev.onvoid.webrtc.media.video.VideoTrack; |
32 | 36 | import dev.onvoid.webrtc.media.video.VideoTrackSink; |
33 | 37 |
|
@@ -256,6 +260,49 @@ void audioFramesAreTransformed() throws Exception { |
256 | 260 | } |
257 | 261 | } |
258 | 262 |
|
| 263 | + @Test |
| 264 | + void audioPushedWithoutTimestampCarriesCaptureTimeOfPush() throws Exception { |
| 265 | + // The call pushes its audio without a timestamp. The source then |
| 266 | + // stamps each chunk with the moment of the push, on the clock a |
| 267 | + // caller's capture time is on, and WebRTC carries that time through |
| 268 | + // to the encoded frames of the sender. |
| 269 | + List<Long> captureTimesUs = new CopyOnWriteArrayList<>(); |
| 270 | + |
| 271 | + try (TestMediaCall call = new TestMediaCall(factory, false, true)) { |
| 272 | + call.getAudioSender().setTransform(frame -> captureTimesUs.add(frame.getCaptureTimeUs())); |
| 273 | + |
| 274 | + call.negotiate(); |
| 275 | + call.awaitConnected(); |
| 276 | + |
| 277 | + long startUs = SyncClock.currentTimeUs(); |
| 278 | + |
| 279 | + call.startMedia(); |
| 280 | + |
| 281 | + waitFor(() -> captureTimesUs.size() >= 20); |
| 282 | + |
| 283 | + List<Long> seen = new ArrayList<>(captureTimesUs); |
| 284 | + long endUs = SyncClock.currentTimeUs(); |
| 285 | + |
| 286 | + // WebRTC keeps the capture time in whole milliseconds, cut off |
| 287 | + // rather than rounded. |
| 288 | + long earliestUs = startUs - 1000; |
| 289 | + |
| 290 | + for (int i = 0; i < seen.size(); i++) { |
| 291 | + long captureUs = seen.get(i); |
| 292 | + |
| 293 | + assertTrue(captureUs >= earliestUs && captureUs <= endUs, |
| 294 | + "Capture time " + captureUs + " µs is not between " + earliestUs |
| 295 | + + " µs and " + endUs + " µs on SyncClock.currentTimeUs()"); |
| 296 | + |
| 297 | + if (i > 0) { |
| 298 | + assertTrue(captureUs >= seen.get(i - 1), |
| 299 | + "Capture time went back from " + seen.get(i - 1) + " µs to " |
| 300 | + + captureUs + " µs"); |
| 301 | + } |
| 302 | + } |
| 303 | + } |
| 304 | + } |
| 305 | + |
259 | 306 | @Test |
260 | 307 | void requestKeyFrameReachesSender() throws Exception { |
261 | 308 | AtomicInteger keyFrames = new AtomicInteger(); |
|
0 commit comments