bolar_2 backup
This commit is contained in:
321
main/audio/playback_controller.cc
Normal file
321
main/audio/playback_controller.cc
Normal file
@@ -0,0 +1,321 @@
|
||||
#include "audio/playback_controller.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr PlaybackSessionId kLocalSessionIdBase = 0x80000000U;
|
||||
|
||||
bool IsRemoteSource(PlaybackSource source)
|
||||
{
|
||||
return source == PlaybackSource::kRemoteTts ||
|
||||
source == PlaybackSource::kRemoteMusic;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
PlaybackStartResult PlaybackController::StartSession(
|
||||
PlaybackSource source,
|
||||
PlaybackPriority priority,
|
||||
PlaybackSessionId requested_id,
|
||||
uint64_t now_ms)
|
||||
{
|
||||
if (IsRemoteSource(source) &&
|
||||
(requested_id == 0 || requested_id >= kLocalSessionIdBase)) {
|
||||
return {
|
||||
PlaybackStartStatus::kRejectedInvalidSessionId,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
}
|
||||
|
||||
if (session_.active && requested_id != 0 &&
|
||||
requested_id == session_.id && source == session_.source) {
|
||||
return {
|
||||
PlaybackStartStatus::kAlreadyActive,
|
||||
session_.id,
|
||||
0,
|
||||
session_.generation,
|
||||
};
|
||||
}
|
||||
|
||||
if (session_.active &&
|
||||
static_cast<uint8_t>(priority) <
|
||||
static_cast<uint8_t>(session_.priority)) {
|
||||
metrics_.lower_priority_rejections++;
|
||||
return {
|
||||
PlaybackStartStatus::kRejectedLowerPriority,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
}
|
||||
|
||||
PlaybackSessionId preempted = 0;
|
||||
if (session_.active) {
|
||||
preempted = session_.id;
|
||||
session_.state = PlaybackSessionState::kCancelled;
|
||||
session_.active = false;
|
||||
metrics_.sessions_cancelled++;
|
||||
metrics_.sessions_preempted++;
|
||||
}
|
||||
|
||||
session_ = {};
|
||||
session_.id = AllocateSessionId(requested_id);
|
||||
session_.generation = next_generation_++;
|
||||
if (next_generation_ == 0) {
|
||||
next_generation_ = 1;
|
||||
}
|
||||
session_.source = source;
|
||||
session_.priority = priority;
|
||||
session_.state = PlaybackSessionState::kPending;
|
||||
session_.active = true;
|
||||
started_at_ms_ = now_ms;
|
||||
last_progress_ms_ = now_ms;
|
||||
metrics_.sessions_started++;
|
||||
|
||||
return {
|
||||
PlaybackStartStatus::kStarted,
|
||||
session_.id,
|
||||
preempted,
|
||||
session_.generation,
|
||||
};
|
||||
}
|
||||
|
||||
bool PlaybackController::CanAcceptFrame(
|
||||
PlaybackSessionId session_id) const
|
||||
{
|
||||
return session_.active && session_id != 0 &&
|
||||
session_.id == session_id;
|
||||
}
|
||||
|
||||
bool PlaybackController::QueueFrame(
|
||||
PlaybackSessionId session_id,
|
||||
uint64_t now_ms)
|
||||
{
|
||||
if (!CanAcceptFrame(session_id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
session_.producer_started = true;
|
||||
session_.queued_frames++;
|
||||
session_.state = session_.input_ended
|
||||
? PlaybackSessionState::kDraining
|
||||
: PlaybackSessionState::kStreaming;
|
||||
metrics_.frames_queued++;
|
||||
metrics_.queue_high_watermark =
|
||||
std::max(metrics_.queue_high_watermark, session_.queued_frames);
|
||||
Touch(now_ms);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PlaybackController::RollbackQueuedFrame(
|
||||
PlaybackSessionId session_id)
|
||||
{
|
||||
if (!IsCurrent(session_id) || session_.queued_frames == 0) {
|
||||
return false;
|
||||
}
|
||||
session_.queued_frames--;
|
||||
metrics_.frames_dropped++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PlaybackController::BeginFrame(
|
||||
PlaybackSessionId session_id,
|
||||
uint64_t now_ms)
|
||||
{
|
||||
if (!CanAcceptFrame(session_id) || session_.queued_frames == 0) {
|
||||
return false;
|
||||
}
|
||||
session_.queued_frames--;
|
||||
session_.in_flight_frames++;
|
||||
Touch(now_ms);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PlaybackController::FinishFrame(
|
||||
PlaybackSessionId session_id,
|
||||
bool write_succeeded,
|
||||
uint64_t now_ms)
|
||||
{
|
||||
if (!CanAcceptFrame(session_id) ||
|
||||
session_.in_flight_frames == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
session_.in_flight_frames--;
|
||||
if (write_succeeded) {
|
||||
session_.written_frames++;
|
||||
metrics_.frames_written++;
|
||||
} else {
|
||||
metrics_.frames_dropped++;
|
||||
}
|
||||
Touch(now_ms);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PlaybackController::EndInput(
|
||||
PlaybackSessionId session_id,
|
||||
uint64_t now_ms)
|
||||
{
|
||||
if (!CanAcceptFrame(session_id)) {
|
||||
return false;
|
||||
}
|
||||
session_.input_ended = true;
|
||||
session_.state = PlaybackSessionState::kDraining;
|
||||
Touch(now_ms);
|
||||
return true;
|
||||
}
|
||||
|
||||
PlaybackSessionId PlaybackController::TryComplete()
|
||||
{
|
||||
if (!session_.active || !session_.producer_started ||
|
||||
!session_.input_ended || session_.queued_frames != 0 ||
|
||||
session_.in_flight_frames != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
session_.state = PlaybackSessionState::kCompleted;
|
||||
session_.active = false;
|
||||
metrics_.sessions_completed++;
|
||||
return session_.id;
|
||||
}
|
||||
|
||||
PlaybackSessionId PlaybackController::Cancel(
|
||||
PlaybackSessionId session_id)
|
||||
{
|
||||
if (!session_.active ||
|
||||
(session_id != 0 && session_id != session_.id)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
session_.state = PlaybackSessionState::kCancelled;
|
||||
session_.active = false;
|
||||
session_.queued_frames = 0;
|
||||
metrics_.sessions_cancelled++;
|
||||
return session_.id;
|
||||
}
|
||||
|
||||
PlaybackSessionId PlaybackController::Fail(
|
||||
PlaybackSessionId session_id)
|
||||
{
|
||||
if (!session_.active || session_id != session_.id) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
session_.state = PlaybackSessionState::kFailed;
|
||||
session_.active = false;
|
||||
session_.queued_frames = 0;
|
||||
metrics_.sessions_failed++;
|
||||
return session_.id;
|
||||
}
|
||||
|
||||
PlaybackTimeoutResult PlaybackController::CheckTimeout(
|
||||
uint64_t now_ms,
|
||||
uint32_t producer_start_timeout_ms,
|
||||
uint32_t stalled_timeout_ms)
|
||||
{
|
||||
if (!session_.active) {
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!session_.producer_started &&
|
||||
now_ms - started_at_ms_ >= producer_start_timeout_ms) {
|
||||
const PlaybackSessionId failed = Fail(session_.id);
|
||||
metrics_.producer_start_timeouts++;
|
||||
return {PlaybackTimeoutReason::kProducerStart, failed};
|
||||
}
|
||||
|
||||
if (session_.producer_started && stalled_timeout_ms > 0 &&
|
||||
now_ms - last_progress_ms_ >= stalled_timeout_ms) {
|
||||
const PlaybackSessionId failed = Fail(session_.id);
|
||||
metrics_.stalled_timeouts++;
|
||||
return {PlaybackTimeoutReason::kStalled, failed};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void PlaybackController::RecordStaleFrame()
|
||||
{
|
||||
metrics_.stale_frames_rejected++;
|
||||
metrics_.frames_dropped++;
|
||||
}
|
||||
|
||||
bool PlaybackController::IsCurrent(
|
||||
PlaybackSessionId session_id) const
|
||||
{
|
||||
return session_.active && session_.id == session_id;
|
||||
}
|
||||
|
||||
PlaybackSessionId PlaybackController::AllocateSessionId(
|
||||
PlaybackSessionId requested_id)
|
||||
{
|
||||
if (requested_id != 0) {
|
||||
return requested_id;
|
||||
}
|
||||
|
||||
const PlaybackSessionId allocated = next_session_id_;
|
||||
next_session_id_++;
|
||||
if (next_session_id_ < kLocalSessionIdBase) {
|
||||
next_session_id_ = kLocalSessionIdBase;
|
||||
}
|
||||
return allocated;
|
||||
}
|
||||
|
||||
void PlaybackController::Touch(uint64_t now_ms)
|
||||
{
|
||||
last_progress_ms_ = now_ms;
|
||||
}
|
||||
|
||||
const char* ToString(PlaybackSessionState state)
|
||||
{
|
||||
switch (state) {
|
||||
case PlaybackSessionState::kPending:
|
||||
return "pending";
|
||||
case PlaybackSessionState::kStreaming:
|
||||
return "streaming";
|
||||
case PlaybackSessionState::kDraining:
|
||||
return "draining";
|
||||
case PlaybackSessionState::kCancelled:
|
||||
return "cancelled";
|
||||
case PlaybackSessionState::kCompleted:
|
||||
return "completed";
|
||||
case PlaybackSessionState::kFailed:
|
||||
return "failed";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
const char* ToString(PlaybackStartStatus status)
|
||||
{
|
||||
switch (status) {
|
||||
case PlaybackStartStatus::kStarted:
|
||||
return "started";
|
||||
case PlaybackStartStatus::kAlreadyActive:
|
||||
return "already_active";
|
||||
case PlaybackStartStatus::kRejectedLowerPriority:
|
||||
return "rejected_lower_priority";
|
||||
case PlaybackStartStatus::kRejectedInvalidSessionId:
|
||||
return "rejected_invalid_session_id";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
const char* ToString(PlaybackTimeoutReason reason)
|
||||
{
|
||||
switch (reason) {
|
||||
case PlaybackTimeoutReason::kNone:
|
||||
return "none";
|
||||
case PlaybackTimeoutReason::kProducerStart:
|
||||
return "producer_start";
|
||||
case PlaybackTimeoutReason::kStalled:
|
||||
return "stalled";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user