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

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

Issue 1834323002: MediaStream audio: Refactor 3 separate "glue" implementations into one. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: REBASE + Workaround to ensure MediaStreamAudioProcessor is destroyed on the main thread. Created 4 years, 7 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
6
7 #include "base/location.h"
8 #include "base/logging.h"
9 #include "content/renderer/media/media_stream_audio_processor.h"
10 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
11 #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"
14 #include "third_party/webrtc/api/mediastreaminterface.h"
15
16 namespace content {
17
18 static const char kAudioTrackKind[] = "audio";
19
20 scoped_refptr<WebRtcLocalAudioTrackAdapter>
21 WebRtcLocalAudioTrackAdapter::Create(
22 const std::string& label,
23 webrtc::AudioSourceInterface* track_source) {
24 // TODO(tommi): Change this so that the signaling thread is one of the
25 // parameters to this method.
26 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner;
27 RenderThreadImpl* current = RenderThreadImpl::current();
28 if (current) {
29 PeerConnectionDependencyFactory* pc_factory =
30 current->GetPeerConnectionDependencyFactory();
31 signaling_task_runner = pc_factory->GetWebRtcSignalingThread();
32 LOG_IF(ERROR, !signaling_task_runner) << "No signaling thread!";
33 } else {
34 LOG(WARNING) << "Assuming single-threaded operation for unit test.";
35 }
36
37 rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>* adapter =
38 new rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>(
39 label, track_source, std::move(signaling_task_runner));
40 return adapter;
41 }
42
43 WebRtcLocalAudioTrackAdapter::WebRtcLocalAudioTrackAdapter(
44 const std::string& label,
45 webrtc::AudioSourceInterface* track_source,
46 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner)
47 : webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>(label),
48 owner_(NULL),
49 track_source_(track_source),
50 signaling_task_runner_(std::move(signaling_task_runner)) {}
51
52 WebRtcLocalAudioTrackAdapter::~WebRtcLocalAudioTrackAdapter() {
53 }
54
55 void WebRtcLocalAudioTrackAdapter::Initialize(WebRtcLocalAudioTrack* owner) {
56 DCHECK(!owner_);
57 DCHECK(owner);
58 owner_ = owner;
59 }
60
61 void WebRtcLocalAudioTrackAdapter::SetAudioProcessor(
62 scoped_refptr<MediaStreamAudioProcessor> processor) {
63 DCHECK(processor.get());
64 DCHECK(!audio_processor_);
65 audio_processor_ = std::move(processor);
66 }
67
68 void WebRtcLocalAudioTrackAdapter::SetLevel(
69 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level) {
70 DCHECK(level.get());
71 DCHECK(!level_);
72 level_ = std::move(level);
73 }
74
75 std::string WebRtcLocalAudioTrackAdapter::kind() const {
76 return kAudioTrackKind;
77 }
78
79 bool WebRtcLocalAudioTrackAdapter::set_enabled(bool enable) {
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.
82 if (signaling_task_runner_ &&
83 !signaling_task_runner_->BelongsToCurrentThread()) {
84 signaling_task_runner_->PostTask(FROM_HERE,
85 base::Bind(
86 base::IgnoreResult(&WebRtcLocalAudioTrackAdapter::set_enabled),
87 this, enable));
88 return true;
89 }
90
91 return webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>::
92 set_enabled(enable);
93 }
94
95 void WebRtcLocalAudioTrackAdapter::AddSink(
96 webrtc::AudioTrackSinkInterface* sink) {
97 DCHECK(!signaling_task_runner_ ||
98 signaling_task_runner_->RunsTasksOnCurrentThread());
99 DCHECK(sink);
100 #ifndef NDEBUG
101 // Verify that |sink| has not been added.
102 for (ScopedVector<WebRtcAudioSinkAdapter>::const_iterator it =
103 sink_adapters_.begin();
104 it != sink_adapters_.end(); ++it) {
105 DCHECK(!(*it)->IsEqual(sink));
106 }
107 #endif
108
109 std::unique_ptr<WebRtcAudioSinkAdapter> adapter(
110 new WebRtcAudioSinkAdapter(sink));
111 owner_->AddSink(adapter.get());
112 sink_adapters_.push_back(adapter.release());
113 }
114
115 void WebRtcLocalAudioTrackAdapter::RemoveSink(
116 webrtc::AudioTrackSinkInterface* sink) {
117 DCHECK(!signaling_task_runner_ ||
118 signaling_task_runner_->RunsTasksOnCurrentThread());
119 DCHECK(sink);
120 for (ScopedVector<WebRtcAudioSinkAdapter>::iterator it =
121 sink_adapters_.begin();
122 it != sink_adapters_.end(); ++it) {
123 if ((*it)->IsEqual(sink)) {
124 owner_->RemoveSink(*it);
125 sink_adapters_.erase(it);
126 return;
127 }
128 }
129 }
130
131 bool WebRtcLocalAudioTrackAdapter::GetSignalLevel(int* level) {
132 DCHECK(!signaling_task_runner_ ||
133 signaling_task_runner_->RunsTasksOnCurrentThread());
134
135 // |level_| is only set once, so it's safe to read without first acquiring a
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 */);
145 return true;
146 }
147
148 rtc::scoped_refptr<webrtc::AudioProcessorInterface>
149 WebRtcLocalAudioTrackAdapter::GetAudioProcessor() {
150 DCHECK(!signaling_task_runner_ ||
151 signaling_task_runner_->RunsTasksOnCurrentThread());
152 return audio_processor_.get();
153 }
154
155 webrtc::AudioSourceInterface* WebRtcLocalAudioTrackAdapter::GetSource() const {
156 DCHECK(!signaling_task_runner_ ||
157 signaling_task_runner_->RunsTasksOnCurrentThread());
158 return track_source_;
159 }
160
161 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698