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

RTSPAudioStreamer - Timer-driven RTP Audio Streaming Engine. More...

#include <RTSPAudioStreamer.h>

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

Public Member Functions

 RTSPAudioStreamer ()
 Default constructor for RTSPAudioStreamer.
 
 RTSPAudioStreamer (IAudioSource &source)
 Constructor with audio source.
 
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.
 
void start () override
 Start timer-driven RTP streaming.
 
void stop () override
 Stop timer-driven RTP streaming.
 
void updateTimer ()
 Update timer period if audio format has changed.
 

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
 
audio_tools::TimerAlarmRepeating rtpTimer
 
const int STREAMING_BUFFER_SIZE = 1024 * 3
 

Detailed Description

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:

Note
This is the recommended class for most use cases
Use RTSPAudioStreamerBase for custom streaming control
Author
Thomas Pfitzinger
Version
0.2.0

Constructor & Destructor Documentation

◆ RTSPAudioStreamer() [1/2]

template<typename Platform >
RTSPAudioStreamer ( )
inline

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

Constructor with audio source.

Creates a new RTSPAudioStreamer instance with timer functionality and immediately configures it with the specified audio source.

Parameters
sourceReference to an object implementing the IAudioSource interface
See also
IAudioSource

Member Function Documentation

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

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

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
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 
)
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
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  )
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
-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)
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.

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 >
void start ( )
inlineoverridevirtual

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

Stop timer-driven RTP streaming.

Stops the RTP streaming process by:

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

Reimplemented from RTSPAudioStreamerBase< 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
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()

◆ updateTimer()

template<typename Platform >
void updateTimer ( )
inline

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

Member Data Documentation

◆ m_timer_restart_needed

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

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