Index: media/audio/virtual_audio_sink.cc |
diff --git a/media/audio/virtual_audio_sink.cc b/media/audio/virtual_audio_sink.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0877912d1c5c42dec0d801ae2b4b77395052ab61 |
--- /dev/null |
+++ b/media/audio/virtual_audio_sink.cc |
@@ -0,0 +1,76 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/audio/virtual_audio_sink.h" |
+ |
+#include "base/bind.h" |
+#include "base/callback_helpers.h" |
+#include "base/location.h" |
+#include "base/single_thread_task_runner.h" |
+#include "media/audio/virtual_audio_input_stream.h" |
+ |
+namespace media { |
+ |
+// Buffer size limit is chosen large enough that in the normal case, we do not |
+// have data loss. |
+constexpr int kBufferSizeSecond = 1; |
+ |
+// This is not the system clock accuracy. But considering the cross-process |
+// communication, there is fluctuation between the actual and the ideal instant |
+// when we receive the audio data. Testing shows the fluctuation is on the order |
+// of 1 millisecond. |
+constexpr int kClockAccuracyMillisecond = 1; |
miu
2016/05/28 02:45:00
This seems wrong, given the code comments here:
h
qiangchen
2016/05/31 21:17:33
Done.
|
+ |
+// See AudioShifter comment for detail about this parameter. We just take the |
+// suggestion from there. |
+constexpr int kAdjustTimeSecond = 1; |
+ |
+VirtualAudioSink::VirtualAudioSink(AudioParameters param, |
+ VirtualAudioInputStream* target, |
+ AfterCloseCallback callback) |
+ : params_(param), |
+ target_(target), |
+ shifter_(base::TimeDelta::FromSeconds(kBufferSizeSecond), |
+ base::TimeDelta::FromMilliseconds(kClockAccuracyMillisecond), |
+ base::TimeDelta::FromSeconds(kAdjustTimeSecond), |
+ param.sample_rate(), |
+ param.channels()), |
+ task_runner_(target->GetTaskRunner()), |
+ after_close_callback_(callback) { |
+ target_->AddInputProvider(this, params_); |
+} |
+ |
+VirtualAudioSink::~VirtualAudioSink() {} |
+ |
+void VirtualAudioSink::Close() { |
+ target_->RemoveInputProvider(this, params_); |
+ const AfterCloseCallback& cb = base::ResetAndReturn(&after_close_callback_); |
+ if (!cb.is_null()) |
+ cb.Run(this); |
+} |
+ |
+void VirtualAudioSink::OnData(const AudioBus& source, |
+ base::TimeTicks reference_time) { |
+ // We must copy data here, and should not pass |source| to the |
miu
2016/05/28 02:45:00
Given this should be done in AudioOutputController
qiangchen
2016/05/31 21:17:33
Then I think we need a lock for shifter_, otherwis
miu
2016/06/01 00:29:42
Yep. Sounds good to me.
qiangchen
2016/06/01 17:51:11
Acknowledged.
|
+ // |task_runner_|, because there is no guarantee that |source| is still a |
+ // valid object when the posted task is run. |
+ std::unique_ptr<AudioBus> source_copy(AudioBus::Create(params_)); |
+ source.CopyTo(source_copy.get()); |
+ task_runner_->PostTask(FROM_HERE, |
+ base::Bind(&VirtualAudioSink::StoreData, AsWeakPtr(), |
+ source_copy.release(), reference_time)); |
+} |
+ |
+void VirtualAudioSink::StoreData(AudioBus* source, |
+ base::TimeTicks reference_time) { |
+ shifter_.Push(std::unique_ptr<AudioBus>(source), reference_time); |
+} |
+ |
+double VirtualAudioSink::ProvideInput(AudioBus* audio_bus, |
+ base::TimeDelta buffer_delay) { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ shifter_.Pull(audio_bus, base::TimeTicks::Now() + buffer_delay); |
+ return 1; |
+} |
+} |