2 * Copyright (C) 2013-2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef PlatformMediaSessionManager_h
27 #define PlatformMediaSessionManager_h
29 #include "AudioHardwareListener.h"
30 #include "PlatformMediaSession.h"
31 #include "RemoteCommandListener.h"
33 #include <pal/system/SystemSleepListener.h>
34 #include <wtf/Vector.h>
39 class HTMLMediaElement;
40 class PlatformMediaSession;
41 class RemoteCommandListener;
43 class PlatformMediaSessionManager : private RemoteCommandListenerClient, private PAL::SystemSleepListener::Client, private AudioHardwareListener::Client {
44 WTF_MAKE_FAST_ALLOCATED;
46 WEBCORE_EXPORT static PlatformMediaSessionManager* sharedManagerIfExists();
47 WEBCORE_EXPORT static PlatformMediaSessionManager& sharedManager();
49 static void updateNowPlayingInfoIfNecessary();
51 virtual ~PlatformMediaSessionManager() { }
53 virtual void scheduleUpdateNowPlayingInfo() { }
54 bool has(PlatformMediaSession::MediaType) const;
55 int count(PlatformMediaSession::MediaType) const;
56 bool activeAudioSessionRequired() const;
57 bool canProduceAudio() const;
59 WEBCORE_EXPORT virtual bool hasActiveNowPlayingSession() const { return false; }
60 WEBCORE_EXPORT virtual String lastUpdatedNowPlayingTitle() const { return emptyString(); }
61 WEBCORE_EXPORT virtual double lastUpdatedNowPlayingDuration() const { return NAN; }
62 WEBCORE_EXPORT virtual double lastUpdatedNowPlayingElapsedTime() const { return NAN; }
64 bool willIgnoreSystemInterruptions() const { return m_willIgnoreSystemInterruptions; }
65 void setWillIgnoreSystemInterruptions(bool ignore) { m_willIgnoreSystemInterruptions = ignore; }
67 WEBCORE_EXPORT void beginInterruption(PlatformMediaSession::InterruptionType);
68 WEBCORE_EXPORT void endInterruption(PlatformMediaSession::EndInterruptionFlags);
70 WEBCORE_EXPORT void applicationWillBecomeInactive() const;
71 WEBCORE_EXPORT void applicationDidBecomeActive() const;
72 WEBCORE_EXPORT void applicationWillEnterForeground(bool suspendedUnderLock) const;
73 WEBCORE_EXPORT void applicationDidEnterBackground(bool suspendedUnderLock) const;
75 void stopAllMediaPlaybackForDocument(const Document*);
76 WEBCORE_EXPORT void stopAllMediaPlaybackForProcess();
78 enum SessionRestrictionFlags {
80 ConcurrentPlaybackNotPermitted = 1 << 0,
81 BackgroundProcessPlaybackRestricted = 1 << 1,
82 BackgroundTabPlaybackRestricted = 1 << 2,
83 InterruptedPlaybackNotPermitted = 1 << 3,
84 InactiveProcessPlaybackRestricted = 1 << 4,
85 SuspendedUnderLockPlaybackRestricted = 1 << 5,
87 typedef unsigned SessionRestrictions;
89 WEBCORE_EXPORT void addRestriction(PlatformMediaSession::MediaType, SessionRestrictions);
90 WEBCORE_EXPORT void removeRestriction(PlatformMediaSession::MediaType, SessionRestrictions);
91 WEBCORE_EXPORT SessionRestrictions restrictions(PlatformMediaSession::MediaType);
92 virtual void resetRestrictions();
94 virtual bool sessionWillBeginPlayback(PlatformMediaSession&);
95 virtual void sessionWillEndPlayback(PlatformMediaSession&);
96 virtual bool sessionCanLoadMedia(const PlatformMediaSession&) const;
97 virtual void sessionDidEndRemoteScrubbing(const PlatformMediaSession&) { };
98 virtual void clientCharacteristicsChanged(PlatformMediaSession&) { }
101 virtual void configureWireLessTargetMonitoring() { }
102 virtual bool hasWirelessTargetsAvailable() { return false; }
105 void setCurrentSession(PlatformMediaSession&);
106 PlatformMediaSession* currentSession() const;
108 Vector<PlatformMediaSession*> currentSessionsMatching(const WTF::Function<bool(const PlatformMediaSession&)>&);
110 void sessionIsPlayingToWirelessPlaybackTargetChanged(PlatformMediaSession&);
111 void sessionCanProduceAudioChanged(PlatformMediaSession&);
114 friend class PlatformMediaSession;
115 explicit PlatformMediaSessionManager();
117 void addSession(PlatformMediaSession&);
118 virtual void removeSession(PlatformMediaSession&);
120 void forEachSession(const Function<void(PlatformMediaSession&, size_t)>&) const;
121 PlatformMediaSession* findSession(const Function<bool(PlatformMediaSession&, size_t)>&) const;
122 bool anyOfSessions(const Function<bool(PlatformMediaSession&, size_t)>& predicate) const { return findSession(predicate); }
125 friend class Internals;
127 void updateSessionState();
129 // RemoteCommandListenerClient
130 WEBCORE_EXPORT void didReceiveRemoteControlCommand(PlatformMediaSession::RemoteControlCommandType, const PlatformMediaSession::RemoteCommandArgument*) override;
131 WEBCORE_EXPORT bool supportsSeeking() const override;
133 // AudioHardwareListenerClient
134 void audioHardwareDidBecomeActive() override { }
135 void audioHardwareDidBecomeInactive() override { }
136 void audioOutputDeviceChanged() override;
138 // PAL::SystemSleepListener
139 void systemWillSleep() override;
140 void systemDidWake() override;
142 SessionRestrictions m_restrictions[PlatformMediaSession::MediaStreamCapturingAudio + 1];
143 mutable Vector<PlatformMediaSession*> m_sessions;
144 std::unique_ptr<RemoteCommandListener> m_remoteCommandListener;
145 std::unique_ptr<PAL::SystemSleepListener> m_systemSleepListener;
146 RefPtr<AudioHardwareListener> m_audioHardwareListener;
148 #if ENABLE(WIRELESS_PLAYBACK_TARGET) && !PLATFORM(IOS)
149 RefPtr<MediaPlaybackTarget> m_playbackTarget;
150 bool m_canPlayToTarget { false };
153 bool m_interrupted { false };
154 mutable bool m_isApplicationInBackground { false };
155 bool m_willIgnoreSystemInterruptions { false };
156 mutable int m_iteratingOverSessions { 0 };
161 #endif // PlatformMediaSessionManager_h