OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/api/remoteaudiosource.h" | 11 #include "webrtc/api/remoteaudiosource.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <functional> | 14 #include <functional> |
15 #include <memory> | 15 #include <memory> |
16 #include <utility> | 16 #include <utility> |
17 | 17 |
| 18 #include "webrtc/api/mediastreamprovider.h" |
18 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
19 #include "webrtc/base/constructormagic.h" | 20 #include "webrtc/base/constructormagic.h" |
20 #include "webrtc/base/logging.h" | 21 #include "webrtc/base/logging.h" |
21 #include "webrtc/base/thread.h" | 22 #include "webrtc/base/thread.h" |
22 | 23 |
23 namespace webrtc { | 24 namespace webrtc { |
24 | 25 |
25 class RemoteAudioSource::MessageHandler : public rtc::MessageHandler { | 26 class RemoteAudioSource::MessageHandler : public rtc::MessageHandler { |
26 public: | 27 public: |
27 explicit MessageHandler(RemoteAudioSource* source) : source_(source) {} | 28 explicit MessageHandler(RemoteAudioSource* source) : source_(source) {} |
28 | 29 |
29 private: | 30 private: |
30 ~MessageHandler() override {} | 31 ~MessageHandler() override {} |
31 | 32 |
32 void OnMessage(rtc::Message* msg) override { | 33 void OnMessage(rtc::Message* msg) override { |
33 source_->OnMessage(msg); | 34 source_->OnMessage(msg); |
34 delete this; | 35 delete this; |
35 } | 36 } |
36 | 37 |
37 const rtc::scoped_refptr<RemoteAudioSource> source_; | 38 const rtc::scoped_refptr<RemoteAudioSource> source_; |
38 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MessageHandler); | 39 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MessageHandler); |
39 }; | 40 }; |
40 | 41 |
41 class RemoteAudioSource::Sink : public AudioSinkInterface { | 42 class RemoteAudioSource::Sink : public AudioSinkInterface { |
42 public: | 43 public: |
43 explicit Sink(RemoteAudioSource* source) : source_(source) {} | 44 explicit Sink(RemoteAudioSource* source) : source_(source) {} |
44 ~Sink() override { source_->OnAudioChannelGone(); } | 45 ~Sink() override { source_->OnAudioProviderGone(); } |
45 | 46 |
46 private: | 47 private: |
47 void OnData(const AudioSinkInterface::Data& audio) override { | 48 void OnData(const AudioSinkInterface::Data& audio) override { |
48 if (source_) | 49 if (source_) |
49 source_->OnData(audio); | 50 source_->OnData(audio); |
50 } | 51 } |
51 | 52 |
52 const rtc::scoped_refptr<RemoteAudioSource> source_; | 53 const rtc::scoped_refptr<RemoteAudioSource> source_; |
53 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sink); | 54 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sink); |
54 }; | 55 }; |
55 | 56 |
56 rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create( | 57 rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create( |
57 uint32_t ssrc, | 58 uint32_t ssrc, |
58 cricket::VoiceChannel* channel) { | 59 AudioProviderInterface* provider) { |
59 rtc::scoped_refptr<RemoteAudioSource> ret( | 60 rtc::scoped_refptr<RemoteAudioSource> ret( |
60 new rtc::RefCountedObject<RemoteAudioSource>()); | 61 new rtc::RefCountedObject<RemoteAudioSource>()); |
61 ret->Initialize(ssrc, channel); | 62 ret->Initialize(ssrc, provider); |
62 return ret; | 63 return ret; |
63 } | 64 } |
64 | 65 |
65 RemoteAudioSource::RemoteAudioSource() | 66 RemoteAudioSource::RemoteAudioSource() |
66 : main_thread_(rtc::Thread::Current()), | 67 : main_thread_(rtc::Thread::Current()), |
67 state_(MediaSourceInterface::kLive) { | 68 state_(MediaSourceInterface::kLive) { |
68 RTC_DCHECK(main_thread_); | 69 RTC_DCHECK(main_thread_); |
69 } | 70 } |
70 | 71 |
71 RemoteAudioSource::~RemoteAudioSource() { | 72 RemoteAudioSource::~RemoteAudioSource() { |
72 RTC_DCHECK(main_thread_->IsCurrent()); | 73 RTC_DCHECK(main_thread_->IsCurrent()); |
73 RTC_DCHECK(audio_observers_.empty()); | 74 RTC_DCHECK(audio_observers_.empty()); |
74 RTC_DCHECK(sinks_.empty()); | 75 RTC_DCHECK(sinks_.empty()); |
75 } | 76 } |
76 | 77 |
77 void RemoteAudioSource::Initialize(uint32_t ssrc, | 78 void RemoteAudioSource::Initialize(uint32_t ssrc, |
78 cricket::VoiceChannel* channel) { | 79 AudioProviderInterface* provider) { |
79 RTC_DCHECK(main_thread_->IsCurrent()); | 80 RTC_DCHECK(main_thread_->IsCurrent()); |
80 // To make sure we always get notified when the channel goes out of scope, | 81 // To make sure we always get notified when the provider goes out of scope, |
81 // we register for callbacks here and not on demand in AddSink. | 82 // we register for callbacks here and not on demand in AddSink. |
82 if (channel) { // May be null in tests. | 83 if (provider) { // May be null in tests. |
83 channel->SetRawAudioSink( | 84 provider->SetRawAudioSink( |
84 ssrc, std::unique_ptr<AudioSinkInterface>(new Sink(this))); | 85 ssrc, std::unique_ptr<AudioSinkInterface>(new Sink(this))); |
85 } | 86 } |
86 } | 87 } |
87 | 88 |
88 MediaSourceInterface::SourceState RemoteAudioSource::state() const { | 89 MediaSourceInterface::SourceState RemoteAudioSource::state() const { |
89 RTC_DCHECK(main_thread_->IsCurrent()); | 90 RTC_DCHECK(main_thread_->IsCurrent()); |
90 return state_; | 91 return state_; |
91 } | 92 } |
92 | 93 |
93 bool RemoteAudioSource::remote() const { | 94 bool RemoteAudioSource::remote() const { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 138 |
138 void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) { | 139 void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) { |
139 // Called on the externally-owned audio callback thread, via/from webrtc. | 140 // Called on the externally-owned audio callback thread, via/from webrtc. |
140 rtc::CritScope lock(&sink_lock_); | 141 rtc::CritScope lock(&sink_lock_); |
141 for (auto* sink : sinks_) { | 142 for (auto* sink : sinks_) { |
142 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels, | 143 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels, |
143 audio.samples_per_channel); | 144 audio.samples_per_channel); |
144 } | 145 } |
145 } | 146 } |
146 | 147 |
147 void RemoteAudioSource::OnAudioChannelGone() { | 148 void RemoteAudioSource::OnAudioProviderGone() { |
148 // Called when the audio channel is deleted. It may be the worker thread | 149 // Called when the data provider is deleted. It may be the worker thread |
149 // in libjingle or may be a different worker thread. | 150 // in libjingle or may be a different worker thread. |
150 main_thread_->Post(RTC_FROM_HERE, new MessageHandler(this)); | 151 main_thread_->Post(RTC_FROM_HERE, new MessageHandler(this)); |
151 } | 152 } |
152 | 153 |
153 void RemoteAudioSource::OnMessage(rtc::Message* msg) { | 154 void RemoteAudioSource::OnMessage(rtc::Message* msg) { |
154 RTC_DCHECK(main_thread_->IsCurrent()); | 155 RTC_DCHECK(main_thread_->IsCurrent()); |
155 sinks_.clear(); | 156 sinks_.clear(); |
156 state_ = MediaSourceInterface::kEnded; | 157 state_ = MediaSourceInterface::kEnded; |
157 FireOnChanged(); | 158 FireOnChanged(); |
158 } | 159 } |
159 | 160 |
160 } // namespace webrtc | 161 } // namespace webrtc |
OLD | NEW |