blob: 05d0899f2a1e3fa5c4efeb0ca86d8162138dc946 [file] [log] [blame]
wu@webrtc.orgb9a088b2014-02-13 23:18:491/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2014 The WebRTC project authors. All Rights Reserved.
wu@webrtc.orgb9a088b2014-02-13 23:18:493 *
kjellanderb24317b2016-02-10 15:54:434 * 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:499 */
10
Steve Anton10542f22019-01-11 17:11:0011#include "pc/remote_audio_source.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:4912
Yves Gerey3e707812018-11-28 15:47:4913#include <stddef.h>
Jonas Olssona4d87372019-07-05 17:08:3314
Mirko Bonadei317a1f02019-09-17 15:06:1815#include <memory>
Yves Gerey3e707812018-11-28 15:47:4916#include <string>
wu@webrtc.orgb9a088b2014-02-13 23:18:4917
Steve Anton64b626b2019-01-29 01:25:2618#include "absl/algorithm/container.h"
Mirko Bonadeid9708072019-01-25 19:26:4819#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0021#include "rtc_base/constructor_magic.h"
Yves Gerey3e707812018-11-28 15:47:4922#include "rtc_base/location.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/logging.h"
Ruslan Burakov7ea46052019-02-16 01:07:0524#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3125#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 15:47:4926#include "rtc_base/thread_checker.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:4927
28namespace webrtc {
29
Steve Antond3679212018-01-18 01:41:0230// This proxy is passed to the underlying media engine to receive audio data as
31// they come in. The data will then be passed back up to the RemoteAudioSource
32// which will fan it out to all the sinks that have been added to it.
33class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
Tommif888bb52015-12-12 00:37:0134 public:
Steve Antond3679212018-01-18 01:41:0235 explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
36 RTC_DCHECK(source);
37 }
38 ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 00:37:0139
Steve Antond3679212018-01-18 01:41:0240 // AudioSinkInterface implementation.
Tommif888bb52015-12-12 00:37:0141 void OnData(const AudioSinkInterface::Data& audio) override {
Steve Antond3679212018-01-18 01:41:0242 source_->OnData(audio);
Tommif888bb52015-12-12 00:37:0143 }
44
Steve Antond3679212018-01-18 01:41:0245 private:
Tommif888bb52015-12-12 00:37:0146 const rtc::scoped_refptr<RemoteAudioSource> source_;
Steve Antond3679212018-01-18 01:41:0247
48 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDataProxy);
Tommif888bb52015-12-12 00:37:0149};
50
Steve Antond3679212018-01-18 01:41:0251RemoteAudioSource::RemoteAudioSource(rtc::Thread* worker_thread)
Tommif888bb52015-12-12 00:37:0152 : main_thread_(rtc::Thread::Current()),
Steve Antond3679212018-01-18 01:41:0253 worker_thread_(worker_thread),
Ruslan Burakov428dcb22019-04-18 15:49:4954 state_(MediaSourceInterface::kLive) {
Tommif888bb52015-12-12 00:37:0155 RTC_DCHECK(main_thread_);
Steve Antond3679212018-01-18 01:41:0256 RTC_DCHECK(worker_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:4957}
58
59RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 00:37:0160 RTC_DCHECK(main_thread_->IsCurrent());
61 RTC_DCHECK(audio_observers_.empty());
62 RTC_DCHECK(sinks_.empty());
wu@webrtc.orgb9a088b2014-02-13 23:18:4963}
64
Steve Antond3679212018-01-18 01:41:0265void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
Saurav Das749f6602019-12-04 17:31:3666 absl::optional<uint32_t> ssrc) {
Steve Antond3679212018-01-18 01:41:0267 RTC_DCHECK_RUN_ON(main_thread_);
68 RTC_DCHECK(media_channel);
Ruslan Burakov7ea46052019-02-16 01:07:0569
Steve Antond3679212018-01-18 01:41:0270 // Register for callbacks immediately before AddSink so that we always get
71 // notified when a channel goes out of scope (signaled when "AudioDataProxy"
72 // is destroyed).
73 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
Saurav Das749f6602019-12-04 17:31:3674 ssrc ? media_channel->SetRawAudioSink(
75 *ssrc, std::make_unique<AudioDataProxy>(this))
76 : media_channel->SetDefaultRawAudioSink(
77 std::make_unique<AudioDataProxy>(this));
Steve Antond3679212018-01-18 01:41:0278 });
79}
80
81void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
Saurav Das749f6602019-12-04 17:31:3682 absl::optional<uint32_t> ssrc) {
Steve Antond3679212018-01-18 01:41:0283 RTC_DCHECK_RUN_ON(main_thread_);
84 RTC_DCHECK(media_channel);
Ruslan Burakov7ea46052019-02-16 01:07:0585
Saurav Das749f6602019-12-04 17:31:3686 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
87 ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr)
88 : media_channel->SetDefaultRawAudioSink(nullptr);
89 });
wu@webrtc.orgb9a088b2014-02-13 23:18:4990}
91
Tommif888bb52015-12-12 00:37:0192MediaSourceInterface::SourceState RemoteAudioSource::state() const {
93 RTC_DCHECK(main_thread_->IsCurrent());
94 return state_;
95}
96
tommi6eca7e32015-12-15 12:27:1197bool RemoteAudioSource::remote() const {
98 RTC_DCHECK(main_thread_->IsCurrent());
99 return true;
100}
101
Tommif888bb52015-12-12 00:37:01102void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-10 00:22:01103 RTC_DCHECK_GE(volume, 0);
104 RTC_DCHECK_LE(volume, 10);
Steve Antond3679212018-01-18 01:41:02105 for (auto* observer : audio_observers_) {
Tommif888bb52015-12-12 00:37:01106 observer->OnSetVolume(volume);
Steve Antond3679212018-01-18 01:41:02107 }
Tommif888bb52015-12-12 00:37:01108}
109
wu@webrtc.orgb9a088b2014-02-13 23:18:49110void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 00:37:01111 RTC_DCHECK(observer != NULL);
Steve Anton64b626b2019-01-29 01:25:26112 RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
wu@webrtc.orgb9a088b2014-02-13 23:18:49113 audio_observers_.push_back(observer);
114}
115
116void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 00:37:01117 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49118 audio_observers_.remove(observer);
119}
120
Tommif888bb52015-12-12 00:37:01121void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
122 RTC_DCHECK(main_thread_->IsCurrent());
123 RTC_DCHECK(sink);
124
125 if (state_ != MediaSourceInterface::kLive) {
Mirko Bonadei675513b2017-11-09 10:09:25126 RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
Tommif888bb52015-12-12 00:37:01127 return;
128 }
129
130 rtc::CritScope lock(&sink_lock_);
Steve Anton3d023842019-01-29 03:48:28131 RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
Tommif888bb52015-12-12 00:37:01132 sinks_.push_back(sink);
133}
134
135void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
136 RTC_DCHECK(main_thread_->IsCurrent());
137 RTC_DCHECK(sink);
138
139 rtc::CritScope lock(&sink_lock_);
140 sinks_.remove(sink);
141}
142
143void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
144 // Called on the externally-owned audio callback thread, via/from webrtc.
145 rtc::CritScope lock(&sink_lock_);
146 for (auto* sink : sinks_) {
147 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
148 audio.samples_per_channel);
149 }
150}
151
Taylor Brandstetterba29c6a2016-06-27 23:30:35152void RemoteAudioSource::OnAudioChannelGone() {
153 // Called when the audio channel is deleted. It may be the worker thread
Tommif888bb52015-12-12 00:37:01154 // in libjingle or may be a different worker thread.
Steve Anton3b80aac2017-10-19 17:17:12155 // This object needs to live long enough for the cleanup logic in OnMessage to
156 // run, so take a reference to it as the data. Sometimes the message may not
157 // be processed (because the thread was destroyed shortly after this call),
158 // but that is fine because the thread destructor will take care of destroying
159 // the message data which will release the reference on RemoteAudioSource.
160 main_thread_->Post(RTC_FROM_HERE, this, 0,
161 new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
Tommif888bb52015-12-12 00:37:01162}
163
164void RemoteAudioSource::OnMessage(rtc::Message* msg) {
165 RTC_DCHECK(main_thread_->IsCurrent());
166 sinks_.clear();
167 state_ = MediaSourceInterface::kEnded;
168 FireOnChanged();
Steve Anton3b80aac2017-10-19 17:17:12169 // Will possibly delete this RemoteAudioSource since it is reference counted
170 // in the message.
171 delete msg->pdata;
Tommif888bb52015-12-12 00:37:01172}
173
wu@webrtc.orgb9a088b2014-02-13 23:18:49174} // namespace webrtc