Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/gmp/GMPVideoEncodedFrameImpl.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "GMPVideoEncodedFrameImpl.h"
7
#include "GMPVideoHost.h"
8
#include "mozilla/gmp/GMPTypes.h"
9
#include "GMPSharedMemManager.h"
10
11
namespace mozilla {
12
namespace gmp {
13
14
GMPVideoEncodedFrameImpl::GMPVideoEncodedFrameImpl(GMPVideoHostImpl* aHost)
15
: mEncodedWidth(0),
16
  mEncodedHeight(0),
17
  mTimeStamp(0ll),
18
  mDuration(0ll),
19
  mFrameType(kGMPDeltaFrame),
20
  mSize(0),
21
  mCompleteFrame(false),
22
  mHost(aHost),
23
  mBufferType(GMP_BufferSingle)
24
0
{
25
0
  MOZ_ASSERT(aHost);
26
0
  aHost->EncodedFrameCreated(this);
27
0
}
28
29
GMPVideoEncodedFrameImpl::GMPVideoEncodedFrameImpl(const GMPVideoEncodedFrameData& aFrameData,
30
                                                   GMPVideoHostImpl* aHost)
31
: mEncodedWidth(aFrameData.mEncodedWidth()),
32
  mEncodedHeight(aFrameData.mEncodedHeight()),
33
  mTimeStamp(aFrameData.mTimestamp()),
34
  mDuration(aFrameData.mDuration()),
35
  mFrameType(static_cast<GMPVideoFrameType>(aFrameData.mFrameType())),
36
  mSize(aFrameData.mSize()),
37
  mCompleteFrame(aFrameData.mCompleteFrame()),
38
  mHost(aHost),
39
  mBuffer(aFrameData.mBuffer()),
40
  mBufferType(aFrameData.mBufferType())
41
0
{
42
0
  MOZ_ASSERT(aHost);
43
0
  aHost->EncodedFrameCreated(this);
44
0
}
45
46
GMPVideoEncodedFrameImpl::~GMPVideoEncodedFrameImpl()
47
0
{
48
0
  DestroyBuffer();
49
0
  if (mHost) {
50
0
    mHost->EncodedFrameDestroyed(this);
51
0
  }
52
0
}
53
54
GMPVideoFrameFormat
55
GMPVideoEncodedFrameImpl::GetFrameFormat()
56
0
{
57
0
  return kGMPEncodedVideoFrame;
58
0
}
59
60
void
61
GMPVideoEncodedFrameImpl::DoneWithAPI()
62
0
{
63
0
  DestroyBuffer();
64
0
65
0
  // Do this after destroying the buffer because destruction
66
0
  // involves deallocation, which requires a host.
67
0
  mHost = nullptr;
68
0
}
69
70
void
71
GMPVideoEncodedFrameImpl::ActorDestroyed()
72
0
{
73
0
  // Simply clear out Shmem reference, do not attempt to
74
0
  // properly free it. It has already been freed.
75
0
  mBuffer = ipc::Shmem();
76
0
  // No more host.
77
0
  mHost = nullptr;
78
0
}
79
80
bool
81
GMPVideoEncodedFrameImpl::RelinquishFrameData(GMPVideoEncodedFrameData& aFrameData)
82
0
{
83
0
  aFrameData.mEncodedWidth() = mEncodedWidth;
84
0
  aFrameData.mEncodedHeight() = mEncodedHeight;
85
0
  aFrameData.mTimestamp() = mTimeStamp;
86
0
  aFrameData.mDuration() = mDuration;
87
0
  aFrameData.mFrameType() = mFrameType;
88
0
  aFrameData.mSize() = mSize;
89
0
  aFrameData.mCompleteFrame() = mCompleteFrame;
90
0
  aFrameData.mBuffer() = mBuffer;
91
0
  aFrameData.mBufferType() = mBufferType;
92
0
93
0
  // This method is called right before Shmem is sent to another process.
94
0
  // We need to effectively zero out our member copy so that we don't
95
0
  // try to delete Shmem we don't own later.
96
0
  mBuffer = ipc::Shmem();
97
0
98
0
  return true;
99
0
}
100
101
void
102
GMPVideoEncodedFrameImpl::DestroyBuffer()
103
0
{
104
0
  if (mHost && mBuffer.IsWritable()) {
105
0
    mHost->SharedMemMgr()->MgrDeallocShmem(GMPSharedMem::kGMPEncodedData, mBuffer);
106
0
  }
107
0
  mBuffer = ipc::Shmem();
108
0
}
109
110
GMPErr
111
GMPVideoEncodedFrameImpl::CreateEmptyFrame(uint32_t aSize)
112
0
{
113
0
  if (aSize == 0) {
114
0
    DestroyBuffer();
115
0
  } else if (aSize > AllocatedSize()) {
116
0
    DestroyBuffer();
117
0
    if (!mHost->SharedMemMgr()->MgrAllocShmem(GMPSharedMem::kGMPEncodedData, aSize,
118
0
                                              ipc::SharedMemory::TYPE_BASIC, &mBuffer) ||
119
0
        !Buffer()) {
120
0
      return GMPAllocErr;
121
0
    }
122
0
  }
123
0
  mSize = aSize;
124
0
125
0
  return GMPNoErr;
126
0
}
127
128
GMPErr
129
GMPVideoEncodedFrameImpl::CopyFrame(const GMPVideoEncodedFrame& aFrame)
130
0
{
131
0
  auto& f = static_cast<const GMPVideoEncodedFrameImpl&>(aFrame);
132
0
133
0
  if (f.mSize != 0) {
134
0
    GMPErr err = CreateEmptyFrame(f.mSize);
135
0
    if (err != GMPNoErr) {
136
0
      return err;
137
0
    }
138
0
    memcpy(Buffer(), f.Buffer(), f.mSize);
139
0
  }
140
0
  mEncodedWidth = f.mEncodedWidth;
141
0
  mEncodedHeight = f.mEncodedHeight;
142
0
  mTimeStamp = f.mTimeStamp;
143
0
  mDuration = f.mDuration;
144
0
  mFrameType = f.mFrameType;
145
0
  mSize = f.mSize; // already set...
146
0
  mCompleteFrame = f.mCompleteFrame;
147
0
  mBufferType = f.mBufferType;
148
0
  // Don't copy host, that should have been set properly on object creation via host.
149
0
150
0
  return GMPNoErr;
151
0
}
152
153
void
154
GMPVideoEncodedFrameImpl::SetEncodedWidth(uint32_t aEncodedWidth)
155
0
{
156
0
  mEncodedWidth = aEncodedWidth;
157
0
}
158
159
uint32_t
160
GMPVideoEncodedFrameImpl::EncodedWidth()
161
0
{
162
0
  return mEncodedWidth;
163
0
}
164
165
void
166
GMPVideoEncodedFrameImpl::SetEncodedHeight(uint32_t aEncodedHeight)
167
0
{
168
0
  mEncodedHeight = aEncodedHeight;
169
0
}
170
171
uint32_t
172
GMPVideoEncodedFrameImpl::EncodedHeight()
173
0
{
174
0
  return mEncodedHeight;
175
0
}
176
177
void
178
GMPVideoEncodedFrameImpl::SetTimeStamp(uint64_t aTimeStamp)
179
0
{
180
0
  mTimeStamp = aTimeStamp;
181
0
}
182
183
uint64_t
184
GMPVideoEncodedFrameImpl::TimeStamp()
185
0
{
186
0
  return mTimeStamp;
187
0
}
188
189
void
190
GMPVideoEncodedFrameImpl::SetDuration(uint64_t aDuration)
191
0
{
192
0
  mDuration = aDuration;
193
0
}
194
195
uint64_t
196
GMPVideoEncodedFrameImpl::Duration() const
197
0
{
198
0
  return mDuration;
199
0
}
200
201
void
202
GMPVideoEncodedFrameImpl::SetFrameType(GMPVideoFrameType aFrameType)
203
0
{
204
0
  mFrameType = aFrameType;
205
0
}
206
207
GMPVideoFrameType
208
GMPVideoEncodedFrameImpl::FrameType()
209
0
{
210
0
  return mFrameType;
211
0
}
212
213
void
214
GMPVideoEncodedFrameImpl::SetAllocatedSize(uint32_t aNewSize)
215
0
{
216
0
  if (aNewSize <= AllocatedSize()) {
217
0
    return;
218
0
  }
219
0
220
0
  if (!mHost) {
221
0
    return;
222
0
  }
223
0
224
0
  ipc::Shmem new_mem;
225
0
  if (!mHost->SharedMemMgr()->MgrAllocShmem(GMPSharedMem::kGMPEncodedData, aNewSize,
226
0
                                            ipc::SharedMemory::TYPE_BASIC, &new_mem) ||
227
0
      !new_mem.get<uint8_t>()) {
228
0
    return;
229
0
  }
230
0
231
0
  if (mBuffer.IsReadable()) {
232
0
    memcpy(new_mem.get<uint8_t>(), Buffer(), mSize);
233
0
  }
234
0
235
0
  DestroyBuffer();
236
0
237
0
  mBuffer = new_mem;
238
0
}
239
240
uint32_t
241
GMPVideoEncodedFrameImpl::AllocatedSize()
242
0
{
243
0
  if (mBuffer.IsWritable()) {
244
0
    return mBuffer.Size<uint8_t>();
245
0
  }
246
0
  return 0;
247
0
}
248
249
void
250
GMPVideoEncodedFrameImpl::SetSize(uint32_t aSize)
251
0
{
252
0
  mSize = aSize;
253
0
}
254
255
uint32_t
256
GMPVideoEncodedFrameImpl::Size()
257
0
{
258
0
  return mSize;
259
0
}
260
261
void
262
GMPVideoEncodedFrameImpl::SetCompleteFrame(bool aCompleteFrame)
263
0
{
264
0
  mCompleteFrame = aCompleteFrame;
265
0
}
266
267
bool
268
GMPVideoEncodedFrameImpl::CompleteFrame()
269
0
{
270
0
  return mCompleteFrame;
271
0
}
272
273
const uint8_t*
274
GMPVideoEncodedFrameImpl::Buffer() const
275
0
{
276
0
  return mBuffer.get<uint8_t>();
277
0
}
278
279
uint8_t*
280
GMPVideoEncodedFrameImpl::Buffer()
281
0
{
282
0
  return mBuffer.get<uint8_t>();
283
0
}
284
285
void
286
GMPVideoEncodedFrameImpl::Destroy()
287
0
{
288
0
  delete this;
289
0
}
290
291
GMPBufferType
292
GMPVideoEncodedFrameImpl::BufferType() const
293
0
{
294
0
  return mBufferType;
295
0
}
296
297
void
298
GMPVideoEncodedFrameImpl::SetBufferType(GMPBufferType aBufferType)
299
0
{
300
0
  mBufferType = aBufferType;
301
0
}
302
303
} // namespace gmp
304
} // namespace mozilla