| 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: |
| 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 |
| 41 | ~AudioDataProxy() override { source_->OnAudioChannelGone(); } |
| 42 |
| 43 | // AudioSinkInterface implementation. |
| 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 |
| 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 |
| 63 | RemoteAudioSource::~RemoteAudioSource() { |
| 64 | RTC_DCHECK_RUN_ON(main_thread_); |
| 65 | RTC_DCHECK(audio_observers_.empty()); |
| 66 | if (!sinks_.empty()) { |
| 67 | RTC_LOG(LS_WARNING) |
| 68 | << "RemoteAudioSource destroyed while sinks_ is non-empty."; |
| 69 | } |
| 70 | } |
| 71 |
| 72 | void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel, |
| 73 | absl::optional<uint32_t> ssrc) { |
| 74 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 75 |
| 76 | // Register for callbacks immediately before AddSink so that we always get |
| 77 | // notified when a channel goes out of scope (signaled when "AudioDataProxy" |
| 78 | // is destroyed). |
| 79 | RTC_DCHECK(media_channel); |
| 80 | ssrc ? media_channel->SetRawAudioSink(*ssrc, |
| 81 | std::make_unique<AudioDataProxy>(this)) |
| 82 | : media_channel->SetDefaultRawAudioSink( |
| 83 | std::make_unique<AudioDataProxy>(this)); |
| 84 | } |
| 85 |
| 86 | void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel, |
| 87 | absl::optional<uint32_t> ssrc) { |
| 88 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 89 | RTC_DCHECK(media_channel); |
| 90 | ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr) |
| 91 | : media_channel->SetDefaultRawAudioSink(nullptr); |
| 92 | } |
| 93 |
| 94 | void RemoteAudioSource::SetState(SourceState new_state) { |
| 95 | RTC_DCHECK_RUN_ON(main_thread_); |
| 96 | if (state_ != new_state) { |
| 97 | state_ = new_state; |
| 98 | FireOnChanged(); |
| 99 | } |
| 100 | } |
| 101 |
| 102 | MediaSourceInterface::SourceState RemoteAudioSource::state() const { |
| 103 | RTC_DCHECK_RUN_ON(main_thread_); |
| 104 | return state_; |
| 105 | } |
| 106 |
| 107 | bool RemoteAudioSource::remote() const { |
| 108 | RTC_DCHECK_RUN_ON(main_thread_); |
| 109 | return true; |
| 110 | } |
| 111 |
| 112 | void RemoteAudioSource::SetVolume(double volume) { |
| 113 | RTC_DCHECK_GE(volume, 0); |
| 114 | RTC_DCHECK_LE(volume, 10); |
| 115 | RTC_LOG(LS_INFO) << rtc::StringFormat("RAS::%s({volume=%.2f})", __func__, |
| 116 | volume); |
| 117 | for (auto* observer : audio_observers_) { |
| 118 | observer->OnSetVolume(volume); |
| 119 | } |
| 120 | } |
| 121 |
| 122 | void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) { |
| 123 | RTC_DCHECK(observer != NULL); |
| 124 | RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer)); |
| 125 | audio_observers_.push_back(observer); |
| 126 | } |
| 127 |
| 128 | void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) { |
| 129 | RTC_DCHECK(observer != NULL); |
| 130 | audio_observers_.remove(observer); |
| 131 | } |
| 132 |
| 133 | void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) { |
| 134 | RTC_DCHECK_RUN_ON(main_thread_); |
| 135 | RTC_DCHECK(sink); |
| 136 |
| 137 | if (state_ != MediaSourceInterface::kLive) { |
| 138 | RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live."; |
| 139 | return; |
| 140 | } |
| 141 |
| 142 | MutexLock lock(&sink_lock_); |
| 143 | RTC_DCHECK(!absl::c_linear_search(sinks_, sink)); |
| 144 | sinks_.push_back(sink); |
| 145 | } |
| 146 |
| 147 | void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) { |
| 148 | RTC_DCHECK_RUN_ON(main_thread_); |
| 149 | RTC_DCHECK(sink); |
| 150 |
| 151 | MutexLock lock(&sink_lock_); |
| 152 | sinks_.remove(sink); |
| 153 | } |
| 154 |
| 155 | void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) { |
| 156 | // Called on the externally-owned audio callback thread, via/from webrtc. |
| 157 | MutexLock lock(&sink_lock_); |
| 158 | for (auto* sink : sinks_) { |
| 159 | // When peerconnection acts as an audio source, it should not provide |
| 160 | // absolute capture timestamp. |
| 161 | sink->OnData(audio.data, 16, audio.sample_rate, audio.channels, |
| 162 | audio.samples_per_channel, |
| 163 | /*absolute_capture_timestamp_ms=*/absl::nullopt); |
| 164 | } |
| 165 | } |
| 166 |
| 167 | void RemoteAudioSource::OnAudioChannelGone() { |
| 168 | if (on_audio_channel_gone_action_ != OnAudioChannelGoneAction::kEnd) { |
| 169 | return; |
| 170 | } |
| 171 | // Called when the audio channel is deleted. It may be the worker thread |
| 172 | // in libjingle or may be a different worker thread. |
| 173 | // This object needs to live long enough for the cleanup logic in OnMessage to |
| 174 | // run, so take a reference to it as the data. Sometimes the message may not |
| 175 | // be processed (because the thread was destroyed shortly after this call), |
| 176 | // but that is fine because the thread destructor will take care of destroying |
| 177 | // the message data which will release the reference on RemoteAudioSource. |
| 178 | main_thread_->Post(RTC_FROM_HERE, this, 0, |
| 179 | new rtc::ScopedRefMessageData<RemoteAudioSource>(this)); |
| 180 | } |
| 181 |
| 182 | void RemoteAudioSource::OnMessage(rtc::Message* msg) { |
| 183 | RTC_DCHECK_RUN_ON(main_thread_); |
| 184 | sinks_.clear(); |
| 185 | SetState(MediaSourceInterface::kEnded); |
| 186 | // Will possibly delete this RemoteAudioSource since it is reference counted |
| 187 | // in the message. |
| 188 | delete msg->pdata; |
| 189 | } |
| 190 |
| 191 | } // namespace webrtc |
| 192 |