8000 Wait for audio and video thread to fully stop to avoid corrupted reco… · flutter-webrtc/flutter-webrtc@86dac0c · GitHub
[go: up one dir, main page]

Skip to content

Commit 86dac0c

Browse files
authored
Wait for audio and video thread to fully stop to avoid corrupted recordings (#1836)
1 parent dfead5d commit 86dac0c

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

android/src/main/java/com/cloudwebrtc/webrtc/record/VideoFileRenderer.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Modifications by Signify, Copyright 2025, Signify Holding - SPDX-License-Identifier: MIT
2+
13
package com.cloudwebrtc.webrtc.record;
24

35
import android.media.MediaCodec;
@@ -19,6 +21,7 @@
1921

2022
import java.io.IOException;
2123
import java.nio.ByteBuffer;
24+
import java.util.concurrent.CountDownLatch;
2225

2326
class VideoFileRenderer implements VideoSink, SamplesReadyCallback {
2427
private static final String TAG = "VideoFileRenderer";
@@ -127,27 +130,47 @@ private void renderFrameOnRenderThread(VideoFrame frame) {
127130
/**
128131
* Release all resources. All already posted frames will be rendered first.
129132
*/
133+
// Start Signify modification
130134
void release() {
131135
isRunning = false;
132-
if (audioThreadHandler != null)
136+
CountDownLatch latch = new CountDownLatch(audioThreadHandler != null ? 2 : 1);
137+
if (audioThreadHandler != null) {
133138
audioThreadHandler.post(() -> {
134-
if (audioEncoder != null) {
135-
audioEncoder.stop();
136-
audioEncoder.release();
139+
try{
140+
if (audioEncoder != null) {
141+
audioEncoder.stop();
142+
audioEncoder.release();
143+
}
144+
audioThread.quit();
145+
} finally {
146+
latch.countDown();
137147
}
138-
audioThread.quit();
139148
});
149+
}
150+
140151
renderThreadHandler.post(() -> {
141-
if (encoder != null) {
142-
encoder.stop();
143-
encoder.release();
152+
try {
153+
if (encoder != null) {
154+
encoder.stop();
155+
encoder.release();
156+
}
157+
eglBase.release();
158+
mediaMuxer.stop();
159+
mediaMuxer.release();
160+
renderThread.quit();
161+
} finally {
162+
latch.countDown();
144163
}
145-
eglBase.release();
146-
mediaMuxer.stop();
147-
mediaMuxer.release();
148-
renderThread.quit();
149164
});
165+
166+
try {
167+
latch.await();
168+
} catch (InterruptedException e) {
169+
Log.e(TAG, "Release interrupted", e);
170+
Thread.currentThread().interrupt();
171+
}
150172
}
173+
// End Signify modification
151174

152175
private boolean encoderStarted = false;
153176
private volatile boolean muxerStarted = false;

0 commit comments

Comments
 (0)
0