| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/webrtc/webrtc_local_audio_track_adapter.h" | 5 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "content/renderer/media/media_stream_audio_processor.h" | 9 #include "content/renderer/media/media_stream_audio_processor.h" |
| 10 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h" | 10 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h" |
| 11 #include "content/renderer/media/webrtc/webrtc_audio_sink_adapter.h" | 11 #include "content/renderer/media/webrtc/webrtc_audio_sink_adapter.h" |
| 12 #include "content/renderer/media/webrtc_local_audio_track.h" | 12 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 13 #include "content/renderer/render_thread_impl.h" | 13 #include "content/renderer/render_thread_impl.h" |
| 14 #include "third_party/webrtc/api/mediastreaminterface.h" | 14 #include "third_party/webrtc/api/mediastreaminterface.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 static const char kAudioTrackKind[] = "audio"; | 18 static const char kAudioTrackKind[] = "audio"; |
| 19 | 19 |
| 20 scoped_refptr<WebRtcLocalAudioTrackAdapter> | 20 scoped_refptr<WebRtcLocalAudioTrackAdapter> |
| 21 WebRtcLocalAudioTrackAdapter::Create( | 21 WebRtcLocalAudioTrackAdapter::Create( |
| 22 const std::string& label, | 22 const std::string& label, |
| 23 webrtc::AudioSourceInterface* track_source) { | 23 webrtc::AudioSourceInterface* track_source) { |
| 24 // TODO(tommi): Change this so that the signaling thread is one of the | 24 // TODO(tommi): Change this so that the signaling thread is one of the |
| 25 // parameters to this method. | 25 // parameters to this method. |
| 26 scoped_refptr<base::SingleThreadTaskRunner> signaling_thread; | 26 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner; |
| 27 RenderThreadImpl* current = RenderThreadImpl::current(); | 27 RenderThreadImpl* current = RenderThreadImpl::current(); |
| 28 if (current) { | 28 if (current) { |
| 29 PeerConnectionDependencyFactory* pc_factory = | 29 PeerConnectionDependencyFactory* pc_factory = |
| 30 current->GetPeerConnectionDependencyFactory(); | 30 current->GetPeerConnectionDependencyFactory(); |
| 31 signaling_thread = pc_factory->GetWebRtcSignalingThread(); | 31 signaling_task_runner = pc_factory->GetWebRtcSignalingThread(); |
| 32 CHECK(signaling_task_runner); |
| 33 } else { |
| 34 LOG(WARNING) << "Assuming single-threaded operation for unit test."; |
| 32 } | 35 } |
| 33 | 36 |
| 34 LOG_IF(ERROR, !signaling_thread.get()) << "No signaling thread!"; | |
| 35 | |
| 36 rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>* adapter = | 37 rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>* adapter = |
| 37 new rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>( | 38 new rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>( |
| 38 label, track_source, signaling_thread); | 39 label, track_source, signaling_task_runner); |
| 39 return adapter; | 40 return adapter; |
| 40 } | 41 } |
| 41 | 42 |
| 42 WebRtcLocalAudioTrackAdapter::WebRtcLocalAudioTrackAdapter( | 43 WebRtcLocalAudioTrackAdapter::WebRtcLocalAudioTrackAdapter( |
| 43 const std::string& label, | 44 const std::string& label, |
| 44 webrtc::AudioSourceInterface* track_source, | 45 webrtc::AudioSourceInterface* track_source, |
| 45 const scoped_refptr<base::SingleThreadTaskRunner>& signaling_thread) | 46 const scoped_refptr<base::SingleThreadTaskRunner>& signaling_task_runner) |
| 46 : webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>(label), | 47 : webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>(label), |
| 47 owner_(NULL), | 48 owner_(NULL), |
| 48 track_source_(track_source), | 49 track_source_(track_source), |
| 49 signaling_thread_(signaling_thread), | 50 signaling_task_runner_(signaling_task_runner) {} |
| 50 signal_level_(0) { | |
| 51 signaling_thread_checker_.DetachFromThread(); | |
| 52 capture_thread_.DetachFromThread(); | |
| 53 } | |
| 54 | 51 |
| 55 WebRtcLocalAudioTrackAdapter::~WebRtcLocalAudioTrackAdapter() { | 52 WebRtcLocalAudioTrackAdapter::~WebRtcLocalAudioTrackAdapter() { |
| 56 } | 53 } |
| 57 | 54 |
| 58 void WebRtcLocalAudioTrackAdapter::Initialize(WebRtcLocalAudioTrack* owner) { | 55 void WebRtcLocalAudioTrackAdapter::Initialize(WebRtcLocalAudioTrack* owner) { |
| 59 DCHECK(!owner_); | 56 DCHECK(!owner_); |
| 60 DCHECK(owner); | 57 DCHECK(owner); |
| 61 owner_ = owner; | 58 owner_ = owner; |
| 62 } | 59 } |
| 63 | 60 |
| 64 void WebRtcLocalAudioTrackAdapter::SetAudioProcessor( | 61 void WebRtcLocalAudioTrackAdapter::SetAudioProcessor( |
| 65 const scoped_refptr<MediaStreamAudioProcessor>& processor) { | 62 const scoped_refptr<MediaStreamAudioProcessor>& processor) { |
| 66 // SetAudioProcessor will be called when a new capture thread has been | 63 // |audio_processor_| must only be set once. This eliminates the need to |
| 67 // initialized, so we need to detach from any current capture thread we're | 64 // protect |audio_processor_| with a mutex. |
| 68 // checking and attach to the current one. | 65 DCHECK(processor.get()); |
| 69 capture_thread_.DetachFromThread(); | 66 DCHECK(!audio_processor_); |
| 70 DCHECK(capture_thread_.CalledOnValidThread()); | |
| 71 base::AutoLock auto_lock(lock_); | |
| 72 audio_processor_ = processor; | 67 audio_processor_ = processor; |
| 73 } | 68 } |
| 74 | 69 |
| 70 void WebRtcLocalAudioTrackAdapter::SetLevel( |
| 71 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level) { |
| 72 // |level_| must only be set once. This eliminates the need to protect |
| 73 // |level_| with a mutex. |
| 74 DCHECK(level.get()); |
| 75 DCHECK(!level_); |
| 76 level_ = level; |
| 77 } |
| 78 |
| 75 std::string WebRtcLocalAudioTrackAdapter::kind() const { | 79 std::string WebRtcLocalAudioTrackAdapter::kind() const { |
| 76 return kAudioTrackKind; | 80 return kAudioTrackKind; |
| 77 } | 81 } |
| 78 | 82 |
| 79 bool WebRtcLocalAudioTrackAdapter::set_enabled(bool enable) { | 83 bool WebRtcLocalAudioTrackAdapter::set_enabled(bool enable) { |
| 80 // If we're not called on the signaling thread, we need to post a task to | 84 // If we're not called on the signaling thread, we need to post a task to |
| 81 // change the state on the correct thread. | 85 // change the state on the correct thread. |
| 82 if (signaling_thread_.get() && !signaling_thread_->BelongsToCurrentThread()) { | 86 if (signaling_task_runner_ && |
| 83 signaling_thread_->PostTask(FROM_HERE, | 87 !signaling_task_runner_->BelongsToCurrentThread()) { |
| 88 signaling_task_runner_->PostTask(FROM_HERE, |
| 84 base::Bind( | 89 base::Bind( |
| 85 base::IgnoreResult(&WebRtcLocalAudioTrackAdapter::set_enabled), | 90 base::IgnoreResult(&WebRtcLocalAudioTrackAdapter::set_enabled), |
| 86 this, enable)); | 91 this, enable)); |
| 87 return true; | 92 return true; |
| 88 } | 93 } |
| 89 | 94 |
| 90 return webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>:: | 95 return webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>:: |
| 91 set_enabled(enable); | 96 set_enabled(enable); |
| 92 } | 97 } |
| 93 | 98 |
| 94 void WebRtcLocalAudioTrackAdapter::AddSink( | 99 void WebRtcLocalAudioTrackAdapter::AddSink( |
| 95 webrtc::AudioTrackSinkInterface* sink) { | 100 webrtc::AudioTrackSinkInterface* sink) { |
| 96 DCHECK(signaling_thread_checker_.CalledOnValidThread()); | 101 DCHECK(!signaling_task_runner_ || |
| 102 signaling_task_runner_->RunsTasksOnCurrentThread()); |
| 97 DCHECK(sink); | 103 DCHECK(sink); |
| 98 #ifndef NDEBUG | 104 #ifndef NDEBUG |
| 99 // Verify that |sink| has not been added. | 105 // Verify that |sink| has not been added. |
| 100 for (ScopedVector<WebRtcAudioSinkAdapter>::const_iterator it = | 106 for (ScopedVector<WebRtcAudioSinkAdapter>::const_iterator it = |
| 101 sink_adapters_.begin(); | 107 sink_adapters_.begin(); |
| 102 it != sink_adapters_.end(); ++it) { | 108 it != sink_adapters_.end(); ++it) { |
| 103 DCHECK(!(*it)->IsEqual(sink)); | 109 DCHECK(!(*it)->IsEqual(sink)); |
| 104 } | 110 } |
| 105 #endif | 111 #endif |
| 106 | 112 |
| 107 scoped_ptr<WebRtcAudioSinkAdapter> adapter( | 113 scoped_ptr<WebRtcAudioSinkAdapter> adapter( |
| 108 new WebRtcAudioSinkAdapter(sink)); | 114 new WebRtcAudioSinkAdapter(sink)); |
| 109 owner_->AddSink(adapter.get()); | 115 owner_->AddSink(adapter.get()); |
| 110 sink_adapters_.push_back(adapter.release()); | 116 sink_adapters_.push_back(adapter.release()); |
| 111 } | 117 } |
| 112 | 118 |
| 113 void WebRtcLocalAudioTrackAdapter::RemoveSink( | 119 void WebRtcLocalAudioTrackAdapter::RemoveSink( |
| 114 webrtc::AudioTrackSinkInterface* sink) { | 120 webrtc::AudioTrackSinkInterface* sink) { |
| 115 DCHECK(signaling_thread_checker_.CalledOnValidThread()); | 121 DCHECK(!signaling_task_runner_ || |
| 122 signaling_task_runner_->RunsTasksOnCurrentThread()); |
| 116 DCHECK(sink); | 123 DCHECK(sink); |
| 117 for (ScopedVector<WebRtcAudioSinkAdapter>::iterator it = | 124 for (ScopedVector<WebRtcAudioSinkAdapter>::iterator it = |
| 118 sink_adapters_.begin(); | 125 sink_adapters_.begin(); |
| 119 it != sink_adapters_.end(); ++it) { | 126 it != sink_adapters_.end(); ++it) { |
| 120 if ((*it)->IsEqual(sink)) { | 127 if ((*it)->IsEqual(sink)) { |
| 121 owner_->RemoveSink(*it); | 128 owner_->RemoveSink(*it); |
| 122 sink_adapters_.erase(it); | 129 sink_adapters_.erase(it); |
| 123 return; | 130 return; |
| 124 } | 131 } |
| 125 } | 132 } |
| 126 } | 133 } |
| 127 | 134 |
| 128 bool WebRtcLocalAudioTrackAdapter::GetSignalLevel(int* level) { | 135 bool WebRtcLocalAudioTrackAdapter::GetSignalLevel(int* level) { |
| 129 DCHECK(signaling_thread_checker_.CalledOnValidThread()); | 136 DCHECK(!signaling_task_runner_ || |
| 137 signaling_task_runner_->RunsTasksOnCurrentThread()); |
| 130 | 138 |
| 131 base::AutoLock auto_lock(lock_); | 139 // |level_| is only set once, so it's safe to read without first acquiring a |
| 132 *level = signal_level_; | 140 // mutex. |
| 141 if (!level_) |
| 142 return false; |
| 143 const float signal_level = level_->GetCurrent(); |
| 144 DCHECK_GE(signal_level, 0.0f); |
| 145 DCHECK_LE(signal_level, 1.0f); |
| 146 // Convert from float in range [0.0,1.0] to an int in range [0,32767]. |
| 147 *level = static_cast<int>(signal_level * std::numeric_limits<int16_t>::max() + |
| 148 0.5f /* rounding to nearest int */); |
| 133 return true; | 149 return true; |
| 134 } | 150 } |
| 135 | 151 |
| 136 rtc::scoped_refptr<webrtc::AudioProcessorInterface> | 152 rtc::scoped_refptr<webrtc::AudioProcessorInterface> |
| 137 WebRtcLocalAudioTrackAdapter::GetAudioProcessor() { | 153 WebRtcLocalAudioTrackAdapter::GetAudioProcessor() { |
| 138 DCHECK(signaling_thread_checker_.CalledOnValidThread()); | 154 DCHECK(!signaling_task_runner_ || |
| 139 base::AutoLock auto_lock(lock_); | 155 signaling_task_runner_->RunsTasksOnCurrentThread()); |
| 140 return audio_processor_.get(); | 156 return audio_processor_.get(); |
| 141 } | 157 } |
| 142 | 158 |
| 143 void WebRtcLocalAudioTrackAdapter::SetSignalLevel(int signal_level) { | |
| 144 DCHECK(capture_thread_.CalledOnValidThread()); | |
| 145 base::AutoLock auto_lock(lock_); | |
| 146 signal_level_ = signal_level; | |
| 147 } | |
| 148 | |
| 149 webrtc::AudioSourceInterface* WebRtcLocalAudioTrackAdapter::GetSource() const { | 159 webrtc::AudioSourceInterface* WebRtcLocalAudioTrackAdapter::GetSource() const { |
| 150 DCHECK(signaling_thread_checker_.CalledOnValidThread()); | 160 DCHECK(!signaling_task_runner_ || |
| 161 signaling_task_runner_->RunsTasksOnCurrentThread()); |
| 151 return track_source_; | 162 return track_source_; |
| 152 } | 163 } |
| 153 | 164 |
| 154 } // namespace content | 165 } // namespace content |
| OLD | NEW |