OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "media/cast/receiver/audio_decoder.h" | 5 #include "media/cast/receiver/audio_decoder.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
11 #include "base/location.h" | 12 #include "base/location.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/sys_byteorder.h" | 15 #include "base/sys_byteorder.h" |
15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
16 #include "third_party/opus/src/include/opus.h" | 17 #include "third_party/opus/src/include/opus.h" |
17 | 18 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 scoped_ptr<AudioBus> decoded_audio = Decode( | 63 scoped_ptr<AudioBus> decoded_audio = Decode( |
63 encoded_frame->mutable_bytes(), | 64 encoded_frame->mutable_bytes(), |
64 static_cast<int>(encoded_frame->data.size())); | 65 static_cast<int>(encoded_frame->data.size())); |
65 | 66 |
66 scoped_ptr<FrameEvent> event(new FrameEvent()); | 67 scoped_ptr<FrameEvent> event(new FrameEvent()); |
67 event->timestamp = cast_environment_->Clock()->NowTicks(); | 68 event->timestamp = cast_environment_->Clock()->NowTicks(); |
68 event->type = FRAME_DECODED; | 69 event->type = FRAME_DECODED; |
69 event->media_type = AUDIO_EVENT; | 70 event->media_type = AUDIO_EVENT; |
70 event->rtp_timestamp = encoded_frame->rtp_timestamp; | 71 event->rtp_timestamp = encoded_frame->rtp_timestamp; |
71 event->frame_id = encoded_frame->frame_id; | 72 event->frame_id = encoded_frame->frame_id; |
72 cast_environment_->logger()->DispatchFrameEvent(event.Pass()); | 73 cast_environment_->logger()->DispatchFrameEvent(std::move(event)); |
73 | 74 |
74 cast_environment_->PostTask(CastEnvironment::MAIN, | 75 cast_environment_->PostTask(CastEnvironment::MAIN, |
75 FROM_HERE, | 76 FROM_HERE, |
76 base::Bind(callback, | 77 base::Bind(callback, |
77 base::Passed(&decoded_audio), | 78 base::Passed(&decoded_audio), |
78 is_continuous)); | 79 is_continuous)); |
79 } | 80 } |
80 | 81 |
81 protected: | 82 protected: |
82 friend class base::RefCountedThreadSafe<ImplBase>; | 83 friend class base::RefCountedThreadSafe<ImplBase>; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 opus_decode_float( | 135 opus_decode_float( |
135 opus_decoder_, NULL, 0, buffer_.get(), max_samples_per_frame_, 0); | 136 opus_decoder_, NULL, 0, buffer_.get(), max_samples_per_frame_, 0); |
136 DCHECK_GE(result, 0); | 137 DCHECK_GE(result, 0); |
137 } | 138 } |
138 | 139 |
139 scoped_ptr<AudioBus> Decode(uint8_t* data, int len) final { | 140 scoped_ptr<AudioBus> Decode(uint8_t* data, int len) final { |
140 scoped_ptr<AudioBus> audio_bus; | 141 scoped_ptr<AudioBus> audio_bus; |
141 const opus_int32 num_samples_decoded = opus_decode_float( | 142 const opus_int32 num_samples_decoded = opus_decode_float( |
142 opus_decoder_, data, len, buffer_.get(), max_samples_per_frame_, 0); | 143 opus_decoder_, data, len, buffer_.get(), max_samples_per_frame_, 0); |
143 if (num_samples_decoded <= 0) | 144 if (num_samples_decoded <= 0) |
144 return audio_bus.Pass(); // Decode error. | 145 return audio_bus; // Decode error. |
145 | 146 |
146 // Copy interleaved samples from |buffer_| into a new AudioBus (where | 147 // Copy interleaved samples from |buffer_| into a new AudioBus (where |
147 // samples are stored in planar format, for each channel). | 148 // samples are stored in planar format, for each channel). |
148 audio_bus = AudioBus::Create(num_channels_, num_samples_decoded).Pass(); | 149 audio_bus = AudioBus::Create(num_channels_, num_samples_decoded); |
149 // TODO(miu): This should be moved into AudioBus::FromInterleaved(). | 150 // TODO(miu): This should be moved into AudioBus::FromInterleaved(). |
150 for (int ch = 0; ch < num_channels_; ++ch) { | 151 for (int ch = 0; ch < num_channels_; ++ch) { |
151 const float* src = buffer_.get() + ch; | 152 const float* src = buffer_.get() + ch; |
152 const float* const src_end = src + num_samples_decoded * num_channels_; | 153 const float* const src_end = src + num_samples_decoded * num_channels_; |
153 float* dest = audio_bus->channel(ch); | 154 float* dest = audio_bus->channel(ch); |
154 for (; src < src_end; src += num_channels_, ++dest) | 155 for (; src < src_end; src += num_channels_, ++dest) |
155 *dest = *src; | 156 *dest = *src; |
156 } | 157 } |
157 return audio_bus.Pass(); | 158 return audio_bus; |
158 } | 159 } |
159 | 160 |
160 const scoped_ptr<uint8_t[]> decoder_memory_; | 161 const scoped_ptr<uint8_t[]> decoder_memory_; |
161 OpusDecoder* const opus_decoder_; | 162 OpusDecoder* const opus_decoder_; |
162 const int max_samples_per_frame_; | 163 const int max_samples_per_frame_; |
163 const scoped_ptr<float[]> buffer_; | 164 const scoped_ptr<float[]> buffer_; |
164 | 165 |
165 // According to documentation in third_party/opus/src/include/opus.h, we must | 166 // According to documentation in third_party/opus/src/include/opus.h, we must |
166 // provide enough space in |buffer_| to contain 120ms of samples. At 48 kHz, | 167 // provide enough space in |buffer_| to contain 120ms of samples. At 48 kHz, |
167 // then, that means 5760 samples times the number of channels. | 168 // then, that means 5760 samples times the number of channels. |
(...skipping 16 matching lines...) Expand all Loading... |
184 ImplBase::operational_status_ = STATUS_INITIALIZED; | 185 ImplBase::operational_status_ = STATUS_INITIALIZED; |
185 } | 186 } |
186 | 187 |
187 private: | 188 private: |
188 ~Pcm16Impl() final {} | 189 ~Pcm16Impl() final {} |
189 | 190 |
190 scoped_ptr<AudioBus> Decode(uint8_t* data, int len) final { | 191 scoped_ptr<AudioBus> Decode(uint8_t* data, int len) final { |
191 scoped_ptr<AudioBus> audio_bus; | 192 scoped_ptr<AudioBus> audio_bus; |
192 const int num_samples = len / sizeof(int16_t) / num_channels_; | 193 const int num_samples = len / sizeof(int16_t) / num_channels_; |
193 if (num_samples <= 0) | 194 if (num_samples <= 0) |
194 return audio_bus.Pass(); | 195 return audio_bus; |
195 | 196 |
196 int16_t* const pcm_data = reinterpret_cast<int16_t*>(data); | 197 int16_t* const pcm_data = reinterpret_cast<int16_t*>(data); |
197 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 198 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
198 // Convert endianness. | 199 // Convert endianness. |
199 const int num_elements = num_samples * num_channels_; | 200 const int num_elements = num_samples * num_channels_; |
200 for (int i = 0; i < num_elements; ++i) | 201 for (int i = 0; i < num_elements; ++i) |
201 pcm_data[i] = static_cast<int16_t>(base::NetToHost16(pcm_data[i])); | 202 pcm_data[i] = static_cast<int16_t>(base::NetToHost16(pcm_data[i])); |
202 #endif | 203 #endif |
203 audio_bus = AudioBus::Create(num_channels_, num_samples).Pass(); | 204 audio_bus = AudioBus::Create(num_channels_, num_samples); |
204 audio_bus->FromInterleaved(pcm_data, num_samples, sizeof(int16_t)); | 205 audio_bus->FromInterleaved(pcm_data, num_samples, sizeof(int16_t)); |
205 return audio_bus.Pass(); | 206 return audio_bus; |
206 } | 207 } |
207 | 208 |
208 DISALLOW_COPY_AND_ASSIGN(Pcm16Impl); | 209 DISALLOW_COPY_AND_ASSIGN(Pcm16Impl); |
209 }; | 210 }; |
210 | 211 |
211 AudioDecoder::AudioDecoder( | 212 AudioDecoder::AudioDecoder( |
212 const scoped_refptr<CastEnvironment>& cast_environment, | 213 const scoped_refptr<CastEnvironment>& cast_environment, |
213 int channels, | 214 int channels, |
214 int sampling_rate, | 215 int sampling_rate, |
215 Codec codec) | 216 Codec codec) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 cast_environment_->PostTask(CastEnvironment::AUDIO, | 248 cast_environment_->PostTask(CastEnvironment::AUDIO, |
248 FROM_HERE, | 249 FROM_HERE, |
249 base::Bind(&AudioDecoder::ImplBase::DecodeFrame, | 250 base::Bind(&AudioDecoder::ImplBase::DecodeFrame, |
250 impl_, | 251 impl_, |
251 base::Passed(&encoded_frame), | 252 base::Passed(&encoded_frame), |
252 callback)); | 253 callback)); |
253 } | 254 } |
254 | 255 |
255 } // namespace cast | 256 } // namespace cast |
256 } // namespace media | 257 } // namespace media |
OLD | NEW |