Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 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/webrtc_local_audio_track.h" |
| 6 | |
| 7 | #include "content/renderer/media/webrtc_audio_capturer.h" |
| 8 | #include "content/renderer/media/webrtc_audio_capturer_sink_owner.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 9 | #include "third_party/libjingle/source/talk/media/base/audiorenderer.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 10 | |
| 11 | namespace content { |
| 12 | |
| 13 | static const char kAudioTrackKind[] = "audio"; |
| 14 | |
| 15 | scoped_refptr<WebRtcLocalAudioTrack> WebRtcLocalAudioTrack::Create( |
| 16 | const std::string& id, |
| 17 | const scoped_refptr<WebRtcAudioCapturer>& capturer, |
| 18 | webrtc::AudioSourceInterface* track_source) { |
| 19 | talk_base::RefCountedObject<WebRtcLocalAudioTrack>* track = |
| 20 | new talk_base::RefCountedObject<WebRtcLocalAudioTrack>( |
| 21 | id, capturer, track_source); |
| 22 | return track; |
| 23 | } |
| 24 | |
| 25 | WebRtcLocalAudioTrack::WebRtcLocalAudioTrack( |
| 26 | const std::string& label, |
| 27 | const scoped_refptr<WebRtcAudioCapturer>& capturer, |
| 28 | webrtc::AudioSourceInterface* track_source) |
| 29 | : webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>(label), |
| 30 | capturer_(capturer), |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 31 | track_source_(track_source), |
| 32 | need_audio_processing_(!capturer->device_id().empty()) { |
| 33 | // The capturer with a valid device id is using microphone as source, |
| 34 | // and APM (AudioProcessingModule) is turned on only for microphone data. |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 35 | DCHECK(capturer.get()); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 36 | DVLOG(1) << "WebRtcLocalAudioTrack::WebRtcLocalAudioTrack()"; |
| 37 | } |
| 38 | |
| 39 | WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack() { |
| 40 | DCHECK(thread_checker_.CalledOnValidThread()); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 41 | DVLOG(1) << "WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack()"; |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 42 | // Users might not call Stop() on the track. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 43 | Stop(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 46 | void WebRtcLocalAudioTrack::CaptureData(const int16* audio_data, |
| 47 | int number_of_channels, |
| 48 | int number_of_frames, |
| 49 | int audio_delay_milliseconds, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 50 | int volume) { |
| 51 | scoped_refptr<WebRtcAudioCapturer> capturer; |
| 52 | std::vector<int> voe_channels; |
| 53 | int sample_rate = 0; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 54 | SinkList sinks; |
| 55 | { |
| 56 | base::AutoLock auto_lock(lock_); |
| 57 | // When the track is diabled, we simply return here. |
| 58 | // TODO(xians): Figure out if we should feed zero to sinks instead, in |
| 59 | // order to inject VAD data in such case. |
| 60 | if (!enabled()) |
| 61 | return; |
| 62 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 63 | capturer = capturer_; |
| 64 | voe_channels = voe_channels_; |
| 65 | sample_rate = params_.sample_rate(), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 66 | sinks = sinks_; |
| 67 | } |
| 68 | |
| 69 | // Feed the data to the sinks. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 70 | for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) { |
| 71 | int new_volume = (*it)->CaptureData(voe_channels, audio_data, sample_rate, |
| 72 | number_of_channels, number_of_frames, |
| 73 | audio_delay_milliseconds, volume, |
| 74 | need_audio_processing_); |
| 75 | if (new_volume != 0 && capturer.get()) |
| 76 | capturer->SetVolume(new_volume); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | void WebRtcLocalAudioTrack::SetCaptureFormat( |
| 81 | const media::AudioParameters& params) { |
| 82 | base::AutoLock auto_lock(lock_); |
| 83 | params_ = params; |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 84 | |
| 85 | // Update all the existing sinks with the new format. |
| 86 | for (SinkList::const_iterator it = sinks_.begin(); |
| 87 | it != sinks_.end(); ++it) |
| 88 | (*it)->SetCaptureFormat(params); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 91 | void WebRtcLocalAudioTrack::AddChannel(int channel_id) { |
| 92 | DVLOG(1) << "WebRtcLocalAudioTrack::AddChannel(channel_id=" |
| 93 | << channel_id << ")"; |
| 94 | base::AutoLock auto_lock(lock_); |
| 95 | if (std::find(voe_channels_.begin(), voe_channels_.end(), channel_id) != |
| 96 | voe_channels_.end()) { |
| 97 | // We need to handle the case when the same channel is connected to the |
| 98 | // track more than once. |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | voe_channels_.push_back(channel_id); |
| 103 | } |
| 104 | |
| 105 | void WebRtcLocalAudioTrack::RemoveChannel(int channel_id) { |
| 106 | DVLOG(1) << "WebRtcLocalAudioTrack::RemoveChannel(channel_id=" |
| 107 | << channel_id << ")"; |
| 108 | base::AutoLock auto_lock(lock_); |
| 109 | std::vector<int>::iterator iter = |
| 110 | std::find(voe_channels_.begin(), voe_channels_.end(), channel_id); |
| 111 | DCHECK(iter != voe_channels_.end()); |
| 112 | voe_channels_.erase(iter); |
| 113 | } |
| 114 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 115 | // webrtc::AudioTrackInterface implementation. |
| 116 | webrtc::AudioSourceInterface* WebRtcLocalAudioTrack::GetSource() const { |
| 117 | return track_source_; |
| 118 | } |
| 119 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 120 | cricket::AudioRenderer* WebRtcLocalAudioTrack::GetRenderer() { |
| 121 | return this; |
| 122 | } |
| 123 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 124 | std::string WebRtcLocalAudioTrack::kind() const { |
| 125 | return kAudioTrackKind; |
| 126 | } |
| 127 | |
| 128 | void WebRtcLocalAudioTrack::AddSink(WebRtcAudioCapturerSink* sink) { |
| 129 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 130 | DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()"; |
| 131 | base::AutoLock auto_lock(lock_); |
| 132 | sink->SetCaptureFormat(params_); |
| 133 | |
| 134 | // Verify that |sink| is not already added to the list. |
| 135 | DCHECK(std::find_if( |
| 136 | sinks_.begin(), sinks_.end(), |
| 137 | WebRtcAudioCapturerSinkOwner::WrapsSink(sink)) == sinks_.end()); |
| 138 | |
| 139 | // Create (and add to the list) a new WebRtcAudioCapturerSinkOwner which owns |
| 140 | // the |sink| and delagates all calls to the WebRtcAudioCapturerSink |
| 141 | // interface. |
| 142 | sinks_.push_back(new WebRtcAudioCapturerSinkOwner(sink)); |
| 143 | } |
| 144 | |
| 145 | void WebRtcLocalAudioTrack::RemoveSink( |
| 146 | WebRtcAudioCapturerSink* sink) { |
| 147 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 148 | DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()"; |
| 149 | |
| 150 | base::AutoLock auto_lock(lock_); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 151 | // Get iterator to the first element for which WrapsSink(sink) returns true. |
| 152 | SinkList::iterator it = std::find_if( |
| 153 | sinks_.begin(), sinks_.end(), |
| 154 | WebRtcAudioCapturerSinkOwner::WrapsSink(sink)); |
| 155 | if (it != sinks_.end()) { |
| 156 | // Clear the delegate to ensure that no more capture callbacks will |
| 157 | // be sent to this sink. Also avoids a possible crash which can happen |
| 158 | // if this method is called while capturing is active. |
| 159 | (*it)->Reset(); |
| 160 | sinks_.erase(it); |
| 161 | } |
| 162 | } |
| 163 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 164 | void WebRtcLocalAudioTrack::Start() { |
| 165 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 166 | DVLOG(1) << "WebRtcLocalAudioTrack::Start()"; |
| 167 | if (capturer_.get()) |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 168 | capturer_->AddTrack(this); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void WebRtcLocalAudioTrack::Stop() { |
| 172 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 173 | DVLOG(1) << "WebRtcLocalAudioTrack::Stop()"; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 174 | if (!capturer_.get()) |
| 175 | return; |
| 176 | |
| 177 | capturer_->RemoveTrack(this); |
| 178 | |
| 179 | // Protect the pointers using the lock when accessing |sinks_| and |
| 180 | // setting the |capturer_| to NULL. |
| 181 | SinkList sinks; |
| 182 | { |
| 183 | base::AutoLock auto_lock(lock_); |
| 184 | sinks = sinks_; |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 185 | capturer_ = NULL; |
| 186 | } |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 187 | |
| 188 | for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) |
| 189 | (*it)->Reset(); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 192 | } // namespace content |