1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* Copyright (C) 2014-2021 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#if ENABLE(VIDEO)
#include "MediaPlayer.h"
#include "MediaProducer.h"
#include "MediaUsageInfo.h"
#include "PlatformMediaSession.h"
#include "Timer.h"
#include <memory>
#include <wtf/TZoneMalloc.h>
#include <wtf/TypeCasts.h>
namespace WebCore {
class MediaElementSession;
}
namespace WTF {
template<typename T> struct IsDeprecatedWeakRefSmartPointerException;
template<> struct IsDeprecatedWeakRefSmartPointerException<WebCore::MediaElementSession> : std::true_type { };
}
namespace WebCore {
enum class MediaSessionMainContentPurpose { MediaControls, Autoplay };
enum class MediaPlaybackState { Playing, Paused };
enum class MediaPlaybackDenialReason {
UserGestureRequired,
FullscreenRequired,
PageConsentRequired,
InvalidState,
};
class Document;
class HTMLMediaElement;
class MediaMetadata;
class MediaSession;
class MediaElementSessionObserver;
class SourceBuffer;
struct MediaPositionState;
enum class MediaSessionPlaybackState : uint8_t;
class MediaElementSession final : public PlatformMediaSession {
WTF_MAKE_TZONE_ALLOCATED(MediaElementSession);
public:
explicit MediaElementSession(HTMLMediaElement&);
virtual ~MediaElementSession();
void registerWithDocument(Document&);
void unregisterWithDocument(Document&);
void clientWillBeginAutoplaying() final;
bool clientWillBeginPlayback() final;
bool clientWillPausePlayback() final;
void clientCharacteristicsChanged(bool) final;
void visibilityChanged();
void isVisibleInViewportChanged();
void inActiveDocumentChanged();
Expected<void, MediaPlaybackDenialReason> playbackStateChangePermitted(MediaPlaybackState) const;
bool autoplayPermitted() const;
bool dataLoadingPermitted() const;
MediaPlayer::BufferingPolicy preferredBufferingPolicy() const;
bool fullscreenPermitted() const;
bool pageAllowsDataLoading() const;
bool pageAllowsPlaybackAfterResuming() const;
#if ENABLE(WIRELESS_PLAYBACK_TARGET)
void showPlaybackTargetPicker();
bool hasWirelessPlaybackTargets() const;
bool wirelessVideoPlaybackDisabled() const;
void setWirelessVideoPlaybackDisabled(bool);
void setHasPlaybackTargetAvailabilityListeners(bool);
bool isPlayingToWirelessPlaybackTarget() const override;
void mediaStateDidChange(MediaProducerMediaStateFlags);
#endif
bool requiresFullscreenForVideoPlayback() const;
WEBCORE_EXPORT bool allowsPictureInPicture() const;
MediaPlayer::Preload effectivePreloadForElement() const;
bool allowsAutomaticMediaDataLoading() const;
void mediaEngineUpdated();
void resetPlaybackSessionState() override;
void suspendBuffering() override;
void resumeBuffering() override;
bool bufferingSuspended() const;
void updateBufferingPolicy() { scheduleClientDataBufferingCheck(); }
// Restrictions to modify default behaviors.
enum BehaviorRestrictionFlags : unsigned {
NoRestrictions = 0,
RequireUserGestureForLoad = 1 << 0,
RequireUserGestureForVideoRateChange = 1 << 1,
RequireUserGestureForFullscreen = 1 << 2,
RequirePageConsentToLoadMedia = 1 << 3,
RequirePageConsentToResumeMedia = 1 << 4,
RequireUserGestureForAudioRateChange = 1 << 5,
RequireUserGestureToShowPlaybackTargetPicker = 1 << 6,
WirelessVideoPlaybackDisabled = 1 << 7,
RequireUserGestureToAutoplayToExternalDevice = 1 << 8,
AutoPreloadingNotPermitted = 1 << 10,
InvisibleAutoplayNotPermitted = 1 << 11,
OverrideUserGestureRequirementForMainContent = 1 << 12,
RequireUserGestureToControlControlsManager = 1 << 13,
RequirePlaybackToControlControlsManager = 1 << 14,
RequireUserGestureForVideoDueToLowPowerMode = 1 << 15,
RequirePageVisibilityToPlayAudio = 1 << 16,
RequireUserGestureForVideoDueToAggressiveThermalMitigation = 1 << 17,
RequirePageVisibilityForVideoToBeNowPlaying = 1 << 18,
AllRestrictions = ~NoRestrictions,
};
typedef unsigned BehaviorRestrictions;
WEBCORE_EXPORT BehaviorRestrictions behaviorRestrictions() const { return m_restrictions; }
WEBCORE_EXPORT void addBehaviorRestriction(BehaviorRestrictions);
WEBCORE_EXPORT void removeBehaviorRestriction(BehaviorRestrictions);
bool hasBehaviorRestriction(BehaviorRestrictions restriction) const { return restriction & m_restrictions; }
HTMLMediaElement& element() const { return m_element; }
bool wantsToObserveViewportVisibilityForMediaControls() const;
bool wantsToObserveViewportVisibilityForAutoplay() const;
bool canShowControlsManager(PlaybackControlsPurpose) const;
bool isLargeEnoughForMainContent(MediaSessionMainContentPurpose) const;
bool isLongEnoughForMainContent() const final;
bool isMainContentForPurposesOfAutoplayEvents() const;
MonotonicTime mostRecentUserInteractionTime() const;
bool allowsPlaybackControlsForAutoplayingAudio() const;
static bool isMediaElementSessionMediaType(MediaType type)
{
return type == MediaType::Video
|| type == MediaType::Audio
|| type == MediaType::VideoAudio;
}
std::optional<NowPlayingInfo> computeNowPlayingInfo() const;
WEBCORE_EXPORT void updateMediaUsageIfChanged() final;
std::optional<MediaUsageInfo> mediaUsageInfo() const { return m_mediaUsageInfo; }
#if !RELEASE_LOG_DISABLED
String description() const final;
ASCIILiteral logClassName() const final { return "MediaElementSession"_s; }
#endif
#if ENABLE(MEDIA_SESSION)
void didReceiveRemoteControlCommand(RemoteControlCommandType, const RemoteCommandArgument&) final;
#endif
void metadataChanged(const RefPtr<MediaMetadata>&);
void positionStateChanged(const std::optional<MediaPositionState>&);
void playbackStateChanged(MediaSessionPlaybackState);
void actionHandlersChanged();
MediaSession* mediaSession() const;
bool hasNowPlayingInfo() const;
private:
#if ENABLE(WIRELESS_PLAYBACK_TARGET)
void targetAvailabilityChangedTimerFired();
// MediaPlaybackTargetClient
void setPlaybackTarget(Ref<MediaPlaybackTarget>&&) override;
void externalOutputDeviceAvailableDidChange(bool) override;
void setShouldPlayToPlaybackTarget(bool) override;
void playbackTargetPickerWasDismissed() override;
#endif
#if PLATFORM(IOS_FAMILY)
bool requiresPlaybackTargetRouteMonitoring() const override;
#endif
void ensureIsObservingMediaSession();
bool updateIsMainContent() const;
void mainContentCheckTimerFired();
void scheduleClientDataBufferingCheck();
void clientDataBufferingTimerFired();
void updateClientDataBuffering();
void addMediaUsageManagerSessionIfNecessary();
HTMLMediaElement& m_element;
BehaviorRestrictions m_restrictions;
std::optional<MediaUsageInfo> m_mediaUsageInfo;
bool m_elementIsHiddenUntilVisibleInViewport { false };
bool m_elementIsHiddenBecauseItWasRemovedFromDOM { false };
#if ENABLE(WIRELESS_PLAYBACK_TARGET)
mutable Timer m_targetAvailabilityChangedTimer;
RefPtr<MediaPlaybackTarget> m_playbackTarget;
bool m_shouldPlayToPlaybackTarget { false };
mutable bool m_hasPlaybackTargets { false };
#endif
#if PLATFORM(IOS_FAMILY)
bool m_hasPlaybackTargetAvailabilityListeners { false };
#endif
MonotonicTime m_mostRecentUserInteractionTime;
mutable bool m_isMainContent { false };
Timer m_mainContentCheckTimer;
Timer m_clientDataBufferingTimer;
#if ENABLE(MEDIA_USAGE)
bool m_haveAddedMediaUsageManagerSession { false };
#endif
#if ENABLE(MEDIA_SESSION)
bool m_isScrubbing { false };
std::unique_ptr<MediaElementSessionObserver> m_observer;
#endif
};
String convertEnumerationToString(const MediaPlaybackDenialReason);
} // namespace WebCore
namespace WTF {
template<typename Type>
struct LogArgument;
template <>
struct LogArgument<WebCore::MediaPlaybackDenialReason> {
static String toString(const WebCore::MediaPlaybackDenialReason reason)
{
return convertEnumerationToString(reason);
}
};
}; // namespace WTF
SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::MediaElementSession)
static bool isType(const WebCore::PlatformMediaSession& session) { return WebCore::MediaElementSession::isMediaElementSessionMediaType(session.mediaType()); }
SPECIALIZE_TYPE_TRAITS_END()
#endif // ENABLE(VIDEO)
|