wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2014 Google Inc. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "talk/app/webrtc/remoteaudiosource.h" |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <functional> |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame^] | 32 | #include <utility> |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 33 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame^] | 34 | #include "talk/app/webrtc/mediastreamprovider.h" |
| 35 | #include "webrtc/base/checks.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 36 | #include "webrtc/base/logging.h" |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame^] | 37 | #include "webrtc/base/thread.h" |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 38 | |
| 39 | namespace webrtc { |
| 40 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame^] | 41 | class RemoteAudioSource::MessageHandler : public rtc::MessageHandler { |
| 42 | public: |
| 43 | explicit MessageHandler(RemoteAudioSource* source) : source_(source) {} |
| 44 | |
| 45 | private: |
| 46 | ~MessageHandler() override {} |
| 47 | |
| 48 | void OnMessage(rtc::Message* msg) override { |
| 49 | source_->OnMessage(msg); |
| 50 | delete this; |
| 51 | } |
| 52 | |
| 53 | const rtc::scoped_refptr<RemoteAudioSource> source_; |
| 54 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MessageHandler); |
| 55 | }; |
| 56 | |
| 57 | class RemoteAudioSource::Sink : public AudioSinkInterface { |
| 58 | public: |
| 59 | explicit Sink(RemoteAudioSource* source) : source_(source) {} |
| 60 | ~Sink() override { source_->OnAudioProviderGone(); } |
| 61 | |
| 62 | private: |
| 63 | void OnData(const AudioSinkInterface::Data& audio) override { |
| 64 | if (source_) |
| 65 | source_->OnData(audio); |
| 66 | } |
| 67 | |
| 68 | const rtc::scoped_refptr<RemoteAudioSource> source_; |
| 69 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sink); |
| 70 | }; |
| 71 | |
| 72 | rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create( |
| 73 | uint32_t ssrc, |
| 74 | AudioProviderInterface* provider) { |
| 75 | rtc::scoped_refptr<RemoteAudioSource> ret( |
| 76 | new rtc::RefCountedObject<RemoteAudioSource>()); |
| 77 | ret->Initialize(ssrc, provider); |
| 78 | return ret; |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame^] | 81 | RemoteAudioSource::RemoteAudioSource() |
| 82 | : main_thread_(rtc::Thread::Current()), |
| 83 | state_(MediaSourceInterface::kLive) { |
| 84 | RTC_DCHECK(main_thread_); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | RemoteAudioSource::~RemoteAudioSource() { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame^] | 88 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 89 | RTC_DCHECK(audio_observers_.empty()); |
| 90 | RTC_DCHECK(sinks_.empty()); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame^] | 93 | void RemoteAudioSource::Initialize(uint32_t ssrc, |
| 94 | AudioProviderInterface* provider) { |
| 95 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 96 | // To make sure we always get notified when the provider goes out of scope, |
| 97 | // we register for callbacks here and not on demand in AddSink. |
| 98 | if (provider) { // May be null in tests. |
| 99 | provider->SetRawAudioSink( |
| 100 | ssrc, std::move(rtc::scoped_ptr<AudioSinkInterface>(new Sink(this)))); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame^] | 104 | MediaSourceInterface::SourceState RemoteAudioSource::state() const { |
| 105 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 106 | return state_; |
| 107 | } |
| 108 | |
| 109 | void RemoteAudioSource::SetVolume(double volume) { |
| 110 | RTC_DCHECK(volume >= 0 && volume <= 10); |
| 111 | for (auto* observer : audio_observers_) |
| 112 | observer->OnSetVolume(volume); |
| 113 | } |
| 114 | |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 115 | void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame^] | 116 | RTC_DCHECK(observer != NULL); |
| 117 | RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(), |
| 118 | observer) == audio_observers_.end()); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 119 | audio_observers_.push_back(observer); |
| 120 | } |
| 121 | |
| 122 | void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame^] | 123 | RTC_DCHECK(observer != NULL); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 124 | audio_observers_.remove(observer); |
| 125 | } |
| 126 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame^] | 127 | void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) { |
| 128 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 129 | RTC_DCHECK(sink); |
| 130 | |
| 131 | if (state_ != MediaSourceInterface::kLive) { |
| 132 | LOG(LS_ERROR) << "Can't register sink as the source isn't live."; |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | rtc::CritScope lock(&sink_lock_); |
| 137 | RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end()); |
| 138 | sinks_.push_back(sink); |
| 139 | } |
| 140 | |
| 141 | void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) { |
| 142 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 143 | RTC_DCHECK(sink); |
| 144 | |
| 145 | rtc::CritScope lock(&sink_lock_); |
| 146 | sinks_.remove(sink); |
| 147 | } |
| 148 | |
| 149 | void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) { |
| 150 | // Called on the externally-owned audio callback thread, via/from webrtc. |
| 151 | rtc::CritScope lock(&sink_lock_); |
| 152 | for (auto* sink : sinks_) { |
| 153 | sink->OnData(audio.data, 16, audio.sample_rate, audio.channels, |
| 154 | audio.samples_per_channel); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void RemoteAudioSource::OnAudioProviderGone() { |
| 159 | // Called when the data provider is deleted. It may be the worker thread |
| 160 | // in libjingle or may be a different worker thread. |
| 161 | main_thread_->Post(new MessageHandler(this)); |
| 162 | } |
| 163 | |
| 164 | void RemoteAudioSource::OnMessage(rtc::Message* msg) { |
| 165 | RTC_DCHECK(main_thread_->IsCurrent()); |
| 166 | sinks_.clear(); |
| 167 | state_ = MediaSourceInterface::kEnded; |
| 168 | FireOnChanged(); |
| 169 | } |
| 170 | |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 171 | } // namespace webrtc |