⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 243958 in webkit


Ignore:
Timestamp:
Apr 5, 2019, 6:08:50 PM (7 years ago)
Author:
jer.noble@apple.com
Message:

[Cocoa] Deactivate the audio session before the WebProcess suspends.
https://bugs.webkit.org/show_bug.cgi?id=196658

Reviewed by Eric Carlson.

Source/WebCore:

Test: platform/mac/media/audio-session-deactivated-when-suspended.html

Deactivate the audio session when we are notified that the session will suspend.

Drive-by fix: don't try to begin playback when the process is suspended.

  • platform/audio/PlatformMediaSessionManager.cpp:

(WebCore::PlatformMediaSessionManager::sessionWillBeginPlayback):
(WebCore::PlatformMediaSessionManager::processWillSuspend):
(WebCore::PlatformMediaSessionManager::processDidResume):

  • platform/audio/PlatformMediaSessionManager.h:

(WebCore::PlatformMediaSessionManager::processIsSuspended const):

  • testing/InternalSettings.cpp:

(WebCore::InternalSettings::Backup::Backup):
(WebCore::InternalSettings::Backup::restoreTo):
(WebCore::InternalSettings::setShouldDeactivateAudioSession):

  • testing/InternalSettings.h:
  • testing/InternalSettings.idl:
  • testing/Internals.cpp:

(WebCore::Internals::processWillSuspend):
(WebCore::Internals::processDidResume):

  • testing/Internals.h:
  • testing/Internals.idl:

Source/WebKit:

Notify the PlatformMediaSessionManager when the process suspends or resumes.

  • WebProcess/WebProcess.cpp:

(WebKit::WebProcess::actualPrepareToSuspend):
(WebKit::WebProcess::cancelPrepareToSuspend):
(WebKit::WebProcess::processDidResume):

LayoutTests:

  • platform/mac/media/audio-session-deactivated-when-suspended-expected.txt: Added.
  • platform/mac/media/audio-session-deactivated-when-suspended.html: Added.
Location:
trunk
Files:
2 added
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r243953 r243958  
     12019-04-05  Jer Noble  <jer.noble@apple.com>
     2
     3        [Cocoa] Deactivate the audio session before the WebProcess suspends.
     4        https://bugs.webkit.org/show_bug.cgi?id=196658
     5
     6        Reviewed by Eric Carlson.
     7
     8        * platform/mac/media/audio-session-deactivated-when-suspended-expected.txt: Added.
     9        * platform/mac/media/audio-session-deactivated-when-suspended.html: Added.
     10
    1112019-04-05  Devin Rousso  <drousso@apple.com>
    212
  • trunk/Source/WebCore/ChangeLog

    r243957 r243958  
     12019-04-05  Jer Noble  <jer.noble@apple.com>
     2
     3        [Cocoa] Deactivate the audio session before the WebProcess suspends.
     4        https://bugs.webkit.org/show_bug.cgi?id=196658
     5
     6        Reviewed by Eric Carlson.
     7
     8        Test: platform/mac/media/audio-session-deactivated-when-suspended.html
     9
     10        Deactivate the audio session when we are notified that the session will suspend.
     11
     12        Drive-by fix: don't try to begin playback when the process is suspended.
     13
     14        * platform/audio/PlatformMediaSessionManager.cpp:
     15        (WebCore::PlatformMediaSessionManager::sessionWillBeginPlayback):
     16        (WebCore::PlatformMediaSessionManager::processWillSuspend):
     17        (WebCore::PlatformMediaSessionManager::processDidResume):
     18        * platform/audio/PlatformMediaSessionManager.h:
     19        (WebCore::PlatformMediaSessionManager::processIsSuspended const):
     20        * testing/InternalSettings.cpp:
     21        (WebCore::InternalSettings::Backup::Backup):
     22        (WebCore::InternalSettings::Backup::restoreTo):
     23        (WebCore::InternalSettings::setShouldDeactivateAudioSession):
     24        * testing/InternalSettings.h:
     25        * testing/InternalSettings.idl:
     26        * testing/Internals.cpp:
     27        (WebCore::Internals::processWillSuspend):
     28        (WebCore::Internals::processDidResume):
     29        * testing/Internals.h:
     30        * testing/Internals.idl:
     31
    1322019-04-05  Sihui Liu  <sihui_liu@apple.com>
    233
  • trunk/Source/WebCore/platform/audio/PlatformMediaSessionManager.cpp

    r242901 r243958  
    205205bool PlatformMediaSessionManager::sessionWillBeginPlayback(PlatformMediaSession& session)
    206206{
    207     ALWAYS_LOG(LOGIDENTIFIER, session.logIdentifier());
    208    
    209207    setCurrentSession(session);
    210208
    211209    PlatformMediaSession::MediaType sessionType = session.mediaType();
    212210    SessionRestrictions restrictions = m_restrictions[sessionType];
    213     if (session.state() == PlatformMediaSession::Interrupted && restrictions & InterruptedPlaybackNotPermitted)
     211    if (session.state() == PlatformMediaSession::Interrupted && restrictions & InterruptedPlaybackNotPermitted) {
     212        ALWAYS_LOG(LOGIDENTIFIER, session.logIdentifier(), " returning false because session.state() is Interrupted, and InterruptedPlaybackNotPermitted");
    214213        return false;
     214    }
     215
     216    if (m_processIsSuspended) {
     217        ALWAYS_LOG(LOGIDENTIFIER, session.logIdentifier(), " returning false because process is suspended");
     218        return false;
     219    }
    215220
    216221#if USE(AUDIO_SESSION)
    217     if (activeAudioSessionRequired() && !AudioSession::sharedSession().tryToSetActive(true))
     222    if (activeAudioSessionRequired() && !AudioSession::sharedSession().tryToSetActive(true)) {
     223        ALWAYS_LOG(LOGIDENTIFIER, session.logIdentifier(), " returning false failed to set active AudioSession");
    218224        return false;
     225    }
    219226
    220227    m_becameActive = true;
     
    233240    });
    234241
     242    ALWAYS_LOG(LOGIDENTIFIER, session.logIdentifier(), " returning true");
    235243    return true;
    236244}
     
    363371}
    364372
     373void PlatformMediaSessionManager::processWillSuspend()
     374{
     375    if (m_processIsSuspended)
     376        return;
     377    m_processIsSuspended = true;
     378
     379#if USE(AUDIO_SESSION)
     380    if (m_becameActive && shouldDeactivateAudioSession()) {
     381        AudioSession::sharedSession().tryToSetActive(false);
     382        ALWAYS_LOG(LOGIDENTIFIER, "tried to set inactive AudioSession");
     383        m_becameActive = false;
     384    }
     385#endif
     386}
     387
     388void PlatformMediaSessionManager::processDidResume()
     389{
     390    if (!m_processIsSuspended)
     391        return;
     392    m_processIsSuspended = false;
     393
     394#if USE(AUDIO_SESSION)
     395    if (!m_becameActive && activeAudioSessionRequired()) {
     396        m_becameActive = AudioSession::sharedSession().tryToSetActive(true);
     397        ALWAYS_LOG(LOGIDENTIFIER, "tried to set active AudioSession, ", m_becameActive ? "succeeded" : "failed");
     398    }
     399#endif
     400}
     401
     402
    365403void PlatformMediaSessionManager::sessionIsPlayingToWirelessPlaybackTargetChanged(PlatformMediaSession& session)
    366404{
  • trunk/Source/WebCore/platform/audio/PlatformMediaSessionManager.h

    r242901 r243958  
    5858
    5959    WEBCORE_EXPORT static void setShouldDeactivateAudioSession(bool);
     60    WEBCORE_EXPORT static bool shouldDeactivateAudioSession();
    6061
    6162    virtual ~PlatformMediaSessionManager() = default;
     
    8586    WEBCORE_EXPORT void applicationWillEnterForeground(bool suspendedUnderLock) const;
    8687    WEBCORE_EXPORT void applicationDidEnterBackground(bool suspendedUnderLock) const;
     88    WEBCORE_EXPORT void processWillSuspend();
     89    WEBCORE_EXPORT void processDidResume();
    8790
    8891    void stopAllMediaPlaybackForDocument(const Document*);
     
    142145    AudioHardwareListener* audioHardwareListener() { return m_audioHardwareListener.get(); }
    143146
     147    bool processIsSuspended() const { return m_processIsSuspended; }
     148
    144149#if !RELEASE_LOG_DISABLED
    145150    const Logger& logger() const final { return m_logger; }
     
    167172    void systemDidWake() override;
    168173
    169     static bool shouldDeactivateAudioSession();
    170 
    171174    SessionRestrictions m_restrictions[PlatformMediaSession::MediaStreamCapturingAudio + 1];
    172175    mutable Vector<PlatformMediaSession*> m_sessions;
     
    184187    bool m_willIgnoreSystemInterruptions { false };
    185188    mutable int m_iteratingOverSessions { 0 };
     189    bool m_processIsSuspended { false };
    186190
    187191#if USE(AUDIO_SESSION)
  • trunk/Source/WebCore/testing/InternalSettings.cpp

    r243763 r243958  
    3737#include "Page.h"
    3838#include "PageGroup.h"
     39#include "PlatformMediaSessionManager.h"
    3940#include "RenderTheme.h"
    4041#include "RuntimeEnabledFeatures.h"
     
    9899    , m_accessibilityEventsEnabled(settings.accessibilityEventsEnabled())
    99100#endif
     101    , m_shouldDeactivateAudioSession(PlatformMediaSessionManager::shouldDeactivateAudioSession())
    100102    , m_userInterfaceDirectionPolicy(settings.userInterfaceDirectionPolicy())
    101103    , m_systemLayoutDirection(settings.systemLayoutDirection())
     
    203205    settings.setFrameFlattening(m_frameFlattening);
    204206    settings.setIncompleteImageBorderEnabled(m_incompleteImageBorderEnabled);
     207    PlatformMediaSessionManager::setShouldDeactivateAudioSession(m_shouldDeactivateAudioSession);
    205208#if ENABLE(ACCESSIBILITY_EVENTS)
    206209    settings.setAccessibilityEventsEnabled(m_accessibilityEventsEnabled);
     
    973976}
    974977
     978void InternalSettings::setShouldDeactivateAudioSession(bool should)
     979{
     980    PlatformMediaSessionManager::setShouldDeactivateAudioSession(should);
     981}
     982
    975983// If you add to this class, make sure that you update the Backup class for test reproducability!
    976984
  • trunk/Source/WebCore/testing/InternalSettings.h

    r243763 r243958  
    130130    static bool webAnimationsCSSIntegrationEnabled();
    131131
     132    void setShouldDeactivateAudioSession(bool);
     133
    132134private:
    133135    explicit InternalSettings(Page*);
     
    197199        bool m_accessibilityEventsEnabled;
    198200#endif
     201        bool m_shouldDeactivateAudioSession;
    199202        UserInterfaceDirectionPolicy m_userInterfaceDirectionPolicy;
    200203        TextDirection m_systemLayoutDirection;
  • trunk/Source/WebCore/testing/InternalSettings.idl

    r243763 r243958  
    116116
    117117    [MayThrowException] void setAccessibilityEventsEnabled(boolean enabled);
     118    void setShouldDeactivateAudioSession(boolean shouldDeactivate);
    118119};
    119120
  • trunk/Source/WebCore/testing/Internals.cpp

    r243899 r243958  
    49724972}
    49734973
     4974void Internals::processWillSuspend()
     4975{
     4976    PlatformMediaSessionManager::sharedManager().processWillSuspend();
     4977}
     4978
     4979void Internals::processDidResume()
     4980{
     4981    PlatformMediaSessionManager::sharedManager().processDidResume();
     4982}
     4983
    49744984} // namespace WebCore
  • trunk/Source/WebCore/testing/Internals.h

    r243804 r243958  
    804804
    805805    void setAlwaysAllowLocalWebarchive(bool);
     806    void processWillSuspend();
     807    void processDidResume();
    806808
    807809private:
  • trunk/Source/WebCore/testing/Internals.idl

    r243804 r243958  
    740740
    741741    void setAlwaysAllowLocalWebarchive(boolean alwaysAllowLocalWebarchive);
    742 };
     742
     743    void processWillSuspend();
     744    void processDidResume();
     745};
  • trunk/Source/WebKit/ChangeLog

    r243957 r243958  
     12019-04-05  Jer Noble  <jer.noble@apple.com>
     2
     3        [Cocoa] Deactivate the audio session before the WebProcess suspends.
     4        https://bugs.webkit.org/show_bug.cgi?id=196658
     5
     6        Reviewed by Eric Carlson.
     7
     8        Notify the PlatformMediaSessionManager when the process suspends or resumes.
     9
     10        * WebProcess/WebProcess.cpp:
     11        (WebKit::WebProcess::actualPrepareToSuspend):
     12        (WebKit::WebProcess::cancelPrepareToSuspend):
     13        (WebKit::WebProcess::processDidResume):
     14
    1152019-04-05  Sihui Liu  <sihui_liu@apple.com>
    216
  • trunk/Source/WebKit/WebProcess/WebProcess.cpp

    r243957 r243958  
    14541454#if ENABLE(VIDEO)
    14551455    suspendAllMediaBuffering();
     1456    PlatformMediaSessionManager::sharedManager().processWillSuspend();
    14561457#endif
    14571458
     
    15201521
    15211522#if ENABLE(VIDEO)
     1523    PlatformMediaSessionManager::sharedManager().processDidResume();
    15221524    resumeAllMediaBuffering();
    15231525#endif
     
    15931595
    15941596#if ENABLE(VIDEO)
     1597    PlatformMediaSessionManager::sharedManager().processDidResume();
    15951598    resumeAllMediaBuffering();
    15961599#endif
Note: See TracChangeset for help on using the changeset viewer.