core icon indicating copy to clipboard operation
core copied to clipboard

Google Cast: selecting another track goes silent – stale metadata reload is retried after "Load cancelled" and overrides the new track

Open timanders22 opened this issue 3 weeks ago • 4 comments

Version: 4.0.0-beta.22 (Docker image ghcr.io/sonn-audio/core@sha256:f3fb20d1…). The code in question is unchanged in v4.0.0-beta.23 and on main (src/adapters/outputs/googleCast/googleCastOutput.ts, loadStream()).

Output: Google Cast (Google Home speaker), two zones, local library tracks.

What happens

  1. A track from the local library plays on a Google Cast zone.
  2. In the Loxone app, tap a different track of the same album (audio/1/library/play/…).
  3. The app shows the new track as playing, but the speaker is silent.
  4. Pause and then Play make the new track audible.

Reproduced twice in a row, in both zones each time (4 of 4 cases behaved identically).

What the log shows (debug log, one zone, times UTC)

22:06:12.564  command=audio/1/library/play/library:local:track:…           command received
22:06:12.686  contentId=…/streams/1/1-e6a5….flac?prime=0  elapsed=22 token=5  Google Cast load start   <- OLD stream, from updateMetadata()
22:06:12.768  action=play zoneId=1                                         dispatchOutputs
22:06:12.806  contentId=…/streams/1/1-7b5c….mp3          elapsed=0  token=6  Google Cast load start   <- NEW stream, from play()
22:06:12.988  idleReason=INTERRUPTED state=IDLE                            Google Cast status
22:06:12.992  contentId=…/1-e6a5….flac?prime=0  message="Load cancelled"   Google Cast load error   <- token 5 cancelled by the receiver (correct)
22:06:13.005  durationMs=196                                               Google Cast stream loaded  (token 6)
22:06:13.006  token=6                                                      Google Cast load finished
22:06:13.076  durationMs=79                                                Google Cast stream loaded  (token 5, second loadMedia attempt)
22:06:13.077  token=5                                                      Google Cast load finished  <- OLD stream lands last
22:06:15.111  idleReason=ERROR state=IDLE                                  Google Cast status         <- silence
22:06:23.425  message="Invalid request"                                    cast pause failed

The other three cases look the same (token pairs 2/3 and 4/5 at 22:05:46, 8/9 at 22:06:12): the older token always finishes after the newer one, and the receiver ends in IDLE / ERROR about two seconds later. The next Play then fails to resume ("Invalid request") and a fresh load finally plays the new track.

Cause (from reading the code)

  • On a track change the zone first pushes the new metadata while the session still carries the old stream URL. updateMetadata() sees a new signature and calls loadStream() with the old stream (?prime=0, because the old session started more than 2.5 s ago).
  • About 100 ms later play() calls loadStream() for the new stream. The signatures differ, so both loads run concurrently; loadToken is only used to clear loadPromise.
  • The receiver cancels the older load ("Load cancelled"). loadStream() treats that like any failed load and calls loadMedia() a second time with the same, now stale, media. That retry reaches the receiver after the new load and replaces it with a stream whose source has already ended.

Suggested fix

In loadStream(), give up on a superseded load instead of retrying it, and do not record its signature:

      let loaded = await this.loadMedia(media, { autoplay: true, currentTime: 0 });
      if (!loaded && this.loadToken !== token) {
        return; // a newer load has started; retrying this one would replace it on the receiver
      }
      if (!loaded) {
        // existing reconnect + retry
      }
      if (!loaded || this.loadToken !== token) {
        return;
      }

Possibly the metadata reload should also not fire for a session whose stream is about to be replaced, but the guard above is enough to stop the stale load from winning. Not tested yet – I have not patched the container. I am happy to test a build or a patch on the same two speakers, or to open a small PR if you prefer.

Probably affected the same way (not measured): next/previous track and moving within a playlist, since they go through the same updateMetadata() → play() sequence. Radio should not be affected, as updateMetadata() skips reloads for live streams.

timanders22 avatar Nov 08 '26 22:11 timanders22

Thanks, your analysis was spot on. Fixed on dev, in the next beta: a load that a newer one has overtaken is no longer retried or recorded, so the old stream can't land on top of the new one. A related fix is in as well: the receiver's "Load cancelled" was taken as the speaker refusing FLAC, which switched the device to MP3 for every later session (that's why your new track was an .mp3). A cancellation no longer counts as a refusal.

rudyberends avatar Nov 11 '26 16:11 rudyberends

Thanks a lot for the quick fix, and for the explanation of the MP3 fallback – that explains the .mp3 in my log. I will test beta.24 on the same two Google Cast zones and report back here; if it behaves, I will close the issue.

timanders22 avatar Nov 22 '26 10:11 timanders22

This fix is in beta.24, so I'm closing this. If it still happens on beta.24 or later, just leave a comment here and I'll reopen it.

rudyberends avatar Nov 28 '26 18:11 rudyberends

Thanks for closing it. beta.24 is running here (image 7a99016f) on the same two Google Cast zones; if switching tracks goes silent again, I'll comment here.

timanders22 avatar Nov 28 '26 21:11 timanders22