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