Skip to content

Commit 27d8535

Browse files
Ky1eYangyangkaiyue
andauthored
修改send packet时直接获取时间戳为队列获取时间戳 (#628)
* 修改实时aec传输时间戳改队列方式 * 删/注释化一下log代码, pr --------- Co-authored-by: yangkaiyue <[email protected]>
1 parent 7ceeffc commit 27d8535

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

main/application.cc

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,25 @@ void Application::Start() {
531531
opus_encoder_->Encode(std::move(data), [this](std::vector<uint8_t>&& opus) {
532532
AudioStreamPacket packet;
533533
packet.payload = std::move(opus);
534-
packet.timestamp = last_output_timestamp_;
535-
last_output_timestamp_ = 0;
536-
Schedule([this, packet = std::move(packet)]() {
534+
uint32_t last_output_timestamp_value = last_output_timestamp_.load();
535+
{
536+
std::lock_guard<std::mutex> lock(timestamp_mutex_);
537+
if (!timestamp_queue_.empty()) {
538+
packet.timestamp = timestamp_queue_.front();
539+
timestamp_queue_.pop_front();
540+
} else {
541+
packet.timestamp = 0;
542+
}
543+
544+
if (timestamp_queue_.size() > 3) { // 限制队列长度3
545+
timestamp_queue_.pop_front(); // 该包发送前先出队保持队列长度
546+
return;
547+
}
548+
}
549+
Schedule([this, last_output_timestamp_value, packet = std::move(packet)]() {
537550
protocol_->SendAudio(packet);
551+
// ESP_LOGI(TAG, "Send %zu bytes, timestamp %lu, last_ts %lu, qsize %zu",
552+
// packet.payload.size(), packet.timestamp, last_output_timestamp_value, timestamp_queue_.size());
538553
});
539554
});
540555
});
@@ -717,7 +732,11 @@ void Application::OnAudioOutput() {
717732
pcm = std::move(resampled);
718733
}
719734
codec->OutputData(pcm);
720-
last_output_timestamp_ = packet.timestamp;
735+
{
736+
std::lock_guard<std::mutex> lock(timestamp_mutex_);
737+
timestamp_queue_.push_back(packet.timestamp);
738+
last_output_timestamp_ = packet.timestamp;
739+
}
721740
last_output_time_ = std::chrono::steady_clock::now();
722741
});
723742
}
@@ -816,6 +835,7 @@ void Application::SetDeviceState(DeviceState state) {
816835
display->SetStatus(Lang::Strings::STANDBY);
817836
display->SetEmotion("neutral");
818837
audio_processor_->Stop();
838+
819839
#if CONFIG_USE_WAKE_WORD_DETECT
820840
wake_word_detect_.StartDetection();
821841
#endif
@@ -824,11 +844,12 @@ void Application::SetDeviceState(DeviceState state) {
824844
display->SetStatus(Lang::Strings::CONNECTING);
825845
display->SetEmotion("neutral");
826846
display->SetChatMessage("system", "");
847+
timestamp_queue_.clear();
848+
last_output_timestamp_ = 0;
827849
break;
828850
case kDeviceStateListening:
829851
display->SetStatus(Lang::Strings::LISTENING);
830852
display->SetEmotion("neutral");
831-
832853
// Update the IoT states before sending the start listening command
833854
UpdateIotStates();
834855

@@ -870,7 +891,6 @@ void Application::ResetDecoder() {
870891
audio_decode_queue_.clear();
871892
audio_decode_cv_.notify_all();
872893
last_output_time_ = std::chrono::steady_clock::now();
873-
874894
auto codec = Board::GetInstance().GetAudioCodec();
875895
codec->EnableOutput(true);
876896
}

main/application.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,14 @@ class Application {
104104
TaskHandle_t audio_loop_task_handle_ = nullptr;
105105
BackgroundTask* background_task_ = nullptr;
106106
std::chrono::steady_clock::time_point last_output_time_;
107-
std::atomic<uint32_t> last_output_timestamp_ = 0;
108107
std::list<AudioStreamPacket> audio_decode_queue_;
109108
std::condition_variable audio_decode_cv_;
110109

110+
// 新增:用于维护音频包的timestamp队列
111+
std::list<uint32_t> timestamp_queue_;
112+
std::mutex timestamp_mutex_;
113+
std::atomic<uint32_t> last_output_timestamp_ = 0;
114+
111115
std::unique_ptr<OpusEncoderWrapper> opus_encoder_;
112116
std::unique_ptr<OpusDecoderWrapper> opus_decoder_;
113117

0 commit comments

Comments
 (0)