1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/HTMLVideoElement.h"
8 #include "mozilla/dom/HTMLVideoElementBinding.h"
9 #include "nsGenericHTMLElement.h"
10 #include "nsGkAtoms.h"
13 #include "nsNodeInfoManager.h"
16 #include "nsThreadUtils.h"
17 #include "ImageContainer.h"
18 #include "VideoFrameContainer.h"
20 #include "nsIScriptSecurityManager.h"
21 #include "nsIXPConnect.h"
25 #include "FrameStatistics.h"
26 #include "MediaError.h"
27 #include "MediaDecoder.h"
28 #include "mozilla/Preferences.h"
29 #include "mozilla/dom/WakeLock.h"
30 #include "mozilla/dom/power/PowerManagerService.h"
31 #include "mozilla/dom/Performance.h"
32 #include "mozilla/dom/TimeRanges.h"
33 #include "mozilla/dom/VideoPlaybackQuality.h"
38 NS_IMPL_NS_NEW_HTML_ELEMENT(Video
)
43 static bool sVideoStatsEnabled
;
45 NS_IMPL_ELEMENT_CLONE(HTMLVideoElement
)
47 HTMLVideoElement::HTMLVideoElement(already_AddRefed
<NodeInfo
>&& aNodeInfo
)
48 : HTMLMediaElement(std::move(aNodeInfo
))
49 , mIsOrientationLocked(false)
51 DecoderDoctorLogger::LogConstruction(this);
54 HTMLVideoElement::~HTMLVideoElement()
56 DecoderDoctorLogger::LogDestruction(this);
59 nsresult
HTMLVideoElement::GetVideoSize(nsIntSize
* size
)
61 if (!mMediaInfo
.HasVideo()) {
62 return NS_ERROR_FAILURE
;
66 return NS_ERROR_FAILURE
;
69 switch (mMediaInfo
.mVideo
.mRotation
) {
70 case VideoInfo::Rotation::kDegree_90
:
71 case VideoInfo::Rotation::kDegree_270
: {
72 size
->width
= mMediaInfo
.mVideo
.mDisplay
.height
;
73 size
->height
= mMediaInfo
.mVideo
.mDisplay
.width
;
76 case VideoInfo::Rotation::kDegree_0
:
77 case VideoInfo::Rotation::kDegree_180
:
79 size
->height
= mMediaInfo
.mVideo
.mDisplay
.height
;
80 size
->width
= mMediaInfo
.mVideo
.mDisplay
.width
;
88 HTMLVideoElement::ParseAttribute(int32_t aNamespaceID
,
90 const nsAString
& aValue
,
91 nsIPrincipal
* aMaybeScriptedPrincipal
,
94 if (aAttribute
== nsGkAtoms::width
|| aAttribute
== nsGkAtoms::height
) {
95 return aResult
.ParseSpecialIntValue(aValue
);
98 return HTMLMediaElement::ParseAttribute(aNamespaceID
, aAttribute
, aValue
,
99 aMaybeScriptedPrincipal
, aResult
);
103 HTMLVideoElement::MapAttributesIntoRule(const nsMappedAttributes
* aAttributes
,
104 MappedDeclarations
& aDecls
)
106 nsGenericHTMLElement::MapImageSizeAttributesInto(aAttributes
, aDecls
);
107 nsGenericHTMLElement::MapCommonAttributesInto(aAttributes
, aDecls
);
111 HTMLVideoElement::IsAttributeMapped(const nsAtom
* aAttribute
) const
113 static const MappedAttributeEntry attributes
[] = {
114 { nsGkAtoms::width
},
115 { nsGkAtoms::height
},
119 static const MappedAttributeEntry
* const map
[] = {
124 return FindAttributeDependence(aAttribute
, map
);
127 nsMapRuleToAttributesFunc
128 HTMLVideoElement::GetAttributeMappingFunction() const
130 return &MapAttributesIntoRule
;
133 nsresult
HTMLVideoElement::SetAcceptHeader(nsIHttpChannel
* aChannel
)
139 "application/ogg;q=0.7,"
140 "audio/*;q=0.6,*/*;q=0.5");
142 return aChannel
->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
148 HTMLVideoElement::IsInteractiveHTMLContent(bool aIgnoreTabindex
) const
150 return HasAttr(kNameSpaceID_None
, nsGkAtoms::controls
) ||
151 HTMLMediaElement::IsInteractiveHTMLContent(aIgnoreTabindex
);
154 uint32_t HTMLVideoElement::MozParsedFrames() const
156 MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
157 if (!IsVideoStatsEnabled()) {
161 if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
162 return nsRFPService::GetSpoofedTotalFrames(TotalPlayTime());
165 return mDecoder
? mDecoder
->GetFrameStatistics().GetParsedFrames() : 0;
168 uint32_t HTMLVideoElement::MozDecodedFrames() const
170 MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
171 if (!IsVideoStatsEnabled()) {
175 if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
176 return nsRFPService::GetSpoofedTotalFrames(TotalPlayTime());
179 return mDecoder
? mDecoder
->GetFrameStatistics().GetDecodedFrames() : 0;
182 uint32_t HTMLVideoElement::MozPresentedFrames() const
184 MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
185 if (!IsVideoStatsEnabled()) {
189 if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
190 return nsRFPService::GetSpoofedPresentedFrames(TotalPlayTime(), VideoWidth(), VideoHeight());
193 return mDecoder
? mDecoder
->GetFrameStatistics().GetPresentedFrames() : 0;
196 uint32_t HTMLVideoElement::MozPaintedFrames()
198 MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
199 if (!IsVideoStatsEnabled()) {
203 if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
204 return nsRFPService::GetSpoofedPresentedFrames(TotalPlayTime(), VideoWidth(), VideoHeight());
207 layers::ImageContainer
* container
= GetImageContainer();
208 return container
? container
->GetPaintCount() : 0;
211 double HTMLVideoElement::MozFrameDelay()
213 MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
215 if (!IsVideoStatsEnabled() ||
216 nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
220 VideoFrameContainer
* container
= GetVideoFrameContainer();
221 // Hide negative delays. Frame timing tweaks in the compositor (e.g.
222 // adding a bias value to prevent multiple dropped/duped frames when
223 // frame times are aligned with composition times) may produce apparent
224 // negative delay, but we shouldn't report that.
225 return container
? std::max(0.0, container
->GetFrameDelay()) : 0.0;
228 bool HTMLVideoElement::MozHasAudio() const
230 MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
235 HTMLVideoElement::WrapNode(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
)
237 return HTMLVideoElement_Binding::Wrap(aCx
, this, aGivenProto
);
241 HTMLVideoElement::GetFrameStatistics()
243 return mDecoder
? &(mDecoder
->GetFrameStatistics()) : nullptr;
246 already_AddRefed
<VideoPlaybackQuality
>
247 HTMLVideoElement::GetVideoPlaybackQuality()
249 DOMHighResTimeStamp creationTime
= 0;
250 uint32_t totalFrames
= 0;
251 uint32_t droppedFrames
= 0;
252 uint32_t corruptedFrames
= 0;
254 if (IsVideoStatsEnabled()) {
255 if (nsPIDOMWindowInner
* window
= OwnerDoc()->GetInnerWindow()) {
256 Performance
* perf
= window
->GetPerformance();
258 creationTime
= perf
->Now();
263 if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
264 totalFrames
= nsRFPService::GetSpoofedTotalFrames(TotalPlayTime());
265 droppedFrames
= nsRFPService::GetSpoofedDroppedFrames(TotalPlayTime(),
270 FrameStatisticsData stats
=
271 mDecoder
->GetFrameStatistics().GetFrameStatisticsData();
272 if (sizeof(totalFrames
) >= sizeof(stats
.mParsedFrames
)) {
273 totalFrames
= stats
.mPresentedFrames
+ stats
.mDroppedFrames
;
274 droppedFrames
= stats
.mDroppedFrames
;
276 uint64_t total
= stats
.mPresentedFrames
+ stats
.mDroppedFrames
;
277 const auto maxNumber
= std::numeric_limits
<uint32_t>::max();
278 if (total
<= maxNumber
) {
279 totalFrames
= uint32_t(total
);
280 droppedFrames
= uint32_t(stats
.mDroppedFrames
);
282 // Too big number(s) -> Resize everything to fit in 32 bits.
283 double ratio
= double(maxNumber
) / double(total
);
284 totalFrames
= maxNumber
; // === total * ratio
285 droppedFrames
= uint32_t(double(stats
.mDroppedFrames
) * ratio
);
293 RefPtr
<VideoPlaybackQuality
> playbackQuality
=
294 new VideoPlaybackQuality(this, creationTime
, totalFrames
, droppedFrames
,
296 return playbackQuality
.forget();
301 HTMLVideoElement::WakeLockRelease()
303 HTMLMediaElement::WakeLockRelease();
304 ReleaseVideoWakeLockIfExists();
308 HTMLVideoElement::UpdateWakeLock()
310 HTMLMediaElement::UpdateWakeLock();
312 CreateVideoWakeLockIfNeeded();
314 ReleaseVideoWakeLockIfExists();
319 HTMLVideoElement::ShouldCreateVideoWakeLock() const
321 // Make sure we only request wake lock for video with audio track, because
322 // video without audio track is often used as background image which seems no
323 // need to hold a wakelock.
324 return HasVideo() && HasAudio();
328 HTMLVideoElement::CreateVideoWakeLockIfNeeded()
330 if (!mScreenWakeLock
&& ShouldCreateVideoWakeLock()) {
331 RefPtr
<power::PowerManagerService
> pmService
=
332 power::PowerManagerService::GetInstance();
333 NS_ENSURE_TRUE_VOID(pmService
);
336 mScreenWakeLock
= pmService
->NewWakeLock(NS_LITERAL_STRING("video-playing"),
337 OwnerDoc()->GetInnerWindow(),
343 HTMLVideoElement::ReleaseVideoWakeLockIfExists()
345 if (mScreenWakeLock
) {
347 mScreenWakeLock
->Unlock(rv
);
348 rv
.SuppressException();
349 mScreenWakeLock
= nullptr;
355 HTMLVideoElement::Init()
357 Preferences::AddBoolVarCache(&sVideoStatsEnabled
, "media.video_stats.enabled");
362 HTMLVideoElement::IsVideoStatsEnabled()
364 return sVideoStatsEnabled
;
368 HTMLVideoElement::TotalPlayTime() const
373 uint32_t timeRangeCount
= mPlayed
->Length();
375 for (uint32_t i
= 0; i
< timeRangeCount
; i
++) {
376 double begin
= mPlayed
->Start(i
);
377 double end
= mPlayed
->End(i
);
378 total
+= end
- begin
;
381 if (mCurrentPlayRangeStart
!= -1.0) {
382 double now
= CurrentTime();
383 if (mCurrentPlayRangeStart
!= now
) {
384 total
+= now
- mCurrentPlayRangeStart
;
393 } // namespace mozilla