5353public class VideoEncoder extends BaseEncoder implements GetCameraData {
5454
5555 private final GetVideoData getVideoData ;
56- private boolean spsPpsSetted = false ;
56+ private volatile boolean spsPpsSetted = false ;
5757 private boolean forceKey = false ;
5858 //video data necessary to send after requestKeyframe.
5959 private ByteBuffer oldSps , oldPps , oldVps ;
@@ -255,7 +255,7 @@ public void setVideoBitrateOnFly(int bitrate) {
255255 @ RequiresApi (api = Build .VERSION_CODES .KITKAT )
256256 public void requestKeyframe () {
257257 if (isRunning ()) {
258- if (spsPpsSetted ) {
258+ if (spsPpsSetted && oldSps != null ) {
259259 Bundle bundle = new Bundle ();
260260 bundle .putInt (MediaCodec .PARAMETER_KEY_REQUEST_SYNC_FRAME , 0 );
261261 try {
@@ -266,6 +266,7 @@ public void requestKeyframe() {
266266 }
267267 } else {
268268 //You need wait until encoder generate first frame.
269+ spsPpsSetted = false ;
269270 forceKey = true ;
270271 }
271272 }
@@ -324,6 +325,7 @@ private boolean sendSPSandPPS(MediaFormat mediaFormat) {
324325 ByteBuffer bufferInfo = mediaFormat .getByteBuffer ("csd-0" );
325326 //we need an av1ConfigurationRecord with sequenceObu to work
326327 if (bufferInfo != null && bufferInfo .remaining () > 4 ) {
328+ oldSps = bufferInfo ;
327329 getVideoData .onVideoInfo (bufferInfo , null , null );
328330 return true ;
329331 }
@@ -340,11 +342,15 @@ private boolean sendSPSandPPS(MediaFormat mediaFormat) {
340342 }
341343 //H264
342344 } else {
343- oldSps = mediaFormat .getByteBuffer ("csd-0" );
344- oldPps = mediaFormat .getByteBuffer ("csd-1" );
345- oldVps = null ;
346- getVideoData .onVideoInfo (oldSps , oldPps , oldVps );
347- return true ;
345+ ByteBuffer sps = mediaFormat .getByteBuffer ("csd-0" );
346+ ByteBuffer pps = mediaFormat .getByteBuffer ("csd-1" );
347+ if (sps != null && pps != null ) {
348+ oldSps = sps ;
349+ oldPps = pps ;
350+ oldVps = null ;
351+ getVideoData .onVideoInfo (oldSps , oldPps , oldVps );
352+ return true ;
353+ }
348354 }
349355 return false ;
350356 }
@@ -520,8 +526,7 @@ public void formatChanged(@NonNull MediaCodec mediaCodec, @NonNull MediaFormat m
520526 }
521527
522528 @ Override
523- protected void checkBuffer (@ NonNull ByteBuffer byteBuffer ,
524- @ NonNull MediaCodec .BufferInfo bufferInfo ) {
529+ protected void checkBuffer (@ NonNull ByteBuffer byteBuffer , @ NonNull MediaCodec .BufferInfo bufferInfo ) {
525530 if (forceKey && Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
526531 forceKey = false ;
527532 requestKeyframe ();
@@ -557,6 +562,7 @@ protected void checkBuffer(@NonNull ByteBuffer byteBuffer,
557562 Log .i (TAG , "formatChanged not called, doing manual av1 extraction..." );
558563 ByteBuffer obuSequence = extractObuSequence (byteBuffer .duplicate (), bufferInfo );
559564 if (obuSequence != null ) {
565+ oldSps = obuSequence ;
560566 getVideoData .onVideoInfo (obuSequence , null , null );
561567 spsPpsSetted = true ;
562568 } else {
0 commit comments