Skip to content

Commit f76f31a

Browse files
author
Xiaoxia
committed
Prevent too many opus packets in queue
1 parent eac5830 commit f76f31a

File tree

7 files changed

+33
-2
lines changed

7 files changed

+33
-2
lines changed

main/application.cc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,11 @@ void Application::Start() {
362362
Alert(Lang::Strings::ERROR, message.c_str(), "sad", Lang::Sounds::P3_EXCLAMATION);
363363
});
364364
protocol_->OnIncomingAudio([this](std::vector<uint8_t>&& data) {
365+
const int max_packets_in_queue = 300 / OPUS_FRAME_DURATION_MS;
365366
std::lock_guard<std::mutex> lock(mutex_);
366-
audio_decode_queue_.emplace_back(std::move(data));
367+
if (audio_decode_queue_.size() < max_packets_in_queue) {
368+
audio_decode_queue_.emplace_back(std::move(data));
369+
}
367370
});
368371
protocol_->OnAudioChannelOpened([this, codec, &board]() {
369372
board.SetPowerSaveMode(false);
@@ -451,6 +454,9 @@ void Application::Start() {
451454
audio_processor_.Initialize(codec, realtime_chat_enabled_);
452455
audio_processor_.OnOutput([this](std::vector<int16_t>&& data) {
453456
background_task_->Schedule([this, data = std::move(data)]() mutable {
457+
if (protocol_->IsAudioChannelBusy()) {
458+
return;
459+
}
454460
opus_encoder_->Encode(std::move(data), [this](std::vector<uint8_t>&& opus) {
455461
Schedule([this, opus = std::move(opus)]() {
456462
protocol_->SendAudio(opus);
@@ -524,6 +530,8 @@ void Application::OnClockTimer() {
524530

525531
// Print the debug info every 10 seconds
526532
if (clock_ticks_ % 10 == 0) {
533+
// SystemInfo::PrintRealTimeStats(pdMS_TO_TICKS(1000));
534+
527535
int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
528536
int min_free_sram = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
529537
ESP_LOGI(TAG, "Free internal: %u minimal internal: %u", free_sram, min_free_sram);
@@ -582,6 +590,10 @@ void Application::AudioLoop() {
582590
}
583591

584592
void Application::OnAudioOutput() {
593+
if (busy_decoding_audio_) {
594+
return;
595+
}
596+
585597
auto now = std::chrono::steady_clock::now();
586598
auto codec = Board::GetInstance().GetAudioCodec();
587599
const int max_silence_seconds = 10;
@@ -609,7 +621,9 @@ void Application::OnAudioOutput() {
609621
lock.unlock();
610622
audio_decode_cv_.notify_all();
611623

624+
busy_decoding_audio_ = true;
612625
background_task_->Schedule([this, codec, opus = std::move(opus)]() mutable {
626+
busy_decoding_audio_ = false;
613627
if (aborted_) {
614628
return;
615629
}
@@ -651,6 +665,9 @@ void Application::OnAudioInput() {
651665
std::vector<int16_t> data;
652666
ReadAudio(data, 16000, 30 * 16000 / 1000);
653667
background_task_->Schedule([this, data = std::move(data)]() mutable {
668+
if (protocol_->IsAudioChannelBusy()) {
669+
return;
670+
}
654671
opus_encoder_->Encode(std::move(data), [this](std::vector<uint8_t>&& opus) {
655672
Schedule([this, opus = std::move(opus)]() {
656673
protocol_->SendAudio(opus);

main/application.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class Application {
9999
#endif
100100
bool aborted_ = false;
101101
bool voice_detected_ = false;
102+
bool busy_decoding_audio_ = false;
102103
int clock_ticks_ = 0;
103104
TaskHandle_t check_new_version_task_handle_ = nullptr;
104105

main/display/display.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Display {
6767
class DisplayLockGuard {
6868
public:
6969
DisplayLockGuard(Display *display) : display_(display) {
70-
if (!display_->Lock(3000)) {
70+
if (!display_->Lock(30000)) {
7171
ESP_LOGE("Display", "Failed to lock display");
7272
}
7373
}

main/protocols/mqtt_protocol.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ void MqttProtocol::SendAudio(const std::vector<uint8_t>& data) {
133133
ESP_LOGE(TAG, "Failed to encrypt audio data");
134134
return;
135135
}
136+
137+
busy_sending_audio_ = true;
136138
udp_->Send(encrypted);
139+
busy_sending_audio_ = false;
137140
}
138141

139142
void MqttProtocol::CloseAudioChannel() {
@@ -164,6 +167,7 @@ bool MqttProtocol::OpenAudioChannel() {
164167
}
165168
}
166169

170+
busy_sending_audio_ = false;
167171
error_occurred_ = false;
168172
session_id_ = "";
169173
xEventGroupClearBits(event_group_handle_, MQTT_PROTOCOL_SERVER_HELLO_EVENT);

main/protocols/protocol.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,7 @@ bool Protocol::IsTimeout() const {
126126
return timeout;
127127
}
128128

129+
bool Protocol::IsAudioChannelBusy() const {
130+
return busy_sending_audio_;
131+
}
132+

main/protocols/protocol.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Protocol {
4848
virtual bool OpenAudioChannel() = 0;
4949
virtual void CloseAudioChannel() = 0;
5050
virtual bool IsAudioChannelOpened() const = 0;
51+
virtual bool IsAudioChannelBusy() const;
5152
virtual void SendAudio(const std::vector<uint8_t>& data) = 0;
5253
virtual void SendWakeWordDetected(const std::string& wake_word);
5354
virtual void SendStartListening(ListeningMode mode);
@@ -66,6 +67,7 @@ class Protocol {
6667
int server_sample_rate_ = 24000;
6768
int server_frame_duration_ = 60;
6869
bool error_occurred_ = false;
70+
bool busy_sending_audio_ = false;
6971
std::string session_id_;
7072
std::chrono::time_point<std::chrono::steady_clock> last_incoming_time_;
7173

main/protocols/websocket_protocol.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ void WebsocketProtocol::SendAudio(const std::vector<uint8_t>& data) {
3030
return;
3131
}
3232

33+
busy_sending_audio_ = true;
3334
websocket_->Send(data.data(), data.size(), true);
35+
busy_sending_audio_ = false;
3436
}
3537

3638
bool WebsocketProtocol::SendText(const std::string& text) {
@@ -63,6 +65,7 @@ bool WebsocketProtocol::OpenAudioChannel() {
6365
delete websocket_;
6466
}
6567

68+
busy_sending_audio_ = false;
6669
error_occurred_ = false;
6770
std::string url = CONFIG_WEBSOCKET_URL;
6871
std::string token = "Bearer " + std::string(CONFIG_WEBSOCKET_ACCESS_TOKEN);

0 commit comments

Comments
 (0)