Class FLVReader

java.lang.Object
org.red5.io.flv.impl.FLVReader
All Implemented Interfaces:
IKeyFrameDataAnalyzer, IoConstants, ITagReader

public class FLVReader extends Object implements IoConstants, ITagReader, IKeyFrameDataAnalyzer
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)
  • Field Details

    • log

      private static org.slf4j.Logger log
    • file

      private File file
      File
    • fis

      private FileInputStream fis
      File input stream
    • channel

      private FileChannel channel
      File channel
    • channelSize

      private long channelSize
    • keyframeMeta

      private IKeyFrameDataAnalyzer.KeyFrameMeta keyframeMeta
      Keyframe metadata
    • in

      private org.apache.mina.core.buffer.IoBuffer in
      Input byte buffer
    • generateMetadata

      private boolean generateMetadata
      Set to true to generate metadata automatically before the first tag.
    • firstVideoTag

      private long firstVideoTag
      Position of first video tag.
    • firstAudioTag

      private long firstAudioTag
      Position of first audio tag.
    • audioConfigRead

      private AtomicBoolean audioConfigRead
      If audio configuration data has been read
    • videoConfigRead

      private AtomicBoolean videoConfigRead
      If video configuration data has been read
    • metadataSent

      private boolean metadataSent
      metadata sent flag
    • duration

      private long duration
      Duration in milliseconds.
    • posTimeMap

      private HashMap<Long,Long> posTimeMap
      Mapping between file position and timestamp in ms.
    • bufferType

      private static BufferType bufferType
      Buffer type / style to use
    • bufferSize

      private static int bufferSize
    • useLoadBuf

      private boolean useLoadBuf
      Use load buffer
    • keyframeCache

      private static IKeyFrameMetaCache keyframeCache
      Cache for keyframe informations.
    • lock

      private final ReentrantLock lock
  • Constructor Details

    • FLVReader

      FLVReader()
      Constructs a new FLVReader.
    • FLVReader

      public FLVReader(File f) throws IOException
      Creates FLV reader from file input stream.
      Parameters:
      f - File
      Throws:
      IOException - on error
    • FLVReader

      public FLVReader(File f, boolean generateMetadata) throws IOException
      Creates FLV reader from file input stream, sets up metadata generation flag.
      Parameters:
      f - File input stream
      generateMetadata - true if metadata generation required, false otherwise
      Throws:
      IOException - on error
    • FLVReader

      public FLVReader(FileChannel channel) throws IOException
      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 otherwise
      buffer - IoBuffer
  • Method Details

    • setKeyFrameCache

      public void setKeyFrameCache(IKeyFrameMetaCache keyframeCache)
    • 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 interface ITagReader
      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 bufferSize
      reload - 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 interface ITagReader
      Returns:
      has video
    • getBufferType

      public static String getBufferType()
      Getter for buffer type (auto, direct or heap).
      Returns:
      Value for property 'bufferType'
    • setBufferType

      public static void setBufferType(String bufferType)
      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 interface ITagReader
    • getFile

      public IStreamableFile getFile()
      Return the file that is loaded.
      Specified by:
      getFile in interface ITagReader
      Returns:
      the file to be loaded
    • getOffset

      public int getOffset()
      Returns the offet length
      Specified by:
      getOffset in interface ITagReader
      Returns:
      int
    • getBytesRead

      public long getBytesRead()
      Returns the amount of bytes read
      Specified by:
      getBytesRead in interface ITagReader
      Returns:
      long
    • getDuration

      public long getDuration()
      Return length in seconds
      Specified by:
      getDuration in interface ITagReader
      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 interface ITagReader
      Returns:
      boolean
    • createFileMeta

      private ITag createFileMeta()
      Create tag for metadata event.
      Returns:
      Metadata event tag
    • readTag

      public ITag readTag()
      Returns a Tag object
      Specified by:
      readTag in interface ITagReader
      Returns:
      Tag
    • close

      public void close()
      Closes the reader and free any allocated memory.
      Specified by:
      close in interface ITagReader
    • analyzeKeyFrames

      public IKeyFrameDataAnalyzer.KeyFrameMeta analyzeKeyFrames()
      Key frames analysis may be used as a utility method so synchronize it.
      Specified by:
      analyzeKeyFrames in interface IKeyFrameDataAnalyzer
      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 interface ITagReader
      Parameters:
      pos - New position in file. Pass Long.MAX_VALUE to seek to end of file.
    • readTagHeader

      private ITag readTagHeader() throws FLVReader.UnsupportedDataTypeException
      Read only header part of a tag.
      Returns:
      Tag header
      Throws:
      FLVReader.UnsupportedDataTypeException
    • getDuration

      public static int getDuration(File flvFile)
      Returns the last tag's timestamp as the files duration.
      Parameters:
      flvFile - FLV file
      Returns:
      duration