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 kMaxNumberOfBuffer = 5; |
tommi (sloooow) - chröme
2013/09/06 11:20:30
kMaxNumberOfBuffers (plural).
It would also be goo
no longer working on chromium
2013/09/10 12:43:15
Done.
| |
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) { |
33 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" | 32 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" |
34 << sample_rate << ")"; | 33 << sample_rate << ")"; |
35 if (number_of_channels <= 2) { | 34 if (number_of_channels > 2) { |
36 set_format_channels_ = number_of_channels; | 35 // 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."; | 36 LOG(WARNING) << "WebAudioCapturerSource::setFormat() : unhandled format."; |
37 return; | |
43 } | 38 } |
39 | |
40 ChannelLayout channel_layout = | |
41 number_of_channels == 1 ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; | |
42 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
tommi (sloooow) - chröme
2013/09/06 11:20:30
does params_ not need to be behind the lock? (if
no longer working on chromium
2013/09/10 12:43:15
Put it behind a lock as well.
| |
43 channel_layout, number_of_channels, 0, sample_rate, 16, | |
44 sample_rate / 100); | |
tommi (sloooow) - chröme
2013/09/06 11:20:30
Can you document here that we always use a 10ms bu
no longer working on chromium
2013/09/10 12:43:15
Done.
| |
45 | |
46 base::AutoLock auto_lock(lock_); | |
47 // Update the downstream client to use the same format as what WebKit | |
48 // is using. | |
49 if (track_) | |
50 track_->SetCaptureFormat(params_); | |
51 | |
52 wrapper_bus_ = AudioBus::CreateWrapper(params_.channels()); | |
53 capture_bus_ = AudioBus::Create(params_); | |
54 fifo_.reset(new AudioFifo(params_.channels(), | |
55 kMaxNumberOfBuffer * params_.frames_per_buffer())); | |
44 } | 56 } |
45 | 57 |
46 void WebAudioCapturerSource::Initialize( | 58 void WebAudioCapturerSource::Start( |
47 const media::AudioParameters& params, | 59 WebRtcLocalAudioTrack* track, |
48 media::AudioCapturerSource::CaptureCallback* callback, | 60 WebRtcLocalAudioSourceProvider* source_provider) { |
49 int session_id) { | 61 DCHECK(track); |
62 DCHECK(source_provider); | |
50 // The downstream client should be configured the same as what WebKit | 63 // The downstream client should be configured the same as what WebKit |
51 // is feeding it. | 64 // is feeding it. |
52 DCHECK_EQ(set_format_channels_, params.channels()); | 65 track->SetCaptureFormat(params_); |
53 | 66 |
54 base::AutoLock auto_lock(lock_); | 67 base::AutoLock auto_lock(lock_); |
55 params_ = params; | 68 track_ = track; |
56 callback_ = callback; | 69 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 } | 70 } |
65 | 71 |
66 void WebAudioCapturerSource::Stop() { | 72 void WebAudioCapturerSource::Stop() { |
67 started_ = false; | 73 base::AutoLock auto_lock(lock_); |
74 track_ = NULL; | |
75 source_provider_ = NULL; | |
68 } | 76 } |
69 | 77 |
70 void WebAudioCapturerSource::consumeAudio( | 78 void WebAudioCapturerSource::consumeAudio( |
71 const WebKit::WebVector<const float*>& audio_data, | 79 const WebKit::WebVector<const float*>& audio_data, |
72 size_t number_of_frames) { | 80 size_t number_of_frames) { |
tommi (sloooow) - chröme
2013/09/06 11:20:30
On which thread are we here? If this is not the s
no longer working on chromium
2013/09/10 12:43:15
Added a thread check for Start/Stop/setFormat;
Thi
| |
73 base::AutoLock auto_lock(lock_); | 81 base::AutoLock auto_lock(lock_); |
74 | 82 if (!track_) |
75 if (!callback_) | |
76 return; | 83 return; |
77 | 84 |
78 wrapper_bus_->set_frames(number_of_frames); | 85 wrapper_bus_->set_frames(number_of_frames); |
79 | 86 |
80 // Make sure WebKit is honoring what it told us up front | 87 // Make sure WebKit is honoring what it told us up front |
81 // about the channels. | 88 // about the channels. |
82 DCHECK_EQ(set_format_channels_, static_cast<int>(audio_data.size())); | 89 DCHECK_EQ(params_.channels(), static_cast<int>(audio_data.size())); |
83 DCHECK_EQ(set_format_channels_, wrapper_bus_->channels()); | |
84 | 90 |
85 for (size_t i = 0; i < audio_data.size(); ++i) | 91 for (size_t i = 0; i < audio_data.size(); ++i) |
86 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i])); | 92 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i])); |
87 | 93 |
88 // Handle mismatch between WebAudio buffer-size and WebRTC. | 94 // Handle mismatch between WebAudio buffer-size and WebRTC. |
89 int available = fifo_->max_frames() - fifo_->frames(); | 95 int available = fifo_->max_frames() - fifo_->frames(); |
90 if (available < static_cast<int>(number_of_frames)) { | 96 if (available < static_cast<int>(number_of_frames)) { |
91 LOG(ERROR) << "WebAudioCapturerSource::Consume() : FIFO overrun."; | 97 NOTREACHED() << "WebAudioCapturerSource::Consume() : FIFO overrun."; |
92 return; | 98 return; |
93 } | 99 } |
94 | 100 |
95 fifo_->Push(wrapper_bus_.get()); | 101 fifo_->Push(wrapper_bus_.get()); |
96 int capture_frames = params_.frames_per_buffer(); | 102 int capture_frames = params_.frames_per_buffer(); |
103 int delay_ms = 0; | |
104 int volume = 0; | |
105 bool key_pressed = false; | |
97 while (fifo_->frames() >= capture_frames) { | 106 while (fifo_->frames() >= capture_frames) { |
107 source_provider_->GetStreamInfo(&delay_ms, &volume, &key_pressed); | |
98 fifo_->Consume(capture_bus_.get(), 0, capture_frames); | 108 fifo_->Consume(capture_bus_.get(), 0, capture_frames); |
99 callback_->Capture(capture_bus_.get(), 0, 1.0, false); | 109 track_->Capture(capture_bus_.get(), delay_ms, volume, key_pressed); |
100 } | 110 } |
101 } | 111 } |
102 | 112 |
103 } // namespace content | 113 } // namespace content |
OLD | NEW |