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 "content/renderer/media/audio_track_recorder.h" | 5 #include "content/renderer/media/audio_track_recorder.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "media/audio/audio_parameters.h" | 13 #include "media/audio/audio_parameters.h" |
14 #include "media/base/audio_bus.h" | 14 #include "media/base/audio_bus.h" |
| 15 #include "media/base/audio_converter.h" |
| 16 #include "media/base/audio_fifo.h" |
15 #include "media/base/bind_to_current_loop.h" | 17 #include "media/base/bind_to_current_loop.h" |
16 #include "third_party/opus/src/include/opus.h" | 18 #include "third_party/opus/src/include/opus.h" |
17 | 19 |
18 // Note that this code follows the Chrome media convention of defining a "frame" | 20 // Note that this code follows the Chrome media convention of defining a "frame" |
19 // as "one multi-channel sample" as opposed to another common definition | 21 // as "one multi-channel sample" as opposed to another common definition meaning |
20 // meaning "a chunk of samples". Here this second definition of "frame" is | 22 // "a chunk of samples". Here this second definition of "frame" is called a |
21 // called a "buffer"; so what might be called "frame duration" is instead | 23 // "buffer"; so what might be called "frame duration" is instead "buffer |
22 // "buffer duration", and so on. | 24 // duration", and so on. |
23 | 25 |
24 namespace content { | 26 namespace content { |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
28 enum { | 30 enum : int { |
29 // This is the recommended value, according to documentation in | 31 // Recommended value for opus_encode_float(), according to documentation in |
30 // third_party/opus/src/include/opus.h, so that the Opus encoder does not | 32 // third_party/opus/src/include/opus.h, so that the Opus encoder does not |
31 // degrade the audio due to memory constraints. | 33 // degrade the audio due to memory constraints, and is independent of the |
32 OPUS_MAX_PAYLOAD_SIZE = 4000, | 34 // duration of the encoded buffer. |
| 35 kOpusMaxDataBytes = 4000, |
33 | 36 |
34 // Support for max sampling rate of 48KHz, 2 channels, 60 ms duration. | 37 // Opus preferred sampling rate for encoding. This is also the one WebM likes |
35 MAX_SAMPLES_PER_BUFFER = 48 * 2 * 60, | 38 // to have: https://wiki.xiph.org/MatroskaOpus. |
| 39 kOpusPreferredSamplingRate = 48000, |
| 40 |
| 41 // For quality reasons we try to encode 60ms, the maximum Opus buffer. |
| 42 kOpusPreferredBufferDurationMs = 60, |
| 43 |
| 44 // Maximum amount of buffers that can be held in the AudioEncoders' AudioFifo. |
| 45 // Recording is not real time, hence a certain buffering is allowed. |
| 46 kMaxNumberOfFifoBuffers = 2, |
36 }; | 47 }; |
37 | 48 |
| 49 // The amount of Frames in a 60 ms buffer @ 48000 samples/second. |
| 50 const int kOpusPreferredFramesPerBuffer = kOpusPreferredSamplingRate * |
| 51 kOpusPreferredBufferDurationMs / |
| 52 base::Time::kMillisecondsPerSecond; |
| 53 |
| 54 // Tries to encode |data_in|'s |num_samples| into |data_out|. |
| 55 bool DoEncode(OpusEncoder* opus_encoder, |
| 56 float* data_in, |
| 57 int num_samples, |
| 58 std::string* data_out) { |
| 59 DCHECK_EQ(kOpusPreferredFramesPerBuffer, num_samples); |
| 60 |
| 61 data_out->resize(kOpusMaxDataBytes); |
| 62 const opus_int32 result = opus_encode_float( |
| 63 opus_encoder, data_in, num_samples, |
| 64 reinterpret_cast<uint8_t*>(string_as_array(data_out)), kOpusMaxDataBytes); |
| 65 |
| 66 if (result > 1) { |
| 67 // TODO(ajose): Investigate improving this. http://crbug.com/547918 |
| 68 data_out->resize(result); |
| 69 return true; |
| 70 } |
| 71 // If |result| in {0,1}, do nothing; the documentation says that a return |
| 72 // value of zero or one means the packet does not need to be transmitted. |
| 73 // Otherwise, we have an error. |
| 74 DLOG_IF(ERROR, result < 0) << " encode failed: " << opus_strerror(result); |
| 75 return false; |
| 76 } |
| 77 |
| 78 // Interleaves |audio_bus| channels() of floats into a single output linear |
| 79 // |buffer|. |
| 80 // TODO(mcasas) https://crbug.com/580391 use AudioBus::ToInterleavedFloat(). |
| 81 void ToInterleaved(media::AudioBus* audio_bus, float* buffer) { |
| 82 for (int ch = 0; ch < audio_bus->channels(); ++ch) { |
| 83 const float* src = audio_bus->channel(ch); |
| 84 const float* const src_end = src + audio_bus->frames(); |
| 85 float* dest = buffer + ch; |
| 86 for (; src < src_end; ++src, dest += audio_bus->channels()) |
| 87 *dest = *src; |
| 88 } |
| 89 } |
| 90 |
38 } // anonymous namespace | 91 } // anonymous namespace |
39 | 92 |
40 // Nested class encapsulating opus-related encoding details. | 93 // Nested class encapsulating opus-related encoding details. It contains an |
41 // AudioEncoder is created and destroyed on ATR's main thread (usually the | 94 // AudioConverter to adapt incoming data to the format Opus likes to have. |
42 // main render thread) but otherwise should operate entirely on | 95 // AudioEncoder is created and destroyed on ATR's main thread (usually the main |
43 // |encoder_thread_|, which is owned by AudioTrackRecorder. Be sure to delete | 96 // render thread) but otherwise should operate entirely on |encoder_thread_|, |
44 // |encoder_thread_| before deleting the AudioEncoder using it. | 97 // which is owned by AudioTrackRecorder. Be sure to delete |encoder_thread_| |
| 98 // before deleting the AudioEncoder using it. |
45 class AudioTrackRecorder::AudioEncoder | 99 class AudioTrackRecorder::AudioEncoder |
46 : public base::RefCountedThreadSafe<AudioEncoder> { | 100 : public base::RefCountedThreadSafe<AudioEncoder>, |
| 101 public media::AudioConverter::InputCallback { |
47 public: | 102 public: |
48 AudioEncoder(const OnEncodedAudioCB& on_encoded_audio_cb, | 103 AudioEncoder(const OnEncodedAudioCB& on_encoded_audio_cb, |
49 int32_t bits_per_second); | 104 int32_t bits_per_second); |
50 | 105 |
51 void OnSetFormat(const media::AudioParameters& params); | 106 void OnSetFormat(const media::AudioParameters& params); |
52 | 107 |
53 void EncodeAudio(scoped_ptr<media::AudioBus> audio_bus, | 108 void EncodeAudio(scoped_ptr<media::AudioBus> audio_bus, |
54 const base::TimeTicks& capture_time); | 109 const base::TimeTicks& capture_time); |
55 | 110 |
56 private: | 111 private: |
57 friend class base::RefCountedThreadSafe<AudioEncoder>; | 112 friend class base::RefCountedThreadSafe<AudioEncoder>; |
58 | 113 |
59 ~AudioEncoder(); | 114 ~AudioEncoder() override; |
60 | 115 |
61 bool is_initialized() const { return !!opus_encoder_; } | 116 bool is_initialized() const { return !!opus_encoder_; } |
62 | 117 |
| 118 // media::AudioConverted::InputCallback implementation. |
| 119 double ProvideInput(media::AudioBus* audio_bus, |
| 120 base::TimeDelta buffer_delay) override; |
| 121 |
63 void DestroyExistingOpusEncoder(); | 122 void DestroyExistingOpusEncoder(); |
64 | 123 |
65 void TransferSamplesIntoBuffer(const media::AudioBus* audio_bus, | |
66 int source_offset, | |
67 int buffer_fill_offset, | |
68 int num_samples); | |
69 bool EncodeFromFilledBuffer(std::string* out); | |
70 | |
71 const OnEncodedAudioCB on_encoded_audio_cb_; | 124 const OnEncodedAudioCB on_encoded_audio_cb_; |
72 | 125 |
73 // Target bitrate for Opus. If 0, Opus provide automatic bitrate is used. | 126 // Target bitrate for Opus. If 0, Opus provide automatic bitrate is used. |
74 const int32_t bits_per_second_; | 127 const int32_t bits_per_second_; |
75 | 128 |
76 base::ThreadChecker encoder_thread_checker_; | 129 base::ThreadChecker encoder_thread_checker_; |
77 | 130 |
78 // In the case where a call to EncodeAudio() cannot completely fill the | 131 // Track Audio (ingress) and Opus encoder input parameters, respectively. They |
79 // buffer, this points to the position at which to populate data in a later | 132 // only differ in their sample_rate() and frames_per_buffer(): output is |
80 // call. | 133 // 48ksamples/s and 2880, respectively. |
81 int buffer_fill_end_; | 134 media::AudioParameters input_params_; |
| 135 media::AudioParameters output_params_; |
82 | 136 |
83 int frames_per_buffer_; | 137 // Sampling rate adapter between an OpusEncoder supported and the provided. |
84 | 138 scoped_ptr<media::AudioConverter> converter_; |
85 // The duration of one set of frames of encoded audio samples. | 139 scoped_ptr<media::AudioFifo> fifo_; |
86 base::TimeDelta buffer_duration_; | |
87 | |
88 media::AudioParameters audio_params_; | |
89 | 140 |
90 // Buffer for passing AudioBus data to OpusEncoder. | 141 // Buffer for passing AudioBus data to OpusEncoder. |
91 scoped_ptr<float[]> buffer_; | 142 scoped_ptr<float[]> buffer_; |
92 | 143 |
93 OpusEncoder* opus_encoder_; | 144 OpusEncoder* opus_encoder_; |
94 | 145 |
95 DISALLOW_COPY_AND_ASSIGN(AudioEncoder); | 146 DISALLOW_COPY_AND_ASSIGN(AudioEncoder); |
96 }; | 147 }; |
97 | 148 |
98 AudioTrackRecorder::AudioEncoder::AudioEncoder( | 149 AudioTrackRecorder::AudioEncoder::AudioEncoder( |
99 const OnEncodedAudioCB& on_encoded_audio_cb, | 150 const OnEncodedAudioCB& on_encoded_audio_cb, |
100 int32_t bits_per_second) | 151 int32_t bits_per_second) |
101 : on_encoded_audio_cb_(on_encoded_audio_cb), | 152 : on_encoded_audio_cb_(on_encoded_audio_cb), |
102 bits_per_second_(bits_per_second), | 153 bits_per_second_(bits_per_second), |
103 opus_encoder_(nullptr) { | 154 opus_encoder_(nullptr) { |
104 // AudioEncoder is constructed on the thread that ATR lives on, but should | 155 // AudioEncoder is constructed on the thread that ATR lives on, but should |
105 // operate only on the encoder thread after that. Reset | 156 // operate only on the encoder thread after that. Reset |
106 // |encoder_thread_checker_| here, as the next call to CalledOnValidThread() | 157 // |encoder_thread_checker_| here, as the next call to CalledOnValidThread() |
107 // will be from the encoder thread. | 158 // will be from the encoder thread. |
108 encoder_thread_checker_.DetachFromThread(); | 159 encoder_thread_checker_.DetachFromThread(); |
109 } | 160 } |
110 | 161 |
111 AudioTrackRecorder::AudioEncoder::~AudioEncoder() { | 162 AudioTrackRecorder::AudioEncoder::~AudioEncoder() { |
112 // We don't DCHECK that we're on the encoder thread here, as it should have | 163 // We don't DCHECK that we're on the encoder thread here, as it should have |
113 // already been deleted at this point. | 164 // already been deleted at this point. |
114 DestroyExistingOpusEncoder(); | 165 DestroyExistingOpusEncoder(); |
115 } | 166 } |
116 | 167 |
117 void AudioTrackRecorder::AudioEncoder::OnSetFormat( | 168 void AudioTrackRecorder::AudioEncoder::OnSetFormat( |
118 const media::AudioParameters& params) { | 169 const media::AudioParameters& input_params) { |
| 170 DVLOG(1) << __FUNCTION__; |
119 DCHECK(encoder_thread_checker_.CalledOnValidThread()); | 171 DCHECK(encoder_thread_checker_.CalledOnValidThread()); |
120 if (audio_params_.Equals(params)) | 172 if (input_params_.Equals(input_params)) |
121 return; | 173 return; |
122 | 174 |
123 DestroyExistingOpusEncoder(); | 175 DestroyExistingOpusEncoder(); |
124 | 176 |
125 if (!params.IsValid() || params.channels() > 2) { | 177 if (!input_params.IsValid()) { |
126 DLOG(ERROR) << "Invalid audio params: " << params.AsHumanReadableString(); | 178 DLOG(ERROR) << "Invalid params: " << input_params.AsHumanReadableString(); |
| 179 return; |
| 180 } |
| 181 input_params_ = input_params; |
| 182 input_params_.set_frames_per_buffer(input_params_.sample_rate() * |
| 183 kOpusPreferredBufferDurationMs / |
| 184 base::Time::kMillisecondsPerSecond); |
| 185 |
| 186 // third_party/libopus supports up to 2 channels (see implementation of |
| 187 // opus_encoder_create()): force |output_params_| to at most those. |
| 188 output_params_ = media::AudioParameters( |
| 189 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 190 media::GuessChannelLayout(std::min(input_params_.channels(), 2)), |
| 191 kOpusPreferredSamplingRate, |
| 192 input_params_.bits_per_sample(), |
| 193 kOpusPreferredFramesPerBuffer); |
| 194 DVLOG(1) << "|input_params_|:" << input_params_.AsHumanReadableString() |
| 195 << " -->|output_params_|:" << output_params_.AsHumanReadableString(); |
| 196 |
| 197 converter_.reset(new media::AudioConverter(input_params_, output_params_, |
| 198 false /* disable_fifo */)); |
| 199 converter_->AddInput(this); |
| 200 converter_->PrimeWithSilence(); |
| 201 |
| 202 fifo_.reset(new media::AudioFifo( |
| 203 input_params_.channels(), |
| 204 kMaxNumberOfFifoBuffers * input_params_.frames_per_buffer())); |
| 205 |
| 206 buffer_.reset(new float[output_params_.channels() * |
| 207 output_params_.frames_per_buffer()]); |
| 208 |
| 209 // Initialize OpusEncoder. |
| 210 int opus_result; |
| 211 opus_encoder_ = opus_encoder_create(output_params_.sample_rate(), |
| 212 output_params_.channels(), |
| 213 OPUS_APPLICATION_AUDIO, |
| 214 &opus_result); |
| 215 if (opus_result < 0) { |
| 216 DLOG(ERROR) << "Couldn't init opus encoder: " << opus_strerror(opus_result) |
| 217 << ", sample rate: " << output_params_.sample_rate() |
| 218 << ", channels: " << output_params_.channels(); |
127 return; | 219 return; |
128 } | 220 } |
129 | 221 |
130 buffer_duration_ = base::TimeDelta::FromMilliseconds( | |
131 AudioTrackRecorder::GetOpusBufferDuration(params.sample_rate())); | |
132 if (buffer_duration_ == base::TimeDelta()) { | |
133 DLOG(ERROR) << "Could not find a valid |buffer_duration| for the given " | |
134 << "sample rate: " << params.sample_rate(); | |
135 return; | |
136 } | |
137 | |
138 frames_per_buffer_ = | |
139 params.sample_rate() * buffer_duration_.InMilliseconds() / 1000; | |
140 if (frames_per_buffer_ * params.channels() > MAX_SAMPLES_PER_BUFFER) { | |
141 DLOG(ERROR) << "Invalid |frames_per_buffer_|: " << frames_per_buffer_; | |
142 return; | |
143 } | |
144 | |
145 // Initialize AudioBus buffer for OpusEncoder. | |
146 buffer_fill_end_ = 0; | |
147 buffer_.reset(new float[params.channels() * frames_per_buffer_]); | |
148 | |
149 // Initialize OpusEncoder. | |
150 DCHECK((params.sample_rate() != 48000) || (params.sample_rate() != 24000) || | |
151 (params.sample_rate() != 16000) || (params.sample_rate() != 12000) || | |
152 (params.sample_rate() != 8000)) | |
153 << "Opus supports only sample rates of {48, 24, 16, 12, 8}000, requested " | |
154 << params.sample_rate(); | |
155 int opus_result; | |
156 opus_encoder_ = opus_encoder_create(params.sample_rate(), params.channels(), | |
157 OPUS_APPLICATION_AUDIO, &opus_result); | |
158 if (opus_result < 0) { | |
159 DLOG(ERROR) << "Couldn't init opus encoder: " << opus_strerror(opus_result) | |
160 << ", sample rate: " << params.sample_rate() | |
161 << ", channels: " << params.channels(); | |
162 return; | |
163 } | |
164 | |
165 // Note: As of 2013-10-31, the encoder in "auto bitrate" mode would use a | 222 // Note: As of 2013-10-31, the encoder in "auto bitrate" mode would use a |
166 // variable bitrate up to 102kbps for 2-channel, 48 kHz audio and a 10 ms | 223 // variable bitrate up to 102kbps for 2-channel, 48 kHz audio and a 10 ms |
167 // buffer duration. The opus library authors may, of course, adjust this in | 224 // buffer duration. The opus library authors may, of course, adjust this in |
168 // later versions. | 225 // later versions. |
169 const opus_int32 bitrate = | 226 const opus_int32 bitrate = |
170 (bits_per_second_ > 0) ? bits_per_second_ : OPUS_AUTO; | 227 (bits_per_second_ > 0) ? bits_per_second_ : OPUS_AUTO; |
171 if (opus_encoder_ctl(opus_encoder_, OPUS_SET_BITRATE(bitrate)) != OPUS_OK) { | 228 if (opus_encoder_ctl(opus_encoder_, OPUS_SET_BITRATE(bitrate)) != OPUS_OK) { |
172 DLOG(ERROR) << "Failed to set opus bitrate: " << bitrate; | 229 DLOG(ERROR) << "Failed to set opus bitrate: " << bitrate; |
173 return; | 230 return; |
174 } | 231 } |
175 | |
176 audio_params_ = params; | |
177 } | 232 } |
178 | 233 |
179 void AudioTrackRecorder::AudioEncoder::EncodeAudio( | 234 void AudioTrackRecorder::AudioEncoder::EncodeAudio( |
180 scoped_ptr<media::AudioBus> audio_bus, | 235 scoped_ptr<media::AudioBus> input_bus, |
181 const base::TimeTicks& capture_time) { | 236 const base::TimeTicks& capture_time) { |
| 237 DVLOG(1) << __FUNCTION__ << ", #frames " << input_bus->frames(); |
182 DCHECK(encoder_thread_checker_.CalledOnValidThread()); | 238 DCHECK(encoder_thread_checker_.CalledOnValidThread()); |
183 DCHECK_EQ(audio_bus->channels(), audio_params_.channels()); | 239 DCHECK_EQ(input_bus->channels(), input_params_.channels()); |
| 240 DCHECK(!capture_time.is_null()); |
| 241 DCHECK(converter_); |
184 | 242 |
185 if (!is_initialized()) | 243 if (!is_initialized()) |
186 return; | 244 return; |
| 245 // TODO(mcasas): Consider using a std::deque<scoped_ptr<AudioBus>> instead of |
| 246 // an AudioFifo, to avoid copying data needlessly since we know the sizes of |
| 247 // both input and output and they are multiples. |
| 248 fifo_->Push(input_bus.get()); |
187 | 249 |
188 base::TimeDelta buffer_fill_duration = | 250 // Wait to have enough |input_bus|s to guarantee a satisfactory conversion. |
189 buffer_fill_end_ * buffer_duration_ / frames_per_buffer_; | 251 while (fifo_->frames() >= input_params_.frames_per_buffer()) { |
190 base::TimeTicks buffer_capture_time = capture_time - buffer_fill_duration; | 252 scoped_ptr<media::AudioBus> audio_bus = media::AudioBus::Create( |
191 | 253 output_params_.channels(), kOpusPreferredFramesPerBuffer); |
192 // Encode all audio in |audio_bus| into zero or more packets. | 254 converter_->Convert(audio_bus.get()); |
193 int src_pos = 0; | 255 ToInterleaved(audio_bus.get(), buffer_.get()); |
194 while (src_pos < audio_bus->frames()) { | |
195 const int num_samples_to_xfer = std::min( | |
196 frames_per_buffer_ - buffer_fill_end_, audio_bus->frames() - src_pos); | |
197 TransferSamplesIntoBuffer(audio_bus.get(), src_pos, buffer_fill_end_, | |
198 num_samples_to_xfer); | |
199 src_pos += num_samples_to_xfer; | |
200 buffer_fill_end_ += num_samples_to_xfer; | |
201 | |
202 if (buffer_fill_end_ < frames_per_buffer_) | |
203 break; | |
204 | 256 |
205 scoped_ptr<std::string> encoded_data(new std::string()); | 257 scoped_ptr<std::string> encoded_data(new std::string()); |
206 if (EncodeFromFilledBuffer(encoded_data.get())) { | 258 if (DoEncode(opus_encoder_, buffer_.get(), kOpusPreferredFramesPerBuffer, |
207 on_encoded_audio_cb_.Run(audio_params_, std::move(encoded_data), | 259 encoded_data.get())) { |
208 buffer_capture_time); | 260 const base::TimeTicks capture_time_of_first_sample = |
| 261 capture_time - |
| 262 base::TimeDelta::FromMicroseconds(fifo_->frames() * |
| 263 base::Time::kMicrosecondsPerSecond / |
| 264 input_params_.sample_rate()); |
| 265 on_encoded_audio_cb_.Run(output_params_, std::move(encoded_data), |
| 266 capture_time_of_first_sample); |
209 } | 267 } |
| 268 } |
| 269 } |
210 | 270 |
211 // Reset the capture timestamp and internal buffer for next set of frames. | 271 double AudioTrackRecorder::AudioEncoder::ProvideInput( |
212 buffer_capture_time += buffer_duration_; | 272 media::AudioBus* audio_bus, |
213 buffer_fill_end_ = 0; | 273 base::TimeDelta buffer_delay) { |
214 } | 274 fifo_->Consume(audio_bus, 0, audio_bus->frames()); |
| 275 return 1.0; // Return volume greater than zero to indicate we have more data. |
215 } | 276 } |
216 | 277 |
217 void AudioTrackRecorder::AudioEncoder::DestroyExistingOpusEncoder() { | 278 void AudioTrackRecorder::AudioEncoder::DestroyExistingOpusEncoder() { |
218 // We don't DCHECK that we're on the encoder thread here, as this could be | 279 // We don't DCHECK that we're on the encoder thread here, as this could be |
219 // called from the dtor (main thread) or from OnSetForamt() (render thread); | 280 // called from the dtor (main thread) or from OnSetForamt() (render thread); |
220 if (opus_encoder_) { | 281 if (opus_encoder_) { |
221 opus_encoder_destroy(opus_encoder_); | 282 opus_encoder_destroy(opus_encoder_); |
222 opus_encoder_ = nullptr; | 283 opus_encoder_ = nullptr; |
223 } | 284 } |
224 } | 285 } |
225 | 286 |
226 void AudioTrackRecorder::AudioEncoder::TransferSamplesIntoBuffer( | |
227 const media::AudioBus* audio_bus, | |
228 int source_offset, | |
229 int buffer_fill_offset, | |
230 int num_samples) { | |
231 // TODO(ajose): Consider replacing with AudioBus::ToInterleaved(). | |
232 // http://crbug.com/547918 | |
233 DCHECK(encoder_thread_checker_.CalledOnValidThread()); | |
234 DCHECK(is_initialized()); | |
235 // Opus requires channel-interleaved samples in a single array. | |
236 for (int ch = 0; ch < audio_bus->channels(); ++ch) { | |
237 const float* src = audio_bus->channel(ch) + source_offset; | |
238 const float* const src_end = src + num_samples; | |
239 float* dest = | |
240 buffer_.get() + buffer_fill_offset * audio_params_.channels() + ch; | |
241 for (; src < src_end; ++src, dest += audio_params_.channels()) | |
242 *dest = *src; | |
243 } | |
244 } | |
245 | |
246 bool AudioTrackRecorder::AudioEncoder::EncodeFromFilledBuffer( | |
247 std::string* out) { | |
248 DCHECK(encoder_thread_checker_.CalledOnValidThread()); | |
249 DCHECK(is_initialized()); | |
250 | |
251 out->resize(OPUS_MAX_PAYLOAD_SIZE); | |
252 const opus_int32 result = opus_encode_float( | |
253 opus_encoder_, buffer_.get(), frames_per_buffer_, | |
254 reinterpret_cast<uint8_t*>(string_as_array(out)), OPUS_MAX_PAYLOAD_SIZE); | |
255 if (result > 1) { | |
256 // TODO(ajose): Investigate improving this. http://crbug.com/547918 | |
257 out->resize(result); | |
258 return true; | |
259 } | |
260 // If |result| in {0,1}, do nothing; the documentation says that a return | |
261 // value of zero or one means the packet does not need to be transmitted. | |
262 // Otherwise, we have an error. | |
263 DLOG_IF(ERROR, result < 0) << __FUNCTION__ | |
264 << " failed: " << opus_strerror(result); | |
265 return false; | |
266 } | |
267 | |
268 AudioTrackRecorder::AudioTrackRecorder( | 287 AudioTrackRecorder::AudioTrackRecorder( |
269 const blink::WebMediaStreamTrack& track, | 288 const blink::WebMediaStreamTrack& track, |
270 const OnEncodedAudioCB& on_encoded_audio_cb, | 289 const OnEncodedAudioCB& on_encoded_audio_cb, |
271 int32_t bits_per_second) | 290 int32_t bits_per_second) |
272 : track_(track), | 291 : track_(track), |
273 encoder_(new AudioEncoder(media::BindToCurrentLoop(on_encoded_audio_cb), | 292 encoder_(new AudioEncoder(media::BindToCurrentLoop(on_encoded_audio_cb), |
274 bits_per_second)), | 293 bits_per_second)), |
275 encoder_thread_("AudioEncoderThread") { | 294 encoder_thread_("AudioEncoderThread") { |
276 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 295 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
277 DCHECK(!track_.isNull()); | 296 DCHECK(!track_.isNull()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 | 328 |
310 scoped_ptr<media::AudioBus> audio_data = | 329 scoped_ptr<media::AudioBus> audio_data = |
311 media::AudioBus::Create(audio_bus.channels(), audio_bus.frames()); | 330 media::AudioBus::Create(audio_bus.channels(), audio_bus.frames()); |
312 audio_bus.CopyTo(audio_data.get()); | 331 audio_bus.CopyTo(audio_data.get()); |
313 | 332 |
314 encoder_thread_.task_runner()->PostTask( | 333 encoder_thread_.task_runner()->PostTask( |
315 FROM_HERE, base::Bind(&AudioEncoder::EncodeAudio, encoder_, | 334 FROM_HERE, base::Bind(&AudioEncoder::EncodeAudio, encoder_, |
316 base::Passed(&audio_data), capture_time)); | 335 base::Passed(&audio_data), capture_time)); |
317 } | 336 } |
318 | 337 |
319 int AudioTrackRecorder::GetOpusBufferDuration(int sample_rate) { | |
320 // Valid buffer durations in millseconds. Note there are other valid | |
321 // durations for Opus, see https://tools.ietf.org/html/rfc6716#section-2.1.4 | |
322 // Descending order as longer durations can increase compression performance. | |
323 const std::vector<int> opus_valid_buffer_durations_ms = {60, 40, 20, 10}; | |
324 | |
325 // Search for a duration such that |sample_rate| % |buffers_per_second| == 0, | |
326 // where |buffers_per_second| = 1000ms / |possible_duration|. | |
327 for (auto possible_duration : opus_valid_buffer_durations_ms) { | |
328 if (sample_rate * possible_duration % 1000 == 0) { | |
329 return possible_duration; | |
330 } | |
331 } | |
332 | |
333 // Otherwise, couldn't find a good duration. | |
334 return 0; | |
335 } | |
336 | |
337 } // namespace content | 338 } // namespace content |
OLD | NEW |