Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * libjingle | 2 * libjingle |
| 3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 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 | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 #include "talk/app/webrtc/remoteaudiosource.h" | 28 #include "talk/app/webrtc/remoteaudiosource.h" |
| 29 | 29 |
| 30 #include <algorithm> | 30 #include <algorithm> |
| 31 #include <functional> | 31 #include <functional> |
| 32 | 32 |
| 33 #include "talk/app/webrtc/mediastreamprovider.h" | |
| 34 #include "webrtc/base/checks.h" | |
| 33 #include "webrtc/base/logging.h" | 35 #include "webrtc/base/logging.h" |
| 36 #include "webrtc/base/thread.h" | |
| 34 | 37 |
| 35 namespace webrtc { | 38 namespace webrtc { |
| 36 | 39 |
| 37 rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create() { | 40 class RemoteAudioSource::MessageHandler : public rtc::MessageHandler { |
| 38 return new rtc::RefCountedObject<RemoteAudioSource>(); | 41 public: |
| 42 MessageHandler(RemoteAudioSource* source) : source_(source) {} | |
| 43 | |
| 44 private: | |
| 45 ~MessageHandler() override {} | |
| 46 | |
| 47 void OnMessage(rtc::Message* msg) override { | |
| 48 source_->OnMessage(msg); | |
| 49 delete this; | |
| 50 } | |
| 51 | |
| 52 const rtc::scoped_refptr<RemoteAudioSource> source_; | |
| 53 RTC_DISALLOW_COPY_AND_ASSIGN(MessageHandler); | |
| 54 }; | |
| 55 | |
| 56 rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create( | |
| 57 uint32_t ssrc, | |
| 58 AudioProviderInterface* provider) { | |
| 59 return new rtc::RefCountedObject<RemoteAudioSource>(ssrc, provider); | |
| 39 } | 60 } |
| 40 | 61 |
| 41 RemoteAudioSource::RemoteAudioSource() { | 62 RemoteAudioSource::RemoteAudioSource(uint32_t ssrc, |
| 63 AudioProviderInterface* provider) | |
| 64 : ssrc_(ssrc), provider_(provider), main_thread_(rtc::Thread::Current()), | |
| 65 state_(MediaSourceInterface::kLive) { | |
| 66 RTC_DCHECK(provider_); | |
| 67 RTC_DCHECK(main_thread_); | |
| 42 } | 68 } |
| 43 | 69 |
| 44 RemoteAudioSource::~RemoteAudioSource() { | 70 RemoteAudioSource::~RemoteAudioSource() { |
| 45 ASSERT(audio_observers_.empty()); | 71 RTC_DCHECK(main_thread_->IsCurrent()); |
| 72 RTC_DCHECK(audio_observers_.empty()); | |
| 73 RTC_DCHECK(sinks_.empty()); | |
| 46 } | 74 } |
| 47 | 75 |
| 48 MediaSourceInterface::SourceState RemoteAudioSource::state() const { | 76 MediaSourceInterface::SourceState RemoteAudioSource::state() const { |
| 49 return MediaSourceInterface::kLive; | 77 return state_; |
| 50 } | 78 } |
| 51 | 79 |
| 52 void RemoteAudioSource::SetVolume(double volume) { | 80 void RemoteAudioSource::SetVolume(double volume) { |
| 53 ASSERT(volume >= 0 && volume <= 10); | 81 RTC_DCHECK(volume >= 0 && volume <= 10); |
| 54 for (AudioObserverList::iterator it = audio_observers_.begin(); | 82 for (auto* observer : audio_observers_) { |
| 55 it != audio_observers_.end(); ++it) { | 83 observer->OnSetVolume(volume); |
| 56 (*it)->OnSetVolume(volume); | |
| 57 } | 84 } |
| 58 } | 85 } |
| 59 | 86 |
| 60 void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) { | 87 void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) { |
| 61 ASSERT(observer != NULL); | 88 RTC_DCHECK(observer != NULL); |
| 62 ASSERT(std::find(audio_observers_.begin(), audio_observers_.end(), | 89 RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(), |
| 63 observer) == audio_observers_.end()); | 90 observer) == audio_observers_.end()); |
| 64 audio_observers_.push_back(observer); | 91 audio_observers_.push_back(observer); |
| 65 } | 92 } |
| 66 | 93 |
| 67 void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) { | 94 void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) { |
| 68 ASSERT(observer != NULL); | 95 RTC_DCHECK(observer != NULL); |
| 69 audio_observers_.remove(observer); | 96 audio_observers_.remove(observer); |
| 70 } | 97 } |
| 71 | 98 |
| 99 void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) { | |
| 100 RTC_DCHECK(main_thread_->IsCurrent()); | |
| 101 RTC_DCHECK(sink); | |
| 102 RTC_DCHECK(state_ == MediaSourceInterface::kLive); | |
| 103 | |
| 104 if (!provider_) { | |
| 105 LOG(LS_ERROR) << "No audio provider, so can't set an audio sink."; | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 if (sinks_.empty()) | |
| 110 provider_->SetRawAudioSink(ssrc_, this); | |
| 111 | |
| 112 rtc::CritScope lock(&sink_lock_); | |
| 113 RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end()); | |
| 114 sinks_.push_back(sink); | |
| 115 } | |
| 116 | |
| 117 void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) { | |
| 118 RTC_DCHECK(main_thread_->IsCurrent()); | |
| 119 RTC_DCHECK(sink); | |
| 120 { | |
| 121 rtc::CritScope lock(&sink_lock_); | |
| 122 sinks_.remove(sink); | |
| 123 } | |
| 124 | |
| 125 if (sinks_.empty() && provider_) | |
| 126 provider_->SetRawAudioSink(ssrc_, nullptr); | |
| 127 } | |
| 128 | |
| 129 void RemoteAudioSource::OnData(const void* audio_data, | |
| 130 int bits_per_sample, | |
| 131 int sample_rate, | |
| 132 int number_of_channels, | |
| 133 size_t number_of_frames) { | |
| 134 // Called on the externally-owned audio callback thread, via/from webrtc. | |
| 135 rtc::CritScope lock(&sink_lock_); | |
| 136 for (auto* sink : sinks_) { | |
| 137 sink->OnData(audio_data, bits_per_sample, sample_rate, number_of_channels, | |
| 138 number_of_frames); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void RemoteAudioSource::OnClose() { | |
|
perkj_webrtc
2015/12/10 12:24:05
What triggers this?
tommi
2015/12/10 22:37:25
Deletion of the session. It tears down the voice
| |
| 143 main_thread_->Post(new MessageHandler(this)); | |
|
perkj_webrtc
2015/12/10 12:24:05
use async_invoker instead. You don't need the Mess
tommi
2015/12/10 22:37:25
I started doing that but I'm hesitating. Have you
| |
| 144 } | |
| 145 | |
| 146 void RemoteAudioSource::OnMessage(rtc::Message* msg) { | |
| 147 RTC_DCHECK(main_thread_->IsCurrent()); | |
| 148 provider_ = nullptr; | |
| 149 sinks_.clear(); | |
| 150 state_ = MediaSourceInterface::kEnded; | |
| 151 FireOnChanged(); | |
| 152 } | |
| 153 | |
| 72 } // namespace webrtc | 154 } // namespace webrtc |
| OLD | NEW |