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
17 #include "absl/algorithm/container.h"
18 #include "api/scoped_refptr.h"
19 #include "api/sequence_checker.h"
20 #include "rtc_base/checks.h"
21 #include "rtc_base/location.h"
22 #include "rtc_base/logging.h"
23 #include "rtc_base/strings/string_format.h"
24 #include "rtc_base/thread.h"
25
26 namespace webrtc {
27
28 // This proxy is passed to the underlying media engine to receive audio data as
29 // they come in. The data will then be passed back up to the RemoteAudioSource
30 // which will fan it out to all the sinks that have been added to it.
31 class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
32 public:
AudioDataProxy(RemoteAudioSource * source)33 explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
34 RTC_DCHECK(source);
35 }
36
37 AudioDataProxy() = delete;
38 AudioDataProxy(const AudioDataProxy&) = delete;
39 AudioDataProxy& operator=(const AudioDataProxy&) = delete;
40
~AudioDataProxy()41 ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
42
43 // AudioSinkInterface implementation.
OnData(const AudioSinkInterface::Data & audio)44 void OnData(const AudioSinkInterface::Data& audio) override {
45 source_->OnData(audio);
46 }
47
48 private:
49 const rtc::scoped_refptr<RemoteAudioSource> source_;
50 };
51
RemoteAudioSource(rtc::Thread * worker_thread,OnAudioChannelGoneAction on_audio_channel_gone_action)52 RemoteAudioSource::RemoteAudioSource(
53 rtc::Thread* worker_thread,
54 OnAudioChannelGoneAction on_audio_channel_gone_action)
55 : main_thread_(rtc::Thread::Current()),
56 worker_thread_(worker_thread),
57 on_audio_channel_gone_action_(on_audio_channel_gone_action),
58 state_(MediaSourceInterface::kLive) {
59 RTC_DCHECK(main_thread_);
60 RTC_DCHECK(worker_thread_);
61 }
62
~RemoteAudioSource()63 RemoteAudioSource::~RemoteAudioSource() {
64 RTC_DCHECK(main_thread_->IsCurrent());
65 RTC_DCHECK(audio_observers_.empty());
66 RTC_DCHECK(sinks_.empty());
67 }
68
Start(cricket::VoiceMediaChannel * media_channel,absl::optional<uint32_t> ssrc)69 void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
70 absl::optional<uint32_t> ssrc) {
71 RTC_DCHECK_RUN_ON(main_thread_);
72 RTC_DCHECK(media_channel);
73
74 // Register for callbacks immediately before AddSink so that we always get
75 // notified when a channel goes out of scope (signaled when "AudioDataProxy"
76 // is destroyed).
77 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
78 ssrc ? media_channel->SetRawAudioSink(
79 *ssrc, std::make_unique<AudioDataProxy>(this))
80 : media_channel->SetDefaultRawAudioSink(
81 std::make_unique<AudioDataProxy>(this));
82 });
83 }
84
Stop(cricket::VoiceMediaChannel * media_channel,absl::optional<uint32_t> ssrc)85 void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
86 absl::optional<uint32_t> ssrc) {
87 RTC_DCHECK_RUN_ON(main_thread_);
88 RTC_DCHECK(media_channel);
89
90 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
91 ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr)
92 : media_channel->SetDefaultRawAudioSink(nullptr);
93 });
94 }
95
SetState(SourceState new_state)96 void RemoteAudioSource::SetState(SourceState new_state) {
97 if (state_ != new_state) {
98 state_ = new_state;
99 FireOnChanged();
100 }
101 }
102
state() const103 MediaSourceInterface::SourceState RemoteAudioSource::state() const {
104 RTC_DCHECK(main_thread_->IsCurrent());
105 return state_;
106 }
107
remote() const108 bool RemoteAudioSource::remote() const {
109 RTC_DCHECK(main_thread_->IsCurrent());
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(main_thread_->IsCurrent());
136 RTC_DCHECK(sink);
137
138 if (state_ != MediaSourceInterface::kLive) {
139 RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
140 return;
141 }
142
143 MutexLock lock(&sink_lock_);
144 RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
145 sinks_.push_back(sink);
146 }
147
RemoveSink(AudioTrackSinkInterface * sink)148 void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
149 RTC_DCHECK(main_thread_->IsCurrent());
150 RTC_DCHECK(sink);
151
152 MutexLock lock(&sink_lock_);
153 sinks_.remove(sink);
154 }
155
OnData(const AudioSinkInterface::Data & audio)156 void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
157 // Called on the externally-owned audio callback thread, via/from webrtc.
158 MutexLock lock(&sink_lock_);
159 for (auto* sink : sinks_) {
160 // When peerconnection acts as an audio source, it should not provide
161 // absolute capture timestamp.
162 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
163 audio.samples_per_channel,
164 /*absolute_capture_timestamp_ms=*/absl::nullopt);
165 }
166 }
167
OnAudioChannelGone()168 void RemoteAudioSource::OnAudioChannelGone() {
169 if (on_audio_channel_gone_action_ != OnAudioChannelGoneAction::kEnd) {
170 return;
171 }
172 // Called when the audio channel is deleted. It may be the worker thread
173 // in libjingle or may be a different worker thread.
174 // This object needs to live long enough for the cleanup logic in OnMessage to
175 // run, so take a reference to it as the data. Sometimes the message may not
176 // be processed (because the thread was destroyed shortly after this call),
177 // but that is fine because the thread destructor will take care of destroying
178 // the message data which will release the reference on RemoteAudioSource.
179 main_thread_->Post(RTC_FROM_HERE, this, 0,
180 new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
181 }
182
OnMessage(rtc::Message * msg)183 void RemoteAudioSource::OnMessage(rtc::Message* msg) {
184 RTC_DCHECK(main_thread_->IsCurrent());
185 sinks_.clear();
186 SetState(MediaSourceInterface::kEnded);
187 // Will possibly delete this RemoteAudioSource since it is reference counted
188 // in the message.
189 delete msg->pdata;
190 }
191
192 } // namespace webrtc
193