RTSPAudioStreamerBase - Core 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 |
|
const int | STREAMING_BUFFER_SIZE = 1024 * 3 |
|
template<typename Platform>
class audio_tools::RTSPAudioStreamerBase< Platform >
RTSPAudioStreamerBase - Core RTP Audio Streaming Engine.
The RTSPAudioStreamerBase class provides the fundamental RTP streaming functionality without timer management. This base class handles:
- Audio source initialization and management
- UDP transport setup for RTP/RTCP communications
- RTP packet formation with proper headers and sequencing
- Network format conversion (endianness handling)
- Manual packet transmission via sendRtpPacketDirect()
Technical Details
- Uses RTP protocol with L16 payload format (16-bit linear PCM)
- Default streaming buffer size: 2048 bytes
- Supports multiple concurrent clients via reference counting
- Automatic port allocation starting from 6970
- Note
- This base class does not include timer functionality
-
Use RTSPAudioStreamer for automatic timer-driven streaming
- Author
- Thomas Pfitzinger
- Version
- 0.2.0
◆ RTSPAudioStreamerBase() [1/2]
template<typename Platform >
Default constructor for RTSPAudioStreamerBase.
Creates a new RTSPAudioStreamerBase instance with default configuration. Initializes internal state including ports, sequence numbers, and streaming parameters. No audio source is assigned - use setAudioSource() to configure streaming source.
- Note
- Audio source must be set before streaming can begin
- See also
- setAudioSource()
◆ RTSPAudioStreamerBase() [2/2]
template<typename Platform >
Constructor with audio source.
Creates a new RTSPAudioStreamerBase instance and immediately configures it with the specified audio source. This is equivalent to calling the default constructor followed by setAudioSource().
- Parameters
-
source | Pointer to an object implementing the IAudioSource interface. The source provides audio data and format information for streaming. |
- Note
- The audio source object must remain valid for the lifetime of the RTSPAudioStreamerBase
- See also
- IAudioSource
◆ ~RTSPAudioStreamerBase()
template<typename Platform >
Destructor.
Cleans up allocated resources. Automatically stops any active streaming.
- Note
- UDP sockets are managed separately via releaseUdpTransport()
◆ checkTimerPeriodChange()
template<typename Platform >
bool checkTimerPeriodChange |
( |
| ) |
|
|
inline |
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 | ) |
|
|
inlineprotected |
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 |
|
inline |
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 |
|
inline |
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 |
( |
| ) |
|
|
inline |
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 |
( |
| ) |
|
|
inline |
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 |
|
inline |
Get the timer period in milliseconds.
- Returns
- Timer period in milliseconds for convenience
- See also
- getTimerPeriodUs()
◆ getTimerPeriodUs()
template<typename Platform >
uint32_t getTimerPeriodUs |
( |
| ) |
const |
|
inline |
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 |
|
) |
| |
|
inline |
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 |
|
) |
| |
|
inline |
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 |
| ) |
|
|
inline |
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 |
( |
| ) |
|
|
inline |
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 audio source (base implementation)
Begins the audio source and initializes the RTP buffer if not already done. This base implementation does not start any timer - derived classes should override this method to add timer functionality.
- 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 in RTSPAudioStreamer< Platform >, and RTSPAudioStreamerUsingTask< Platform >.
◆ stop()
template<typename Platform >
◆ timerCallback()
template<typename Platform >
static void timerCallback |
( |
void * |
audioStreamerObj | ) |
|
|
inlinestatic |
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()
◆ m_timer_restart_needed
template<typename Platform >
volatile bool m_timer_restart_needed |
|
protected |
The documentation for this class was generated from the following file: