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/logging.h" | 8 #include "base/logging.h" |
8 #include "base/time/time.h" | 9 #include "base/time/time.h" |
9 #include "content/renderer/media/webrtc_local_audio_track.h" | 10 #include "content/renderer/media/webrtc_local_audio_track.h" |
10 | 11 |
11 using media::AudioBus; | 12 using media::AudioBus; |
12 using media::AudioFifo; | 13 using media::AudioFifo; |
13 using media::AudioParameters; | 14 using media::AudioParameters; |
14 using media::ChannelLayout; | 15 using media::ChannelLayout; |
15 using media::CHANNEL_LAYOUT_MONO; | 16 using media::CHANNEL_LAYOUT_MONO; |
16 using media::CHANNEL_LAYOUT_STEREO; | 17 using media::CHANNEL_LAYOUT_STEREO; |
17 | 18 |
18 static const int kMaxNumberOfBuffersInFifo = 5; | 19 static const int kMaxNumberOfBuffersInFifo = 5; |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 | 22 |
22 WebAudioCapturerSource::WebAudioCapturerSource( | 23 WebAudioCapturerSource::WebAudioCapturerSource( |
23 const blink::WebMediaStreamSource& blink_source) | 24 blink::WebMediaStreamSource* blink_source) |
24 : track_(NULL), | 25 : track_(NULL), audio_format_changed_(false), blink_source_(*blink_source) { |
25 audio_format_changed_(false), | 26 DCHECK(blink_source); |
26 blink_source_(blink_source) { | 27 DCHECK(!blink_source_.isNull()); |
| 28 DVLOG(1) << "WebAudioCapturerSource::WebAudioCapturerSource()"; |
| 29 blink_source_.addAudioConsumer(this); |
27 } | 30 } |
28 | 31 |
29 WebAudioCapturerSource::~WebAudioCapturerSource() { | 32 WebAudioCapturerSource::~WebAudioCapturerSource() { |
30 DCHECK(thread_checker_.CalledOnValidThread()); | 33 DCHECK(thread_checker_.CalledOnValidThread()); |
31 removeFromBlinkSource(); | 34 DVLOG(1) << "WebAudioCapturerSource::~WebAudioCapturerSource()"; |
| 35 DeregisterFromBlinkSource(); |
32 } | 36 } |
33 | 37 |
34 void WebAudioCapturerSource::setFormat( | 38 void WebAudioCapturerSource::setFormat( |
35 size_t number_of_channels, float sample_rate) { | 39 size_t number_of_channels, float sample_rate) { |
36 DCHECK(thread_checker_.CalledOnValidThread()); | 40 DCHECK(thread_checker_.CalledOnValidThread()); |
37 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" | 41 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" |
38 << sample_rate << ")"; | 42 << sample_rate << ")"; |
39 | 43 |
40 // If the channel count is greater than 8, use discrete layout. However, | 44 // If the channel count is greater than 8, use discrete layout. However, |
41 // anything beyond 8 is ignored by the subsequent (WebRTC) audio pipeline. | 45 // anything beyond 8 is ignored by the subsequent (WebRTC) audio pipeline. |
(...skipping 28 matching lines...) Expand all Loading... |
70 base::AutoLock auto_lock(lock_); | 74 base::AutoLock auto_lock(lock_); |
71 track_ = track; | 75 track_ = track; |
72 } | 76 } |
73 | 77 |
74 void WebAudioCapturerSource::Stop() { | 78 void WebAudioCapturerSource::Stop() { |
75 DCHECK(thread_checker_.CalledOnValidThread()); | 79 DCHECK(thread_checker_.CalledOnValidThread()); |
76 { | 80 { |
77 base::AutoLock auto_lock(lock_); | 81 base::AutoLock auto_lock(lock_); |
78 track_ = NULL; | 82 track_ = NULL; |
79 } | 83 } |
80 // removeFromBlinkSource() should not be called while |lock_| is acquired, | 84 // DeregisterFromBlinkSource() should not be called while |lock_| is acquired, |
81 // as it could result in a deadlock. | 85 // as it could result in a deadlock. |
82 removeFromBlinkSource(); | 86 DeregisterFromBlinkSource(); |
83 } | 87 } |
84 | 88 |
85 void WebAudioCapturerSource::consumeAudio( | 89 void WebAudioCapturerSource::consumeAudio( |
86 const blink::WebVector<const float*>& audio_data, | 90 const blink::WebVector<const float*>& audio_data, |
87 size_t number_of_frames) { | 91 size_t number_of_frames) { |
88 base::AutoLock auto_lock(lock_); | 92 base::AutoLock auto_lock(lock_); |
89 if (!track_) | 93 if (!track_) |
90 return; | 94 return; |
91 | 95 |
92 // Update the downstream client if the audio format has been changed. | 96 // Update the downstream client if the audio format has been changed. |
(...skipping 19 matching lines...) Expand all Loading... |
112 } | 116 } |
113 | 117 |
114 // Compute the estimated capture time of the first sample frame of audio that | 118 // Compute the estimated capture time of the first sample frame of audio that |
115 // will be consumed from the FIFO in the loop below. | 119 // will be consumed from the FIFO in the loop below. |
116 base::TimeTicks estimated_capture_time = base::TimeTicks::Now() - | 120 base::TimeTicks estimated_capture_time = base::TimeTicks::Now() - |
117 fifo_->frames() * base::TimeDelta::FromSeconds(1) / params_.sample_rate(); | 121 fifo_->frames() * base::TimeDelta::FromSeconds(1) / params_.sample_rate(); |
118 | 122 |
119 fifo_->Push(wrapper_bus_.get()); | 123 fifo_->Push(wrapper_bus_.get()); |
120 while (fifo_->frames() >= capture_bus_->frames()) { | 124 while (fifo_->frames() >= capture_bus_->frames()) { |
121 fifo_->Consume(capture_bus_.get(), 0, capture_bus_->frames()); | 125 fifo_->Consume(capture_bus_.get(), 0, capture_bus_->frames()); |
122 track_->Capture(*capture_bus_, estimated_capture_time, false); | 126 track_->Capture(*capture_bus_, estimated_capture_time); |
123 | 127 |
124 // Advance the estimated capture time for the next FIFO consume operation. | 128 // Advance the estimated capture time for the next FIFO consume operation. |
125 estimated_capture_time += | 129 estimated_capture_time += |
126 capture_bus_->frames() * base::TimeDelta::FromSeconds(1) / | 130 capture_bus_->frames() * base::TimeDelta::FromSeconds(1) / |
127 params_.sample_rate(); | 131 params_.sample_rate(); |
128 } | 132 } |
129 } | 133 } |
130 | 134 |
131 // If registered as audio consumer in |blink_source_|, deregister from | 135 void WebAudioCapturerSource::DeregisterFromBlinkSource() { |
132 // |blink_source_| and stop keeping a reference to |blink_source_|. | |
133 // Failure to call this method when stopping the track might leave an invalid | |
134 // WebAudioCapturerSource reference still registered as an audio consumer on | |
135 // the blink side. | |
136 void WebAudioCapturerSource::removeFromBlinkSource() { | |
137 if (!blink_source_.isNull()) { | 136 if (!blink_source_.isNull()) { |
138 blink_source_.removeAudioConsumer(this); | 137 blink_source_.removeAudioConsumer(this); |
139 blink_source_.reset(); | 138 blink_source_.reset(); |
140 } | 139 } |
141 } | 140 } |
142 | 141 |
143 } // namespace content | 142 } // namespace content |
OLD | NEW |