1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "RemoteMediaData.h"
#include "PerformanceRecorder.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/dom/MediaIPCUtils.h"
#include "mozilla/ipc/Shmem.h"
namespace mozilla {
bool RemoteArrayOfByteBuffer::AllocateShmem(
size_t aSize, std::function<ShmemBuffer(size_t)>& aAllocator) {
ShmemBuffer buffer = aAllocator(aSize);
if (!buffer.Valid()) {
return false;
}
mBuffers.emplace(std::move(buffer.Get()));
return true;
}
uint8_t* RemoteArrayOfByteBuffer::BuffersStartAddress() const {
MOZ_ASSERT(mBuffers);
return mBuffers->get<uint8_t>();
}
bool RemoteArrayOfByteBuffer::Check(size_t aOffset, size_t aSizeInBytes) const {
return mBuffers && mBuffers->IsReadable() &&
detail::IsAddValid(aOffset, aSizeInBytes) &&
aOffset + aSizeInBytes <= mBuffers->Size<uint8_t>();
}
void RemoteArrayOfByteBuffer::Write(size_t aOffset, const void* aSourceAddr,
size_t aSizeInBytes) {
if (!aSizeInBytes) {
return;
}
MOZ_DIAGNOSTIC_ASSERT(Check(aOffset, aSizeInBytes),
"Allocated Shmem is too small");
memcpy(BuffersStartAddress() + aOffset, aSourceAddr, aSizeInBytes);
}
RemoteArrayOfByteBuffer::RemoteArrayOfByteBuffer() = default;
RemoteArrayOfByteBuffer::RemoteArrayOfByteBuffer(
const nsTArray<RefPtr<MediaByteBuffer>>& aArray,
std::function<ShmemBuffer(size_t)>& aAllocator) {
// Determine the total size we will need for this object.
size_t totalSize = 0;
for (const auto& buffer : aArray) {
if (buffer) {
totalSize += buffer->Length();
}
}
if (totalSize) {
if (!AllocateShmem(totalSize, aAllocator)) {
return;
}
}
size_t offset = 0;
for (const auto& buffer : aArray) {
size_t sizeBuffer = buffer ? buffer->Length() : 0;
if (totalSize && sizeBuffer) {
Write(offset, buffer->Elements(), sizeBuffer);
}
mOffsets.AppendElement(OffsetEntry{offset, sizeBuffer});
offset += sizeBuffer;
}
mIsValid = true;
}
RemoteArrayOfByteBuffer& RemoteArrayOfByteBuffer::operator=(
RemoteArrayOfByteBuffer&& aOther) noexcept {
mIsValid = aOther.mIsValid;
mBuffers = std::move(aOther.mBuffers);
mOffsets = std::move(aOther.mOffsets);
aOther.mIsValid = false;
return *this;
}
RemoteArrayOfByteBuffer::~RemoteArrayOfByteBuffer() = default;
already_AddRefed<MediaByteBuffer> RemoteArrayOfByteBuffer::MediaByteBufferAt(
size_t aIndex) const {
MOZ_ASSERT(aIndex < Count());
const OffsetEntry& entry = mOffsets[aIndex];
if (!mBuffers || !std::get<1>(entry)) {
// It's an empty one.
return nullptr;
}
size_t entrySize = std::get<1>(entry);
if (!Check(std::get<0>(entry), entrySize)) {
// This Shmem is corrupted and can't contain the data we are about to
// retrieve. We return an empty array instead of asserting to allow for
// recovery.
return nullptr;
}
RefPtr<MediaByteBuffer> buffer = new MediaByteBuffer(entrySize);
buffer->SetLength(entrySize);
memcpy(buffer->Elements(), mBuffers->get<uint8_t>() + std::get<0>(entry),
entrySize);
return buffer.forget();
}
/*static */ void ipc::IPDLParamTraits<RemoteArrayOfByteBuffer>::Write(
IPC::MessageWriter* aWriter, ipc::IProtocol* aActor,
const RemoteArrayOfByteBuffer& aVar) {
WriteIPDLParam(aWriter, aActor, aVar.mIsValid);
// We need the following gymnastic as the Shmem transfered over IPC will be
// revoked. We must create a temporary one instead so that it can be recycled
// later back into the original ShmemPool.
if (aVar.mBuffers) {
WriteIPDLParam(aWriter, aActor, Some(ipc::Shmem(*aVar.mBuffers)));
} else {
WriteIPDLParam(aWriter, aActor, Maybe<ipc::Shmem>());
}
WriteIPDLParam(aWriter, aActor, aVar.mOffsets);
}
/* static */ bool ipc::IPDLParamTraits<RemoteArrayOfByteBuffer>::Read(
IPC::MessageReader* aReader, mozilla::ipc::IProtocol* aActor,
RemoteArrayOfByteBuffer* aVar) {
return ReadIPDLParam(aReader, aActor, &aVar->mIsValid) &&
ReadIPDLParam(aReader, aActor, &aVar->mBuffers) &&
ReadIPDLParam(aReader, aActor, &aVar->mOffsets);
}
bool ArrayOfRemoteMediaRawData::Fill(
const nsTArray<RefPtr<MediaRawData>>& aData,
std::function<ShmemBuffer(size_t)>&& aAllocator) {
nsTArray<AlignedByteBuffer> dataBuffers(aData.Length());
nsTArray<AlignedByteBuffer> alphaBuffers(aData.Length());
nsTArray<RefPtr<MediaByteBuffer>> extraDataBuffers(aData.Length());
int32_t height = 0;
for (auto&& entry : aData) {
dataBuffers.AppendElement(std::move(entry->mBuffer));
alphaBuffers.AppendElement(std::move(entry->mAlphaBuffer));
extraDataBuffers.AppendElement(std::move(entry->mExtraData));
if (auto&& info = entry->mTrackInfo; info && info->GetAsVideoInfo()) {
height = info->GetAsVideoInfo()->mImage.height;
}
mSamples.AppendElement(RemoteMediaRawData{
MediaDataIPDL(entry->mOffset, entry->mTime, entry->mTimecode,
entry->mDuration, entry->mKeyframe),
entry->mEOS, height, entry->mOriginalPresentationWindow,
entry->mCrypto.IsEncrypted() && entry->mShouldCopyCryptoToRemoteRawData
? Some(CryptoInfo{
entry->mCrypto.mCryptoScheme,
entry->mCrypto.mIV,
entry->mCrypto.mKeyId,
entry->mCrypto.mPlainSizes,
entry->mCrypto.mEncryptedSizes,
})
: Nothing()});
}
PerformanceRecorder<PlaybackStage> perfRecorder(MediaStage::CopyDemuxedData,
height);
mBuffers = RemoteArrayOfByteBuffer(dataBuffers, aAllocator);
if (!mBuffers.IsValid()) {
return false;
}
mAlphaBuffers = RemoteArrayOfByteBuffer(alphaBuffers, aAllocator);
if (!mAlphaBuffers.IsValid()) {
return false;
}
mExtraDatas = RemoteArrayOfByteBuffer(extraDataBuffers, aAllocator);
if (!mExtraDatas.IsValid()) {
return false;
}
perfRecorder.Record();
return true;
}
already_AddRefed<MediaRawData> ArrayOfRemoteMediaRawData::ElementAt(
size_t aIndex) const {
if (!IsValid()) {
return nullptr;
}
MOZ_ASSERT(aIndex < Count());
MOZ_DIAGNOSTIC_ASSERT(mBuffers.Count() == Count() &&
mAlphaBuffers.Count() == Count() &&
mExtraDatas.Count() == Count(),
"Something ain't right here");
const auto& sample = mSamples[aIndex];
PerformanceRecorder<PlaybackStage> perfRecorder(MediaStage::CopyDemuxedData,
sample.mHeight);
AlignedByteBuffer data = mBuffers.AlignedBufferAt<uint8_t>(aIndex);
if (mBuffers.SizeAt(aIndex) && !data) {
// OOM
return nullptr;
}
AlignedByteBuffer alphaData = mAlphaBuffers.AlignedBufferAt<uint8_t>(aIndex);
if (mAlphaBuffers.SizeAt(aIndex) && !alphaData) {
// OOM
return nullptr;
}
RefPtr<MediaRawData> rawData;
if (mAlphaBuffers.SizeAt(aIndex)) {
rawData = new MediaRawData(std::move(data), std::move(alphaData));
} else {
rawData = new MediaRawData(std::move(data));
}
rawData->mOffset = sample.mBase.offset();
rawData->mTime = sample.mBase.time();
rawData->mTimecode = sample.mBase.timecode();
rawData->mDuration = sample.mBase.duration();
rawData->mKeyframe = sample.mBase.keyframe();
rawData->mEOS = sample.mEOS;
rawData->mExtraData = mExtraDatas.MediaByteBufferAt(aIndex);
if (sample.mCryptoConfig) {
CryptoSample& cypto = rawData->GetWritableCrypto();
cypto.mCryptoScheme = sample.mCryptoConfig->mEncryptionScheme();
cypto.mIV = std::move(sample.mCryptoConfig->mIV());
cypto.mIVSize = cypto.mIV.Length();
cypto.mKeyId = std::move(sample.mCryptoConfig->mKeyId());
cypto.mPlainSizes = std::move(sample.mCryptoConfig->mClearBytes());
cypto.mEncryptedSizes = std::move(sample.mCryptoConfig->mCipherBytes());
}
perfRecorder.Record();
return rawData.forget();
}
/*static */ void ipc::IPDLParamTraits<ArrayOfRemoteMediaRawData*>::Write(
IPC::MessageWriter* aWriter, ipc::IProtocol* aActor,
ArrayOfRemoteMediaRawData* aVar) {
WriteIPDLParam(aWriter, aActor, std::move(aVar->mSamples));
WriteIPDLParam(aWriter, aActor, std::move(aVar->mBuffers));
WriteIPDLParam(aWriter, aActor, std::move(aVar->mAlphaBuffers));
WriteIPDLParam(aWriter, aActor, std::move(aVar->mExtraDatas));
}
/* static */ bool ipc::IPDLParamTraits<ArrayOfRemoteMediaRawData*>::Read(
IPC::MessageReader* aReader, mozilla::ipc::IProtocol* aActor,
RefPtr<ArrayOfRemoteMediaRawData>* aVar) {
auto array = MakeRefPtr<ArrayOfRemoteMediaRawData>();
if (!ReadIPDLParam(aReader, aActor, &array->mSamples) ||
!ReadIPDLParam(aReader, aActor, &array->mBuffers) ||
!ReadIPDLParam(aReader, aActor, &array->mAlphaBuffers) ||
!ReadIPDLParam(aReader, aActor, &array->mExtraDatas)) {
return false;
}
*aVar = std::move(array);
return true;
}
/* static */ void
ipc::IPDLParamTraits<ArrayOfRemoteMediaRawData::RemoteMediaRawData>::Write(
IPC::MessageWriter* aWriter, ipc::IProtocol* aActor,
const paramType& aVar) {
WriteIPDLParam(aWriter, aActor, aVar.mBase);
WriteIPDLParam(aWriter, aActor, aVar.mEOS);
WriteIPDLParam(aWriter, aActor, aVar.mHeight);
WriteIPDLParam(aWriter, aActor, aVar.mOriginalPresentationWindow);
WriteIPDLParam(aWriter, aActor, aVar.mCryptoConfig);
}
/* static */ bool
ipc::IPDLParamTraits<ArrayOfRemoteMediaRawData::RemoteMediaRawData>::Read(
IPC::MessageReader* aReader, ipc::IProtocol* aActor, paramType* aVar) {
MediaDataIPDL mBase;
return ReadIPDLParam(aReader, aActor, &aVar->mBase) &&
ReadIPDLParam(aReader, aActor, &aVar->mEOS) &&
ReadIPDLParam(aReader, aActor, &aVar->mHeight) &&
ReadIPDLParam(aReader, aActor, &aVar->mOriginalPresentationWindow) &&
ReadIPDLParam(aReader, aActor, &aVar->mCryptoConfig);
};
bool ArrayOfRemoteAudioData::Fill(
const nsTArray<RefPtr<AudioData>>& aData,
std::function<ShmemBuffer(size_t)>&& aAllocator) {
nsTArray<AlignedAudioBuffer> dataBuffers(aData.Length());
for (auto&& entry : aData) {
dataBuffers.AppendElement(std::move(entry->mAudioData));
mSamples.AppendElement(RemoteAudioData{
MediaDataIPDL(entry->mOffset, entry->mTime, entry->mTimecode,
entry->mDuration, entry->mKeyframe),
entry->mChannels, entry->mRate, uint32_t(entry->mChannelMap),
entry->mOriginalTime, entry->mTrimWindow, entry->mFrames,
entry->mDataOffset});
}
mBuffers = RemoteArrayOfByteBuffer(dataBuffers, aAllocator);
if (!mBuffers.IsValid()) {
return false;
}
return true;
}
already_AddRefed<AudioData> ArrayOfRemoteAudioData::ElementAt(
size_t aIndex) const {
if (!IsValid()) {
return nullptr;
}
MOZ_ASSERT(aIndex < Count());
MOZ_DIAGNOSTIC_ASSERT(mBuffers.Count() == Count(),
"Something ain't right here");
const auto& sample = mSamples[aIndex];
AlignedAudioBuffer data = mBuffers.AlignedBufferAt<AudioDataValue>(aIndex);
if (mBuffers.SizeAt(aIndex) && !data) {
// OOM
return nullptr;
}
auto audioData = MakeRefPtr<AudioData>(
sample.mBase.offset(), sample.mBase.time(), std::move(data),
sample.mChannels, sample.mRate, sample.mChannelMap);
// An AudioData's duration is set at construction time based on the size of
// the provided buffer. However, if a trim window is set, this value will be
// incorrect. We have to re-set it to what it actually was.
audioData->mDuration = sample.mBase.duration();
audioData->mOriginalTime = sample.mOriginalTime;
audioData->mTrimWindow = sample.mTrimWindow;
audioData->mFrames = sample.mFrames;
audioData->mDataOffset = sample.mDataOffset;
return audioData.forget();
}
/*static */ void ipc::IPDLParamTraits<ArrayOfRemoteAudioData*>::Write(
IPC::MessageWriter* aWriter, ipc::IProtocol* aActor,
ArrayOfRemoteAudioData* aVar) {
WriteIPDLParam(aWriter, aActor, std::move(aVar->mSamples));
WriteIPDLParam(aWriter, aActor, std::move(aVar->mBuffers));
}
/* static */ bool ipc::IPDLParamTraits<ArrayOfRemoteAudioData*>::Read(
IPC::MessageReader* aReader, mozilla::ipc::IProtocol* aActor,
RefPtr<ArrayOfRemoteAudioData>* aVar) {
auto array = MakeRefPtr<ArrayOfRemoteAudioData>();
if (!ReadIPDLParam(aReader, aActor, &array->mSamples) ||
!ReadIPDLParam(aReader, aActor, &array->mBuffers)) {
return false;
}
*aVar = std::move(array);
return true;
}
/* static */ void
ipc::IPDLParamTraits<ArrayOfRemoteAudioData::RemoteAudioData>::Write(
IPC::MessageWriter* aWriter, ipc::IProtocol* aActor,
const paramType& aVar) {
WriteIPDLParam(aWriter, aActor, aVar.mBase);
WriteIPDLParam(aWriter, aActor, aVar.mChannels);
WriteIPDLParam(aWriter, aActor, aVar.mRate);
WriteIPDLParam(aWriter, aActor, aVar.mChannelMap);
WriteIPDLParam(aWriter, aActor, aVar.mOriginalTime);
WriteIPDLParam(aWriter, aActor, aVar.mTrimWindow);
WriteIPDLParam(aWriter, aActor, aVar.mFrames);
WriteIPDLParam(aWriter, aActor, aVar.mDataOffset);
}
/* static */ bool
ipc::IPDLParamTraits<ArrayOfRemoteAudioData::RemoteAudioData>::Read(
IPC::MessageReader* aReader, ipc::IProtocol* aActor, paramType* aVar) {
MediaDataIPDL mBase;
if (!ReadIPDLParam(aReader, aActor, &aVar->mBase) ||
!ReadIPDLParam(aReader, aActor, &aVar->mChannels) ||
!ReadIPDLParam(aReader, aActor, &aVar->mRate) ||
!ReadIPDLParam(aReader, aActor, &aVar->mChannelMap) ||
!ReadIPDLParam(aReader, aActor, &aVar->mOriginalTime) ||
!ReadIPDLParam(aReader, aActor, &aVar->mTrimWindow) ||
!ReadIPDLParam(aReader, aActor, &aVar->mFrames) ||
!ReadIPDLParam(aReader, aActor, &aVar->mDataOffset)) {
return false;
}
return true;
};
} // namespace mozilla
|