blob: 1058d1cbf98c6f0b2bd0301f01f2e2c7c97a539d [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
Steve Antond3679212018-01-17 17:41:02 -080073void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
Saurav Das749f6602019-12-04 09:31:36 -080074 absl::optional<uint32_t> ssrc) {
Tommi4ccdf932021-05-17 14:50:10 +020075 RTC_DCHECK_RUN_ON(worker_thread_);
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).
Tommi4ccdf932021-05-17 14:50:10 +020080 RTC_DCHECK(media_channel);
81 ssrc ? media_channel->SetRawAudioSink(*ssrc,
82 std::make_unique<AudioDataProxy>(this))
83 : media_channel->SetDefaultRawAudioSink(
84 std::make_unique<AudioDataProxy>(this));
Steve Antond3679212018-01-17 17:41:02 -080085}
86
87void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
Saurav Das749f6602019-12-04 09:31:36 -080088 absl::optional<uint32_t> ssrc) {
Tommi4ccdf932021-05-17 14:50:10 +020089 RTC_DCHECK_RUN_ON(worker_thread_);
Steve Antond3679212018-01-17 17:41:02 -080090 RTC_DCHECK(media_channel);
Tommi4ccdf932021-05-17 14:50:10 +020091 ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr)
92 : media_channel->SetDefaultRawAudioSink(nullptr);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000093}
94
Henrik Boströmc335b0e2021-04-08 07:25:38 +020095void RemoteAudioSource::SetState(SourceState new_state) {
Tommi4ccdf932021-05-17 14:50:10 +020096 RTC_DCHECK_RUN_ON(main_thread_);
Henrik Boströmc335b0e2021-04-08 07:25:38 +020097 if (state_ != new_state) {
98 state_ = new_state;
99 FireOnChanged();
100 }
101}
102
Tommif888bb52015-12-12 01:37:01 +0100103MediaSourceInterface::SourceState RemoteAudioSource::state() const {
Tommi4ccdf932021-05-17 14:50:10 +0200104 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 01:37:01 +0100105 return state_;
106}
107
tommi6eca7e32015-12-15 04:27:11 -0800108bool RemoteAudioSource::remote() const {
Tommi4ccdf932021-05-17 14:50:10 +0200109 RTC_DCHECK_RUN_ON(main_thread_);
tommi6eca7e32015-12-15 04:27:11 -0800110 return true;
111}
112
Tommif888bb52015-12-12 01:37:01 +0100113void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -0700114 RTC_DCHECK_GE(volume, 0);
115 RTC_DCHECK_LE(volume, 10);
henrikac6cf9022020-07-29 17:20:13 +0200116 RTC_LOG(LS_INFO) << rtc::StringFormat("RAS::%s({volume=%.2f})", __func__,
117 volume);
Steve Antond3679212018-01-17 17:41:02 -0800118 for (auto* observer : audio_observers_) {
Tommif888bb52015-12-12 01:37:01 +0100119 observer->OnSetVolume(volume);
Steve Antond3679212018-01-17 17:41:02 -0800120 }
Tommif888bb52015-12-12 01:37:01 +0100121}
122
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000123void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100124 RTC_DCHECK(observer != NULL);
Steve Anton64b626b2019-01-28 17:25:26 -0800125 RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000126 audio_observers_.push_back(observer);
127}
128
129void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100130 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000131 audio_observers_.remove(observer);
132}
133
Tommif888bb52015-12-12 01:37:01 +0100134void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
Tommi4ccdf932021-05-17 14:50:10 +0200135 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 01:37:01 +0100136 RTC_DCHECK(sink);
137
Markus Handell6fcd0f82020-07-07 19:08:53 +0200138 MutexLock lock(&sink_lock_);
Steve Anton3d023842019-01-28 19:48:28 -0800139 RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
Tommif888bb52015-12-12 01:37:01 +0100140 sinks_.push_back(sink);
141}
142
143void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
Tommi4ccdf932021-05-17 14:50:10 +0200144 RTC_DCHECK_RUN_ON(main_thread_);
Tommif888bb52015-12-12 01:37:01 +0100145 RTC_DCHECK(sink);
146
Markus Handell6fcd0f82020-07-07 19:08:53 +0200147 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 01:37:01 +0100148 sinks_.remove(sink);
149}
150
151void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
152 // Called on the externally-owned audio callback thread, via/from webrtc.
Olga Sharonova2d0ba282022-09-27 15:22:34 +0200153 TRACE_EVENT0("webrtc", "RemoteAudioSource::OnData");
Markus Handell6fcd0f82020-07-07 19:08:53 +0200154 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 01:37:01 +0100155 for (auto* sink : sinks_) {
Minyue Li99d6d812020-01-29 10:25:12 +0100156 // When peerconnection acts as an audio source, it should not provide
157 // absolute capture timestamp.
Tommif888bb52015-12-12 01:37:01 +0100158 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
Minyue Li99d6d812020-01-29 10:25:12 +0100159 audio.samples_per_channel,
160 /*absolute_capture_timestamp_ms=*/absl::nullopt);
Tommif888bb52015-12-12 01:37:01 +0100161 }
162}
163
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700164void RemoteAudioSource::OnAudioChannelGone() {
Henrik Boströmc335b0e2021-04-08 07:25:38 +0200165 if (on_audio_channel_gone_action_ != OnAudioChannelGoneAction::kEnd) {
166 return;
167 }
Danil Chapovalovc6c346d2022-08-22 10:22:40 +0200168 // Called when the audio channel is deleted. It may be the worker thread or
169 // may be a different task queue.
170 // This object needs to live long enough for the cleanup logic in the posted
171 // task to run, so take a reference to it. Sometimes the task may not be
172 // processed (because the task queue was destroyed shortly after this call),
173 // but that is fine because the task queue destructor will take care of
174 // destroying task which will release the reference on RemoteAudioSource.
175 rtc::scoped_refptr<RemoteAudioSource> thiz(this);
176 main_thread_->PostTask([thiz = std::move(thiz)] {
177 thiz->sinks_.clear();
178 thiz->SetState(MediaSourceInterface::kEnded);
179 });
Tommif888bb52015-12-12 01:37:01 +0100180}
181
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000182} // namespace webrtc