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
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/media/cma/decoder/cast_audio_decoder.h"
#include <stdint.h>
#include <limits>
#include <queue>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/single_thread_task_runner.h"
#include "chromecast/media/cma/base/decoder_buffer_adapter.h"
#include "chromecast/media/cma/base/decoder_buffer_base.h"
#include "chromecast/media/cma/base/decoder_config_adapter.h"
#include "media/base/audio_buffer.h"
#include "media/base/audio_bus.h"
#include "media/base/cdm_context.h"
#include "media/base/channel_layout.h"
#include "media/base/channel_mixer.h"
#include "media/base/decoder_buffer.h"
#include "media/base/sample_format.h"
#include "media/filters/ffmpeg_audio_decoder.h"
#include "media/filters/opus_audio_decoder.h"
namespace chromecast {
namespace media {
namespace {
const int kOpusSamplingRate = 48000;
const uint8_t kFakeOpusExtraData[19] = {
'O', 'p', 'u', 's', 'H', 'e', 'a', 'd', // offset 0, OpusHead
0, // offset 8, version
2, // offset 9, channels
0, 0, // offset 10, skip
static_cast<uint8_t>(kOpusSamplingRate & 0xFF), // offset 12, LE
static_cast<uint8_t>((kOpusSamplingRate >> 8) & 0xFF),
static_cast<uint8_t>((kOpusSamplingRate >> 16) & 0xFF),
static_cast<uint8_t>((kOpusSamplingRate >> 24) & 0xFF),
0, 0, // offset 16, gain
0, // offset 18, stereo mapping
};
const int kOutputChannelCount = 2; // Always output stereo audio.
const int kMaxChannelInput = 2;
class CastAudioDecoderImpl : public CastAudioDecoder {
public:
CastAudioDecoderImpl(
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
const InitializedCallback& initialized_callback,
OutputFormat output_format)
: task_runner_(task_runner),
initialized_callback_(initialized_callback),
output_format_(output_format),
initialized_(false),
decode_pending_(false),
weak_factory_(this) {}
~CastAudioDecoderImpl() override {}
void Initialize(const media::AudioConfig& config) {
DCHECK(!initialized_);
DCHECK_LE(config_.channel_number, kMaxChannelInput);
config_ = config;
if (config_.channel_number == 1) {
// If the input is mono, create a ChannelMixer to convert mono to stereo.
// TODO(kmackay) Support other channel format conversions?
mixer_.reset(new ::media::ChannelMixer(::media::CHANNEL_LAYOUT_MONO,
::media::CHANNEL_LAYOUT_STEREO));
}
base::WeakPtr<CastAudioDecoderImpl> self = weak_factory_.GetWeakPtr();
if (config.codec == media::kCodecOpus) {
// Insert fake extradata to make OpusAudioDecoder work with v2mirroring.
if (config_.extra_data.empty() &&
config_.samples_per_second == kOpusSamplingRate &&
config_.channel_number == 2)
config_.extra_data.assign(
kFakeOpusExtraData,
kFakeOpusExtraData + sizeof(kFakeOpusExtraData));
decoder_.reset(new ::media::OpusAudioDecoder(task_runner_));
} else {
decoder_.reset(new ::media::FFmpegAudioDecoder(
task_runner_, make_scoped_refptr(new ::media::MediaLog())));
}
decoder_->Initialize(
media::DecoderConfigAdapter::ToMediaAudioDecoderConfig(config_),
::media::SetCdmReadyCB(),
base::Bind(&CastAudioDecoderImpl::OnInitialized, self),
base::Bind(&CastAudioDecoderImpl::OnDecoderOutput, self));
// Unfortunately there is no result from decoder_->Initialize() until later
// (the pipeline status callback is posted to the task runner).
}
// CastAudioDecoder implementation:
bool Decode(const scoped_refptr<media::DecoderBufferBase>& data,
const DecodeCallback& decode_callback) override {
DCHECK(!decode_callback.is_null());
DCHECK(task_runner_->BelongsToCurrentThread());
if (!initialized_ || decode_pending_) {
decode_queue_.push(std::make_pair(data, decode_callback));
} else {
DecodeNow(data, decode_callback);
}
return true;
}
private:
typedef std::pair<scoped_refptr<media::DecoderBufferBase>, DecodeCallback>
DecodeBufferCallbackPair;
void DecodeNow(const scoped_refptr<media::DecoderBufferBase>& data,
const DecodeCallback& decode_callback) {
if (data->end_of_stream()) {
// Post the task to ensure that |decode_callback| is not called from
// within a call to Decode().
task_runner_->PostTask(FROM_HERE,
base::Bind(decode_callback, kDecodeOk, data));
return;
}
// FFmpegAudioDecoder requires a timestamp to be set.
base::TimeDelta timestamp =
base::TimeDelta::FromMicroseconds(data->timestamp());
if (timestamp == ::media::kNoTimestamp())
data->set_timestamp(base::TimeDelta());
decode_pending_ = true;
decoder_->Decode(data->ToMediaBuffer(),
base::Bind(&CastAudioDecoderImpl::OnDecodeStatus,
weak_factory_.GetWeakPtr(),
timestamp,
decode_callback));
}
void OnInitialized(bool success) {
DCHECK(!initialized_);
LOG_IF(ERROR, !success) << "Failed to initialize FFmpegAudioDecoder";
if (success)
initialized_ = true;
if (success && !decode_queue_.empty()) {
const auto& d = decode_queue_.front();
DecodeNow(d.first, d.second);
decode_queue_.pop();
}
if (!initialized_callback_.is_null())
initialized_callback_.Run(initialized_);
}
void OnDecodeStatus(base::TimeDelta buffer_timestamp,
const DecodeCallback& decode_callback,
::media::AudioDecoder::Status status) {
Status result_status = kDecodeOk;
scoped_refptr<media::DecoderBufferBase> decoded;
if (status == ::media::AudioDecoder::kOk && !decoded_chunks_.empty()) {
decoded = ConvertDecoded();
} else {
if (status != ::media::AudioDecoder::kOk)
result_status = kDecodeError;
decoded = new media::DecoderBufferAdapter(config_.id,
new ::media::DecoderBuffer(0));
}
decoded_chunks_.clear();
decoded->set_timestamp(buffer_timestamp);
base::WeakPtr<CastAudioDecoderImpl> self = weak_factory_.GetWeakPtr();
decode_callback.Run(result_status, decoded);
if (!self.get())
return; // Return immediately if the decode callback deleted this.
// Do not reset decode_pending_ to false until after the callback has
// finished running because the callback may call Decode().
decode_pending_ = false;
if (decode_queue_.empty())
return;
const auto& d = decode_queue_.front();
// Calling DecodeNow() here does not result in a loop, because
// OnDecodeStatus() is always called asynchronously (guaranteed by the
// AudioDecoder interface).
DecodeNow(d.first, d.second);
decode_queue_.pop();
}
void OnDecoderOutput(const scoped_refptr<::media::AudioBuffer>& decoded) {
decoded_chunks_.push_back(decoded);
}
scoped_refptr<media::DecoderBufferBase> ConvertDecoded() {
DCHECK(!decoded_chunks_.empty());
int num_frames = 0;
for (auto& chunk : decoded_chunks_)
num_frames += chunk->frame_count();
// Copy decoded data into an AudioBus for conversion.
scoped_ptr<::media::AudioBus> decoded =
::media::AudioBus::Create(config_.channel_number, num_frames);
int bus_frame_offset = 0;
for (auto& chunk : decoded_chunks_) {
chunk->ReadFrames(
chunk->frame_count(), 0, bus_frame_offset, decoded.get());
bus_frame_offset += chunk->frame_count();
}
if (mixer_) {
// Convert to stereo if necessary.
scoped_ptr<::media::AudioBus> converted_to_stereo =
::media::AudioBus::Create(kOutputChannelCount, num_frames);
mixer_->Transform(decoded.get(), converted_to_stereo.get());
decoded.swap(converted_to_stereo);
}
// Convert to the desired output format.
return FinishConversion(decoded.get());
}
scoped_refptr<media::DecoderBufferBase> FinishConversion(
::media::AudioBus* bus) {
DCHECK_EQ(kOutputChannelCount, bus->channels());
int size = bus->frames() * kOutputChannelCount *
OutputFormatSizeInBytes(output_format_);
scoped_refptr<::media::DecoderBuffer> result(
new ::media::DecoderBuffer(size));
if (output_format_ == kOutputSigned16) {
bus->ToInterleaved(bus->frames(),
OutputFormatSizeInBytes(output_format_),
result->writable_data());
} else if (output_format_ == kOutputPlanarFloat) {
// Data in an AudioBus is already in planar float format; just copy each
// channel into the result buffer in order.
float* ptr = reinterpret_cast<float*>(result->writable_data());
for (int c = 0; c < bus->channels(); ++c) {
memcpy(ptr, bus->channel(c), bus->frames() * sizeof(float));
ptr += bus->frames();
}
} else {
NOTREACHED();
}
result->set_duration(base::TimeDelta::FromMicroseconds(
bus->frames() * base::Time::kMicrosecondsPerSecond /
config_.samples_per_second));
return make_scoped_refptr(
new media::DecoderBufferAdapter(config_.id, result));
}
const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
InitializedCallback initialized_callback_;
OutputFormat output_format_;
media::AudioConfig config_;
scoped_ptr<::media::AudioDecoder> decoder_;
std::queue<DecodeBufferCallbackPair> decode_queue_;
bool initialized_;
scoped_ptr<::media::ChannelMixer> mixer_;
bool decode_pending_;
std::vector<scoped_refptr<::media::AudioBuffer>> decoded_chunks_;
base::WeakPtrFactory<CastAudioDecoderImpl> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(CastAudioDecoderImpl);
};
} // namespace
// static
scoped_ptr<CastAudioDecoder> CastAudioDecoder::Create(
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
const media::AudioConfig& config,
OutputFormat output_format,
const InitializedCallback& initialized_callback) {
scoped_ptr<CastAudioDecoderImpl> decoder(new CastAudioDecoderImpl(
task_runner, initialized_callback, output_format));
decoder->Initialize(config);
return std::move(decoder);
}
// static
int CastAudioDecoder::OutputFormatSizeInBytes(
CastAudioDecoder::OutputFormat format) {
switch (format) {
case CastAudioDecoder::OutputFormat::kOutputSigned16:
return 2;
case CastAudioDecoder::OutputFormat::kOutputPlanarFloat:
return 4;
}
NOTREACHED();
return 1;
}
} // namespace media
} // namespace chromecast
|