TSDuck Version 3.24-2000 (TSDuck - The MPEG Transport Stream Toolkit)
ts::TSFileInputBuffered Class Reference

Transport stream file input with a seekable buffer. More...

Inheritance diagram for ts::TSFileInputBuffered:
Collaboration diagram for ts::TSFileInputBuffered:

Public Types

enum  OpenFlags {
  NONE = 0x0000,
  READ = 0x0001,
  WRITE = 0x0002,
  APPEND = 0x0004,
  KEEP = 0x0008,
  SHARED = 0x0010,
  TEMPORARY = 0x0020,
  REOPEN = 0x0040,
  REOPEN_SPEC = 0x0080
}
 Flags for open(). More...
 

Public Member Functions

 TSFileInputBuffered (size_t buffer_size)
 Constructor. More...
 
virtual ~TSFileInputBuffered ()
 Destructor.
 
void abort ()
 Abort any currenly read/write operation in progress. More...
 
bool canSeek (PacketCounter position) const
 Check if we can seek to the specified absolute position. More...
 
bool close (Report &report)
 Close the file. More...
 
size_t getBackwardSeekableCount () const
 Get the backward seekable distance inside the buffer. More...
 
size_t getBufferedCount () const
 Get the number of TS packets in the buffer. More...
 
size_t getBufferFreeSize () const
 Get the size of the free space in the buffer. More...
 
size_t getBufferSize () const
 Get the buffer size. More...
 
UString getDisplayFileName () const
 Get the file name as a display string. More...
 
UString getFileName () const
 Get the file name. More...
 
size_t getForwardSeekableCount () const
 Get the forward seekable distance inside the buffer. More...
 
bool isOpen () const
 Check if the file is open. More...
 
bool openRead (const UString &filename, uint64_t start_offset, Report &report, TSPacketFormat format=TSPacketFormat::AUTODETECT)
 Open the file for read in rewindable mode. More...
 
bool openRead (const UString &filename, size_t repeat_count, uint64_t start_offset, Report &report, TSPacketFormat format=TSPacketFormat::AUTODETECT)
 Open the file. More...
 
TSPacketFormat packetFormat () const
 Get the file format. More...
 
UString packetFormatString () const
 Get the file format as a string. More...
 
size_t packetHeaderSize () const
 Get the packet header size, based on the packet format. More...
 
size_t packetTrailerSize () const
 Get the packet trailer size, based on the packet format. More...
 
size_t read (TSPacket *buffer, size_t max_packets, Report &report, TSPacketMetadata *metadata=nullptr)
 Read TS packets. More...
 
virtual size_t readPackets (TSPacket *buffer, TSPacketMetadata *metadata, size_t max_packets, Report &report) override
 Read TS packets from the stream. More...
 
PacketCounter readPacketsCount () const
 Get the number of read packets. More...
 
bool seek (PacketCounter position, Report &report)
 Seek to the specified absolute position, if it is inside the buffer. More...
 
bool seekBackward (size_t packet_count, Report &report)
 Seek the file backward the specified number of packets. More...
 
bool seekForward (size_t packet_count, Report &report)
 Seek the file forward the specified number of packets. More...
 
bool setBufferSize (size_t buffer_size, Report &report)
 Set the buffer size. More...
 
virtual bool writePackets (const TSPacket *buffer, const TSPacketMetadata *metadata, size_t packet_count, Report &report)
 Write TS packets to the stream. More...
 
PacketCounter writePacketsCount () const
 Get the number of written packets. More...
 

Static Public Attributes

static constexpr size_t MAX_HEADER_SIZE = ts::TSPacketMetadata::SERIALIZATION_SIZE
 Maximum size in bytes of a packet header for non-TS format. More...
 
static constexpr size_t MAX_TRAILER_SIZE = ts::RS_SIZE
 Maximum size in bytes of a packet trailer for non-TS format. More...
 
static const size_t MIN_BUFFER_SIZE = 16
 Minimum buffer size. More...
 

Protected Member Functions

virtual bool endOfStream () override
 Check if the end of stream was reached. More...
 
virtual bool readStreamChunks (void *addr, size_t max_size, size_t chunk_size, size_t &ret_size, Report &report)
 Read chunks of data from the stream. More...
 
virtual bool readStreamComplete (void *addr, size_t max_size, size_t &ret_size, Report &report)
 Read complete data from the stream. More...
 
virtual bool readStreamPartial (void *addr, size_t max_size, size_t &ret_size, Report &report) override
 Read partial data from the stream. More...
 
void resetPacketStream (TSPacketFormat format, AbstractReadStreamInterface *reader, AbstractWriteStreamInterface *writer)
 Reset the stream format and counters. More...
 
virtual bool writeStream (const void *addr, size_t size, size_t &written_size, Report &report) override
 Write data to the stream. More...
 

Protected Attributes

PacketCounter _total_read
 Total read packets.
 
PacketCounter _total_write
 Total written packets.
 

Detailed Description

Transport stream file input with a seekable buffer.

This variant of TSFile allows to seek back and forth to some extent without doing I/O's and can work on non-seekable files (pipes for instance).

Member Enumeration Documentation

◆ OpenFlags

enum ts::TSFile::OpenFlags
inherited

Flags for open().

Enumerator
NONE 

No option, do not open the file.

READ 

Read the file.

WRITE 

Write the file.

APPEND 

Append packets to an existing file.

KEEP 

Keep previous file with same name. Fail if it already exists.

SHARED 

Write open with shared read for other processes. Windows only. Always shared on Unix.

TEMPORARY 

Temporary file, deleted on close, not always visible in the file system.

REOPEN 

Close and reopen the file instead of rewind to start of file when looping on input file.

REOPEN_SPEC 

Force REOPEN when the file is not a regular file.

Constructor & Destructor Documentation

◆ TSFileInputBuffered()

ts::TSFileInputBuffered::TSFileInputBuffered ( size_t  buffer_size)

Constructor.

Parameters
[in]buffer_sizeSize of the seekable buffer in number of TS packets.

Member Function Documentation

◆ setBufferSize()

bool ts::TSFileInputBuffered::setBufferSize ( size_t  buffer_size,
Report report 
)

Set the buffer size.

Can be done only when the file is closed.

Parameters
[in]buffer_sizeSize of the seekable buffer in number of TS packets.
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

◆ getBufferSize()

size_t ts::TSFileInputBuffered::getBufferSize ( ) const
inline

Get the buffer size.

Returns
The buffer size in number of TS packets.

◆ getBufferFreeSize()

size_t ts::TSFileInputBuffered::getBufferFreeSize ( ) const
inline

Get the size of the free space in the buffer.

Returns
The number of free TS packets in the buffer.

◆ getBufferedCount()

size_t ts::TSFileInputBuffered::getBufferedCount ( ) const
inline

Get the number of TS packets in the buffer.

Returns
The number of TS packets in the buffer.

◆ openRead() [1/2]

bool ts::TSFileInputBuffered::openRead ( const UString filename,
size_t  repeat_count,
uint64_t  start_offset,
Report report,
TSPacketFormat  format = TSPacketFormat::AUTODETECT 
)

Open the file.

Override TSFile::openRead(). There is no rewindable version.

Parameters
[in]filenameFile name. If empty, use standard input. Must be a regular file is repeat_count is not 1 or if start_offset is not zero.
[in]repeat_countReading packets loops back after end of file until all repeat are done. If zero, infinitely repeat.
[in]start_offsetOffset in bytes from the beginning of the file where to start reading packets at each iteration.
[in,out]reportWhere to report errors.
[in]formatExpected format of the TS file.
Returns
True on success, false on error.

◆ read()

size_t ts::TSFileInputBuffered::read ( TSPacket buffer,
size_t  max_packets,
Report report,
TSPacketMetadata metadata = nullptr 
)

Read TS packets.

Override TSFile::read(). If the file file was opened with a repeat_count different from 1, reading packets transparently loops back at end if file.

Parameters
[out]bufferAddress of reception packet buffer.
[in]max_packetsSize of buffer in packets.
[in,out]reportWhere to report errors.
[in,out]metadataOptional packet metadata. If the file format provides time stamps, they are set in the metadata. Ignored if null pointer.
Returns
The actual number of read packets. Returning zero means error or end of file repetition.

◆ getBackwardSeekableCount()

size_t ts::TSFileInputBuffered::getBackwardSeekableCount ( ) const
inline

Get the backward seekable distance inside the buffer.

This is the minimum guaranteed seekable distance.

Returns
The buffer size from the highest previously read packet or the beginning of file, whichever comes first.

◆ getForwardSeekableCount()

size_t ts::TSFileInputBuffered::getForwardSeekableCount ( ) const
inline

Get the forward seekable distance inside the buffer.

This is the minimum guaranteed seekable distance.

Returns
The highest previously read packet index, before backward seek.

◆ seekBackward()

bool ts::TSFileInputBuffered::seekBackward ( size_t  packet_count,
Report report 
)

Seek the file backward the specified number of packets.

Parameters
[in]packet_countThe number of packets to seek backward from the current position.
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

◆ seekForward()

bool ts::TSFileInputBuffered::seekForward ( size_t  packet_count,
Report report 
)

Seek the file forward the specified number of packets.

Parameters
[in]packet_countThe number of packets to seek forward from the current position.
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

◆ readPacketsCount()

PacketCounter ts::TSFileInputBuffered::readPacketsCount ( ) const

Get the number of read packets.

Override TSFile::readPacketsCount().

Returns
The number of read packets.

◆ canSeek()

bool ts::TSFileInputBuffered::canSeek ( PacketCounter  position) const

Check if we can seek to the specified absolute position.

Parameters
[in]positionAbsolute packet index in the file.
Returns
True if the specified position is inside the buffer.

◆ seek()

bool ts::TSFileInputBuffered::seek ( PacketCounter  position,
Report report 
)

Seek to the specified absolute position, if it is inside the buffer.

Parameters
[in]positionAbsolute packet index in the file.
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

◆ openRead() [2/2]

bool ts::TSFile::openRead ( const UString filename,
uint64_t  start_offset,
Report report,
TSPacketFormat  format = TSPacketFormat::AUTODETECT 
)
inherited

Open the file for read in rewindable mode.

The file must be a rewindable file, eg. not a pipe. There is no repeat count, rewind must be done explicitly.

Parameters
[in]filenameFile name. If empty, use standard input.
[in]start_offsetOffset in bytes from the beginning of the file where to start reading packets.
[in,out]reportWhere to report errors.
[in]formatExpected format of the TS file.
Returns
True on success, false on error.
See also
rewind()
seek()

◆ isOpen()

bool ts::TSFile::isOpen ( ) const
inlineinherited

Check if the file is open.

Returns
True if the file is open.

◆ getFileName()

UString ts::TSFile::getFileName ( ) const
inlineinherited

Get the file name.

Returns
The file name.

◆ getDisplayFileName()

UString ts::TSFile::getDisplayFileName ( ) const
inherited

Get the file name as a display string.

Returns
The file name as a display string. Not always a valid file name. Use in error messages only.

◆ close()

bool ts::TSFile::close ( Report report)
inherited

Close the file.

Parameters
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

◆ abort()

void ts::TSFile::abort ( )
inherited

Abort any currenly read/write operation in progress.

The file is left in a broken state and can be only closed.

◆ readPackets()

virtual size_t ts::TSFile::readPackets ( TSPacket buffer,
TSPacketMetadata metadata,
size_t  max_packets,
Report report 
)
overridevirtualinherited

Read TS packets from the stream.

Parameters
[out]bufferAddress of reception packet buffer.
[out]metadataOptional packet metadata. If the file format provides time stamps, they are set in the metadata. Ignored if null pointer.
[in]max_packetsSize of buffer in packets. Also size of metadata in number of objects (when specified).
[in,out]reportWhere to report errors.
Returns
The actual number of read packets. Returning zero means error or end of stream.

Reimplemented from ts::TSPacketStream.

◆ endOfStream()

virtual bool ts::TSFile::endOfStream ( )
overrideprotectedvirtualinherited

Check if the end of stream was reached.

Returns
True on end of stream, false otherwise.

Implements ts::AbstractReadStreamInterface.

◆ readStreamPartial()

virtual bool ts::TSFile::readStreamPartial ( void *  addr,
size_t  max_size,
size_t &  ret_size,
Report report 
)
overrideprotectedvirtualinherited

Read partial data from the stream.

Wait and read at least one byte. Don't try to read exactly max_size bytes. If ret_size is less than max_bytes, it is possible to read more.

Parameters
[out]addrAddress of the buffer for the incoming data.
[in]max_sizeMaximum size in bytes of the buffer.
[out]ret_sizeReturned input size in bytes. If zero, end of file has been reached or an error occurred.
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

Implements ts::AbstractReadStreamInterface.

◆ writeStream()

virtual bool ts::TSFile::writeStream ( const void *  addr,
size_t  size,
size_t &  written_size,
Report report 
)
overrideprotectedvirtualinherited

Write data to the stream.

All bytes are written to the stream, blocking or retrying when necessary.

Parameters
[in]addrAddress of the data to write.
[in]sizeSize in bytes of the data to write.
[out]written_sizeActually written size in bytes. Can be less than size in case of error in the middle of the write.
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

Implements ts::AbstractWriteStreamInterface.

◆ writePackets()

virtual bool ts::TSPacketStream::writePackets ( const TSPacket buffer,
const TSPacketMetadata metadata,
size_t  packet_count,
Report report 
)
virtualinherited

Write TS packets to the stream.

Parameters
[in]bufferAddress of first packet to write.
[in]metadataOptional packet metadata containing time stamps. If the file format requires time stamps, metadata must not be a null pointer and all packets must have a time stamp. Otherwise, the last written timestamp is repeated.
[in]packet_countNumber of packets to write. Also size of metadata in number of objects (when specified).
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

◆ writePacketsCount()

PacketCounter ts::TSPacketStream::writePacketsCount ( ) const
inlineinherited

Get the number of written packets.

Returns
The number of written packets.

◆ packetHeaderSize()

size_t ts::TSPacketStream::packetHeaderSize ( ) const
inherited

Get the packet header size, based on the packet format.

This "header" comes before the classical 188-byte TS packet.

Returns
The packet header size in bytes (before the TS packet).

◆ packetTrailerSize()

size_t ts::TSPacketStream::packetTrailerSize ( ) const
inherited

Get the packet trailer size, based on the packet format.

This "trailer" comes after the classical 188-byte TS packet.

Returns
The packet trailer size in bytes (before the TS packet).

◆ packetFormat()

TSPacketFormat ts::TSPacketStream::packetFormat ( ) const
inlineinherited

Get the file format.

Returns
The file format.

◆ packetFormatString()

UString ts::TSPacketStream::packetFormatString ( ) const
inlineinherited

Get the file format as a string.

Returns
The file format as a string.

◆ resetPacketStream()

void ts::TSPacketStream::resetPacketStream ( TSPacketFormat  format,
AbstractReadStreamInterface reader,
AbstractWriteStreamInterface writer 
)
protectedinherited

Reset the stream format and counters.

Parameters
[in]formatInitial packet format.
[in]readerReader interface. If null, all read operations will fail.
[in]writerWriter interface. If null, all write operations will fail.

◆ readStreamComplete()

virtual bool ts::AbstractReadStreamInterface::readStreamComplete ( void *  addr,
size_t  max_size,
size_t &  ret_size,
Report report 
)
virtualinherited

Read complete data from the stream.

Wait and read exactly max_size bytes. If ret_size is less than max_bytes, it is not possible to read more. End of file has probably been reached.

Parameters
[out]addrAddress of the buffer for the incoming data.
[in]max_sizeMaximum size in bytes of the buffer.
[out]ret_sizeReturned input size in bytes.
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

◆ readStreamChunks()

virtual bool ts::AbstractReadStreamInterface::readStreamChunks ( void *  addr,
size_t  max_size,
size_t  chunk_size,
size_t &  ret_size,
Report report 
)
virtualinherited

Read chunks of data from the stream.

Parameters
[out]addrAddress of the buffer for the incoming data.
[in]max_sizeMaximum size in bytes of the buffer.
[in]chunk_sizeIf not zero, make sure that the input size is always a multiple of chunk_size. If the initial read ends in the middle of a chunk, read again and again, up to the end of the current chunk or end of file.
[out]ret_sizeReturned input size in bytes.
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

Member Data Documentation

◆ MIN_BUFFER_SIZE

const size_t ts::TSFileInputBuffered::MIN_BUFFER_SIZE = 16
static

Minimum buffer size.

Used to minimize buffer_size in constructor and setBufferSize().

◆ MAX_HEADER_SIZE

constexpr size_t ts::TSPacketStream::MAX_HEADER_SIZE = ts::TSPacketMetadata::SERIALIZATION_SIZE
staticinherited

Maximum size in bytes of a packet header for non-TS format.

Must be lower than the TS packet size to allow auto-detection on read.

◆ MAX_TRAILER_SIZE

constexpr size_t ts::TSPacketStream::MAX_TRAILER_SIZE = ts::RS_SIZE
staticinherited

Maximum size in bytes of a packet trailer for non-TS format.

Must be lower than the TS packet size to allow auto-detection on read.


The documentation for this class was generated from the following file: