OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/audio/virtual_audio_sink.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/location.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 #include "media/audio/virtual_audio_input_stream.h" |
| 12 |
| 13 namespace media { |
| 14 |
| 15 // Buffer size limit is chosen large enough that in the normal case, we do not |
| 16 // have data loss. |
| 17 constexpr int kBufferSizeSecond = 1; |
| 18 |
| 19 // The accuracy contains two components: 1. the cross-process communication, |
| 20 // there is fluctuation between the actual and the ideal instant when we receive |
| 21 // the audio data; 2. The system clock resolution: worst case is ~15ms on |
| 22 // Windows machines without a working high-resolution clock. |
| 23 constexpr int kClockAccuracyMillisecond = 20; |
| 24 |
| 25 // See AudioShifter comment for detail about this parameter. We just take the |
| 26 // suggestion from there. |
| 27 constexpr int kAdjustTimeSecond = 1; |
| 28 |
| 29 VirtualAudioSink::VirtualAudioSink(const AudioParameters& param, |
| 30 VirtualAudioInputStream* target, |
| 31 const AfterCloseCallback& callback) |
| 32 : params_(param), |
| 33 target_(target), |
| 34 shifter_(base::TimeDelta::FromSeconds(kBufferSizeSecond), |
| 35 base::TimeDelta::FromMilliseconds(kClockAccuracyMillisecond), |
| 36 base::TimeDelta::FromSeconds(kAdjustTimeSecond), |
| 37 param.sample_rate(), |
| 38 param.channels()), |
| 39 after_close_callback_(callback) { |
| 40 target_->AddInputProvider(this, params_); |
| 41 } |
| 42 |
| 43 VirtualAudioSink::~VirtualAudioSink() {} |
| 44 |
| 45 void VirtualAudioSink::Close() { |
| 46 target_->RemoveInputProvider(this, params_); |
| 47 const AfterCloseCallback& cb = base::ResetAndReturn(&after_close_callback_); |
| 48 if (!cb.is_null()) |
| 49 cb.Run(this); |
| 50 } |
| 51 |
| 52 void VirtualAudioSink::OnData(std::unique_ptr<AudioBus> source, |
| 53 base::TimeTicks reference_time) { |
| 54 base::AutoLock lock(shifter_lock_); |
| 55 shifter_.Push(std::move(source), reference_time); |
| 56 } |
| 57 |
| 58 double VirtualAudioSink::ProvideInput(AudioBus* audio_bus, |
| 59 uint32_t frames_delayed) { |
| 60 base::AutoLock lock(shifter_lock_); |
| 61 shifter_.Pull(audio_bus, |
| 62 base::TimeTicks::Now() + |
| 63 base::TimeDelta::FromMicroseconds( |
| 64 frames_delayed * params_.GetMicrosecondsPerFrame())); |
| 65 return 1; |
| 66 } |
| 67 } |
OLD | NEW |