Class FragmentedMp4Writer

  • All Implemented Interfaces:
    Closeable, AutoCloseable, SampleSink

    public class FragmentedMp4Writer
    extends DefaultBoxes
    implements SampleSink
    Creates a fragmented MP4 file consisting of a header [ftyp, moov], any number of fragments [moof, mdat]+ and a footer [mfra]. The MultiTrackFragmentedMp4Writer is a passive component. It will only be active if one of the source tracks pushes a sample via acceptSample(StreamingSample, StreamingTrack). It has to be closed (close()) actively to trigger the write of remaining buffered samples and the footer.
    • Method Detail

      • close

        public void close()
                   throws IOException
        Writes the remaining samples to file (even though the typical condition for wrapping up a segment have not yet been met) and writes the MovieFragmentRandomAccessBox. It does not close the sink!
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Specified by:
        close in interface SampleSink
        Throws:
        IOException - if writing to the underlying data sink fails
        See Also:
        MovieFragmentRandomAccessBox
      • createMvex

        protected Box createMvex()
      • createMoov

        protected Box createMoov()
      • createHeader

        protected Box[] createHeader()
      • acceptSample

        public void acceptSample​(StreamingSample streamingSample,
                                 StreamingTrack streamingTrack)
                          throws IOException
        Description copied from interface: SampleSink
        Adds a samples to the SampleSink. This might or might not cause writing the sample any output stream or channel. Once this method is called the StreamingTrack must be ready and accept calls to any method.
        Specified by:
        acceptSample in interface SampleSink
        Throws:
        IOException - if writing (or reading) fails.
      • isFragmentReady

        protected boolean isFragmentReady​(StreamingTrack streamingTrack,
                                          StreamingSample next)
        Tests if the currently received samples for a given track form a valid fragment taking the latest received sample into account. The next sample is not part of the segment and will be added to the fragment buffer later.
        Parameters:
        streamingTrack - track to test
        next - the lastest samples
        Returns:
        true if a fragment has been created.
      • writeHeader

        protected void writeHeader​(Box... boxes)
                            throws IOException
        Writes the given boxes. It's called as soon as the MultiTrackFragmentedMp4Writer received a sample from each source as this is the first point in time where the MultiTrackFragmentedMp4Writer can be sure that all config data is available from the sources. It typically writes a ftyp/moov pair but will write what ever the boxes argument contains
        Parameters:
        boxes - any number of boxes that form the header
        Throws:
        IOException - when writing to the sink fails.
        See Also:
        FileTypeBox, ProgressiveDownloadInformationBox, MovieBox, SegmentIndexBox
      • writeFragment

        protected void writeFragment​(Box... boxes)
                              throws IOException
        Writes the given boxes. It's called as soon as a fragment is created. It typically write a single moof/mdat pair but will write what ever the boxes argument contains
        Parameters:
        boxes - any number of boxes that form fragment
        Throws:
        IOException - when writing to the sink fails.
        See Also:
        MovieFragmentBox, MediaDataBox, SegmentTypeBox, SegmentIndexBox
      • writeFooter

        protected void writeFooter​(Box... boxes)
                            throws IOException
        Writes the given boxes. It's called as last write operation. Typically the only box written is the MovieFragmentRandomAccessBox.
        Parameters:
        boxes - any number of boxes to conclude the file.
        Throws:
        IOException - when writing to the sink fails.
        See Also:
        MovieFragmentRandomAccessBox
      • createFooter

        protected Box[] createFooter()
      • createTfra

        protected Box createTfra​(StreamingTrack track)
        Creates a 'tfra' - track fragment random access box for the given track with the isoFile. The tfra contains a map of random access points with time as key and offset within the isofile as value.
        Parameters:
        track - the concerned track
        Returns:
        a track fragment random access box.