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 "chrome/renderer/media/cast_receiver_audio_valve.h" | 5 #include "chrome/renderer/media/cast_receiver_audio_valve.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "media/audio/audio_parameters.h" |
| 10 |
7 CastReceiverAudioValve::CastReceiverAudioValve( | 11 CastReceiverAudioValve::CastReceiverAudioValve( |
| 12 const media::AudioParameters& params, |
8 media::AudioCapturerSource::CaptureCallback* cb) | 13 media::AudioCapturerSource::CaptureCallback* cb) |
9 : cb_(cb) { | 14 : cb_(cb), |
| 15 fifo_(base::Bind(&CastReceiverAudioValve::DeliverRebufferedAudio, |
| 16 base::Unretained(this))), |
| 17 sample_rate_(params.sample_rate()) { |
| 18 fifo_.Reset(params.frames_per_buffer()); |
10 } | 19 } |
| 20 |
11 CastReceiverAudioValve::~CastReceiverAudioValve() {} | 21 CastReceiverAudioValve::~CastReceiverAudioValve() {} |
12 | 22 |
13 void CastReceiverAudioValve::Capture(const media::AudioBus* audio_source, | 23 void CastReceiverAudioValve::DeliverDecodedAudio( |
14 int audio_delay_milliseconds, | 24 const media::AudioBus* audio_bus, |
15 double volume, | 25 base::TimeTicks playout_time) { |
16 bool key_pressed) { | 26 current_playout_time_ = playout_time; |
| 27 // The following will result in zero, one, or multiple synchronous calls to |
| 28 // DeliverRebufferedAudio(). |
| 29 fifo_.Push(*audio_bus); |
| 30 } |
| 31 |
| 32 void CastReceiverAudioValve::DeliverRebufferedAudio( |
| 33 const media::AudioBus& audio_bus, |
| 34 int frame_delay) { |
| 35 const base::TimeTicks playout_time = |
| 36 current_playout_time_ + |
| 37 base::TimeDelta::FromMicroseconds( |
| 38 frame_delay * base::Time::kMicrosecondsPerSecond / sample_rate_); |
| 39 |
17 base::AutoLock lock(lock_); | 40 base::AutoLock lock(lock_); |
18 if (cb_) { | 41 if (cb_) { |
19 cb_->Capture(audio_source, audio_delay_milliseconds, volume, key_pressed); | 42 // The AudioCapturerSource::Callback interface provides timing information |
| 43 // only as a relative delay value that means "captured N milliseconds ago." |
| 44 // Therefore, translate the playout time to these semantics. Since playout |
| 45 // time is generally in the future, because audio should be decoded well |
| 46 // ahead of when it should be played out, the audio delay value will |
| 47 // generally be negative. |
| 48 // |
| 49 // TimeTicks::Now() is being called after the |lock_| was acquired in order |
| 50 // to provide the most accurate delay value. |
| 51 const int audio_delay_milliseconds = |
| 52 (base::TimeTicks::Now() - playout_time).InMilliseconds(); |
| 53 |
| 54 cb_->Capture(&audio_bus, audio_delay_milliseconds, 1.0 /* volume */, |
| 55 false /* key_pressed */); |
20 } | 56 } |
21 } | 57 } |
22 | 58 |
23 void CastReceiverAudioValve::OnCaptureError(const std::string& message) { | |
24 base::AutoLock lock(lock_); | |
25 if (cb_) { | |
26 cb_->OnCaptureError(message); | |
27 } | |
28 } | |
29 | |
30 void CastReceiverAudioValve::Stop() { | 59 void CastReceiverAudioValve::Stop() { |
31 base::AutoLock lock(lock_); | 60 base::AutoLock lock(lock_); |
32 cb_ = nullptr; | 61 cb_ = nullptr; |
33 } | 62 } |
OLD | NEW |