Repository navigation
YouTube: keep the media session in sync with playback - #844
Conversation
2986852 to
92c2f86
Compare
2b08567 to
3c70490
Compare
|
Review of the last two commits (car disconnect pause, new car screen takeover), plus notes on the rest.
Verified and not an issue: Not checked: the 3 s delayed seek/pause race in the takeover flow, and the last two commits have not been tested in the car. Reviewed by Claude |
The home feed autoplays muted previews in the main player, which were reported as playback and now show up as an active media session. Ignore muted videos outside /watch and /shorts, report a pause when playback turns into a preview, and report playing when an opened preview is unmuted.
Going back with Fermata stops the YouTube playback (goBack()), but YouTube's own back button navigates within the page, so the video kept playing muted and hidden as the home feed preview and the session kept reporting playback. Do the same as goBack() when the page navigates away from a video page.
The title was read from the page title when playback started, which YouTube updates only some time after a video is opened, so the media session could show "YouTube" until the next update. Use the player's own video data, falling back to the page title. The result is also decoded from the JSON that evaluateJavascript() returns, which quoted the title and showed its quotation marks escaped.
Exiting Fermata Auto from Android Auto's launcher destroys the YouTube page, but that page's playback engine stayed current, so the session kept reporting playback and play requests went to a page that no longer exists. Stop the playback when the page's activity is destroyed. Switching to another section only detaches the page, which is shown again when coming back, so that doesn't stop anything. Stopping no longer asks an engine without a source for its position, which a destroyed page may never answer.
A play request with no current playback and no last played item (for example Android Auto's autoplay after watching YouTube, whose items are never recorded as last played) did nothing, so the caller waited until it timed out. Report an error right away instead. A stopped external item, which can't be resumed from the service, no longer keeps its metadata, so the media card doesn't offer it.
Switching the car off doesn't always end the car screen or send a pause, so the playback carried on from the phone. Android Auto broadcasts the car connection changes, so pause when it goes from connected to not connected.
A car screen has no saved state to restore its page from, as a phone screen does. A car that's switched off doesn't always close Fermata's screen: when Android Auto started again, the new screen opened on YouTube's home while the previous page kept playing out of sight, until it was closed and stopped, and play then had nothing to resume. Take over the previous screen's video instead, at its position: playing or paused as it was if its page is still there, paused if the page is gone.
- Remember a closed page's video only for a car screen: a phone screen restores its own. - After reopening a video, seek and pause only if it's still this page's video. - Stop on leaving a video page only if the current engine is this page's. - Treat /live and /embed as video pages too, in both the Java and the JS check. - Query the car connection state off the main thread, as the Car App Library's CarConnection does.
3c70490 to
7325379
Compare
|
Thanks! Rebased on master and addressed in 7325379:
3 s seek/pause after a takeover: it could seek and pause another video if one was opened within those 3 s. It now acts only if the current engine is the new page's ( Car testing: the disconnect pause and the takeover work in my car (switching off mid-video paused it, and reopening Fermata brought the video back). They haven't had extensive use yet. |
Reopening a video sought and paused it after a fixed 3 s. A page that took longer to load, as in a car that's just been started, started the video after that and kept playing it, and YouTube could start it muted: without the audio focus, the previously playing app carried on and kept the media card. The video now opens at its position with YouTube's start time parameter, and it's paused and unmuted as soon as it starts playing. The same applies to restoring the page from its saved state.
A new car screen reopened the last video however long ago it was left, e.g. the next time the car was started. It now does so only within a set number of minutes, 10 by default and up to 60, and opens YouTube's home otherwise; 0 never reopens it. The time counts from when the video was paused, or from when its screen was closed if it was still playing.
With YouTube playing in Fermata Auto, the media session often didn't reflect what was on screen: cards for videos nobody was watching, cards left behind after leaving a video, and Android Auto's autoplay waiting on Fermata for nothing. Seven commits, each fixing one cause, plus one addressing the review (the inactive session during playback was fixed separately by #849, so it's no longer part of this PR):
/watchand/shortsis now treated as a preview; it's reported once it's unmuted on a video page.goBack()already did.document.title, which YouTube updates some time after opening a video, so the card could say "YouTube". It now comes from the player'sgetVideoData(), falling back to the page title, and is decoded from the JSON thatevaluateJavascript()returns, which had quoted the title and shown its quotation marks escaped.CarConnectionuses) and pauses when the car goes from connected to not connected. This pauses whatever is playing, music included, as other media apps do.Tested on a OnePlus 12 (Android 16) with Android Auto 17.7, on the phone and in the car. Checked: media card and AA media bar during playback, no card from feed previews, the card going away on every exit path (YouTube's back button, Fermata's back, exiting Fermata Auto from AA's launcher), the right title from the start, and AA's autoplay no longer hanging on Fermata. Session state was checked with
logcat -s MediaSessionService. The car disconnect pause and the video takeover are also tested in the car, though not extensively yet. The last commit addresses the review.