arduino-audio-tools
|
RTSPAudioStreamerUsingTask - Task-driven RTP Audio Streaming Engine. More...
#include <RTSPAudioStreamer.h>
Public Member Functions | |
RTSPAudioStreamerUsingTask (bool throttled=true) | |
Default constructor for RTSPAudioStreamerUsingTask. | |
RTSPAudioStreamerUsingTask (IAudioSource &source, bool throttled=true) | |
Constructor with audio source and throttling control. | |
virtual | ~RTSPAudioStreamerUsingTask () |
Destructor. | |
bool | checkTimerPeriodChange () |
Check if timer period has changed and update if necessary. | |
uint32_t | currentRtpTimestamp () const |
Get current RTP timestamp value that will be used in the next packet. | |
uint16_t | currentSeq () const |
Get current RTP sequence number that will be used in the next packet. | |
uint32_t | currentSsrc () const |
Get current SSRC used in RTP header. | |
IAudioSource * | getAudioSource () |
Get the configured audio source. | |
u_short | getRtcpServerPort () |
Get the RTCP server port number. | |
u_short | getRtpServerPort () |
Get the RTP server port number. | |
uint32_t | getTimerPeriodMs () const |
Get the timer period in milliseconds. | |
uint32_t | getTimerPeriodUs () const |
Get the timer period in microseconds. | |
bool | initAudioSource () |
Initialize audio source configuration. | |
void | initTcpInterleavedTransport (typename Platform::TcpClientType *tcpSock, int rtpChannel, int rtcpChannel) |
Initialize TCP interleaved transport for RTP over RTSP. | |
bool | initUdpTransport (IPAddress aClientIP, uint16_t aClientPort) |
Initialize UDP transport for RTP streaming. | |
bool | isTaskRunning () const |
Check if streaming task is currently running. | |
void | releaseUdpTransport (void) |
Release UDP transport resources. | |
int | sendRtpPacketDirect () |
Send a single RTP packet with audio data. | |
virtual void | setAudioSource (IAudioSource *source) |
Configure the audio source for streaming. | |
void | setFixedDelayMs (uint32_t delayUs) |
Set a fixed inter-packet delay in non-throttled mode. | |
void | setTaskParameters (uint32_t stackSize, uint8_t priority, int core=-1) |
Set task parameters for streaming task. | |
void | setThrottled (bool isThrottled) |
Enable or disable throttled timing mode. | |
void | setThrottleInterval (uint32_t interval) |
Set the throttle interval (number of sends before precise correction) | |
void | start () override |
Start task-driven RTP streaming. | |
void | stop () override |
Stop task-driven RTP streaming. | |
Static Public Member Functions | |
static void | timerCallback (void *audioStreamerObj) |
Static timer callback function for periodic RTP streaming. | |
Protected Member Functions | |
void | applyThrottling (unsigned long iterationStartUs) |
Apply streaming throttling policy. | |
void | buildRtpHeader () |
uint32_t | computeTimestampIncrement (int bytesSent) |
Compute RTP timestamp increment based on samples sent. | |
void | sendOut (uint16_t totalLen) |
void | streamingTaskLoop () |
Main streaming task loop iteration. | |
Protected Attributes | |
const int | HEADER_SIZE = 12 |
IAudioSource * | m_audioSource = nullptr |
IPAddress | m_ClientIP |
uint16_t | m_ClientPort |
uint16_t | m_fixed_delay_ms = 1 |
Fixed delay in milliseconds (if used) | |
int | m_fragmentSize = 0 |
unsigned long | m_last_throttle_us |
Start timestamp of current throttle window (micros) | |
int | m_lastSamplesSent = 0 |
int | m_payloadType = 96 |
uint32_t | m_prevMsec |
uint16_t | m_RtcpServerPort |
Platform::UdpSocketType * | m_RtcpSocket |
uint16_t | m_RtpServerPort |
Platform::UdpSocketType * | m_RtpSocket |
Platform::TcpClientType * | m_RtspTcpSocket |
uint32_t | m_send_counter |
Counts sends within the current throttle window. | |
int | m_SendIdx |
u_short | m_SequenceNumber |
uint32_t | m_Ssrc = 0x13F97E67 |
audio_tools::Task | m_streamingTask |
AudioTools task for streaming loop. | |
int | m_taskCore = -1 |
CPU core affinity (-1 = any core) | |
uint8_t | m_taskPriority = 5 |
Task priority (5 = medium priority) | |
volatile bool | m_taskRunning |
Flag indicating if task is active. | |
uint32_t | m_taskStackSize = 8192 |
Task stack size in bytes (8KB default) | |
int | m_TcpRtcpChannel |
int | m_TcpRtpChannel |
uint32_t | m_throttle_interval |
Number of sends before precise correction. | |
bool | m_throttled = true |
Enable precise microsecond timing. | |
int | m_timer_period_us = 20000 |
volatile bool | m_timer_restart_needed |
uint32_t | m_Timestamp |
int | m_udpRefCount |
bool | m_useTcpInterleaved |
audio_tools::Vector< uint8_t > | mMp3Carry |
int | mMp3CarryLen = 0 |
audio_tools::Vector< uint8_t > | mRtpBuf |
const int | STREAMING_BUFFER_SIZE = 1024 * 3 |
RTSPAudioStreamerUsingTask - Task-driven RTP Audio Streaming Engine.
The RTSPAudioStreamerUsingTask class extends RTSPAudioStreamerBase with AudioTools Task-driven streaming functionality. Instead of using hardware timers, this class creates a dedicated task that continuously calls the timer callback at the appropriate intervals. This approach provides:
The throttling feature is particularly important when working with audio sources that can provide data faster than the defined sampling rate, such as file readers, buffer sources, or fast generators. Without throttling, these sources would flood the network with packets faster than real-time playback.
|
inline |
Default constructor for RTSPAudioStreamerUsingTask.
Creates a new RTSPAudioStreamerUsingTask instance with task functionality. Initializes the base class and sets default task parameters.
throttled | Enable precise timing control with microsecond delays. Set to true when your audio source provides data faster than the defined sampling rate (e.g., reading from files, buffers, or fast generators). Set to false when the source naturally produces data at the correct rate (e.g., ADC, microphone input, or rate-limited streams). Default: false for better performance. |
|
inline |
Constructor with audio source and throttling control.
Creates a new RTSPAudioStreamerUsingTask instance with task functionality and immediately configures it with the specified audio source.
source | Reference to an object implementing the IAudioSource interface. The source provides audio data and format information. |
throttled | Enable precise timing control with microsecond delays. Set to true when your audio source provides data faster than the defined sampling rate (e.g., reading from files, buffers, or fast generators). Set to false when the source naturally produces data at the correct rate (e.g., ADC, microphone input, or rate-limited streams). Default: false for better performance. |
|
inlinevirtual |
Destructor.
Ensures the streaming task is properly stopped and cleaned up.
|
inlineprotected |
Apply streaming throttling policy.
Implements the two-stage throttling:
m_fixed_delay_ms
after each sendm_throttle_interval
sends, precisely correct drift to match m_timer_period_us
pacingResets the throttle window if the source format changes.
iterationStartUs | Start timestamp of this loop iteration (micros) |
|
inlineinherited |
Check if timer period has changed and update if necessary.
Checks the audio source format for timer period changes and updates the cached value if different. This enables dynamic timer period adjustments during playback when the audio format changes.
|
inlineprotectedinherited |
Compute RTP timestamp increment based on samples sent.
Calculates the appropriate RTP timestamp increment based on the number of audio samples sent in the last packet. This ensures proper timing and synchronization in the RTP stream.
bytesSent | Number of bytes sent in the last RTP packet |
|
inlineinherited |
Get current RTP timestamp value that will be used in the next packet.
|
inlineinherited |
Get current RTP sequence number that will be used in the next packet.
|
inlineinherited |
Get the configured audio source.
|
inlineinherited |
Get the RTCP server port number.
|
inlineinherited |
Get the RTP server port number.
|
inlineinherited |
Get the timer period in milliseconds.
|
inlineinherited |
Get the timer period in microseconds.
|
inlineinherited |
Initialize audio source configuration.
Extracts and caches configuration parameters from the audio source including fragment size (bytes per packet) and timer period (microseconds between packets). This method must be called before streaming can begin.
|
inlineinherited |
Initialize TCP interleaved transport for RTP over RTSP.
Configures the streamer to send RTP packets using RTSP TCP interleaving. RTP and RTCP packets are framed with '$', channel byte, and 2-byte length per RFC 2326 section 10.12 and sent over the RTSP control socket.
tcpSock | Pointer to the RTSP TCP client socket |
rtpChannel | Interleaved channel number for RTP packets (e.g., 0) |
rtcpChannel | Interleaved channel number for RTCP packets (e.g., 1) |
|
inlineinherited |
Initialize UDP transport for RTP streaming.
Sets up UDP sockets for RTP and RTCP communication with a specific client. Automatically allocates consecutive port pairs starting from 6970. Supports multiple clients through reference counting - subsequent calls with different clients will reuse existing sockets.
aClientIP | IP address of the RTP client that will receive the stream |
aClientPort | Port number on the client for receiving RTP packets |
|
inline |
Check if streaming task is currently running.
|
inlineinherited |
Release UDP transport resources.
Decrements the reference count for UDP socket usage and closes sockets when no more clients are using them. This implements proper resource management for multiple concurrent streaming sessions.
|
inlineinherited |
Send a single RTP packet with audio data.
Constructs and sends one RTP packet containing audio data from the configured source. This method handles the complete RTP packet creation process including:
-1 | RTP buffer not allocated or audio source not configured |
|
inlinevirtualinherited |
Configure the audio source for streaming.
Sets or changes the audio source that provides data for the RTP stream. This method automatically initializes the audio source configuration including fragment size and timer period based on the source's format specifications.
source | Pointer to an IAudioSource implementation. Must not be nullptr. |
|
inline |
Set a fixed inter-packet delay in non-throttled mode.
Disables microsecond-level throttling and applies a constant delay between iterations of the streaming task using delay()
. This is useful when the audio source is naturally rate-limited and a small cooperative sleep is sufficient to avoid busy looping.
delayUs | Fixed delay in milliseconds applied via delay() . |
|
inline |
Set task parameters for streaming task.
Configure the AudioTools Task parameters before starting streaming. These parameters control the task's resource usage and scheduling behavior. Must be called before start() to take effect.
stackSize | Stack size in bytes for the streaming task (default: 8192) Increase if experiencing stack overflow issues |
priority | Task priority (0-configMAX_PRIORITIES-1, default: 5) Higher values = higher priority. Use with caution. |
core | CPU core to pin task to (default: -1 for any core) ESP32 only: 0 or 1 for specific core, -1 for automatic |
|
inline |
Enable or disable throttled timing mode.
Throttled mode provides more precise timing by using microsecond-level delays, but consumes more CPU resources. This is essential when your audio source can provide data faster than the defined sampling rate.
When to enable throttling (isThrottled = true):
When to disable throttling (isThrottled = false):
isThrottled | true to enable precise timing with delays, false for efficient timing |
|
inline |
Set the throttle interval (number of sends before precise correction)
After each packet send, the streamer delays by a fixed millisecond value. After interval
sends, it applies a precise microsecond correction based on the expected elapsed time (interval * timerPeriodUs) versus the actual elapsed time, then resets the interval window.
interval | Number of sends per throttle window (e.g., 1000 or 10000) |
|
inlineoverridevirtual |
Start task-driven RTP streaming.
Begins the RTP streaming process by:
Reimplemented from RTSPAudioStreamerBase< Platform >.
|
inlineoverridevirtual |
Stop task-driven RTP streaming.
Stops the RTP streaming process by:
Reimplemented from RTSPAudioStreamerBase< Platform >.
|
inlineprotected |
Main streaming task loop iteration.
This method contains a single iteration of the streaming loop that is called continuously by the AudioTools Task. Each iteration:
The task runs in its own context using the AudioTools Task class and handles timing based on the throttling mode setting. Dynamic timer period changes are handled seamlessly within the task loop.
|
inlinestaticinherited |
Static timer callback function for periodic RTP streaming.
This static method is called periodically by timers to send RTP packets. It handles timing measurements and calls sendRtpPacketDirect() to transmit audio data. Performance monitoring ensures the streaming keeps up with real-time requirements.
This method can be used by any derived class that implements timer-driven streaming functionality.
audioStreamerObj | Void pointer to the RTSPAudioStreamerBase instance (passed as callback parameter) |
|
protected |
Start timestamp of current throttle window (micros)
|
protected |
Counts sends within the current throttle window.
|
protected |
Number of sends before precise correction.
|
protectedinherited |