bolar_2 backup
This commit is contained in:
241
main/activation/activation_replay_service.cc
Normal file
241
main/activation/activation_replay_service.cc
Normal file
@@ -0,0 +1,241 @@
|
||||
#include "activation/activation_replay_service.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "ActivationReplay2";
|
||||
|
||||
} // namespace
|
||||
|
||||
bool ActivationReplayService::Initialize(IEventPublisher& publisher)
|
||||
{
|
||||
if (timer_ != nullptr) {
|
||||
publisher_ = &publisher;
|
||||
return true;
|
||||
}
|
||||
const esp_timer_create_args_t arguments = {
|
||||
.callback = &ActivationReplayService::TimerCallback,
|
||||
.arg = this,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "activation2",
|
||||
.skip_unhandled_events = true,
|
||||
};
|
||||
if (esp_timer_create(&arguments, &timer_) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
publisher_ = &publisher;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ActivationReplayService::StartLocalSequence(
|
||||
const LocalPromptSequence& sequence)
|
||||
{
|
||||
if (publisher_ == nullptr || sequence.Empty() ||
|
||||
sequence.count > kMaxLocalPromptSequenceItems) {
|
||||
return false;
|
||||
}
|
||||
uint32_t generation = 0;
|
||||
PlaybackSessionId previous_session = 0;
|
||||
bool repeating = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
esp_timer_stop(timer_);
|
||||
previous_session = current_session_;
|
||||
sequence_ = sequence;
|
||||
current_session_ = 0;
|
||||
repeating_ =
|
||||
sequence.Contains(LocalPrompt::kActivationDevice);
|
||||
repeating = repeating_;
|
||||
active_ = true;
|
||||
if (++generation_ == 0) {
|
||||
++generation_;
|
||||
}
|
||||
generation = generation_;
|
||||
}
|
||||
if (previous_session != 0) {
|
||||
audio_.CancelPlayback(previous_session);
|
||||
}
|
||||
if (!publisher_->Publish(AppEvent::Make(
|
||||
AppEventType::kLocalSequenceRequested,
|
||||
generation))) {
|
||||
StopLocalSequence();
|
||||
return false;
|
||||
}
|
||||
ESP_LOGI(kTag, "sequence queued items=%u repeat=%d generation=%lu",
|
||||
static_cast<unsigned>(sequence.count),
|
||||
repeating ? 1 : 0,
|
||||
static_cast<unsigned long>(generation));
|
||||
return true;
|
||||
}
|
||||
|
||||
void ActivationReplayService::StopLocalSequence()
|
||||
{
|
||||
StopWithReason("handler request");
|
||||
}
|
||||
|
||||
void ActivationReplayService::StopWithReason(const char* reason)
|
||||
{
|
||||
PlaybackSessionId session = 0;
|
||||
bool was_active = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
was_active = active_;
|
||||
StopLocked(&session);
|
||||
}
|
||||
if (session != 0) {
|
||||
audio_.CancelPlayback(session);
|
||||
}
|
||||
if (was_active) {
|
||||
ESP_LOGI(kTag, "replay stopped (%s)", reason);
|
||||
}
|
||||
}
|
||||
|
||||
void ActivationReplayService::OnApplicationEvent(
|
||||
const AppEvent& event,
|
||||
const RuntimeState& state)
|
||||
{
|
||||
if (event.type == AppEventType::kLocalSequenceRequested ||
|
||||
event.type == AppEventType::kActivationReplayDue) {
|
||||
PlayCurrent(state, event.correlation_id);
|
||||
return;
|
||||
}
|
||||
|
||||
PlaybackSessionId current_session = 0;
|
||||
bool active = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
current_session = current_session_;
|
||||
active = active_;
|
||||
}
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.type == AppEventType::kPlaybackDrained &&
|
||||
event.correlation_id == current_session) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
current_session_ = 0;
|
||||
if (active_ && repeating_ && CanPlay(state)) {
|
||||
esp_timer_stop(timer_);
|
||||
if (esp_timer_start_once(timer_, kReplayDelayUs) == ESP_OK) {
|
||||
ESP_LOGI(kTag, "next activation replay in 6000 ms");
|
||||
return;
|
||||
}
|
||||
}
|
||||
active_ = false;
|
||||
repeating_ = false;
|
||||
sequence_ = {};
|
||||
return;
|
||||
}
|
||||
|
||||
if ((event.type == AppEventType::kPlaybackFailed &&
|
||||
(event.correlation_id == 0 ||
|
||||
event.correlation_id == current_session)) ||
|
||||
(event.type == AppEventType::kPlaybackPreempted &&
|
||||
event.correlation_id == current_session) ||
|
||||
event.type == AppEventType::kTtsStarted ||
|
||||
event.type == AppEventType::kLocalPromptRequested ||
|
||||
event.type == AppEventType::kShutdownPromptRequested) {
|
||||
StopWithReason(ToString(event.type));
|
||||
} else if (!CanPlay(state)) {
|
||||
StopWithReason("state no longer allows playback");
|
||||
}
|
||||
}
|
||||
|
||||
void ActivationReplayService::TimerCallback(void* context)
|
||||
{
|
||||
static_cast<ActivationReplayService*>(context)->OnTimer();
|
||||
}
|
||||
|
||||
void ActivationReplayService::OnTimer()
|
||||
{
|
||||
IEventPublisher* publisher = nullptr;
|
||||
uint32_t generation = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!active_ || !repeating_) {
|
||||
return;
|
||||
}
|
||||
publisher = publisher_;
|
||||
generation = generation_;
|
||||
}
|
||||
if (publisher != nullptr) {
|
||||
publisher->Publish(AppEvent::Make(
|
||||
AppEventType::kActivationReplayDue,
|
||||
generation));
|
||||
}
|
||||
}
|
||||
|
||||
void ActivationReplayService::PlayCurrent(
|
||||
const RuntimeState& state,
|
||||
uint32_t generation)
|
||||
{
|
||||
LocalPromptSequence sequence;
|
||||
bool should_stop = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!active_ || generation != generation_) {
|
||||
return;
|
||||
}
|
||||
if (!CanPlay(state)) {
|
||||
should_stop = true;
|
||||
} else {
|
||||
sequence = sequence_;
|
||||
}
|
||||
}
|
||||
if (should_stop) {
|
||||
StopLocalSequence();
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(kTag, "replay cycle items=%u generation=%lu",
|
||||
static_cast<unsigned>(sequence.count),
|
||||
static_cast<unsigned long>(generation));
|
||||
const PlaybackSessionId session = audio_.PlayLocalSequence(
|
||||
sequence, PlaybackPriority::kLocalPrompt);
|
||||
bool cancel_stale_session = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!active_ || generation != generation_) {
|
||||
cancel_stale_session = session != 0;
|
||||
} else {
|
||||
current_session_ = session;
|
||||
if (session == 0) {
|
||||
active_ = false;
|
||||
repeating_ = false;
|
||||
sequence_ = {};
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cancel_stale_session) {
|
||||
audio_.CancelPlayback(session);
|
||||
}
|
||||
}
|
||||
|
||||
bool ActivationReplayService::CanPlay(
|
||||
const RuntimeState& state) const
|
||||
{
|
||||
return state.lifecycle == LifecycleState::kRunning &&
|
||||
state.realtime == RealtimeState::kOpen &&
|
||||
state.call == CallState::kInactive &&
|
||||
!state.friend_mode &&
|
||||
!state.alert_ringing &&
|
||||
!state.idle_sleeping;
|
||||
}
|
||||
|
||||
void ActivationReplayService::StopLocked(
|
||||
PlaybackSessionId* session_to_cancel)
|
||||
{
|
||||
esp_timer_stop(timer_);
|
||||
*session_to_cancel = current_session_;
|
||||
current_session_ = 0;
|
||||
active_ = false;
|
||||
repeating_ = false;
|
||||
sequence_ = {};
|
||||
if (++generation_ == 0) {
|
||||
++generation_;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user