blob: 63944c6d83ae219868ce1c99ac0061581c120350 [file] [log] [blame]
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2014 The WebRTC project authors. All Rights Reserved.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/remote_audio_source.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <string>
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000015
Steve Anton64b626b2019-01-28 17:25:26 -080016#include "absl/algorithm/container.h"
Karl Wiberg918f50c2018-07-05 11:40:33 +020017#include "absl/memory/memory.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010018#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/constructor_magic.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/location.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/logging.h"
Ruslan Burakov7ea46052019-02-16 02:07:05 +010023#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "rtc_base/thread_checker.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000026
27namespace webrtc {
28
Ruslan Burakov7ea46052019-02-16 02:07:05 +010029namespace {
30constexpr int kDefaultLatency = 0;
31constexpr int kRoundToZeroThresholdMs = 10;
32} // namespace
33
Steve Antond3679212018-01-17 17:41:02 -080034// This proxy is passed to the underlying media engine to receive audio data as
35// they come in. The data will then be passed back up to the RemoteAudioSource
36// which will fan it out to all the sinks that have been added to it.
37class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
Tommif888bb52015-12-12 01:37:01 +010038 public:
Steve Antond3679212018-01-17 17:41:02 -080039 explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
40 RTC_DCHECK(source);
41 }
42 ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 01:37:01 +010043
Steve Antond3679212018-01-17 17:41:02 -080044 // AudioSinkInterface implementation.
Tommif888bb52015-12-12 01:37:01 +010045 void OnData(const AudioSinkInterface::Data& audio) override {
Steve Antond3679212018-01-17 17:41:02 -080046 source_->OnData(audio);
Tommif888bb52015-12-12 01:37:01 +010047 }
48
Steve Antond3679212018-01-17 17:41:02 -080049 private:
Tommif888bb52015-12-12 01:37:01 +010050 const rtc::scoped_refptr<RemoteAudioSource> source_;
Steve Antond3679212018-01-17 17:41:02 -080051
52 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDataProxy);
Tommif888bb52015-12-12 01:37:01 +010053};
54
Steve Antond3679212018-01-17 17:41:02 -080055RemoteAudioSource::RemoteAudioSource(rtc::Thread* worker_thread)
Tommif888bb52015-12-12 01:37:01 +010056 : main_thread_(rtc::Thread::Current()),
Steve Antond3679212018-01-17 17:41:02 -080057 worker_thread_(worker_thread),
Tommif888bb52015-12-12 01:37:01 +010058 state_(MediaSourceInterface::kLive) {
59 RTC_DCHECK(main_thread_);
Steve Antond3679212018-01-17 17:41:02 -080060 RTC_DCHECK(worker_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000061}
62
63RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 01:37:01 +010064 RTC_DCHECK(main_thread_->IsCurrent());
65 RTC_DCHECK(audio_observers_.empty());
66 RTC_DCHECK(sinks_.empty());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000067}
68
Steve Antond3679212018-01-17 17:41:02 -080069void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
70 uint32_t ssrc) {
71 RTC_DCHECK_RUN_ON(main_thread_);
72 RTC_DCHECK(media_channel);
Ruslan Burakov7ea46052019-02-16 02:07:05 +010073 // Check that there are no consecutive start calls.
74 RTC_DCHECK(!media_channel_ && !ssrc_);
75
76 // Remember media channel ssrc pair for latency calls.
77 media_channel_ = media_channel;
78 ssrc_ = ssrc;
79
Steve Antond3679212018-01-17 17:41:02 -080080 // Register for callbacks immediately before AddSink so that we always get
81 // notified when a channel goes out of scope (signaled when "AudioDataProxy"
82 // is destroyed).
83 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
Karl Wiberg918f50c2018-07-05 11:40:33 +020084 media_channel->SetRawAudioSink(ssrc,
85 absl::make_unique<AudioDataProxy>(this));
Steve Antond3679212018-01-17 17:41:02 -080086 });
Ruslan Burakov7ea46052019-02-16 02:07:05 +010087
88 // Trying to apply cached latency for the audio stream.
89 if (cached_latency_) {
90 SetLatency(cached_latency_.value());
91 }
Steve Antond3679212018-01-17 17:41:02 -080092}
93
94void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
95 uint32_t ssrc) {
96 RTC_DCHECK_RUN_ON(main_thread_);
97 RTC_DCHECK(media_channel);
Ruslan Burakov7ea46052019-02-16 02:07:05 +010098
99 // Assume that audio stream is no longer present for latency calls.
100 media_channel_ = nullptr;
101 ssrc_ = absl::nullopt;
102
Steve Antond3679212018-01-17 17:41:02 -0800103 worker_thread_->Invoke<void>(
104 RTC_FROM_HERE, [&] { media_channel->SetRawAudioSink(ssrc, nullptr); });
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000105}
106
Tommif888bb52015-12-12 01:37:01 +0100107MediaSourceInterface::SourceState RemoteAudioSource::state() const {
108 RTC_DCHECK(main_thread_->IsCurrent());
109 return state_;
110}
111
tommi6eca7e32015-12-15 04:27:11 -0800112bool RemoteAudioSource::remote() const {
113 RTC_DCHECK(main_thread_->IsCurrent());
114 return true;
115}
116
Tommif888bb52015-12-12 01:37:01 +0100117void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -0700118 RTC_DCHECK_GE(volume, 0);
119 RTC_DCHECK_LE(volume, 10);
Steve Antond3679212018-01-17 17:41:02 -0800120 for (auto* observer : audio_observers_) {
Tommif888bb52015-12-12 01:37:01 +0100121 observer->OnSetVolume(volume);
Steve Antond3679212018-01-17 17:41:02 -0800122 }
Tommif888bb52015-12-12 01:37:01 +0100123}
124
Ruslan Burakov7ea46052019-02-16 02:07:05 +0100125void RemoteAudioSource::SetLatency(double latency) {
126 RTC_DCHECK_GE(latency, 0);
127 RTC_DCHECK_LE(latency, 10);
128
129 int delay_ms = rtc::dchecked_cast<int>(latency * 1000);
130 // In NetEq 0 delay has special meaning of being unconstrained value that is
131 // why we round delay to 0 if it is small enough during conversion from
132 // latency.
133 if (delay_ms <= kRoundToZeroThresholdMs) {
134 delay_ms = 0;
135 }
136
137 cached_latency_ = latency;
138 SetDelayMs(delay_ms);
139}
140
141double RemoteAudioSource::GetLatency() const {
142 absl::optional<int> delay_ms = GetDelayMs();
143
144 if (delay_ms) {
145 return delay_ms.value() / 1000.0;
146 } else {
147 return cached_latency_.value_or(kDefaultLatency);
148 }
149}
150
151bool RemoteAudioSource::SetDelayMs(int delay_ms) {
152 if (!media_channel_ || !ssrc_) {
153 return false;
154 }
155
156 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
157 media_channel_->SetBaseMinimumPlayoutDelayMs(ssrc_.value(), delay_ms);
158 });
159 return true;
160}
161
162absl::optional<int> RemoteAudioSource::GetDelayMs() const {
163 if (!media_channel_ || !ssrc_) {
164 return absl::nullopt;
165 }
166
167 return worker_thread_->Invoke<absl::optional<int>>(RTC_FROM_HERE, [&] {
168 return media_channel_->GetBaseMinimumPlayoutDelayMs(ssrc_.value());
169 });
170}
171
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000172void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100173 RTC_DCHECK(observer != NULL);
Steve Anton64b626b2019-01-28 17:25:26 -0800174 RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000175 audio_observers_.push_back(observer);
176}
177
178void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100179 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000180 audio_observers_.remove(observer);
181}
182
Tommif888bb52015-12-12 01:37:01 +0100183void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
184 RTC_DCHECK(main_thread_->IsCurrent());
185 RTC_DCHECK(sink);
186
187 if (state_ != MediaSourceInterface::kLive) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100188 RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
Tommif888bb52015-12-12 01:37:01 +0100189 return;
190 }
191
192 rtc::CritScope lock(&sink_lock_);
Steve Anton3d023842019-01-28 19:48:28 -0800193 RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
Tommif888bb52015-12-12 01:37:01 +0100194 sinks_.push_back(sink);
195}
196
197void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
198 RTC_DCHECK(main_thread_->IsCurrent());
199 RTC_DCHECK(sink);
200
201 rtc::CritScope lock(&sink_lock_);
202 sinks_.remove(sink);
203}
204
205void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
206 // Called on the externally-owned audio callback thread, via/from webrtc.
207 rtc::CritScope lock(&sink_lock_);
208 for (auto* sink : sinks_) {
209 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
210 audio.samples_per_channel);
211 }
212}
213
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700214void RemoteAudioSource::OnAudioChannelGone() {
215 // Called when the audio channel is deleted. It may be the worker thread
Tommif888bb52015-12-12 01:37:01 +0100216 // in libjingle or may be a different worker thread.
Steve Anton3b80aac2017-10-19 10:17:12 -0700217 // This object needs to live long enough for the cleanup logic in OnMessage to
218 // run, so take a reference to it as the data. Sometimes the message may not
219 // be processed (because the thread was destroyed shortly after this call),
220 // but that is fine because the thread destructor will take care of destroying
221 // the message data which will release the reference on RemoteAudioSource.
222 main_thread_->Post(RTC_FROM_HERE, this, 0,
223 new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
Tommif888bb52015-12-12 01:37:01 +0100224}
225
226void RemoteAudioSource::OnMessage(rtc::Message* msg) {
227 RTC_DCHECK(main_thread_->IsCurrent());
228 sinks_.clear();
229 state_ = MediaSourceInterface::kEnded;
230 FireOnChanged();
Steve Anton3b80aac2017-10-19 10:17:12 -0700231 // Will possibly delete this RemoteAudioSource since it is reference counted
232 // in the message.
233 delete msg->pdata;
Tommif888bb52015-12-12 01:37:01 +0100234}
235
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000236} // namespace webrtc