The audio moves to plain HTTP, with no WebSocket.
This commit is contained in:
+1
-1
@@ -1,3 +1,3 @@
|
||||
idf_component_register(SRCS "take_picture.c" "esp32_s3_cam_ovxxxx.c" "camera.c" "audio.c" "wifi.c"
|
||||
idf_component_register(SRCS "main.c" "esp32_s3_cam_ovxxxx.c" "camera.c" "audio.c" "wifi.c"
|
||||
PRIV_INCLUDE_DIRS .
|
||||
REQUIRES nvs_flash esp32-camera esp_io_expander custom_io_expander_ch32v003 esp_jpeg)
|
||||
+215
-98
@@ -23,129 +23,158 @@ static esp_codec_dev_handle_t spk_dev, mic_dev;
|
||||
|
||||
static i2s_chan_handle_t tx_chan = NULL;
|
||||
static i2s_chan_handle_t rx_chan = NULL;
|
||||
static volatile int s_audio_fd = -1;
|
||||
static httpd_handle_t s_audio_hd = NULL;
|
||||
// static volatile int s_audio_fd = -1;
|
||||
// static httpd_handle_t s_audio_hd = NULL;
|
||||
static StreamBufferHandle_t s_mic_sb;
|
||||
static volatile bool s_mic_on = false;
|
||||
|
||||
esp_err_t audio_ws_handler(httpd_req_t *req)
|
||||
{
|
||||
if (req->method == HTTP_GET) { // WebSocket handshake
|
||||
s_audio_hd = req->handle;
|
||||
s_audio_fd = httpd_req_to_sockfd(req);
|
||||
ESP_LOGI("AUDIO", "WS client connected, fd=%d", s_audio_fd);
|
||||
return ESP_OK;
|
||||
}
|
||||
// esp_err_t audio_ws_handler(httpd_req_t *req)
|
||||
// {
|
||||
// if (req->method == HTTP_GET) { // WebSocket handshake
|
||||
// s_audio_hd = req->handle;
|
||||
// s_audio_fd = httpd_req_to_sockfd(req);
|
||||
// ESP_LOGI("AUDIO", "WS client connected, fd=%d", s_audio_fd);
|
||||
// return ESP_OK;
|
||||
// }
|
||||
|
||||
if (req->method == HTTP_GET) {
|
||||
s_audio_hd = req->handle;
|
||||
s_audio_fd = httpd_req_to_sockfd(req);
|
||||
int one = 1;
|
||||
setsockopt(s_audio_fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
||||
return ESP_OK;
|
||||
}
|
||||
// if (req->method == HTTP_GET) {
|
||||
// s_audio_hd = req->handle;
|
||||
// s_audio_fd = httpd_req_to_sockfd(req);
|
||||
// int one = 1;
|
||||
// setsockopt(s_audio_fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
||||
// return ESP_OK;
|
||||
// }
|
||||
|
||||
static int16_t in[2048]; // mono from browser (max 4096 bytes)
|
||||
// static int16_t st[4096]; // stereo for the codec
|
||||
// static int16_t in[2048]; // mono from browser (max 4096 bytes)
|
||||
// // static int16_t st[4096]; // stereo for the codec
|
||||
|
||||
httpd_ws_frame_t pkt = {0};
|
||||
esp_err_t ret = httpd_ws_recv_frame(req, &pkt, 0);
|
||||
if (ret != ESP_OK) return ret;
|
||||
if (pkt.type == HTTPD_WS_TYPE_CLOSE) { s_audio_fd = -1; return ESP_OK; }
|
||||
// httpd_ws_frame_t pkt = {0};
|
||||
// esp_err_t ret = httpd_ws_recv_frame(req, &pkt, 0);
|
||||
// if (ret != ESP_OK) return ret;
|
||||
// if (pkt.type == HTTPD_WS_TYPE_CLOSE) { s_audio_fd = -1; return ESP_OK; }
|
||||
|
||||
if (pkt.type == HTTPD_WS_TYPE_TEXT) { // control messages
|
||||
char txt[16] = {0};
|
||||
if (pkt.len >= sizeof(txt)) return ESP_FAIL;
|
||||
pkt.payload = (uint8_t *)txt;
|
||||
ret = httpd_ws_recv_frame(req, &pkt, pkt.len);
|
||||
if (ret != ESP_OK) return ret;
|
||||
int v;
|
||||
if (sscanf(txt, "VOL:%d", &v) == 1) {
|
||||
if (v < 0) v = 0;
|
||||
if (v > 100) v = 100;
|
||||
esp_codec_dev_set_out_vol(spk_dev, v);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
// if (pkt.type == HTTPD_WS_TYPE_TEXT) { // control messages
|
||||
// char txt[16] = {0};
|
||||
// if (pkt.len >= sizeof(txt)) return ESP_FAIL;
|
||||
// pkt.payload = (uint8_t *)txt;
|
||||
// ret = httpd_ws_recv_frame(req, &pkt, pkt.len);
|
||||
// if (ret != ESP_OK) return ret;
|
||||
// int v;
|
||||
// if (sscanf(txt, "VOL:%d", &v) == 1) {
|
||||
// if (v < 0) v = 0;
|
||||
// if (v > 100) v = 100;
|
||||
// esp_codec_dev_set_out_vol(spk_dev, v);
|
||||
// }
|
||||
// return ESP_OK;
|
||||
// }
|
||||
|
||||
if (pkt.len == 0 || pkt.len > sizeof(in)) return ESP_FAIL;
|
||||
// if (pkt.len == 0 || pkt.len > sizeof(in)) return ESP_FAIL;
|
||||
|
||||
pkt.payload = (uint8_t *)in;
|
||||
ret = httpd_ws_recv_frame(req, &pkt, pkt.len);
|
||||
if (ret != ESP_OK) return ret;
|
||||
// pkt.payload = (uint8_t *)in;
|
||||
// ret = httpd_ws_recv_frame(req, &pkt, pkt.len);
|
||||
// if (ret != ESP_OK) return ret;
|
||||
|
||||
int n = pkt.len / 2;
|
||||
size_t len = pkt.len & ~1u;
|
||||
if (xStreamBufferSpacesAvailable(s_play_sb) >= len)
|
||||
xStreamBufferSend(s_play_sb, in, len, 0); // never blocks
|
||||
// else: buffer is full, drop this packet to keep latency bounded
|
||||
return ESP_OK;
|
||||
// esp_codec_dev_write(spk_dev, st, n * 4);
|
||||
// return ESP_OK;
|
||||
}
|
||||
// int n = pkt.len / 2;
|
||||
// size_t len = pkt.len & ~1u;
|
||||
// if (xStreamBufferSpacesAvailable(s_play_sb) >= len)
|
||||
// xStreamBufferSend(s_play_sb, in, len, 0); // never blocks
|
||||
// // else: buffer is full, drop this packet to keep latency bounded
|
||||
// return ESP_OK;
|
||||
// // esp_codec_dev_write(spk_dev, st, n * 4);
|
||||
// // return ESP_OK;
|
||||
// }
|
||||
|
||||
httpd_handle_t start_audio_server(void) {
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.server_port = 81;
|
||||
config.ctrl_port = 32769;
|
||||
config.max_open_sockets = 3;
|
||||
// httpd_handle_t start_audio_server(void) {
|
||||
// httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
// config.server_port = 81;
|
||||
// config.ctrl_port = 32769;
|
||||
// config.max_open_sockets = 3;
|
||||
|
||||
httpd_handle_t audio_server = NULL;
|
||||
if (httpd_start(&audio_server, &config) == ESP_OK) {
|
||||
httpd_uri_t uri_audio_ws = {
|
||||
.uri = "/audio",
|
||||
.method = HTTP_GET,
|
||||
.handler = audio_ws_handler,
|
||||
.is_websocket = true
|
||||
};
|
||||
httpd_register_uri_handler(audio_server, &uri_audio_ws);
|
||||
ESP_LOGI("AUDIO", "Audio WebSocket Server started on port 81");
|
||||
}
|
||||
return audio_server;
|
||||
}
|
||||
// httpd_handle_t audio_server = NULL;
|
||||
// if (httpd_start(&audio_server, &config) == ESP_OK) {
|
||||
// httpd_uri_t uri_audio_ws = {
|
||||
// .uri = "/audio",
|
||||
// .method = HTTP_GET,
|
||||
// .handler = audio_ws_handler,
|
||||
// .is_websocket = true
|
||||
// };
|
||||
// httpd_register_uri_handler(audio_server, &uri_audio_ws);
|
||||
// ESP_LOGI("AUDIO", "Audio WebSocket Server started on port 81");
|
||||
// }
|
||||
// return audio_server;
|
||||
// }
|
||||
|
||||
#define MIC_BOOST 4
|
||||
|
||||
void mic_task(void *arg)
|
||||
{
|
||||
static int16_t st[512]; // 512 stereo frames
|
||||
static int16_t st[512]; // 256 stereo frames
|
||||
static int16_t mono[256];
|
||||
int blocks = 0, sendFail = 0, pkL = 0, pkR = 0;
|
||||
|
||||
while (1) {
|
||||
int rd = esp_codec_dev_read(mic_dev, st, sizeof(st));
|
||||
if (rd != ESP_CODEC_DEV_OK) {
|
||||
ESP_LOGE("MIC", "read error %d", rd);
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
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++) {
|
||||
int l = abs(st[2*i]), r = abs(st[2*i+1]);
|
||||
eL += l; eR += r;
|
||||
if (l > pkL) pkL = l;
|
||||
if (r > pkR) pkR = r;
|
||||
}
|
||||
for (int i = 0; i < 256; i++) { eL += abs(st[2*i]); eR += abs(st[2*i+1]); }
|
||||
int useRight = eR > eL;
|
||||
|
||||
if (s_audio_fd >= 0) {
|
||||
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;
|
||||
}
|
||||
httpd_ws_frame_t f = { .payload = (uint8_t *)mono, .len = sizeof(mono),
|
||||
.type = HTTPD_WS_TYPE_BINARY };
|
||||
if (httpd_ws_send_frame_async(s_audio_hd, s_audio_fd, &f) != ESP_OK)
|
||||
sendFail++;
|
||||
}
|
||||
|
||||
if (++blocks >= 62) { // about once per second
|
||||
ESP_LOGI("MIC", "peakL=%d peakR=%d client_fd=%d sendFail=%d",
|
||||
pkL, pkR, (int)s_audio_fd, sendFail);
|
||||
blocks = 0; pkL = pkR = 0; sendFail = 0;
|
||||
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]; // 512 stereo frames
|
||||
// static int16_t mono[256];
|
||||
// int blocks = 0, sendFail = 0, pkL = 0, pkR = 0;
|
||||
|
||||
// while (1) {
|
||||
// int rd = esp_codec_dev_read(mic_dev, st, sizeof(st));
|
||||
// if (rd != ESP_CODEC_DEV_OK) {
|
||||
// ESP_LOGE("MIC", "read error %d", rd);
|
||||
// vTaskDelay(pdMS_TO_TICKS(50));
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// long eL = 0, eR = 0;
|
||||
// for (int i = 0; i < 256; i++) {
|
||||
// int l = abs(st[2*i]), r = abs(st[2*i+1]);
|
||||
// eL += l; eR += r;
|
||||
// if (l > pkL) pkL = l;
|
||||
// if (r > pkR) pkR = r;
|
||||
// }
|
||||
// int useRight = eR > eL;
|
||||
|
||||
// if (s_audio_fd >= 0) {
|
||||
// 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;
|
||||
// }
|
||||
// httpd_ws_frame_t f = { .payload = (uint8_t *)mono, .len = sizeof(mono),
|
||||
// .type = HTTPD_WS_TYPE_BINARY };
|
||||
// if (httpd_ws_send_frame_async(s_audio_hd, s_audio_fd, &f) != ESP_OK)
|
||||
// sendFail++;
|
||||
// }
|
||||
|
||||
// if (++blocks >= 62) { // about once per second
|
||||
// ESP_LOGI("MIC", "peakL=%d peakR=%d client_fd=%d sendFail=%d",
|
||||
// pkL, pkR, (int)s_audio_fd, sendFail);
|
||||
// blocks = 0; pkL = pkR = 0; sendFail = 0;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
static void play_task(void *arg)
|
||||
{
|
||||
static int16_t mono[256];
|
||||
@@ -221,7 +250,95 @@ esp_err_t audio_init(i2c_master_bus_handle_t bus)
|
||||
esp_codec_dev_set_in_gain(mic_dev, 30.0);
|
||||
|
||||
s_play_sb = xStreamBufferCreate(4096, 1); // about 128 ms maximum queue
|
||||
s_mic_sb = xStreamBufferCreate(4096, 1);
|
||||
xTaskCreate(play_task, "play", 4096, NULL, 6, NULL);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void cors(httpd_req_t *req) {
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
httpd_resp_set_hdr(req, "Cache-Control", "no-store");
|
||||
}
|
||||
|
||||
// GET /mic : endless raw PCM stream (16 kHz, 16-bit, mono)
|
||||
static esp_err_t mic_handler(httpd_req_t *req)
|
||||
{
|
||||
static int16_t buf[256];
|
||||
cors(req);
|
||||
httpd_resp_set_type(req, "application/octet-stream");
|
||||
xStreamBufferReset(s_mic_sb);
|
||||
s_mic_on = true;
|
||||
|
||||
esp_err_t res = ESP_OK;
|
||||
while (1) {
|
||||
size_t n = xStreamBufferReceive(s_mic_sb, buf, sizeof(buf), pdMS_TO_TICKS(200));
|
||||
if (!n) continue;
|
||||
res = httpd_resp_send_chunk(req, (const char *)buf, n);
|
||||
if (res != ESP_OK) break; // client left
|
||||
}
|
||||
s_mic_on = false;
|
||||
return res;
|
||||
}
|
||||
|
||||
// POST /spk : body = mono 16-bit PCM
|
||||
static esp_err_t spk_handler(httpd_req_t *req)
|
||||
{
|
||||
static uint8_t body[4096];
|
||||
cors(req);
|
||||
size_t total = req->content_len;
|
||||
if (total == 0 || total > sizeof(body)) {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "bad length");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
size_t got = 0;
|
||||
while (got < total) {
|
||||
int r = httpd_req_recv(req, (char *)body + got, total - got);
|
||||
if (r <= 0) return ESP_FAIL;
|
||||
got += r;
|
||||
}
|
||||
size_t len = got & ~1u;
|
||||
if (xStreamBufferSpacesAvailable(s_play_sb) >= len)
|
||||
xStreamBufferSend(s_play_sb, body, len, 0); // else drop, keeps latency bounded
|
||||
|
||||
httpd_resp_set_status(req, "204 No Content");
|
||||
return httpd_resp_send(req, NULL, 0);
|
||||
}
|
||||
|
||||
// GET /vol?v=85
|
||||
static esp_err_t vol_handler(httpd_req_t *req)
|
||||
{
|
||||
char q[32], val[8];
|
||||
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) {
|
||||
int v = atoi(val);
|
||||
if (v < 0) v = 0;
|
||||
if (v > 100) v = 100;
|
||||
esp_codec_dev_set_out_vol(spk_dev, v);
|
||||
}
|
||||
httpd_resp_set_status(req, "204 No Content");
|
||||
return httpd_resp_send(req, NULL, 0);
|
||||
}
|
||||
|
||||
void start_audio_server(void)
|
||||
{
|
||||
httpd_handle_t h;
|
||||
|
||||
httpd_config_t c1 = HTTPD_DEFAULT_CONFIG();
|
||||
c1.server_port = 81; c1.ctrl_port = 32769; c1.stack_size = 6144;
|
||||
if (httpd_start(&h, &c1) == ESP_OK) {
|
||||
httpd_uri_t u = { .uri = "/mic", .method = HTTP_GET, .handler = mic_handler };
|
||||
httpd_register_uri_handler(h, &u);
|
||||
}
|
||||
|
||||
httpd_config_t c2 = HTTPD_DEFAULT_CONFIG();
|
||||
c2.server_port = 82; c2.ctrl_port = 32770; c2.stack_size = 6144;
|
||||
c2.lru_purge_enable = true;
|
||||
if (httpd_start(&h, &c2) == ESP_OK) {
|
||||
httpd_uri_t s = { .uri = "/spk", .method = HTTP_POST, .handler = spk_handler };
|
||||
httpd_uri_t v = { .uri = "/vol", .method = HTTP_GET, .handler = vol_handler };
|
||||
httpd_register_uri_handler(h, &s);
|
||||
httpd_register_uri_handler(h, &v);
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -15,5 +15,4 @@ esp_err_t init_camera(void);
|
||||
esp_err_t audio_init(i2c_master_bus_handle_t bus);
|
||||
void mic_task(void *arg);
|
||||
void start_wifi_and_server(void);
|
||||
httpd_handle_t start_audio_server(void);
|
||||
|
||||
void start_audio_server(void);
|
||||
|
||||
+1
-9
@@ -74,13 +74,5 @@ void start_wifi_and_server(void) {
|
||||
.user_ctx = NULL
|
||||
};
|
||||
httpd_register_uri_handler(server, &uri_stream);
|
||||
|
||||
httpd_uri_t uri_audio_ws = {
|
||||
.uri = "/audio",
|
||||
.method = HTTP_GET,
|
||||
.handler = audio_ws_handler,
|
||||
.is_websocket = true
|
||||
};
|
||||
httpd_register_uri_handler(server, &uri_audio_ws);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user