@@ -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
584592void 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);
0 commit comments