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