Package org.red5.io.flv.impl
Class FLVWriter
java.lang.Object
org.red5.io.flv.impl.FLVWriter
- All Implemented Interfaces:
ITagWriter
A Writer is used to write the contents of a FLV file
- Author:
- The Red5 Project, Dominick Accattato (daccattato@gmail.com), Luke Hubbard, Codegent Ltd (luke@codegent.com), Tiago Jacobs (tiago@imdt.com.br), Paul Gregoire (mondain@gmail.com)
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionprivate boolean
Are we appending to an existing file?private long
private int
Id of the audio codec used.private AtomicBoolean
If audio configuration data has been writtenprivate int
Size of audio dataprivate AtomicLong
Number of bytes writtenprivate SeekableByteChannel
Destination to which stream data is stored without an flv header.private static final byte[]
For now all recorded streams carry a stream id of 0.private int
Duration of the file.private ExecutorService
Executor for tasks within this instanceprivate SeekableByteChannel
Flv output destination.private String
private AtomicBoolean
private static IFLV
FLV objectprivate static final int
Length of the flv header in bytesprivate int
private final Semaphore
private static org.slf4j.Logger
private int
Position in fileprivate LinkedList<IPostProcessor>
private String
private int
Sampling rateprivate int
Size of each audio sampleprivate boolean
Mono (0) or stereo (1) soundprivate static final int
Length of the flv tag in bytesprivate int
Position in fileprivate int
Id of the video codec used.private AtomicBoolean
If video configuration data has been writtenprivate int
Size of video data -
Constructor Summary
ConstructorDescriptionCreates writer implementation with for a given fileCreates writer implementation with given file and flag indicating whether or not to append.Creates writer implementation with for a given fileCreates writer implementation with given file and flag indicating whether or not to append. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addPostProcessor
(IPostProcessor postProcessor) Adds a post-process for execution once the instance completes.void
close()
Ends the writing process, then merges the data file with the flv file header and metadata.private void
Create the stream data file.private void
Create the stream output file; the flv itself.private void
Create the stream data file for repair.private long
Finalizes the FLV file.long
Return the bytes writtengetFile()
Return the file that is written.getMetaData
(Path path, int maxTags) int
Return the offsetstatic void
Exposed to allow repair of flv files if .info and .ser files still exist.private static int[]
readInfoFile
(File tmpFile) Read flv file information from pre-finalization file.static boolean
Allows repair of flv files if .info and .ser files still exist.void
setAudioCodecId
(int audioCodecId) void
setAudioDataSize
(int audioDataSize) void
setDuration
(int duration) static void
Setter for FLV objectvoid
setOffset
(int offset) Setter for offsetvoid
setSoundRate
(int soundRate) void
setSoundSize
(int soundSize) void
setSoundType
(boolean soundType) void
setVideoCodecId
(int videoCodecId) void
setVideoDataSize
(int videoDataSize) private Future<?>
submit
(FLVWriter.FLVFinalizer flvFinalizer) Submits a finalizer internally.private void
Write or update flv file information into the pre-finalization file.void
Writes the header bytesprivate void
writeMetadataTag
(double duration, int videoCodecId, int audioCodecId) Write "onMetaData" tag to the file.boolean
writeStream
(byte[] b) Write a Stream to disk using bytesboolean
writeTag
(byte dataType, org.apache.mina.core.buffer.IoBuffer data) Write a Tag using bytesboolean
Writes a Tag object
-
Field Details
-
log
private static org.slf4j.Logger log -
HEADER_LENGTH
private static final int HEADER_LENGTHLength of the flv header in bytes- See Also:
-
TAG_HEADER_LENGTH
private static final int TAG_HEADER_LENGTHLength of the flv tag in bytes- See Also:
-
DEFAULT_STREAM_ID
private static final byte[] DEFAULT_STREAM_IDFor now all recorded streams carry a stream id of 0. -
executor
Executor for tasks within this instance -
flv
FLV object -
bytesWritten
Number of bytes written -
offset
private int offsetPosition in file -
timeOffset
private int timeOffsetPosition in file -
audioCodecId
private volatile int audioCodecIdId of the audio codec used. -
videoCodecId
private volatile int videoCodecIdId of the video codec used. -
audioConfigWritten
If audio configuration data has been written -
videoConfigWritten
If video configuration data has been written -
soundRate
private volatile int soundRateSampling rate -
soundSize
private volatile int soundSizeSize of each audio sample -
soundType
private volatile boolean soundTypeMono (0) or stereo (1) sound -
append
private boolean appendAre we appending to an existing file? -
duration
private int durationDuration of the file. -
videoDataSize
private int videoDataSizeSize of video data -
audioDataSize
private int audioDataSizeSize of audio data -
fileChannel
Flv output destination. -
dataChannel
Destination to which stream data is stored without an flv header. -
filePath
-
lock
-
lastTagSize
private volatile int lastTagSize -
postProcessors
-
finalized
-
recordedDate
-
meta
-
appendOffset
private long appendOffset
-
-
Constructor Details
-
FLVWriter
Creates writer implementation with for a given file- Parameters:
filePath
- path to existing file
-
FLVWriter
Creates writer implementation with for a given file- Parameters:
repair
- repair the .ser filefilePath
- path to existing file
-
FLVWriter
Creates writer implementation with given file and flag indicating whether or not to append. FLV.java uses this constructor so we have access to the file object- Parameters:
file
- File output streamappend
- true if append to existing file
-
FLVWriter
Creates writer implementation with given file and flag indicating whether or not to append. FLV.java uses this constructor so we have access to the file object- Parameters:
path
- File output pathappend
- true if append to existing file
-
-
Method Details
-
getMetaData
- Throws:
IOException
-
writeHeader
Writes the header bytes- Specified by:
writeHeader
in interfaceITagWriter
- Throws:
IOException
- Any I/O exception
-
writeTag
Writes a Tag object- Specified by:
writeTag
in interfaceITagWriter
- Parameters:
tag
- Tag to write- Returns:
true
on success,false
otherwise- Throws:
IOException
- I/O exception
-
writeTag
public boolean writeTag(byte dataType, org.apache.mina.core.buffer.IoBuffer data) throws IOException Write a Tag using bytes- Specified by:
writeTag
in interfaceITagWriter
- Parameters:
dataType
- Tag typedata
- Byte data- Returns:
true
on success,false
otherwise- Throws:
IOException
- I/O exception
-
writeStream
public boolean writeStream(byte[] b) Write a Stream to disk using bytes- Specified by:
writeStream
in interfaceITagWriter
- Parameters:
b
- Array of bytes to write- Returns:
true
on success,false
otherwise
-
createOutputFile
Create the stream output file; the flv itself.- Throws:
IOException
-
createDataFile
Create the stream data file.- Throws:
IOException
-
createRepairDataFile
Create the stream data file for repair.- Throws:
IOException
-
writeMetadataTag
private void writeMetadataTag(double duration, int videoCodecId, int audioCodecId) throws IOException, InterruptedException, ExecutionException Write "onMetaData" tag to the file.- Parameters:
duration
- Duration to write in milliseconds.videoCodecId
- Id of the video codec used while recording.audioCodecId
- Id of the audio codec used while recording.- Throws:
IOException
- if the tag could not be writtenExecutionException
InterruptedException
-
finalizeFlv
private long finalizeFlv()Finalizes the FLV file.- Returns:
- bytes transferred
-
readInfoFile
Read flv file information from pre-finalization file.- Parameters:
tmpFile
-- Returns:
- array containing audio codec id, video codec id, and duration
-
updateInfoFile
private void updateInfoFile()Write or update flv file information into the pre-finalization file. -
close
public void close()Ends the writing process, then merges the data file with the flv file header and metadata.- Specified by:
close
in interfaceITagWriter
-
addPostProcessor
Adds a post-process for execution once the instance completes.- Specified by:
addPostProcessor
in interfaceITagWriter
- Parameters:
postProcessor
- an implementation instance of IPostProcessor
-
getFile
Return the file that is written.- Specified by:
getFile
in interfaceITagWriter
- Returns:
- the File to be written
-
setFLV
Setter for FLV object- Parameters:
flv
- FLV source
-
getOffset
public int getOffset()Return the offset- Specified by:
getOffset
in interfaceITagWriter
- Returns:
- Offset value
-
setOffset
public void setOffset(int offset) Setter for offset- Parameters:
offset
- Value to set for offset
-
getBytesWritten
public long getBytesWritten()Return the bytes written- Specified by:
getBytesWritten
in interfaceITagWriter
- Returns:
- Number of bytes written
-
setVideoCodecId
public void setVideoCodecId(int videoCodecId) -
setAudioCodecId
public void setAudioCodecId(int audioCodecId) -
setSoundRate
public void setSoundRate(int soundRate) -
setSoundSize
public void setSoundSize(int soundSize) -
setSoundType
public void setSoundType(boolean soundType) -
setDuration
public void setDuration(int duration) -
setVideoDataSize
public void setVideoDataSize(int videoDataSize) -
setAudioDataSize
public void setAudioDataSize(int audioDataSize) -
repair
public static boolean repair(String path, Integer audioId, Integer videoId) throws InterruptedException Allows repair of flv files if .info and .ser files still exist.- Parameters:
path
- path to .ser fileaudioId
- audio codec idvideoId
- video codec id- Returns:
- true if conversion was successful
- Throws:
InterruptedException
- Exception on interruption
-
submit
Submits a finalizer internally.- Parameters:
flvFinalizer
-- Returns:
- Future representing task
-
main
Exposed to allow repair of flv files if .info and .ser files still exist.- Parameters:
args
- 0: path to .ser file 1: audio codec id 2: video codec id- Throws:
InterruptedException
- Exception on interruption
-