OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/audio_sender/audio_encoder.h" | 5 #include "media/cast/audio_sender/audio_encoder.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 buffer_fill_end_ = 0; | 87 buffer_fill_end_ = 0; |
88 } | 88 } |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
92 protected: | 92 protected: |
93 virtual void TransferSamplesIntoBuffer(const AudioBus* audio_bus, | 93 virtual void TransferSamplesIntoBuffer(const AudioBus* audio_bus, |
94 int source_offset, | 94 int source_offset, |
95 int buffer_fill_offset, | 95 int buffer_fill_offset, |
96 int num_samples) = 0; | 96 int num_samples) = 0; |
97 virtual bool EncodeFromFilledBuffer(std::vector<uint8>* out) = 0; | 97 virtual bool EncodeFromFilledBuffer(std::string* out) = 0; |
98 | 98 |
99 CastEnvironment* const cast_environment_; | 99 CastEnvironment* const cast_environment_; |
100 const AudioCodec codec_; | 100 const AudioCodec codec_; |
101 const int num_channels_; | 101 const int num_channels_; |
102 const int samples_per_10ms_; | 102 const int samples_per_10ms_; |
103 const FrameEncodedCallback callback_; | 103 const FrameEncodedCallback callback_; |
104 | 104 |
105 private: | 105 private: |
106 // In the case where a call to EncodeAudio() cannot completely fill the | 106 // In the case where a call to EncodeAudio() cannot completely fill the |
107 // buffer, this points to the position at which to populate data in a later | 107 // buffer, this points to the position at which to populate data in a later |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 // Opus requires channel-interleaved samples in a single array. | 149 // Opus requires channel-interleaved samples in a single array. |
150 for (int ch = 0; ch < audio_bus->channels(); ++ch) { | 150 for (int ch = 0; ch < audio_bus->channels(); ++ch) { |
151 const float* src = audio_bus->channel(ch) + source_offset; | 151 const float* src = audio_bus->channel(ch) + source_offset; |
152 const float* const src_end = src + num_samples; | 152 const float* const src_end = src + num_samples; |
153 float* dest = buffer_.get() + buffer_fill_offset * num_channels_ + ch; | 153 float* dest = buffer_.get() + buffer_fill_offset * num_channels_ + ch; |
154 for (; src < src_end; ++src, dest += num_channels_) | 154 for (; src < src_end; ++src, dest += num_channels_) |
155 *dest = *src; | 155 *dest = *src; |
156 } | 156 } |
157 } | 157 } |
158 | 158 |
159 virtual bool EncodeFromFilledBuffer(std::vector<uint8>* out) OVERRIDE { | 159 virtual bool EncodeFromFilledBuffer(std::string* out) OVERRIDE { |
160 out->resize(kOpusMaxPayloadSize); | 160 out->resize(kOpusMaxPayloadSize); |
161 const opus_int32 result = opus_encode_float( | 161 const opus_int32 result = opus_encode_float( |
162 opus_encoder_, buffer_.get(), samples_per_10ms_, &out->front(), | 162 opus_encoder_, buffer_.get(), samples_per_10ms_, |
163 kOpusMaxPayloadSize); | 163 reinterpret_cast<uint8*>(&out->at(0)), kOpusMaxPayloadSize); |
164 if (result > 1) { | 164 if (result > 1) { |
165 out->resize(result); | 165 out->resize(result); |
166 return true; | 166 return true; |
167 } else if (result < 0) { | 167 } else if (result < 0) { |
168 LOG(ERROR) << "Error code from opus_encode_float(): " << result; | 168 LOG(ERROR) << "Error code from opus_encode_float(): " << result; |
169 return false; | 169 return false; |
170 } else { | 170 } else { |
171 // Do nothing: The documentation says that a return value of zero or | 171 // Do nothing: The documentation says that a return value of zero or |
172 // one byte means the packet does not need to be transmitted. | 172 // one byte means the packet does not need to be transmitted. |
173 return false; | 173 return false; |
(...skipping 29 matching lines...) Expand all Loading... |
203 private: | 203 private: |
204 virtual void TransferSamplesIntoBuffer(const AudioBus* audio_bus, | 204 virtual void TransferSamplesIntoBuffer(const AudioBus* audio_bus, |
205 int source_offset, | 205 int source_offset, |
206 int buffer_fill_offset, | 206 int buffer_fill_offset, |
207 int num_samples) OVERRIDE { | 207 int num_samples) OVERRIDE { |
208 audio_bus->ToInterleavedPartial( | 208 audio_bus->ToInterleavedPartial( |
209 source_offset, num_samples, sizeof(int16), | 209 source_offset, num_samples, sizeof(int16), |
210 buffer_.get() + buffer_fill_offset * num_channels_); | 210 buffer_.get() + buffer_fill_offset * num_channels_); |
211 } | 211 } |
212 | 212 |
213 virtual bool EncodeFromFilledBuffer(std::vector<uint8>* out) OVERRIDE { | 213 virtual bool EncodeFromFilledBuffer(std::string* out) OVERRIDE { |
214 // Output 16-bit PCM integers in big-endian byte order. | 214 // Output 16-bit PCM integers in big-endian byte order. |
215 out->resize(num_channels_ * samples_per_10ms_ * sizeof(int16)); | 215 out->resize(num_channels_ * samples_per_10ms_ * sizeof(int16)); |
216 const int16* src = buffer_.get(); | 216 const int16* src = buffer_.get(); |
217 const int16* const src_end = src + num_channels_ * samples_per_10ms_; | 217 const int16* const src_end = src + num_channels_ * samples_per_10ms_; |
218 uint16* dest = reinterpret_cast<uint16*>(&out->front()); | 218 uint16* dest = reinterpret_cast<uint16*>(&out->at(0)); |
219 for (; src < src_end; ++src, ++dest) | 219 for (; src < src_end; ++src, ++dest) |
220 *dest = base::HostToNet16(*src); | 220 *dest = base::HostToNet16(*src); |
221 return true; | 221 return true; |
222 } | 222 } |
223 | 223 |
224 private: | 224 private: |
225 const scoped_ptr<int16[]> buffer_; | 225 const scoped_ptr<int16[]> buffer_; |
226 | 226 |
227 DISALLOW_COPY_AND_ASSIGN(Pcm16Impl); | 227 DISALLOW_COPY_AND_ASSIGN(Pcm16Impl); |
228 }; | 228 }; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 void AudioEncoder::EncodeAudio( | 274 void AudioEncoder::EncodeAudio( |
275 const AudioBus* audio_bus, | 275 const AudioBus* audio_bus, |
276 const base::TimeTicks& recorded_time, | 276 const base::TimeTicks& recorded_time, |
277 const base::Closure& done_callback) { | 277 const base::Closure& done_callback) { |
278 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO_ENCODER)); | 278 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO_ENCODER)); |
279 impl_->EncodeAudio(audio_bus, recorded_time, done_callback); | 279 impl_->EncodeAudio(audio_bus, recorded_time, done_callback); |
280 } | 280 } |
281 | 281 |
282 } // namespace cast | 282 } // namespace cast |
283 } // namespace media | 283 } // namespace media |
OLD | NEW |