Skip to content

Commit

Permalink
use executor service in encoder if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Nov 13, 2024
1 parent 25f05b6 commit bed8cd2
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions encoder/src/main/java/com/pedro/encoder/BaseEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Created by pedro on 18/09/19.
Expand All @@ -43,6 +45,7 @@ public abstract class BaseEncoder implements EncoderCallback {
protected final G711Codec g711Codec = new G711Codec();
private final MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
private HandlerThread handlerThread;
private ExecutorService executorService;
protected BlockingQueue<Frame> queue = new ArrayBlockingQueue<>(80);
protected MediaCodec codec;
protected long presentTimeUs;
Expand Down Expand Up @@ -93,10 +96,10 @@ public void start() {
}

protected void setCallback() {
handlerThread = new HandlerThread(TAG);
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !type.equals(CodecUtil.G711_MIME)) {
handlerThread = new HandlerThread(TAG);
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
createAsyncCallback();
codec.setCallback(callback, handler);
}
Expand All @@ -105,7 +108,8 @@ protected void setCallback() {
private void initCodec() {
if (!type.equals(CodecUtil.G711_MIME)) codec.start();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || type.equals(CodecUtil.G711_MIME)) {
handler.post(() -> {
executorService = Executors.newSingleThreadExecutor();
executorService.submit(() -> {
while (running) {
try {
getDataFromEncoder();
Expand Down Expand Up @@ -173,6 +177,7 @@ public void stop(boolean resetTs) {
handlerThread.getLooper().getThread().join(500);
} catch (Exception ignored) { }
}
if (executorService != null) executorService.shutdownNow();
queue.clear();
queue = new ArrayBlockingQueue<>(80);
try {
Expand Down

0 comments on commit bed8cd2

Please sign in to comment.