修改send packet时直接获取时间戳为队列获取时间戳 (#628)

* 修改实时aec传输时间戳改队列方式

* 删/注释化一下log代码, pr

---------

Co-authored-by: yangkaiyue <yangkaiyue1@tenclass.com>
This commit is contained in:
Ky1eYang
2025-05-16 18:46:20 +08:00
committed by GitHub
parent 7ceeffc157
commit 27d85356e4
2 changed files with 31 additions and 7 deletions

View File

@ -531,10 +531,25 @@ void Application::Start() {
opus_encoder_->Encode(std::move(data), [this](std::vector<uint8_t>&& opus) {
AudioStreamPacket packet;
packet.payload = std::move(opus);
packet.timestamp = last_output_timestamp_;
last_output_timestamp_ = 0;
Schedule([this, packet = std::move(packet)]() {
uint32_t last_output_timestamp_value = last_output_timestamp_.load();
{
std::lock_guard<std::mutex> lock(timestamp_mutex_);
if (!timestamp_queue_.empty()) {
packet.timestamp = timestamp_queue_.front();
timestamp_queue_.pop_front();
} else {
packet.timestamp = 0;
}
if (timestamp_queue_.size() > 3) { // 限制队列长度3
timestamp_queue_.pop_front(); // 该包发送前先出队保持队列长度
return;
}
}
Schedule([this, last_output_timestamp_value, packet = std::move(packet)]() {
protocol_->SendAudio(packet);
// ESP_LOGI(TAG, "Send %zu bytes, timestamp %lu, last_ts %lu, qsize %zu",
// packet.payload.size(), packet.timestamp, last_output_timestamp_value, timestamp_queue_.size());
});
});
});
@ -717,7 +732,11 @@ void Application::OnAudioOutput() {
pcm = std::move(resampled);
}
codec->OutputData(pcm);
{
std::lock_guard<std::mutex> lock(timestamp_mutex_);
timestamp_queue_.push_back(packet.timestamp);
last_output_timestamp_ = packet.timestamp;
}
last_output_time_ = std::chrono::steady_clock::now();
});
}
@ -816,6 +835,7 @@ void Application::SetDeviceState(DeviceState state) {
display->SetStatus(Lang::Strings::STANDBY);
display->SetEmotion("neutral");
audio_processor_->Stop();
#if CONFIG_USE_WAKE_WORD_DETECT
wake_word_detect_.StartDetection();
#endif
@ -824,11 +844,12 @@ void Application::SetDeviceState(DeviceState state) {
display->SetStatus(Lang::Strings::CONNECTING);
display->SetEmotion("neutral");
display->SetChatMessage("system", "");
timestamp_queue_.clear();
last_output_timestamp_ = 0;
break;
case kDeviceStateListening:
display->SetStatus(Lang::Strings::LISTENING);
display->SetEmotion("neutral");
// Update the IoT states before sending the start listening command
UpdateIotStates();
@ -870,7 +891,6 @@ void Application::ResetDecoder() {
audio_decode_queue_.clear();
audio_decode_cv_.notify_all();
last_output_time_ = std::chrono::steady_clock::now();
auto codec = Board::GetInstance().GetAudioCodec();
codec->EnableOutput(true);
}

View File

@ -104,10 +104,14 @@ private:
TaskHandle_t audio_loop_task_handle_ = nullptr;
BackgroundTask* background_task_ = nullptr;
std::chrono::steady_clock::time_point last_output_time_;
std::atomic<uint32_t> last_output_timestamp_ = 0;
std::list<AudioStreamPacket> audio_decode_queue_;
std::condition_variable audio_decode_cv_;
// 新增用于维护音频包的timestamp队列
std::list<uint32_t> timestamp_queue_;
std::mutex timestamp_mutex_;
std::atomic<uint32_t> last_output_timestamp_ = 0;
std::unique_ptr<OpusEncoderWrapper> opus_encoder_;
std::unique_ptr<OpusDecoderWrapper> opus_decoder_;