Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: content/renderer/media/webrtc/webrtc_local_audio_track_adapter.cc

Issue 1721273002: MediaStream audio object graph untangling and clean-ups. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unit tests broken by recent MSAudioTrack::Stop() reworking. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 DCHECK(processor.get());
67 // initialized, so we need to detach from any current capture thread we're 64 DCHECK(!audio_processor_);
68 // checking and attach to the current one.
69 capture_thread_.DetachFromThread();
70 DCHECK(capture_thread_.CalledOnValidThread());
71 base::AutoLock auto_lock(lock_);
72 audio_processor_ = processor; 65 audio_processor_ = processor;
73 } 66 }
74 67
68 void WebRtcLocalAudioTrackAdapter::SetLevel(
69 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level) {
70 DCHECK(level.get());
71 DCHECK(!level_);
72 level_ = level;
tommi (sloooow) - chröme 2016/03/03 11:07:31 assuming |level| took ref ownership, you probably
miu 2016/03/05 02:55:31 Done.
73 }
74
75 std::string WebRtcLocalAudioTrackAdapter::kind() const { 75 std::string WebRtcLocalAudioTrackAdapter::kind() const {
76 return kAudioTrackKind; 76 return kAudioTrackKind;
77 } 77 }
78 78
79 bool WebRtcLocalAudioTrackAdapter::set_enabled(bool enable) { 79 bool WebRtcLocalAudioTrackAdapter::set_enabled(bool enable) {
80 // If we're not called on the signaling thread, we need to post a task to 80 // If we're not called on the signaling thread, we need to post a task to
81 // change the state on the correct thread. 81 // change the state on the correct thread.
82 if (signaling_thread_.get() && !signaling_thread_->BelongsToCurrentThread()) { 82 if (signaling_task_runner_ &&
83 signaling_thread_->PostTask(FROM_HERE, 83 !signaling_task_runner_->BelongsToCurrentThread()) {
84 signaling_task_runner_->PostTask(FROM_HERE,
84 base::Bind( 85 base::Bind(
85 base::IgnoreResult(&WebRtcLocalAudioTrackAdapter::set_enabled), 86 base::IgnoreResult(&WebRtcLocalAudioTrackAdapter::set_enabled),
86 this, enable)); 87 this, enable));
87 return true; 88 return true;
88 } 89 }
89 90
90 return webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>:: 91 return webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>::
91 set_enabled(enable); 92 set_enabled(enable);
92 } 93 }
93 94
94 void WebRtcLocalAudioTrackAdapter::AddSink( 95 void WebRtcLocalAudioTrackAdapter::AddSink(
95 webrtc::AudioTrackSinkInterface* sink) { 96 webrtc::AudioTrackSinkInterface* sink) {
96 DCHECK(signaling_thread_checker_.CalledOnValidThread()); 97 DCHECK(!signaling_task_runner_ ||
98 signaling_task_runner_->RunsTasksOnCurrentThread());
97 DCHECK(sink); 99 DCHECK(sink);
98 #ifndef NDEBUG 100 #ifndef NDEBUG
99 // Verify that |sink| has not been added. 101 // Verify that |sink| has not been added.
100 for (ScopedVector<WebRtcAudioSinkAdapter>::const_iterator it = 102 for (ScopedVector<WebRtcAudioSinkAdapter>::const_iterator it =
101 sink_adapters_.begin(); 103 sink_adapters_.begin();
102 it != sink_adapters_.end(); ++it) { 104 it != sink_adapters_.end(); ++it) {
103 DCHECK(!(*it)->IsEqual(sink)); 105 DCHECK(!(*it)->IsEqual(sink));
104 } 106 }
105 #endif 107 #endif
106 108
107 scoped_ptr<WebRtcAudioSinkAdapter> adapter( 109 scoped_ptr<WebRtcAudioSinkAdapter> adapter(
108 new WebRtcAudioSinkAdapter(sink)); 110 new WebRtcAudioSinkAdapter(sink));
109 owner_->AddSink(adapter.get()); 111 owner_->AddSink(adapter.get());
110 sink_adapters_.push_back(adapter.release()); 112 sink_adapters_.push_back(adapter.release());
111 } 113 }
112 114
113 void WebRtcLocalAudioTrackAdapter::RemoveSink( 115 void WebRtcLocalAudioTrackAdapter::RemoveSink(
114 webrtc::AudioTrackSinkInterface* sink) { 116 webrtc::AudioTrackSinkInterface* sink) {
115 DCHECK(signaling_thread_checker_.CalledOnValidThread()); 117 DCHECK(!signaling_task_runner_ ||
118 signaling_task_runner_->RunsTasksOnCurrentThread());
116 DCHECK(sink); 119 DCHECK(sink);
117 for (ScopedVector<WebRtcAudioSinkAdapter>::iterator it = 120 for (ScopedVector<WebRtcAudioSinkAdapter>::iterator it =
118 sink_adapters_.begin(); 121 sink_adapters_.begin();
119 it != sink_adapters_.end(); ++it) { 122 it != sink_adapters_.end(); ++it) {
120 if ((*it)->IsEqual(sink)) { 123 if ((*it)->IsEqual(sink)) {
121 owner_->RemoveSink(*it); 124 owner_->RemoveSink(*it);
122 sink_adapters_.erase(it); 125 sink_adapters_.erase(it);
123 return; 126 return;
124 } 127 }
125 } 128 }
126 } 129 }
127 130
128 bool WebRtcLocalAudioTrackAdapter::GetSignalLevel(int* level) { 131 bool WebRtcLocalAudioTrackAdapter::GetSignalLevel(int* level) {
129 DCHECK(signaling_thread_checker_.CalledOnValidThread()); 132 DCHECK(!signaling_task_runner_ ||
133 signaling_task_runner_->RunsTasksOnCurrentThread());
130 134
131 base::AutoLock auto_lock(lock_); 135 // |level_| is only set once, so it's safe to read without first acquiring a
132 *level = signal_level_; 136 // mutex.
137 if (!level_)
138 return false;
139 const float signal_level = level_->GetCurrent();
140 DCHECK_GE(signal_level, 0.0f);
141 DCHECK_LE(signal_level, 1.0f);
142 // Convert from float in range [0.0,1.0] to an int in range [0,32767].
143 *level = static_cast<int>(signal_level * std::numeric_limits<int16_t>::max() +
144 0.5f /* rounding to nearest int */);
tommi (sloooow) - chröme 2016/03/03 11:07:31 Double checking on the behavior... I see you're m
miu 2016/03/05 02:55:31 Yes, it will round to 1. Here's a test program I
133 return true; 145 return true;
134 } 146 }
135 147
136 rtc::scoped_refptr<webrtc::AudioProcessorInterface> 148 rtc::scoped_refptr<webrtc::AudioProcessorInterface>
137 WebRtcLocalAudioTrackAdapter::GetAudioProcessor() { 149 WebRtcLocalAudioTrackAdapter::GetAudioProcessor() {
138 DCHECK(signaling_thread_checker_.CalledOnValidThread()); 150 DCHECK(!signaling_task_runner_ ||
139 base::AutoLock auto_lock(lock_); 151 signaling_task_runner_->RunsTasksOnCurrentThread());
140 return audio_processor_.get(); 152 return audio_processor_.get();
141 } 153 }
142 154
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 { 155 webrtc::AudioSourceInterface* WebRtcLocalAudioTrackAdapter::GetSource() const {
150 DCHECK(signaling_thread_checker_.CalledOnValidThread()); 156 DCHECK(!signaling_task_runner_ ||
157 signaling_task_runner_->RunsTasksOnCurrentThread());
151 return track_source_; 158 return track_source_;
152 } 159 }
153 160
154 } // namespace content 161 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698