Skip to content

fix(linux): alert sounds leave no media player in the media controls (#120) - #139

Open
mhasant19 wants to merge 1 commit into
karem505:masterfrom
mhasant19:fix/issue-120-media-player
Open

mhasant19 wants to merge 1 commit into
karem505:masterfrom
mhasant19:fix/issue-120-media-player

Conversation

@mhasant19

Copy link
Copy Markdown
Contributor

Fixes #120. This replaces #124, which did not work in the real app; the reason is below.

Problem

On Linux, GNOME's media controls keep a whatRust media player after WhatsApp plays an alert, until whatRust quits. It shows as "Paused" (greyed out), or stuck at "Playing" while nothing is playing.

Cause

Two WebKitGTK behaviours, and how WhatsApp makes its alert sounds:

  1. WebKitGTK registers a page's audio as an MPRIS media player and removes it only when the element's document goes away.
  2. Once anything has played, an <audio> that merely loads registers a player too, and every player then reports "Playing" for good.
  3. WhatsApp creates its alert sounds once with new Audio(url) and keeps them:
    • WAWebNotificationTone: an .mp3, 2.3 s.
    • WAWebOutgoingMessageTone: the 40 ms send_message .m4a. It is created when that module loads, even with the outgoing sound turned off.

So the first alert leaves a player, and the .m4a then pins one at "Playing".

#124 failed because it only acted on play(). It let these elements load, and the .m4a load alone was enough to bring back a stuck player.

Reproduced on the real web.whatsapp.com in a bare WebKitGTK 2.52 view, calling WhatsApp's own tone modules:

After the sound Media player
nothing done stays, "Paused"
pause(), muted, currentTime = 0 stays
src removed + load() (what v0.7.0 does) stuck at "Playing"
MediaSession feature turned off stays
the sound played from an iframe that is then removed gone

Fix

New section 7 in media.js (Linux only):

  • The page's alert elements (https:/data: <audio> in the top page, including new Audio(url)) keep their URL, but the engine never gets it, so they never load. The page still reads back its own src.
  • Each play() sounds from a copy in a hidden iframe. The iframe is removed when the copy ends, errors or is paused.
  • The page's element still gets play, playing, ended, pause and error, so WhatsApp's own listeners run as before. A new alert restarts the sound.
  • Voice messages (blob:) and calls (srcObject) keep the engine's own playback, so section 5 handles them as before.
  • If the copy is refused, the element gets its URL and plays natively, so a sound is never lost (the leftover player comes back for it).

Section 7 runs after sections 5 and 6, so section 5's idle release never sees an alert element that has media.

Results

On the real page (not logged in): a notification, then loading the outgoing-tone module, then a second notification.

v0.7.1 (master) #124 this PR
after the notification "Paused" gone none
after the .m4a loads 2 × "Playing" "Playing" none
after the 2nd notification stays gone none

The notification still sounds: WhatsApp gets ended after 2.3 s, the sound's real length.

Tests

  • media-audio.test.mjs: 7 new tests. 6 fail on master; the 7th checks that nothing changes inside an iframe.
  • All Node tests in check.yml (on Node 24), cargo fmt --check, cargo clippy -D warnings, and cargo test (287 passed).

Not covered

  • Voice messages: a voice message still leaves a player after it ends. Section 5's idle release (src removed + load()) turns it from "Paused" into "Playing". The next alert removes it. Copying voice messages like alerts would break their seek bar and progress, so I left them alone. Worth deciding how section 5 should handle this.
  • Not yet confirmed in a logged-in session with real incoming messages. With this change, whatrust.log shows media: alert sounds play from throwaway frames. If the copy is refused there, it shows alert sound copy refused.

…arem505#120)

On Linux, GNOME's media controls kept a whatRust media player after
WhatsApp played an alert, until whatRust quit: "Paused" (greyed out), or
stuck at "Playing" when nothing was playing at all.

WebKitGTK registers a page's audio as an MPRIS media player and removes
it only when the element's document goes away. And once anything has
played, an <audio> that merely loads registers a player too, after which
every player reports "Playing" for good. WhatsApp creates its alert
sounds once with new Audio(url) and keeps them: WAWebNotificationTone
(an .mp3) and WAWebOutgoingMessageTone (the 40 ms send_message .m4a,
created when that module loads, even with the sound turned off). So the
first alert left a player, and the .m4a then pinned a "Playing" one.

Reproduced on the real web.whatsapp.com in a bare WebKitGTK 2.52 view.
Pausing, muting, clearing src, or turning off the MediaSession feature
does not remove the player. Playing from an element in an iframe that
is then removed does.

media.js section 7: the page's alert elements (https:/data: <audio> in
the top page, including new Audio(url)) keep their URL, but the engine
never gets it, so they never load. Each play() sounds from a copy in a
hidden iframe, removed when the copy ends, errors or is paused. The
page's element gets play, playing, ended, pause and error as before, and
reads back its src. A new alert restarts the sound. Voice messages
(blob:) and calls (srcObject) keep the engine's playback. If the copy is
refused, the element gets its URL and plays natively, so a sound is
never lost.

On the real page (not logged in), after a notification, loading the
outgoing tone module and a second notification: unchanged, the player
is "Paused" and then two players are stuck at "Playing"; with this
change no player remains.

Not covered: a voice message still leaves a player after it ends, which
section 5's idle release turns into "Playing". The next alert removes it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Ubuntu 26:04] Ubuntu Gnome control center always has a WhatRust media player.

1 participant