blob: a516c576174e957dc6ddc0401b8a7b892552e7fa [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"
Olga Sharonova2d0ba282022-09-27 15:22:34 +020026#include "rtc_base/trace_event.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000027
28namespace webrtc {
29
Steve Antond3679212018-01-17 17:41:02 -080030// 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 01:37:01 +010034 public:
Steve Antond3679212018-01-17 17:41:02 -080035 explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
36 RTC_DCHECK(source);
37 }
Niels Möllerde953292020-09-29 09:46:21 +020038
39 AudioDataProxy() = delete;
40 AudioDataProxy(const AudioDataProxy&) = delete;
41 AudioDataProxy& operator=(const AudioDataProxy&) = delete;
42
Steve Antond3679212018-01-17 17:41:02 -080043 ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 01:37:01 +010044
Steve Antond3679212018-01-17 17:41:02 -080045 // AudioSinkInterface implementation.
Tommif888bb52015-12-12 01:37:01 +010046 void OnData(const AudioSinkInterface::Data& audio) override {
Steve Antond3679212018-01-17 17:41:02 -080047 source_->OnData(audio);
Tommif888bb52015-12-12 01:37:01 +010048 }
49
Steve Antond3679212018-01-17 17:41:02 -080050 private:
Tommif888bb52015-12-12 01:37:01 +010051 const rtc::scoped_refptr<RemoteAudioSource> source_;
Tommif888bb52015-12-12 01:37:01 +010052};
53
Henrik Boströmc335b0e2021-04-08 07:25:38 +020054RemoteAudioSource::RemoteAudioSource(
Danil Chapovalovc6c346d2022-08-22 10:22:40 +020055 TaskQueueBase* worker_thread,
Henrik Boströmc335b0e2021-04-08 07:25:38 +020056 OnAudioChannelGoneAction on_audio_channel_gone_action)
Danil Chapovalovc6c346d2022-08-22 10:22:40 +020057 : main_thread_(TaskQueueBase::Current()),
Steve Antond3679212018-01-17 17:41:02 -080058 worker_thread_(worker_thread),
Henrik Boströmc335b0e2021-04-08 07:25:38 +020059 on_audio_channel_gone_action_(on_audio_channel_gone_action),
Tommi20d8d912022-02-08 21:12:15 +010060 state_(MediaSourceInterface::kInitializing) {
Tommif888bb52015-12-12 01:37:01 +010061 RTC_DCHECK(main_thread_);
Steve Antond3679212018-01-17 17:41:02 -080062 RTC_DCHECK(worker_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000063}
64
65RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 01:37:01 +010066 RTC_DCHECK(audio_observers_.empty());
Henrik Boströmfa8a9462021-04-15 10:44:00 +020067 if (!sinks_.empty()) {
68 RTC_LOG(LS_WARNING)
69 << "RemoteAudioSource destroyed while sinks_ is non-empty.";
70 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000071}
72
Harald Alvestrandc0d44d92022-12-13 12:57:24 +000073void RemoteAudioSource::Start(
74 cricket::VoiceMediaReceiveChannelInterface* media_channel,
75 absl::optional<uint32_t> ssrc) {
Tommi4ccdf932021-05-17 14:50:10 +020076 RTC_DCHECK_RUN_ON(worker_thread_);
Ruslan Burakov7ea46052019-02-16 02:07:05 +010077
Steve Antond3679212018-01-17 17:41:02 -080078 // 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).
Tommi4ccdf932021-05-17 14:50:10 +020081 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-17 17:41:02 -080086}
87
Harald Alvestrandc0d44d92022-12-13 12:57:24 +000088void RemoteAudioSource::Stop(
89 cricket::VoiceMediaReceiveChannelInterface* media_channel,
90 absl::optional<uint32_t> ssrc) {
Tommi4ccdf932021-05-17 14:50:10 +020091 RTC_DCHECK_RUN_ON(worker_thread_);
Steve Antond3679212018-01-17 17:41:02 -080092 RTC_DCHECK(media_channel);
Tommi4ccdf932021-05-17 14:50:10 +020093 ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr)
94 : media_channel->SetDefaultRawAudioSink(nullptr);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000095}
96
Henrik Boströmc335b0e2021-04-08 07:25:38 +020097void RemoteAudioSource::SetState(SourceState new_state) {
Tommi4ccdf932021-05-17 14:50:10 +020098 RTC_DCHECK_RUN_ON(main_thread_);
Henrik Boströmc335b0e2021-04-08 07:25:38 +020099 if (state_ != new_state) {
100 state_ = new_state;
101 FireOnChanged();
102 }
103}
104
Tommif888bb52015-12-12 01:37:01 +0100105MediaSourceInterface::SourceState RemoteAudioSource::state() const {
Tommi4ccdf932021-05-17 14:50:10 +0200106 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 01:37:01 +0100107 return state_;
108}
109
tommi6eca7e32015-12-15 04:27:11 -0800110bool RemoteAudioSource::remote() const {
Tommi4ccdf932021-05-17 14:50:10 +0200111 RTC_DCHECK_RUN_ON(main_thread_);
tommi6eca7e32015-12-15 04:27:11 -0800112 return true;
113}
114
Tommif888bb52015-12-12 01:37:01 +0100115void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -0700116 RTC_DCHECK_GE(volume, 0);
117 RTC_DCHECK_LE(volume, 10);
henrikac6cf9022020-07-29 17:20:13 +0200118 RTC_LOG(LS_INFO) << rtc::StringFormat("RAS::%s({volume=%.2f})", __func__,
119 volume);
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
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000125void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100126 RTC_DCHECK(observer != NULL);
Steve Anton64b626b2019-01-28 17:25:26 -0800127 RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000128 audio_observers_.push_back(observer);
129}
130
131void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100132 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000133 audio_observers_.remove(observer);
134}
135
Tommif888bb52015-12-12 01:37:01 +0100136void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
Tommi4ccdf932021-05-17 14:50:10 +0200137 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 01:37:01 +0100138 RTC_DCHECK(sink);
139
Markus Handell6fcd0f82020-07-07 19:08:53 +0200140 MutexLock lock(&sink_lock_);
Steve Anton3d023842019-01-28 19:48:28 -0800141 RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
Tommif888bb52015-12-12 01:37:01 +0100142 sinks_.push_back(sink);
143}
144
145void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
Tommi4ccdf932021-05-17 14:50:10 +0200146 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 01:37:01 +0100147 RTC_DCHECK(sink);
148
Markus Handell6fcd0f82020-07-07 19:08:53 +0200149 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 01:37:01 +0100150 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 15:22:34 +0200155 TRACE_EVENT0("webrtc", "RemoteAudioSource::OnData");
Markus Handell6fcd0f82020-07-07 19:08:53 +0200156 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 01:37:01 +0100157 for (auto* sink : sinks_) {
Minyue Li99d6d812020-01-29 10:25:12 +0100158 // When peerconnection acts as an audio source, it should not provide
159 // absolute capture timestamp.
Tommif888bb52015-12-12 01:37:01 +0100160 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
Minyue Li99d6d812020-01-29 10:25:12 +0100161 audio.samples_per_channel,
162 /*absolute_capture_timestamp_ms=*/absl::nullopt);
Tommif888bb52015-12-12 01:37:01 +0100163 }
164}
165
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700166void RemoteAudioSource::OnAudioChannelGone() {
Henrik Boströmc335b0e2021-04-08 07:25:38 +0200167 if (on_audio_channel_gone_action_ != OnAudioChannelGoneAction::kEnd) {
168 return;
169 }
Danil Chapovalovc6c346d2022-08-22 10:22:40 +0200170 // 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 01:37:01 +0100182}
183
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000184} // namespace webrtc