Class PacketBuffer

java.lang.Object
org.codice.alliance.video.stream.mpegts.netty.PacketBuffer

public class PacketBuffer extends Object
Buffers raw MPEG-TS packet data and writes the data to a temporary file so that the data written is on a clean IDR boundary. If an IDR boundary cannot be found, the data will be eventually flush on a arbitrary point to avoid memory exhaustion. This implementation is thread-safe.

NOTE: This implementation could probably be improved by using some kind of circular buffer with read and write pointers

  • Field Details

    • ACTIVITY_TIMEOUT

      public static final long ACTIVITY_TIMEOUT
      After this number of milliseconds of no activity, the current frameset in memory will be considered complete.
  • Constructor Details

    • PacketBuffer

      public PacketBuffer()
  • Method Details

    • setTempFileGenerator

      public void setTempFileGenerator(TempFileGenerator tempFileGenerator)
      Parameters:
      tempFileGenerator - must be non-null
    • setOutputStreamFactory

      public void setOutputStreamFactory(OutputStreamFactory outputStreamFactory)
      Parameters:
      outputStreamFactory - must be non-null
    • setMaxIncompleteFrameBytes

      public void setMaxIncompleteFrameBytes(long maxIncompleteFrameBytes)
      Parameters:
      maxIncompleteFrameBytes - must be non-null
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • reset

      public void reset()
      Clear all stored data and reset to the initial state.
    • getAge

      public long getAge()
      Get the age (milliseconds) of the temporary data file. Returns 0 if there is no file.
      Returns:
      age in milliseconds
    • getByteCount

      public long getByteCount()
      Get the number of bytes that have been written to the temporary data file.
      Returns:
      bytes
    • write

      public void write(byte[] rawPacket)
      Write raw data into the buffer. Empty or null values are handled. If the size of the incomplete frame data exceeds maxIncompleteFrameBytes, then the current incomplete frame data will be push to the frame list as type PacketBuffer.FrameType.UNKNOWN and a flush to disk will be attempted.
      Parameters:
      rawPacket - may be null or empty
    • frameComplete

      public void frameComplete(PacketBuffer.FrameType frameType)
      Tell the packet buffer that the recently written data represents a complete frame. A flush to disk will be attempted.
      Parameters:
      frameType - must be non-null
    • rotate

      public RotateResult rotate(RolloverCondition rolloverCondition)
      If the rollover condition is not met, then the method will return Optional.empty(). If the rollover condition is met, then the method may return a temp file. The only reason a temp file may not be returned is when the condition is true even when no data has been written to file. The caller is responsible for deleting the temp file.
      Parameters:
      rolloverCondition - the rollover condition
      Returns:
      an optional temp file
    • flushAndRotate

      public RotateResult flushAndRotate() throws IOException
      Flush all buffered data to disk and rotate. Will return Optional.empty() if no data has been written to file. The caller is responsible for deleting the temp file.
      Returns:
      an optional temp file
      Throws:
      IOException
    • cancelTimer

      public void cancelTimer()
    • getLastSegmentStart

      public long getLastSegmentStart()
    • getLastSegmentEnd

      public long getLastSegmentEnd()