Quite some time ago, I was describing how we can stream audio with the help of RTSP. In that example we needed to provide a data stream from which we can read the audio!

This is not very flexible and often we want to specify RTSP as output: e.g. in the AudioPlayer we want to tell to send the audio to a RTSP stream. Therefore I decided to extend the functionality a bit by

  • supporting a RTSP stream as a output destination.
  • supporting different formats with the help of encoders

To get started, I have defined some example formats. I suggest that you have a look the implementation that can be found in the RTSPStream.h file.

Arduino Example Sketch

Here is the example Arduino sketch: We just copy the audio generated by a sine wave generator to a RTSPStream object. In this example we use a G711 ulaw encoder.

#include "AudioTools.h"
#include "AudioLibs/RTSPStream.h"
#include "AudioCodecs/CodecG7xx.h"
#include "RTSPServer.h"

int port = 554;
AudioInfo info(8000, 1, 16); 
const char* wifi = "ssid";
const char* password = "password";

// Sine tone generator
SineFromTable<int16_t> sineWave(32000);         // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> sound(sineWave);  // Stream generated from sine wave
// rtsp
RTSPFormatG711 format;
G711_ULAWEncoder encoder;
RTSPStream rtsp_stream(format, encoder);
StreamCopy copier(rtsp_stream, sound);  // rtsp to sine
// Server
RTSPServer rtsp(rtsp_stream.streamer(), port);


void setup() {
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Warning);

  // Start Wifi & rtsp server
  rtsp.begin(wifi, password);

  // Setup sine wave
  sineWave.begin(info, N_B4);

  // Start Output Stream
  rtsp_stream.begin(info);

}

void loop() {
  if (rtsp_stream) {
      copier.copy();
  }
}

The audio can be played with the help of FFmpeg e.g. by executing the following command on the desktop:

ffplay -v debug rtsp://192.168.1.34:554

Just replace the ip address with the one for your ESP32!

Example Code

The sketch above has been tested at the time of writing. But please note that the API might change, so I suggest to use the (potentially) updated version in the examples directory.

Dependencies

Do not forget to install the relevant libraries for this example:


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *