blob: a516c576174e957dc6ddc0401b8a7b892552e7fa [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>
Harald Alvestrandc24a2182022-02-23 13:44:5916#include <string>
Danil Chapovalovc6c346d2022-08-22 08:22:4017#include <utility>
wu@webrtc.orgb9a088b2014-02-13 23:18:4918
Steve Anton64b626b2019-01-29 01:25:2619#include "absl/algorithm/container.h"
Mirko Bonadeid9708072019-01-25 19:26:4820#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 13:31:2421#include "api/sequence_checker.h"
Danil Chapovalovc6c346d2022-08-22 08:22:4022#include "api/task_queue/task_queue_base.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3124#include "rtc_base/logging.h"
henrikac6cf9022020-07-29 15:20:1325#include "rtc_base/strings/string_format.h"
Olga Sharonova2d0ba282022-09-27 13:22:3426#include "rtc_base/trace_event.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 }
Niels Möllerde953292020-09-29 07:46:2138
39 AudioDataProxy() = delete;
40 AudioDataProxy(const AudioDataProxy&) = delete;
41 AudioDataProxy& operator=(const AudioDataProxy&) = delete;
42
Steve Antond3679212018-01-18 01:41:0243 ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 00:37:0144
Steve Antond3679212018-01-18 01:41:0245 // AudioSinkInterface implementation.
Tommif888bb52015-12-12 00:37:0146 void OnData(const AudioSinkInterface::Data& audio) override {
Steve Antond3679212018-01-18 01:41:0247 source_->OnData(audio);
Tommif888bb52015-12-12 00:37:0148 }
49
Steve Antond3679212018-01-18 01:41:0250 private:
Tommif888bb52015-12-12 00:37:0151 const rtc::scoped_refptr<RemoteAudioSource> source_;
Tommif888bb52015-12-12 00:37:0152};
53
Henrik Boströmc335b0e2021-04-08 05:25:3854RemoteAudioSource::RemoteAudioSource(
Danil Chapovalovc6c346d2022-08-22 08:22:4055 TaskQueueBase* worker_thread,
Henrik Boströmc335b0e2021-04-08 05:25:3856 OnAudioChannelGoneAction on_audio_channel_gone_action)
Danil Chapovalovc6c346d2022-08-22 08:22:4057 : main_thread_(TaskQueueBase::Current()),
Steve Antond3679212018-01-18 01:41:0258 worker_thread_(worker_thread),
Henrik Boströmc335b0e2021-04-08 05:25:3859 on_audio_channel_gone_action_(on_audio_channel_gone_action),
Tommi20d8d912022-02-08 20:12:1560 state_(MediaSourceInterface::kInitializing) {
Tommif888bb52015-12-12 00:37:0161 RTC_DCHECK(main_thread_);
Steve Antond3679212018-01-18 01:41:0262 RTC_DCHECK(worker_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:4963}
64
65RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 00:37:0166 RTC_DCHECK(audio_observers_.empty());
Henrik Boströmfa8a9462021-04-15 08:44:0067 if (!sinks_.empty()) {
68 RTC_LOG(LS_WARNING)
69 << "RemoteAudioSource destroyed while sinks_ is non-empty.";
70 }
wu@webrtc.orgb9a088b2014-02-13 23:18:4971}
72
Harald Alvestrandc0d44d92022-12-13 12:57:2473void RemoteAudioSource::Start(
74 cricket::VoiceMediaReceiveChannelInterface* media_channel,
75 absl::optional<uint32_t> ssrc) {
Tommi4ccdf9322021-05-17 12:50:1076 RTC_DCHECK_RUN_ON(worker_thread_);
Ruslan Burakov7ea46052019-02-16 01:07:0577
Steve Antond3679212018-01-18 01:41:0278 // Register for callbacks immediately before AddSink so that we always get
79 // notified when a channel goes out of scope (signaled when "AudioDataProxy"
80 // is destroyed).
Tommi4ccdf9322021-05-17 12:50:1081 RTC_DCHECK(media_channel);
82 ssrc ? media_channel->SetRawAudioSink(*ssrc,
83 std::make_unique<AudioDataProxy>(this))
84 : media_channel->SetDefaultRawAudioSink(
85 std::make_unique<AudioDataProxy>(this));
Steve Antond3679212018-01-18 01:41:0286}
87
Harald Alvestrandc0d44d92022-12-13 12:57:2488void RemoteAudioSource::Stop(
89 cricket::VoiceMediaReceiveChannelInterface* media_channel,
90 absl::optional<uint32_t> ssrc) {
Tommi4ccdf9322021-05-17 12:50:1091 RTC_DCHECK_RUN_ON(worker_thread_);
Steve Antond3679212018-01-18 01:41:0292 RTC_DCHECK(media_channel);
Tommi4ccdf9322021-05-17 12:50:1093 ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr)
94 : media_channel->SetDefaultRawAudioSink(nullptr);
wu@webrtc.orgb9a088b2014-02-13 23:18:4995}
96
Henrik Boströmc335b0e2021-04-08 05:25:3897void RemoteAudioSource::SetState(SourceState new_state) {
Tommi4ccdf9322021-05-17 12:50:1098 RTC_DCHECK_RUN_ON(main_thread_);
Henrik Boströmc335b0e2021-04-08 05:25:3899 if (state_ != new_state) {
100 state_ = new_state;
101 FireOnChanged();
102 }
103}
104
Tommif888bb52015-12-12 00:37:01105MediaSourceInterface::SourceState RemoteAudioSource::state() const {
Tommi4ccdf9322021-05-17 12:50:10106 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 00:37:01107 return state_;
108}
109
tommi6eca7e32015-12-15 12:27:11110bool RemoteAudioSource::remote() const {
Tommi4ccdf9322021-05-17 12:50:10111 RTC_DCHECK_RUN_ON(main_thread_);
tommi6eca7e32015-12-15 12:27:11112 return true;
113}
114
Tommif888bb52015-12-12 00:37:01115void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-10 00:22:01116 RTC_DCHECK_GE(volume, 0);
117 RTC_DCHECK_LE(volume, 10);
henrikac6cf9022020-07-29 15:20:13118 RTC_LOG(LS_INFO) << rtc::StringFormat("RAS::%s({volume=%.2f})", __func__,
119 volume);
Steve Antond3679212018-01-18 01:41:02120 for (auto* observer : audio_observers_) {
Tommif888bb52015-12-12 00:37:01121 observer->OnSetVolume(volume);
Steve Antond3679212018-01-18 01:41:02122 }
Tommif888bb52015-12-12 00:37:01123}
124
wu@webrtc.orgb9a088b2014-02-13 23:18:49125void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 00:37:01126 RTC_DCHECK(observer != NULL);
Steve Anton64b626b2019-01-29 01:25:26127 RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
wu@webrtc.orgb9a088b2014-02-13 23:18:49128 audio_observers_.push_back(observer);
129}
130
131void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 00:37:01132 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49133 audio_observers_.remove(observer);
134}
135
Tommif888bb52015-12-12 00:37:01136void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
Tommi4ccdf9322021-05-17 12:50:10137 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 00:37:01138 RTC_DCHECK(sink);
139
Markus Handell6fcd0f82020-07-07 17:08:53140 MutexLock lock(&sink_lock_);
Steve Anton3d023842019-01-29 03:48:28141 RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
Tommif888bb52015-12-12 00:37:01142 sinks_.push_back(sink);
143}
144
145void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
Tommi4ccdf9322021-05-17 12:50:10146 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 00:37:01147 RTC_DCHECK(sink);
148
Markus Handell6fcd0f82020-07-07 17:08:53149 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 00:37:01150 sinks_.remove(sink);
151}
152
153void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
154 // Called on the externally-owned audio callback thread, via/from webrtc.
Olga Sharonova2d0ba282022-09-27 13:22:34155 TRACE_EVENT0("webrtc", "RemoteAudioSource::OnData");
Markus Handell6fcd0f82020-07-07 17:08:53156 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 00:37:01157 for (auto* sink : sinks_) {
Minyue Li99d6d812020-01-29 09:25:12158 // When peerconnection acts as an audio source, it should not provide
159 // absolute capture timestamp.
Tommif888bb52015-12-12 00:37:01160 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
Minyue Li99d6d812020-01-29 09:25:12161 audio.samples_per_channel,
162 /*absolute_capture_timestamp_ms=*/absl::nullopt);
Tommif888bb52015-12-12 00:37:01163 }
164}
165
Taylor Brandstetterba29c6a2016-06-27 23:30:35166void RemoteAudioSource::OnAudioChannelGone() {
Henrik Boströmc335b0e2021-04-08 05:25:38167 if (on_audio_channel_gone_action_ != OnAudioChannelGoneAction::kEnd) {
168 return;
169 }
Danil Chapovalovc6c346d2022-08-22 08:22:40170 // Called when the audio channel is deleted. It may be the worker thread or
171 // may be a different task queue.
172 // This object needs to live long enough for the cleanup logic in the posted
173 // task to run, so take a reference to it. Sometimes the task may not be
174 // processed (because the task queue was destroyed shortly after this call),
175 // but that is fine because the task queue destructor will take care of
176 // destroying task which will release the reference on RemoteAudioSource.
177 rtc::scoped_refptr<RemoteAudioSource> thiz(this);
178 main_thread_->PostTask([thiz = std::move(thiz)] {
179 thiz->sinks_.clear();
180 thiz->SetState(MediaSourceInterface::kEnded);
181 });
Tommif888bb52015-12-12 00:37:01182}
183
wu@webrtc.orgb9a088b2014-02-13 23:18:49184} // namespace webrtc