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/logging.h" | 7 #include "base/logging.h" |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 #include "content/renderer/media/webrtc_local_audio_track.h" | 9 #include "content/renderer/media/webrtc_local_audio_track.h" |
10 | 10 |
11 using media::AudioBus; | 11 using media::AudioBus; |
12 using media::AudioFifo; | 12 using media::AudioFifo; |
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 static const int kMaxNumberOfBuffersInFifo = 5; | 18 static const int kMaxNumberOfBuffersInFifo = 5; |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 WebAudioCapturerSource::WebAudioCapturerSource() | 22 WebAudioCapturerSource::WebAudioCapturerSource( |
| 23 const blink::WebMediaStreamSource& blink_source) |
23 : track_(NULL), | 24 : track_(NULL), |
24 audio_format_changed_(false) { | 25 audio_format_changed_(false), |
| 26 blink_source_(blink_source) { |
25 } | 27 } |
26 | 28 |
27 WebAudioCapturerSource::~WebAudioCapturerSource() { | 29 WebAudioCapturerSource::~WebAudioCapturerSource() { |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); |
| 31 removeFromBlinkSource(); |
28 } | 32 } |
29 | 33 |
30 void WebAudioCapturerSource::setFormat( | 34 void WebAudioCapturerSource::setFormat( |
31 size_t number_of_channels, float sample_rate) { | 35 size_t number_of_channels, float sample_rate) { |
32 DCHECK(thread_checker_.CalledOnValidThread()); | 36 DCHECK(thread_checker_.CalledOnValidThread()); |
33 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" | 37 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" |
34 << sample_rate << ")"; | 38 << sample_rate << ")"; |
35 if (number_of_channels > 2) { | 39 if (number_of_channels > 2) { |
36 // TODO(xians): Handle more than just the mono and stereo cases. | 40 // TODO(xians): Handle more than just the mono and stereo cases. |
37 LOG(WARNING) << "WebAudioCapturerSource::setFormat() : unhandled format."; | 41 LOG(WARNING) << "WebAudioCapturerSource::setFormat() : unhandled format."; |
(...skipping 23 matching lines...) Expand all Loading... |
61 DCHECK(thread_checker_.CalledOnValidThread()); | 65 DCHECK(thread_checker_.CalledOnValidThread()); |
62 DCHECK(track); | 66 DCHECK(track); |
63 base::AutoLock auto_lock(lock_); | 67 base::AutoLock auto_lock(lock_); |
64 track_ = track; | 68 track_ = track; |
65 } | 69 } |
66 | 70 |
67 void WebAudioCapturerSource::Stop() { | 71 void WebAudioCapturerSource::Stop() { |
68 DCHECK(thread_checker_.CalledOnValidThread()); | 72 DCHECK(thread_checker_.CalledOnValidThread()); |
69 base::AutoLock auto_lock(lock_); | 73 base::AutoLock auto_lock(lock_); |
70 track_ = NULL; | 74 track_ = NULL; |
| 75 removeFromBlinkSource(); |
71 } | 76 } |
72 | 77 |
73 void WebAudioCapturerSource::consumeAudio( | 78 void WebAudioCapturerSource::consumeAudio( |
74 const blink::WebVector<const float*>& audio_data, | 79 const blink::WebVector<const float*>& audio_data, |
75 size_t number_of_frames) { | 80 size_t number_of_frames) { |
76 base::AutoLock auto_lock(lock_); | 81 base::AutoLock auto_lock(lock_); |
77 if (!track_) | 82 if (!track_) |
78 return; | 83 return; |
79 | 84 |
80 // Update the downstream client if the audio format has been changed. | 85 // Update the downstream client if the audio format has been changed. |
(...skipping 28 matching lines...) Expand all Loading... |
109 fifo_->Consume(capture_bus_.get(), 0, capture_bus_->frames()); | 114 fifo_->Consume(capture_bus_.get(), 0, capture_bus_->frames()); |
110 track_->Capture(*capture_bus_, estimated_capture_time, false); | 115 track_->Capture(*capture_bus_, estimated_capture_time, false); |
111 | 116 |
112 // Advance the estimated capture time for the next FIFO consume operation. | 117 // Advance the estimated capture time for the next FIFO consume operation. |
113 estimated_capture_time += | 118 estimated_capture_time += |
114 capture_bus_->frames() * base::TimeDelta::FromSeconds(1) / | 119 capture_bus_->frames() * base::TimeDelta::FromSeconds(1) / |
115 params_.sample_rate(); | 120 params_.sample_rate(); |
116 } | 121 } |
117 } | 122 } |
118 | 123 |
| 124 // If registered as audio consumer in |blink_source_|, deregister from |
| 125 // |blink_source_| and stop keeping a reference to |blink_source_|. |
| 126 // Failure to call this method when stopping the track might leave an invalid |
| 127 // WebAudioCapturerSource reference still registered as an audio consumer on |
| 128 // the blink side. |
| 129 void WebAudioCapturerSource::removeFromBlinkSource() { |
| 130 if (!blink_source_.isNull()) { |
| 131 blink_source_.removeAudioConsumer(this); |
| 132 blink_source_.reset(); |
| 133 } |
| 134 } |
| 135 |
119 } // namespace content | 136 } // namespace content |
OLD | NEW |