Index: chrome/renderer/media/cast_receiver_audio_valve.cc |
diff --git a/chrome/renderer/media/cast_receiver_audio_valve.cc b/chrome/renderer/media/cast_receiver_audio_valve.cc |
index 785d90df7e85c5f5acbbdb6afff9c06c16cc1759..6efb23db0b73d7cebd2be8e54b7a3cd246c9414d 100644 |
--- a/chrome/renderer/media/cast_receiver_audio_valve.cc |
+++ b/chrome/renderer/media/cast_receiver_audio_valve.cc |
@@ -4,26 +4,55 @@ |
#include "chrome/renderer/media/cast_receiver_audio_valve.h" |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "media/audio/audio_parameters.h" |
+ |
CastReceiverAudioValve::CastReceiverAudioValve( |
+ const media::AudioParameters& params, |
media::AudioCapturerSource::CaptureCallback* cb) |
- : cb_(cb) { |
+ : cb_(cb), |
+ fifo_(base::Bind(&CastReceiverAudioValve::DeliverRebufferedAudio, |
+ base::Unretained(this))), |
+ sample_rate_(params.sample_rate()) { |
+ fifo_.Reset(params.frames_per_buffer()); |
} |
+ |
CastReceiverAudioValve::~CastReceiverAudioValve() {} |
-void CastReceiverAudioValve::Capture(const media::AudioBus* audio_source, |
- int audio_delay_milliseconds, |
- double volume, |
- bool key_pressed) { |
- base::AutoLock lock(lock_); |
- if (cb_) { |
- cb_->Capture(audio_source, audio_delay_milliseconds, volume, key_pressed); |
- } |
+void CastReceiverAudioValve::DeliverDecodedAudio( |
+ const media::AudioBus* audio_bus, |
+ base::TimeTicks playout_time) { |
+ current_playout_time_ = playout_time; |
+ // The following will result in zero, one, or multiple synchronous calls to |
+ // DeliverRebufferedAudio(). |
+ fifo_.Push(*audio_bus); |
} |
-void CastReceiverAudioValve::OnCaptureError(const std::string& message) { |
+void CastReceiverAudioValve::DeliverRebufferedAudio( |
+ const media::AudioBus& audio_bus, |
+ int frame_delay) { |
+ const base::TimeTicks playout_time = |
+ current_playout_time_ + |
+ base::TimeDelta::FromMicroseconds( |
+ frame_delay * base::Time::kMicrosecondsPerSecond / sample_rate_); |
+ |
base::AutoLock lock(lock_); |
if (cb_) { |
- cb_->OnCaptureError(message); |
+ // The AudioCapturerSource::Callback interface provides timing information |
+ // only as a relative delay value that means "captured N milliseconds ago." |
+ // Therefore, translate the playout time to these semantics. Since playout |
+ // time is generally in the future, because audio should be decoded well |
+ // ahead of when it should be played out, the audio delay value will |
+ // generally be negative. |
+ // |
+ // TimeTicks::Now() is being called after the |lock_| was acquired in order |
+ // to provide the most accurate delay value. |
+ const int audio_delay_milliseconds = |
+ (base::TimeTicks::Now() - playout_time).InMilliseconds(); |
+ |
+ cb_->Capture(&audio_bus, audio_delay_milliseconds, 1.0 /* volume */, |
+ false /* key_pressed */); |
} |
} |