@@ -43,8 +43,6 @@ public class VideoEncoder {
4343 private long seq = 0L ;
4444 private int MAX_FRAMERATE_LENGTH = 20 ;
4545 private int MIN_FRAMERATE_LENGTH = 5 ;
46- // 缓存 SPS PPS 信息
47- ByteBuffer mSpsPpsBuffer = null ;
4846
4947 public VideoEncoder (VideoEncodeParam param ) {
5048 this .videoEncodeParam = param ;
@@ -183,40 +181,24 @@ public void encoderH264(byte[] data, boolean mirror) {
183181 outputBuffer .get (outData );
184182 boolean isKeyFrame = false ;
185183
186- if ((bufferInfo .flags & MediaCodec .BUFFER_FLAG_CODEC_CONFIG ) != 0 ) {
187- // Log.w(TAG, "encoderH264: sps pps info, outData: " + UtilsKt.toHexString(outData));
188-
189- if (mSpsPpsBuffer != null ) {
190- mSpsPpsBuffer .clear ();
191- mSpsPpsBuffer = null ;
192- }
193-
194- // 缓存 SPS/PPS
195- mSpsPpsBuffer = ByteBuffer .allocate (outData .length );
196- mSpsPpsBuffer .put (outData );
197- mSpsPpsBuffer .flip ();
198- } else if ((bufferInfo .flags & MediaCodec .BUFFER_FLAG_KEY_FRAME ) != 0 ) {
199- // Log.w(TAG, "encoderH264: I frame, outData: " + UtilsKt.toHexString(outData));
184+ if ((bufferInfo .flags & MediaCodec .BUFFER_FLAG_KEY_FRAME ) != 0 ) {
200185 isKeyFrame = true ;
201-
202- if (mSpsPpsBuffer != null ) {
203- byte [] spsPpsData = new byte [mSpsPpsBuffer .remaining ()];
204- mSpsPpsBuffer .get (spsPpsData );
205- mSpsPpsBuffer .rewind ();
206- Log .w (TAG , "encoderH264: I frame, spsPpsData: " + UtilsKt .toHexString (spsPpsData ));
207-
208- if (spsPpsData .length > 0 ) {
209- notifyEncoded (spsPpsData , isKeyFrame );
210- } else {
211- Log .w (TAG , "encoderH264: no sps pps data" );
212- }
213- } else {
214- Log .w (TAG , "encoderH264: I frame spsPpsBuffer = null" );
215- }
186+ ByteBuffer spsb = mediaCodec .getOutputFormat ().getByteBuffer ("csd-0" );
187+ byte [] sps = new byte [spsb .remaining ()];
188+ spsb .get (sps , 0 , sps .length );
189+ ByteBuffer ppsb = mediaCodec .getOutputFormat ().getByteBuffer ("csd-1" );
190+ byte [] pps = new byte [ppsb .remaining ()];
191+ ppsb .get (pps , 0 , pps .length );
192+ byte [] dataBytes = new byte [sps .length + pps .length + outData .length ];
193+ System .arraycopy (sps , 0 , dataBytes , 0 , sps .length );
194+ System .arraycopy (pps , 0 , dataBytes , sps .length , pps .length );
195+ System .arraycopy (outData , 0 , dataBytes , pps .length + sps .length , outData .length );
196+ notifyEncoded (dataBytes , isKeyFrame );
197+ } else {
198+ // 打印编码后的数据大小
199+ notifyEncoded (outData , isKeyFrame );
216200 }
217201
218- // 打印编码后的数据大小
219- notifyEncoded (outData , isKeyFrame );
220202 // 释放输出缓冲区
221203 mediaCodec .releaseOutputBuffer (outputBufferIndex , false );
222204 outputBufferIndex = mediaCodec .dequeueOutputBuffer (bufferInfo , 0 );
@@ -255,11 +237,6 @@ public void stop() {
255237 mediaCodec = null ;
256238 }
257239
258- if (mSpsPpsBuffer != null ) {
259- mSpsPpsBuffer .clear ();
260- mSpsPpsBuffer = null ;
261- }
262-
263240 executor .shutdown ();
264241 } catch (Exception e ) {
265242 e .printStackTrace ();
0 commit comments