Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/platforms/agnostic/BlankDecoderModule.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
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 "BlankDecoderModule.h"
8
9
#include "mozilla/CheckedInt.h"
10
#include "mozilla/UniquePtrExtensions.h"
11
#include "mozilla/RefPtr.h"
12
#include "mozilla/gfx/Rect.h"
13
#include "mozilla/gfx/Point.h"
14
#include "ImageContainer.h"
15
#include "MediaData.h"
16
#include "MediaInfo.h"
17
#include "VideoUtils.h"
18
19
namespace mozilla {
20
21
BlankVideoDataCreator::BlankVideoDataCreator(uint32_t aFrameWidth,
22
                                             uint32_t aFrameHeight,
23
                                             layers::ImageContainer* aImageContainer)
24
  : mFrameWidth(aFrameWidth)
25
  , mFrameHeight(aFrameHeight)
26
  , mImageContainer(aImageContainer)
27
0
{
28
0
  mInfo.mDisplay = gfx::IntSize(mFrameWidth, mFrameHeight);
29
0
  mPicture = gfx::IntRect(0, 0, mFrameWidth, mFrameHeight);
30
0
}
31
32
already_AddRefed<MediaData>
33
BlankVideoDataCreator::Create(MediaRawData* aSample)
34
0
{
35
0
  // Create a fake YUV buffer in a 420 format. That is, an 8bpp Y plane,
36
0
  // with a U and V plane that are half the size of the Y plane, i.e 8 bit,
37
0
  // 2x2 subsampled. Have the data pointer of each frame point to the
38
0
  // first plane, they'll always be zero'd memory anyway.
39
0
  const CheckedUint32 size = CheckedUint32(mFrameWidth) * mFrameHeight;
40
0
  if (!size.isValid()) {
41
0
    // Overflow happened.
42
0
    return nullptr;
43
0
  }
44
0
  auto frame = MakeUniqueFallible<uint8_t[]>(size.value());
45
0
  if (!frame) {
46
0
    return nullptr;
47
0
  }
48
0
  memset(frame.get(), 0, mFrameWidth * mFrameHeight);
49
0
  VideoData::YCbCrBuffer buffer;
50
0
51
0
  // Y plane.
52
0
  buffer.mPlanes[0].mData = frame.get();
53
0
  buffer.mPlanes[0].mStride = mFrameWidth;
54
0
  buffer.mPlanes[0].mHeight = mFrameHeight;
55
0
  buffer.mPlanes[0].mWidth = mFrameWidth;
56
0
  buffer.mPlanes[0].mOffset = 0;
57
0
  buffer.mPlanes[0].mSkip = 0;
58
0
59
0
  // Cb plane.
60
0
  buffer.mPlanes[1].mData = frame.get();
61
0
  buffer.mPlanes[1].mStride = (mFrameWidth + 1) / 2;
62
0
  buffer.mPlanes[1].mHeight = (mFrameHeight + 1) / 2;
63
0
  buffer.mPlanes[1].mWidth = (mFrameWidth + 1) / 2;
64
0
  buffer.mPlanes[1].mOffset = 0;
65
0
  buffer.mPlanes[1].mSkip = 0;
66
0
67
0
  // Cr plane.
68
0
  buffer.mPlanes[2].mData = frame.get();
69
0
  buffer.mPlanes[2].mStride = (mFrameWidth + 1) / 2;
70
0
  buffer.mPlanes[2].mHeight = (mFrameHeight + 1) / 2;
71
0
  buffer.mPlanes[2].mWidth = (mFrameWidth + 1) / 2;
72
0
  buffer.mPlanes[2].mOffset = 0;
73
0
  buffer.mPlanes[2].mSkip = 0;
74
0
75
0
  return VideoData::CreateAndCopyData(mInfo,
76
0
                                      mImageContainer,
77
0
                                      aSample->mOffset,
78
0
                                      aSample->mTime,
79
0
                                      aSample->mDuration,
80
0
                                      buffer,
81
0
                                      aSample->mKeyframe,
82
0
                                      aSample->mTime,
83
0
                                      mPicture);
84
0
}
85
86
BlankAudioDataCreator::BlankAudioDataCreator(uint32_t aChannelCount, uint32_t aSampleRate)
87
  : mFrameSum(0), mChannelCount(aChannelCount), mSampleRate(aSampleRate)
88
0
{
89
0
}
90
91
already_AddRefed<MediaData>
92
BlankAudioDataCreator::Create(MediaRawData* aSample)
93
0
{
94
0
  // Convert duration to frames. We add 1 to duration to account for
95
0
  // rounding errors, so we get a consistent tone.
96
0
  CheckedInt64 frames = UsecsToFrames(
97
0
    aSample->mDuration.ToMicroseconds()+1, mSampleRate);
98
0
  if (!frames.isValid() ||
99
0
      !mChannelCount ||
100
0
      !mSampleRate ||
101
0
      frames.value() > (UINT32_MAX / mChannelCount)) {
102
0
    return nullptr;
103
0
  }
104
0
  AlignedAudioBuffer samples(frames.value() * mChannelCount);
105
0
  if (!samples) {
106
0
    return nullptr;
107
0
  }
108
0
  // Fill the sound buffer with an A4 tone.
109
0
  static const float pi = 3.14159265f;
110
0
  static const float noteHz = 440.0f;
111
0
  for (int i = 0; i < frames.value(); i++) {
112
0
    float f = sin(2 * pi * noteHz * mFrameSum / mSampleRate);
113
0
    for (unsigned c = 0; c < mChannelCount; c++) {
114
0
      samples[i * mChannelCount + c] = AudioDataValue(f);
115
0
    }
116
0
    mFrameSum++;
117
0
  }
118
0
  RefPtr<AudioData> data(new AudioData(aSample->mOffset,
119
0
                                       aSample->mTime,
120
0
                                       aSample->mDuration,
121
0
                                       uint32_t(frames.value()),
122
0
                                       std::move(samples),
123
0
                                       mChannelCount,
124
0
                                       mSampleRate));
125
0
  return data.forget();
126
0
}
127
128
already_AddRefed<MediaDataDecoder>
129
BlankDecoderModule::CreateVideoDecoder(const CreateDecoderParams& aParams)
130
0
{
131
0
  const VideoInfo& config = aParams.VideoConfig();
132
0
  UniquePtr<DummyDataCreator> creator =
133
0
    MakeUnique<BlankVideoDataCreator>(config.mDisplay.width, config.mDisplay.height, aParams.mImageContainer);
134
0
  RefPtr<MediaDataDecoder> decoder = new DummyMediaDataDecoder(
135
0
    std::move(creator), NS_LITERAL_CSTRING("blank media data decoder"), aParams);
136
0
  return decoder.forget();
137
0
}
138
139
already_AddRefed<MediaDataDecoder>
140
BlankDecoderModule::CreateAudioDecoder(const CreateDecoderParams& aParams)
141
0
{
142
0
  const AudioInfo& config = aParams.AudioConfig();
143
0
  UniquePtr<DummyDataCreator> creator =
144
0
    MakeUnique<BlankAudioDataCreator>(config.mChannels, config.mRate);
145
0
  RefPtr<MediaDataDecoder> decoder = new DummyMediaDataDecoder(
146
0
    std::move(creator), NS_LITERAL_CSTRING("blank media data decoder"), aParams);
147
0
  return decoder.forget();
148
0
}
149
150
bool
151
BlankDecoderModule::SupportsMimeType(const nsACString& aMimeType,
152
                                     DecoderDoctorDiagnostics* aDiagnostics) const
153
0
{
154
0
  return true;
155
0
}
156
157
already_AddRefed<PlatformDecoderModule> CreateBlankDecoderModule()
158
0
{
159
0
  RefPtr<PlatformDecoderModule> pdm = new BlankDecoderModule();
160
0
  return pdm.forget();
161
0
}
162
163
} // namespace mozilla