OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromecast/media/cma/decoder/cast_audio_decoder.h" | 5 #include "chromecast/media/cma/decoder/cast_audio_decoder.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <limits> | 8 #include <limits> |
9 #include <queue> | 9 #include <queue> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/command_line.h" | |
15 #include "base/location.h" | 14 #include "base/location.h" |
16 #include "base/logging.h" | 15 #include "base/logging.h" |
17 #include "base/macros.h" | 16 #include "base/macros.h" |
18 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
19 #include "base/strings/string_number_conversions.h" | |
20 #include "chromecast/base/chromecast_switches.h" | |
21 #include "chromecast/media/cma/base/decoder_buffer_adapter.h" | 18 #include "chromecast/media/cma/base/decoder_buffer_adapter.h" |
22 #include "chromecast/media/cma/base/decoder_buffer_base.h" | 19 #include "chromecast/media/cma/base/decoder_buffer_base.h" |
23 #include "chromecast/media/cma/base/decoder_config_adapter.h" | 20 #include "chromecast/media/cma/base/decoder_config_adapter.h" |
24 #include "media/base/audio_buffer.h" | 21 #include "media/base/audio_buffer.h" |
25 #include "media/base/audio_bus.h" | 22 #include "media/base/audio_bus.h" |
26 #include "media/base/cdm_context.h" | 23 #include "media/base/cdm_context.h" |
27 #include "media/base/channel_layout.h" | 24 #include "media/base/channel_layout.h" |
28 #include "media/base/channel_mixer.h" | 25 #include "media/base/channel_mixer.h" |
29 #include "media/base/decoder_buffer.h" | 26 #include "media/base/decoder_buffer.h" |
30 #include "media/base/sample_format.h" | 27 #include "media/base/sample_format.h" |
(...skipping 12 matching lines...) Expand all Loading... |
43 2, // offset 9, channels | 40 2, // offset 9, channels |
44 0, 0, // offset 10, skip | 41 0, 0, // offset 10, skip |
45 static_cast<uint8_t>(kOpusSamplingRate & 0xFF), // offset 12, LE | 42 static_cast<uint8_t>(kOpusSamplingRate & 0xFF), // offset 12, LE |
46 static_cast<uint8_t>((kOpusSamplingRate >> 8) & 0xFF), | 43 static_cast<uint8_t>((kOpusSamplingRate >> 8) & 0xFF), |
47 static_cast<uint8_t>((kOpusSamplingRate >> 16) & 0xFF), | 44 static_cast<uint8_t>((kOpusSamplingRate >> 16) & 0xFF), |
48 static_cast<uint8_t>((kOpusSamplingRate >> 24) & 0xFF), | 45 static_cast<uint8_t>((kOpusSamplingRate >> 24) & 0xFF), |
49 0, 0, // offset 16, gain | 46 0, 0, // offset 16, gain |
50 0, // offset 18, stereo mapping | 47 0, // offset 18, stereo mapping |
51 }; | 48 }; |
52 | 49 |
53 const int kStereoOutputChannelCount = 2; | 50 const int kOutputChannelCount = 2; // Always output stereo audio. |
54 const int kLayout71OutputChannelCount = 8; | |
55 const int kMaxChannelInput = 2; | 51 const int kMaxChannelInput = 2; |
56 | 52 |
57 class CastAudioDecoderImpl : public CastAudioDecoder { | 53 class CastAudioDecoderImpl : public CastAudioDecoder { |
58 public: | 54 public: |
59 CastAudioDecoderImpl( | 55 CastAudioDecoderImpl( |
60 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 56 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
61 const InitializedCallback& initialized_callback, | 57 const InitializedCallback& initialized_callback, |
62 OutputFormat output_format) | 58 OutputFormat output_format) |
63 : task_runner_(task_runner), | 59 : task_runner_(task_runner), |
64 initialized_callback_(initialized_callback), | 60 initialized_callback_(initialized_callback), |
65 output_format_(output_format), | 61 output_format_(output_format), |
66 initialized_(false), | 62 initialized_(false), |
67 decode_pending_(false), | 63 decode_pending_(false), |
68 weak_factory_(this) { | 64 weak_factory_(this) {} |
69 if (base::CommandLine::InitializedForCurrentProcess() && | |
70 base::CommandLine::ForCurrentProcess()->HasSwitch( | |
71 switches::kAlsaNumOutputChannels)) { | |
72 base::StringToInt( | |
73 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
74 switches::kAlsaNumOutputChannels), | |
75 &num_output_channels_); | |
76 } else { | |
77 num_output_channels_ = kStereoOutputChannelCount; | |
78 } | |
79 DCHECK(num_output_channels_ == kStereoOutputChannelCount || | |
80 num_output_channels_ == kLayout71OutputChannelCount); | |
81 } | |
82 | 65 |
83 ~CastAudioDecoderImpl() override {} | 66 ~CastAudioDecoderImpl() override {} |
84 | 67 |
85 void Initialize(const media::AudioConfig& config) { | 68 void Initialize(const media::AudioConfig& config) { |
86 DCHECK(!initialized_); | 69 DCHECK(!initialized_); |
87 DCHECK_LE(config_.channel_number, kMaxChannelInput); | 70 DCHECK_LE(config_.channel_number, kMaxChannelInput); |
88 config_ = config; | 71 config_ = config; |
89 | |
90 if (config_.channel_number == 1) { | 72 if (config_.channel_number == 1) { |
91 // If the input is mono, create a ChannelMixer to convert mono to stereo. | 73 // If the input is mono, create a ChannelMixer to convert mono to stereo. |
92 // TODO(kmackay) Support other channel format conversions? | 74 // TODO(kmackay) Support other channel format conversions? |
93 mixer_.reset(new ::media::ChannelMixer(::media::CHANNEL_LAYOUT_MONO, | 75 mixer_.reset(new ::media::ChannelMixer(::media::CHANNEL_LAYOUT_MONO, |
94 ::media::CHANNEL_LAYOUT_STEREO)); | 76 ::media::CHANNEL_LAYOUT_STEREO)); |
95 } | 77 } |
96 if (num_output_channels_ == kLayout71OutputChannelCount) { | |
97 // If there are 8 output channel, create a Channel Mixer to convert | |
98 // stereo to CHANNEL_LAYOUT_7_1. | |
99 // TODO(tianyuwang): This is a hack for 8 channel test USB speaker. | |
100 // Channel mixer from MONO TO LAYOUT 7_1 doesn't work with the current | |
101 // test USB speaker. As a result, we need to convert MONO channel | |
102 // to STEREO first then convert it to LAYOUT_7_1. | |
103 mixer_7_1_.reset(new ::media::ChannelMixer(::media::CHANNEL_LAYOUT_STEREO, | |
104 ::media::CHANNEL_LAYOUT_7_1)); | |
105 } | |
106 | |
107 base::WeakPtr<CastAudioDecoderImpl> self = weak_factory_.GetWeakPtr(); | 78 base::WeakPtr<CastAudioDecoderImpl> self = weak_factory_.GetWeakPtr(); |
108 if (config.codec == media::kCodecOpus) { | 79 if (config.codec == media::kCodecOpus) { |
109 // Insert fake extradata to make OpusAudioDecoder work with v2mirroring. | 80 // Insert fake extradata to make OpusAudioDecoder work with v2mirroring. |
110 if (config_.extra_data.empty() && | 81 if (config_.extra_data.empty() && |
111 config_.samples_per_second == kOpusSamplingRate && | 82 config_.samples_per_second == kOpusSamplingRate && |
112 config_.channel_number == 2) | 83 config_.channel_number == 2) |
113 config_.extra_data.assign( | 84 config_.extra_data.assign( |
114 kFakeOpusExtraData, | 85 kFakeOpusExtraData, |
115 kFakeOpusExtraData + sizeof(kFakeOpusExtraData)); | 86 kFakeOpusExtraData + sizeof(kFakeOpusExtraData)); |
116 decoder_.reset(new ::media::OpusAudioDecoder(task_runner_)); | 87 decoder_.reset(new ::media::OpusAudioDecoder(task_runner_)); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 int bus_frame_offset = 0; | 204 int bus_frame_offset = 0; |
234 for (auto& chunk : decoded_chunks_) { | 205 for (auto& chunk : decoded_chunks_) { |
235 chunk->ReadFrames(chunk->frame_count(), 0, bus_frame_offset, | 206 chunk->ReadFrames(chunk->frame_count(), 0, bus_frame_offset, |
236 decoded.get()); | 207 decoded.get()); |
237 bus_frame_offset += chunk->frame_count(); | 208 bus_frame_offset += chunk->frame_count(); |
238 } | 209 } |
239 | 210 |
240 if (mixer_) { | 211 if (mixer_) { |
241 // Convert to stereo if necessary. | 212 // Convert to stereo if necessary. |
242 std::unique_ptr<::media::AudioBus> converted_to_stereo = | 213 std::unique_ptr<::media::AudioBus> converted_to_stereo = |
243 ::media::AudioBus::Create(kStereoOutputChannelCount, num_frames); | 214 ::media::AudioBus::Create(kOutputChannelCount, num_frames); |
244 mixer_->Transform(decoded.get(), converted_to_stereo.get()); | 215 mixer_->Transform(decoded.get(), converted_to_stereo.get()); |
245 decoded.swap(converted_to_stereo); | 216 decoded.swap(converted_to_stereo); |
246 } | 217 } |
247 | 218 |
248 // TODO(tianyuwang): Remove this hack for 7_1 USB test speaker. | |
249 if (mixer_7_1_) { | |
250 // Convert to layout 7_1 if necessary. | |
251 std::unique_ptr<::media::AudioBus> converted_to_7_1 = | |
252 ::media::AudioBus::Create(num_output_channels_, num_frames); | |
253 mixer_7_1_->Transform(decoded.get(), converted_to_7_1.get()); | |
254 decoded.swap(converted_to_7_1); | |
255 } | |
256 | |
257 // Convert to the desired output format. | 219 // Convert to the desired output format. |
258 return FinishConversion(decoded.get()); | 220 return FinishConversion(decoded.get()); |
259 } | 221 } |
260 | 222 |
261 scoped_refptr<media::DecoderBufferBase> FinishConversion( | 223 scoped_refptr<media::DecoderBufferBase> FinishConversion( |
262 ::media::AudioBus* bus) { | 224 ::media::AudioBus* bus) { |
263 DCHECK_EQ(num_output_channels_, bus->channels()); | 225 DCHECK_EQ(kOutputChannelCount, bus->channels()); |
264 int size = bus->frames() * num_output_channels_ * | 226 int size = bus->frames() * kOutputChannelCount * |
265 OutputFormatSizeInBytes(output_format_); | 227 OutputFormatSizeInBytes(output_format_); |
266 scoped_refptr<::media::DecoderBuffer> result( | 228 scoped_refptr<::media::DecoderBuffer> result( |
267 new ::media::DecoderBuffer(size)); | 229 new ::media::DecoderBuffer(size)); |
268 | 230 |
269 if (output_format_ == kOutputSigned16) { | 231 if (output_format_ == kOutputSigned16) { |
270 bus->ToInterleaved(bus->frames(), OutputFormatSizeInBytes(output_format_), | 232 bus->ToInterleaved(bus->frames(), OutputFormatSizeInBytes(output_format_), |
271 result->writable_data()); | 233 result->writable_data()); |
272 } else if (output_format_ == kOutputPlanarFloat) { | 234 } else if (output_format_ == kOutputPlanarFloat) { |
273 // Data in an AudioBus is already in planar float format; just copy each | 235 // Data in an AudioBus is already in planar float format; just copy each |
274 // channel into the result buffer in order. | 236 // channel into the result buffer in order. |
(...skipping 13 matching lines...) Expand all Loading... |
288 new media::DecoderBufferAdapter(config_.id, result)); | 250 new media::DecoderBufferAdapter(config_.id, result)); |
289 } | 251 } |
290 | 252 |
291 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 253 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
292 InitializedCallback initialized_callback_; | 254 InitializedCallback initialized_callback_; |
293 OutputFormat output_format_; | 255 OutputFormat output_format_; |
294 media::AudioConfig config_; | 256 media::AudioConfig config_; |
295 std::unique_ptr<::media::AudioDecoder> decoder_; | 257 std::unique_ptr<::media::AudioDecoder> decoder_; |
296 std::queue<DecodeBufferCallbackPair> decode_queue_; | 258 std::queue<DecodeBufferCallbackPair> decode_queue_; |
297 bool initialized_; | 259 bool initialized_; |
298 int num_output_channels_; | |
299 std::unique_ptr<::media::ChannelMixer> mixer_; | 260 std::unique_ptr<::media::ChannelMixer> mixer_; |
300 std::unique_ptr<::media::ChannelMixer> mixer_7_1_; | |
301 bool decode_pending_; | 261 bool decode_pending_; |
302 std::vector<scoped_refptr<::media::AudioBuffer>> decoded_chunks_; | 262 std::vector<scoped_refptr<::media::AudioBuffer>> decoded_chunks_; |
303 base::WeakPtrFactory<CastAudioDecoderImpl> weak_factory_; | 263 base::WeakPtrFactory<CastAudioDecoderImpl> weak_factory_; |
304 | 264 |
305 DISALLOW_COPY_AND_ASSIGN(CastAudioDecoderImpl); | 265 DISALLOW_COPY_AND_ASSIGN(CastAudioDecoderImpl); |
306 }; | 266 }; |
307 | 267 |
308 } // namespace | 268 } // namespace |
309 | 269 |
310 // static | 270 // static |
(...skipping 16 matching lines...) Expand all Loading... |
327 return 2; | 287 return 2; |
328 case CastAudioDecoder::OutputFormat::kOutputPlanarFloat: | 288 case CastAudioDecoder::OutputFormat::kOutputPlanarFloat: |
329 return 4; | 289 return 4; |
330 } | 290 } |
331 NOTREACHED(); | 291 NOTREACHED(); |
332 return 1; | 292 return 1; |
333 } | 293 } |
334 | 294 |
335 } // namespace media | 295 } // namespace media |
336 } // namespace chromecast | 296 } // namespace chromecast |
OLD | NEW |