| 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" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 data_out->resize(result); | 69 data_out->resize(result); |
| 70 return true; | 70 return true; |
| 71 } | 71 } |
| 72 // If |result| in {0,1}, do nothing; the documentation says that a return | 72 // If |result| in {0,1}, do nothing; the documentation says that a return |
| 73 // value of zero or one means the packet does not need to be transmitted. | 73 // value of zero or one means the packet does not need to be transmitted. |
| 74 // Otherwise, we have an error. | 74 // Otherwise, we have an error. |
| 75 DLOG_IF(ERROR, result < 0) << " encode failed: " << opus_strerror(result); | 75 DLOG_IF(ERROR, result < 0) << " encode failed: " << opus_strerror(result); |
| 76 return false; | 76 return false; |
| 77 } | 77 } |
| 78 | 78 |
| 79 // Interleaves |audio_bus| channels() of floats into a single output linear | |
| 80 // |buffer|. | |
| 81 // TODO(mcasas) https://crbug.com/580391 use AudioBus::ToInterleavedFloat(). | |
| 82 void ToInterleaved(media::AudioBus* audio_bus, float* buffer) { | |
| 83 for (int ch = 0; ch < audio_bus->channels(); ++ch) { | |
| 84 const float* src = audio_bus->channel(ch); | |
| 85 const float* const src_end = src + audio_bus->frames(); | |
| 86 float* dest = buffer + ch; | |
| 87 for (; src < src_end; ++src, dest += audio_bus->channels()) | |
| 88 *dest = *src; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 } // anonymous namespace | 79 } // anonymous namespace |
| 93 | 80 |
| 94 // Nested class encapsulating opus-related encoding details. It contains an | 81 // Nested class encapsulating opus-related encoding details. It contains an |
| 95 // AudioConverter to adapt incoming data to the format Opus likes to have. | 82 // AudioConverter to adapt incoming data to the format Opus likes to have. |
| 96 // AudioEncoder is created and destroyed on ATR's main thread (usually the main | 83 // AudioEncoder is created and destroyed on ATR's main thread (usually the main |
| 97 // render thread) but otherwise should operate entirely on |encoder_thread_|, | 84 // render thread) but otherwise should operate entirely on |encoder_thread_|, |
| 98 // which is owned by AudioTrackRecorder. Be sure to delete |encoder_thread_| | 85 // which is owned by AudioTrackRecorder. Be sure to delete |encoder_thread_| |
| 99 // before deleting the AudioEncoder using it. | 86 // before deleting the AudioEncoder using it. |
| 100 class AudioTrackRecorder::AudioEncoder | 87 class AudioTrackRecorder::AudioEncoder |
| 101 : public base::RefCountedThreadSafe<AudioEncoder>, | 88 : public base::RefCountedThreadSafe<AudioEncoder>, |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 // instead of | 239 // instead of |
| 253 // an AudioFifo, to avoid copying data needlessly since we know the sizes of | 240 // an AudioFifo, to avoid copying data needlessly since we know the sizes of |
| 254 // both input and output and they are multiples. | 241 // both input and output and they are multiples. |
| 255 fifo_->Push(input_bus.get()); | 242 fifo_->Push(input_bus.get()); |
| 256 | 243 |
| 257 // Wait to have enough |input_bus|s to guarantee a satisfactory conversion. | 244 // Wait to have enough |input_bus|s to guarantee a satisfactory conversion. |
| 258 while (fifo_->frames() >= input_params_.frames_per_buffer()) { | 245 while (fifo_->frames() >= input_params_.frames_per_buffer()) { |
| 259 std::unique_ptr<media::AudioBus> audio_bus = media::AudioBus::Create( | 246 std::unique_ptr<media::AudioBus> audio_bus = media::AudioBus::Create( |
| 260 output_params_.channels(), kOpusPreferredFramesPerBuffer); | 247 output_params_.channels(), kOpusPreferredFramesPerBuffer); |
| 261 converter_->Convert(audio_bus.get()); | 248 converter_->Convert(audio_bus.get()); |
| 262 ToInterleaved(audio_bus.get(), buffer_.get()); | 249 audio_bus->ToInterleavedFloat(0, 0, audio_bus->frames(), buffer_.get()); |
| 263 | 250 |
| 264 std::unique_ptr<std::string> encoded_data(new std::string()); | 251 std::unique_ptr<std::string> encoded_data(new std::string()); |
| 265 if (DoEncode(opus_encoder_, buffer_.get(), kOpusPreferredFramesPerBuffer, | 252 if (DoEncode(opus_encoder_, buffer_.get(), kOpusPreferredFramesPerBuffer, |
| 266 encoded_data.get())) { | 253 encoded_data.get())) { |
| 267 const base::TimeTicks capture_time_of_first_sample = | 254 const base::TimeTicks capture_time_of_first_sample = |
| 268 capture_time - | 255 capture_time - |
| 269 base::TimeDelta::FromMicroseconds(fifo_->frames() * | 256 base::TimeDelta::FromMicroseconds(fifo_->frames() * |
| 270 base::Time::kMicrosecondsPerSecond / | 257 base::Time::kMicrosecondsPerSecond / |
| 271 input_params_.sample_rate()); | 258 input_params_.sample_rate()); |
| 272 on_encoded_audio_cb_.Run(output_params_, std::move(encoded_data), | 259 on_encoded_audio_cb_.Run(output_params_, std::move(encoded_data), |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 } | 337 } |
| 351 | 338 |
| 352 void AudioTrackRecorder::Resume() { | 339 void AudioTrackRecorder::Resume() { |
| 353 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 340 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 354 DCHECK(encoder_); | 341 DCHECK(encoder_); |
| 355 encoder_thread_.task_runner()->PostTask( | 342 encoder_thread_.task_runner()->PostTask( |
| 356 FROM_HERE, base::Bind(&AudioEncoder::set_paused, encoder_, false)); | 343 FROM_HERE, base::Bind(&AudioEncoder::set_paused, encoder_, false)); |
| 357 } | 344 } |
| 358 | 345 |
| 359 } // namespace content | 346 } // namespace content |
| OLD | NEW |