TSDuck v3.40-3968
MPEG Transport Stream Toolkit
|
Transport stream packet queue for inter-thread communication. More...
#include <tsTSPacketQueue.h>
Public Member Functions | |
TSPacketQueue (size_t size=DEFAULT_SIZE) | |
Default constructor. | |
size_t | bufferSize () const |
Get the size of the buffer in packets. | |
size_t | currentSize () const |
Get the current number of packets in the buffer. | |
bool | eof () const |
Check if the writer thread has reported an end of file condition. | |
bool | getPacket (TSPacket &packet, TSPacketMetadata *mdata, BitRate &bitrate) |
Called by the reader thread to get the next packet without waiting. | |
bool | lockWriteBuffer (TSPacket *&buffer, TSPacketMetadata *&mdata, size_t &buffer_size, size_t min_size=1) |
Called by the writer thread to get a write buffer. | |
void | releaseWriteBuffer (size_t count) |
Called by the writer thread to release the write buffer. | |
void | reset (size_t size=NPOS) |
Reset and resize the buffer. | |
void | setBitrate (const BitRate &bitrate) |
Called by the writer thread to report the input bitrate. | |
void | setEOF () |
Called by the writer thread to report the end of input thread. | |
void | stop () |
Called by the reader thread to tell the writer thread to stop immediately. | |
bool | stopped () const |
Check if the reader thread has reported a stop condition. | |
bool | waitPackets (TSPacket *buffer, TSPacketMetadata *mdata, size_t buffer_count, size_t &actual_count, BitRate &bitrate) |
Called by the reader thread to wait for packets. | |
Static Public Attributes | |
static constexpr size_t | DEFAULT_SIZE = 1000 |
Default size in packets of the buffer. | |
Transport stream packet queue for inter-thread communication.
A writer thread produces packets. The input packets are directly written into the buffer. The writer thread invokes lockWriteBuffer() to get a write window inside the buffer. When packets have been written into this buffer, the writer thread calls releaseWriteBuffer().
A reader thread consumes packets. The packets are consumed one by one and copied out of the buffer using getPacket().
The input bitrate, if known, is transmitted to the reader thread. If the writer thread is aware of the exact bitrate, it calls setBitrate() and the specified value is returned to the reader thread. If the input bitrate is unknown, the buffer automatically computes it based on PCR's.
Termination conditions can be triggered on both sides.
ts::TSPacketQueue::TSPacketQueue | ( | size_t | size = DEFAULT_SIZE | ) |
Default constructor.
[in] | size | Size of the buffer in packets. |
void ts::TSPacketQueue::reset | ( | size_t | size = NPOS | ) |
Reset and resize the buffer.
It is illegal to reset the buffer while the writer thread has locked the buffer. This is not enforced by this class. It is the responsibility of the application to check this.
[in] | size | New size of the buffer in packets. By default, when set to NPOS, reset the queue without resizing the buffer. |
size_t ts::TSPacketQueue::bufferSize | ( | ) | const |
Get the size of the buffer in packets.
size_t ts::TSPacketQueue::currentSize | ( | ) | const |
Get the current number of packets in the buffer.
bool ts::TSPacketQueue::lockWriteBuffer | ( | TSPacket *& | buffer, |
TSPacketMetadata *& | mdata, | ||
size_t & | buffer_size, | ||
size_t | min_size = 1 |
||
) |
Called by the writer thread to get a write buffer.
The writer thread is suspended until enough free space is made in the buffer or the reader thread triggers a stop condition.
[out] | buffer | Address of the packet write buffer. |
[out] | mdata | Address of the packet metadata write buffer. |
[out] | buffer_size | Size in packets of the write buffer. |
[in] | min_size | Minimum number of free packets to get. This is just a hint. The returned size can be smaller, for instance when the write window of the circular buffer is close to the end of the buffer. |
void ts::TSPacketQueue::releaseWriteBuffer | ( | size_t | count | ) |
Called by the writer thread to release the write buffer.
The packets were written by the writer thread at the address which was returned by lockWriteBuffer().
[in] | count | Number of packets which were written in the buffer. Must be no greater than the size which was returned by lockWriteBuffer(). |
void ts::TSPacketQueue::setBitrate | ( | const BitRate & | bitrate | ) |
Called by the writer thread to report the input bitrate.
[in] | bitrate | Input bitrate. If zero, the input bitrate is unknown and will be computed from PCR's. |
|
inline |
Check if the reader thread has reported a stop condition.
bool ts::TSPacketQueue::getPacket | ( | TSPacket & | packet, |
TSPacketMetadata * | mdata, | ||
BitRate & | bitrate | ||
) |
Called by the reader thread to get the next packet without waiting.
The reader thread is never suspended. If no packet is available, return false.
[out] | packet | The returned packet. Unmodified when no packet is available. |
[out] | mdata | The returned packet metadata, if not null. Unmodified when no packet is available. |
[out] | bitrate | Input bitrate or zero if unknown. |
bool ts::TSPacketQueue::waitPackets | ( | TSPacket * | buffer, |
TSPacketMetadata * | mdata, | ||
size_t | buffer_count, | ||
size_t & | actual_count, | ||
BitRate & | bitrate | ||
) |
Called by the reader thread to wait for packets.
The reader thread is suspended until at least one packet is available.
[out] | buffer | Address of packet buffer. |
[out] | mdata | Address of the packet metadata buffer. Can be null. |
[in] | buffer_count | Size of buffer in number of packets. |
[out] | actual_count | Number of returned packets in buffer. |
[out] | bitrate | Input bitrate or zero if unknown. |
bool ts::TSPacketQueue::eof | ( | ) | const |
Check if the writer thread has reported an end of file condition.