From d1c586239c9064c82b3162048cb8bee900446e0e Mon Sep 17 00:00:00 2001 From: Terrence Date: Sun, 29 Jun 2025 05:40:27 +0800 Subject: [PATCH] fix CPU usage of esp32c3 with ml307 --- main/application.cc | 17 +++++++++++------ main/background_task.cc | 11 +++++++++-- main/background_task.h | 5 +++-- main/boards/common/ml307_board.cc | 18 +++++++++++------- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/main/application.cc b/main/application.cc index b5cedd10..efbd464b 100644 --- a/main/application.cc +++ b/main/application.cc @@ -410,15 +410,18 @@ void Application::Start() { auto codec = board.GetAudioCodec(); opus_decoder_ = std::make_unique(codec->output_sample_rate(), 1, OPUS_FRAME_DURATION_MS); opus_encoder_ = std::make_unique(16000, 1, OPUS_FRAME_DURATION_MS); + opus_encoder_->SetComplexity(0); if (aec_mode_ != kAecOff) { ESP_LOGI(TAG, "AEC mode: %d, setting opus encoder complexity to 0", aec_mode_); opus_encoder_->SetComplexity(0); - } else if (board.GetBoardType() == "ml307") { - ESP_LOGI(TAG, "ML307 board detected, setting opus encoder complexity to 5"); - opus_encoder_->SetComplexity(5); } else { - ESP_LOGI(TAG, "WiFi board detected, setting opus encoder complexity to 0"); +#if CONFIG_USE_AUDIO_PROCESSOR + ESP_LOGI(TAG, "Audio processor detected, setting opus encoder complexity to 5"); + opus_encoder_->SetComplexity(5); +#else + ESP_LOGI(TAG, "Audio processor not detected, setting opus encoder complexity to 0"); opus_encoder_->SetComplexity(0); +#endif } if (codec->input_sample_rate() != 16000) { @@ -826,7 +829,7 @@ void Application::OnAudioOutput() { SetDecodeSampleRate(packet.sample_rate, packet.frame_duration); busy_decoding_audio_ = true; - background_task_->Schedule([this, codec, packet = std::move(packet)]() mutable { + if (!background_task_->Schedule([this, codec, packet = std::move(packet)]() mutable { busy_decoding_audio_ = false; if (aborted_) { return; @@ -849,7 +852,9 @@ void Application::OnAudioOutput() { timestamp_queue_.push_back(packet.timestamp); #endif last_output_time_ = std::chrono::steady_clock::now(); - }); + })) { + busy_decoding_audio_ = false; + } } void Application::OnAudioInput() { diff --git a/main/background_task.cc b/main/background_task.cc index ac90b754..48461164 100644 --- a/main/background_task.cc +++ b/main/background_task.cc @@ -18,12 +18,16 @@ BackgroundTask::~BackgroundTask() { } } -void BackgroundTask::Schedule(std::function callback) { +bool BackgroundTask::Schedule(std::function callback) { std::lock_guard lock(mutex_); + if (waiting_for_completion_ > 0) { + return false; + } if (active_tasks_ >= 30) { int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL); if (free_sram < 10000) { - ESP_LOGW(TAG, "active_tasks_ == %u, free_sram == %u", active_tasks_.load(), free_sram); + ESP_LOGW(TAG, "active_tasks_ == %d, free_sram == %u", active_tasks_, free_sram); + return false; } } active_tasks_++; @@ -38,13 +42,16 @@ void BackgroundTask::Schedule(std::function callback) { } }); condition_variable_.notify_all(); + return true; } void BackgroundTask::WaitForCompletion() { std::unique_lock lock(mutex_); + waiting_for_completion_++; condition_variable_.wait(lock, [this]() { return background_tasks_.empty() && active_tasks_ == 0; }); + waiting_for_completion_--; } void BackgroundTask::BackgroundTaskLoop() { diff --git a/main/background_task.h b/main/background_task.h index a843ed2b..659b5e33 100644 --- a/main/background_task.h +++ b/main/background_task.h @@ -13,7 +13,7 @@ public: BackgroundTask(uint32_t stack_size = 4096 * 2); ~BackgroundTask(); - void Schedule(std::function callback); + bool Schedule(std::function callback); void WaitForCompletion(); private: @@ -21,7 +21,8 @@ private: std::list> background_tasks_; std::condition_variable condition_variable_; TaskHandle_t background_task_handle_ = nullptr; - std::atomic active_tasks_{0}; + int active_tasks_ = 0; + int waiting_for_completion_ = 0; void BackgroundTaskLoop(); }; diff --git a/main/boards/common/ml307_board.cc b/main/boards/common/ml307_board.cc index 9211cf8d..247e6cb5 100644 --- a/main/boards/common/ml307_board.cc +++ b/main/boards/common/ml307_board.cc @@ -46,13 +46,17 @@ void Ml307Board::WaitForNetworkReady() { auto& application = Application::GetInstance(); auto display = Board::GetInstance().GetDisplay(); display->SetStatus(Lang::Strings::REGISTERING_NETWORK); - int result = modem_.WaitForNetworkReady(); - if (result == -1) { - application.Alert(Lang::Strings::ERROR, Lang::Strings::PIN_ERROR, "sad", Lang::Sounds::P3_ERR_PIN); - return; - } else if (result == -2) { - application.Alert(Lang::Strings::ERROR, Lang::Strings::REG_ERROR, "sad", Lang::Sounds::P3_ERR_REG); - return; + + while (true) { + int result = modem_.WaitForNetworkReady(); + if (result == -1) { + application.Alert(Lang::Strings::ERROR, Lang::Strings::PIN_ERROR, "sad", Lang::Sounds::P3_ERR_PIN); + } else if (result == -2) { + application.Alert(Lang::Strings::ERROR, Lang::Strings::REG_ERROR, "sad", Lang::Sounds::P3_ERR_REG); + } else { + break; + } + vTaskDelay(pdMS_TO_TICKS(10000)); } // Print the ML307 modem information