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