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
+49
View File
@@ -33,6 +33,9 @@ Every feature — user-facing or significant internal mechanism — is listed he
| F-021 | RelayHandler v1/v2 dual-format compatibility | v2.0.3 | Stable |
| F-022 | Player speed guard (zero-speed defaults to 300 ms) | v2.0.3 | Stable |
| F-023 | Telemetry bell max load default raised to 500 | v2.0.3 | Stable |
| F-024 | V1 playback segment_duration fix (single-segment mode) | v2.0.4 | Stable |
| F-025 | V2 playback command namespace (`playback.play` / `playback.stop`) | v2.0.4 | Stable |
| F-026 | V2 playback `mode` field (`single` / `timed` / `interval` / `infinite`) | v2.0.4 | Stable |
---
@@ -241,6 +244,52 @@ Subsystem: `src/Player/Player.cpp:setMelodyAttributes()`
---
### F-024 — V1 playback segment_duration fix (single-segment mode)
When `continuous_loop: false` and `segment_duration > 0`, the melody now plays for the full `segment_duration` milliseconds (completing the last loop in progress) before stopping. Previously, sending `total_duration: 0` explicitly caused the player to stop after the very first loop regardless of `segment_duration`.
**Root cause:** `setMelodyAttributes` only derived `total_duration` from `segment_duration` when `total_duration` was *absent* from the payload. An explicit `total_duration: 0` bypassed the derivation, leaving the stop timer at zero. `onMelodyLoopCompleted` then hit the "play-once" shortcut and stopped immediately.
**Fix:** When `continuous_loop` is false, `total_duration` is now always overwritten with `segment_duration` regardless of what the client sent — it is semantically meaningless in single-segment mode. The play-once fallback in `onMelodyLoopCompleted` is also tightened to only fire when *both* `segment_duration` and `total_duration` are zero (truly no duration configured).
Subsystem: `src/Player/Player.cpp:setMelodyAttributes()`, `Player.cpp:onMelodyLoopCompleted()`
---
### F-025 — V2 playback command namespace
V2 clients send `playback.play` and `playback.stop` as the `cmd` field (with `"v": 2`), consistent with all other V2 namespaced commands (`relay.set_durations`, `system.restart`, etc.).
`PlaybackHandler` now registers these two commands directly. The old root-level `"playback"` command no longer exists in V2.
**Legacy (V1) compatibility:** `LegacyAdapter` maps `{ cmd:"playback", action:"play" }` → `"playback.play"` and `{ cmd:"playback", action:"stop" }` → `"playback.stop"`. All deployed V1 clients continue to work without any changes.
Subsystem: `src/Communication/Handlers/PlaybackHandler/PlaybackHandler.cpp`, `src/Communication/CommandBus/LegacyAdapter.cpp`
---
### F-026 — V2 playback `mode` field
V2 play commands replace the ambiguous `continuous_loop` / `segment_duration` / `total_duration` triplet with a single `mode` string and a `duration` field:
| `mode` | Behaviour | Required fields |
|---|---|---|
| `"single"` | Play exactly one melody loop, then stop | none |
| `"timed"` | Loop continuously for `duration` ms, stop at end of a full loop | `duration` |
| `"interval"` | Loop for `duration` ms, pause for `pause_duration` ms, repeat until `total_duration` ms elapsed | `duration`, `pause_duration`, `total_duration` |
| `"infinite"` | Loop forever until a `stop` command | none |
V1 timing fields (`segment_duration`, `continuous_loop`, `total_duration`) are still parsed when `mode` is absent, so the fix from F-024 continues to apply to V1 clients.
**Example V2 play command:**
```json
{ "cmd": "playback.play", "v": 2, "contents": { "pid": "builtin_festive_4voices", "speed": 232, "mode": "timed", "duration": 30000, "note_assignments": [1,2,3,4,0,0,0,0,0,0,0,0,0,0,0,0], "url": "..." } }
```
Subsystem: `src/Player/Player.cpp:setMelodyAttributes()`
---
### F-023 — Telemetry bell max load default raised to 500
`bellMaxLoad[i]` initialised to `500` (was `200`). This is the denominator used to calculate bell load percentage in telemetry reports. The old value caused false "overload" warnings on normal use patterns.
@@ -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;