[iOS] Enable media layout tests to run simultaneously by ignoring system-level interr...
[WebKit-https.git] / Source / WebCore / platform / audio / PlatformMediaSessionManager.h
1 /*
2  * Copyright (C) 2013-2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef PlatformMediaSessionManager_h
27 #define PlatformMediaSessionManager_h
28
29 #include "AudioHardwareListener.h"
30 #include "PlatformMediaSession.h"
31 #include "RemoteCommandListener.h"
32 #include "Settings.h"
33 #include "SystemSleepListener.h"
34 #include <map>
35 #include <wtf/Vector.h>
36
37 namespace WebCore {
38
39 class Document;
40 class HTMLMediaElement;
41 class PlatformMediaSession;
42 class RemoteCommandListener;
43
44 class PlatformMediaSessionManager : private RemoteCommandListenerClient, private SystemSleepListener::Client, private AudioHardwareListener::Client {
45     WTF_MAKE_FAST_ALLOCATED;
46 public:
47     WEBCORE_EXPORT static PlatformMediaSessionManager* sharedManagerIfExists();
48     WEBCORE_EXPORT static PlatformMediaSessionManager& sharedManager();
49     virtual ~PlatformMediaSessionManager() { }
50
51     bool has(PlatformMediaSession::MediaType) const;
52     int count(PlatformMediaSession::MediaType) const;
53     bool activeAudioSessionRequired() const;
54
55     bool willIgnoreSystemInterruptions() const { return m_willIgnoreSystemInterruptions; }
56     void setWillIgnoreSystemInterruptions(bool ignore) { m_willIgnoreSystemInterruptions = ignore; }
57
58     WEBCORE_EXPORT void beginInterruption(PlatformMediaSession::InterruptionType);
59     WEBCORE_EXPORT void endInterruption(PlatformMediaSession::EndInterruptionFlags);
60
61     WEBCORE_EXPORT void applicationWillEnterForeground() const;
62     WEBCORE_EXPORT void applicationWillEnterBackground() const;
63     WEBCORE_EXPORT void applicationDidEnterBackground(bool isSuspendedUnderLock) const;
64
65     void stopAllMediaPlaybackForDocument(const Document*);
66     WEBCORE_EXPORT void stopAllMediaPlaybackForProcess();
67
68     enum SessionRestrictionFlags {
69         NoRestrictions = 0,
70         ConcurrentPlaybackNotPermitted = 1 << 0,
71         BackgroundProcessPlaybackRestricted = 1 << 1,
72         BackgroundTabPlaybackRestricted = 1 << 2,
73         InterruptedPlaybackNotPermitted = 1 << 3,
74     };
75     typedef unsigned SessionRestrictions;
76
77     WEBCORE_EXPORT void addRestriction(PlatformMediaSession::MediaType, SessionRestrictions);
78     WEBCORE_EXPORT void removeRestriction(PlatformMediaSession::MediaType, SessionRestrictions);
79     WEBCORE_EXPORT SessionRestrictions restrictions(PlatformMediaSession::MediaType);
80     virtual void resetRestrictions();
81
82     virtual bool sessionWillBeginPlayback(PlatformMediaSession&);
83     virtual void sessionWillEndPlayback(PlatformMediaSession&);
84     virtual bool sessionCanLoadMedia(const PlatformMediaSession&) const;
85
86 #if PLATFORM(IOS)
87     virtual void configureWireLessTargetMonitoring() { }
88     virtual bool hasWirelessTargetsAvailable() { return false; }
89 #endif
90
91     void setCurrentSession(PlatformMediaSession&);
92     PlatformMediaSession* currentSession();
93
94     void sessionIsPlayingToWirelessPlaybackTargetChanged(PlatformMediaSession&);
95
96 protected:
97     friend class PlatformMediaSession;
98     explicit PlatformMediaSessionManager();
99
100     void addSession(PlatformMediaSession&);
101     void removeSession(PlatformMediaSession&);
102
103     Vector<PlatformMediaSession*> sessions() { return m_sessions; }
104
105 private:
106     friend class Internals;
107
108     void updateSessionState();
109
110     // RemoteCommandListenerClient
111     WEBCORE_EXPORT virtual void didReceiveRemoteControlCommand(PlatformMediaSession::RemoteControlCommandType) override;
112
113     // AudioHardwareListenerClient
114     virtual void audioHardwareDidBecomeActive() override { }
115     virtual void audioHardwareDidBecomeInactive() override { }
116     virtual void audioOutputDeviceChanged() override;
117
118     // SystemSleepListener
119     virtual void systemWillSleep() override;
120     virtual void systemDidWake() override;
121
122     SessionRestrictions m_restrictions[PlatformMediaSession::WebAudio + 1];
123     Vector<PlatformMediaSession*> m_sessions;
124     std::unique_ptr<RemoteCommandListener> m_remoteCommandListener;
125     std::unique_ptr<SystemSleepListener> m_systemSleepListener;
126     RefPtr<AudioHardwareListener> m_audioHardwareListener;
127
128 #if ENABLE(WIRELESS_PLAYBACK_TARGET) && !PLATFORM(IOS)
129     RefPtr<MediaPlaybackTarget> m_playbackTarget;
130     bool m_canPlayToTarget { false };
131 #endif
132
133     bool m_interrupted { false };
134     mutable bool m_isApplicationInBackground { false };
135     bool m_willIgnoreSystemInterruptions { false };
136 };
137
138 }
139
140 #endif // PlatformMediaSessionManager_h