xref: /aosp_15_r20/external/webrtc/pc/remote_audio_source.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2014 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "pc/remote_audio_source.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #include <memory>
16*d9f75844SAndroid Build Coastguard Worker #include <string>
17*d9f75844SAndroid Build Coastguard Worker #include <utility>
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker #include "absl/algorithm/container.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
21*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
22*d9f75844SAndroid Build Coastguard Worker #include "api/task_queue/task_queue_base.h"
23*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
24*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/logging.h"
25*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/strings/string_format.h"
26*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/trace_event.h"
27*d9f75844SAndroid Build Coastguard Worker 
28*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
29*d9f75844SAndroid Build Coastguard Worker 
30*d9f75844SAndroid Build Coastguard Worker // This proxy is passed to the underlying media engine to receive audio data as
31*d9f75844SAndroid Build Coastguard Worker // they come in. The data will then be passed back up to the RemoteAudioSource
32*d9f75844SAndroid Build Coastguard Worker // which will fan it out to all the sinks that have been added to it.
33*d9f75844SAndroid Build Coastguard Worker class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
34*d9f75844SAndroid Build Coastguard Worker  public:
AudioDataProxy(RemoteAudioSource * source)35*d9f75844SAndroid Build Coastguard Worker   explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
36*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK(source);
37*d9f75844SAndroid Build Coastguard Worker   }
38*d9f75844SAndroid Build Coastguard Worker 
39*d9f75844SAndroid Build Coastguard Worker   AudioDataProxy() = delete;
40*d9f75844SAndroid Build Coastguard Worker   AudioDataProxy(const AudioDataProxy&) = delete;
41*d9f75844SAndroid Build Coastguard Worker   AudioDataProxy& operator=(const AudioDataProxy&) = delete;
42*d9f75844SAndroid Build Coastguard Worker 
~AudioDataProxy()43*d9f75844SAndroid Build Coastguard Worker   ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
44*d9f75844SAndroid Build Coastguard Worker 
45*d9f75844SAndroid Build Coastguard Worker   // AudioSinkInterface implementation.
OnData(const AudioSinkInterface::Data & audio)46*d9f75844SAndroid Build Coastguard Worker   void OnData(const AudioSinkInterface::Data& audio) override {
47*d9f75844SAndroid Build Coastguard Worker     source_->OnData(audio);
48*d9f75844SAndroid Build Coastguard Worker   }
49*d9f75844SAndroid Build Coastguard Worker 
50*d9f75844SAndroid Build Coastguard Worker  private:
51*d9f75844SAndroid Build Coastguard Worker   const rtc::scoped_refptr<RemoteAudioSource> source_;
52*d9f75844SAndroid Build Coastguard Worker };
53*d9f75844SAndroid Build Coastguard Worker 
RemoteAudioSource(TaskQueueBase * worker_thread,OnAudioChannelGoneAction on_audio_channel_gone_action)54*d9f75844SAndroid Build Coastguard Worker RemoteAudioSource::RemoteAudioSource(
55*d9f75844SAndroid Build Coastguard Worker     TaskQueueBase* worker_thread,
56*d9f75844SAndroid Build Coastguard Worker     OnAudioChannelGoneAction on_audio_channel_gone_action)
57*d9f75844SAndroid Build Coastguard Worker     : main_thread_(TaskQueueBase::Current()),
58*d9f75844SAndroid Build Coastguard Worker       worker_thread_(worker_thread),
59*d9f75844SAndroid Build Coastguard Worker       on_audio_channel_gone_action_(on_audio_channel_gone_action),
60*d9f75844SAndroid Build Coastguard Worker       state_(MediaSourceInterface::kInitializing) {
61*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(main_thread_);
62*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(worker_thread_);
63*d9f75844SAndroid Build Coastguard Worker }
64*d9f75844SAndroid Build Coastguard Worker 
~RemoteAudioSource()65*d9f75844SAndroid Build Coastguard Worker RemoteAudioSource::~RemoteAudioSource() {
66*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(audio_observers_.empty());
67*d9f75844SAndroid Build Coastguard Worker   if (!sinks_.empty()) {
68*d9f75844SAndroid Build Coastguard Worker     RTC_LOG(LS_WARNING)
69*d9f75844SAndroid Build Coastguard Worker         << "RemoteAudioSource destroyed while sinks_ is non-empty.";
70*d9f75844SAndroid Build Coastguard Worker   }
71*d9f75844SAndroid Build Coastguard Worker }
72*d9f75844SAndroid Build Coastguard Worker 
Start(cricket::VoiceMediaChannel * media_channel,absl::optional<uint32_t> ssrc)73*d9f75844SAndroid Build Coastguard Worker void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
74*d9f75844SAndroid Build Coastguard Worker                               absl::optional<uint32_t> ssrc) {
75*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
76*d9f75844SAndroid Build Coastguard Worker 
77*d9f75844SAndroid Build Coastguard Worker   // Register for callbacks immediately before AddSink so that we always get
78*d9f75844SAndroid Build Coastguard Worker   // notified when a channel goes out of scope (signaled when "AudioDataProxy"
79*d9f75844SAndroid Build Coastguard Worker   // is destroyed).
80*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(media_channel);
81*d9f75844SAndroid Build Coastguard Worker   ssrc ? media_channel->SetRawAudioSink(*ssrc,
82*d9f75844SAndroid Build Coastguard Worker                                         std::make_unique<AudioDataProxy>(this))
83*d9f75844SAndroid Build Coastguard Worker        : media_channel->SetDefaultRawAudioSink(
84*d9f75844SAndroid Build Coastguard Worker              std::make_unique<AudioDataProxy>(this));
85*d9f75844SAndroid Build Coastguard Worker }
86*d9f75844SAndroid Build Coastguard Worker 
Stop(cricket::VoiceMediaChannel * media_channel,absl::optional<uint32_t> ssrc)87*d9f75844SAndroid Build Coastguard Worker void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
88*d9f75844SAndroid Build Coastguard Worker                              absl::optional<uint32_t> ssrc) {
89*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
90*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(media_channel);
91*d9f75844SAndroid Build Coastguard Worker   ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr)
92*d9f75844SAndroid Build Coastguard Worker        : media_channel->SetDefaultRawAudioSink(nullptr);
93*d9f75844SAndroid Build Coastguard Worker }
94*d9f75844SAndroid Build Coastguard Worker 
SetState(SourceState new_state)95*d9f75844SAndroid Build Coastguard Worker void RemoteAudioSource::SetState(SourceState new_state) {
96*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(main_thread_);
97*d9f75844SAndroid Build Coastguard Worker   if (state_ != new_state) {
98*d9f75844SAndroid Build Coastguard Worker     state_ = new_state;
99*d9f75844SAndroid Build Coastguard Worker     FireOnChanged();
100*d9f75844SAndroid Build Coastguard Worker   }
101*d9f75844SAndroid Build Coastguard Worker }
102*d9f75844SAndroid Build Coastguard Worker 
state() const103*d9f75844SAndroid Build Coastguard Worker MediaSourceInterface::SourceState RemoteAudioSource::state() const {
104*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(main_thread_);
105*d9f75844SAndroid Build Coastguard Worker   return state_;
106*d9f75844SAndroid Build Coastguard Worker }
107*d9f75844SAndroid Build Coastguard Worker 
remote() const108*d9f75844SAndroid Build Coastguard Worker bool RemoteAudioSource::remote() const {
109*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(main_thread_);
110*d9f75844SAndroid Build Coastguard Worker   return true;
111*d9f75844SAndroid Build Coastguard Worker }
112*d9f75844SAndroid Build Coastguard Worker 
SetVolume(double volume)113*d9f75844SAndroid Build Coastguard Worker void RemoteAudioSource::SetVolume(double volume) {
114*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(volume, 0);
115*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_LE(volume, 10);
116*d9f75844SAndroid Build Coastguard Worker   RTC_LOG(LS_INFO) << rtc::StringFormat("RAS::%s({volume=%.2f})", __func__,
117*d9f75844SAndroid Build Coastguard Worker                                         volume);
118*d9f75844SAndroid Build Coastguard Worker   for (auto* observer : audio_observers_) {
119*d9f75844SAndroid Build Coastguard Worker     observer->OnSetVolume(volume);
120*d9f75844SAndroid Build Coastguard Worker   }
121*d9f75844SAndroid Build Coastguard Worker }
122*d9f75844SAndroid Build Coastguard Worker 
RegisterAudioObserver(AudioObserver * observer)123*d9f75844SAndroid Build Coastguard Worker void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
124*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(observer != NULL);
125*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
126*d9f75844SAndroid Build Coastguard Worker   audio_observers_.push_back(observer);
127*d9f75844SAndroid Build Coastguard Worker }
128*d9f75844SAndroid Build Coastguard Worker 
UnregisterAudioObserver(AudioObserver * observer)129*d9f75844SAndroid Build Coastguard Worker void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
130*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(observer != NULL);
131*d9f75844SAndroid Build Coastguard Worker   audio_observers_.remove(observer);
132*d9f75844SAndroid Build Coastguard Worker }
133*d9f75844SAndroid Build Coastguard Worker 
AddSink(AudioTrackSinkInterface * sink)134*d9f75844SAndroid Build Coastguard Worker void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
135*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(main_thread_);
136*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(sink);
137*d9f75844SAndroid Build Coastguard Worker 
138*d9f75844SAndroid Build Coastguard Worker   MutexLock lock(&sink_lock_);
139*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
140*d9f75844SAndroid Build Coastguard Worker   sinks_.push_back(sink);
141*d9f75844SAndroid Build Coastguard Worker }
142*d9f75844SAndroid Build Coastguard Worker 
RemoveSink(AudioTrackSinkInterface * sink)143*d9f75844SAndroid Build Coastguard Worker void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
144*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(main_thread_);
145*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(sink);
146*d9f75844SAndroid Build Coastguard Worker 
147*d9f75844SAndroid Build Coastguard Worker   MutexLock lock(&sink_lock_);
148*d9f75844SAndroid Build Coastguard Worker   sinks_.remove(sink);
149*d9f75844SAndroid Build Coastguard Worker }
150*d9f75844SAndroid Build Coastguard Worker 
OnData(const AudioSinkInterface::Data & audio)151*d9f75844SAndroid Build Coastguard Worker void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
152*d9f75844SAndroid Build Coastguard Worker   // Called on the externally-owned audio callback thread, via/from webrtc.
153*d9f75844SAndroid Build Coastguard Worker   TRACE_EVENT0("webrtc", "RemoteAudioSource::OnData");
154*d9f75844SAndroid Build Coastguard Worker   MutexLock lock(&sink_lock_);
155*d9f75844SAndroid Build Coastguard Worker   for (auto* sink : sinks_) {
156*d9f75844SAndroid Build Coastguard Worker     // When peerconnection acts as an audio source, it should not provide
157*d9f75844SAndroid Build Coastguard Worker     // absolute capture timestamp.
158*d9f75844SAndroid Build Coastguard Worker     sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
159*d9f75844SAndroid Build Coastguard Worker                  audio.samples_per_channel,
160*d9f75844SAndroid Build Coastguard Worker                  /*absolute_capture_timestamp_ms=*/absl::nullopt);
161*d9f75844SAndroid Build Coastguard Worker   }
162*d9f75844SAndroid Build Coastguard Worker }
163*d9f75844SAndroid Build Coastguard Worker 
OnAudioChannelGone()164*d9f75844SAndroid Build Coastguard Worker void RemoteAudioSource::OnAudioChannelGone() {
165*d9f75844SAndroid Build Coastguard Worker   if (on_audio_channel_gone_action_ != OnAudioChannelGoneAction::kEnd) {
166*d9f75844SAndroid Build Coastguard Worker     return;
167*d9f75844SAndroid Build Coastguard Worker   }
168*d9f75844SAndroid Build Coastguard Worker   // Called when the audio channel is deleted. It may be the worker thread or
169*d9f75844SAndroid Build Coastguard Worker   // may be a different task queue.
170*d9f75844SAndroid Build Coastguard Worker   // This object needs to live long enough for the cleanup logic in the posted
171*d9f75844SAndroid Build Coastguard Worker   // task to run, so take a reference to it. Sometimes the task may not be
172*d9f75844SAndroid Build Coastguard Worker   // processed (because the task queue was destroyed shortly after this call),
173*d9f75844SAndroid Build Coastguard Worker   // but that is fine because the task queue destructor will take care of
174*d9f75844SAndroid Build Coastguard Worker   // destroying task which will release the reference on RemoteAudioSource.
175*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<RemoteAudioSource> thiz(this);
176*d9f75844SAndroid Build Coastguard Worker   main_thread_->PostTask([thiz = std::move(thiz)] {
177*d9f75844SAndroid Build Coastguard Worker     thiz->sinks_.clear();
178*d9f75844SAndroid Build Coastguard Worker     thiz->SetState(MediaSourceInterface::kEnded);
179*d9f75844SAndroid Build Coastguard Worker   });
180*d9f75844SAndroid Build Coastguard Worker }
181*d9f75844SAndroid Build Coastguard Worker 
182*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
183