OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/media/webaudio_capturer_source.h" | 5 #include "content/renderer/media/webaudio_capturer_source.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "content/renderer/media/webrtc_local_audio_track.h" | 10 #include "content/renderer/media/webrtc_local_audio_track.h" |
11 | 11 |
12 using media::AudioBus; | 12 using media::AudioBus; |
13 using media::AudioParameters; | 13 using media::AudioParameters; |
14 using media::ChannelLayout; | 14 using media::ChannelLayout; |
15 using media::CHANNEL_LAYOUT_MONO; | 15 using media::CHANNEL_LAYOUT_MONO; |
16 using media::CHANNEL_LAYOUT_STEREO; | 16 using media::CHANNEL_LAYOUT_STEREO; |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 | 19 |
20 WebAudioCapturerSource::WebAudioCapturerSource( | 20 WebAudioCapturerSource::WebAudioCapturerSource( |
21 blink::WebMediaStreamSource* blink_source) | 21 const blink::WebMediaStreamSource& blink_source) |
22 : track_(NULL), | 22 : track_(NULL), |
23 audio_format_changed_(false), | 23 audio_format_changed_(false), |
24 fifo_(base::Bind(&WebAudioCapturerSource::DeliverRebufferedAudio, | 24 fifo_(base::Bind(&WebAudioCapturerSource::DeliverRebufferedAudio, |
25 base::Unretained(this))), | 25 base::Unretained(this))), |
26 blink_source_(*blink_source) { | 26 blink_source_(blink_source) {} |
27 DCHECK(blink_source); | |
28 DCHECK(!blink_source_.isNull()); | |
29 DVLOG(1) << "WebAudioCapturerSource::WebAudioCapturerSource()"; | |
30 blink_source_.addAudioConsumer(this); | |
31 } | |
32 | 27 |
33 WebAudioCapturerSource::~WebAudioCapturerSource() { | 28 WebAudioCapturerSource::~WebAudioCapturerSource() { |
34 DCHECK(thread_checker_.CalledOnValidThread()); | 29 DCHECK(thread_checker_.CalledOnValidThread()); |
35 DVLOG(1) << "WebAudioCapturerSource::~WebAudioCapturerSource()"; | 30 removeFromBlinkSource(); |
36 DeregisterFromBlinkSource(); | |
37 } | 31 } |
38 | 32 |
39 void WebAudioCapturerSource::setFormat( | 33 void WebAudioCapturerSource::setFormat( |
40 size_t number_of_channels, float sample_rate) { | 34 size_t number_of_channels, float sample_rate) { |
41 DCHECK(thread_checker_.CalledOnValidThread()); | 35 DCHECK(thread_checker_.CalledOnValidThread()); |
42 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" | 36 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" |
43 << sample_rate << ")"; | 37 << sample_rate << ")"; |
44 | 38 |
45 // If the channel count is greater than 8, use discrete layout. However, | 39 // If the channel count is greater than 8, use discrete layout. However, |
46 // anything beyond 8 is ignored by the subsequent (WebRTC) audio pipeline. | 40 // anything beyond 8 is ignored by the subsequent (WebRTC) audio pipeline. |
(...skipping 27 matching lines...) Expand all Loading... |
74 base::AutoLock auto_lock(lock_); | 68 base::AutoLock auto_lock(lock_); |
75 track_ = track; | 69 track_ = track; |
76 } | 70 } |
77 | 71 |
78 void WebAudioCapturerSource::Stop() { | 72 void WebAudioCapturerSource::Stop() { |
79 DCHECK(thread_checker_.CalledOnValidThread()); | 73 DCHECK(thread_checker_.CalledOnValidThread()); |
80 { | 74 { |
81 base::AutoLock auto_lock(lock_); | 75 base::AutoLock auto_lock(lock_); |
82 track_ = NULL; | 76 track_ = NULL; |
83 } | 77 } |
84 // DeregisterFromBlinkSource() should not be called while |lock_| is acquired, | 78 // removeFromBlinkSource() should not be called while |lock_| is acquired, |
85 // as it could result in a deadlock. | 79 // as it could result in a deadlock. |
86 DeregisterFromBlinkSource(); | 80 removeFromBlinkSource(); |
87 } | 81 } |
88 | 82 |
89 void WebAudioCapturerSource::consumeAudio( | 83 void WebAudioCapturerSource::consumeAudio( |
90 const blink::WebVector<const float*>& audio_data, | 84 const blink::WebVector<const float*>& audio_data, |
91 size_t number_of_frames) { | 85 size_t number_of_frames) { |
92 // TODO(miu): Plumbing is needed to determine the actual capture timestamp | 86 // TODO(miu): Plumbing is needed to determine the actual capture timestamp |
93 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper | 87 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper |
94 // audio/video sync. http://crbug.com/335335 | 88 // audio/video sync. http://crbug.com/335335 |
95 current_reference_time_ = base::TimeTicks::Now(); | 89 current_reference_time_ = base::TimeTicks::Now(); |
96 | 90 |
(...skipping 19 matching lines...) Expand all Loading... |
116 | 110 |
117 void WebAudioCapturerSource::DeliverRebufferedAudio( | 111 void WebAudioCapturerSource::DeliverRebufferedAudio( |
118 const media::AudioBus& audio_bus, | 112 const media::AudioBus& audio_bus, |
119 int frame_delay) { | 113 int frame_delay) { |
120 lock_.AssertAcquired(); | 114 lock_.AssertAcquired(); |
121 const base::TimeTicks reference_time = | 115 const base::TimeTicks reference_time = |
122 current_reference_time_ + | 116 current_reference_time_ + |
123 base::TimeDelta::FromMicroseconds(frame_delay * | 117 base::TimeDelta::FromMicroseconds(frame_delay * |
124 base::Time::kMicrosecondsPerSecond / | 118 base::Time::kMicrosecondsPerSecond / |
125 params_.sample_rate()); | 119 params_.sample_rate()); |
126 track_->Capture(audio_bus, reference_time); | 120 track_->Capture(audio_bus, reference_time, false); |
127 } | 121 } |
128 | 122 |
129 void WebAudioCapturerSource::DeregisterFromBlinkSource() { | 123 // If registered as audio consumer in |blink_source_|, deregister from |
| 124 // |blink_source_| and stop keeping a reference to |blink_source_|. |
| 125 // Failure to call this method when stopping the track might leave an invalid |
| 126 // WebAudioCapturerSource reference still registered as an audio consumer on |
| 127 // the blink side. |
| 128 void WebAudioCapturerSource::removeFromBlinkSource() { |
130 if (!blink_source_.isNull()) { | 129 if (!blink_source_.isNull()) { |
131 blink_source_.removeAudioConsumer(this); | 130 blink_source_.removeAudioConsumer(this); |
132 blink_source_.reset(); | 131 blink_source_.reset(); |
133 } | 132 } |
134 } | 133 } |
135 | 134 |
136 } // namespace content | 135 } // namespace content |
OLD | NEW |