| 1 | /* |
| 2 | * Copyright (C) 2014-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 | #pragma once |
| 27 | |
| 28 | #if ENABLE(VIDEO) |
| 29 | |
| 30 | #include "MediaPlayer.h" |
| 31 | #include "MediaProducer.h" |
| 32 | #include "PlatformMediaSession.h" |
| 33 | #include "SuccessOr.h" |
| 34 | #include "Timer.h" |
| 35 | #include <wtf/TypeCasts.h> |
| 36 | |
| 37 | namespace WebCore { |
| 38 | |
| 39 | enum class MediaSessionMainContentPurpose { |
| 40 | MediaControls, |
| 41 | Autoplay |
| 42 | }; |
| 43 | |
| 44 | enum class MediaPlaybackDenialReason { |
| 45 | UserGestureRequired, |
| 46 | FullscreenRequired, |
| 47 | PageConsentRequired, |
| 48 | InvalidState, |
| 49 | }; |
| 50 | |
| 51 | class Document; |
| 52 | class HTMLMediaElement; |
| 53 | class SourceBuffer; |
| 54 | |
| 55 | class MediaElementSession final : public PlatformMediaSession |
| 56 | { |
| 57 | WTF_MAKE_FAST_ALLOCATED; |
| 58 | public: |
| 59 | explicit MediaElementSession(HTMLMediaElement&); |
| 60 | virtual ~MediaElementSession() = default; |
| 61 | |
| 62 | void registerWithDocument(Document&); |
| 63 | void unregisterWithDocument(Document&); |
| 64 | |
| 65 | void clientWillBeginAutoplaying() final; |
| 66 | bool clientWillBeginPlayback() final; |
| 67 | bool clientWillPausePlayback() final; |
| 68 | |
| 69 | void visibilityChanged(); |
| 70 | void isVisibleInViewportChanged(); |
| 71 | void inActiveDocumentChanged(); |
| 72 | |
| 73 | SuccessOr<MediaPlaybackDenialReason> playbackPermitted() const; |
| 74 | bool autoplayPermitted() const; |
| 75 | bool dataLoadingPermitted() const; |
| 76 | MediaPlayer::BufferingPolicy preferredBufferingPolicy() const; |
| 77 | bool fullscreenPermitted() const; |
| 78 | bool pageAllowsDataLoading() const; |
| 79 | bool pageAllowsPlaybackAfterResuming() const; |
| 80 | |
| 81 | #if ENABLE(WIRELESS_PLAYBACK_TARGET) |
| 82 | void showPlaybackTargetPicker(); |
| 83 | bool hasWirelessPlaybackTargets() const; |
| 84 | |
| 85 | bool wirelessVideoPlaybackDisabled() const; |
| 86 | void setWirelessVideoPlaybackDisabled(bool); |
| 87 | |
| 88 | void setHasPlaybackTargetAvailabilityListeners(bool); |
| 89 | |
| 90 | bool isPlayingToWirelessPlaybackTarget() const override; |
| 91 | |
| 92 | void mediaStateDidChange(MediaProducer::MediaStateFlags); |
| 93 | #endif |
| 94 | |
| 95 | bool requiresFullscreenForVideoPlayback() const; |
| 96 | WEBCORE_EXPORT bool allowsPictureInPicture() const; |
| 97 | MediaPlayer::Preload effectivePreloadForElement() const; |
| 98 | bool allowsAutomaticMediaDataLoading() const; |
| 99 | |
| 100 | void mediaEngineUpdated(); |
| 101 | |
| 102 | void resetPlaybackSessionState() override; |
| 103 | |
| 104 | void suspendBuffering() override; |
| 105 | void resumeBuffering() override; |
| 106 | bool bufferingSuspended() const; |
| 107 | void updateBufferingPolicy() { scheduleClientDataBufferingCheck(); } |
| 108 | |
| 109 | // Restrictions to modify default behaviors. |
| 110 | enum BehaviorRestrictionFlags : unsigned { |
| 111 | NoRestrictions = 0, |
| 112 | RequireUserGestureForLoad = 1 << 0, |
| 113 | RequireUserGestureForVideoRateChange = 1 << 1, |
| 114 | RequireUserGestureForFullscreen = 1 << 2, |
| 115 | RequirePageConsentToLoadMedia = 1 << 3, |
| 116 | RequirePageConsentToResumeMedia = 1 << 4, |
| 117 | RequireUserGestureForAudioRateChange = 1 << 5, |
| 118 | RequireUserGestureToShowPlaybackTargetPicker = 1 << 6, |
| 119 | WirelessVideoPlaybackDisabled = 1 << 7, |
| 120 | RequireUserGestureToAutoplayToExternalDevice = 1 << 8, |
| 121 | AutoPreloadingNotPermitted = 1 << 10, |
| 122 | InvisibleAutoplayNotPermitted = 1 << 11, |
| 123 | OverrideUserGestureRequirementForMainContent = 1 << 12, |
| 124 | RequireUserGestureToControlControlsManager = 1 << 13, |
| 125 | RequirePlaybackToControlControlsManager = 1 << 14, |
| 126 | RequireUserGestureForVideoDueToLowPowerMode = 1 << 15, |
| 127 | AllRestrictions = ~NoRestrictions, |
| 128 | }; |
| 129 | typedef unsigned BehaviorRestrictions; |
| 130 | |
| 131 | WEBCORE_EXPORT BehaviorRestrictions behaviorRestrictions() const { return m_restrictions; } |
| 132 | WEBCORE_EXPORT void addBehaviorRestriction(BehaviorRestrictions); |
| 133 | WEBCORE_EXPORT void removeBehaviorRestriction(BehaviorRestrictions); |
| 134 | bool hasBehaviorRestriction(BehaviorRestrictions restriction) const { return restriction & m_restrictions; } |
| 135 | |
| 136 | #if ENABLE(MEDIA_SOURCE) |
| 137 | size_t maximumMediaSourceBufferSize(const SourceBuffer&) const; |
| 138 | #endif |
| 139 | |
| 140 | HTMLMediaElement& element() const { return m_element; } |
| 141 | |
| 142 | bool wantsToObserveViewportVisibilityForMediaControls() const; |
| 143 | bool wantsToObserveViewportVisibilityForAutoplay() const; |
| 144 | |
| 145 | enum class PlaybackControlsPurpose { ControlsManager, NowPlaying }; |
| 146 | bool canShowControlsManager(PlaybackControlsPurpose) const; |
| 147 | bool isLargeEnoughForMainContent(MediaSessionMainContentPurpose) const; |
| 148 | bool isMainContentForPurposesOfAutoplayEvents() const; |
| 149 | MonotonicTime mostRecentUserInteractionTime() const; |
| 150 | |
| 151 | bool allowsPlaybackControlsForAutoplayingAudio() const; |
| 152 | bool allowsNowPlayingControlsVisibility() const override; |
| 153 | |
| 154 | static bool isMediaElementSessionMediaType(MediaType type) |
| 155 | { |
| 156 | return type == Video |
| 157 | || type == Audio |
| 158 | || type == VideoAudio; |
| 159 | } |
| 160 | |
| 161 | #if !RELEASE_LOG_DISABLED |
| 162 | const void* logIdentifier() const final { return m_logIdentifier; } |
| 163 | const char* logClassName() const final { return "MediaElementSession" ; } |
| 164 | #endif |
| 165 | |
| 166 | private: |
| 167 | |
| 168 | #if ENABLE(WIRELESS_PLAYBACK_TARGET) |
| 169 | void targetAvailabilityChangedTimerFired(); |
| 170 | |
| 171 | // MediaPlaybackTargetClient |
| 172 | void setPlaybackTarget(Ref<MediaPlaybackTarget>&&) override; |
| 173 | void externalOutputDeviceAvailableDidChange(bool) override; |
| 174 | void setShouldPlayToPlaybackTarget(bool) override; |
| 175 | #endif |
| 176 | #if PLATFORM(IOS_FAMILY) |
| 177 | bool requiresPlaybackTargetRouteMonitoring() const override; |
| 178 | #endif |
| 179 | bool updateIsMainContent() const; |
| 180 | void mainContentCheckTimerFired(); |
| 181 | |
| 182 | void scheduleClientDataBufferingCheck(); |
| 183 | void clientDataBufferingTimerFired(); |
| 184 | void updateClientDataBuffering(); |
| 185 | |
| 186 | HTMLMediaElement& m_element; |
| 187 | BehaviorRestrictions m_restrictions; |
| 188 | |
| 189 | bool m_elementIsHiddenUntilVisibleInViewport { false }; |
| 190 | bool m_elementIsHiddenBecauseItWasRemovedFromDOM { false }; |
| 191 | |
| 192 | #if ENABLE(WIRELESS_PLAYBACK_TARGET) |
| 193 | mutable Timer m_targetAvailabilityChangedTimer; |
| 194 | RefPtr<MediaPlaybackTarget> m_playbackTarget; |
| 195 | bool m_shouldPlayToPlaybackTarget { false }; |
| 196 | mutable bool m_hasPlaybackTargets { false }; |
| 197 | #endif |
| 198 | #if PLATFORM(IOS_FAMILY) |
| 199 | bool m_hasPlaybackTargetAvailabilityListeners { false }; |
| 200 | #endif |
| 201 | |
| 202 | MonotonicTime m_mostRecentUserInteractionTime; |
| 203 | |
| 204 | mutable bool m_isMainContent { false }; |
| 205 | Timer m_mainContentCheckTimer; |
| 206 | Timer m_clientDataBufferingTimer; |
| 207 | |
| 208 | #if !RELEASE_LOG_DISABLED |
| 209 | const void* m_logIdentifier; |
| 210 | #endif |
| 211 | }; |
| 212 | |
| 213 | String convertEnumerationToString(const MediaPlaybackDenialReason); |
| 214 | |
| 215 | } // namespace WebCore |
| 216 | |
| 217 | namespace WTF { |
| 218 | |
| 219 | template<typename Type> |
| 220 | struct LogArgument; |
| 221 | |
| 222 | template <> |
| 223 | struct LogArgument<WebCore::MediaPlaybackDenialReason> { |
| 224 | static String toString(const WebCore::MediaPlaybackDenialReason reason) |
| 225 | { |
| 226 | return convertEnumerationToString(reason); |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | }; // namespace WTF |
| 231 | |
| 232 | |
| 233 | SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::MediaElementSession) |
| 234 | static bool isType(const WebCore::PlatformMediaSession& session) { return WebCore::MediaElementSession::isMediaElementSessionMediaType(session.mediaType()); } |
| 235 | SPECIALIZE_TYPE_TRAITS_END() |
| 236 | |
| 237 | #endif // ENABLE(VIDEO) |
| 238 | |