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