pschatzmann / Micro-RTSP-Audio

A RTSP audio server intended for the ESP32
38 stars 5 forks source link

esp32 + rtsp + INMP441(mems micro) #2

Closed D10n1x closed 2 years ago

D10n1x commented 2 years ago

I've just started studying esp32, figured out how to launch and listen from the INMP441 microphone via i2s, figured out how to run your code for broadcasting over rtsp, but for a couple of days I can't figure out how to replace AudioTestSource(); with the sound received from the microphone. could you tell me where to dig or help

pschatzmann commented 2 years ago

You will need to implement your own Subclass of IAudioSource. If you use my https://github.com/pschatzmann/arduino-audio-tools library this should be quite easy:

Or you can try this example replacing the AudioKit with an I2SStream. But you might need to take the latest correction form the Audio Tools...

I am not happy with the current functionality and I think we need to integrate some codecs...

D10n1x commented 2 years ago

Thanks for the hint, but it’s difficult to understand with my knowledge, although I’ll figure it out individually, it seems like I’ll study something simpler for now

D10n1x commented 2 years ago

Using the above library https://github.com/pschatzmann/arduino-audio-tools I compiled the following code

#include <Arduino.h>

#include "AudioTools.h"
#include "AudioI2S/I2SStream.h"
#include "AudioLibs/MicroRTSPAudio.h"
#include "AudioStreamer.h"
#include "RTSPServer.h"

const char* ssid = "ssid";
const char* password = "password";

int port = 554;

I2SStream i2s;  // Audio source
RTSPSourceAudioStream source(i2s); // IAudioSource for RTSP
AudioStreamer streamer = AudioStreamer(&source); // Stream audio via RTSP
RTSPServer rtsp = RTSPServer(&streamer, port);
// RTSPServer rtsp(&streamer, port);

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

    // start i2s input with default configuration
    Serial.println("starting I2S...");
    auto cfg = i2s.defaultConfig(RX_MODE); 
    cfg.i2s_format = I2S_STD_FORMAT; // I2S_LSB_FORMAT I2S_STD_FORMAT
    cfg.bits_per_sample = 16;
    cfg.channels = 1;
    cfg.sample_rate = 44100;
    cfg.is_master = true;
    i2s.begin(cfg);

    Serial.println("Start Wifi");
    rtsp.begin(ssid, password);    
}

void loop() {
    delay(1000);
}

But when the client connects to RTSP, I get an error

E (32714) I2S: i2s_read(2213): RX mode is not enabled
[E] I2SESP32.h : 93 - size_t audio_tools::I2SBase::readBytes(void*, size_t)

before this happens the following

starting I2S...
[I] I2SConfig.h : 89 - rx/tx mode: RX
[I] I2SConfig.h : 90 - port_no: 0
[I] I2SConfig.h : 91 - is_master: Master
[I] I2SConfig.h : 92 - sample rate: 44100
[I] I2SConfig.h : 93 - bits per sample: 16
[I] I2SConfig.h : 94 - number of channels: 1
[I] I2SConfig.h : 95 - i2s_format: I2S_STD_FORMAT
[I] I2SConfig.h : 97 - auto_clear:0
[I] I2SConfig.h : 104 - buffer_count:10
[I] I2SConfig.h : 105 - buffer_size:512
[I] I2SConfig.h : 109 - pin_bck: 14
[I] I2SConfig.h : 110 - pin_ws: 15
[I] I2SConfig.h : 111 - pin_data: 32

and after a while it

[I] I2SConfig.h : 89 - rx/tx mode: TX
[I] I2SConfig.h : 90 - port_no: 0
[I] I2SConfig.h : 91 - is_master: Master
[I] I2SConfig.h : 92 - sample rate: 44100
[I] I2SConfig.h : 93 - bits per sample: 16
[I] I2SConfig.h : 94 - number of channels: 2
[I] I2SConfig.h : 95 - i2s_format: I2S_STD_FORMAT
[I] I2SConfig.h : 97 - auto_clear:1
[I] I2SConfig.h : 104 - buffer_count:10
[I] I2SConfig.h : 105 - buffer_size:512
[I] I2SConfig.h : 109 - pin_bck: 14
[I] I2SConfig.h : 110 - pin_ws: 15
[I] I2SConfig.h : 111 - pin_data: 22
pschatzmann commented 2 years ago

I committed some corrections in both libraries.

For the time being only a sampling rate of 16000 with a mono signal is supported. If you try other settings you will need to adjust the fragment_size and timer_period. But I noticed that the UDP of the ESP32 is just not fast enough to handle more and the only way around this limitation is to use CODECs.

Please note that I have not released this project yet, so it is purely experimental!