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
media / mojo / services / mojo_decryptor_service.cc [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/mojo/services/mojo_decryptor_service.h"
#include <memory>
#include <utility>
#include "base/functional/bind.h"
#include "base/numerics/safe_conversions.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/cdm_context.h"
#include "media/base/decoder_buffer.h"
#include "media/base/decryptor.h"
#include "media/base/video_decoder_config.h"
#include "media/base/video_frame.h"
#include "media/mojo/common/media_type_converters.h"
#include "media/mojo/common/mojo_decoder_buffer_converter.h"
#include "media/mojo/mojom/demuxer_stream.mojom.h"
#include "media/mojo/services/mojo_cdm_service_context.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
namespace media {
namespace {
// A mojom::FrameResourceReleaser implementation. This object is created when
// DecryptAndDecodeVideo() returns a shared memory video frame, and holds
// on to the local frame. When MojoDecryptor is done using the frame,
// the connection should be broken and this will free the shared resources
// associated with the frame.
class FrameResourceReleaserImpl final : public mojom::FrameResourceReleaser {
public:
explicit FrameResourceReleaserImpl(scoped_refptr<VideoFrame> frame)
: frame_(std::move(frame)) {
DVLOG(3) << __func__;
DCHECK_EQ(VideoFrame::STORAGE_SHMEM, frame_->storage_type());
}
FrameResourceReleaserImpl(const FrameResourceReleaserImpl&) = delete;
FrameResourceReleaserImpl& operator=(const FrameResourceReleaserImpl&) =
delete;
~FrameResourceReleaserImpl() override { DVLOG(3) << __func__; }
private:
scoped_refptr<VideoFrame> frame_;
};
const char kInvalidStateMessage[] = "MojoDecryptorService - invalid state";
} // namespace
MojoDecryptorService::MojoDecryptorService(
media::Decryptor* decryptor,
std::unique_ptr<CdmContextRef> cdm_context_ref)
: decryptor_(decryptor), cdm_context_ref_(std::move(cdm_context_ref)) {
DVLOG(1) << __func__;
DCHECK(decryptor_);
// |cdm_context_ref_| could be null, in which case the owner of |this| will
// make sure |decryptor_| is always valid.
weak_this_ = weak_factory_.GetWeakPtr();
}
MojoDecryptorService::~MojoDecryptorService() {
DVLOG(1) << __func__;
}
void MojoDecryptorService::Initialize(
mojo::ScopedDataPipeConsumerHandle audio_pipe,
mojo::ScopedDataPipeConsumerHandle video_pipe,
mojo::ScopedDataPipeConsumerHandle decrypt_pipe,
mojo::ScopedDataPipeProducerHandle decrypted_pipe) {
DVLOG(1) << __func__;
if (has_initialize_been_called_) {
mojo::ReportBadMessage(kInvalidStateMessage);
return;
}
has_initialize_been_called_ = true;
audio_buffer_reader_ =
std::make_unique<MojoDecoderBufferReader>(std::move(audio_pipe));
video_buffer_reader_ =
std::make_unique<MojoDecoderBufferReader>(std::move(video_pipe));
decrypt_buffer_reader_ =
std::make_unique<MojoDecoderBufferReader>(std::move(decrypt_pipe));
decrypted_buffer_writer_ =
std::make_unique<MojoDecoderBufferWriter>(std::move(decrypted_pipe));
}
void MojoDecryptorService::Decrypt(StreamType stream_type,
mojom::DecoderBufferPtr encrypted,
DecryptCallback callback) {
DVLOG(3) << __func__;
if (!decrypt_buffer_reader_) {
mojo::ReportBadMessage(kInvalidStateMessage);
return;
}
decrypt_buffer_reader_->ReadDecoderBuffer(
std::move(encrypted),
base::BindOnce(&MojoDecryptorService::OnReadDone, weak_this_, stream_type,
std::move(callback)));
}
void MojoDecryptorService::CancelDecrypt(StreamType stream_type) {
DVLOG(2) << __func__;
decryptor_->CancelDecrypt(stream_type);
}
void MojoDecryptorService::InitializeAudioDecoder(
const AudioDecoderConfig& config,
InitializeAudioDecoderCallback callback) {
DVLOG(1) << __func__;
decryptor_->InitializeAudioDecoder(
config, base::BindOnce(&MojoDecryptorService::OnAudioDecoderInitialized,
weak_this_, std::move(callback)));
}
void MojoDecryptorService::InitializeVideoDecoder(
const VideoDecoderConfig& config,
InitializeVideoDecoderCallback callback) {
DVLOG(2) << __func__;
decryptor_->InitializeVideoDecoder(
config, base::BindOnce(&MojoDecryptorService::OnVideoDecoderInitialized,
weak_this_, std::move(callback)));
}
void MojoDecryptorService::DecryptAndDecodeAudio(
mojom::DecoderBufferPtr encrypted,
DecryptAndDecodeAudioCallback callback) {
DVLOG(3) << __func__;
if (!audio_buffer_reader_) {
mojo::ReportBadMessage(kInvalidStateMessage);
return;
}
audio_buffer_reader_->ReadDecoderBuffer(
std::move(encrypted), base::BindOnce(&MojoDecryptorService::OnAudioRead,
weak_this_, std::move(callback)));
}
void MojoDecryptorService::DecryptAndDecodeVideo(
mojom::DecoderBufferPtr encrypted,
DecryptAndDecodeVideoCallback callback) {
DVLOG(3) << __func__;
if (!video_buffer_reader_) {
mojo::ReportBadMessage(kInvalidStateMessage);
return;
}
video_buffer_reader_->ReadDecoderBuffer(
std::move(encrypted), base::BindOnce(&MojoDecryptorService::OnVideoRead,
weak_this_, std::move(callback)));
}
void MojoDecryptorService::ResetDecoder(StreamType stream_type) {
DVLOG(2) << __func__ << ": stream_type = " << stream_type;
// Reset the reader so that pending decodes will be dispatched first.
if (!GetBufferReader(stream_type))
return;
GetBufferReader(stream_type)
->Flush(base::BindOnce(&MojoDecryptorService::OnReaderFlushDone,
weak_this_, stream_type));
}
void MojoDecryptorService::DeinitializeDecoder(StreamType stream_type) {
DVLOG(2) << __func__;
DCHECK(!GetBufferReader(stream_type)->HasPendingReads())
<< "The decoder should be fully flushed before deinitialized.";
decryptor_->DeinitializeDecoder(stream_type);
}
void MojoDecryptorService::OnReadDone(StreamType stream_type,
DecryptCallback callback,
scoped_refptr<DecoderBuffer> buffer) {
if (!buffer) {
std::move(callback).Run(Status::kError, nullptr);
return;
}
decryptor_->Decrypt(stream_type, std::move(buffer),
base::BindOnce(&MojoDecryptorService::OnDecryptDone,
weak_this_, std::move(callback)));
}
void MojoDecryptorService::OnDecryptDone(DecryptCallback callback,
Status status,
scoped_refptr<DecoderBuffer> buffer) {
DVLOG_IF(1, status != Status::kSuccess) << __func__ << "(" << status << ")";
DVLOG_IF(3, status == Status::kSuccess) << __func__;
if (!buffer) {
std::move(callback).Run(status, nullptr);
return;
}
mojom::DecoderBufferPtr mojo_buffer =
decrypted_buffer_writer_->WriteDecoderBuffer(std::move(buffer));
if (!mojo_buffer) {
std::move(callback).Run(Status::kError, nullptr);
return;
}
std::move(callback).Run(status, std::move(mojo_buffer));
}
void MojoDecryptorService::OnAudioDecoderInitialized(
InitializeAudioDecoderCallback callback,
bool success) {
DVLOG(2) << __func__ << "(" << success << ")";
std::move(callback).Run(success);
}
void MojoDecryptorService::OnVideoDecoderInitialized(
InitializeVideoDecoderCallback callback,
bool success) {
DVLOG(2) << __func__ << "(" << success << ")";
std::move(callback).Run(success);
}
void MojoDecryptorService::OnAudioRead(DecryptAndDecodeAudioCallback callback,
scoped_refptr<DecoderBuffer> buffer) {
if (!buffer) {
std::move(callback).Run(Status::kError,
std::vector<mojom::AudioBufferPtr>());
return;
}
decryptor_->DecryptAndDecodeAudio(
std::move(buffer), base::BindOnce(&MojoDecryptorService::OnAudioDecoded,
weak_this_, std::move(callback)));
}
void MojoDecryptorService::OnVideoRead(DecryptAndDecodeVideoCallback callback,
scoped_refptr<DecoderBuffer> buffer) {
if (!buffer) {
std::move(callback).Run(Status::kError, nullptr, mojo::NullRemote());
return;
}
decryptor_->DecryptAndDecodeVideo(
std::move(buffer), base::BindOnce(&MojoDecryptorService::OnVideoDecoded,
weak_this_, std::move(callback)));
}
void MojoDecryptorService::OnReaderFlushDone(StreamType stream_type) {
DVLOG(2) << __func__ << ": stream_type = " << stream_type;
decryptor_->ResetDecoder(stream_type);
}
void MojoDecryptorService::OnAudioDecoded(
DecryptAndDecodeAudioCallback callback,
Status status,
const media::Decryptor::AudioFrames& frames) {
DVLOG_IF(1, status != Status::kSuccess) << __func__ << "(" << status << ")";
DVLOG_IF(3, status == Status::kSuccess) << __func__;
// Note that the audio data is sent over the mojo pipe. This could be
// improved to use shared memory (http://crbug.com/593896).
std::vector<mojom::AudioBufferPtr> audio_buffers;
for (const auto& frame : frames)
audio_buffers.push_back(mojom::AudioBuffer::From(*frame));
std::move(callback).Run(status, std::move(audio_buffers));
}
void MojoDecryptorService::OnVideoDecoded(
DecryptAndDecodeVideoCallback callback,
Status status,
scoped_refptr<VideoFrame> frame) {
DVLOG_IF(1, status != Status::kSuccess)
<< __func__ << ": status = " << status;
DVLOG_IF(3, status == Status::kSuccess) << __func__;
if (!frame) {
DCHECK_NE(status, Status::kSuccess);
std::move(callback).Run(status, nullptr, mojo::NullRemote());
return;
}
// If |frame| has shared memory that will be passed back, keep the reference
// to it until the other side is done with the memory.
mojo::PendingRemote<mojom::FrameResourceReleaser> releaser;
if (frame->storage_type() == VideoFrame::STORAGE_SHMEM) {
mojo::MakeSelfOwnedReceiver(
std::make_unique<FrameResourceReleaserImpl>(frame),
releaser.InitWithNewPipeAndPassReceiver());
}
std::move(callback).Run(status, std::move(frame), std::move(releaser));
}
MojoDecoderBufferReader* MojoDecryptorService::GetBufferReader(
StreamType stream_type) const {
switch (stream_type) {
case StreamType::kAudio:
return audio_buffer_reader_.get();
case StreamType::kVideo:
return video_buffer_reader_.get();
}
NOTREACHED() << "Unexpected stream_type: " << stream_type;
}
} // namespace media