RTSPAudioStreamer - Timer-driven RTP Audio Streaming Engine.
More...
#include <RTSPAudioStreamer.h>
|
static void | timerCallback (void *audioStreamerObj) |
| Static timer callback function for periodic RTP streaming.
|
|
|
void | buildRtpHeader () |
|
uint32_t | computeTimestampIncrement (int bytesSent) |
| Compute RTP timestamp increment based on samples sent.
|
|
void | sendOut (uint16_t totalLen) |
|
|
const int | HEADER_SIZE = 12 |
|
IAudioSource * | m_audioSource = nullptr |
|
IPAddress | m_ClientIP |
|
uint16_t | m_ClientPort |
|
int | m_fragmentSize = 0 |
|
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 |
|
int | m_SendIdx |
|
u_short | m_SequenceNumber |
|
uint32_t | m_Ssrc = 0x13F97E67 |
|
int | m_TcpRtcpChannel |
|
int | m_TcpRtpChannel |
|
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 |
|
audio_tools::TimerAlarmRepeating | rtpTimer |
|
const int | STREAMING_BUFFER_SIZE = 1024 * 3 |
|
template<typename Platform>
class audio_tools::RTSPAudioStreamer< Platform >
RTSPAudioStreamer - Timer-driven RTP Audio Streaming Engine.
The RTSPAudioStreamer class extends RTSPAudioStreamerBase with automatic timer-driven streaming functionality. This class provides:
- All base class functionality (audio source, UDP transport, RTP packets)
- Automatic periodic streaming using AudioTools TimerAlarmRepeating
- Timer safety configuration for ESP32 platforms
- Background streaming without manual intervention
- Note
- This is the recommended class for most use cases
-
Use RTSPAudioStreamerBase for custom streaming control
- Author
- Thomas Pfitzinger
- Version
- 0.2.0
◆ RTSPAudioStreamer() [1/2]
template<typename Platform >
Default constructor for RTSPAudioStreamer.
Creates a new RTSPAudioStreamer instance with timer functionality. Initializes the base class and configures the timer for safe operation.
- Note
- Audio source must be set before streaming can begin
- See also
- setAudioSource()
◆ RTSPAudioStreamer() [2/2]
template<typename Platform >
Constructor with audio source.
Creates a new RTSPAudioStreamer instance with timer functionality and immediately configures it with the specified audio source.
- Parameters
-
source | Reference to an object implementing the IAudioSource interface |
- See also
- IAudioSource
◆ checkTimerPeriodChange()
template<typename Platform >
bool checkTimerPeriodChange |
( |
| ) |
|
|
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.
- Returns
- true if timer period changed, false if unchanged or no audio source
- Note
- Call this periodically to detect format changes during streaming
- See also
- updateTimerPeriod()
◆ computeTimestampIncrement()
template<typename Platform >
uint32_t computeTimestampIncrement |
( |
int |
bytesSent | ) |
|
|
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.
- Parameters
-
bytesSent | Number of bytes sent in the last RTP packet |
- Returns
- Calculated timestamp increment value
- Note
- Uses exact samples sent if available, otherwise falls back to audio source format or a crude estimate
◆ currentRtpTimestamp()
template<typename Platform >
uint32_t currentRtpTimestamp |
( |
| ) |
const |
|
inlineinherited |
Get current RTP timestamp value that will be used in the next packet.
- Returns
- Current RTP timestamp
◆ currentSeq()
template<typename Platform >
uint16_t currentSeq |
( |
| ) |
const |
|
inlineinherited |
Get current RTP sequence number that will be used in the next packet.
- Returns
- Current RTP sequence number
◆ getAudioSource()
template<typename Platform >
Get the configured audio source.
- Returns
- Pointer to the current audio source, nullptr if not set
- See also
- setAudioSource()
◆ getRtcpServerPort()
template<typename Platform >
u_short getRtcpServerPort |
( |
| ) |
|
|
inlineinherited |
Get the RTCP server port number.
- Returns
- Port number used for RTCP communication, 0 if not initialized
- See also
- initUdpTransport()
◆ getRtpServerPort()
template<typename Platform >
u_short getRtpServerPort |
( |
| ) |
|
|
inlineinherited |
Get the RTP server port number.
- Returns
- Port number used for RTP packet transmission, 0 if not initialized
- See also
- initUdpTransport()
◆ getTimerPeriodMs()
template<typename Platform >
uint32_t getTimerPeriodMs |
( |
| ) |
const |
|
inlineinherited |
Get the timer period in milliseconds.
- Returns
- Timer period in milliseconds for convenience
- See also
- getTimerPeriodUs()
◆ getTimerPeriodUs()
template<typename Platform >
uint32_t getTimerPeriodUs |
( |
| ) |
const |
|
inlineinherited |
Get the timer period in microseconds.
- Returns
- Timer period configured from audio source format
- See also
- initAudioSource()
◆ initAudioSource()
template<typename Platform >
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.
- Returns
- true if initialization succeeded, false if audio source or format is invalid
- Note
- Fragment size determines how much audio data is sent per RTP packet
-
Timer period controls the streaming rate and must match the audio sample rate
◆ initTcpInterleavedTransport()
template<typename Platform >
void initTcpInterleavedTransport |
( |
typename Platform::TcpClientType * |
tcpSock, |
|
|
int |
rtpChannel, |
|
|
int |
rtcpChannel |
|
) |
| |
|
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.
- Parameters
-
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) |
◆ initUdpTransport()
template<typename Platform >
bool initUdpTransport |
( |
IPAddress |
aClientIP, |
|
|
uint16_t |
aClientPort |
|
) |
| |
|
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.
- Parameters
-
aClientIP | IP address of the RTP client that will receive the stream |
aClientPort | Port number on the client for receiving RTP packets |
- Returns
- true if transport initialization succeeded, false on socket creation failure
- Note
- Port allocation: RTP uses even ports (6970, 6972, ...), RTCP uses odd ports (6971, 6973, ...)
-
Call releaseUdpTransport() when streaming to this client is complete
- See also
- releaseUdpTransport(), getRtpServerPort(), getRtcpServerPort()
◆ releaseUdpTransport()
template<typename Platform >
void releaseUdpTransport |
( |
void |
| ) |
|
|
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.
- Note
- Only closes sockets when reference count reaches zero
-
Always call this method when streaming to a client is complete
- See also
- initUdpTransport()
◆ sendRtpPacketDirect()
template<typename Platform >
int sendRtpPacketDirect |
( |
| ) |
|
|
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:
- RTP header construction with sequence numbers and timestamps
- Audio data retrieval from the source
- Network byte order conversion
- UDP transmission to the configured client
- Returns
- Number of bytes sent in the packet payload, or negative value on error
- Return values
-
-1 | RTP buffer not allocated or audio source not configured |
- Note
- This method is typically called periodically by the timer-driven streaming
-
Automatically increments sequence numbers and updates timestamps
- See also
- start(), timerCallback()
◆ setAudioSource()
template<typename Platform >
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.
- Parameters
-
source | Pointer to an IAudioSource implementation. Must not be nullptr. |
- Note
- Calling this method will reinitialize audio source parameters
- See also
- IAudioSource, initAudioSource()
◆ start()
template<typename Platform >
Start timer-driven RTP streaming.
Begins the RTP streaming process by:
- Calling base class start() to initialize audio source and buffer
- Creating and configuring the TimerAlarmRepeating for periodic streaming
- Beginning periodic timer-driven packet transmission
- Note
- UDP transport must be initialized before calling this method
-
Audio source must be configured before calling this method
- Warning
- Will log errors if audio source is not properly configured
- See also
- stop(), initUdpTransport(), setAudioSource()
Reimplemented from RTSPAudioStreamerBase< Platform >.
◆ stop()
template<typename Platform >
◆ timerCallback()
template<typename Platform >
static void timerCallback |
( |
void * |
audioStreamerObj | ) |
|
|
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.
- Parameters
-
- Note
- This is a static method suitable for use as a timer callback
-
Logs warnings if packet transmission takes too long
- See also
- sendRtpPacketDirect()
◆ updateTimer()
template<typename Platform >
Update timer period if audio format has changed.
Checks for timer period changes and updates the timer if needed. Call this periodically to enable dynamic timer period adjustments.
- Note
- Safe to call during streaming - timer restarts with new period
◆ m_timer_restart_needed
template<typename Platform >
volatile bool m_timer_restart_needed |
|
protectedinherited |
The documentation for this class was generated from the following file: