From 4b4f045e196fe037a841f7004eaabb09cf571942 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 14 Nov 2023 09:40:42 +0100 Subject: [PATCH] Fix audio PTS by the duration of 1 sample If the difference of PTS between two consecutive blocks of audio is less than 1 sample, then it will be considered as non-increasing by FFmpeg muxers having a time_base of 1/sample_rate. Increase the PTS by 1 sample instead. --- .../src/main/java/com/genymobile/scrcpy/AudioCapture.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java b/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java index c05bb41d..e3de50e6 100644 --- a/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java +++ b/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java @@ -29,6 +29,8 @@ public final class AudioCapture { // receive 4 successive blocks without waiting, then we wait for the 4 next ones). public static final int MAX_READ_SIZE = 1024 * CHANNELS * BYTES_PER_SAMPLE; + private static final long ONE_SAMPLE_US = (1000000 + SAMPLE_RATE - 1) / SAMPLE_RATE; // 1 sample in microseconds (used for fixing PTS) + private final int audioSource; private AudioRecord recorder; @@ -160,13 +162,13 @@ public final class AudioCapture { long durationUs = r * 1000000 / (CHANNELS * BYTES_PER_SAMPLE * SAMPLE_RATE); nextPts = pts + durationUs; - if (previousPts != 0 && pts < previousPts) { + if (previousPts != 0 && pts < previousPts + ONE_SAMPLE_US) { // Audio PTS may come from two sources: // - recorder.getTimestamp() if the call works; // - an estimation from the previous PTS and the packet size as a fallback. // // Therefore, the property that PTS are monotonically increasing is no guaranteed in corner cases, so enforce it. - pts = previousPts + 1; + pts = previousPts + ONE_SAMPLE_US; } previousPts = pts;