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
|
/* -*- 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 "RemoteDecoderChild.h"
#include "RemoteDecoderManagerChild.h"
#include "mozilla/RemoteDecodeUtils.h"
namespace mozilla {
RemoteDecoderChild::RemoteDecoderChild(RemoteDecodeIn aLocation)
: ShmemRecycleAllocator(this),
mLocation(aLocation),
mThread(GetCurrentSerialEventTarget()) {
MOZ_DIAGNOSTIC_ASSERT(
RemoteDecoderManagerChild::GetManagerThread() &&
RemoteDecoderManagerChild::GetManagerThread()->IsOnCurrentThread(),
"Must be created on the manager thread");
}
RemoteDecoderChild::~RemoteDecoderChild() = default;
void RemoteDecoderChild::HandleRejectionError(
const ipc::ResponseRejectReason& aReason,
std::function<void(const MediaResult&)>&& aCallback) {
// If the channel goes down and CanSend() returns false, the IPDL promise will
// be rejected with SendError rather than ActorDestroyed. Both means the same
// thing and we can consider that the parent has crashed. The child can no
// longer be used.
//
// The GPU/RDD process crashed.
if (mLocation == RemoteDecodeIn::GpuProcess) {
// The GPU process will get automatically restarted by the parent process.
// Once it has been restarted the ContentChild will receive the message and
// will call GetManager()->InitForGPUProcess.
// We defer reporting an error until we've recreated the RemoteDecoder
// manager so that it'll be safe for MediaFormatReader to recreate decoders
RefPtr<RemoteDecoderChild> self = this;
GetManager()->RunWhenGPUProcessRecreated(NS_NewRunnableFunction(
"RemoteDecoderChild::HandleRejectionError",
[self, callback = std::move(aCallback)]() {
MediaResult error(
NS_ERROR_DOM_MEDIA_REMOTE_DECODER_CRASHED_RDD_OR_GPU_ERR,
__func__);
callback(error);
}));
return;
}
nsresult err = NS_ERROR_DOM_MEDIA_REMOTE_DECODER_CRASHED_UTILITY_ERR;
if (mLocation == RemoteDecodeIn::GpuProcess ||
mLocation == RemoteDecodeIn::RddProcess) {
err = NS_ERROR_DOM_MEDIA_REMOTE_DECODER_CRASHED_RDD_OR_GPU_ERR;
} else if (mLocation == RemoteDecodeIn::UtilityProcess_MFMediaEngineCDM) {
err = NS_ERROR_DOM_MEDIA_REMOTE_DECODER_CRASHED_MF_CDM_ERR;
}
// The RDD process is restarted on demand and asynchronously, we can
// immediately inform the caller that a new decoder is needed. The RDD will
// then be restarted during the new decoder creation by
aCallback(MediaResult(err, __func__));
}
// ActorDestroy is called if the channel goes down while waiting for a response.
void RemoteDecoderChild::ActorDestroy(ActorDestroyReason aWhy) {
mRemoteDecoderCrashed = (aWhy == AbnormalShutdown);
mDecodedData.Clear();
CleanupShmemRecycleAllocator();
RecordShutdownTelemetry(mRemoteDecoderCrashed);
}
void RemoteDecoderChild::DestroyIPDL() {
AssertOnManagerThread();
MOZ_DIAGNOSTIC_ASSERT(mInitPromise.IsEmpty() && mDecodePromise.IsEmpty() &&
mDrainPromise.IsEmpty() &&
mFlushPromise.IsEmpty() &&
mShutdownPromise.IsEmpty(),
"All promises should have been rejected");
if (CanSend()) {
PRemoteDecoderChild::Send__delete__(this);
}
}
void RemoteDecoderChild::IPDLActorDestroyed() { mIPDLSelfRef = nullptr; }
// MediaDataDecoder methods
RefPtr<MediaDataDecoder::InitPromise> RemoteDecoderChild::Init() {
AssertOnManagerThread();
mRemoteDecoderCrashed = false;
RefPtr<RemoteDecoderChild> self = this;
SendInit()
->Then(
mThread, __func__,
[self, this](InitResultIPDL&& aResponse) {
mInitPromiseRequest.Complete();
if (aResponse.type() == InitResultIPDL::TMediaResult) {
mInitPromise.Reject(aResponse.get_MediaResult(), __func__);
return;
}
const auto& initResponse = aResponse.get_InitCompletionIPDL();
mDescription = initResponse.decoderDescription();
mDescription.Append(" (");
mDescription.Append(RemoteDecodeInToStr(GetManager()->Location()));
mDescription.Append(" remote)");
mProcessName = initResponse.decoderProcessName();
mCodecName = initResponse.decoderCodecName();
mIsHardwareAccelerated = initResponse.hardware();
mHardwareAcceleratedReason = initResponse.hardwareReason();
mConversion = initResponse.conversion();
// Either the promise has not yet been resolved or the handler has
// been disconnected and we can't get here.
mInitPromise.Resolve(initResponse.type(), __func__);
},
[self](const mozilla::ipc::ResponseRejectReason& aReason) {
self->mInitPromiseRequest.Complete();
self->HandleRejectionError(
aReason, [self](const MediaResult& aError) {
self->mInitPromise.RejectIfExists(aError, __func__);
});
})
->Track(mInitPromiseRequest);
return mInitPromise.Ensure(__func__);
}
RefPtr<MediaDataDecoder::DecodePromise> RemoteDecoderChild::Decode(
const nsTArray<RefPtr<MediaRawData>>& aSamples) {
AssertOnManagerThread();
if (mRemoteDecoderCrashed) {
nsresult err = NS_ERROR_DOM_MEDIA_REMOTE_DECODER_CRASHED_UTILITY_ERR;
if (mLocation == RemoteDecodeIn::GpuProcess ||
mLocation == RemoteDecodeIn::RddProcess) {
err = NS_ERROR_DOM_MEDIA_REMOTE_DECODER_CRASHED_RDD_OR_GPU_ERR;
} else if (mLocation == RemoteDecodeIn::UtilityProcess_MFMediaEngineCDM) {
err = NS_ERROR_DOM_MEDIA_REMOTE_DECODER_CRASHED_MF_CDM_ERR;
}
return MediaDataDecoder::DecodePromise::CreateAndReject(err, __func__);
}
auto samples = MakeRefPtr<ArrayOfRemoteMediaRawData>();
if (!samples->Fill(aSamples,
[&](size_t aSize) { return AllocateBuffer(aSize); })) {
return MediaDataDecoder::DecodePromise::CreateAndReject(
NS_ERROR_OUT_OF_MEMORY, __func__);
}
SendDecode(samples)->Then(
mThread, __func__,
[self = RefPtr{this}, this](
PRemoteDecoderChild::DecodePromise::ResolveOrRejectValue&& aValue) {
// We no longer need the samples as the data has been
// processed by the parent.
// If the parent died, the error being fatal will cause the
// decoder to be torn down and all shmem in the pool will be
// deallocated.
ReleaseAllBuffers();
if (aValue.IsReject()) {
HandleRejectionError(
aValue.RejectValue(), [self](const MediaResult& aError) {
self->mDecodePromise.RejectIfExists(aError, __func__);
});
return;
}
MOZ_DIAGNOSTIC_ASSERT(CanSend(),
"The parent unexpectedly died, promise should "
"have been rejected first");
if (mDecodePromise.IsEmpty()) {
// We got flushed.
return;
}
auto response = std::move(aValue.ResolveValue());
if (response.type() == DecodeResultIPDL::TMediaResult &&
NS_FAILED(response.get_MediaResult())) {
mDecodePromise.Reject(response.get_MediaResult(), __func__);
return;
}
if (response.type() == DecodeResultIPDL::TDecodedOutputIPDL) {
ProcessOutput(std::move(response.get_DecodedOutputIPDL()));
}
mDecodePromise.Resolve(std::move(mDecodedData), __func__);
mDecodedData = MediaDataDecoder::DecodedData();
});
return mDecodePromise.Ensure(__func__);
}
RefPtr<MediaDataDecoder::FlushPromise> RemoteDecoderChild::Flush() {
AssertOnManagerThread();
mDecodePromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
mDrainPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
RefPtr<RemoteDecoderChild> self = this;
SendFlush()->Then(
mThread, __func__,
[self](const MediaResult& aResult) {
if (NS_SUCCEEDED(aResult)) {
self->mFlushPromise.ResolveIfExists(true, __func__);
} else {
self->mFlushPromise.RejectIfExists(aResult, __func__);
}
},
[self](const mozilla::ipc::ResponseRejectReason& aReason) {
self->HandleRejectionError(aReason, [self](const MediaResult& aError) {
self->mFlushPromise.RejectIfExists(aError, __func__);
});
});
return mFlushPromise.Ensure(__func__);
}
RefPtr<MediaDataDecoder::DecodePromise> RemoteDecoderChild::Drain() {
AssertOnManagerThread();
RefPtr<RemoteDecoderChild> self = this;
SendDrain()->Then(
mThread, __func__,
[self, this](DecodeResultIPDL&& aResponse) {
if (mDrainPromise.IsEmpty()) {
// We got flushed.
return;
}
if (aResponse.type() == DecodeResultIPDL::TMediaResult &&
NS_FAILED(aResponse.get_MediaResult())) {
mDrainPromise.Reject(aResponse.get_MediaResult(), __func__);
return;
}
MOZ_DIAGNOSTIC_ASSERT(CanSend(),
"The parent unexpectedly died, promise should "
"have been rejected first");
if (aResponse.type() == DecodeResultIPDL::TDecodedOutputIPDL) {
ProcessOutput(std::move(aResponse.get_DecodedOutputIPDL()));
}
mDrainPromise.Resolve(std::move(mDecodedData), __func__);
mDecodedData = MediaDataDecoder::DecodedData();
},
[self](const mozilla::ipc::ResponseRejectReason& aReason) {
self->HandleRejectionError(aReason, [self](const MediaResult& aError) {
self->mDrainPromise.RejectIfExists(aError, __func__);
});
});
return mDrainPromise.Ensure(__func__);
}
RefPtr<mozilla::ShutdownPromise> RemoteDecoderChild::Shutdown() {
AssertOnManagerThread();
// Shutdown() can be called while an InitPromise is pending.
mInitPromiseRequest.DisconnectIfExists();
mInitPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
mDecodePromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
mDrainPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
mFlushPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
RefPtr<RemoteDecoderChild> self = this;
SendShutdown()->Then(
mThread, __func__,
[self](
PRemoteDecoderChild::ShutdownPromise::ResolveOrRejectValue&& aValue) {
self->mShutdownPromise.Resolve(aValue.IsResolve(), __func__);
});
return mShutdownPromise.Ensure(__func__);
}
bool RemoteDecoderChild::IsHardwareAccelerated(
nsACString& aFailureReason) const {
AssertOnManagerThread();
aFailureReason = mHardwareAcceleratedReason;
return mIsHardwareAccelerated;
}
nsCString RemoteDecoderChild::GetDescriptionName() const {
AssertOnManagerThread();
return mDescription;
}
nsCString RemoteDecoderChild::GetProcessName() const {
AssertOnManagerThread();
return mProcessName;
}
nsCString RemoteDecoderChild::GetCodecName() const {
AssertOnManagerThread();
return mCodecName;
}
void RemoteDecoderChild::SetSeekThreshold(const media::TimeUnit& aTime) {
AssertOnManagerThread();
Unused << SendSetSeekThreshold(aTime);
}
MediaDataDecoder::ConversionRequired RemoteDecoderChild::NeedsConversion()
const {
AssertOnManagerThread();
return mConversion;
}
void RemoteDecoderChild::AssertOnManagerThread() const {
MOZ_ASSERT(mThread->IsOnCurrentThread());
}
RemoteDecoderManagerChild* RemoteDecoderChild::GetManager() {
if (!CanSend()) {
return nullptr;
}
return static_cast<RemoteDecoderManagerChild*>(Manager());
}
} // namespace mozilla
|