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