fix CPU usage of esp32c3 with ml307

This commit is contained in:
Terrence
2025-06-29 05:40:27 +08:00
parent f2f54ba1fd
commit d1c586239c
4 changed files with 34 additions and 17 deletions

View File

@@ -410,15 +410,18 @@ void Application::Start() {
auto codec = board.GetAudioCodec();
opus_decoder_ = std::make_unique<OpusDecoderWrapper>(codec->output_sample_rate(), 1, OPUS_FRAME_DURATION_MS);
opus_encoder_ = std::make_unique<OpusEncoderWrapper>(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() {

View File

@@ -18,12 +18,16 @@ BackgroundTask::~BackgroundTask() {
}
}
void BackgroundTask::Schedule(std::function<void()> callback) {
bool BackgroundTask::Schedule(std::function<void()> callback) {
std::lock_guard<std::mutex> 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<void()> callback) {
}
});
condition_variable_.notify_all();
return true;
}
void BackgroundTask::WaitForCompletion() {
std::unique_lock<std::mutex> lock(mutex_);
waiting_for_completion_++;
condition_variable_.wait(lock, [this]() {
return background_tasks_.empty() && active_tasks_ == 0;
});
waiting_for_completion_--;
}
void BackgroundTask::BackgroundTaskLoop() {

View File

@@ -13,7 +13,7 @@ public:
BackgroundTask(uint32_t stack_size = 4096 * 2);
~BackgroundTask();
void Schedule(std::function<void()> callback);
bool Schedule(std::function<void()> callback);
void WaitForCompletion();
private:
@@ -21,7 +21,8 @@ private:
std::list<std::function<void()>> background_tasks_;
std::condition_variable condition_variable_;
TaskHandle_t background_task_handle_ = nullptr;
std::atomic<size_t> active_tasks_{0};
int active_tasks_ = 0;
int waiting_for_completion_ = 0;
void BackgroundTaskLoop();
};

View File

@@ -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