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 "content/renderer/media/webrtc_audio_capturer.h" | 8 #include "content/renderer/media/webrtc_local_audio_source_provider.h" |
| 9 #include "content/renderer/media/webrtc_local_audio_track.h" |
9 | 10 |
10 using media::AudioBus; | 11 using media::AudioBus; |
11 using media::AudioFifo; | 12 using media::AudioFifo; |
12 using media::AudioParameters; | 13 using media::AudioParameters; |
13 using media::ChannelLayout; | 14 using media::ChannelLayout; |
14 using media::CHANNEL_LAYOUT_MONO; | 15 using media::CHANNEL_LAYOUT_MONO; |
15 using media::CHANNEL_LAYOUT_STEREO; | 16 using media::CHANNEL_LAYOUT_STEREO; |
16 | 17 |
17 static const int kFifoSize = 2048; | 18 static const int kMaxNumberOfBuffersInFifo = 5; |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 | 21 |
21 WebAudioCapturerSource::WebAudioCapturerSource(WebRtcAudioCapturer* capturer) | 22 WebAudioCapturerSource::WebAudioCapturerSource() |
22 : capturer_(capturer), | 23 : track_(NULL), |
23 set_format_channels_(0), | 24 source_provider_(NULL) { |
24 callback_(0), | |
25 started_(false) { | |
26 } | 25 } |
27 | 26 |
28 WebAudioCapturerSource::~WebAudioCapturerSource() { | 27 WebAudioCapturerSource::~WebAudioCapturerSource() { |
29 } | 28 } |
30 | 29 |
31 void WebAudioCapturerSource::setFormat( | 30 void WebAudioCapturerSource::setFormat( |
32 size_t number_of_channels, float sample_rate) { | 31 size_t number_of_channels, float sample_rate) { |
| 32 DCHECK(thread_checker_.CalledOnValidThread()); |
33 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" | 33 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" |
34 << sample_rate << ")"; | 34 << sample_rate << ")"; |
35 if (number_of_channels <= 2) { | 35 if (number_of_channels > 2) { |
36 set_format_channels_ = number_of_channels; | 36 // TODO(xians): Handle more than just the mono and stereo cases. |
37 ChannelLayout channel_layout = | |
38 number_of_channels == 1 ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; | |
39 capturer_->SetCapturerSource(this, channel_layout, sample_rate); | |
40 } else { | |
41 // TODO(crogers): Handle more than just the mono and stereo cases. | |
42 LOG(WARNING) << "WebAudioCapturerSource::setFormat() : unhandled format."; | 37 LOG(WARNING) << "WebAudioCapturerSource::setFormat() : unhandled format."; |
| 38 return; |
43 } | 39 } |
| 40 |
| 41 ChannelLayout channel_layout = |
| 42 number_of_channels == 1 ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; |
| 43 |
| 44 base::AutoLock auto_lock(lock_); |
| 45 // Set the format used by this WebAudioCapturerSource. We are using 10ms data |
| 46 // as buffer size since that is the native buffer size of WebRtc packet |
| 47 // running on. |
| 48 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 49 channel_layout, number_of_channels, 0, sample_rate, 16, |
| 50 sample_rate / 100); |
| 51 |
| 52 // Update the downstream client to use the same format as what WebKit |
| 53 // is using. |
| 54 if (track_) |
| 55 track_->SetCaptureFormat(params_); |
| 56 |
| 57 wrapper_bus_ = AudioBus::CreateWrapper(params_.channels()); |
| 58 capture_bus_ = AudioBus::Create(params_); |
| 59 fifo_.reset(new AudioFifo( |
| 60 params_.channels(), |
| 61 kMaxNumberOfBuffersInFifo * params_.frames_per_buffer())); |
44 } | 62 } |
45 | 63 |
46 void WebAudioCapturerSource::Initialize( | 64 void WebAudioCapturerSource::Start( |
47 const media::AudioParameters& params, | 65 WebRtcLocalAudioTrack* track, |
48 media::AudioCapturerSource::CaptureCallback* callback, | 66 WebRtcLocalAudioSourceProvider* source_provider) { |
49 int session_id) { | 67 DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 DCHECK(track); |
| 69 DCHECK(source_provider); |
50 // The downstream client should be configured the same as what WebKit | 70 // The downstream client should be configured the same as what WebKit |
51 // is feeding it. | 71 // is feeding it. |
52 DCHECK_EQ(set_format_channels_, params.channels()); | 72 track->SetCaptureFormat(params_); |
53 | 73 |
54 base::AutoLock auto_lock(lock_); | 74 base::AutoLock auto_lock(lock_); |
55 params_ = params; | 75 track_ = track; |
56 callback_ = callback; | 76 source_provider_ = source_provider; |
57 wrapper_bus_ = AudioBus::CreateWrapper(params.channels()); | |
58 capture_bus_ = AudioBus::Create(params); | |
59 fifo_.reset(new AudioFifo(params.channels(), kFifoSize)); | |
60 } | |
61 | |
62 void WebAudioCapturerSource::Start() { | |
63 started_ = true; | |
64 } | 77 } |
65 | 78 |
66 void WebAudioCapturerSource::Stop() { | 79 void WebAudioCapturerSource::Stop() { |
67 started_ = false; | 80 DCHECK(thread_checker_.CalledOnValidThread()); |
| 81 base::AutoLock auto_lock(lock_); |
| 82 track_ = NULL; |
| 83 source_provider_ = NULL; |
68 } | 84 } |
69 | 85 |
70 void WebAudioCapturerSource::consumeAudio( | 86 void WebAudioCapturerSource::consumeAudio( |
71 const WebKit::WebVector<const float*>& audio_data, | 87 const WebKit::WebVector<const float*>& audio_data, |
72 size_t number_of_frames) { | 88 size_t number_of_frames) { |
73 base::AutoLock auto_lock(lock_); | 89 base::AutoLock auto_lock(lock_); |
74 | 90 if (!track_) |
75 if (!callback_) | |
76 return; | 91 return; |
77 | 92 |
78 wrapper_bus_->set_frames(number_of_frames); | 93 wrapper_bus_->set_frames(number_of_frames); |
79 | 94 |
80 // Make sure WebKit is honoring what it told us up front | 95 // Make sure WebKit is honoring what it told us up front |
81 // about the channels. | 96 // about the channels. |
82 DCHECK_EQ(set_format_channels_, static_cast<int>(audio_data.size())); | 97 DCHECK_EQ(params_.channels(), static_cast<int>(audio_data.size())); |
83 DCHECK_EQ(set_format_channels_, wrapper_bus_->channels()); | |
84 | 98 |
85 for (size_t i = 0; i < audio_data.size(); ++i) | 99 for (size_t i = 0; i < audio_data.size(); ++i) |
86 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i])); | 100 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i])); |
87 | 101 |
88 // Handle mismatch between WebAudio buffer-size and WebRTC. | 102 // Handle mismatch between WebAudio buffer-size and WebRTC. |
89 int available = fifo_->max_frames() - fifo_->frames(); | 103 int available = fifo_->max_frames() - fifo_->frames(); |
90 if (available < static_cast<int>(number_of_frames)) { | 104 if (available < static_cast<int>(number_of_frames)) { |
91 LOG(ERROR) << "WebAudioCapturerSource::Consume() : FIFO overrun."; | 105 NOTREACHED() << "WebAudioCapturerSource::Consume() : FIFO overrun."; |
92 return; | 106 return; |
93 } | 107 } |
94 | 108 |
95 fifo_->Push(wrapper_bus_.get()); | 109 fifo_->Push(wrapper_bus_.get()); |
96 int capture_frames = params_.frames_per_buffer(); | 110 int capture_frames = params_.frames_per_buffer(); |
| 111 int delay_ms = 0; |
| 112 int volume = 0; |
| 113 bool key_pressed = false; |
97 while (fifo_->frames() >= capture_frames) { | 114 while (fifo_->frames() >= capture_frames) { |
| 115 source_provider_->GetAudioProcessingParams( |
| 116 &delay_ms, &volume, &key_pressed); |
98 fifo_->Consume(capture_bus_.get(), 0, capture_frames); | 117 fifo_->Consume(capture_bus_.get(), 0, capture_frames); |
99 callback_->Capture(capture_bus_.get(), 0, 1.0, false); | 118 track_->Capture(capture_bus_.get(), delay_ms, volume, key_pressed); |
100 } | 119 } |
101 } | 120 } |
102 | 121 |
103 } // namespace content | 122 } // namespace content |
OLD | NEW |