Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: content/renderer/media/audio_track_recorder.cc

Issue 2024993004: AudioBus: Add a ToInterleavedFloat() method to AudioBus (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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(),
250 buffer_.get());
mcasas 2016/06/02 01:13:14 Fits in one line? audio_bus->ToInterleavedFloa
chfremer 2016/06/02 18:13:33 Done.
263 251
264 std::unique_ptr<std::string> encoded_data(new std::string()); 252 std::unique_ptr<std::string> encoded_data(new std::string());
265 if (DoEncode(opus_encoder_, buffer_.get(), kOpusPreferredFramesPerBuffer, 253 if (DoEncode(opus_encoder_, buffer_.get(), kOpusPreferredFramesPerBuffer,
266 encoded_data.get())) { 254 encoded_data.get())) {
267 const base::TimeTicks capture_time_of_first_sample = 255 const base::TimeTicks capture_time_of_first_sample =
268 capture_time - 256 capture_time -
269 base::TimeDelta::FromMicroseconds(fifo_->frames() * 257 base::TimeDelta::FromMicroseconds(fifo_->frames() *
270 base::Time::kMicrosecondsPerSecond / 258 base::Time::kMicrosecondsPerSecond /
271 input_params_.sample_rate()); 259 input_params_.sample_rate());
272 on_encoded_audio_cb_.Run(output_params_, std::move(encoded_data), 260 on_encoded_audio_cb_.Run(output_params_, std::move(encoded_data),
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 } 338 }
351 339
352 void AudioTrackRecorder::Resume() { 340 void AudioTrackRecorder::Resume() {
353 DCHECK(main_render_thread_checker_.CalledOnValidThread()); 341 DCHECK(main_render_thread_checker_.CalledOnValidThread());
354 DCHECK(encoder_); 342 DCHECK(encoder_);
355 encoder_thread_.task_runner()->PostTask( 343 encoder_thread_.task_runner()->PostTask(
356 FROM_HERE, base::Bind(&AudioEncoder::set_paused, encoder_, false)); 344 FROM_HERE, base::Bind(&AudioEncoder::set_paused, encoder_, false));
357 } 345 }
358 346
359 } // namespace content 347 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698