Package org.red5.io.flv.impl
Class FLVReader
java.lang.Object
org.red5.io.flv.impl.FLVReader
- All Implemented Interfaces:
IKeyFrameDataAnalyzer
,IoConstants
,ITagReader
A Reader is used to read the contents of a FLV file. NOTE: This class is not implemented as threading-safe. The caller should make sure
the threading-safety.
- Author:
- The Red5 Project, Dominick Accattato (daccattato@gmail.com), Luke Hubbard, Codegent Ltd (luke@codegent.com), Paul Gregoire, (mondain@gmail.com)
-
Nested Class Summary
Modifier and TypeClassDescription(package private) final class
Used when an unsupported datatype is found in a file.Nested classes/interfaces inherited from interface org.red5.io.flv.IKeyFrameDataAnalyzer
IKeyFrameDataAnalyzer.KeyFrameMeta
-
Field Summary
Modifier and TypeFieldDescriptionprivate AtomicBoolean
If audio configuration data has been readprivate static int
private static BufferType
Buffer type / style to useprivate FileChannel
File channelprivate long
private long
Duration in milliseconds.private File
Fileprivate long
Position of first audio tag.private long
Position of first video tag.private FileInputStream
File input streamprivate boolean
Set to true to generate metadata automatically before the first tag.private FLVHeader
The header of this FLV file.private org.apache.mina.core.buffer.IoBuffer
Input byte bufferprivate static IKeyFrameMetaCache
Cache for keyframe informations.Keyframe metadataprivate final ReentrantLock
private static org.slf4j.Logger
private boolean
metadata sent flagMapping between file position and timestamp in ms.private boolean
Use load bufferprivate AtomicBoolean
If video configuration data has been readFields inherited from interface org.red5.io.IoConstants
AVC_PACKET_TYPE_END_OF_SEQUENCE, AVC_PACKET_TYPE_NALU, AVC_PACKET_TYPE_SEQUENCE_HEADER, FLAG_CODEC_H263, FLAG_CODEC_SCREEN, FLAG_CODEC_VP6, FLAG_FORMAT_ADPCM, FLAG_FORMAT_MP3, FLAG_FORMAT_NELLYMOSER, FLAG_FORMAT_NELLYMOSER_8_KHZ, FLAG_FORMAT_RAW, FLAG_FRAMETYPE_DISPOSABLE, FLAG_FRAMETYPE_GENERATED_KEYFRAME, FLAG_FRAMETYPE_INFO, FLAG_FRAMETYPE_INTERFRAME, FLAG_FRAMETYPE_KEYFRAME, FLAG_RATE_11_KHZ, FLAG_RATE_22_KHZ, FLAG_RATE_44_KHZ, FLAG_RATE_48_KHZ, FLAG_RATE_5_5_KHZ, FLAG_SIZE_16_BIT, FLAG_SIZE_8_BIT, FLAG_TYPE_MONO, FLAG_TYPE_STEREO, INFO_PACKET_SEEK_END, INFO_PACKET_SEEK_START, MASK_SOUND_FORMAT, MASK_SOUND_RATE, MASK_SOUND_SIZE, MASK_SOUND_TYPE, MASK_VIDEO_CODEC, MASK_VIDEO_FRAMETYPE, TYPE_AUDIO, TYPE_ENCRYPTED, TYPE_ENCRYPTED_AUDIO, TYPE_ENCRYPTED_METADATA, TYPE_ENCRYPTED_VIDEO, TYPE_METADATA, TYPE_VIDEO
-
Constructor Summary
ConstructorDescriptionConstructs a new FLVReader.Creates FLV reader from file input stream.Creates FLV reader from file input stream, sets up metadata generation flag.FLVReader
(FileChannel channel) Creates FLV reader from file channel.FLVReader
(org.apache.mina.core.buffer.IoBuffer buffer, boolean generateMetadata) Accepts mapped file bytes to construct internal members. -
Method Summary
Modifier and TypeMethodDescriptionKey frames analysis may be used as a utility method so synchronize it.void
close()
Closes the reader and free any allocated memory.private ITag
Create tag for metadata event.void
Decode the header of the stream;private void
Loads whole buffer from file channel, with no reloading (that is, appending).private void
fillBuffer
(long amount) Loads data from channel to buffer.private void
fillBuffer
(long amount, boolean reload) Load enough bytes from channel to buffer.int
static int
Getter for buffer size.static String
Getter for buffer type (auto, direct or heap).long
Returns the amount of bytes readprivate long
Get the current position in a file or ByteBuffer.long
Return length in secondsstatic int
getDuration
(File flvFile) Returns the last tag's timestamp as the files duration.getFile()
Return the file that is loaded.org.apache.mina.core.buffer.IoBuffer
Returns the file buffer.int
Returns the offet lengthprivate long
Get the remaining bytes that could be read from a file or ByteBuffer.long
Get the total readable bytes in a file or ByteBuffer.int
boolean
Returns a boolean stating whether the FLV has more tagsboolean
hasVideo()
Check if the reader also has video tags.void
position
(long pos) Put the current position to pos.private void
Post-initialization hook, reads keyframe metadata and decodes header (if any).readTag()
Returns a Tag objectprivate ITag
Read only header part of a tag.static void
setBufferSize
(int bufferSize) Setter for property 'bufferSize'.static void
setBufferType
(String bufferType) Setter for buffer type.private void
setCurrentPosition
(long pos) Modifies current position.void
setKeyFrameCache
(IKeyFrameMetaCache keyframeCache)
-
Field Details
-
log
private static org.slf4j.Logger log -
file
File -
fis
File input stream -
channel
File channel -
channelSize
private long channelSize -
keyframeMeta
Keyframe metadata -
in
private org.apache.mina.core.buffer.IoBuffer inInput byte buffer -
generateMetadata
private boolean generateMetadataSet to true to generate metadata automatically before the first tag. -
firstVideoTag
private long firstVideoTagPosition of first video tag. -
firstAudioTag
private long firstAudioTagPosition of first audio tag. -
audioConfigRead
If audio configuration data has been read -
videoConfigRead
If video configuration data has been read -
metadataSent
private boolean metadataSentmetadata sent flag -
duration
private long durationDuration in milliseconds. -
posTimeMap
Mapping between file position and timestamp in ms. -
bufferType
Buffer type / style to use -
bufferSize
private static int bufferSize -
useLoadBuf
private boolean useLoadBufUse load buffer -
keyframeCache
Cache for keyframe informations. -
header
The header of this FLV file. -
lock
-
-
Constructor Details
-
FLVReader
FLVReader()Constructs a new FLVReader. -
FLVReader
Creates FLV reader from file input stream.- Parameters:
f
- File- Throws:
IOException
- on error
-
FLVReader
Creates FLV reader from file input stream, sets up metadata generation flag.- Parameters:
f
- File input streamgenerateMetadata
-true
if metadata generation required,false
otherwise- Throws:
IOException
- on error
-
FLVReader
Creates FLV reader from file channel.- Parameters:
channel
- file channel- Throws:
IOException
- on error
-
FLVReader
public FLVReader(org.apache.mina.core.buffer.IoBuffer buffer, boolean generateMetadata) Accepts mapped file bytes to construct internal members.- Parameters:
generateMetadata
-true
if metadata generation required,false
otherwisebuffer
- IoBuffer
-
-
Method Details
-
setKeyFrameCache
-
getRemainingBytes
private long getRemainingBytes()Get the remaining bytes that could be read from a file or ByteBuffer.- Returns:
- Number of remaining bytes
-
getTotalBytes
public long getTotalBytes()Get the total readable bytes in a file or ByteBuffer.- Specified by:
getTotalBytes
in interfaceITagReader
- Returns:
- Total readable bytes
-
getCurrentPosition
private long getCurrentPosition()Get the current position in a file or ByteBuffer.- Returns:
- Current position in a file
-
setCurrentPosition
private void setCurrentPosition(long pos) Modifies current position.- Parameters:
pos
- Current position in file
-
fillBuffer
private void fillBuffer()Loads whole buffer from file channel, with no reloading (that is, appending). -
fillBuffer
private void fillBuffer(long amount) Loads data from channel to buffer.- Parameters:
amount
- Amount of data to load with no reloading
-
fillBuffer
private void fillBuffer(long amount, boolean reload) Load enough bytes from channel to buffer. After the loading process, the caller can make sure the amount in buffer is of size 'amount' if we haven't reached the end of channel.- Parameters:
amount
- The amount of bytes in buffer after returning, no larger than bufferSizereload
- Whether to reload or append
-
postInitialize
private void postInitialize()Post-initialization hook, reads keyframe metadata and decodes header (if any). -
hasVideo
public boolean hasVideo()Check if the reader also has video tags.- Specified by:
hasVideo
in interfaceITagReader
- Returns:
- has video
-
getBufferType
Getter for buffer type (auto, direct or heap).- Returns:
- Value for property 'bufferType'
-
setBufferType
Setter for buffer type.- Parameters:
bufferType
- Value to set for property 'bufferType'
-
getBufferSize
public static int getBufferSize()Getter for buffer size.- Returns:
- Value for property 'bufferSize'
-
setBufferSize
public static void setBufferSize(int bufferSize) Setter for property 'bufferSize'.- Parameters:
bufferSize
- Value to set for property 'bufferSize'
-
getFileData
public org.apache.mina.core.buffer.IoBuffer getFileData()Returns the file buffer.- Returns:
- File contents as byte buffer
-
decodeHeader
public void decodeHeader()Decode the header of the stream;- Specified by:
decodeHeader
in interfaceITagReader
-
getFile
Return the file that is loaded.- Specified by:
getFile
in interfaceITagReader
- Returns:
- the file to be loaded
-
getOffset
public int getOffset()Returns the offet length- Specified by:
getOffset
in interfaceITagReader
- Returns:
- int
-
getBytesRead
public long getBytesRead()Returns the amount of bytes read- Specified by:
getBytesRead
in interfaceITagReader
- Returns:
- long
-
getDuration
public long getDuration()Return length in seconds- Specified by:
getDuration
in interfaceITagReader
- Returns:
- length in seconds
-
getVideoCodecId
public int getVideoCodecId() -
getAudioCodecId
public int getAudioCodecId() -
hasMoreTags
public boolean hasMoreTags()Returns a boolean stating whether the FLV has more tags- Specified by:
hasMoreTags
in interfaceITagReader
- Returns:
- boolean
-
createFileMeta
Create tag for metadata event.- Returns:
- Metadata event tag
-
readTag
Returns a Tag object- Specified by:
readTag
in interfaceITagReader
- Returns:
- Tag
-
close
public void close()Closes the reader and free any allocated memory.- Specified by:
close
in interfaceITagReader
-
analyzeKeyFrames
Key frames analysis may be used as a utility method so synchronize it.- Specified by:
analyzeKeyFrames
in interfaceIKeyFrameDataAnalyzer
- Returns:
- Keyframe metadata
-
position
public void position(long pos) Put the current position to pos. The caller must ensure the pos is a valid one (eg. not sit in the middle of a frame).- Specified by:
position
in interfaceITagReader
- Parameters:
pos
- New position in file. PassLong.MAX_VALUE
to seek to end of file.
-
readTagHeader
Read only header part of a tag.- Returns:
- Tag header
- Throws:
FLVReader.UnsupportedDataTypeException
-
getDuration
Returns the last tag's timestamp as the files duration.- Parameters:
flvFile
- FLV file- Returns:
- duration
-