Class FragmentedMp4Writer
- java.lang.Object
-
- org.mp4parser.streaming.output.mp4.DefaultBoxes
-
- org.mp4parser.streaming.output.mp4.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 viaacceptSample(StreamingSample, StreamingTrack). It has to be closed (close()) actively to trigger the write of remaining buffered samples and the footer.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classFragmentedMp4Writer.FragmentContainer
-
Field Summary
Fields Modifier and Type Field Description protected Map<StreamingTrack,CountDownLatch>congestionControlprotected DatecreationTimeprotected Map<StreamingTrack,Queue<FragmentedMp4Writer.FragmentContainer>>fragmentBuffersBuffers segements until it's time for a segment to be written.protected Map<StreamingTrack,Long>nextFragmentCreateStartTimeContains the start time of the next segment in line that will be created.protected Map<StreamingTrack,Long>nextFragmentWriteStartTimeContains the start time of the next segment in line that will be written.protected Map<StreamingTrack,Long>nextSampleStartTimeContains the next sample's start time.static ObjectOBJprotected Map<StreamingTrack,List<StreamingSample>>sampleBuffersBuffers the samples per track until there are enough samples to form a Segment.protected longsequenceNumberprotected WritableByteChannelsinkprotected List<StreamingTrack>sourceprotected Map<StreamingTrack,long[]>tfraOffsetsprotected Map<StreamingTrack,long[]>tfraTimes
-
Constructor Summary
Constructors Constructor Description FragmentedMp4Writer(List<StreamingTrack> source, WritableByteChannel sink)
-
Method Summary
Modifier and Type Method Description voidacceptSample(StreamingSample streamingSample, StreamingTrack streamingTrack)Adds a samples to the SampleSink.voidclose()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.protected Box[]createFooter()protected Box[]createFragment(StreamingTrack streamingTrack, List<StreamingSample> samples)protected Box[]createHeader()protected BoxcreateMdhd(StreamingTrack streamingTrack)protected BoxcreateMoov()protected BoxcreateMvex()protected BoxcreateMvhd()protected voidcreateTfdt(StreamingTrack streamingTrack, TrackFragmentBox parent)protected voidcreateTfhd(StreamingTrack streamingTrack, TrackFragmentBox parent)protected BoxcreateTfra(StreamingTrack track)Creates a 'tfra' - track fragment random access box for the given track with the isoFile.protected BoxcreateTrex(StreamingTrack streamingTrack)protected voidcreateTrun(StreamingTrack streamingTrack, TrackFragmentBox parent, List<StreamingSample> samples)protected booleanisFragmentReady(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.protected voidwrite(WritableByteChannel out, Box... boxes)protected voidwriteFooter(Box... boxes)Writes the given boxes.protected voidwriteFragment(Box... boxes)Writes the given boxes.protected voidwriteHeader(Box... boxes)Writes the given boxes.-
Methods inherited from class org.mp4parser.streaming.output.mp4.DefaultBoxes
createDinf, createFtyp, createMdia, createMdiaHdlr, createMinf, createStbl, createTkhd, createTrak
-
-
-
-
Field Detail
-
OBJ
public static final Object OBJ
-
sink
protected final WritableByteChannel sink
-
source
protected List<StreamingTrack> source
-
creationTime
protected Date creationTime
-
sequenceNumber
protected long sequenceNumber
-
congestionControl
protected Map<StreamingTrack,CountDownLatch> congestionControl
-
nextFragmentCreateStartTime
protected Map<StreamingTrack,Long> nextFragmentCreateStartTime
Contains the start time of the next segment in line that will be created.
-
nextFragmentWriteStartTime
protected Map<StreamingTrack,Long> nextFragmentWriteStartTime
Contains the start time of the next segment in line that will be written.
-
nextSampleStartTime
protected Map<StreamingTrack,Long> nextSampleStartTime
Contains the next sample's start time.
-
sampleBuffers
protected Map<StreamingTrack,List<StreamingSample>> sampleBuffers
Buffers the samples per track until there are enough samples to form a Segment.
-
fragmentBuffers
protected Map<StreamingTrack,Queue<FragmentedMp4Writer.FragmentContainer>> fragmentBuffers
Buffers segements until it's time for a segment to be written.
-
tfraOffsets
protected Map<StreamingTrack,long[]> tfraOffsets
-
tfraTimes
protected Map<StreamingTrack,long[]> tfraTimes
-
-
Constructor Detail
-
FragmentedMp4Writer
public FragmentedMp4Writer(List<StreamingTrack> source, WritableByteChannel sink) throws IOException
- Throws:
IOException
-
-
Method Detail
-
close
public void close() throws IOExceptionWrites 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceSampleSink- Throws:
IOException- if writing to the underlying data sink fails- See Also:
MovieFragmentRandomAccessBox
-
write
protected void write(WritableByteChannel out, Box... boxes) throws IOException
- Throws:
IOException
-
createMdhd
protected Box createMdhd(StreamingTrack streamingTrack)
- Specified by:
createMdhdin classDefaultBoxes
-
createMvex
protected Box createMvex()
-
createTrex
protected Box createTrex(StreamingTrack streamingTrack)
-
createMvhd
protected Box createMvhd()
- Specified by:
createMvhdin classDefaultBoxes
-
createMoov
protected Box createMoov()
-
createHeader
protected Box[] createHeader()
-
acceptSample
public void acceptSample(StreamingSample streamingSample, StreamingTrack streamingTrack) throws IOException
Description copied from interface:SampleSinkAdds a samples to the SampleSink. This might or might not cause writing the sample any output stream or channel. Once this method is called theStreamingTrackmust be ready and accept calls to any method.- Specified by:
acceptSamplein interfaceSampleSink- 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 testnext- the lastest samples- Returns:
- true if a fragment has been created.
-
createFragment
protected Box[] createFragment(StreamingTrack streamingTrack, List<StreamingSample> samples)
-
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
-
createTfhd
protected void createTfhd(StreamingTrack streamingTrack, TrackFragmentBox parent)
-
createTfdt
protected void createTfdt(StreamingTrack streamingTrack, TrackFragmentBox parent)
-
createTrun
protected void createTrun(StreamingTrack streamingTrack, TrackFragmentBox parent, List<StreamingSample> samples)
-
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.
-
-