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 "base/time/time.h" | |
10 #include "media/audio/audio_parameters.h" | |
11 | |
7 CastReceiverAudioValve::CastReceiverAudioValve( | 12 CastReceiverAudioValve::CastReceiverAudioValve( |
13 const media::AudioParameters& params, | |
8 media::AudioCapturerSource::CaptureCallback* cb) | 14 media::AudioCapturerSource::CaptureCallback* cb) |
9 : cb_(cb) { | 15 : cb_(cb), |
16 rechunker_( | |
17 base::TimeDelta::FromMicroseconds(params.frames_per_buffer() * | |
18 base::Time::kMicrosecondsPerSecond / | |
19 params.sample_rate()), | |
20 base::Bind(&CastReceiverAudioValve::DeliverRechunkedAudio, | |
21 base::Unretained(this))) { | |
22 rechunker_.SetSampleRate(params.sample_rate()); | |
xjz
2016/02/20 01:53:04
Need to check returning true?
miu
2016/02/23 04:27:41
No longer applicable.
| |
10 } | 23 } |
24 | |
11 CastReceiverAudioValve::~CastReceiverAudioValve() {} | 25 CastReceiverAudioValve::~CastReceiverAudioValve() {} |
12 | 26 |
13 void CastReceiverAudioValve::Capture(const media::AudioBus* audio_source, | 27 void CastReceiverAudioValve::DeliverDecodedAudio( |
14 int audio_delay_milliseconds, | 28 const media::AudioBus* audio_bus, |
15 double volume, | 29 base::TimeTicks playout_time) { |
16 bool key_pressed) { | 30 const base::TimeDelta timestamp_relative_to_origin = |
31 playout_time - base::TimeTicks(); | |
32 // The following will result in zero, one, or multiple synchronous calls to | |
33 // DeliverRechunkedAudio(). | |
34 rechunker_.Push(*audio_bus, timestamp_relative_to_origin); | |
35 } | |
36 | |
37 void CastReceiverAudioValve::DeliverRechunkedAudio( | |
38 const media::AudioBus& audio_bus, | |
39 base::TimeDelta timestamp_relative_to_origin) { | |
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 base::TimeTicks playout_time = | |
52 base::TimeTicks() + timestamp_relative_to_origin; | |
53 const int audio_delay_milliseconds = | |
54 (base::TimeTicks::Now() - playout_time).InMilliseconds(); | |
55 | |
56 cb_->Capture(&audio_bus, audio_delay_milliseconds, 1.0 /* volume */, | |
57 false /* key_pressed */); | |
20 } | 58 } |
21 } | 59 } |
22 | 60 |
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() { | 61 void CastReceiverAudioValve::Stop() { |
31 base::AutoLock lock(lock_); | 62 base::AutoLock lock(lock_); |
32 cb_ = nullptr; | 63 cb_ = nullptr; |
33 } | 64 } |
OLD | NEW |