wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2014 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/remote_audio_source.h" |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 15 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include <string> |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 17 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 18 | #include "absl/algorithm/container.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 19 | #include "api/scoped_refptr.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/constructor_magic.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 22 | #include "rtc_base/location.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
Ruslan Burakov | 7ea4605 | 2019-02-16 02:07:05 +0100 | [diff] [blame] | 24 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/thread.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 26 | #include "rtc_base/thread_checker.h" |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 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 { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 34 | public: |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 35 | explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) { |
| 36 | RTC_DCHECK(source); |
| 37 | } |
| 38 | ~AudioDataProxy() override { source_->OnAudioChannelGone(); } |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 39 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 40 | // AudioSinkInterface implementation. |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 41 | void OnData(const AudioSinkInterface::Data& audio) override { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 42 | source_->OnData(audio); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 45 | private: |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 46 | const rtc::scoped_refptr<RemoteAudioSource> source_; |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 47 | |
| 48 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDataProxy); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 51 | RemoteAudioSource::RemoteAudioSource(rtc::Thread* worker_thread) |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 52 | : main_thread_(rtc::Thread::Current()), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 53 | worker_thread_(worker_thread), |
Ruslan Burakov | 428dcb2 | 2019-04-18 17:49:49 +0200 | [diff] [blame] | 54 | state_(MediaSourceInterface::kLive) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 55 | RTC_DCHECK(main_thread_); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 56 | RTC_DCHECK(worker_thread_); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | RemoteAudioSource::~RemoteAudioSource() { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 60 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 61 | RTC_DCHECK(audio_observers_.empty()); |
| 62 | RTC_DCHECK(sinks_.empty()); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 65 | void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel, |
Saurav Das | 749f660 | 2019-12-04 09:31:36 -0800 | [diff] [blame] | 66 | absl::optional<uint32_t> ssrc) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 67 | RTC_DCHECK_RUN_ON(main_thread_); |
| 68 | RTC_DCHECK(media_channel); |
Ruslan Burakov | 7ea4605 | 2019-02-16 02:07:05 +0100 | [diff] [blame] | 69 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 70 | // Register for callbacks immediately before AddSink so that we always get |
| 71 | // notified when a channel goes out of scope (signaled when "AudioDataProxy" |
| 72 | // is destroyed). |
| 73 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
Saurav Das | 749f660 | 2019-12-04 09:31:36 -0800 | [diff] [blame] | 74 | ssrc ? media_channel->SetRawAudioSink( |
| 75 | *ssrc, std::make_unique<AudioDataProxy>(this)) |
| 76 | : media_channel->SetDefaultRawAudioSink( |
| 77 | std::make_unique<AudioDataProxy>(this)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 78 | }); |
| 79 | } |
| 80 | |
| 81 | void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel, |
Saurav Das | 749f660 | 2019-12-04 09:31:36 -0800 | [diff] [blame] | 82 | absl::optional<uint32_t> ssrc) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 83 | RTC_DCHECK_RUN_ON(main_thread_); |
| 84 | RTC_DCHECK(media_channel); |
Ruslan Burakov | 7ea4605 | 2019-02-16 02:07:05 +0100 | [diff] [blame] | 85 | |
Saurav Das | 749f660 | 2019-12-04 09:31:36 -0800 | [diff] [blame] | 86 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 87 | ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr) |
| 88 | : media_channel->SetDefaultRawAudioSink(nullptr); |
| 89 | }); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 92 | MediaSourceInterface::SourceState RemoteAudioSource::state() const { |
| 93 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 94 | return state_; |
| 95 | } |
| 96 | |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 97 | bool RemoteAudioSource::remote() const { |
| 98 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 99 | return true; |
| 100 | } |
| 101 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 102 | void RemoteAudioSource::SetVolume(double volume) { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 103 | RTC_DCHECK_GE(volume, 0); |
| 104 | RTC_DCHECK_LE(volume, 10); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 105 | for (auto* observer : audio_observers_) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 106 | observer->OnSetVolume(volume); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 107 | } |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 108 | } |
| 109 | |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 110 | void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 111 | RTC_DCHECK(observer != NULL); |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 112 | RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer)); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 113 | audio_observers_.push_back(observer); |
| 114 | } |
| 115 | |
| 116 | void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 117 | RTC_DCHECK(observer != NULL); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 118 | audio_observers_.remove(observer); |
| 119 | } |
| 120 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 121 | void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) { |
| 122 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 123 | RTC_DCHECK(sink); |
| 124 | |
| 125 | if (state_ != MediaSourceInterface::kLive) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 126 | RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live."; |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | |
| 130 | rtc::CritScope lock(&sink_lock_); |
Steve Anton | 3d02384 | 2019-01-28 19:48:28 -0800 | [diff] [blame] | 131 | RTC_DCHECK(!absl::c_linear_search(sinks_, sink)); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 132 | sinks_.push_back(sink); |
| 133 | } |
| 134 | |
| 135 | void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) { |
| 136 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 137 | RTC_DCHECK(sink); |
| 138 | |
| 139 | rtc::CritScope lock(&sink_lock_); |
| 140 | sinks_.remove(sink); |
| 141 | } |
| 142 | |
| 143 | void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) { |
| 144 | // Called on the externally-owned audio callback thread, via/from webrtc. |
| 145 | rtc::CritScope lock(&sink_lock_); |
| 146 | for (auto* sink : sinks_) { |
| 147 | sink->OnData(audio.data, 16, audio.sample_rate, audio.channels, |
| 148 | audio.samples_per_channel); |
| 149 | } |
| 150 | } |
| 151 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 152 | void RemoteAudioSource::OnAudioChannelGone() { |
| 153 | // Called when the audio channel is deleted. It may be the worker thread |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 154 | // in libjingle or may be a different worker thread. |
Steve Anton | 3b80aac | 2017-10-19 10:17:12 -0700 | [diff] [blame] | 155 | // This object needs to live long enough for the cleanup logic in OnMessage to |
| 156 | // run, so take a reference to it as the data. Sometimes the message may not |
| 157 | // be processed (because the thread was destroyed shortly after this call), |
| 158 | // but that is fine because the thread destructor will take care of destroying |
| 159 | // the message data which will release the reference on RemoteAudioSource. |
| 160 | main_thread_->Post(RTC_FROM_HERE, this, 0, |
| 161 | new rtc::ScopedRefMessageData<RemoteAudioSource>(this)); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void RemoteAudioSource::OnMessage(rtc::Message* msg) { |
| 165 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 166 | sinks_.clear(); |
| 167 | state_ = MediaSourceInterface::kEnded; |
| 168 | FireOnChanged(); |
Steve Anton | 3b80aac | 2017-10-19 10:17:12 -0700 | [diff] [blame] | 169 | // Will possibly delete this RemoteAudioSource since it is reference counted |
| 170 | // in the message. |
| 171 | delete msg->pdata; |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 172 | } |
| 173 | |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 174 | } // namespace webrtc |