Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/html/HTMLVideoElement.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
6
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"
19
20
#include "nsIScriptSecurityManager.h"
21
#include "nsIXPConnect.h"
22
23
#include "nsITimer.h"
24
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"
34
35
#include <algorithm>
36
#include <limits>
37
38
NS_IMPL_NS_NEW_HTML_ELEMENT(Video)
39
40
namespace mozilla {
41
namespace dom {
42
43
static bool sVideoStatsEnabled;
44
45
NS_IMPL_ELEMENT_CLONE(HTMLVideoElement)
46
47
HTMLVideoElement::HTMLVideoElement(already_AddRefed<NodeInfo>&& aNodeInfo)
48
  : HTMLMediaElement(std::move(aNodeInfo))
49
  , mIsOrientationLocked(false)
50
0
{
51
0
  DecoderDoctorLogger::LogConstruction(this);
52
0
}
53
54
HTMLVideoElement::~HTMLVideoElement()
55
0
{
56
0
  DecoderDoctorLogger::LogDestruction(this);
57
0
}
58
59
nsresult HTMLVideoElement::GetVideoSize(nsIntSize* size)
60
0
{
61
0
  if (!mMediaInfo.HasVideo()) {
62
0
    return NS_ERROR_FAILURE;
63
0
  }
64
0
65
0
  if (mDisableVideo) {
66
0
    return NS_ERROR_FAILURE;
67
0
  }
68
0
69
0
  switch (mMediaInfo.mVideo.mRotation) {
70
0
    case VideoInfo::Rotation::kDegree_90:
71
0
    case VideoInfo::Rotation::kDegree_270: {
72
0
      size->width = mMediaInfo.mVideo.mDisplay.height;
73
0
      size->height = mMediaInfo.mVideo.mDisplay.width;
74
0
      break;
75
0
    }
76
0
    case VideoInfo::Rotation::kDegree_0:
77
0
    case VideoInfo::Rotation::kDegree_180:
78
0
    default: {
79
0
      size->height = mMediaInfo.mVideo.mDisplay.height;
80
0
      size->width = mMediaInfo.mVideo.mDisplay.width;
81
0
      break;
82
0
    }
83
0
  }
84
0
  return NS_OK;
85
0
}
86
87
bool
88
HTMLVideoElement::ParseAttribute(int32_t aNamespaceID,
89
                                 nsAtom* aAttribute,
90
                                 const nsAString& aValue,
91
                                 nsIPrincipal* aMaybeScriptedPrincipal,
92
                                 nsAttrValue& aResult)
93
0
{
94
0
   if (aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height) {
95
0
     return aResult.ParseSpecialIntValue(aValue);
96
0
   }
97
0
98
0
   return HTMLMediaElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
99
0
                                           aMaybeScriptedPrincipal, aResult);
100
0
}
101
102
void
103
HTMLVideoElement::MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
104
                                        MappedDeclarations& aDecls)
105
0
{
106
0
  nsGenericHTMLElement::MapImageSizeAttributesInto(aAttributes, aDecls);
107
0
  nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aDecls);
108
0
}
109
110
NS_IMETHODIMP_(bool)
111
HTMLVideoElement::IsAttributeMapped(const nsAtom* aAttribute) const
112
0
{
113
0
  static const MappedAttributeEntry attributes[] = {
114
0
    { &nsGkAtoms::width },
115
0
    { &nsGkAtoms::height },
116
0
    { nullptr }
117
0
  };
118
0
119
0
  static const MappedAttributeEntry* const map[] = {
120
0
    attributes,
121
0
    sCommonAttributeMap
122
0
  };
123
0
124
0
  return FindAttributeDependence(aAttribute, map);
125
0
}
126
127
nsMapRuleToAttributesFunc
128
HTMLVideoElement::GetAttributeMappingFunction() const
129
0
{
130
0
  return &MapAttributesIntoRule;
131
0
}
132
133
nsresult HTMLVideoElement::SetAcceptHeader(nsIHttpChannel* aChannel)
134
0
{
135
0
  nsAutoCString value(
136
0
      "video/webm,"
137
0
      "video/ogg,"
138
0
      "video/*;q=0.9,"
139
0
      "application/ogg;q=0.7,"
140
0
      "audio/*;q=0.6,*/*;q=0.5");
141
0
142
0
  return aChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
143
0
                                    value,
144
0
                                    false);
145
0
}
146
147
bool
148
HTMLVideoElement::IsInteractiveHTMLContent(bool aIgnoreTabindex) const
149
0
{
150
0
  return HasAttr(kNameSpaceID_None, nsGkAtoms::controls) ||
151
0
         HTMLMediaElement::IsInteractiveHTMLContent(aIgnoreTabindex);
152
0
}
153
154
uint32_t HTMLVideoElement::MozParsedFrames() const
155
0
{
156
0
  MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
157
0
  if (!IsVideoStatsEnabled()) {
158
0
    return 0;
159
0
  }
160
0
161
0
  if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
162
0
    return nsRFPService::GetSpoofedTotalFrames(TotalPlayTime());
163
0
  }
164
0
165
0
  return mDecoder ? mDecoder->GetFrameStatistics().GetParsedFrames() : 0;
166
0
}
167
168
uint32_t HTMLVideoElement::MozDecodedFrames() const
169
0
{
170
0
  MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
171
0
  if (!IsVideoStatsEnabled()) {
172
0
    return 0;
173
0
  }
174
0
175
0
  if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
176
0
    return nsRFPService::GetSpoofedTotalFrames(TotalPlayTime());
177
0
  }
178
0
179
0
  return mDecoder ? mDecoder->GetFrameStatistics().GetDecodedFrames() : 0;
180
0
}
181
182
uint32_t HTMLVideoElement::MozPresentedFrames() const
183
0
{
184
0
  MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
185
0
  if (!IsVideoStatsEnabled()) {
186
0
    return 0;
187
0
  }
188
0
189
0
  if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
190
0
    return nsRFPService::GetSpoofedPresentedFrames(TotalPlayTime(), VideoWidth(), VideoHeight());
191
0
  }
192
0
193
0
  return mDecoder ? mDecoder->GetFrameStatistics().GetPresentedFrames() : 0;
194
0
}
195
196
uint32_t HTMLVideoElement::MozPaintedFrames()
197
0
{
198
0
  MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
199
0
  if (!IsVideoStatsEnabled()) {
200
0
    return 0;
201
0
  }
202
0
203
0
  if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
204
0
    return nsRFPService::GetSpoofedPresentedFrames(TotalPlayTime(), VideoWidth(), VideoHeight());
205
0
  }
206
0
207
0
  layers::ImageContainer* container = GetImageContainer();
208
0
  return container ? container->GetPaintCount() : 0;
209
0
}
210
211
double HTMLVideoElement::MozFrameDelay()
212
0
{
213
0
  MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
214
0
215
0
  if (!IsVideoStatsEnabled() ||
216
0
      nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
217
0
    return 0.0;
218
0
  }
219
0
220
0
  VideoFrameContainer* container = GetVideoFrameContainer();
221
0
  // Hide negative delays. Frame timing tweaks in the compositor (e.g.
222
0
  // adding a bias value to prevent multiple dropped/duped frames when
223
0
  // frame times are aligned with composition times) may produce apparent
224
0
  // negative delay, but we shouldn't report that.
225
0
  return container ? std::max(0.0, container->GetFrameDelay()) : 0.0;
226
0
}
227
228
bool HTMLVideoElement::MozHasAudio() const
229
0
{
230
0
  MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
231
0
  return HasAudio();
232
0
}
233
234
JSObject*
235
HTMLVideoElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
236
0
{
237
0
  return HTMLVideoElement_Binding::Wrap(aCx, this, aGivenProto);
238
0
}
239
240
FrameStatistics*
241
HTMLVideoElement::GetFrameStatistics()
242
0
{
243
0
  return mDecoder ? &(mDecoder->GetFrameStatistics()) : nullptr;
244
0
}
245
246
already_AddRefed<VideoPlaybackQuality>
247
HTMLVideoElement::GetVideoPlaybackQuality()
248
0
{
249
0
  DOMHighResTimeStamp creationTime = 0;
250
0
  uint32_t totalFrames = 0;
251
0
  uint32_t droppedFrames = 0;
252
0
  uint32_t corruptedFrames = 0;
253
0
254
0
  if (IsVideoStatsEnabled()) {
255
0
    if (nsPIDOMWindowInner* window = OwnerDoc()->GetInnerWindow()) {
256
0
      Performance* perf = window->GetPerformance();
257
0
      if (perf) {
258
0
        creationTime = perf->Now();
259
0
      }
260
0
    }
261
0
262
0
    if (mDecoder) {
263
0
      if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
264
0
        totalFrames = nsRFPService::GetSpoofedTotalFrames(TotalPlayTime());
265
0
        droppedFrames = nsRFPService::GetSpoofedDroppedFrames(TotalPlayTime(),
266
0
                                                              VideoWidth(),
267
0
                                                              VideoHeight());
268
0
        corruptedFrames = 0;
269
0
      } else {
270
0
        FrameStatisticsData stats =
271
0
          mDecoder->GetFrameStatistics().GetFrameStatisticsData();
272
0
        if (sizeof(totalFrames) >= sizeof(stats.mParsedFrames)) {
273
0
          totalFrames = stats.mPresentedFrames + stats.mDroppedFrames;
274
0
          droppedFrames = stats.mDroppedFrames;
275
0
        } else {
276
0
          uint64_t total = stats.mPresentedFrames + stats.mDroppedFrames;
277
0
          const auto maxNumber = std::numeric_limits<uint32_t>::max();
278
0
          if (total <= maxNumber) {
279
0
            totalFrames = uint32_t(total);
280
0
            droppedFrames = uint32_t(stats.mDroppedFrames);
281
0
          } else {
282
0
            // Too big number(s) -> Resize everything to fit in 32 bits.
283
0
            double ratio = double(maxNumber) / double(total);
284
0
            totalFrames = maxNumber; // === total * ratio
285
0
            droppedFrames = uint32_t(double(stats.mDroppedFrames) * ratio);
286
0
          }
287
0
        }
288
0
        corruptedFrames = 0;
289
0
      }
290
0
    }
291
0
  }
292
0
293
0
  RefPtr<VideoPlaybackQuality> playbackQuality =
294
0
    new VideoPlaybackQuality(this, creationTime, totalFrames, droppedFrames,
295
0
                             corruptedFrames);
296
0
  return playbackQuality.forget();
297
0
}
298
299
void
300
HTMLVideoElement::WakeLockCreate()
301
0
{
302
0
  HTMLMediaElement::WakeLockCreate();
303
0
  UpdateScreenWakeLock();
304
0
}
305
306
void
307
HTMLVideoElement::WakeLockRelease()
308
0
{
309
0
  UpdateScreenWakeLock();
310
0
  HTMLMediaElement::WakeLockRelease();
311
0
}
312
313
void
314
HTMLVideoElement::UpdateScreenWakeLock()
315
0
{
316
0
  if (mScreenWakeLock && mPaused) {
317
0
    ErrorResult rv;
318
0
    mScreenWakeLock->Unlock(rv);
319
0
    rv.SuppressException();
320
0
    mScreenWakeLock = nullptr;
321
0
    return;
322
0
  }
323
0
324
0
  if (!mScreenWakeLock && !mPaused && HasVideo()) {
325
0
    RefPtr<power::PowerManagerService> pmService =
326
0
      power::PowerManagerService::GetInstance();
327
0
    NS_ENSURE_TRUE_VOID(pmService);
328
0
329
0
    ErrorResult rv;
330
0
    mScreenWakeLock = pmService->NewWakeLock(NS_LITERAL_STRING("video-playing"),
331
0
                                             OwnerDoc()->GetInnerWindow(),
332
0
                                             rv);
333
0
  }
334
0
}
335
336
void
337
HTMLVideoElement::Init()
338
3
{
339
3
  Preferences::AddBoolVarCache(&sVideoStatsEnabled, "media.video_stats.enabled");
340
3
}
341
342
/* static */
343
bool
344
HTMLVideoElement::IsVideoStatsEnabled()
345
0
{
346
0
  return sVideoStatsEnabled;
347
0
}
348
349
double
350
HTMLVideoElement::TotalPlayTime() const
351
0
{
352
0
  double total = 0.0;
353
0
354
0
  if (mPlayed) {
355
0
    uint32_t timeRangeCount = mPlayed->Length();
356
0
357
0
    for (uint32_t i = 0; i < timeRangeCount; i++) {
358
0
      double begin = mPlayed->Start(i);
359
0
      double end = mPlayed->End(i);
360
0
      total += end - begin;
361
0
    }
362
0
363
0
    if (mCurrentPlayRangeStart != -1.0) {
364
0
      double now = CurrentTime();
365
0
      if (mCurrentPlayRangeStart != now) {
366
0
        total += now - mCurrentPlayRangeStart;
367
0
      }
368
0
    }
369
0
  }
370
0
371
0
  return total;
372
0
}
373
374
} // namespace dom
375
} // namespace mozilla