Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pedroSG94/RootEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Nov 14, 2024
2 parents 204efe0 + 53a0dc5 commit 8b428e5
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 8b428e5

Please sign in to comment.