Bug 1494333 - index crons just like artifacts r=Callek
[gecko.git] / dom / html / HTMLVideoElement.cpp
blobe890eecf0cb2709a4d6eea61c7d998fa56ed28e5
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"
11 #include "nsSize.h"
12 #include "nsError.h"
13 #include "nsNodeInfoManager.h"
14 #include "plbase64.h"
15 #include "prlock.h"
16 #include "nsThreadUtils.h"
17 #include "ImageContainer.h"
18 #include "VideoFrameContainer.h"
20 #include "nsIScriptSecurityManager.h"
21 #include "nsIXPConnect.h"
23 #include "nsITimer.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"
35 #include <algorithm>
36 #include <limits>
38 NS_IMPL_NS_NEW_HTML_ELEMENT(Video)
40 namespace mozilla {
41 namespace dom {
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;
65 if (mDisableVideo) {
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;
74 break;
76 case VideoInfo::Rotation::kDegree_0:
77 case VideoInfo::Rotation::kDegree_180:
78 default: {
79 size->height = mMediaInfo.mVideo.mDisplay.height;
80 size->width = mMediaInfo.mVideo.mDisplay.width;
81 break;
84 return NS_OK;
87 bool
88 HTMLVideoElement::ParseAttribute(int32_t aNamespaceID,
89 nsAtom* aAttribute,
90 const nsAString& aValue,
91 nsIPrincipal* aMaybeScriptedPrincipal,
92 nsAttrValue& aResult)
94 if (aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height) {
95 return aResult.ParseSpecialIntValue(aValue);
98 return HTMLMediaElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
99 aMaybeScriptedPrincipal, aResult);
102 void
103 HTMLVideoElement::MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
104 MappedDeclarations& aDecls)
106 nsGenericHTMLElement::MapImageSizeAttributesInto(aAttributes, aDecls);
107 nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aDecls);
110 NS_IMETHODIMP_(bool)
111 HTMLVideoElement::IsAttributeMapped(const nsAtom* aAttribute) const
113 static const MappedAttributeEntry attributes[] = {
114 { nsGkAtoms::width },
115 { nsGkAtoms::height },
116 { nullptr }
119 static const MappedAttributeEntry* const map[] = {
120 attributes,
121 sCommonAttributeMap
124 return FindAttributeDependence(aAttribute, map);
127 nsMapRuleToAttributesFunc
128 HTMLVideoElement::GetAttributeMappingFunction() const
130 return &MapAttributesIntoRule;
133 nsresult HTMLVideoElement::SetAcceptHeader(nsIHttpChannel* aChannel)
135 nsAutoCString value(
136 "video/webm,"
137 "video/ogg,"
138 "video/*;q=0.9,"
139 "application/ogg;q=0.7,"
140 "audio/*;q=0.6,*/*;q=0.5");
142 return aChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
143 value,
144 false);
147 bool
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()) {
158 return 0;
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()) {
172 return 0;
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()) {
186 return 0;
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()) {
200 return 0;
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())) {
217 return 0.0;
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.");
231 return HasAudio();
234 JSObject*
235 HTMLVideoElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
237 return HTMLVideoElement_Binding::Wrap(aCx, this, aGivenProto);
240 FrameStatistics*
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();
257 if (perf) {
258 creationTime = perf->Now();
262 if (mDecoder) {
263 if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
264 totalFrames = nsRFPService::GetSpoofedTotalFrames(TotalPlayTime());
265 droppedFrames = nsRFPService::GetSpoofedDroppedFrames(TotalPlayTime(),
266 VideoWidth(),
267 VideoHeight());
268 corruptedFrames = 0;
269 } else {
270 FrameStatisticsData stats =
271 mDecoder->GetFrameStatistics().GetFrameStatisticsData();
272 if (sizeof(totalFrames) >= sizeof(stats.mParsedFrames)) {
273 totalFrames = stats.mPresentedFrames + stats.mDroppedFrames;
274 droppedFrames = stats.mDroppedFrames;
275 } else {
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);
281 } else {
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);
288 corruptedFrames = 0;
293 RefPtr<VideoPlaybackQuality> playbackQuality =
294 new VideoPlaybackQuality(this, creationTime, totalFrames, droppedFrames,
295 corruptedFrames);
296 return playbackQuality.forget();
300 void
301 HTMLVideoElement::WakeLockRelease()
303 HTMLMediaElement::WakeLockRelease();
304 ReleaseVideoWakeLockIfExists();
307 void
308 HTMLVideoElement::UpdateWakeLock()
310 HTMLMediaElement::UpdateWakeLock();
311 if (!mPaused) {
312 CreateVideoWakeLockIfNeeded();
313 } else {
314 ReleaseVideoWakeLockIfExists();
318 bool
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();
327 void
328 HTMLVideoElement::CreateVideoWakeLockIfNeeded()
330 if (!mScreenWakeLock && ShouldCreateVideoWakeLock()) {
331 RefPtr<power::PowerManagerService> pmService =
332 power::PowerManagerService::GetInstance();
333 NS_ENSURE_TRUE_VOID(pmService);
335 ErrorResult rv;
336 mScreenWakeLock = pmService->NewWakeLock(NS_LITERAL_STRING("video-playing"),
337 OwnerDoc()->GetInnerWindow(),
338 rv);
342 void
343 HTMLVideoElement::ReleaseVideoWakeLockIfExists()
345 if (mScreenWakeLock) {
346 ErrorResult rv;
347 mScreenWakeLock->Unlock(rv);
348 rv.SuppressException();
349 mScreenWakeLock = nullptr;
350 return;
354 void
355 HTMLVideoElement::Init()
357 Preferences::AddBoolVarCache(&sVideoStatsEnabled, "media.video_stats.enabled");
360 /* static */
361 bool
362 HTMLVideoElement::IsVideoStatsEnabled()
364 return sVideoStatsEnabled;
367 double
368 HTMLVideoElement::TotalPlayTime() const
370 double total = 0.0;
372 if (mPlayed) {
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;
389 return total;
392 } // namespace dom
393 } // namespace mozilla