Closed Bug 1659244 Opened 6 years ago Closed 6 years ago

AudioDestinationNode doesn't release its wake lock when it has become inaudible

Categories

(Core :: Web Audio, defect, P1)

Desktop
macOS
defect

Tracking

()

RESOLVED FIXED
83 Branch
Tracking Status
firefox-esr78 --- wontfix
firefox80 --- wontfix
firefox81 --- wontfix
firefox82 --- wontfix
firefox83 --- fixed

People

(Reporter: greenrecyclebin, Assigned: padenot)

References

(Blocks 1 open bug, Regression)

Details

(Keywords: regression)

Attachments

(1 file, 7 external files, 1 obsolete file)

7.52 KB, patch
Details | Diff | Splinter Review
  1. Prerequisites:

  2. Test scenario:

    • URL: https://flukeout.github.io/simple-sounds
    • Source code: https://github.com/flukeout/simple-sounds/blob/632a1cca9c9f221360adf0d1c2be33de08624fd1/sounds.js#L67-L69
    • Reproducible steps:
      1. Visit the URL
      2. Verify that there are no NoIdleSleepAssertions ($ pmset -g assertions | grep NoIdleSleepAssertion | grep firefox | wc -l)
      3. Click on any sound link
      4. Verify that there's one NoIdleSleepAssertion ($ pmset -g assertions | grep NoIdleSleepAssertion | grep firefox | wc -l)
        (This happens immediately on new AudioContext without the previous step in Firefox v79.0 regardless of the Autoplay setting value, but not at the specified tip.)
      5. Wait a few seconds
    • Expected behavior: there are no NoIdleSleepAssertions ($ pmset -g assertions | grep NoIdleSleepAssertion | grep firefox | wc -l)
    • Actual behavior: this assertion doesn't go away unless tab is refreshed or closed
  3. Real scenario:

    • URL: https://outlook.office.com/calendar
    • Reproducible steps:
      1. Visit the URL
      2. Create an event with a reminder that is going to fire soon
      3. Verify that there are no NoIdleSleepAssertions ($ pmset -g assertions | grep NoIdleSleepAssertion | grep firefox | wc -l)
      4. Wait until the reminder fires and the bell rings
      5. (Refresh the tab if nothing happens; the reminder will fire and the bell will ring)
      6. Verify that there's one NoIdleSleepAssertion ($ pmset -g assertions | grep NoIdleSleepAssertion | grep firefox | wc -l)
      7. Wait a few seconds
    • Expected behavior: there are no NoIdleSleepAssertions ($ pmset -g assertions | grep NoIdleSleepAssertion | grep firefox | wc -l)
    • Actual behavior: this assertion doesn't go away unless tab is refreshed or closed
  4. Observations

  5. Conclusion

    • Some websites use the Web Audio API to implement in-app notifications (instead of the Notification API).
    • These in-app notifications ring a short bell to catch the user's attention.
    • After playing the sound, there's no need to hold a wake lock.
    • (The author might not call suspend or close on the AudioContext.)
    • Since the browser can already detect when an audio stream has become inaudible, it should release the audio lock to allow the computer to sleep.
    • (If the audio stream becomes audible again, another audio lock will be acquired.)

I've attached a patch that produces the expected behavior in both scenarios. I'm not familiar with the code so I'm not sure if it's complete, and there are no tests yet. I'd appreciate some pointers. Thank you.

Thank you for your diagnosis, here.

I'm not familiar with the NotifyAudible() logic, but, yes, it sounds plausible to me that the wake lock could use the same logic.
Presumably that could replace the existing construction/destruction, suspend/resume logic.

Perhaps the test added in bug 1621684 could be modified for the new behavior.

See also https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/How_to_Submit_a_Patch

Severity: -- → S3
Priority: -- → P2
Regressed by: 1621684
Has Regression Range: --- → yes

(In reply to Karl Tomlinson (:karlt) from comment #1)

Perhaps the test added in bug 1621684 could be modified for the new behavior.

dom/ipc/tests/browser_ProcessPriorityManager.js was configured to run on Windows only. I modified the rule and ran it but the tests failed at mozilla-central's tip.

I could try to make the tests pass with the tip but it would be great to have passing tests to begin with. Short of going through the test log, is it possible to show some stack traces for the failures in the summary?

$ hg id
8cb700c12bd3+
$ hg diff
diff --git a/dom/ipc/tests/browser.ini b/dom/ipc/tests/browser.ini
--- a/dom/ipc/tests/browser.ini
+++ b/dom/ipc/tests/browser.ini
@@ -9,7 +9,7 @@ support-files =
 [browser_CrashService_crash.js]
 skip-if = !crashreporter
 [browser_ProcessPriorityManager.js]
-skip-if = os != "win" # The Process Priority Manager is only enabled for Windows so far. Bug 1522879.
+# skip-if = os != "win" # The Process Priority Manager is only enabled for Windows so far. Bug 1522879.
 [browser_crash_oopiframe.js]
 skip-if = !fission || !crashreporter || verify
 [browser_domainPolicy.js]
$ ./mach test dom/ipc/tests/browser_ProcessPriorityManager.js
Overall Summary
===============

mochitest-browser
~~~~~~~~~~~~~~~~~
Ran 21 checks (20 subtests, 1 tests)
Expected results: 13
Unexpected results: 8
  subtest: 8 (8 fail)

Unexpected Results
------------------
dom/ipc/tests/browser_ProcessPriorityManager.js
  FAIL Uncaught exception - Waiting for browser at http://example.com/ to reach priority BACKGROUND - timed out after 50 tries.
  FAIL Uncaught exception - Waiting for browser at http://example.com/ to reach priority BACKGROUND_PERCEIVABLE - timed out after 50 tries.
  FAIL Uncaught exception - Waiting for browser at http://example.com/ to reach priority BACKGROUND - timed out after 50 tries.
  FAIL Uncaught exception - Waiting for browser at about:blank to reach priority BACKGROUND - timed out after 50 tries.
  FAIL Found an unexpected tab at the end of test run: http://example.com/ -
  FAIL Found an unexpected tab at the end of test run: http://example.com/ -
  FAIL Found an unexpected tab at the end of test run: http://example.com/ -
  FAIL Found an unexpected tab at the end of test run: http://example.com/ -

./mach mochitest --keep-open=true --jsconsole dom/ipc/tests/browser_ProcessPriorityManager.js and use of console.log() may be the best place to start debugging that test. I don't know whether ProcessPriorityManager is the best way to test "audio-playing".

Perhaps https://searchfox.org/mozilla-central/source/toolkit/content/tests/browser/browser_media_wakelock.js watches "audio-playing" more directly.

This should work, but we might want to debounce this with a longer duration than a single rendering block: say we have an AudioContext playing exactly one block (128 frames) of noise, and then one block of silence, we don't want to switch between audible/inaudible state every block (around 3ms), we'd rather consider the stream as being audible.

I'm also not sure why we're stopping capture and destroying the track when the stream is inaudible, but that's separate, it just happens to be the right next to the line you add in your patch.

Daniel, let us know if you have time and want to work on this. Otherwise, it seems rather important to get done quickly, so I can pick up your work and finish it. Thanks for the very good bug report in any case.

As an aside, we should also open a ticket with Outlook, because their code is buggy. They should not use an AudioContext for notifications, since it's much more expensive than a regular HTMLMediaElement, and sample-perfect timing accuracy isn't needed in this scenario (since it's just a notification). HTMLMediaElement have the benefit of automatically disposing of all the resources (including the system-level low latency audio stream, that is expensive to keep around, and possibly keeping bluetooth headphones On and burning their battery), but calling suspend/close would also work as you note.

Flags: needinfo?(greenrecyclebin)

(In reply to Paul Adenot (:padenot) from comment #4)

This should work, but we might want to debounce this with a longer duration than a single rendering block: say we have an AudioContext playing exactly one block (128 frames) of noise, and then one block of silence, we don't want to switch between audible/inaudible state every block (around 3ms), we'd rather consider the stream as being audible.

Did you propose this as an improvement of or a separate mechanism from the shouldNotifyChanged optimization at https://dxr.mozilla.org/mozilla-central/rev/f46205a42faecda0e5a73bdcde0a8e1caa0126d9/dom/media/webaudio/AudioDestinationNode.cpp#210-247? Currently, it notifies inaudible-to-audible transitions immediately, but does so only after 1 second for the other way round.

I'm also not sure why we're stopping capture and destroying the track when the stream is inaudible, but that's separate, it just happens to be the right next to the line you add in your patch.

I'm sure the Web Audio API code could benefit from many performance improvements.

Daniel, let us know if you have time and want to work on this. Otherwise, it seems rather important to get done quickly, so I can pick up your work and finish it. Thanks for the very good bug report in any case.

I'm trying to contribute to Mozilla again so I definitely want to work on this. However, with the recent addition of a family member, I feel I'll only be slowing you down. However, let me summarize what I've cobbled together in the next message.

As an aside, we should also open a ticket with Outlook, because their code is buggy. They should not use an AudioContext for notifications, since it's much more expensive than a regular HTMLMediaElement, and sample-perfect timing accuracy isn't needed in this scenario (since it's just a notification). HTMLMediaElement have the benefit of automatically disposing of all the resources (including the system-level low latency audio stream, that is expensive to keep around, and possibly keeping bluetooth headphones On and burning their battery), but calling suspend/close would also work as you note.

I'm not sure how to do that. Besides, I can't be sure what's causing the issue on their side since Outlook is proprietary. Nevertheless, they will surely benefit from this bug fix.

Flags: needinfo?(greenrecyclebin)

I added a test for the same implementation in the previous patch. I also addressed a few related test issues. There is duplicate code, but I don't know how to organize it properly.

While researching how to use the Web Audio API, I found toolkit/content/tests/browser/browser_webAudio_silentData.js. However, there were two problems with it:

Furthermore, I noticed that the Web Audio API tests were silent (even when the tracks were not). (It was extremely confusing because there was also the above bug and I couldn't tell what was wrong initially.) Existing tests that use <audio> or <video> tags were audible as expected. I enumerated all media devices and for all these tests, there was only 1 device, which was the microphone. Is this problem specific to tests in macOS? (I found a --use-test-media-devices Mochitest option (for Linux only) at https://searchfox.org/mozilla-central/rev/8a0745cd346f0cfb89ae71690babbf7bff706113/testing/mochitest/mochitest_options.py#493.)

Attachment #9170175 - Attachment is obsolete: true
Flags: needinfo?(padenot)

Thanks for the explanation, patch and test, I'll finish this.

Flags: needinfo?(padenot)
Assignee: nobody → padenot
Severity: S3 → S2
Priority: P2 → P1

This is essentially the same code as the interleaved version, but backwards,
since the memory layout is the opposite, we want to take advantage of memory
locality, and only touch audio samples once each.

The ProcessAudioData method has been renamed, because of the similarity
between the AudioData type and ProcessAudioData, since it can now process
AudioBlocks.

Same rationale as for AudioData, the audibility is best computed externaly.

Depends on D90431

In particular, this removes the code that was limiting the audibility
notifications spam, because this is handled by the AudibilityMonitor.

Depends on D90432

The little dance with the array is unnecessary, we can just use an osc and a
gain here.

Depends on D90435

(In reply to Paul Adenot (:padenot) from comment #7)

Thanks for the explanation, patch and test, I'll finish this.

I've split your patch between test and implementation but retained your commit info, and changed a few things in the area, but your initial fix was what was needed and is untouched. Thank you very much!

(In reply to Daniel from comment #6)
I tried to leave a comment for your changes at https://phabricator.services.mozilla.com/D90446 but I don't think I have permissions.

While researching how to use the Web Audio API, I found toolkit/content/tests/browser/browser_webAudio_silentData.js. However, there were two problems with it:

  • It passed even with audible audio.

This could be simulated in the new version by setting the gain value to 1.0. It (correctly) fails with audible audio. However, it fails only because the test times out (after 90 seconds).

Why don't we make it fail faster (similarly to https://phabricator.services.mozilla.com/D90435?id=340293#change-SknPG54lF5Yt)?

Thanks for rewriting it succinctly.

(In reply to Daniel from comment #16)

  • It passed even with audible audio.

This could be simulated in the new version by setting the gain value to 1.0. It (correctly) fails with audible audio. However, it fails only because the test times out (after 90 seconds).

This is generally OK, but you're right that we can make it fail faster.

Attachment #9176074 - Attachment description: Bug 1659244 - Remove audibilty code from AudioSegment and AudioBlock. r?pehrsons → Bug 1659244 - Remove audibility code from AudioSegment and AudioBlock. r?pehrsons
Blocks: wakelock
Pushed by padenot@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/90f3b8edbd8b Allow processing planar audio in the AudibilityMonitor. r=pehrsons https://hg.mozilla.org/integration/autoland/rev/4f1c72021ec9 Remove audibility code from AudioSegment and AudioBlock. r=pehrsons https://hg.mozilla.org/integration/autoland/rev/9c362f616f93 Use the AudibilityMonitor in AudioDestinationNode. r=pehrsons,alwu https://hg.mozilla.org/integration/autoland/rev/47a5552dece7 Drop the audio wakelock when the AudioDestinationNode is notified that it is not audible. r=pehrsons,alwu https://hg.mozilla.org/integration/autoland/rev/9e748be643e7 Test Web Audio API audibility in various scenarios. r=alwu,pehrsons https://hg.mozilla.org/integration/autoland/rev/da3af45b9195 Simplify browser_webAudio_silentData.js. r=alwu https://hg.mozilla.org/integration/autoland/rev/50d7aabc075e Fix two static analysis warnings in AudioDestinationNode.cpp. r=pehrsons
Pushed by padenot@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/7bfa9d3630ad Allow processing planar audio in the AudibilityMonitor. r=pehrsons https://hg.mozilla.org/integration/autoland/rev/711d880bb405 Remove audibility code from AudioSegment and AudioBlock. r=pehrsons https://hg.mozilla.org/integration/autoland/rev/78363a55022d Use the AudibilityMonitor in AudioDestinationNode. r=pehrsons,alwu https://hg.mozilla.org/integration/autoland/rev/51ea74424373 Drop the audio wakelock when the AudioDestinationNode is notified that it is not audible. r=pehrsons,alwu https://hg.mozilla.org/integration/autoland/rev/2a8fa3be794c Test Web Audio API audibility in various scenarios. r=alwu,pehrsons https://hg.mozilla.org/integration/autoland/rev/d7554a1eb850 Simplify browser_webAudio_silentData.js. r=alwu https://hg.mozilla.org/integration/autoland/rev/c163c94202b3 Fix two static analysis warnings in AudioDestinationNode.cpp. r=pehrsons
Flags: needinfo?(padenot)

The patch landed in nightly and beta is affected.
:padenot, is this bug important enough to require an uplift?
If not please set status_beta to wontfix.

For more information, please visit auto_nag documentation.

Flags: needinfo?(padenot)
Flags: needinfo?(padenot)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: