Skip to content

Commit

Permalink
Merge pull request #1540 from pedroSG94/feature/refactor-callbacks
Browse files Browse the repository at this point in the history
refactor callbacks name
  • Loading branch information
pedroSG94 authored Aug 7, 2024
2 parents c360b51 + 7c23f14 commit e399f58
Show file tree
Hide file tree
Showing 46 changed files with 480 additions and 498 deletions.
10 changes: 5 additions & 5 deletions encoder/src/main/java/com/pedro/encoder/audio/AudioEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

public class AudioEncoder extends BaseEncoder implements GetMicrophoneData {

private final GetAacData getAacData;
private final GetAudioData getAudioData;
private int bitRate = 64 * 1024; //in kbps
private int sampleRate = 32000; //in hz
private int maxInputSize = 0;
Expand All @@ -49,8 +49,8 @@ public class AudioEncoder extends BaseEncoder implements GetMicrophoneData {
private long bytesRead = 0;
private boolean tsModeBuffer = false;

public AudioEncoder(GetAacData getAacData) {
this.getAacData = getAacData;
public AudioEncoder(GetAudioData getAudioData) {
this.getAudioData = getAudioData;
type = CodecUtil.AAC_MIME;
TAG = "AudioEncoder";
}
Expand Down Expand Up @@ -162,7 +162,7 @@ protected void checkBuffer(@NonNull ByteBuffer byteBuffer,
@Override
protected void sendBuffer(@NonNull ByteBuffer byteBuffer,
@NonNull MediaCodec.BufferInfo bufferInfo) {
getAacData.getAacData(byteBuffer, bufferInfo);
getAudioData.getAudioData(byteBuffer, bufferInfo);
}

/**
Expand Down Expand Up @@ -210,6 +210,6 @@ public void setTsModeBuffer(boolean tsModeBuffer) {

@Override
public void formatChanged(@NonNull MediaCodec mediaCodec, @NonNull MediaFormat mediaFormat) {
getAacData.onAudioFormat(mediaFormat);
getAudioData.onAudioFormat(mediaFormat);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.nio.ByteBuffer
/**
* Created by pedro on 19/01/17.
*/
interface GetAacData {
fun getAacData(aacBuffer: ByteBuffer, info: MediaCodec.BufferInfo)
interface GetAudioData {
fun getAudioData(audioBuffer: ByteBuffer, info: MediaCodec.BufferInfo)
fun onAudioFormat(mediaFormat: MediaFormat)
}
4 changes: 2 additions & 2 deletions encoder/src/main/java/com/pedro/encoder/video/GetVideoData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import java.nio.ByteBuffer
* Created by pedro on 20/01/17.
*/
interface GetVideoData {
fun onSpsPpsVps(sps: ByteBuffer, pps: ByteBuffer?, vps: ByteBuffer?)
fun getVideoData(h264Buffer: ByteBuffer, info: MediaCodec.BufferInfo)
fun onVideoInfo(sps: ByteBuffer, pps: ByteBuffer?, vps: ByteBuffer?)
fun getVideoData(videoBuffer: ByteBuffer, info: MediaCodec.BufferInfo)
fun onVideoFormat(mediaFormat: MediaFormat)
}
14 changes: 7 additions & 7 deletions encoder/src/main/java/com/pedro/encoder/video/VideoEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void requestKeyframe() {
bundle.putInt(MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME, 0);
try {
codec.setParameters(bundle);
getVideoData.onSpsPpsVps(oldSps, oldPps, oldVps);
getVideoData.onVideoInfo(oldSps, oldPps, oldVps);
} catch (IllegalStateException e) {
Log.e(TAG, "encoder need be running", e);
}
Expand Down Expand Up @@ -308,7 +308,7 @@ private boolean sendSPSandPPS(MediaFormat mediaFormat) {
ByteBuffer bufferInfo = mediaFormat.getByteBuffer("csd-0");
//we need an av1ConfigurationRecord with sequenceObu to work
if (bufferInfo != null && bufferInfo.remaining() > 4) {
getVideoData.onSpsPpsVps(bufferInfo, null, null);
getVideoData.onVideoInfo(bufferInfo, null, null);
return true;
}
return false;
Expand All @@ -318,14 +318,14 @@ private boolean sendSPSandPPS(MediaFormat mediaFormat) {
oldSps = byteBufferList.get(1);
oldPps = byteBufferList.get(2);
oldVps = byteBufferList.get(0);
getVideoData.onSpsPpsVps(oldSps, oldPps, oldVps);
getVideoData.onVideoInfo(oldSps, oldPps, oldVps);
return true;
//H264
} else {
oldSps = mediaFormat.getByteBuffer("csd-0");
oldPps = mediaFormat.getByteBuffer("csd-1");
oldVps = null;
getVideoData.onSpsPpsVps(oldSps, oldPps, oldVps);
getVideoData.onVideoInfo(oldSps, oldPps, oldVps);
return true;
}
}
Expand Down Expand Up @@ -513,7 +513,7 @@ protected void checkBuffer(@NonNull ByteBuffer byteBuffer,
oldSps = buffers.first;
oldPps = buffers.second;
oldVps = null;
getVideoData.onSpsPpsVps(oldSps, oldPps, oldVps);
getVideoData.onVideoInfo(oldSps, oldPps, oldVps);
spsPpsSetted = true;
} else {
Log.e(TAG, "manual sps/pps extraction failed");
Expand All @@ -526,7 +526,7 @@ protected void checkBuffer(@NonNull ByteBuffer byteBuffer,
oldSps = byteBufferList.get(1);
oldPps = byteBufferList.get(2);
oldVps = byteBufferList.get(0);
getVideoData.onSpsPpsVps(oldSps, oldPps, oldVps);
getVideoData.onVideoInfo(oldSps, oldPps, oldVps);
spsPpsSetted = true;
} else {
Log.e(TAG, "manual vps/sps/pps extraction failed");
Expand All @@ -535,7 +535,7 @@ protected void checkBuffer(@NonNull ByteBuffer byteBuffer,
Log.i(TAG, "formatChanged not called, doing manual av1 extraction...");
ByteBuffer obuSequence = extractObuSequence(byteBuffer.duplicate(), bufferInfo);
if (obuSequence != null) {
getVideoData.onSpsPpsVps(obuSequence, null, null);
getVideoData.onVideoInfo(obuSequence, null, null);
spsPpsSetted = true;
} else {
Log.e(TAG, "manual av1 extraction failed");
Expand Down
44 changes: 22 additions & 22 deletions library/src/main/java/com/pedro/library/base/Camera1Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.pedro.common.VideoCodec;
import com.pedro.encoder.EncoderErrorCallback;
import com.pedro.encoder.audio.AudioEncoder;
import com.pedro.encoder.audio.GetAacData;
import com.pedro.encoder.audio.GetAudioData;
import com.pedro.encoder.input.audio.CustomAudioEffect;
import com.pedro.encoder.input.audio.GetMicrophoneData;
import com.pedro.encoder.input.audio.MicrophoneManager;
Expand Down Expand Up @@ -142,18 +142,18 @@ public void setMicrophoneMode(MicrophoneMode microphoneMode) {
switch (microphoneMode) {
case SYNC:
microphoneManager = new MicrophoneManagerManual();
audioEncoder = new AudioEncoder(getAacData);
audioEncoder = new AudioEncoder(getAudioData);
audioEncoder.setGetFrame(((MicrophoneManagerManual) microphoneManager).getGetFrame());
audioEncoder.setTsModeBuffer(false);
break;
case ASYNC:
microphoneManager = new MicrophoneManager(getMicrophoneData);
audioEncoder = new AudioEncoder(getAacData);
audioEncoder = new AudioEncoder(getAudioData);
audioEncoder.setTsModeBuffer(false);
break;
case BUFFER:
microphoneManager = new MicrophoneManager(getMicrophoneData);
audioEncoder = new AudioEncoder(getAacData);
audioEncoder = new AudioEncoder(getAudioData);
audioEncoder.setTsModeBuffer(true);
break;
}
Expand Down Expand Up @@ -297,7 +297,7 @@ public boolean prepareVideo(int width, int height, int bitrate) {
return prepareVideo(width, height, 30, bitrate, 2, rotation);
}

protected abstract void prepareAudioRtp(boolean isStereo, int sampleRate);
protected abstract void onAudioInfoImp(boolean isStereo, int sampleRate);

/**
* Call this method before use @startStream. If not you will do a stream without audio.
Expand All @@ -316,7 +316,7 @@ public boolean prepareAudio(int audioSource, int bitrate, int sampleRate, boolea
if (!microphoneManager.createMicrophone(audioSource, sampleRate, isStereo, echoCanceler, noiseSuppressor)) {
return false;
}
prepareAudioRtp(isStereo, sampleRate);
onAudioInfoImp(isStereo, sampleRate);
audioInitialized = audioEncoder.prepareAudioEncoder(bitrate, sampleRate, isStereo,
microphoneManager.getMaxInputSize());
return audioInitialized;
Expand Down Expand Up @@ -655,7 +655,7 @@ public void startStreamAndRecord(String url, String path) throws IOException {
startStreamAndRecord(url, path, null);
}

protected abstract void startStreamRtp(String url);
protected abstract void startStreamImp(String url);

/**
* Need be called after @prepareVideo or/and @prepareAudio. This method override resolution of
Expand All @@ -674,7 +674,7 @@ public void startStream(String url) {
} else {
requestKeyFrame();
}
startStreamRtp(url);
startStreamImp(url);
onPreview = true;
}

Expand Down Expand Up @@ -731,15 +731,15 @@ private void prepareGlView(int width, int height, int rotation) {
}
}

protected abstract void stopStreamRtp();
protected abstract void stopStreamImp();

/**
* Stop stream started with @startStream.
*/
public void stopStream() {
if (streaming) {
streaming = false;
stopStreamRtp();
stopStreamImp();
}
if (!recordController.isRecording()) {
if (audioInitialized) microphoneManager.stop();
Expand Down Expand Up @@ -939,11 +939,11 @@ public RecordController.Status getRecordStatus() {
return recordController.getStatus();
}

protected abstract void getAacDataRtp(ByteBuffer aacBuffer, MediaCodec.BufferInfo info);
protected abstract void getAudioDataImp(ByteBuffer audioBuffer, MediaCodec.BufferInfo info);

protected abstract void onSpsPpsVpsRtp(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps);
protected abstract void onVideoInfoImp(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps);

protected abstract void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info);
protected abstract void getVideoDataImp(ByteBuffer videoBuffer, MediaCodec.BufferInfo info);

public void setRecordController(BaseRecordController recordController) {
if (!isRecording()) this.recordController = recordController;
Expand All @@ -957,13 +957,13 @@ public void setRecordController(BaseRecordController recordController) {
audioEncoder.inputPCMData(frame);
};

private final GetAacData getAacData = new GetAacData() {
private final GetAudioData getAudioData = new GetAudioData() {
@Override
public void getAacData(@NonNull ByteBuffer aacBuffer, @NonNull MediaCodec.BufferInfo info) {
public void getAudioData(@NonNull ByteBuffer audioBuffer, @NonNull MediaCodec.BufferInfo info) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
recordController.recordAudio(aacBuffer, info);
recordController.recordAudio(audioBuffer, info);
}
if (streaming) getAacDataRtp(aacBuffer, info);
if (streaming) getAudioDataImp(audioBuffer, info);
}

@Override
Expand All @@ -974,17 +974,17 @@ public void onAudioFormat(@NonNull MediaFormat mediaFormat) {

private final GetVideoData getVideoData = new GetVideoData() {
@Override
public void onSpsPpsVps(@NonNull ByteBuffer sps, @Nullable ByteBuffer pps, @Nullable ByteBuffer vps) {
onSpsPpsVpsRtp(sps.duplicate(), pps != null ? pps.duplicate(): null, vps != null ? vps.duplicate() : null);
public void onVideoInfo(@NonNull ByteBuffer sps, @Nullable ByteBuffer pps, @Nullable ByteBuffer vps) {
onVideoInfoImp(sps.duplicate(), pps != null ? pps.duplicate(): null, vps != null ? vps.duplicate() : null);
}

@Override
public void getVideoData(@NonNull ByteBuffer h264Buffer, @NonNull MediaCodec.BufferInfo info) {
public void getVideoData(@NonNull ByteBuffer videoBuffer, @NonNull MediaCodec.BufferInfo info) {
fpsListener.calculateFps();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
recordController.recordVideo(h264Buffer, info);
recordController.recordVideo(videoBuffer, info);
}
if (streaming) getH264DataRtp(h264Buffer, info);
if (streaming) getVideoDataImp(videoBuffer, info);
}

@Override
Expand Down
44 changes: 22 additions & 22 deletions library/src/main/java/com/pedro/library/base/Camera2Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import com.pedro.common.VideoCodec;
import com.pedro.encoder.EncoderErrorCallback;
import com.pedro.encoder.audio.AudioEncoder;
import com.pedro.encoder.audio.GetAacData;
import com.pedro.encoder.audio.GetAudioData;
import com.pedro.encoder.input.audio.CustomAudioEffect;
import com.pedro.encoder.input.audio.GetMicrophoneData;
import com.pedro.encoder.input.audio.MicrophoneManager;
Expand Down Expand Up @@ -157,18 +157,18 @@ public void setMicrophoneMode(MicrophoneMode microphoneMode) {
switch (microphoneMode) {
case SYNC:
microphoneManager = new MicrophoneManagerManual();
audioEncoder = new AudioEncoder(getAacData);
audioEncoder = new AudioEncoder(getAudioData);
audioEncoder.setGetFrame(((MicrophoneManagerManual) microphoneManager).getGetFrame());
audioEncoder.setTsModeBuffer(false);
break;
case ASYNC:
microphoneManager = new MicrophoneManager(getMicrophoneData);
audioEncoder = new AudioEncoder(getAacData);
audioEncoder = new AudioEncoder(getAudioData);
audioEncoder.setTsModeBuffer(false);
break;
case BUFFER:
microphoneManager = new MicrophoneManager(getMicrophoneData);
audioEncoder = new AudioEncoder(getAacData);
audioEncoder = new AudioEncoder(getAudioData);
audioEncoder.setTsModeBuffer(true);
break;
}
Expand Down Expand Up @@ -338,7 +338,7 @@ public boolean prepareVideo(int width, int height, int bitrate) {
return prepareVideo(width, height, 30, bitrate, 2, rotation);
}

protected abstract void prepareAudioRtp(boolean isStereo, int sampleRate);
protected abstract void onAudioInfoImp(boolean isStereo, int sampleRate);

/**
* Call this method before use @startStream. If not you will do a stream without audio.
Expand All @@ -357,7 +357,7 @@ public boolean prepareAudio(int audioSource, int bitrate, int sampleRate, boolea
if (!microphoneManager.createMicrophone(audioSource, sampleRate, isStereo, echoCanceler, noiseSuppressor)) {
return false;
}
prepareAudioRtp(isStereo, sampleRate);
onAudioInfoImp(isStereo, sampleRate);
audioInitialized = audioEncoder.prepareAudioEncoder(bitrate, sampleRate, isStereo,
microphoneManager.getMaxInputSize());
return audioInitialized;
Expand Down Expand Up @@ -610,7 +610,7 @@ public void startStreamAndRecord(String url, String path) throws IOException {
startStreamAndRecord(url, path, null);
}

protected abstract void startStreamRtp(String url);
protected abstract void startStreamImp(String url);

/**
* Need be called after @prepareVideo or/and @prepareAudio. This method override resolution of
Expand All @@ -629,7 +629,7 @@ public void startStream(String url) {
} else {
requestKeyFrame();
}
startStreamRtp(url);
startStreamImp(url);
onPreview = true;
}

Expand Down Expand Up @@ -674,15 +674,15 @@ private void prepareGlView(int width, int height, int rotation) {
}
}

protected abstract void stopStreamRtp();
protected abstract void stopStreamImp();

/**
* Stop stream started with @startStream.
*/
public void stopStream() {
if (streaming) {
streaming = false;
stopStreamRtp();
stopStreamImp();
}
if (!recordController.isRecording()) {
onPreview = !isBackground;
Expand Down Expand Up @@ -1007,11 +1007,11 @@ public boolean isOnPreview() {
return onPreview;
}

protected abstract void getAacDataRtp(ByteBuffer aacBuffer, MediaCodec.BufferInfo info);
protected abstract void getAudioDataImp(ByteBuffer audioBuffer, MediaCodec.BufferInfo info);

protected abstract void onSpsPpsVpsRtp(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps);
protected abstract void onVideoInfoImp(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps);

protected abstract void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info);
protected abstract void getVideoDataImp(ByteBuffer videoBuffer, MediaCodec.BufferInfo info);

public void setRecordController(BaseRecordController recordController) {
if (!isRecording()) this.recordController = recordController;
Expand All @@ -1021,11 +1021,11 @@ public void setRecordController(BaseRecordController recordController) {
audioEncoder.inputPCMData(frame);
};

private final GetAacData getAacData = new GetAacData() {
private final GetAudioData getAudioData = new GetAudioData() {
@Override
public void getAacData(@NonNull ByteBuffer aacBuffer, @NonNull MediaCodec.BufferInfo info) {
recordController.recordAudio(aacBuffer, info);
if (streaming) getAacDataRtp(aacBuffer, info);
public void getAudioData(@NonNull ByteBuffer audioBuffer, @NonNull MediaCodec.BufferInfo info) {
recordController.recordAudio(audioBuffer, info);
if (streaming) getAudioDataImp(audioBuffer, info);
}

@Override
Expand All @@ -1036,15 +1036,15 @@ public void onAudioFormat(@NonNull MediaFormat mediaFormat) {

private final GetVideoData getVideoData = new GetVideoData() {
@Override
public void onSpsPpsVps(@NonNull ByteBuffer sps, @Nullable ByteBuffer pps, @Nullable ByteBuffer vps) {
onSpsPpsVpsRtp(sps.duplicate(), pps != null ? pps.duplicate(): null, vps != null ? vps.duplicate() : null);
public void onVideoInfo(@NonNull ByteBuffer sps, @Nullable ByteBuffer pps, @Nullable ByteBuffer vps) {
onVideoInfoImp(sps.duplicate(), pps != null ? pps.duplicate(): null, vps != null ? vps.duplicate() : null);
}

@Override
public void getVideoData(@NonNull ByteBuffer h264Buffer, @NonNull MediaCodec.BufferInfo info) {
public void getVideoData(@NonNull ByteBuffer videoBuffer, @NonNull MediaCodec.BufferInfo info) {
fpsListener.calculateFps();
recordController.recordVideo(h264Buffer, info);
if (streaming) getH264DataRtp(h264Buffer, info);
recordController.recordVideo(videoBuffer, info);
if (streaming) getVideoDataImp(videoBuffer, info);
}

@Override
Expand Down
Loading

0 comments on commit e399f58

Please sign in to comment.