OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chromecast/media/cma/decoder/cast_audio_decoder.h" | |
6 | |
7 #include <limits> | |
8 #include <queue> | |
9 #include <vector> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/location.h" | |
13 #include "base/logging.h" | |
14 #include "base/single_thread_task_runner.h" | |
15 #include "base/trace_event/trace_event.h" | |
16 #include "chromecast/media/cma/base/decoder_buffer_adapter.h" | |
17 #include "chromecast/media/cma/base/decoder_buffer_base.h" | |
18 #include "chromecast/media/cma/base/decoder_config_adapter.h" | |
19 #include "media/base/audio_buffer.h" | |
20 #include "media/base/audio_bus.h" | |
21 #include "media/base/cdm_context.h" | |
22 #include "media/base/channel_layout.h" | |
23 #include "media/base/channel_mixer.h" | |
24 #include "media/base/decoder_buffer.h" | |
25 #include "media/base/sample_format.h" | |
26 #include "media/filters/ffmpeg_audio_decoder.h" | |
27 #include "media/filters/opus_audio_decoder.h" | |
28 | |
29 namespace chromecast { | |
30 namespace media { | |
31 | |
32 namespace { | |
33 | |
34 const int kOpusSamplingRate = 48000; | |
35 const uint8 kFakeOpusExtraData[19] = { | |
36 'O', 'p', 'u', 's', 'H', 'e', 'a', 'd', // offset 0, OpusHead | |
37 0, // offset 8, version | |
38 2, // offset 9, channels | |
39 0, 0, // offset 10, skip | |
40 static_cast<uint8>(kOpusSamplingRate & 0xFF), // offset 12, LE | |
41 static_cast<uint8>((kOpusSamplingRate >> 8) & 0xFF), | |
42 static_cast<uint8>((kOpusSamplingRate >> 16) & 0xFF), | |
43 static_cast<uint8>((kOpusSamplingRate >> 24) & 0xFF), | |
44 0, 0, // offset 16, gain | |
45 0, // offset 18, stereo mapping | |
46 }; | |
47 | |
48 const int kOutputChannelCount = 2; // Always output stereo audio. | |
49 const int kMaxChannelInput = 2; | |
50 | |
51 class CastAudioDecoderImpl : public CastAudioDecoder { | |
52 public: | |
53 CastAudioDecoderImpl( | |
54 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
55 const InitializedCallback& initialized_callback, | |
56 OutputFormat output_format) | |
57 : task_runner_(task_runner), | |
58 initialized_callback_(initialized_callback), | |
59 output_format_(output_format), | |
60 initialized_(false), | |
61 decode_pending_(false), | |
62 weak_factory_(this) {} | |
63 | |
64 ~CastAudioDecoderImpl() override {} | |
65 | |
66 void Initialize(const media::AudioConfig& config) { | |
67 TRACE_EVENT0("cma", "CastAudioDecoderImpl::Initialize"); | |
slan
2015/12/02 21:55:26
nit: are trace events needed?
kmackay
2015/12/02 21:58:49
Probably not, I'm not sure why they were added.
kmackay
2015/12/02 22:05:39
Done.
| |
68 DCHECK(!initialized_); | |
69 DCHECK_LE(config_.channel_number, kMaxChannelInput); | |
70 config_ = config; | |
71 if (config_.channel_number == 1) { | |
72 // If the input is mono, create a ChannelMixer to convert mono to stereo. | |
73 // TODO(kmackay) Support other channel format conversions? | |
74 mixer_.reset(new ::media::ChannelMixer(::media::CHANNEL_LAYOUT_MONO, | |
75 ::media::CHANNEL_LAYOUT_STEREO)); | |
76 } | |
77 base::WeakPtr<CastAudioDecoderImpl> self = weak_factory_.GetWeakPtr(); | |
78 if (config.codec == media::kCodecOpus) { | |
79 // Insert fake extradata to make OpusAudioDecoder work with v2mirroring. | |
80 if (config_.extra_data.empty() && | |
81 config_.samples_per_second == kOpusSamplingRate && | |
82 config_.channel_number == 2) | |
83 config_.extra_data.assign( | |
84 kFakeOpusExtraData, | |
85 kFakeOpusExtraData + sizeof(kFakeOpusExtraData)); | |
86 decoder_.reset(new ::media::OpusAudioDecoder(task_runner_)); | |
87 } else { | |
88 decoder_.reset(new ::media::FFmpegAudioDecoder( | |
89 task_runner_, make_scoped_refptr(new ::media::MediaLog()))); | |
90 } | |
91 decoder_->Initialize( | |
92 media::DecoderConfigAdapter::ToMediaAudioDecoderConfig(config_), | |
93 #if !defined(CHROMECAST_BUILD) | |
slan
2015/12/02 21:55:26
hmmm, i am suspicious that this is needed upstream
kmackay
2015/12/02 21:58:49
Looks like there was a compatibility issue between
kmackay
2015/12/02 22:05:39
Done.
| |
94 ::media::SetCdmReadyCB(), | |
95 #endif | |
96 base::Bind(&CastAudioDecoderImpl::OnInitialized, self), | |
97 base::Bind(&CastAudioDecoderImpl::OnDecoderOutput, self)); | |
98 // Unfortunately there is no result from decoder_->Initialize() until later | |
99 // (the pipeline status callback is posted to the task runner). | |
100 } | |
101 | |
102 // CastAudioDecoder implementation: | |
103 bool Decode(const scoped_refptr<media::DecoderBufferBase>& data, | |
104 const DecodeCallback& decode_callback) override { | |
105 TRACE_EVENT0("cma", "CastAudioDecoderImpl::Decode"); | |
106 DCHECK(!decode_callback.is_null()); | |
107 DCHECK(task_runner_->BelongsToCurrentThread()); | |
108 if (!initialized_ || decode_pending_) { | |
109 decode_queue_.push(std::make_pair(data, decode_callback)); | |
110 } else { | |
111 DecodeNow(data, decode_callback); | |
112 } | |
113 return true; | |
114 } | |
115 | |
116 private: | |
117 typedef std::pair<scoped_refptr<media::DecoderBufferBase>, DecodeCallback> | |
118 DecodeBufferCallbackPair; | |
119 | |
120 void DecodeNow(const scoped_refptr<media::DecoderBufferBase>& data, | |
121 const DecodeCallback& decode_callback) { | |
122 if (data->end_of_stream()) { | |
123 // Post the task to ensure that |decode_callback| is not called from | |
124 // within a call to Decode(). | |
125 task_runner_->PostTask(FROM_HERE, | |
126 base::Bind(decode_callback, kDecodeOk, data)); | |
127 return; | |
128 } | |
129 | |
130 // FFmpegAudioDecoder requires a timestamp to be set. | |
131 base::TimeDelta timestamp = | |
132 base::TimeDelta::FromMicroseconds(data->timestamp()); | |
133 if (timestamp == ::media::kNoTimestamp()) | |
134 data->set_timestamp(base::TimeDelta()); | |
135 | |
136 decode_pending_ = true; | |
137 decoder_->Decode(data->ToMediaBuffer(), | |
138 base::Bind(&CastAudioDecoderImpl::OnDecodeStatus, | |
139 weak_factory_.GetWeakPtr(), | |
140 timestamp, | |
141 decode_callback)); | |
142 } | |
143 | |
144 void OnInitialized(bool success) { | |
145 TRACE_EVENT0("cma", "CastAudioDecoderImpl::OnInitialize"); | |
146 DCHECK(!initialized_); | |
147 LOG_IF(ERROR, !success) << "Failed to initialize FFmpegAudioDecoder"; | |
148 if (success) | |
149 initialized_ = true; | |
150 | |
151 if (success && !decode_queue_.empty()) { | |
152 const auto& d = decode_queue_.front(); | |
153 DecodeNow(d.first, d.second); | |
154 decode_queue_.pop(); | |
155 } | |
156 | |
157 if (!initialized_callback_.is_null()) | |
158 initialized_callback_.Run(initialized_); | |
159 } | |
160 | |
161 void OnDecodeStatus(base::TimeDelta buffer_timestamp, | |
162 const DecodeCallback& decode_callback, | |
163 ::media::AudioDecoder::Status status) { | |
164 TRACE_EVENT0("cma", "CastAudioDecoderImpl::OnDecodeStatus"); | |
165 Status result_status = kDecodeOk; | |
166 scoped_refptr<media::DecoderBufferBase> decoded; | |
167 if (status == ::media::AudioDecoder::kOk && !decoded_chunks_.empty()) { | |
168 decoded = ConvertDecoded(); | |
169 } else { | |
170 if (status != ::media::AudioDecoder::kOk) | |
171 result_status = kDecodeError; | |
172 decoded = new media::DecoderBufferAdapter(config_.id, | |
173 new ::media::DecoderBuffer(0)); | |
174 } | |
175 decoded_chunks_.clear(); | |
176 decoded->set_timestamp(buffer_timestamp); | |
177 decode_callback.Run(result_status, decoded); | |
178 | |
179 // Do not reset decode_pending_ to false until after the callback has | |
180 // finished running because the callback may call Decode(). | |
181 decode_pending_ = false; | |
182 | |
183 if (decode_queue_.empty()) | |
184 return; | |
185 | |
186 const auto& d = decode_queue_.front(); | |
187 // Calling DecodeNow() here does not result in a loop, because | |
188 // OnDecodeStatus() is always called asynchronously (guaranteed by the | |
189 // AudioDecoder interface). | |
190 DecodeNow(d.first, d.second); | |
191 decode_queue_.pop(); | |
192 } | |
193 | |
194 void OnDecoderOutput(const scoped_refptr<::media::AudioBuffer>& decoded) { | |
195 decoded_chunks_.push_back(decoded); | |
196 } | |
197 | |
198 scoped_refptr<media::DecoderBufferBase> ConvertDecoded() { | |
199 DCHECK(!decoded_chunks_.empty()); | |
200 int num_frames = 0; | |
201 for (auto& chunk : decoded_chunks_) | |
202 num_frames += chunk->frame_count(); | |
203 | |
204 // Copy decoded data into an AudioBus for conversion. | |
205 scoped_ptr<::media::AudioBus> decoded = | |
206 ::media::AudioBus::Create(config_.channel_number, num_frames); | |
207 int bus_frame_offset = 0; | |
208 for (auto& chunk : decoded_chunks_) { | |
209 chunk->ReadFrames( | |
210 chunk->frame_count(), 0, bus_frame_offset, decoded.get()); | |
211 bus_frame_offset += chunk->frame_count(); | |
212 } | |
213 | |
214 if (mixer_) { | |
215 // Convert to stereo if necessary. | |
216 scoped_ptr<::media::AudioBus> converted_to_stereo = | |
217 ::media::AudioBus::Create(kOutputChannelCount, num_frames); | |
218 mixer_->Transform(decoded.get(), converted_to_stereo.get()); | |
219 decoded.swap(converted_to_stereo); | |
220 } | |
221 | |
222 // Convert to the desired output format. | |
223 return FinishConversion(decoded.get()); | |
224 } | |
225 | |
226 scoped_refptr<media::DecoderBufferBase> FinishConversion( | |
227 ::media::AudioBus* bus) { | |
228 DCHECK_EQ(kOutputChannelCount, bus->channels()); | |
229 int size = bus->frames() * kOutputChannelCount * | |
230 OutputFormatSizeInBytes(output_format_); | |
231 scoped_refptr<::media::DecoderBuffer> result( | |
232 new ::media::DecoderBuffer(size)); | |
233 | |
234 if (output_format_ == kOutputSigned16) { | |
235 bus->ToInterleaved(bus->frames(), | |
236 OutputFormatSizeInBytes(output_format_), | |
237 result->writable_data()); | |
238 } else if (output_format_ == kOutputPlanarFloat) { | |
239 // Data in an AudioBus is already in planar float format; just copy each | |
240 // channel into the result buffer in order. | |
241 float* ptr = reinterpret_cast<float*>(result->writable_data()); | |
242 for (int c = 0; c < bus->channels(); ++c) { | |
243 memcpy(ptr, bus->channel(c), bus->frames() * sizeof(float)); | |
244 ptr += bus->frames(); | |
245 } | |
246 } else { | |
247 NOTREACHED(); | |
248 } | |
249 | |
250 result->set_duration(base::TimeDelta::FromMicroseconds( | |
251 bus->frames() * base::Time::kMicrosecondsPerSecond / | |
252 config_.samples_per_second)); | |
253 return make_scoped_refptr( | |
254 new media::DecoderBufferAdapter(config_.id, result)); | |
255 } | |
256 | |
257 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
258 InitializedCallback initialized_callback_; | |
259 OutputFormat output_format_; | |
260 media::AudioConfig config_; | |
261 scoped_ptr<::media::AudioDecoder> decoder_; | |
262 std::queue<DecodeBufferCallbackPair> decode_queue_; | |
263 bool initialized_; | |
264 scoped_ptr<::media::ChannelMixer> mixer_; | |
265 bool decode_pending_; | |
266 std::vector<scoped_refptr<::media::AudioBuffer>> decoded_chunks_; | |
267 base::WeakPtrFactory<CastAudioDecoderImpl> weak_factory_; | |
268 | |
269 DISALLOW_COPY_AND_ASSIGN(CastAudioDecoderImpl); | |
270 }; | |
271 | |
272 } // namespace | |
273 | |
274 // static | |
275 scoped_ptr<CastAudioDecoder> CastAudioDecoder::Create( | |
276 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
277 const media::AudioConfig& config, | |
278 OutputFormat output_format, | |
279 const InitializedCallback& initialized_callback) { | |
280 scoped_ptr<CastAudioDecoderImpl> decoder(new CastAudioDecoderImpl( | |
281 task_runner, initialized_callback, output_format)); | |
282 decoder->Initialize(config); | |
283 return decoder.Pass(); | |
284 } | |
285 | |
286 // static | |
287 int CastAudioDecoder::OutputFormatSizeInBytes( | |
288 CastAudioDecoder::OutputFormat format) { | |
289 switch (format) { | |
290 case CastAudioDecoder::OutputFormat::kOutputSigned16: | |
291 return 2; | |
292 case CastAudioDecoder::OutputFormat::kOutputPlanarFloat: | |
293 return 4; | |
294 } | |
295 NOTREACHED(); | |
296 return 1; | |
297 } | |
298 | |
299 } // namespace media | |
300 } // namespace chromecast | |
OLD | NEW |