blob: 667312c38cce61e5e7d920ecc96d5c6487dc3461 [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>
Harald Alvestrandc24a2182022-02-23 13:44:59 +000016#include <string>
Danil Chapovalovc6c346d2022-08-22 10:22:40 +020017#include <utility>
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000018
Steve Anton64b626b2019-01-28 17:25:26 -080019#include "absl/algorithm/container.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010020#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 14:31:24 +010021#include "api/sequence_checker.h"
Danil Chapovalovc6c346d2022-08-22 10:22:40 +020022#include "api/task_queue/task_queue_base.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/logging.h"
henrikac6cf9022020-07-29 17:20:13 +020025#include "rtc_base/strings/string_format.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000026
27namespace webrtc {
28
Steve Antond3679212018-01-17 17:41:02 -080029// This proxy is passed to the underlying media engine to receive audio data as
30// they come in. The data will then be passed back up to the RemoteAudioSource
31// which will fan it out to all the sinks that have been added to it.
32class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
Tommif888bb52015-12-12 01:37:01 +010033 public:
Steve Antond3679212018-01-17 17:41:02 -080034 explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
35 RTC_DCHECK(source);
36 }
Niels Möllerde953292020-09-29 09:46:21 +020037
38 AudioDataProxy() = delete;
39 AudioDataProxy(const AudioDataProxy&) = delete;
40 AudioDataProxy& operator=(const AudioDataProxy&) = delete;
41
Steve Antond3679212018-01-17 17:41:02 -080042 ~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_;
Tommif888bb52015-12-12 01:37:01 +010051};
52
Henrik Boströmc335b0e2021-04-08 07:25:38 +020053RemoteAudioSource::RemoteAudioSource(
Danil Chapovalovc6c346d2022-08-22 10:22:40 +020054 TaskQueueBase* worker_thread,
Henrik Boströmc335b0e2021-04-08 07:25:38 +020055 OnAudioChannelGoneAction on_audio_channel_gone_action)
Danil Chapovalovc6c346d2022-08-22 10:22:40 +020056 : main_thread_(TaskQueueBase::Current()),
Steve Antond3679212018-01-17 17:41:02 -080057 worker_thread_(worker_thread),
Henrik Boströmc335b0e2021-04-08 07:25:38 +020058 on_audio_channel_gone_action_(on_audio_channel_gone_action),
Tommi20d8d912022-02-08 21:12:15 +010059 state_(MediaSourceInterface::kInitializing) {
Tommif888bb52015-12-12 01:37:01 +010060 RTC_DCHECK(main_thread_);
Steve Antond3679212018-01-17 17:41:02 -080061 RTC_DCHECK(worker_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000062}
63
64RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 01:37:01 +010065 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) {
Tommi4ccdf932021-05-17 14:50:10 +020074 RTC_DCHECK_RUN_ON(worker_thread_);
Ruslan Burakov7ea46052019-02-16 02:07:05 +010075
Steve Antond3679212018-01-17 17:41:02 -080076 // Register for callbacks immediately before AddSink so that we always get
77 // notified when a channel goes out of scope (signaled when "AudioDataProxy"
78 // is destroyed).
Tommi4ccdf932021-05-17 14:50:10 +020079 RTC_DCHECK(media_channel);
80 ssrc ? media_channel->SetRawAudioSink(*ssrc,
81 std::make_unique<AudioDataProxy>(this))
82 : media_channel->SetDefaultRawAudioSink(
83 std::make_unique<AudioDataProxy>(this));
Steve Antond3679212018-01-17 17:41:02 -080084}
85
86void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
Saurav Das749f6602019-12-04 09:31:36 -080087 absl::optional<uint32_t> ssrc) {
Tommi4ccdf932021-05-17 14:50:10 +020088 RTC_DCHECK_RUN_ON(worker_thread_);
Steve Antond3679212018-01-17 17:41:02 -080089 RTC_DCHECK(media_channel);
Tommi4ccdf932021-05-17 14:50:10 +020090 ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr)
91 : media_channel->SetDefaultRawAudioSink(nullptr);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000092}
93
Henrik Boströmc335b0e2021-04-08 07:25:38 +020094void RemoteAudioSource::SetState(SourceState new_state) {
Tommi4ccdf932021-05-17 14:50:10 +020095 RTC_DCHECK_RUN_ON(main_thread_);
Henrik Boströmc335b0e2021-04-08 07:25:38 +020096 if (state_ != new_state) {
97 state_ = new_state;
98 FireOnChanged();
99 }
100}
101
Tommif888bb52015-12-12 01:37:01 +0100102MediaSourceInterface::SourceState RemoteAudioSource::state() const {
Tommi4ccdf932021-05-17 14:50:10 +0200103 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 01:37:01 +0100104 return state_;
105}
106
tommi6eca7e32015-12-15 04:27:11 -0800107bool RemoteAudioSource::remote() const {
Tommi4ccdf932021-05-17 14:50:10 +0200108 RTC_DCHECK_RUN_ON(main_thread_);
tommi6eca7e32015-12-15 04:27:11 -0800109 return true;
110}
111
Tommif888bb52015-12-12 01:37:01 +0100112void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -0700113 RTC_DCHECK_GE(volume, 0);
114 RTC_DCHECK_LE(volume, 10);
henrikac6cf9022020-07-29 17:20:13 +0200115 RTC_LOG(LS_INFO) << rtc::StringFormat("RAS::%s({volume=%.2f})", __func__,
116 volume);
Steve Antond3679212018-01-17 17:41:02 -0800117 for (auto* observer : audio_observers_) {
Tommif888bb52015-12-12 01:37:01 +0100118 observer->OnSetVolume(volume);
Steve Antond3679212018-01-17 17:41:02 -0800119 }
Tommif888bb52015-12-12 01:37:01 +0100120}
121
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000122void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100123 RTC_DCHECK(observer != NULL);
Steve Anton64b626b2019-01-28 17:25:26 -0800124 RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000125 audio_observers_.push_back(observer);
126}
127
128void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100129 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000130 audio_observers_.remove(observer);
131}
132
Tommif888bb52015-12-12 01:37:01 +0100133void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
Tommi4ccdf932021-05-17 14:50:10 +0200134 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 01:37:01 +0100135 RTC_DCHECK(sink);
136
Markus Handell6fcd0f82020-07-07 19:08:53 +0200137 MutexLock lock(&sink_lock_);
Steve Anton3d023842019-01-28 19:48:28 -0800138 RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
Tommif888bb52015-12-12 01:37:01 +0100139 sinks_.push_back(sink);
140}
141
142void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
Tommi4ccdf932021-05-17 14:50:10 +0200143 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 01:37:01 +0100144 RTC_DCHECK(sink);
145
Markus Handell6fcd0f82020-07-07 19:08:53 +0200146 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 01:37:01 +0100147 sinks_.remove(sink);
148}
149
150void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
151 // Called on the externally-owned audio callback thread, via/from webrtc.
Markus Handell6fcd0f82020-07-07 19:08:53 +0200152 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 01:37:01 +0100153 for (auto* sink : sinks_) {
Minyue Li99d6d812020-01-29 10:25:12 +0100154 // When peerconnection acts as an audio source, it should not provide
155 // absolute capture timestamp.
Tommif888bb52015-12-12 01:37:01 +0100156 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
Minyue Li99d6d812020-01-29 10:25:12 +0100157 audio.samples_per_channel,
158 /*absolute_capture_timestamp_ms=*/absl::nullopt);
Tommif888bb52015-12-12 01:37:01 +0100159 }
160}
161
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700162void RemoteAudioSource::OnAudioChannelGone() {
Henrik Boströmc335b0e2021-04-08 07:25:38 +0200163 if (on_audio_channel_gone_action_ != OnAudioChannelGoneAction::kEnd) {
164 return;
165 }
Danil Chapovalovc6c346d2022-08-22 10:22:40 +0200166 // Called when the audio channel is deleted. It may be the worker thread or
167 // may be a different task queue.
168 // This object needs to live long enough for the cleanup logic in the posted
169 // task to run, so take a reference to it. Sometimes the task may not be
170 // processed (because the task queue was destroyed shortly after this call),
171 // but that is fine because the task queue destructor will take care of
172 // destroying task which will release the reference on RemoteAudioSource.
173 rtc::scoped_refptr<RemoteAudioSource> thiz(this);
174 main_thread_->PostTask([thiz = std::move(thiz)] {
175 thiz->sinks_.clear();
176 thiz->SetState(MediaSourceInterface::kEnded);
177 });
Tommif888bb52015-12-12 01:37:01 +0100178}
179
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000180} // namespace webrtc