Fix BitmapFactoryVideoRenderer sync issues

This commit is contained in:
Dustin 2022-01-28 17:17:32 -07:00
parent 1d85bf2456
commit 1ff78292d1

View File

@ -6,6 +6,7 @@ import android.graphics.Canvas;
import android.graphics.Point; import android.graphics.Point;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.Handler; import android.os.Handler;
import android.os.SystemClock;
import android.view.Surface; import android.view.Surface;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -20,27 +21,21 @@ import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.source.SampleStream; import com.google.android.exoplayer2.source.SampleStream;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class BitmapFactoryVideoRenderer extends BaseRenderer { public class BitmapFactoryVideoRenderer extends BaseRenderer {
private static final String TAG = "BitmapFactoryRenderer"; private static final String TAG = "BitmapFactoryRenderer";
final VideoRendererEventListener.EventDispatcher eventDispatcher; final VideoRendererEventListener.EventDispatcher eventDispatcher;
@Nullable @Nullable
Surface surface; volatile Surface surface;
private boolean firstFrameRendered;
private final Rect rect = new Rect(); private final Rect rect = new Rect();
private final Point lastSurface = new Point(); private final Point lastSurface = new Point();
private final RenderRunnable renderRunnable = new RenderRunnable();
private final Thread thread = new Thread(renderRunnable, "BitmapFactoryVideoRenderer");
private VideoSize lastVideoSize = VideoSize.UNKNOWN; private VideoSize lastVideoSize = VideoSize.UNKNOWN;
@Nullable
private ThreadPoolExecutor renderExecutor;
@Nullable
private Thread thread;
private long currentTimeUs; private long currentTimeUs;
private long nextFrameUs; private long frameUs;
private long frameUs = Long.MIN_VALUE; boolean ended;
private boolean ended; @Nullable
private DecoderCounters decoderCounters; private DecoderCounters decoderCounters;
public BitmapFactoryVideoRenderer(@Nullable Handler eventHandler, public BitmapFactoryVideoRenderer(@Nullable Handler eventHandler,
@ -58,69 +53,44 @@ public class BitmapFactoryVideoRenderer extends BaseRenderer {
@Override @Override
protected void onEnabled(boolean joining, boolean mayRenderStartOfStream) protected void onEnabled(boolean joining, boolean mayRenderStartOfStream)
throws ExoPlaybackException { throws ExoPlaybackException {
firstFrameRendered = ended = false;
renderExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(3));
decoderCounters = new DecoderCounters(); decoderCounters = new DecoderCounters();
eventDispatcher.enabled(decoderCounters); eventDispatcher.enabled(decoderCounters);
thread.start();
} }
@Override @Override
protected void onDisabled() { protected void onDisabled() {
renderExecutor.shutdownNow(); renderRunnable.running = false;
thread.interrupt();
@Nullable
final DecoderCounters decoderCounters = this.decoderCounters;
if (decoderCounters != null) {
eventDispatcher.disabled(decoderCounters); eventDispatcher.disabled(decoderCounters);
} }
}
private void onFormatChanged(@NonNull FormatHolder formatHolder) { private void onFormatChanged(@NonNull FormatHolder formatHolder) {
@Nullable final Format format = formatHolder.format; @Nullable final Format format = formatHolder.format;
if (format != null) { if (format != null) {
eventDispatcher.inputFormatChanged(format, null);
frameUs = (long)(1_000_000L / format.frameRate); frameUs = (long)(1_000_000L / format.frameRate);
eventDispatcher.inputFormatChanged(format, null);
} }
} }
@Override @Override
public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException { public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
//Log.d(TAG, "Render: us=" + positionUs);
synchronized (eventDispatcher) { synchronized (eventDispatcher) {
currentTimeUs = positionUs; currentTimeUs = positionUs;
eventDispatcher.notify(); eventDispatcher.notify();
} }
if (renderExecutor.getActiveCount() > 0) {
//Handle decoder overrun
if (positionUs > nextFrameUs) {
long us = (positionUs - nextFrameUs) + frameUs;
long dropped = us / frameUs;
eventDispatcher.droppedFrames((int)dropped, us);
nextFrameUs += frameUs * dropped;
}
return;
}
final FormatHolder formatHolder = getFormatHolder();
final DecoderInputBuffer decoderInputBuffer =
new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);
final int result = readSource(formatHolder, decoderInputBuffer,
frameUs == Long.MIN_VALUE ? SampleStream.FLAG_REQUIRE_FORMAT : 0);
if (result == C.RESULT_BUFFER_READ) {
renderExecutor.execute(new RenderRunnable(decoderInputBuffer, nextFrameUs));
if (decoderInputBuffer.isEndOfStream()) {
ended = true;
} else {
nextFrameUs += frameUs;
}
} else if (result == C.RESULT_FORMAT_READ) {
onFormatChanged(formatHolder);
}
} }
@Override @Override
protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException { protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException {
nextFrameUs = positionUs;
@Nullable
final Thread thread = this.thread;
if (thread != null) {
thread.interrupt(); thread.interrupt();
} }
}
@Override @Override
public void handleMessage(int messageType, @Nullable Object message) throws ExoPlaybackException { public void handleMessage(int messageType, @Nullable Object message) throws ExoPlaybackException {
@ -141,7 +111,7 @@ public class BitmapFactoryVideoRenderer extends BaseRenderer {
@Override @Override
public boolean isEnded() { public boolean isEnded() {
return ended && renderExecutor.getActiveCount() == 0; return renderRunnable.ended;
} }
@Override @Override
@ -154,61 +124,31 @@ public class BitmapFactoryVideoRenderer extends BaseRenderer {
} }
class RenderRunnable implements Runnable { class RenderRunnable implements Runnable {
@Nullable private volatile boolean ended;
private DecoderInputBuffer decoderInputBuffer; private boolean firstFrameRendered;
private final long renderUs; private volatile boolean running = true;
RenderRunnable(@NonNull final DecoderInputBuffer decoderInputBuffer, long renderUs) {
this.decoderInputBuffer = decoderInputBuffer;
this.renderUs = renderUs;
}
private boolean maybeDropFrame(long frameUs) {
if (Math.abs(frameUs - currentTimeUs) > frameUs) {
eventDispatcher.droppedFrames(1, frameUs);
return true;
}
return false;
}
public void run() {
if (maybeDropFrame(renderUs)) {
return;
}
@Nullable @Nullable
final ByteBuffer byteBuffer = decoderInputBuffer.data; private Bitmap decodeInputBuffer(final DecoderInputBuffer decoderInputBuffer) {
@Nullable @Nullable final ByteBuffer byteBuffer = decoderInputBuffer.data;
final Surface surface = BitmapFactoryVideoRenderer.this.surface; if (byteBuffer != null) {
if (byteBuffer != null && surface != null) {
final Bitmap bitmap; final Bitmap bitmap;
try { try {
bitmap = BitmapFactory.decodeByteArray(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.arrayOffset() + byteBuffer.position()); bitmap = BitmapFactory.decodeByteArray(byteBuffer.array(), byteBuffer.arrayOffset(),
} catch (Exception e) { byteBuffer.arrayOffset() + byteBuffer.position());
eventDispatcher.videoCodecError(e);
return;
}
if (bitmap == null) { if (bitmap == null) {
eventDispatcher.videoCodecError(new NullPointerException("Decode bytes failed")); eventDispatcher.videoCodecError(new NullPointerException("Decode bytes failed"));
return; } else {
return bitmap;
} }
decoderInputBuffer = null; } catch (Exception e) {
//Wait for time to advance to display the Bitmap eventDispatcher.videoCodecError(e);
synchronized (eventDispatcher) {
while (currentTimeUs < renderUs) {
try {
thread = Thread.currentThread();
eventDispatcher.wait();
} catch (InterruptedException e) {
//If we are interrupted, treat as a cancel
return;
} finally {
thread = null;
} }
} }
return null;
} }
if (maybeDropFrame(renderUs)) {
return; private void renderBitmap(final Bitmap bitmap, @NonNull final Surface surface) {
}
//Log.d(TAG, "Drawing: " + bitmap.getWidth() + "x" + bitmap.getHeight()); //Log.d(TAG, "Drawing: " + bitmap.getWidth() + "x" + bitmap.getHeight());
final Canvas canvas = surface.lockCanvas(null); final Canvas canvas = surface.lockCanvas(null);
@ -238,12 +178,85 @@ public class BitmapFactoryVideoRenderer extends BaseRenderer {
canvas.drawBitmap(bitmap, null, rect, null); canvas.drawBitmap(bitmap, null, rect, null);
surface.unlockCanvasAndPost(canvas); surface.unlockCanvasAndPost(canvas);
@Nullable
final DecoderCounters decoderCounters = BitmapFactoryVideoRenderer.this.decoderCounters;
if (decoderCounters != null) {
decoderCounters.renderedOutputBufferCount++; decoderCounters.renderedOutputBufferCount++;
}
if (!firstFrameRendered) { if (!firstFrameRendered) {
firstFrameRendered = true; firstFrameRendered = true;
eventDispatcher.renderedFirstFrame(surface); eventDispatcher.renderedFirstFrame(surface);
} }
} }
/**
*
* @return true if interrupted
*/
private boolean sleep() {
synchronized (eventDispatcher) {
try {
eventDispatcher.wait();
return false;
} catch (InterruptedException e) {
//If we are interrupted, treat as a cancel
return true;
}
}
}
public void run() {
final FormatHolder formatHolder = getFormatHolder();
@NonNull
final DecoderInputBuffer decoderInputBuffer =
new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);
long start = SystemClock.uptimeMillis();
main:
while (running) {
decoderInputBuffer.clear();
final int result = readSource(formatHolder, decoderInputBuffer,
formatHolder.format == null ? SampleStream.FLAG_REQUIRE_FORMAT : 0);
if (result == C.RESULT_BUFFER_READ) {
if (decoderInputBuffer.isEndOfStream()) {
ended = true;
if (!sleep()) {
ended = false;
}
continue;
}
final long leadUs = decoderInputBuffer.timeUs - currentTimeUs;
//If we are more than 1/2 a frame behind, skip the next frame
if (leadUs < -frameUs / 2) {
eventDispatcher.droppedFrames(1, SystemClock.uptimeMillis() - start);
start = SystemClock.uptimeMillis();
continue;
}
start = SystemClock.uptimeMillis();
@Nullable
final Bitmap bitmap = decodeInputBuffer(decoderInputBuffer);
if (bitmap == null) {
continue;
}
while (currentTimeUs < decoderInputBuffer.timeUs) {
//Log.d(TAG, "Sleep: us=" + currentTimeUs);
if (sleep()) {
continue main;
}
if (!running) {
break main;
}
}
@Nullable
final Surface surface = BitmapFactoryVideoRenderer.this.surface;
if (surface != null) {
renderBitmap(bitmap, surface);
}
} else if (result == C.RESULT_FORMAT_READ) {
onFormatChanged(formatHolder);
}
}
ended = true;
} }
} }
} }