arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
RTSPAudioStreamerBase< Platform > Class Template Reference

RTSPAudioStreamerBase - Core RTP Audio Streaming Engine. More...

#include <RTSPAudioStreamer.h>

Inheritance diagram for RTSPAudioStreamerBase< Platform >:
RTSPAudioStreamer< Platform > RTSPAudioStreamerUsingTask< Platform >

Public Member Functions

 RTSPAudioStreamerBase ()
 Default constructor for RTSPAudioStreamerBase.
 
 RTSPAudioStreamerBase (IAudioSource &source)
 Constructor with audio source.
 
virtual ~RTSPAudioStreamerBase ()
 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.
 
IAudioSourcegetAudioSource ()
 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.
 
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.
 
virtual void start ()
 Start audio source (base implementation)
 
virtual void stop ()
 Stop audio source (base implementation)
 

Static Public Member Functions

static void timerCallback (void *audioStreamerObj)
 Static timer callback function for periodic RTP streaming.
 

Protected Member Functions

void buildRtpHeader ()
 
uint32_t computeTimestampIncrement (int bytesSent)
 Compute RTP timestamp increment based on samples sent.
 
void sendOut (uint16_t totalLen)
 

Protected Attributes

const int HEADER_SIZE = 12
 
IAudioSourcem_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
 

Detailed Description

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:

Technical Details

Note
This base class does not include timer functionality
Use RTSPAudioStreamer for automatic timer-driven streaming
Author
Thomas Pfitzinger
Version
0.2.0

Constructor & Destructor Documentation

◆ RTSPAudioStreamerBase() [1/2]

template<typename Platform >
RTSPAudioStreamerBase ( )
inline

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 >
RTSPAudioStreamerBase ( IAudioSource source)
inline

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
sourcePointer 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 >
virtual ~RTSPAudioStreamerBase ( )
inlinevirtual

Destructor.

Cleans up allocated resources. Automatically stops any active streaming.

Note
UDP sockets are managed separately via releaseUdpTransport()

Member Function Documentation

◆ 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
bytesSentNumber 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 >
IAudioSource * getAudioSource ( )
inline

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 >
bool initAudioSource ( )
inline

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
tcpSockPointer to the RTSP TCP client socket
rtpChannelInterleaved channel number for RTP packets (e.g., 0)
rtcpChannelInterleaved 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
aClientIPIP address of the RTP client that will receive the stream
aClientPortPort 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
-1RTP 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 >
virtual void setAudioSource ( IAudioSource source)
inlinevirtual

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
sourcePointer 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 >
virtual void start ( )
inlinevirtual

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 >
virtual void stop ( )
inlinevirtual

Stop audio source (base implementation)

Stops the audio source. This base implementation does not manage any timer - derived classes should override this method to add timer management.

Note
Does not release UDP transport or buffers - use releaseUdpTransport() separately
Can be restarted by calling start() again
See also
start(), releaseUdpTransport()

Reimplemented in RTSPAudioStreamer< Platform >, and RTSPAudioStreamerUsingTask< 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
audioStreamerObjVoid pointer to the RTSPAudioStreamerBase instance (passed as callback parameter)
Note
This is a static method suitable for use as a timer callback
Logs warnings if packet transmission takes too long
See also
sendRtpPacketDirect()

Member Data Documentation

◆ m_timer_restart_needed

template<typename Platform >
volatile bool m_timer_restart_needed
protected
Initial value:
=
false

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