Add : RTSP audio/video with back channel, and auth
- cam_stream: shared JPEG feed for /pic, /stream and RTSP; RTSP server (port 554, TCP) with MJPEG video, G.711 mic audio and an ONVIF-style back channel to the speaker - cam_auth: RTSP Digest login and HTTP Basic / ?key= check on all endpoints - Mic task feeds HTTP and RTSP with a sticky channel choice
This commit is contained in:
+59
-5
@@ -4,9 +4,18 @@
|
||||
#include "driver/i2s_std.h"
|
||||
#include "esp_codec_dev.h"
|
||||
#include "esp_codec_dev_defaults.h"
|
||||
#include "audio.h"
|
||||
#include "cam_stream.h"
|
||||
#include "board.h"
|
||||
|
||||
#define I2S_MCLK GPIO_NUM_10
|
||||
#define I2S_BCLK GPIO_NUM_11
|
||||
#define I2S_WS GPIO_NUM_12
|
||||
#define I2S_DOUT GPIO_NUM_14 // ESP -> ES8311 DSDIN
|
||||
#define I2S_DIN GPIO_NUM_13 // ES7210 SDOUT -> ESP
|
||||
#define SR 16000
|
||||
|
||||
#define MIC_BOOST 4 // software gain, raise if still quiet
|
||||
|
||||
static StreamBufferHandle_t s_play_sb; // mono 16-bit PCM from the browser
|
||||
|
||||
static esp_codec_dev_handle_t spk_dev, mic_dev;
|
||||
@@ -18,22 +27,51 @@ static i2s_chan_handle_t rx_chan = NULL;
|
||||
static StreamBufferHandle_t s_mic_sb;
|
||||
static volatile bool s_mic_on = false;
|
||||
|
||||
#define MIC_BOOST 4
|
||||
// void mic_task(void *arg)
|
||||
// {
|
||||
// static int16_t st[512]; // 256 stereo frames
|
||||
// static int16_t mono[256];
|
||||
// while (1) {
|
||||
// if (esp_codec_dev_read(mic_dev, st, sizeof(st)) != ESP_CODEC_DEV_OK) {
|
||||
// vTaskDelay(pdMS_TO_TICKS(10));
|
||||
// continue;
|
||||
// }
|
||||
// if (!s_mic_on) continue; // nobody listening, just drain
|
||||
|
||||
// long eL = 0, eR = 0;
|
||||
// for (int i = 0; i < 256; i++) { eL += abs(st[2*i]); eR += abs(st[2*i+1]); }
|
||||
// int useRight = eR > eL;
|
||||
|
||||
// for (int i = 0; i < 256; i++) {
|
||||
// int v = (useRight ? st[2*i+1] : st[2*i]) * MIC_BOOST;
|
||||
// if (v > 32767) v = 32767;
|
||||
// if (v < -32768) v = -32768;
|
||||
// mono[i] = (int16_t)v;
|
||||
// }
|
||||
// xStreamBufferSend(s_mic_sb, mono, sizeof(mono), 0); // drops if full
|
||||
// }
|
||||
// }
|
||||
|
||||
void mic_task(void *arg)
|
||||
{
|
||||
static int16_t st[512]; // 256 stereo frames
|
||||
static int16_t mono[256];
|
||||
long aL = 0, aR = 0;
|
||||
int useRight = 0;
|
||||
|
||||
while (1) {
|
||||
if (esp_codec_dev_read(mic_dev, st, sizeof(st)) != ESP_CODEC_DEV_OK) {
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
continue;
|
||||
}
|
||||
if (!s_mic_on) continue; // nobody listening, just drain
|
||||
if (!s_mic_on && !cam_audio_wanted()) continue; // nobody listening
|
||||
|
||||
long eL = 0, eR = 0;
|
||||
for (int i = 0; i < 256; i++) { eL += abs(st[2*i]); eR += abs(st[2*i+1]); }
|
||||
int useRight = eR > eL;
|
||||
aL += (eL - aL) / 16; // smoothed level per channel
|
||||
aR += (eR - aR) / 16;
|
||||
if (!useRight && aR > 2 * aL) useRight = 1; // switch only on a clear difference
|
||||
else if (useRight && aL > 2 * aR) useRight = 0;
|
||||
|
||||
for (int i = 0; i < 256; i++) {
|
||||
int v = (useRight ? st[2*i+1] : st[2*i]) * MIC_BOOST;
|
||||
@@ -41,7 +79,10 @@ void mic_task(void *arg)
|
||||
if (v < -32768) v = -32768;
|
||||
mono[i] = (int16_t)v;
|
||||
}
|
||||
xStreamBufferSend(s_mic_sb, mono, sizeof(mono), 0); // drops if full
|
||||
|
||||
if (s_mic_on && xStreamBufferSpacesAvailable(s_mic_sb) >= sizeof(mono))
|
||||
xStreamBufferSend(s_mic_sb, mono, sizeof(mono), 0);
|
||||
cam_audio_push(mono, 256); // RTSP sessions
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +99,12 @@ static void play_task(void *arg)
|
||||
esp_codec_dev_write(spk_dev, st, n * 4);
|
||||
}
|
||||
}
|
||||
void rtsp_speaker_sink(const int16_t *pcm, size_t samples)
|
||||
{
|
||||
size_t bytes = samples * sizeof(int16_t);
|
||||
if (xStreamBufferSpacesAvailable(s_play_sb) >= bytes)
|
||||
xStreamBufferSend(s_play_sb, pcm, bytes, 0); // full: drop, keeps delay bounded
|
||||
}
|
||||
|
||||
esp_err_t audio_init(i2c_master_bus_handle_t bus)
|
||||
{
|
||||
@@ -147,6 +194,8 @@ static const uint8_t WAV_HDR[44] = {
|
||||
static esp_err_t mic_handler(httpd_req_t *req)
|
||||
{
|
||||
static int16_t buf[256];
|
||||
if (!cam_http_check(req)) return ESP_OK;
|
||||
|
||||
cors(req);
|
||||
httpd_resp_set_type(req, "audio/wav");
|
||||
xStreamBufferReset(s_mic_sb);
|
||||
@@ -169,6 +218,9 @@ static esp_err_t mic_handler(httpd_req_t *req)
|
||||
static esp_err_t spk_handler(httpd_req_t *req)
|
||||
{
|
||||
static uint8_t body[4096];
|
||||
|
||||
if (!cam_http_check(req)) return ESP_FAIL;;
|
||||
|
||||
cors(req);
|
||||
size_t total = req->content_len;
|
||||
if (total == 0 || total > sizeof(body)) {
|
||||
@@ -193,6 +245,8 @@ static esp_err_t spk_handler(httpd_req_t *req)
|
||||
static esp_err_t vol_handler(httpd_req_t *req)
|
||||
{
|
||||
char q[32], val[8];
|
||||
if (!cam_http_check(req)) return ESP_OK;
|
||||
|
||||
cors(req);
|
||||
if (httpd_req_get_url_query_str(req, q, sizeof(q)) == ESP_OK &&
|
||||
httpd_query_key_value(q, "v", val, sizeof(val)) == ESP_OK) {
|
||||
|
||||
Reference in New Issue
Block a user