Skip to content

Commit fd8eb0e

Browse files
authored
test: check the capture time of audio pushed without a timestamp (#473)
A sender transform sees the absolute capture time the source stamped, which must be on the clock of SyncClock.currentTimeUs().
1 parent ecdd3e0 commit fd8eb0e

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

‎webrtc/src/test/java/dev/onvoid/webrtc/RTCEncodedFrameTransformTests.java‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@
2323
import static org.junit.jupiter.api.Assertions.fail;
2424

2525
import java.nio.ByteBuffer;
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
import java.util.concurrent.CopyOnWriteArrayList;
2629
import java.util.concurrent.CountDownLatch;
2730
import java.util.concurrent.TimeUnit;
2831
import java.util.concurrent.atomic.AtomicInteger;
2932
import java.util.concurrent.atomic.AtomicReference;
3033

34+
import dev.onvoid.webrtc.media.SyncClock;
3135
import dev.onvoid.webrtc.media.video.VideoTrack;
3236
import dev.onvoid.webrtc.media.video.VideoTrackSink;
3337

@@ -256,6 +260,49 @@ void audioFramesAreTransformed() throws Exception {
256260
}
257261
}
258262

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+
259306
@Test
260307
void requestKeyFrameReachesSender() throws Exception {
261308
AtomicInteger keyFrames = new AtomicInteger();

0 commit comments

Comments
 (0)