1919
2020import java .io .IOException ;
2121import java .nio .ByteBuffer ;
22+ import java .util .concurrent .CountDownLatch ;
2223
2324class VideoFileRenderer implements VideoSink , SamplesReadyCallback {
2425 private static final String TAG = "VideoFileRenderer" ;
@@ -127,27 +128,50 @@ private void renderFrameOnRenderThread(VideoFrame frame) {
127128 /**
128129 * Release all resources. All already posted frames will be rendered first.
129130 */
131+ // Start Signify modification
130132 void release () {
131133 isRunning = false ;
132- if (audioThreadHandler != null )
134+ CountDownLatch latch = new CountDownLatch (audioThreadHandler != null ? 2 : 1 );
135+ if (audioThreadHandler != null ) {
133136 audioThreadHandler .post (() -> {
134- if (audioEncoder != null ) {
135- audioEncoder .stop ();
136- audioEncoder .release ();
137+ try {
138+ if (audioEncoder != null ) {
139+ audioEncoder .stop ();
140+ audioEncoder .release ();
141+ }
142+ audioThread .quit ();
143+ } finally {
144+ latch .countDown ();
137145 }
138- audioThread .quit ();
139146 });
147+ }
148+
140149 renderThreadHandler .post (() -> {
141- if (encoder != null ) {
142- encoder .stop ();
143- encoder .release ();
150+ try {
151+ if (encoder != null ) {
152+ encoder .stop ();
153+ encoder .release ();
154+ }
155+ eglBase .release ();
156+ if (muxerStarted ) {
157+ mediaMuxer .stop ();
158+ mediaMuxer .release ();
159+ muxerStarted = false ;
160+ }
161+ renderThread .quit ();
162+ } finally {
163+ latch .countDown ();
144164 }
145- eglBase .release ();
146- mediaMuxer .stop ();
147- mediaMuxer .release ();
148- renderThread .quit ();
149165 });
166+
167+ try {
168+ latch .await ();
169+ } catch (InterruptedException e ) {
170+ Log .e (TAG , "Release interrupted" , e );
171+ Thread .currentThread ().interrupt ();
172+ }
150173 }
174+ // End Signify modification
151175
152176 private boolean encoderStarted = false ;
153177 private volatile boolean muxerStarted = false ;
@@ -174,7 +198,7 @@ private void drainEncoder() {
174198
175199 Log .e (TAG , "encoder output format changed: " + newFormat );
176200 trackIndex = mediaMuxer .addTrack (newFormat );
177- if (audioTrackIndex != -1 && !muxerStarted ) {
201+ if (trackIndex != -1 && !muxerStarted ) {
178202 mediaMuxer .start ();
179203 muxerStarted = true ;
180204 }
@@ -230,7 +254,7 @@ private void drainAudio() {
230254
231255 Log .w (TAG , "encoder output format changed: " + newFormat );
232256 audioTrackIndex = mediaMuxer .addTrack (newFormat );
233- if (trackIndex != -1 && !muxerStarted ) {
257+ if (audioTrackIndex != -1 && !muxerStarted ) {
234258 mediaMuxer .start ();
235259 muxerStarted = true ;
236260 }
0 commit comments