Skip to content

Commit

Permalink
add encoders error callback
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Sep 18, 2023
1 parent 8824da8 commit bc51ca4
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 16 deletions.
19 changes: 15 additions & 4 deletions encoder/src/main/java/com/pedro/encoder/BaseEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public abstract class BaseEncoder implements EncoderCallback {
protected boolean shouldReset = true;
protected boolean prepared = false;
private Handler handler;
private EncoderErrorCallback encoderErrorCallback;

public void setEncoderErrorCallback(EncoderErrorCallback encoderErrorCallback) {
this.encoderErrorCallback = encoderErrorCallback;
}

public void restart() {
start(false);
Expand Down Expand Up @@ -86,7 +91,7 @@ private void initCodec() {
getDataFromEncoder();
} catch (IllegalStateException e) {
Log.i(TAG, "Encoding error", e);
reloadCodec();
reloadCodec(e);
}
}
});
Expand All @@ -108,8 +113,12 @@ protected void fixTimeStamp(MediaCodec.BufferInfo info) {
}
}

private void reloadCodec() {
private void reloadCodec(IllegalStateException e) {
//Sometimes encoder crash, we will try recover it. Reset encoder a time if crash
EncoderErrorCallback callback = encoderErrorCallback;
if (callback != null) {
shouldReset = callback.onEncodeError(TAG, e);
}
if (shouldReset) {
Log.e(TAG, "Encoder crashed, trying to recover it");
reset();
Expand Down Expand Up @@ -254,7 +263,7 @@ public void onInputBufferAvailable(@NonNull MediaCodec mediaCodec, int inBufferI
inputAvailable(mediaCodec, inBufferIndex);
} catch (IllegalStateException e) {
Log.i(TAG, "Encoding error", e);
reloadCodec();
reloadCodec(e);
}
}

Expand All @@ -265,13 +274,15 @@ public void onOutputBufferAvailable(@NonNull MediaCodec mediaCodec, int outBuffe
outputAvailable(mediaCodec, outBufferIndex, bufferInfo);
} catch (IllegalStateException e) {
Log.i(TAG, "Encoding error", e);
reloadCodec();
reloadCodec(e);
}
}

@Override
public void onError(@NonNull MediaCodec mediaCodec, @NonNull MediaCodec.CodecException e) {
Log.e(TAG, "Error", e);
EncoderErrorCallback callback = encoderErrorCallback;
if (callback != null) callback.onCodecError(TAG, e);
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions encoder/src/main/java/com/pedro/encoder/EncoderErrorCallback.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.pedro.encoder

import android.media.MediaCodec
import java.lang.IllegalStateException

/**
* Created by pedro on 18/9/23.
*/
interface EncoderErrorCallback {
fun onCodecError(type: String, e: MediaCodec.CodecException)

/**
* @return indicate if should try reset encoder
*/
fun onEncodeError(type: String, e: IllegalStateException): Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public boolean createMicrophone(int audioSource, int sampleRate, boolean isStere
if (noiseSuppressor) audioPostProcessEffect.enableNoiseSuppressor();
String chl = (isStereo) ? "Stereo" : "Mono";
if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
throw new IllegalArgumentException("Some parameters specified is not valid");
throw new IllegalArgumentException("Some parameters specified are not valid");
}
Log.i(TAG, "Microphone created, " + sampleRate + "hz, " + chl);
created = true;
Expand Down Expand Up @@ -138,7 +138,7 @@ public boolean createInternalMicrophone(AudioPlaybackCaptureConfiguration config
if (noiseSuppressor) audioPostProcessEffect.enableNoiseSuppressor();
String chl = (isStereo) ? "Stereo" : "Mono";
if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
throw new IllegalArgumentException("Some parameters specified is not valid");
throw new IllegalArgumentException("Some parameters specified are not valid");
}
Log.i(TAG, "Internal microphone created, " + sampleRate + "hz, " + chl);
created = true;
Expand All @@ -164,14 +164,11 @@ public synchronized void start() {
handlerThread = new HandlerThread(TAG);
handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
while (running) {
Frame frame = read();
if (frame != null) {
getMicrophoneData.inputPCMData(frame);
}
handler.post(() -> {
while (running) {
Frame frame = read();
if (frame != null) {
getMicrophoneData.inputPCMData(frame);
}
}
});
Expand All @@ -183,8 +180,7 @@ private void init() {
running = true;
Log.i(TAG, "Microphone started");
} else {
Log.e(TAG, "Error starting, microphone was stopped or not created, "
+ "use createMicrophone() before start()");
throw new IllegalStateException("Error starting, microphone was stopped or not created, use createMicrophone() before start()");
}
}

Expand Down
10 changes: 10 additions & 0 deletions library/src/main/java/com/pedro/library/base/Camera1Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.pedro.encoder.EncoderErrorCallback;
import com.pedro.encoder.Frame;
import com.pedro.encoder.audio.AudioEncoder;
import com.pedro.encoder.audio.GetAacData;
Expand Down Expand Up @@ -172,6 +173,15 @@ public void setCameraCallbacks(CameraCallbacks callbacks) {
cameraManager.setCameraCallbacks(callbacks);
}

/**
* Set a callback to know errors related with Video/Audio encoders
* @param encoderErrorCallback callback to use, null to remove
*/
public void setEncoderErrorCallback(EncoderErrorCallback encoderErrorCallback) {
videoEncoder.setEncoderErrorCallback(encoderErrorCallback);
audioEncoder.setEncoderErrorCallback(encoderErrorCallback);
}

/**
* Set an audio effect modifying microphone's PCM buffer.
*/
Expand Down
10 changes: 10 additions & 0 deletions library/src/main/java/com/pedro/library/base/Camera2Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.pedro.encoder.EncoderErrorCallback;
import com.pedro.encoder.Frame;
import com.pedro.encoder.audio.AudioEncoder;
import com.pedro.encoder.audio.GetAacData;
Expand Down Expand Up @@ -184,6 +185,15 @@ public void setCameraCallbacks(CameraCallbacks callbacks) {
cameraManager.setCameraCallbacks(callbacks);
}

/**
* Set a callback to know errors related with Video/Audio encoders
* @param encoderErrorCallback callback to use, null to remove
*/
public void setEncoderErrorCallback(EncoderErrorCallback encoderErrorCallback) {
videoEncoder.setEncoderErrorCallback(encoderErrorCallback);
audioEncoder.setEncoderErrorCallback(encoderErrorCallback);
}

/**
* Set an audio effect modifying microphone's PCM buffer.
*/
Expand Down
10 changes: 10 additions & 0 deletions library/src/main/java/com/pedro/library/base/DisplayBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.pedro.encoder.EncoderErrorCallback;
import com.pedro.encoder.Frame;
import com.pedro.encoder.audio.AudioEncoder;
import com.pedro.encoder.audio.GetAacData;
Expand Down Expand Up @@ -123,6 +124,15 @@ public void setMicrophoneMode(MicrophoneMode microphoneMode) {
}
}

/**
* Set a callback to know errors related with Video/Audio encoders
* @param encoderErrorCallback callback to use, null to remove
*/
public void setEncoderErrorCallback(EncoderErrorCallback encoderErrorCallback) {
videoEncoder.setEncoderErrorCallback(encoderErrorCallback);
audioEncoder.setEncoderErrorCallback(encoderErrorCallback);
}

/**
* Set an audio effect modifying microphone's PCM buffer.
*/
Expand Down
10 changes: 10 additions & 0 deletions library/src/main/java/com/pedro/library/base/FromFileBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.pedro.encoder.EncoderErrorCallback;
import com.pedro.encoder.Frame;
import com.pedro.encoder.audio.AudioEncoder;
import com.pedro.encoder.audio.GetAacData;
Expand Down Expand Up @@ -225,6 +226,15 @@ public boolean isAudioDeviceEnabled() {
&& audioTrackPlayer.getPlayState() == AudioTrack.PLAYSTATE_PLAYING;
}

/**
* Set a callback to know errors related with Video/Audio encoders
* @param encoderErrorCallback callback to use, null to remove
*/
public void setEncoderErrorCallback(EncoderErrorCallback encoderErrorCallback) {
videoEncoder.setEncoderErrorCallback(encoderErrorCallback);
audioEncoder.setEncoderErrorCallback(encoderErrorCallback);
}

public void playAudioDevice() {
if (audioEnabled) {
if (isAudioDeviceEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.pedro.encoder.EncoderErrorCallback;
import com.pedro.encoder.Frame;
import com.pedro.encoder.audio.AudioEncoder;
import com.pedro.encoder.audio.GetAacData;
Expand Down Expand Up @@ -86,6 +87,14 @@ public void setMicrophoneMode(MicrophoneMode microphoneMode) {
}
}

/**
* Set a callback to know errors related with Video/Audio encoders
* @param encoderErrorCallback callback to use, null to remove
*/
public void setEncoderErrorCallback(EncoderErrorCallback encoderErrorCallback) {
audioEncoder.setEncoderErrorCallback(encoderErrorCallback);
}

/**
* Set an audio effect modifying microphone's PCM buffer.
*/
Expand Down
10 changes: 10 additions & 0 deletions library/src/main/java/com/pedro/library/base/StreamBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.view.Surface
import android.view.SurfaceView
import android.view.TextureView
import androidx.annotation.RequiresApi
import com.pedro.encoder.EncoderErrorCallback
import com.pedro.encoder.Frame
import com.pedro.encoder.audio.AudioEncoder
import com.pedro.encoder.audio.GetAacData
Expand Down Expand Up @@ -267,6 +268,15 @@ abstract class StreamBase(
audioManager.changeAudioSourceDisabled()
}

/**
* Set a callback to know errors related with Video/Audio encoders
* @param encoderErrorCallback callback to use, null to remove
*/
fun setEncoderErrorCallback(encoderErrorCallback: EncoderErrorCallback?) {
videoEncoder.setEncoderErrorCallback(encoderErrorCallback)
audioEncoder.setEncoderErrorCallback(encoderErrorCallback)
}

/**
* Set a custom size of audio buffer input.
* If you set 0 or less you can disable it to use library default value.
Expand Down

0 comments on commit bc51ca4

Please sign in to comment.