From 50a56bdf722d642cfa9e2d6667371b6ad15cf5ab Mon Sep 17 00:00:00 2001 From: yrom Date: Tue, 5 Dec 2017 15:03:14 +0800 Subject: [PATCH] Correct MiRecorder calulating audio frame's timestamp --- app/build.gradle | 4 +-- .../net/yrom/screenrecorder/MicRecorder.java | 25 +++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 8bf2625..518e11a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "net.yrom.screenrecorder" minSdkVersion 21 targetSdkVersion 26 - versionCode 10 - versionName '2.1' + versionCode 11 + versionName '2.2' } compileOptions { diff --git a/app/src/main/java/net/yrom/screenrecorder/MicRecorder.java b/app/src/main/java/net/yrom/screenrecorder/MicRecorder.java index 3018baa..fcf5842 100644 --- a/app/src/main/java/net/yrom/screenrecorder/MicRecorder.java +++ b/app/src/main/java/net/yrom/screenrecorder/MicRecorder.java @@ -306,31 +306,30 @@ private void feedAudioEncoder(int index) { * 1 sample = 16 bit */ private long calculateFrameTimestamp(int totalBits) { - int frames = totalBits >> 4; - long framesUs = mFramesUsCache.get(frames, -1); - if (framesUs == -1) { - framesUs = frames * 1000_000 / mChannelsSampleRate; - mFramesUsCache.put(frames, framesUs); + int samples = totalBits >> 4; + long frameUs = mFramesUsCache.get(samples, -1); + if (frameUs == -1) { + frameUs = samples * 1000_000 / mChannelsSampleRate; + mFramesUsCache.put(samples, frameUs); } long timeUs = SystemClock.elapsedRealtimeNanos() / 1000; + // accounts the delay of polling the audio sample data + timeUs -= frameUs; long currentUs; long lastFrameUs = mFramesUsCache.get(LAST_FRAME_ID, -1); - if (lastFrameUs == -1) { - // accounts the delay of polling the audio sample data - timeUs -= framesUs; + if (lastFrameUs == -1) { // it's the first frame currentUs = timeUs; } else { - currentUs = lastFrameUs + framesUs; - //+ (1000_000 * mTotalSamples) / mChannelsSampleRate; + currentUs = lastFrameUs; } if (VERBOSE) - Log.d(TAG, "count frames pts: " + currentUs + ", time pts: " + timeUs + ", frames: " + frames); + Log.i(TAG, "count samples pts: " + currentUs + ", time pts: " + timeUs + ", samples: " + samples); // maybe too late to acquire sample data - if (timeUs - currentUs >= (framesUs << 1)) { + if (timeUs - currentUs >= (frameUs << 1)) { // reset currentUs = timeUs; } - mFramesUsCache.put(LAST_FRAME_ID, currentUs); + mFramesUsCache.put(LAST_FRAME_ID, currentUs + frameUs); return currentUs; }