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 11 matching lines...) Expand all Loading... | |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
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 #include <utility> | |
32 | 33 |
34 #include "talk/app/webrtc/mediastreamprovider.h" | |
35 #include "webrtc/base/checks.h" | |
33 #include "webrtc/base/logging.h" | 36 #include "webrtc/base/logging.h" |
37 #include "webrtc/base/thread.h" | |
34 | 38 |
35 namespace webrtc { | 39 namespace webrtc { |
36 | 40 |
37 rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create() { | 41 class RemoteAudioSource::MessageHandler : public rtc::MessageHandler { |
38 return new rtc::RefCountedObject<RemoteAudioSource>(); | 42 public: |
43 MessageHandler(RemoteAudioSource* source) : source_(source) {} | |
the sun
2015/12/11 19:46:28
explicit
tommi
2015/12/12 00:33:11
Done.
| |
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_COPY_AND_ASSIGN(MessageHandler); | |
the sun
2015/12/11 19:46:29
DISALLOW_IMPLICIT...
tommi
2015/12/12 00:33:11
Done.
| |
55 }; | |
56 | |
57 class RemoteAudioSource::Sink : public AudioSinkInterface { | |
58 public: | |
59 Sink(RemoteAudioSource* source) : source_(source) {} | |
the sun
2015/12/11 19:46:28
explicit
tommi
2015/12/12 00:33:11
Done.
| |
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_; | |
the sun
2015/12/11 19:46:28
DISALLOW_IMPLICIT...
tommi
2015/12/12 00:33:11
Done.
| |
69 }; | |
70 | |
71 rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create( | |
72 uint32_t ssrc, | |
73 AudioProviderInterface* provider) { | |
74 rtc::scoped_refptr<RemoteAudioSource> ret( | |
75 new rtc::RefCountedObject<RemoteAudioSource>()); | |
76 ret->Initialize(ssrc, provider); | |
77 return ret; | |
39 } | 78 } |
40 | 79 |
41 RemoteAudioSource::RemoteAudioSource() { | 80 RemoteAudioSource::RemoteAudioSource() |
81 : main_thread_(rtc::Thread::Current()), | |
82 state_(MediaSourceInterface::kLive) { | |
83 RTC_DCHECK(main_thread_); | |
42 } | 84 } |
43 | 85 |
44 RemoteAudioSource::~RemoteAudioSource() { | 86 RemoteAudioSource::~RemoteAudioSource() { |
45 ASSERT(audio_observers_.empty()); | 87 RTC_DCHECK(main_thread_->IsCurrent()); |
88 RTC_DCHECK(audio_observers_.empty()); | |
89 RTC_DCHECK(sinks_.empty()); | |
90 } | |
91 | |
92 void RemoteAudioSource::Initialize(uint32_t ssrc, | |
93 AudioProviderInterface* provider) { | |
94 RTC_DCHECK(main_thread_->IsCurrent()); | |
95 // To make sure we always get notified when the provider goes out of scope, | |
96 // we register for callbacks here and not on demand in AddSink. | |
97 if (provider) { // May be null in tests. | |
98 provider->SetRawAudioSink( | |
99 ssrc, std::move(rtc::scoped_ptr<AudioSinkInterface>(new Sink(this)))); | |
100 } | |
46 } | 101 } |
47 | 102 |
48 MediaSourceInterface::SourceState RemoteAudioSource::state() const { | 103 MediaSourceInterface::SourceState RemoteAudioSource::state() const { |
49 return MediaSourceInterface::kLive; | 104 RTC_DCHECK(main_thread_->IsCurrent()); |
105 return state_; | |
50 } | 106 } |
51 | 107 |
52 void RemoteAudioSource::SetVolume(double volume) { | 108 void RemoteAudioSource::SetVolume(double volume) { |
53 ASSERT(volume >= 0 && volume <= 10); | 109 RTC_DCHECK(volume >= 0 && volume <= 10); |
54 for (AudioObserverList::iterator it = audio_observers_.begin(); | 110 for (auto* observer : audio_observers_) |
55 it != audio_observers_.end(); ++it) { | 111 observer->OnSetVolume(volume); |
56 (*it)->OnSetVolume(volume); | |
57 } | |
58 } | 112 } |
59 | 113 |
60 void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) { | 114 void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) { |
61 ASSERT(observer != NULL); | 115 RTC_DCHECK(observer != NULL); |
62 ASSERT(std::find(audio_observers_.begin(), audio_observers_.end(), | 116 RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(), |
63 observer) == audio_observers_.end()); | 117 observer) == audio_observers_.end()); |
64 audio_observers_.push_back(observer); | 118 audio_observers_.push_back(observer); |
65 } | 119 } |
66 | 120 |
67 void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) { | 121 void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) { |
68 ASSERT(observer != NULL); | 122 RTC_DCHECK(observer != NULL); |
69 audio_observers_.remove(observer); | 123 audio_observers_.remove(observer); |
70 } | 124 } |
71 | 125 |
126 void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) { | |
127 RTC_DCHECK(main_thread_->IsCurrent()); | |
128 RTC_DCHECK(sink); | |
129 | |
130 if (state_ != MediaSourceInterface::kLive) { | |
131 LOG(LS_ERROR) << "Can't register sink as the source isn't live."; | |
132 return; | |
133 } | |
134 | |
135 rtc::CritScope lock(&sink_lock_); | |
136 RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end()); | |
137 sinks_.push_back(sink); | |
138 } | |
139 | |
140 void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) { | |
141 RTC_DCHECK(main_thread_->IsCurrent()); | |
142 RTC_DCHECK(sink); | |
143 | |
144 rtc::CritScope lock(&sink_lock_); | |
145 sinks_.remove(sink); | |
146 } | |
147 | |
148 void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) { | |
149 // Called on the externally-owned audio callback thread, via/from webrtc. | |
150 rtc::CritScope lock(&sink_lock_); | |
151 for (auto* sink : sinks_) { | |
152 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels, | |
153 audio.samples_per_channel); | |
154 } | |
155 } | |
156 | |
157 void RemoteAudioSource::OnAudioProviderGone() { | |
158 // Called when the data provider is deleted. It may be the worker thread | |
159 // in libjingle or may be a different worker thread. | |
160 main_thread_->Post(new MessageHandler(this)); | |
161 } | |
162 | |
163 void RemoteAudioSource::OnMessage(rtc::Message* msg) { | |
164 RTC_DCHECK(main_thread_->IsCurrent()); | |
165 sinks_.clear(); | |
166 state_ = MediaSourceInterface::kEnded; | |
167 FireOnChanged(); | |
168 } | |
169 | |
72 } // namespace webrtc | 170 } // namespace webrtc |
OLD | NEW |