From 9f33f90ea93324e0d0b0b4179d4768326faae20a Mon Sep 17 00:00:00 2001 From: bonamin Date: Tue, 7 Jul 2026 10:30:05 +0300 Subject: [PATCH] feat(Player): V2 playback command namespace + segment_duration fix + mode field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- docs/features/feature-catalog.md | 49 ++++++++++++++ .../CommandBus/LegacyAdapter.cpp | 5 +- .../PlaybackHandler/PlaybackHandler.cpp | 22 ++++++- .../PlaybackHandler/PlaybackHandler.hpp | 3 + src/Player/Player.cpp | 64 ++++++++++++++++--- 5 files changed, 131 insertions(+), 12 deletions(-) diff --git a/docs/features/feature-catalog.md b/docs/features/feature-catalog.md index 4521adb..dcf3640 100644 --- a/docs/features/feature-catalog.md +++ b/docs/features/feature-catalog.md @@ -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. diff --git a/src/Communication/CommandBus/LegacyAdapter.cpp b/src/Communication/CommandBus/LegacyAdapter.cpp index ece13ab..4d2cb78 100644 --- a/src/Communication/CommandBus/LegacyAdapter.cpp +++ b/src/Communication/CommandBus/LegacyAdapter.cpp @@ -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]); diff --git a/src/Communication/Handlers/PlaybackHandler/PlaybackHandler.cpp b/src/Communication/Handlers/PlaybackHandler/PlaybackHandler.cpp index beb1f6a..4bc412f 100644 --- a/src/Communication/Handlers/PlaybackHandler/PlaybackHandler.cpp +++ b/src/Communication/Handlers/PlaybackHandler/PlaybackHandler.cpp @@ -7,11 +7,29 @@ PlaybackHandler::PlaybackHandler(Player& player) : _player(player) {} std::vector PlaybackHandler::commands() const { - return { "playback" }; + return { "playback.play", "playback.stop" }; } CommandResult PlaybackHandler::execute(const CommandEnvelope& envelope) { - bool ok = _player.command(const_cast(envelope.contents).as()); + 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(envelope.contents); + contents["action"] = "play"; + bool ok = _player.command(contents.as()); + 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(envelope.contents); + contents["action"] = "stop"; + bool ok = _player.command(contents.as()); if (ok) return CommandResult::ok("playback", "Playback command executed"); return CommandResult::err("playback", "Playback command failed"); } diff --git a/src/Communication/Handlers/PlaybackHandler/PlaybackHandler.hpp b/src/Communication/Handlers/PlaybackHandler/PlaybackHandler.hpp index 415bcd6..a9cecac 100644 --- a/src/Communication/Handlers/PlaybackHandler/PlaybackHandler.hpp +++ b/src/Communication/Handlers/PlaybackHandler/PlaybackHandler.hpp @@ -14,4 +14,7 @@ public: private: Player& _player; + + CommandResult handlePlay(const CommandEnvelope& envelope); + CommandResult handleStop(const CommandEnvelope& envelope); }; diff --git a/src/Player/Player.cpp b/src/Player/Player.cpp index a96b2b8..25041e7 100644 --- a/src/Player/Player.cpp +++ b/src/Player/Player.cpp @@ -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(); } @@ -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(); + } // 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;