Skip to content

Commit d1c5862

Browse files
committed
fix CPU usage of esp32c3 with ml307
1 parent f2f54ba commit d1c5862

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

main/application.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,18 @@ void Application::Start() {
410410
auto codec = board.GetAudioCodec();
411411
opus_decoder_ = std::make_unique<OpusDecoderWrapper>(codec->output_sample_rate(), 1, OPUS_FRAME_DURATION_MS);
412412
opus_encoder_ = std::make_unique<OpusEncoderWrapper>(16000, 1, OPUS_FRAME_DURATION_MS);
413+
opus_encoder_->SetComplexity(0);
413414
if (aec_mode_ != kAecOff) {
414415
ESP_LOGI(TAG, "AEC mode: %d, setting opus encoder complexity to 0", aec_mode_);
415416
opus_encoder_->SetComplexity(0);
416-
} else if (board.GetBoardType() == "ml307") {
417-
ESP_LOGI(TAG, "ML307 board detected, setting opus encoder complexity to 5");
418-
opus_encoder_->SetComplexity(5);
419417
} else {
420-
ESP_LOGI(TAG, "WiFi board detected, setting opus encoder complexity to 0");
418+
#if CONFIG_USE_AUDIO_PROCESSOR
419+
ESP_LOGI(TAG, "Audio processor detected, setting opus encoder complexity to 5");
420+
opus_encoder_->SetComplexity(5);
421+
#else
422+
ESP_LOGI(TAG, "Audio processor not detected, setting opus encoder complexity to 0");
421423
opus_encoder_->SetComplexity(0);
424+
#endif
422425
}
423426

424427
if (codec->input_sample_rate() != 16000) {
@@ -826,7 +829,7 @@ void Application::OnAudioOutput() {
826829
SetDecodeSampleRate(packet.sample_rate, packet.frame_duration);
827830

828831
busy_decoding_audio_ = true;
829-
background_task_->Schedule([this, codec, packet = std::move(packet)]() mutable {
832+
if (!background_task_->Schedule([this, codec, packet = std::move(packet)]() mutable {
830833
busy_decoding_audio_ = false;
831834
if (aborted_) {
832835
return;
@@ -849,7 +852,9 @@ void Application::OnAudioOutput() {
849852
timestamp_queue_.push_back(packet.timestamp);
850853
#endif
851854
last_output_time_ = std::chrono::steady_clock::now();
852-
});
855+
})) {
856+
busy_decoding_audio_ = false;
857+
}
853858
}
854859

855860
void Application::OnAudioInput() {

main/background_task.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ BackgroundTask::~BackgroundTask() {
1818
}
1919
}
2020

21-
void BackgroundTask::Schedule(std::function<void()> callback) {
21+
bool BackgroundTask::Schedule(std::function<void()> callback) {
2222
std::lock_guard<std::mutex> lock(mutex_);
23+
if (waiting_for_completion_ > 0) {
24+
return false;
25+
}
2326
if (active_tasks_ >= 30) {
2427
int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
2528
if (free_sram < 10000) {
26-
ESP_LOGW(TAG, "active_tasks_ == %u, free_sram == %u", active_tasks_.load(), free_sram);
29+
ESP_LOGW(TAG, "active_tasks_ == %d, free_sram == %u", active_tasks_, free_sram);
30+
return false;
2731
}
2832
}
2933
active_tasks_++;
@@ -38,13 +42,16 @@ void BackgroundTask::Schedule(std::function<void()> callback) {
3842
}
3943
});
4044
condition_variable_.notify_all();
45+
return true;
4146
}
4247

4348
void BackgroundTask::WaitForCompletion() {
4449
std::unique_lock<std::mutex> lock(mutex_);
50+
waiting_for_completion_++;
4551
condition_variable_.wait(lock, [this]() {
4652
return background_tasks_.empty() && active_tasks_ == 0;
4753
});
54+
waiting_for_completion_--;
4855
}
4956

5057
void BackgroundTask::BackgroundTaskLoop() {

main/background_task.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ class BackgroundTask {
1313
BackgroundTask(uint32_t stack_size = 4096 * 2);
1414
~BackgroundTask();
1515

16-
void Schedule(std::function<void()> callback);
16+
bool Schedule(std::function<void()> callback);
1717
void WaitForCompletion();
1818

1919
private:
2020
std::mutex mutex_;
2121
std::list<std::function<void()>> background_tasks_;
2222
std::condition_variable condition_variable_;
2323
TaskHandle_t background_task_handle_ = nullptr;
24-
std::atomic<size_t> active_tasks_{0};
24+
int active_tasks_ = 0;
25+
int waiting_for_completion_ = 0;
2526

2627
void BackgroundTaskLoop();
2728
};

main/boards/common/ml307_board.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ void Ml307Board::WaitForNetworkReady() {
4646
auto& application = Application::GetInstance();
4747
auto display = Board::GetInstance().GetDisplay();
4848
display->SetStatus(Lang::Strings::REGISTERING_NETWORK);
49-
int result = modem_.WaitForNetworkReady();
50-
if (result == -1) {
51-
application.Alert(Lang::Strings::ERROR, Lang::Strings::PIN_ERROR, "sad", Lang::Sounds::P3_ERR_PIN);
52-
return;
53-
} else if (result == -2) {
54-
application.Alert(Lang::Strings::ERROR, Lang::Strings::REG_ERROR, "sad", Lang::Sounds::P3_ERR_REG);
55-
return;
49+
50+
while (true) {
51+
int result = modem_.WaitForNetworkReady();
52+
if (result == -1) {
53+
application.Alert(Lang::Strings::ERROR, Lang::Strings::PIN_ERROR, "sad", Lang::Sounds::P3_ERR_PIN);
54+
} else if (result == -2) {
55+
application.Alert(Lang::Strings::ERROR, Lang::Strings::REG_ERROR, "sad", Lang::Sounds::P3_ERR_REG);
56+
} else {
57+
break;
58+
}
59+
vTaskDelay(pdMS_TO_TICKS(10000));
5660
}
5761

5862
// Print the ML307 modem information

0 commit comments

Comments
 (0)