Open
Description
I am using sendBIN() to send audio data steam from a ESP32 client to a nodejs server. The audio data is captured through a i2s microphone. I create a xTask to keep watching on the audio buffer and send until it reaches a certain length (chunk size was set to 2000 byte in my case). But the server accidentally throwing error "Invalid WebSocket frame: RSV2 and RSV3 must be clear".
Here is the code of my xTask:
void AudioRecorder::_processAudioBuffTask(void* self)
{
AudioRecorder* recorder = static_cast<AudioRecorder*>(self);
uint8_t* chunk = (uint8_t*)malloc(SEND_CHUNK_SIZE);
if (chunk == NULL) {
recorder->print("Failed to allocate memory for chunk");
vTaskDelete(NULL); // Exit task if memory allocation fails
return;
}
while (true) {
Serial.println("Process Task running...");
if (recorder->_audio_buff.size() > SEND_CHUNK_SIZE && recorder->_audioDataComingCallback) {
for (int i = 0; i < SEND_CHUNK_SIZE; i++) {
chunk[i] = recorder->_audio_buff.shift();
}
recorder->_audioDataComingCallback(chunk, SEND_CHUNK_SIZE); //Send the data
}
// Handle remaining data in shutdown mode
if(recorder->shutdownInProgress && recorder->_audio_buff.size() <= SEND_CHUNK_SIZE && recorder->_audioDataComingCallback){
uint16_t size_left = recorder->_audio_buff.size();
chunk = (uint8_t*)realloc(chunk, size_left);
if (chunk == NULL) {
recorder->print("Failed to allocate memory for chunk");
break;
}
// uint8_t chunk[size_left]; // Adjust chunk size to match remaining buffer size
for (size_t i = 0; i < size_left; i++) {
chunk[i] = recorder->_audio_buff.shift();
}
recorder->_audioDataComingCallback(chunk, size_left);
break;
}
vTaskDelay(40 / portTICK_PERIOD_MS); // Adjust as needed
}
recorder->shutdownInProgress = false;
free(chunk);
recorder->print("Process Task Completed");
if(recorder->_stopProcessTaskCallback != NULL)
recorder->_stopProcessTaskCallback(); // Send ending audio signal to the server
recorder->_processAudioBuffTaskHandle = NULL;
vTaskDelete(NULL); // Delete task when done
}
where the _audioDataComingCallback function simple:
void sendAudioChunk(uint8_t* data, size_t size) {
if(webSocketsClient.isConnected()){
webSocketsClient.sendBIN(data, size);
webSocketsClient.loop();
}else{
Serial.println("Web socket server not connected.");
stopRecording();
}
}
I doubt if this is caused by the call of webSocketsClient.loop() since it is called in the main thread loop. I have add webSocketsClient.loop() right after I call sendBIN(), but it won't solved the problem. Can anyone help me on that?
Metadata
Metadata
Assignees
Labels
No labels