1 | /* |
2 | * Copyright (C) 2013-2019 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 "Timer.h" |
33 | #include <pal/system/SystemSleepListener.h> |
34 | #include <wtf/AggregateLogger.h> |
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 |
45 | : private RemoteCommandListenerClient |
46 | , private PAL::SystemSleepListener::Client |
47 | , private AudioHardwareListener::Client |
48 | #if !RELEASE_LOG_DISABLED |
49 | , private LoggerHelper |
50 | #endif |
51 | { |
52 | WTF_MAKE_FAST_ALLOCATED; |
53 | public: |
54 | WEBCORE_EXPORT static PlatformMediaSessionManager* sharedManagerIfExists(); |
55 | WEBCORE_EXPORT static PlatformMediaSessionManager& sharedManager(); |
56 | |
57 | static void updateNowPlayingInfoIfNecessary(); |
58 | |
59 | WEBCORE_EXPORT static void setShouldDeactivateAudioSession(bool); |
60 | WEBCORE_EXPORT static bool shouldDeactivateAudioSession(); |
61 | |
62 | virtual ~PlatformMediaSessionManager() = default; |
63 | |
64 | virtual void scheduleUpdateNowPlayingInfo() { } |
65 | bool has(PlatformMediaSession::MediaType) const; |
66 | int count(PlatformMediaSession::MediaType) const; |
67 | bool activeAudioSessionRequired() const; |
68 | bool canProduceAudio() const; |
69 | |
70 | virtual bool hasActiveNowPlayingSession() const { return false; } |
71 | virtual String lastUpdatedNowPlayingTitle() const { return emptyString(); } |
72 | virtual double lastUpdatedNowPlayingDuration() const { return NAN; } |
73 | virtual double lastUpdatedNowPlayingElapsedTime() const { return NAN; } |
74 | virtual uint64_t lastUpdatedNowPlayingInfoUniqueIdentifier() const { return 0; } |
75 | virtual bool registeredAsNowPlayingApplication() const { return false; } |
76 | virtual void prepareToSendUserMediaPermissionRequest() { } |
77 | |
78 | bool willIgnoreSystemInterruptions() const { return m_willIgnoreSystemInterruptions; } |
79 | void setWillIgnoreSystemInterruptions(bool ignore) { m_willIgnoreSystemInterruptions = ignore; } |
80 | |
81 | WEBCORE_EXPORT virtual void beginInterruption(PlatformMediaSession::InterruptionType); |
82 | WEBCORE_EXPORT void endInterruption(PlatformMediaSession::EndInterruptionFlags); |
83 | |
84 | WEBCORE_EXPORT void applicationWillBecomeInactive(); |
85 | WEBCORE_EXPORT void applicationDidBecomeActive(); |
86 | WEBCORE_EXPORT void applicationWillEnterForeground(bool suspendedUnderLock); |
87 | WEBCORE_EXPORT void applicationDidEnterBackground(bool suspendedUnderLock); |
88 | WEBCORE_EXPORT void processWillSuspend(); |
89 | WEBCORE_EXPORT void processDidResume(); |
90 | |
91 | void stopAllMediaPlaybackForDocument(const Document&); |
92 | WEBCORE_EXPORT void stopAllMediaPlaybackForProcess(); |
93 | |
94 | void suspendAllMediaPlaybackForDocument(const Document&); |
95 | void resumeAllMediaPlaybackForDocument(const Document&); |
96 | void suspendAllMediaBufferingForDocument(const Document&); |
97 | void resumeAllMediaBufferingForDocument(const Document&); |
98 | |
99 | enum SessionRestrictionFlags { |
100 | NoRestrictions = 0, |
101 | ConcurrentPlaybackNotPermitted = 1 << 0, |
102 | BackgroundProcessPlaybackRestricted = 1 << 1, |
103 | BackgroundTabPlaybackRestricted = 1 << 2, |
104 | InterruptedPlaybackNotPermitted = 1 << 3, |
105 | InactiveProcessPlaybackRestricted = 1 << 4, |
106 | SuspendedUnderLockPlaybackRestricted = 1 << 5, |
107 | }; |
108 | typedef unsigned SessionRestrictions; |
109 | |
110 | WEBCORE_EXPORT void addRestriction(PlatformMediaSession::MediaType, SessionRestrictions); |
111 | WEBCORE_EXPORT void removeRestriction(PlatformMediaSession::MediaType, SessionRestrictions); |
112 | WEBCORE_EXPORT SessionRestrictions restrictions(PlatformMediaSession::MediaType); |
113 | virtual void resetRestrictions(); |
114 | |
115 | virtual bool sessionWillBeginPlayback(PlatformMediaSession&); |
116 | virtual void sessionWillEndPlayback(PlatformMediaSession&); |
117 | virtual void sessionStateChanged(PlatformMediaSession&); |
118 | virtual void sessionDidEndRemoteScrubbing(const PlatformMediaSession&) { }; |
119 | virtual void clientCharacteristicsChanged(PlatformMediaSession&) { } |
120 | virtual void sessionCanProduceAudioChanged(PlatformMediaSession&); |
121 | |
122 | #if PLATFORM(IOS_FAMILY) |
123 | virtual void configureWireLessTargetMonitoring() { } |
124 | #endif |
125 | virtual bool hasWirelessTargetsAvailable() { return false; } |
126 | |
127 | void setCurrentSession(PlatformMediaSession&); |
128 | PlatformMediaSession* currentSession() const; |
129 | |
130 | void sessionIsPlayingToWirelessPlaybackTargetChanged(PlatformMediaSession&); |
131 | |
132 | WEBCORE_EXPORT void setIsPlayingToAutomotiveHeadUnit(bool); |
133 | bool isPlayingToAutomotiveHeadUnit() const { return m_isPlayingToAutomotiveHeadUnit; } |
134 | |
135 | void forEachMatchingSession(const Function<bool(const PlatformMediaSession&)>& predicate, const Function<void(PlatformMediaSession&)>& matchingCallback); |
136 | |
137 | protected: |
138 | friend class PlatformMediaSession; |
139 | explicit PlatformMediaSessionManager(); |
140 | |
141 | void addSession(PlatformMediaSession&); |
142 | virtual void removeSession(PlatformMediaSession&); |
143 | |
144 | void forEachSession(const Function<void(PlatformMediaSession&)>&); |
145 | void forEachDocumentSession(const Document&, const Function<void(PlatformMediaSession&)>&); |
146 | bool anyOfSessions(const Function<bool(const PlatformMediaSession&)>&) const; |
147 | |
148 | AudioHardwareListener* audioHardwareListener() { return m_audioHardwareListener.get(); } |
149 | |
150 | bool processIsSuspended() const { return m_processIsSuspended; } |
151 | |
152 | #if !RELEASE_LOG_DISABLED |
153 | const Logger& logger() const final { return m_logger; } |
154 | const void* logIdentifier() const final { return nullptr; } |
155 | const char* logClassName() const override { return "PlatformMediaSessionManager" ; } |
156 | WTFLogChannel& logChannel() const final; |
157 | #endif |
158 | |
159 | private: |
160 | friend class Internals; |
161 | |
162 | virtual void updateSessionState() { } |
163 | |
164 | // RemoteCommandListenerClient |
165 | WEBCORE_EXPORT void didReceiveRemoteControlCommand(PlatformMediaSession::RemoteControlCommandType, const PlatformMediaSession::RemoteCommandArgument*) override; |
166 | WEBCORE_EXPORT bool supportsSeeking() const override; |
167 | |
168 | // AudioHardwareListenerClient |
169 | void audioHardwareDidBecomeActive() override { } |
170 | void audioHardwareDidBecomeInactive() override { } |
171 | void audioOutputDeviceChanged() override; |
172 | |
173 | // PAL::SystemSleepListener |
174 | void systemWillSleep() override; |
175 | void systemDidWake() override; |
176 | |
177 | Vector<WeakPtr<PlatformMediaSession>> sessionsMatching(const Function<bool(const PlatformMediaSession&)>&) const; |
178 | |
179 | SessionRestrictions m_restrictions[PlatformMediaSession::MediaStreamCapturingAudio + 1]; |
180 | mutable Vector<WeakPtr<PlatformMediaSession>> m_sessions; |
181 | std::unique_ptr<RemoteCommandListener> m_remoteCommandListener; |
182 | std::unique_ptr<PAL::SystemSleepListener> m_systemSleepListener; |
183 | RefPtr<AudioHardwareListener> m_audioHardwareListener; |
184 | |
185 | #if ENABLE(WIRELESS_PLAYBACK_TARGET) && !PLATFORM(IOS_FAMILY) |
186 | RefPtr<MediaPlaybackTarget> m_playbackTarget; |
187 | bool m_canPlayToTarget { false }; |
188 | #endif |
189 | |
190 | bool m_interrupted { false }; |
191 | mutable bool m_isApplicationInBackground { false }; |
192 | bool m_willIgnoreSystemInterruptions { false }; |
193 | bool m_processIsSuspended { false }; |
194 | bool m_isPlayingToAutomotiveHeadUnit { false }; |
195 | |
196 | #if USE(AUDIO_SESSION) |
197 | bool m_becameActive { false }; |
198 | #endif |
199 | |
200 | #if !RELEASE_LOG_DISABLED |
201 | Ref<AggregateLogger> m_logger; |
202 | #endif |
203 | }; |
204 | |
205 | } |
206 | |
207 | #endif // PlatformMediaSessionManager_h |
208 | |