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
37namespace WebCore {
38
39class Document;
40class HTMLMediaElement;
41class PlatformMediaSession;
42class RemoteCommandListener;
43
44class 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;
53public:
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() const;
85 WEBCORE_EXPORT void applicationDidBecomeActive() const;
86 WEBCORE_EXPORT void applicationWillEnterForeground(bool suspendedUnderLock) const;
87 WEBCORE_EXPORT void applicationDidEnterBackground(bool suspendedUnderLock) const;
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 Vector<PlatformMediaSession*> currentSessionsMatching(const WTF::Function<bool(const PlatformMediaSession&)>&);
131
132 void sessionIsPlayingToWirelessPlaybackTargetChanged(PlatformMediaSession&);
133
134protected:
135 friend class PlatformMediaSession;
136 explicit PlatformMediaSessionManager();
137
138 void addSession(PlatformMediaSession&);
139 virtual void removeSession(PlatformMediaSession&);
140
141 void forEachSession(const Function<void(PlatformMediaSession&, size_t)>&) const;
142 PlatformMediaSession* findSession(const Function<bool(PlatformMediaSession&, size_t)>&) const;
143 bool anyOfSessions(const Function<bool(PlatformMediaSession&, size_t)>& predicate) const { return findSession(predicate); }
144
145 AudioHardwareListener* audioHardwareListener() { return m_audioHardwareListener.get(); }
146
147 bool processIsSuspended() const { return m_processIsSuspended; }
148
149#if !RELEASE_LOG_DISABLED
150 const Logger& logger() const final { return m_logger; }
151 const void* logIdentifier() const final { return nullptr; }
152 const char* logClassName() const override { return "PlatformMediaSessionManager"; }
153 WTFLogChannel& logChannel() const final;
154#endif
155
156private:
157 friend class Internals;
158
159 virtual void updateSessionState() { }
160
161 // RemoteCommandListenerClient
162 WEBCORE_EXPORT void didReceiveRemoteControlCommand(PlatformMediaSession::RemoteControlCommandType, const PlatformMediaSession::RemoteCommandArgument*) override;
163 WEBCORE_EXPORT bool supportsSeeking() const override;
164
165 // AudioHardwareListenerClient
166 void audioHardwareDidBecomeActive() override { }
167 void audioHardwareDidBecomeInactive() override { }
168 void audioOutputDeviceChanged() override;
169
170 // PAL::SystemSleepListener
171 void systemWillSleep() override;
172 void systemDidWake() override;
173
174 SessionRestrictions m_restrictions[PlatformMediaSession::MediaStreamCapturingAudio + 1];
175 mutable Vector<PlatformMediaSession*> m_sessions;
176 std::unique_ptr<RemoteCommandListener> m_remoteCommandListener;
177 std::unique_ptr<PAL::SystemSleepListener> m_systemSleepListener;
178 RefPtr<AudioHardwareListener> m_audioHardwareListener;
179
180#if ENABLE(WIRELESS_PLAYBACK_TARGET) && !PLATFORM(IOS_FAMILY)
181 RefPtr<MediaPlaybackTarget> m_playbackTarget;
182 bool m_canPlayToTarget { false };
183#endif
184
185 bool m_interrupted { false };
186 mutable bool m_isApplicationInBackground { false };
187 bool m_willIgnoreSystemInterruptions { false };
188 mutable int m_iteratingOverSessions { 0 };
189 bool m_processIsSuspended { false };
190
191#if USE(AUDIO_SESSION)
192 bool m_becameActive { false };
193#endif
194
195#if !RELEASE_LOG_DISABLED
196 Ref<AggregateLogger> m_logger;
197#endif
198};
199
200}
201
202#endif // PlatformMediaSessionManager_h
203