arduino-audio-tools
Loading...
Searching...
No Matches
RTSPMediaSourceMTS Class Reference

IMediaSource for an MPEG transport stream, e.g. live video and audio muxed by MuxerMTS: the muxer writes its output to this object (it is a Print), and an RTSPMediaStreamer sends it as one RTP track (MP2T, RFC 2250 - see RTSPFormatMTS) to the RTSP client, which demuxes video and audio itself. More...

#include <RTSPMediaSourceMTS.h>

Inheritance diagram for RTSPMediaSourceMTS:
IMediaSource Print

Public Member Functions

 RTSPMediaSourceMTS (int bufferPackets=128, float sendRate=100)
virtual int availableForWrite ()
virtual int availableForWrite ()
uint32_t droppedPackets ()
 Number of TS packets dropped because the buffer was full.
uint32_t droppedVideoPackets ()
 Number of video TS packets dropped to keep the audio going.
virtual void flush ()
virtual void flush ()
RTSPFormat & getFormat () override
 Get the media format configuration.
int packetSize () override
 Size of the next RTP payload: up to kPacketsPerFragment TS packets.
int readBytes (void *dest, int maxBytes) override
 Provides whole TS packets only.
void start () override
void stop () override
 Called when the client stops streaming.
size_t write (const uint8_t *data, size_t len) override
 Accepts the transport stream, in any chunk sizes.
size_t write (uint8_t c) override

Static Public Attributes

static constexpr int kPacketSize = 188
static constexpr int kPacketsPerFragment = 7

Protected Member Functions

void addPacket ()
bool isVideoPes ()

Protected Attributes

int _timeout = 10
RingBufferSPSC< uint8_t > buffer
int capacity = 0
uint32_t dropped = 0
uint32_t dropped_video = 0
bool dropping_video = false
RTSPFormatMTS format
volatile bool is_active = false
uint8_t packet [kPacketSize]
size_t packet_len = 0
int video_pid = -1

Detailed Description

IMediaSource for an MPEG transport stream, e.g. live video and audio muxed by MuxerMTS: the muxer writes its output to this object (it is a Print), and an RTSPMediaStreamer sends it as one RTP track (MP2T, RFC 2250 - see RTSPFormatMTS) to the RTSP client, which demuxes video and audio itself.

Unlike RTSPOutput's generic byte stream, this source is meant for a live stream written from other tasks (e.g. by VideoMuxerWithTasks):

  • the data is kept as complete 188 byte TS packets in a fixed size, lock-free single producer/single consumer ring buffer (RingBufferSPSC), and every RTP packet contains whole TS packets (up to 7: 1316 bytes, below the usual MTU);
  • it is a packetized source (packetSize()), so the streamer sends all queued packets at each timer tick instead of a fixed amount: the send rate adapts to the bitrate of the stream;
  • while no client is streaming, the data is dropped, so a new client doesn't get old data;
  • if the network is slower than the stream, video is dropped rather than audio: when the buffer is filled to 3/4, the video packets are dropped from the start of the next video frame on, until the buffer is half empty again and the next keyframe starts (MuxerMTS writes the PAT before each keyframe). So the picture freezes for a moment while the sound continues. Audio and the other packets are only dropped if the buffer is completely full. Audio and video are recognized by the stream id of their PES packets, so this works with any transport stream. See droppedPackets() and droppedVideoPackets(). A client starts decoding at the next keyframe: MuxerMTS repeats the stream description (PAT/PMT) before each one.

The data can be written from any task, but not from several tasks at the same time - e.g. by a MuxerMTS driven by VideoMuxerWithTasks, which serializes the writes of its audio and video task with its mutex.

Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ RTSPMediaSourceMTS()

RTSPMediaSourceMTS ( int bufferPackets = 128,
float sendRate = 100 )
inline
Parameters
bufferPacketssize of the buffer in TS packets
sendRatenumber of times per second the queued packets are sent

Member Function Documentation

◆ addPacket()

void addPacket ( )
inlineprotected

◆ availableForWrite() [1/2]

virtual int availableForWrite ( )
inlinevirtualinherited

Reimplemented in A2DPStream, AbstractURLStream, AdapterAudioStreamToAudioOutput, AnalogAudioStream, AdapterAudioStreamToAudioOutput, AudioOutput, AudioOutput, AudioStreamWrapper, AudioStreamWrapper, BaseStream, BaseStream, ConverterStream< T >, ConverterStream< T >, CsvOutput< T >, CsvOutput< T >, DynamicMemoryStream, DynamicMemoryStream, EncodedAudioOutput, EncodedAudioStream, FilteredStream< T, TF >, FilteredStream< T, TF >, FilteredStream< int16_t, int16_t >, MemoryOutput, MemoryOutput, MemoryStream, MemoryStream, OutputMixer< T >, OutputMixer< T >, ProgressStream, ProgressStream, QueueStream< T >, QueueStream< T >, QueueStream< uint8_t >, ReformatBaseStream, RingBufferStream, RingBufferStream, Throttle, Throttle, TimedStream, AudioBLEClient, AudioBLEClient, AudioBLEServer, AudioBLEServer, AudioEffectStream, AudioEffectStreamT< T >, AudioESP32ULP, AudioFFTBase, AudioLoRa, AudioOutput, AudioServerEx, AudioStreamWrapper, AudioSyncWriter, AutomaticGainControlStream, BaseStream, ChannelFormatConverterStream, ChannelFormatConverterStreamT< T >, ConverterStream< T >, CsvOutput< T >, DynamicMemoryStream, EncodedAudioOutput, EncodedAudioStream, Equalizer3BandsPerChannelT< T >, Equalizer3BandsPerChannelT< float >, Equalizer3BandsT< T >, Equalizer3BandsT< float >, ESPNowStream, FadeStream, FaustStream< DSP >, FileOutput, FilteredStream< T, TF >, FrequencyDetectorAutoCorrelation, FrequencyDetectorZeroCrossing, HammingFEC< bytecount, block_t >, I2SCodecStream, I2SStream, MeasuringStream, MemoryOutput, MemoryStream, MiniAudioStream, NRF24Stream, NumberFormatConverterStream, NumberFormatConverterStreamT< TFrom, TTo >, OutputMixer< T >, Pipeline, ProgressStream, PWMAudioOutput< PWMDriverT >, QualityAnalysisStream, QueueStream< T >, ReadioHeadStream, ReedSolomonFEC< bytecount, additional_bytes >, ReformatBaseStream, RingBufferStream, RTSPOutput< Platform >, SPIAudioMaster, SPIAudioSlave, StdioStream, SupportedRatesStream, SynchronizedStream, Throttle, TimedStream, UDPStream, URLStreamESP32, USBAudioStreamBase, VBANStream, and VolumeStream.

◆ availableForWrite() [2/2]

virtual int availableForWrite ( )
inlinevirtualinherited

Reimplemented in A2DPStream, AbstractURLStream, AdapterAudioStreamToAudioOutput, AnalogAudioStream, AdapterAudioStreamToAudioOutput, AudioOutput, AudioOutput, AudioStreamWrapper, AudioStreamWrapper, BaseStream, BaseStream, ConverterStream< T >, ConverterStream< T >, CsvOutput< T >, CsvOutput< T >, DynamicMemoryStream, DynamicMemoryStream, EncodedAudioOutput, EncodedAudioStream, FilteredStream< T, TF >, FilteredStream< T, TF >, FilteredStream< int16_t, int16_t >, MemoryOutput, MemoryOutput, MemoryStream, MemoryStream, OutputMixer< T >, OutputMixer< T >, ProgressStream, ProgressStream, QueueStream< T >, QueueStream< T >, QueueStream< uint8_t >, ReformatBaseStream, RingBufferStream, RingBufferStream, Throttle, Throttle, TimedStream, AudioBLEClient, AudioBLEClient, AudioBLEServer, AudioBLEServer, AudioEffectStream, AudioEffectStreamT< T >, AudioESP32ULP, AudioFFTBase, AudioLoRa, AudioOutput, AudioServerEx, AudioStreamWrapper, AudioSyncWriter, AutomaticGainControlStream, BaseStream, ChannelFormatConverterStream, ChannelFormatConverterStreamT< T >, ConverterStream< T >, CsvOutput< T >, DynamicMemoryStream, EncodedAudioOutput, EncodedAudioStream, Equalizer3BandsPerChannelT< T >, Equalizer3BandsPerChannelT< float >, Equalizer3BandsT< T >, Equalizer3BandsT< float >, ESPNowStream, FadeStream, FaustStream< DSP >, FileOutput, FilteredStream< T, TF >, FrequencyDetectorAutoCorrelation, FrequencyDetectorZeroCrossing, HammingFEC< bytecount, block_t >, I2SCodecStream, I2SStream, MeasuringStream, MemoryOutput, MemoryStream, MiniAudioStream, NRF24Stream, NumberFormatConverterStream, NumberFormatConverterStreamT< TFrom, TTo >, OutputMixer< T >, Pipeline, ProgressStream, PWMAudioOutput< PWMDriverT >, QualityAnalysisStream, QueueStream< T >, ReadioHeadStream, ReedSolomonFEC< bytecount, additional_bytes >, ReformatBaseStream, RingBufferStream, RTSPOutput< Platform >, SPIAudioMaster, SPIAudioSlave, StdioStream, SupportedRatesStream, SynchronizedStream, Throttle, TimedStream, UDPStream, URLStreamESP32, USBAudioStreamBase, VBANStream, and VolumeStream.

◆ droppedPackets()

uint32_t droppedPackets ( )
inline

Number of TS packets dropped because the buffer was full.

◆ droppedVideoPackets()

uint32_t droppedVideoPackets ( )
inline

Number of video TS packets dropped to keep the audio going.

◆ flush() [1/2]

◆ flush() [2/2]

◆ getFormat()

RTSPFormat & getFormat ( )
inlineoverridevirtual

Get the media format configuration.

Returns the RTSPFormat object that describes the media data characteristics including sample rate, bit depth, number of channels, and RTP packaging parameters. If no format has been explicitly set, creates a default 16-bit PCM format at 16kHz mono.

Returns
Pointer to RTSPFormat describing the media characteristics
Note
Default format: 16-bit PCM, 16000 Hz, 1 channel
See also
setFormat(), RTSPFormatPCM

Implements IMediaSource.

◆ isVideoPes()

bool isVideoPes ( )
inlineprotected

True if the current packet starts a video PES packet (stream id 0xE0-0xEF)

◆ packetSize()

int packetSize ( )
inlineoverridevirtual

Size of the next RTP payload: up to kPacketsPerFragment TS packets.

Reimplemented from IMediaSource.

◆ readBytes()

int readBytes ( void * dest,
int maxBytes )
inlineoverridevirtual

Provides whole TS packets only.

Implements IMediaSource.

◆ start()

void start ( )
inlineoverridevirtual

Called when a client starts streaming, before the first readBytes(): discards the old data (on the consumer side - reset() would not be safe while the producer is writing)

Reimplemented from IMediaSource.

◆ stop()

void stop ( )
inlineoverridevirtual

Called when the client stops streaming.

Reimplemented from IMediaSource.

◆ write() [1/2]

size_t write ( const uint8_t * data,
size_t len )
inlineoverridevirtual

Accepts the transport stream, in any chunk sizes.

Reimplemented from Print.

◆ write() [2/2]

size_t write ( uint8_t c)
inlineoverride

Member Data Documentation

◆ _timeout

int _timeout = 10
protectedinherited

◆ buffer

RingBufferSPSC<uint8_t> buffer
protected

◆ capacity

int capacity = 0
protected

◆ dropped

uint32_t dropped = 0
protected

◆ dropped_video

uint32_t dropped_video = 0
protected

◆ dropping_video

bool dropping_video = false
protected

◆ format

RTSPFormatMTS format
protected

◆ is_active

volatile bool is_active = false
protected

◆ kPacketSize

int kPacketSize = 188
staticconstexpr

◆ kPacketsPerFragment

int kPacketsPerFragment = 7
staticconstexpr

◆ packet

uint8_t packet[kPacketSize]
protected

◆ packet_len

size_t packet_len = 0
protected

◆ video_pid

int video_pid = -1
protected

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