public synchronized void serialize(ByteBuffer sampleBuffer, MediaCodec.BufferInfo sampleBufferInfo) { if ((sampleBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { Log.w("H264Serializer", "[serialize] NOOP - EOS!"); return; // NOOP -> No action need, codec closing and there's so buffer to process } ByteBuffer mediaBuffer = sampleBuffer.slice(); if (mediaBuffer.position() != 0) { throw new AssertionError("Expected new buffer position to be zero!"); } // Read NAL-U header and verify header conformity // NOTE: This implementation assumes encoded media slice data contains NAL-U magic boundary in the beginning only ONCE mediaBuffer.get(naluHeader, 0, H264Defs.NAL_HEADER_SIZE); if (mediaBuffer.position() != H264Defs.NAL_HEADER_SIZE) { throw new AssertionError("Unable to read buffer with the given constraint"); } if (!ProtocolUtils.startsWithNaluMagicHeader(naluHeader)) { throw new AssertionError("NAL units are not preceeded by 0x00000001"); } refCountSlice++; naluByte = naluHeader[H264Defs.NAL_BYTE_POSITION] & 0xFF; nalType = (naluByte & H264Defs.NAL_TYPE_MASK) & 0xFF; // Discard non-IDR frames before a treatment of IDR if ((nalType == H264Defs.NAL_UNIT_TYPE_NON_IDR) && !sentIFrame) { Log.w("H264Serializer", "[serialize] *** Discarding non-IDR coded picture before IDR processing!"); return; } dumpDebugInfo(mediaBuffer); BinaryFileDump.dumpMediaSample(mediaBuffer, mediaBuffer.limit(), sampleBufferInfo.presentationTimeUs); if (mediaBuffer.position() != H264Defs.NAL_HEADER_SIZE) { mediaBuffer.rewind(); mediaBuffer.position(H264Defs.NAL_HEADER_SIZE); } serializeInternal(sampleBufferInfo, mediaBuffer); }
public synchronized void serialize(ByteBuffer sampleBuffer, MediaCodec.BufferInfo sampleBufferInfo) { if ((sampleBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { Log.w("H264Serializer", "[serialize] NOOP - EOS!"); return; // NOOP -> No action need, codec closing and there's so buffer to process } ByteBuffer mediaBuffer = sampleBuffer.slice(); if (mediaBuffer.position() != 0) { throw new AssertionError("Expected new buffer position to be zero!"); } // Read NAL-U header and verify header conformity // NOTE: This implementation assumes encoded media slice data contains NAL-U magic boundary in the beginning only ONCE mediaBuffer.get(naluHeader, 0, H264Defs.NAL_HEADER_SIZE); if (mediaBuffer.position() != H264Defs.NAL_HEADER_SIZE) { throw new AssertionError("Unable to read buffer with the given constraint"); } if (!ProtocolUtils.startsWithNaluMagicHeader(naluHeader)) { throw new AssertionError("NAL units are not preceeded by 0x00000001"); } refCountSlice++; naluByte = naluHeader[H264Defs.NAL_BYTE_POSITION] & 0xFF; nalType = (naluByte & H264Defs.NAL_TYPE_MASK) & 0xFF; // Discard non-IDR frames before a treatment of IDR if ((nalType == H264Defs.NAL_UNIT_TYPE_NON_IDR) && !sentIFrame) { Log.w("H264Serializer", "[serialize] *** Discarding non-IDR coded picture before IDR processing!"); return; } dumpDebugInfo(mediaBuffer); BinaryFileDump.dumpMediaSample(mediaBuffer, mediaBuffer.limit(), sampleBufferInfo.presentationTimeUs); if (mediaBuffer.position() != H264Defs.NAL_HEADER_SIZE) { mediaBuffer.rewind(); mediaBuffer.position(H264Defs.NAL_HEADER_SIZE); } serializeInternal(sampleBufferInfo, mediaBuffer); }