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 <limits> |
| 8 |
| 9 #include "base/bind.h" |
7 #include "base/location.h" | 10 #include "base/location.h" |
8 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/synchronization/waitable_event.h" |
9 #include "content/renderer/media/media_stream_audio_processor.h" | 14 #include "content/renderer/media/media_stream_audio_processor.h" |
| 15 #include "content/renderer/media/media_stream_audio_track.h" |
10 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h" | 16 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h" |
11 #include "content/renderer/media/webrtc/webrtc_audio_sink_adapter.h" | 17 #include "content/renderer/media/webrtc/webrtc_audio_sink_adapter.h" |
12 #include "content/renderer/media/webrtc_local_audio_track.h" | |
13 #include "content/renderer/render_thread_impl.h" | 18 #include "content/renderer/render_thread_impl.h" |
14 #include "third_party/webrtc/api/mediastreaminterface.h" | 19 #include "third_party/webrtc/api/mediastreaminterface.h" |
15 | 20 |
16 namespace content { | 21 namespace content { |
17 | 22 |
18 static const char kAudioTrackKind[] = "audio"; | 23 static const char kAudioTrackKind[] = "audio"; |
19 | 24 |
20 scoped_refptr<WebRtcLocalAudioTrackAdapter> | 25 scoped_refptr<WebRtcLocalAudioTrackAdapter> |
21 WebRtcLocalAudioTrackAdapter::Create( | 26 WebRtcLocalAudioTrackAdapter::Create( |
22 const std::string& label, | 27 const std::string& label, |
(...skipping 15 matching lines...) Expand all Loading... |
38 new rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>( | 43 new rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>( |
39 label, track_source, std::move(signaling_task_runner)); | 44 label, track_source, std::move(signaling_task_runner)); |
40 return adapter; | 45 return adapter; |
41 } | 46 } |
42 | 47 |
43 WebRtcLocalAudioTrackAdapter::WebRtcLocalAudioTrackAdapter( | 48 WebRtcLocalAudioTrackAdapter::WebRtcLocalAudioTrackAdapter( |
44 const std::string& label, | 49 const std::string& label, |
45 webrtc::AudioSourceInterface* track_source, | 50 webrtc::AudioSourceInterface* track_source, |
46 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner) | 51 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner) |
47 : webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>(label), | 52 : webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>(label), |
48 owner_(NULL), | |
49 track_source_(track_source), | 53 track_source_(track_source), |
50 signaling_task_runner_(std::move(signaling_task_runner)) {} | 54 signaling_task_runner_(std::move(signaling_task_runner)) {} |
51 | 55 |
52 WebRtcLocalAudioTrackAdapter::~WebRtcLocalAudioTrackAdapter() { | 56 WebRtcLocalAudioTrackAdapter::~WebRtcLocalAudioTrackAdapter() { |
53 } | 57 } |
54 | 58 |
55 void WebRtcLocalAudioTrackAdapter::Initialize(WebRtcLocalAudioTrack* owner) { | 59 void WebRtcLocalAudioTrackAdapter::SetMediaStreamAudioTrack( |
56 DCHECK(!owner_); | 60 base::WeakPtr<MediaStreamAudioTrack> track) { |
57 DCHECK(owner); | 61 // Note: Single-threaded unit tests might not provide a task runner (or even a |
58 owner_ = owner; | 62 // main-thread MessageLoop). |
| 63 if (base::MessageLoop* main_loop = base::MessageLoop::current()) |
| 64 main_task_runner_ = main_loop->task_runner(); |
| 65 track_ = track; |
59 } | 66 } |
60 | 67 |
61 void WebRtcLocalAudioTrackAdapter::SetAudioProcessor( | 68 void WebRtcLocalAudioTrackAdapter::SetAudioProcessor( |
62 scoped_refptr<MediaStreamAudioProcessor> processor) { | 69 scoped_refptr<MediaStreamAudioProcessor> processor) { |
63 DCHECK(processor.get()); | 70 DCHECK(processor.get()); |
64 DCHECK(!audio_processor_); | 71 DCHECK(!audio_processor_); |
65 audio_processor_ = std::move(processor); | 72 audio_processor_ = std::move(processor); |
66 } | 73 } |
67 | 74 |
68 void WebRtcLocalAudioTrackAdapter::SetLevel( | 75 void WebRtcLocalAudioTrackAdapter::SetLevel( |
(...skipping 18 matching lines...) Expand all Loading... |
87 this, enable)); | 94 this, enable)); |
88 return true; | 95 return true; |
89 } | 96 } |
90 | 97 |
91 return webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>:: | 98 return webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>:: |
92 set_enabled(enable); | 99 set_enabled(enable); |
93 } | 100 } |
94 | 101 |
95 void WebRtcLocalAudioTrackAdapter::AddSink( | 102 void WebRtcLocalAudioTrackAdapter::AddSink( |
96 webrtc::AudioTrackSinkInterface* sink) { | 103 webrtc::AudioTrackSinkInterface* sink) { |
97 DCHECK(!signaling_task_runner_ || | |
98 signaling_task_runner_->RunsTasksOnCurrentThread()); | |
99 DCHECK(sink); | 104 DCHECK(sink); |
| 105 |
| 106 // The MediaStream object graph may only be modified on the main thread, so |
| 107 // trampoline if necessary. |
| 108 if (main_task_runner_ && !main_task_runner_->RunsTasksOnCurrentThread()) { |
| 109 main_task_runner_->PostTask( |
| 110 FROM_HERE, |
| 111 base::Bind(&WebRtcLocalAudioTrackAdapter::AddSink, this, sink)); |
| 112 return; |
| 113 } |
| 114 |
100 #ifndef NDEBUG | 115 #ifndef NDEBUG |
101 // Verify that |sink| has not been added. | 116 // Verify that |sink| has not been added. |
102 for (ScopedVector<WebRtcAudioSinkAdapter>::const_iterator it = | 117 for (const auto& adapter : sink_adapters_) |
103 sink_adapters_.begin(); | 118 DCHECK(!adapter->IsEqual(sink)); |
104 it != sink_adapters_.end(); ++it) { | |
105 DCHECK(!(*it)->IsEqual(sink)); | |
106 } | |
107 #endif | 119 #endif |
108 | 120 |
109 scoped_ptr<WebRtcAudioSinkAdapter> adapter( | 121 if (track_) { |
110 new WebRtcAudioSinkAdapter(sink)); | 122 scoped_ptr<WebRtcAudioSinkAdapter> adapter( |
111 owner_->AddSink(adapter.get()); | 123 new WebRtcAudioSinkAdapter(sink)); |
112 sink_adapters_.push_back(adapter.release()); | 124 track_->AddSink(adapter.get()); |
| 125 sink_adapters_.push_back(std::move(adapter)); |
| 126 } |
113 } | 127 } |
114 | 128 |
115 void WebRtcLocalAudioTrackAdapter::RemoveSink( | 129 void WebRtcLocalAudioTrackAdapter::RemoveSink( |
116 webrtc::AudioTrackSinkInterface* sink) { | 130 webrtc::AudioTrackSinkInterface* sink) { |
117 DCHECK(!signaling_task_runner_ || | |
118 signaling_task_runner_->RunsTasksOnCurrentThread()); | |
119 DCHECK(sink); | 131 DCHECK(sink); |
120 for (ScopedVector<WebRtcAudioSinkAdapter>::iterator it = | 132 |
121 sink_adapters_.begin(); | 133 // The MediaStream object graph may only be modified on the main thread, so |
122 it != sink_adapters_.end(); ++it) { | 134 // trampoline if necessary. Furthermore, block the current thread until the |
123 if ((*it)->IsEqual(sink)) { | 135 // task has completed because the interface contract requires the audio flow |
124 owner_->RemoveSink(*it); | 136 // to |sink| be stopped when this method returns. |
125 sink_adapters_.erase(it); | 137 if (main_task_runner_ && !main_task_runner_->RunsTasksOnCurrentThread()) { |
126 return; | 138 base::WaitableEvent done_event(false, false); |
127 } | 139 main_task_runner_->PostTask( |
| 140 FROM_HERE, |
| 141 base::Bind(&WebRtcLocalAudioTrackAdapter::RemoveSinkOnMainThread, this, |
| 142 sink, &done_event)); |
| 143 done_event.Wait(); |
| 144 } else { |
| 145 RemoveSinkOnMainThread(sink, nullptr); |
128 } | 146 } |
129 } | 147 } |
130 | 148 |
| 149 void WebRtcLocalAudioTrackAdapter::RemoveSinkOnMainThread( |
| 150 webrtc::AudioTrackSinkInterface* sink, |
| 151 base::WaitableEvent* done_event) { |
| 152 DCHECK(!main_task_runner_ || main_task_runner_->RunsTasksOnCurrentThread()); |
| 153 for (auto it = sink_adapters_.begin(); it != sink_adapters_.end(); ++it) { |
| 154 if ((*it)->IsEqual(sink)) { |
| 155 if (track_) |
| 156 track_->RemoveSink(it->get()); |
| 157 sink_adapters_.erase(it); |
| 158 break; |
| 159 } |
| 160 } |
| 161 if (done_event) |
| 162 done_event->Signal(); |
| 163 } |
| 164 |
131 bool WebRtcLocalAudioTrackAdapter::GetSignalLevel(int* level) { | 165 bool WebRtcLocalAudioTrackAdapter::GetSignalLevel(int* level) { |
132 DCHECK(!signaling_task_runner_ || | 166 DCHECK(!signaling_task_runner_ || |
133 signaling_task_runner_->RunsTasksOnCurrentThread()); | 167 signaling_task_runner_->RunsTasksOnCurrentThread()); |
134 | 168 |
135 // |level_| is only set once, so it's safe to read without first acquiring a | 169 // |level_| is only set once, so it's safe to read without first acquiring a |
136 // mutex. | 170 // mutex. |
137 if (!level_) | 171 if (!level_) |
138 return false; | 172 return false; |
139 const float signal_level = level_->GetCurrent(); | 173 const float signal_level = level_->GetCurrent(); |
140 DCHECK_GE(signal_level, 0.0f); | 174 DCHECK_GE(signal_level, 0.0f); |
(...skipping 11 matching lines...) Expand all Loading... |
152 return audio_processor_.get(); | 186 return audio_processor_.get(); |
153 } | 187 } |
154 | 188 |
155 webrtc::AudioSourceInterface* WebRtcLocalAudioTrackAdapter::GetSource() const { | 189 webrtc::AudioSourceInterface* WebRtcLocalAudioTrackAdapter::GetSource() const { |
156 DCHECK(!signaling_task_runner_ || | 190 DCHECK(!signaling_task_runner_ || |
157 signaling_task_runner_->RunsTasksOnCurrentThread()); | 191 signaling_task_runner_->RunsTasksOnCurrentThread()); |
158 return track_source_; | 192 return track_source_; |
159 } | 193 } |
160 | 194 |
161 } // namespace content | 195 } // namespace content |
OLD | NEW |