@@ -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}
0 commit comments