Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions encoder/src/main/java/com/pedro/encoder/video/VideoEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
public class VideoEncoder extends BaseEncoder implements GetCameraData {

private final GetVideoData getVideoData;
private boolean spsPpsSetted = false;
private volatile boolean spsPpsSetted = false;
private boolean forceKey = false;
//video data necessary to send after requestKeyframe.
private ByteBuffer oldSps, oldPps, oldVps;
Expand Down Expand Up @@ -255,7 +255,7 @@ public void setVideoBitrateOnFly(int bitrate) {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void requestKeyframe() {
if (isRunning()) {
if (spsPpsSetted) {
if (spsPpsSetted && oldSps != null) {
Bundle bundle = new Bundle();
bundle.putInt(MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME, 0);
try {
Expand All @@ -266,6 +266,7 @@ public void requestKeyframe() {
}
} else {
//You need wait until encoder generate first frame.
spsPpsSetted = false;
forceKey = true;
}
}
Expand Down Expand Up @@ -324,6 +325,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) {
oldSps = bufferInfo;
getVideoData.onVideoInfo(bufferInfo, null, null);
return true;
}
Expand All @@ -340,11 +342,15 @@ private boolean sendSPSandPPS(MediaFormat mediaFormat) {
}
//H264
} else {
oldSps = mediaFormat.getByteBuffer("csd-0");
oldPps = mediaFormat.getByteBuffer("csd-1");
oldVps = null;
getVideoData.onVideoInfo(oldSps, oldPps, oldVps);
return true;
ByteBuffer sps = mediaFormat.getByteBuffer("csd-0");
ByteBuffer pps = mediaFormat.getByteBuffer("csd-1");
if (sps != null && pps != null) {
oldSps = sps;
oldPps = pps;
oldVps = null;
getVideoData.onVideoInfo(oldSps, oldPps, oldVps);
return true;
}
}
return false;
}
Expand Down Expand Up @@ -520,8 +526,7 @@ public void formatChanged(@NonNull MediaCodec mediaCodec, @NonNull MediaFormat m
}

@Override
protected void checkBuffer(@NonNull ByteBuffer byteBuffer,
@NonNull MediaCodec.BufferInfo bufferInfo) {
protected void checkBuffer(@NonNull ByteBuffer byteBuffer, @NonNull MediaCodec.BufferInfo bufferInfo) {
if (forceKey && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
forceKey = false;
requestKeyframe();
Expand Down Expand Up @@ -557,6 +562,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) {
oldSps = obuSequence;
getVideoData.onVideoInfo(obuSequence, null, null);
spsPpsSetted = true;
} else {
Expand Down