blob: 9e65f6781c82335e8607a6e85128f3575d89b736 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000016
Steve Anton64b626b2019-01-28 17:25:26 -080017#include "absl/algorithm/container.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010018#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 14:31:24 +010019#include "api/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.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"
henrikac6cf9022020-07-29 17:20:13 +020023#include "rtc_base/strings/string_format.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000025
26namespace webrtc {
27
Steve Antond3679212018-01-17 17:41:02 -080028// This proxy is passed to the underlying media engine to receive audio data as
29// they come in. The data will then be passed back up to the RemoteAudioSource
30// which will fan it out to all the sinks that have been added to it.
31class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
Tommif888bb52015-12-12 01:37:01 +010032 public:
Steve Antond3679212018-01-17 17:41:02 -080033 explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
34 RTC_DCHECK(source);
35 }
Niels Möllerde953292020-09-29 09:46:21 +020036
37 AudioDataProxy() = delete;
38 AudioDataProxy(const AudioDataProxy&) = delete;
39 AudioDataProxy& operator=(const AudioDataProxy&) = delete;
40
Steve Antond3679212018-01-17 17:41:02 -080041 ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 01:37:01 +010042
Steve Antond3679212018-01-17 17:41:02 -080043 // AudioSinkInterface implementation.
Tommif888bb52015-12-12 01:37:01 +010044 void OnData(const AudioSinkInterface::Data& audio) override {
Steve Antond3679212018-01-17 17:41:02 -080045 source_->OnData(audio);
Tommif888bb52015-12-12 01:37:01 +010046 }
47
Steve Antond3679212018-01-17 17:41:02 -080048 private:
Tommif888bb52015-12-12 01:37:01 +010049 const rtc::scoped_refptr<RemoteAudioSource> source_;
Tommif888bb52015-12-12 01:37:01 +010050};
51
Henrik Boströmc335b0e2021-04-08 07:25:38 +020052RemoteAudioSource::RemoteAudioSource(
53 rtc::Thread* worker_thread,
54 OnAudioChannelGoneAction on_audio_channel_gone_action)
Tomas Gunnarsson77baeee2020-09-24 22:39:21 +020055 : main_thread_(rtc::Thread::Current()),
Steve Antond3679212018-01-17 17:41:02 -080056 worker_thread_(worker_thread),
Henrik Boströmc335b0e2021-04-08 07:25:38 +020057 on_audio_channel_gone_action_(on_audio_channel_gone_action),
Ruslan Burakov428dcb22019-04-18 17:49:49 +020058 state_(MediaSourceInterface::kLive) {
Tommif888bb52015-12-12 01:37:01 +010059 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());
Henrik Boströmfa8a9462021-04-15 10:44:00 +020066 if (!sinks_.empty()) {
67 RTC_LOG(LS_WARNING)
68 << "RemoteAudioSource destroyed while sinks_ is non-empty.";
69 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000070}
71
Steve Antond3679212018-01-17 17:41:02 -080072void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
Saurav Das749f6602019-12-04 09:31:36 -080073 absl::optional<uint32_t> ssrc) {
Steve Antond3679212018-01-17 17:41:02 -080074 RTC_DCHECK_RUN_ON(main_thread_);
75 RTC_DCHECK(media_channel);
Ruslan Burakov7ea46052019-02-16 02:07:05 +010076
Steve Antond3679212018-01-17 17:41:02 -080077 // Register for callbacks immediately before AddSink so that we always get
78 // notified when a channel goes out of scope (signaled when "AudioDataProxy"
79 // is destroyed).
80 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
Saurav Das749f6602019-12-04 09:31:36 -080081 ssrc ? media_channel->SetRawAudioSink(
82 *ssrc, std::make_unique<AudioDataProxy>(this))
83 : media_channel->SetDefaultRawAudioSink(
84 std::make_unique<AudioDataProxy>(this));
Steve Antond3679212018-01-17 17:41:02 -080085 });
86}
87
88void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
Saurav Das749f6602019-12-04 09:31:36 -080089 absl::optional<uint32_t> ssrc) {
Steve Antond3679212018-01-17 17:41:02 -080090 RTC_DCHECK_RUN_ON(main_thread_);
91 RTC_DCHECK(media_channel);
Ruslan Burakov7ea46052019-02-16 02:07:05 +010092
Saurav Das749f6602019-12-04 09:31:36 -080093 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
94 ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr)
95 : media_channel->SetDefaultRawAudioSink(nullptr);
96 });
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000097}
98
Henrik Boströmc335b0e2021-04-08 07:25:38 +020099void RemoteAudioSource::SetState(SourceState new_state) {
100 if (state_ != new_state) {
101 state_ = new_state;
102 FireOnChanged();
103 }
104}
105
Tommif888bb52015-12-12 01:37:01 +0100106MediaSourceInterface::SourceState RemoteAudioSource::state() const {
107 RTC_DCHECK(main_thread_->IsCurrent());
108 return state_;
109}
110
tommi6eca7e32015-12-15 04:27:11 -0800111bool RemoteAudioSource::remote() const {
112 RTC_DCHECK(main_thread_->IsCurrent());
113 return true;
114}
115
Tommif888bb52015-12-12 01:37:01 +0100116void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -0700117 RTC_DCHECK_GE(volume, 0);
118 RTC_DCHECK_LE(volume, 10);
henrikac6cf9022020-07-29 17:20:13 +0200119 RTC_LOG(LS_INFO) << rtc::StringFormat("RAS::%s({volume=%.2f})", __func__,
120 volume);
Steve Antond3679212018-01-17 17:41:02 -0800121 for (auto* observer : audio_observers_) {
Tommif888bb52015-12-12 01:37:01 +0100122 observer->OnSetVolume(volume);
Steve Antond3679212018-01-17 17:41:02 -0800123 }
Tommif888bb52015-12-12 01:37:01 +0100124}
125
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000126void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100127 RTC_DCHECK(observer != NULL);
Steve Anton64b626b2019-01-28 17:25:26 -0800128 RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000129 audio_observers_.push_back(observer);
130}
131
132void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100133 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000134 audio_observers_.remove(observer);
135}
136
Tommif888bb52015-12-12 01:37:01 +0100137void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
138 RTC_DCHECK(main_thread_->IsCurrent());
139 RTC_DCHECK(sink);
140
141 if (state_ != MediaSourceInterface::kLive) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100142 RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
Tommif888bb52015-12-12 01:37:01 +0100143 return;
144 }
145
Markus Handell6fcd0f82020-07-07 19:08:53 +0200146 MutexLock lock(&sink_lock_);
Steve Anton3d023842019-01-28 19:48:28 -0800147 RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
Tommif888bb52015-12-12 01:37:01 +0100148 sinks_.push_back(sink);
149}
150
151void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
152 RTC_DCHECK(main_thread_->IsCurrent());
153 RTC_DCHECK(sink);
154
Markus Handell6fcd0f82020-07-07 19:08:53 +0200155 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 01:37:01 +0100156 sinks_.remove(sink);
157}
158
159void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
160 // Called on the externally-owned audio callback thread, via/from webrtc.
Markus Handell6fcd0f82020-07-07 19:08:53 +0200161 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 01:37:01 +0100162 for (auto* sink : sinks_) {
Minyue Li99d6d812020-01-29 10:25:12 +0100163 // When peerconnection acts as an audio source, it should not provide
164 // absolute capture timestamp.
Tommif888bb52015-12-12 01:37:01 +0100165 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
Minyue Li99d6d812020-01-29 10:25:12 +0100166 audio.samples_per_channel,
167 /*absolute_capture_timestamp_ms=*/absl::nullopt);
Tommif888bb52015-12-12 01:37:01 +0100168 }
169}
170
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700171void RemoteAudioSource::OnAudioChannelGone() {
Henrik Boströmc335b0e2021-04-08 07:25:38 +0200172 if (on_audio_channel_gone_action_ != OnAudioChannelGoneAction::kEnd) {
173 return;
174 }
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700175 // Called when the audio channel is deleted. It may be the worker thread
Tommif888bb52015-12-12 01:37:01 +0100176 // in libjingle or may be a different worker thread.
Steve Anton3b80aac2017-10-19 10:17:12 -0700177 // This object needs to live long enough for the cleanup logic in OnMessage to
178 // run, so take a reference to it as the data. Sometimes the message may not
179 // be processed (because the thread was destroyed shortly after this call),
180 // but that is fine because the thread destructor will take care of destroying
181 // the message data which will release the reference on RemoteAudioSource.
182 main_thread_->Post(RTC_FROM_HERE, this, 0,
183 new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
Tommif888bb52015-12-12 01:37:01 +0100184}
185
186void RemoteAudioSource::OnMessage(rtc::Message* msg) {
187 RTC_DCHECK(main_thread_->IsCurrent());
188 sinks_.clear();
Henrik Boströmc335b0e2021-04-08 07:25:38 +0200189 SetState(MediaSourceInterface::kEnded);
Steve Anton3b80aac2017-10-19 10:17:12 -0700190 // Will possibly delete this RemoteAudioSource since it is reference counted
191 // in the message.
192 delete msg->pdata;
Tommif888bb52015-12-12 01:37:01 +0100193}
194
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000195} // namespace webrtc