TSDuck Version 3.32-2769 (TSDuck - The MPEG Transport Stream Toolkit)
ts::TSFile Class Reference

Transport stream file, input and/or output. More...

Inheritance diagram for ts::TSFile:
Collaboration diagram for ts::TSFile:

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

 TSFile ()
 Default constructor.
 
 TSFile (const TSFile &other)
 Copy constructor. More...
 
 TSFile (TSFile &&other) noexcept
 Move constructor. More...
 
virtual ~TSFile () override
 Destructor.
 
void abort ()
 Abort any currenly read/write operation in progress. More...
 
bool close (Report &report)
 Close the file. More...
 
UString getDisplayFileName () const
 Get the file name as a display string. More...
 
UString getFileName () const
 Get the file name. More...
 
bool isOpen () const
 Check if the file is open. More...
 
virtual bool open (const UString &filename, OpenFlags flags, Report &report, TSPacketFormat format=TSPacketFormat::AUTODETECT)
 Open or create the file (generic form). More...
 
bool openRead (const UString &filename, size_t repeat_count, uint64_t start_offset, Report &report, TSPacketFormat format=TSPacketFormat::AUTODETECT)
 Open the file for read. 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...
 
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...
 
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 rewind (Report &report)
 Rewind the file. More...
 
bool seek (PacketCounter packet_index, Report &report)
 Seek the file at a specified packet index. More...
 
void setStuffing (size_t initial, size_t final)
 Set initial and final artificial stuffing. 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...
 

Protected Member Functions

void resetPacketStream (TSPacketFormat format, AbstractReadStreamInterface *reader, AbstractWriteStreamInterface *writer)
 Reset the stream format and counters. More...
 

Protected Attributes

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

Detailed Description

Transport stream file, input and/or output.

Member Enumeration Documentation

◆ OpenFlags

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

◆ TSFile() [1/2]

ts::TSFile::TSFile ( const TSFile other)

Copy constructor.

Only the configuration is copied (name, repetition, etc.)

Parameters
[in]otherOther instance to copy.

◆ TSFile() [2/2]

ts::TSFile::TSFile ( TSFile &&  other)
noexcept

Move constructor.

The full state is moved.

Parameters
[in,out]otherOther instance to move. Closed on return.

Member Function Documentation

◆ openRead() [1/2]

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

Open the file for read.

Parameters
[in]filenameFile name. If empty or "-", use standard input. Must be a regular file if start_offset is not zero. If repeat_count is not 1 and the file is not a regular one, the file is closed and reopened instead of being rewound.
[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.

◆ openRead() [2/2]

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

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 or "-", 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()

◆ open()

virtual bool ts::TSFile::open ( const UString filename,
OpenFlags  flags,
Report report,
TSPacketFormat  format = TSPacketFormat::AUTODETECT 
)
virtual

Open or create the file (generic form).

The file is rewindable if the underlying file is seekable, eg. not a pipe.

Parameters
[in]filenameFile name. If empty or "-", use standard input or output. If filename is empty, flags cannot contain both READ and WRITE.
[in]flagsBit mask of open flags.
[in,out]reportWhere to report errors.
[in]formatFormat of the TS file.
Returns
True on success, false on error.

Reimplemented in ts::TSFileOutputResync.

◆ isOpen()

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

Check if the file is open.

Returns
True if the file is open.

◆ getFileName()

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

Get the file name.

Returns
The file name.

◆ getDisplayFileName()

UString ts::TSFile::getDisplayFileName ( ) const

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)

Close the file.

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

◆ setStuffing()

void ts::TSFile::setStuffing ( size_t  initial,
size_t  final 
)

Set initial and final artificial stuffing.

This method shall be called before opening the file. It specifies a number of artificial null packets which are read or written before and after the actual content of the file.

Parameters
[in]initialNumber of artificial initial null packets. On read, the first initial read packets are null packets. The actual content of the physical file will be read afterward. On write, opening the file will immediately write initial null packets, before the application has a chance to explicitly write packets.
[in]finalNumber of artificial final null packets. On read, when the file is completed, after all specified repetitions, reading will successfully continue for the next final packets and returns null packets. On write, closing the file with automatically write final null packets before closing the physical file.

◆ abort()

void ts::TSFile::abort ( )

Abort any currenly read/write operation in progress.

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

◆ rewind()

bool ts::TSFile::rewind ( Report report)
inline

Rewind the file.

The file must have been opened in rewindable mode. If the file file was opened with a start_offset different from 0, rewinding the file means restarting at this start_offset.

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

◆ seek()

bool ts::TSFile::seek ( PacketCounter  packet_index,
Report report 
)

Seek the file at a specified packet index.

The file must have been opened in rewindable mode.

Parameters
[in]packet_indexSeek the file to this specified packet index (plus the specified start_offset from open()).
[in,out]reportWhere to report errors.
Returns
True on success, false on error.

◆ readPackets()

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

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.

◆ 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.

◆ readPacketsCount()

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

Get the number of read packets.

Returns
The number of read packets.

◆ 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.

Member Data Documentation

◆ MAX_HEADER_SIZE

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

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
staticconstexprinherited

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: