1/* 2 * Copyright (C) 2013-2014 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 MediaSessionManager_h 27#define MediaSessionManager_h 28 29#include "AudioHardwareListener.h" 30#include "MediaSession.h" 31#include "RemoteCommandListener.h" 32#include "Settings.h" 33#include "SystemSleepListener.h" 34#include <map> 35#include <wtf/Vector.h> 36 37namespace WebCore { 38 39class HTMLMediaElement; 40class MediaSession; 41class RemoteCommandListener; 42 43class MediaSessionManagerClient { 44public: 45 virtual ~MediaSessionManagerClient() { } 46 47 virtual bool isListeningForRemoteControlCommands() = 0; 48 virtual void startListeningForRemoteControlCommands() = 0; 49 virtual void stopListeningForRemoteControlCommands() = 0; 50 virtual void didBeginPlayback() = 0; 51 52protected: 53 MediaSessionManagerClient() { } 54}; 55 56class MediaSessionManager : private RemoteCommandListenerClient, private SystemSleepListener::Client, private AudioHardwareListener::Client { 57public: 58 static MediaSessionManager& sharedManager(); 59 virtual ~MediaSessionManager() { } 60 61 bool has(MediaSession::MediaType) const; 62 int count(MediaSession::MediaType) const; 63 bool activeAudioSessionRequired() const; 64 65 void beginInterruption(MediaSession::InterruptionType); 66 void endInterruption(MediaSession::EndInterruptionFlags); 67 68 void applicationWillEnterForeground() const; 69 void applicationWillEnterBackground() const; 70 void wirelessRoutesAvailableChanged(); 71 72 enum SessionRestrictionFlags { 73 NoRestrictions = 0, 74 ConcurrentPlaybackNotPermitted = 1 << 0, 75 InlineVideoPlaybackRestricted = 1 << 1, 76 MetadataPreloadingNotPermitted = 1 << 2, 77 AutoPreloadingNotPermitted = 1 << 3, 78 BackgroundPlaybackNotPermitted = 1 << 4, 79 }; 80 typedef unsigned SessionRestrictions; 81 82 void addRestriction(MediaSession::MediaType, SessionRestrictions); 83 void removeRestriction(MediaSession::MediaType, SessionRestrictions); 84 SessionRestrictions restrictions(MediaSession::MediaType); 85 virtual void resetRestrictions(); 86 87 virtual void sessionWillBeginPlayback(MediaSession&); 88 virtual void sessionWillEndPlayback(MediaSession&); 89 90 bool sessionRestrictsInlineVideoPlayback(const MediaSession&) const; 91 92#if ENABLE(IOS_AIRPLAY) 93 virtual bool hasWirelessTargetsAvailable() { return false; } 94 virtual void startMonitoringAirPlayRoutes() { } 95 virtual void stopMonitoringAirPlayRoutes() { } 96#endif 97 98 void addClient(MediaSessionManagerClient*); 99 void removeClient(MediaSessionManagerClient*); 100 101protected: 102 friend class MediaSession; 103 explicit MediaSessionManager(); 104 105 void addSession(MediaSession&); 106 void removeSession(MediaSession&); 107 108 void setCurrentSession(MediaSession&); 109 MediaSession* currentSession(); 110 111private: 112 friend class Internals; 113 114 void updateSessionState(); 115 116 117 // RemoteCommandListenerClient 118 virtual void didReceiveRemoteControlCommand(MediaSession::RemoteControlCommandType) override; 119 120 // AudioHardwareListenerClient 121 virtual void audioHardwareDidBecomeActive() override { } 122 virtual void audioHardwareDidBecomeInactive() override { } 123 virtual void audioOutputDeviceChanged() override; 124 125 // SystemSleepListener 126 virtual void systemWillSleep() override; 127 virtual void systemDidWake() override; 128 129 SessionRestrictions m_restrictions[MediaSession::WebAudio + 1]; 130 131 Vector<MediaSession*> m_sessions; 132 Vector<MediaSessionManagerClient*> m_clients; 133 std::unique_ptr<RemoteCommandListener> m_remoteCommandListener; 134 std::unique_ptr<SystemSleepListener> m_systemSleepListener; 135 RefPtr<AudioHardwareListener> m_audioHardwareListener; 136 bool m_interrupted; 137}; 138 139} 140 141#endif // MediaSessionManager_h 142