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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/remoteaudiosource.h" |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <functional> |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 15 | #include <memory> |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 16 | #include <utility> |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/constructormagic.h" |
| 20 | #include "rtc_base/logging.h" |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 21 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/thread.h" |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 26 | class RemoteAudioSource::Sink : public AudioSinkInterface { |
| 27 | public: |
| 28 | explicit Sink(RemoteAudioSource* source) : source_(source) {} |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 29 | ~Sink() override { source_->OnAudioChannelGone(); } |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 30 | |
| 31 | private: |
| 32 | void OnData(const AudioSinkInterface::Data& audio) override { |
| 33 | if (source_) |
| 34 | source_->OnData(audio); |
| 35 | } |
| 36 | |
| 37 | const rtc::scoped_refptr<RemoteAudioSource> source_; |
| 38 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sink); |
| 39 | }; |
| 40 | |
| 41 | rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 42 | rtc::Thread* worker_thread, |
| 43 | cricket::VoiceMediaChannel* media_channel, |
| 44 | uint32_t ssrc) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 45 | rtc::scoped_refptr<RemoteAudioSource> ret( |
| 46 | new rtc::RefCountedObject<RemoteAudioSource>()); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 47 | ret->Initialize(worker_thread, media_channel, ssrc); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 48 | return ret; |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 51 | RemoteAudioSource::RemoteAudioSource() |
| 52 | : main_thread_(rtc::Thread::Current()), |
| 53 | state_(MediaSourceInterface::kLive) { |
| 54 | RTC_DCHECK(main_thread_); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | RemoteAudioSource::~RemoteAudioSource() { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 58 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 59 | RTC_DCHECK(audio_observers_.empty()); |
| 60 | RTC_DCHECK(sinks_.empty()); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 63 | void RemoteAudioSource::Initialize(rtc::Thread* worker_thread, |
| 64 | cricket::VoiceMediaChannel* media_channel, |
| 65 | uint32_t ssrc) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 66 | RTC_DCHECK(main_thread_->IsCurrent()); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 67 | // To make sure we always get notified when the channel goes out of scope, |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 68 | // we register for callbacks here and not on demand in AddSink. |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 69 | if (media_channel) { // May be null in tests. |
| 70 | worker_thread->Invoke<void>(RTC_FROM_HERE, [&] { |
| 71 | media_channel->SetRawAudioSink(ssrc, rtc::MakeUnique<Sink>(this)); |
| 72 | }); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 76 | MediaSourceInterface::SourceState RemoteAudioSource::state() const { |
| 77 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 78 | return state_; |
| 79 | } |
| 80 | |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 81 | bool RemoteAudioSource::remote() const { |
| 82 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 83 | return true; |
| 84 | } |
| 85 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 86 | void RemoteAudioSource::SetVolume(double volume) { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 87 | RTC_DCHECK_GE(volume, 0); |
| 88 | RTC_DCHECK_LE(volume, 10); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 89 | for (auto* observer : audio_observers_) |
| 90 | observer->OnSetVolume(volume); |
| 91 | } |
| 92 | |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 93 | void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 94 | RTC_DCHECK(observer != NULL); |
| 95 | RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(), |
| 96 | observer) == audio_observers_.end()); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 97 | audio_observers_.push_back(observer); |
| 98 | } |
| 99 | |
| 100 | void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 101 | RTC_DCHECK(observer != NULL); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 102 | audio_observers_.remove(observer); |
| 103 | } |
| 104 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 105 | void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) { |
| 106 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 107 | RTC_DCHECK(sink); |
| 108 | |
| 109 | if (state_ != MediaSourceInterface::kLive) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 110 | 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] | 111 | return; |
| 112 | } |
| 113 | |
| 114 | rtc::CritScope lock(&sink_lock_); |
| 115 | RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end()); |
| 116 | sinks_.push_back(sink); |
| 117 | } |
| 118 | |
| 119 | void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) { |
| 120 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 121 | RTC_DCHECK(sink); |
| 122 | |
| 123 | rtc::CritScope lock(&sink_lock_); |
| 124 | sinks_.remove(sink); |
| 125 | } |
| 126 | |
| 127 | void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) { |
| 128 | // Called on the externally-owned audio callback thread, via/from webrtc. |
| 129 | rtc::CritScope lock(&sink_lock_); |
| 130 | for (auto* sink : sinks_) { |
| 131 | sink->OnData(audio.data, 16, audio.sample_rate, audio.channels, |
| 132 | audio.samples_per_channel); |
| 133 | } |
| 134 | } |
| 135 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 136 | void RemoteAudioSource::OnAudioChannelGone() { |
| 137 | // Called when the audio channel is deleted. It may be the worker thread |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 138 | // in libjingle or may be a different worker thread. |
Steve Anton | 3b80aac | 2017-10-19 10:17:12 -0700 | [diff] [blame] | 139 | // This object needs to live long enough for the cleanup logic in OnMessage to |
| 140 | // run, so take a reference to it as the data. Sometimes the message may not |
| 141 | // be processed (because the thread was destroyed shortly after this call), |
| 142 | // but that is fine because the thread destructor will take care of destroying |
| 143 | // the message data which will release the reference on RemoteAudioSource. |
| 144 | main_thread_->Post(RTC_FROM_HERE, this, 0, |
| 145 | new rtc::ScopedRefMessageData<RemoteAudioSource>(this)); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void RemoteAudioSource::OnMessage(rtc::Message* msg) { |
| 149 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 150 | sinks_.clear(); |
| 151 | state_ = MediaSourceInterface::kEnded; |
| 152 | FireOnChanged(); |
Steve Anton | 3b80aac | 2017-10-19 10:17:12 -0700 | [diff] [blame] | 153 | // Will possibly delete this RemoteAudioSource since it is reference counted |
| 154 | // in the message. |
| 155 | delete msg->pdata; |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 156 | } |
| 157 | |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 158 | } // namespace webrtc |