feat(Player): V2 playback command namespace + segment_duration fix + mode field

F-024 — V1 single-segment fix: when continuous_loop=false, total_duration is
now always overwritten with segment_duration regardless of what the client
sent. Sending total_duration:0 explicitly no longer bypasses the stop timer.
The play-once fallback in onMelodyLoopCompleted is tightened to only fire when
both segment_duration and total_duration are zero.

F-025 — V2 command namespace: PlaybackHandler now registers playback.play and
playback.stop instead of the old root-level playback command. LegacyAdapter
maps V1 { cmd:playback, action:play/stop } to the new V2 names so all deployed
clients keep working without changes.

F-026 — V2 mode field: setMelodyAttributes accepts a mode string (single /
timed / interval / infinite) with a duration field, replacing the ambiguous
continuous_loop + segment_duration + total_duration triplet. V1 timing fields
are still parsed when mode is absent.

Docs: feature-catalog.md entries F-024, F-025, F-026 added.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-07 10:30:05 +03:00
co-authored by Claude Sonnet 4.6
parent 7e8d91673f
commit 9f33f90ea9
5 changed files with 131 additions and 12 deletions
@@ -15,6 +15,10 @@ struct ActionEntry {
};
static const ActionEntry ACTION_MAP[] = {
// playback
{ "playback", "play", "playback.play" },
{ "playback", "stop", "playback.stop" },
// file_manager
{ "file_manager", "list_melodies", "files.list" },
{ "file_manager", "download_melody", "files.download" },
@@ -72,7 +76,6 @@ struct SimpleEntry {
static const SimpleEntry SIMPLE_MAP[] = {
{ "ping", "ping" },
{ "identify", "identify" },
{ "playback", "playback" },
};
static constexpr size_t SIMPLE_MAP_SIZE = sizeof(SIMPLE_MAP) / sizeof(SIMPLE_MAP[0]);
@@ -7,11 +7,29 @@
PlaybackHandler::PlaybackHandler(Player& player) : _player(player) {}
std::vector<String> PlaybackHandler::commands() const {
return { "playback" };
return { "playback.play", "playback.stop" };
}
CommandResult PlaybackHandler::execute(const CommandEnvelope& envelope) {
bool ok = _player.command(const_cast<JsonDocument&>(envelope.contents).as<JsonVariant>());
if (envelope.command == "playback.play") return handlePlay(envelope);
if (envelope.command == "playback.stop") return handleStop(envelope);
return CommandResult::err("playback", "Unknown playback command");
}
CommandResult PlaybackHandler::handlePlay(const CommandEnvelope& envelope) {
// Inject action so Player::command() takes the play branch regardless of
// whether it was set by the V1 adapter or arrived as a bare V2 play command.
JsonDocument& contents = const_cast<JsonDocument&>(envelope.contents);
contents["action"] = "play";
bool ok = _player.command(contents.as<JsonVariant>());
if (ok) return CommandResult::ok("playback", "Playback command executed");
return CommandResult::err("playback", "Playback command failed");
}
CommandResult PlaybackHandler::handleStop(const CommandEnvelope& envelope) {
JsonDocument& contents = const_cast<JsonDocument&>(envelope.contents);
contents["action"] = "stop";
bool ok = _player.command(contents.as<JsonVariant>());
if (ok) return CommandResult::ok("playback", "Playback command executed");
return CommandResult::err("playback", "Playback command failed");
}
@@ -14,4 +14,7 @@ public:
private:
Player& _player;
CommandResult handlePlay(const CommandEnvelope& envelope);
CommandResult handleStop(const CommandEnvelope& envelope);
};
+55 -9
View File
@@ -216,6 +216,10 @@ bool Player::command(JsonVariant data) {
return false;
}
if (action == "stop") {
return true; // already stopped — no-op
}
setMelodyAttributes(data);
if (action == "play") {
@@ -288,6 +292,44 @@ void Player::setMelodyAttributes(JsonVariant doc) {
noteAssignments[i] = noteArray[i];
}
}
// ── V2 mode field ──────────────────────────────────────────────────────────
// V2 clients send a "mode" string instead of the continuous_loop/total_duration
// tangle. Translate into the same internal fields so the rest of the logic
// is unchanged.
//
// "single" → one loop, no timer (segment_duration=0, continuous_loop=false)
// "timed" → loop for duration ms (segment_duration=duration, continuous_loop=false)
// "interval" → timed segments with pauses (continuous_loop=true, all three fields)
// "infinite" → loop forever (continuous_loop=true, total_duration=0)
if (doc.containsKey("mode")) {
const char* mode = doc["mode"] | "single";
uint32_t dur = doc["duration"] | 0;
if (strcmp(mode, "timed") == 0) {
segment_duration = dur;
pause_duration = 0;
total_duration = dur;
continuous_loop = false;
} else if (strcmp(mode, "interval") == 0) {
segment_duration = dur;
pause_duration = doc["pause_duration"] | 0;
total_duration = doc["total_duration"] | 0;
continuous_loop = true;
} else if (strcmp(mode, "infinite") == 0) {
segment_duration = 0;
pause_duration = 0;
total_duration = 0;
continuous_loop = true;
} else {
// "single" or unknown → one loop, no timer
segment_duration = 0;
pause_duration = 0;
total_duration = 0;
continuous_loop = false;
}
}
// ── V1 timing fields (only applied when "mode" is absent) ─────────────────
else {
if (doc.containsKey("segment_duration")) {
segment_duration = doc["segment_duration"].as<uint32_t>();
}
@@ -299,13 +341,17 @@ void Player::setMelodyAttributes(JsonVariant doc) {
}
// Default to false — missing key must not carry over a previous true value
continuous_loop = doc.containsKey("continuous_loop") && doc["continuous_loop"].as<bool>();
} // end V1 timing block
// If total_duration was not explicitly sent, derive it from segment_duration.
// If total_duration WAS sent, it takes precedence regardless of continuous_loop.
bool hasTotalDuration = doc.containsKey("total_duration");
bool hasSegmentDuration = doc.containsKey("segment_duration");
if (!hasTotalDuration && hasSegmentDuration) {
total_duration = segment_duration;
// V1 semantics fixups — only when "mode" was not present (V2 already set fields correctly).
if (!doc.containsKey("mode")) {
// continuous_loop=false → single-segment: total_duration drives the stop timer.
// continuous_loop=true → interval mode: derive total_duration if not explicit.
if (!continuous_loop) {
total_duration = segment_duration;
} else if (!doc.containsKey("total_duration") && doc.containsKey("segment_duration")) {
total_duration = segment_duration;
}
}
// Recalculate infinite_play based on final values
@@ -527,9 +573,9 @@ void Player::onMelodyLoopCompleted() {
return;
}
// Play-once mode: total_duration=0 with no continuous_loop → stop after first loop.
// We are already at the end of the loop, so go directly to STOPPED (not STOPPING).
if (isPlaying && !continuous_loop && total_duration == 0) {
// Play-once fallback: no duration set at all (segment_duration=0, total_duration=0,
// continuous_loop=false) → stop after the first loop completes.
if (isPlaying && !continuous_loop && total_duration == 0 && segment_duration == 0) {
LOG_DEBUG(TAG, "Plbck: Play-once complete — stopped");
if (_bellEngine) _bellEngine->stop();
isPlaying = false;