Buggy code:
} class SrsRawH264Stream { public boolean is_sps ( SrsMp4Muxer . SrsEsFrameBytes frame ) { } public boolean is_pps ( SrsMp4Muxer . SrsEsFrameBytes frame ) { } public SrsMp4Muxer . SrsAnnexbSearch srs_avc_startswith_annexb ( ByteBuffer bb , MediaCodec . BufferInfo bi ) { } public SrsMp4Muxer . SrsEsFrameBytes annexb_demux ( ByteBuffer bb , MediaCodec . BufferInfo bi ) throws IllegalArgumentException { } } public class Sample { private long offset = 0 ; private long size = 0 ; public Sample ( long offset , long size ) { } public long getOffset ( ) { } public long getSize ( ) { } } public class Track { private int trackId = 0 ; private ArrayList < SrsMp4Muxer . Sample > samples = new ArrayList < > ( ) ; private long duration = 0 ; private String handler ; private AbstractMediaHeaderBox headerBox = null ; private SampleDescriptionBox sampleDescriptionBox = null ; private LinkedList < Integer > syncSamples = null ; private int timeScale ; private Date creationTime = new Date ( ) ; private int height ; private int width ; private float volume = 0 ; private ArrayList < Long > sampleDurations = new ArrayList < > ( ) ; private boolean isAudio = false ; private long lastPresentationTimeUs = 0 ; private boolean first = true ; public Track ( int id , MediaFormat format , boolean audio ) { } public void addSample ( long offset , MediaCodec . BufferInfo bufferInfo ) { } public ArrayList < SrsMp4Muxer . Sample > getSamples ( ) { } public long getDuration ( ) { } public String getHandler ( ) { } public AbstractMediaHeaderBox getMediaHeaderBox ( ) { } public SampleDescriptionBox getSampleDescriptionBox ( ) { } public long [ ] getSyncSamples ( ) { } public int getTimeScale ( ) { } public Date getCreationTime ( ) { } public int getWidth ( ) { } public int getHeight ( ) { } public float getVolume ( ) { } public ArrayList < Long > getSampleDurations ( ) { } public boolean isAudio ( ) { } public int getTrackId ( ) { } } public class Mp4Movie { private Matrix matrix = Matrix . ROTATE_0 ; private HashMap < Integer , SrsMp4Muxer . Track > tracks = new HashMap < > ( ) ; public Matrix getMatrix ( ) { } public HashMap < Integer , SrsMp4Muxer . Track > getTracks ( ) { } public void addSample ( int trackIndex , long offset , MediaCodec . BufferInfo bufferInfo ) { } public void addTrack ( MediaFormat format , boolean isAudio ) { } } private SrsMp4Muxer . InterleaveChunkMdat mdat = null ; private FileOutputStream fos = null ; private FileChannel fc = null ; private long dataOffset = 0 ; private HashMap < SrsMp4Muxer . Track , long [ ] > track2SampleSizes = new HashMap < > ( ) ; private void createMovie ( File outputFile ) throws IOException { } private void flushCurrentMdat ( ) throws IOException { } private void writeSampleData ( int trackIndex , ByteBuffer byteBuf , MediaCodec . BufferInfo bufferInfo , boolean isAudio ) throws IOException { if ( mdat . first ) { mdat . setContentSize ( 0 ) ; mdat . getBox ( fc ) ; mdat . setDataOffset ( dataOffset ) ; dataOffset += 16 ; mdat . first = false ; } mdat . setContentSize ( ( ( mdat . getContentSize ( ) ) + ( bufferInfo . size ) ) ) ; mp4Movie . addSample ( trackIndex , dataOffset , bufferInfo ) ; byteBuf . position ( ( ( bufferInfo . offset ) + ( isAudio ? 0 : 4 ) ) ) ; byteBuf . limit ( ( ( bufferInfo . offset ) + ( bufferInfo . size ) ) ) ; if ( ! isAudio ) { ByteBuffer size = ByteBuffer . allocateDirect ( 4 ) ; size . position ( 0 ) ; <START_BUG> size . putInt ( bufferInfo . size ) ; <END_BUG> size . position ( 0 ) ; fc . write ( size ) ; } dataOffset += bufferInfo . size ; fc . write ( byteBuf ) ; fos . flush ( ) ; } private void finishMovie ( ) throws IOException { } private FileTypeBox createFileTypeBox ( ) { } private class InterleaveChunkMdat implements Box { private boolean first = true ; private ContainerBox parent ; private ByteBuffer header ; private long contentSize = ( 1024 * 1024 ) * 1024 ; private long dataOffset = 0 ; public ContainerBox getParent ( ) { } public long getOffset ( ) { } public void setDataOffset ( long offset ) { } public void setParent ( ContainerBox parent ) { } public void setContentSize ( long contentSize ) { } public long getContentSize ( ) { } public String getType ( ) { } public long getSize ( ) { } private boolean isSmallBox ( long contentSize ) { } public void getBox ( WritableByteChannel writableByteChannel ) throws IOException { } @ Override public void parse ( ReadableByteChannel readableByteChannel , ByteBuffer header , long contentSize , BoxParser boxParser ) throws IOException { } } private static long gcd ( long a , long b ) { } private long getTimescale ( SrsMp4Muxer . Mp4Movie mp4Movie ) { } private MovieBox createMovieBox ( SrsMp4Muxer . Mp4Movie movie ) { } private TrackBox createTrackBox ( SrsMp4Muxer . Track track , SrsMp4Muxer . Mp4Movie movie ) { } private Box createStbl ( SrsMp4Muxer . Track track ) { } private void createStsd ( SrsMp4Muxer . Track track , SampleTableBox stbl ) { } private void createStts ( SrsMp4Muxer . Track track , SampleTableBox stbl ) { } private void createStss ( SrsMp4Muxer . Track track , SampleTableBox stbl ) { } private void createStsc ( SrsMp4Muxer . Track track , SampleTableBox stbl ) { } private void createStsz ( SrsMp4Muxer . Track track , SampleTableBox stbl ) { }

Fix:
size . putInt ( ( ( bufferInfo . size ) - 4 ) ) ;