xref: /aosp_15_r20/external/webrtc/pc/remote_audio_source.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2014 The WebRTC project authors. All Rights Reserved.
3  *
4  *  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.
9  */
10 
11 #include "pc/remote_audio_source.h"
12 
13 #include <stddef.h>
14 
15 #include <memory>
16 #include <string>
17 #include <utility>
18 
19 #include "absl/algorithm/container.h"
20 #include "api/scoped_refptr.h"
21 #include "api/sequence_checker.h"
22 #include "api/task_queue/task_queue_base.h"
23 #include "rtc_base/checks.h"
24 #include "rtc_base/logging.h"
25 #include "rtc_base/strings/string_format.h"
26 #include "rtc_base/trace_event.h"
27 
28 namespace webrtc {
29 
30 // 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.
33 class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
34  public:
AudioDataProxy(RemoteAudioSource * source)35   explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
36     RTC_DCHECK(source);
37   }
38 
39   AudioDataProxy() = delete;
40   AudioDataProxy(const AudioDataProxy&) = delete;
41   AudioDataProxy& operator=(const AudioDataProxy&) = delete;
42 
~AudioDataProxy()43   ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
44 
45   // AudioSinkInterface implementation.
OnData(const AudioSinkInterface::Data & audio)46   void OnData(const AudioSinkInterface::Data& audio) override {
47     source_->OnData(audio);
48   }
49 
50  private:
51   const rtc::scoped_refptr<RemoteAudioSource> source_;
52 };
53 
RemoteAudioSource(TaskQueueBase * worker_thread,OnAudioChannelGoneAction on_audio_channel_gone_action)54 RemoteAudioSource::RemoteAudioSource(
55     TaskQueueBase* worker_thread,
56     OnAudioChannelGoneAction on_audio_channel_gone_action)
57     : main_thread_(TaskQueueBase::Current()),
58       worker_thread_(worker_thread),
59       on_audio_channel_gone_action_(on_audio_channel_gone_action),
60       state_(MediaSourceInterface::kInitializing) {
61   RTC_DCHECK(main_thread_);
62   RTC_DCHECK(worker_thread_);
63 }
64 
~RemoteAudioSource()65 RemoteAudioSource::~RemoteAudioSource() {
66   RTC_DCHECK(audio_observers_.empty());
67   if (!sinks_.empty()) {
68     RTC_LOG(LS_WARNING)
69         << "RemoteAudioSource destroyed while sinks_ is non-empty.";
70   }
71 }
72 
Start(cricket::VoiceMediaChannel * media_channel,absl::optional<uint32_t> ssrc)73 void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
74                               absl::optional<uint32_t> ssrc) {
75   RTC_DCHECK_RUN_ON(worker_thread_);
76 
77   // 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).
80   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));
85 }
86 
Stop(cricket::VoiceMediaChannel * media_channel,absl::optional<uint32_t> ssrc)87 void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
88                              absl::optional<uint32_t> ssrc) {
89   RTC_DCHECK_RUN_ON(worker_thread_);
90   RTC_DCHECK(media_channel);
91   ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr)
92        : media_channel->SetDefaultRawAudioSink(nullptr);
93 }
94 
SetState(SourceState new_state)95 void RemoteAudioSource::SetState(SourceState new_state) {
96   RTC_DCHECK_RUN_ON(main_thread_);
97   if (state_ != new_state) {
98     state_ = new_state;
99     FireOnChanged();
100   }
101 }
102 
state() const103 MediaSourceInterface::SourceState RemoteAudioSource::state() const {
104   RTC_DCHECK_RUN_ON(main_thread_);
105   return state_;
106 }
107 
remote() const108 bool RemoteAudioSource::remote() const {
109   RTC_DCHECK_RUN_ON(main_thread_);
110   return true;
111 }
112 
SetVolume(double volume)113 void RemoteAudioSource::SetVolume(double volume) {
114   RTC_DCHECK_GE(volume, 0);
115   RTC_DCHECK_LE(volume, 10);
116   RTC_LOG(LS_INFO) << rtc::StringFormat("RAS::%s({volume=%.2f})", __func__,
117                                         volume);
118   for (auto* observer : audio_observers_) {
119     observer->OnSetVolume(volume);
120   }
121 }
122 
RegisterAudioObserver(AudioObserver * observer)123 void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
124   RTC_DCHECK(observer != NULL);
125   RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
126   audio_observers_.push_back(observer);
127 }
128 
UnregisterAudioObserver(AudioObserver * observer)129 void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
130   RTC_DCHECK(observer != NULL);
131   audio_observers_.remove(observer);
132 }
133 
AddSink(AudioTrackSinkInterface * sink)134 void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
135   RTC_DCHECK_RUN_ON(main_thread_);
136   RTC_DCHECK(sink);
137 
138   MutexLock lock(&sink_lock_);
139   RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
140   sinks_.push_back(sink);
141 }
142 
RemoveSink(AudioTrackSinkInterface * sink)143 void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
144   RTC_DCHECK_RUN_ON(main_thread_);
145   RTC_DCHECK(sink);
146 
147   MutexLock lock(&sink_lock_);
148   sinks_.remove(sink);
149 }
150 
OnData(const AudioSinkInterface::Data & audio)151 void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
152   // Called on the externally-owned audio callback thread, via/from webrtc.
153   TRACE_EVENT0("webrtc", "RemoteAudioSource::OnData");
154   MutexLock lock(&sink_lock_);
155   for (auto* sink : sinks_) {
156     // When peerconnection acts as an audio source, it should not provide
157     // absolute capture timestamp.
158     sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
159                  audio.samples_per_channel,
160                  /*absolute_capture_timestamp_ms=*/absl::nullopt);
161   }
162 }
163 
OnAudioChannelGone()164 void RemoteAudioSource::OnAudioChannelGone() {
165   if (on_audio_channel_gone_action_ != OnAudioChannelGoneAction::kEnd) {
166     return;
167   }
168   // 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   });
180 }
181 
182 }  // namespace webrtc
183