blob: 94fdb870d1d344a30edaed9c3a3d870fa9224b1f [file] [log] [blame]
wu@webrtc.orgb9a088b2014-02-13 23:18:491/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2014 The WebRTC project authors. All Rights Reserved.
wu@webrtc.orgb9a088b2014-02-13 23:18:493 *
kjellanderb24317b2016-02-10 15:54:434 * 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.orgb9a088b2014-02-13 23:18:499 */
10
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "pc/remoteaudiosource.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:4912
13#include <algorithm>
14#include <functional>
kwibergd1fe2812016-04-27 13:47:2915#include <memory>
Tommif888bb52015-12-12 00:37:0116#include <utility>
wu@webrtc.orgb9a088b2014-02-13 23:18:4917
Karl Wiberg918f50c2018-07-05 09:40:3318#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3119#include "rtc_base/checks.h"
20#include "rtc_base/constructormagic.h"
21#include "rtc_base/logging.h"
22#include "rtc_base/thread.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:4923
24namespace webrtc {
25
Steve Antond3679212018-01-18 01:41:0226// This proxy is passed to the underlying media engine to receive audio data as
27// they come in. The data will then be passed back up to the RemoteAudioSource
28// which will fan it out to all the sinks that have been added to it.
29class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
Tommif888bb52015-12-12 00:37:0130 public:
Steve Antond3679212018-01-18 01:41:0231 explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
32 RTC_DCHECK(source);
33 }
34 ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 00:37:0135
Steve Antond3679212018-01-18 01:41:0236 // AudioSinkInterface implementation.
Tommif888bb52015-12-12 00:37:0137 void OnData(const AudioSinkInterface::Data& audio) override {
Steve Antond3679212018-01-18 01:41:0238 source_->OnData(audio);
Tommif888bb52015-12-12 00:37:0139 }
40
Steve Antond3679212018-01-18 01:41:0241 private:
Tommif888bb52015-12-12 00:37:0142 const rtc::scoped_refptr<RemoteAudioSource> source_;
Steve Antond3679212018-01-18 01:41:0243
44 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDataProxy);
Tommif888bb52015-12-12 00:37:0145};
46
Steve Antond3679212018-01-18 01:41:0247RemoteAudioSource::RemoteAudioSource(rtc::Thread* worker_thread)
Tommif888bb52015-12-12 00:37:0148 : main_thread_(rtc::Thread::Current()),
Steve Antond3679212018-01-18 01:41:0249 worker_thread_(worker_thread),
Tommif888bb52015-12-12 00:37:0150 state_(MediaSourceInterface::kLive) {
51 RTC_DCHECK(main_thread_);
Steve Antond3679212018-01-18 01:41:0252 RTC_DCHECK(worker_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:4953}
54
55RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 00:37:0156 RTC_DCHECK(main_thread_->IsCurrent());
57 RTC_DCHECK(audio_observers_.empty());
58 RTC_DCHECK(sinks_.empty());
wu@webrtc.orgb9a088b2014-02-13 23:18:4959}
60
Steve Antond3679212018-01-18 01:41:0261void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
62 uint32_t ssrc) {
63 RTC_DCHECK_RUN_ON(main_thread_);
64 RTC_DCHECK(media_channel);
65 // Register for callbacks immediately before AddSink so that we always get
66 // notified when a channel goes out of scope (signaled when "AudioDataProxy"
67 // is destroyed).
68 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
Karl Wiberg918f50c2018-07-05 09:40:3369 media_channel->SetRawAudioSink(ssrc,
70 absl::make_unique<AudioDataProxy>(this));
Steve Antond3679212018-01-18 01:41:0271 });
72}
73
74void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
75 uint32_t ssrc) {
76 RTC_DCHECK_RUN_ON(main_thread_);
77 RTC_DCHECK(media_channel);
78 worker_thread_->Invoke<void>(
79 RTC_FROM_HERE, [&] { media_channel->SetRawAudioSink(ssrc, nullptr); });
wu@webrtc.orgb9a088b2014-02-13 23:18:4980}
81
Tommif888bb52015-12-12 00:37:0182MediaSourceInterface::SourceState RemoteAudioSource::state() const {
83 RTC_DCHECK(main_thread_->IsCurrent());
84 return state_;
85}
86
tommi6eca7e32015-12-15 12:27:1187bool RemoteAudioSource::remote() const {
88 RTC_DCHECK(main_thread_->IsCurrent());
89 return true;
90}
91
Tommif888bb52015-12-12 00:37:0192void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-10 00:22:0193 RTC_DCHECK_GE(volume, 0);
94 RTC_DCHECK_LE(volume, 10);
Steve Antond3679212018-01-18 01:41:0295 for (auto* observer : audio_observers_) {
Tommif888bb52015-12-12 00:37:0196 observer->OnSetVolume(volume);
Steve Antond3679212018-01-18 01:41:0297 }
Tommif888bb52015-12-12 00:37:0198}
99
wu@webrtc.orgb9a088b2014-02-13 23:18:49100void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 00:37:01101 RTC_DCHECK(observer != NULL);
102 RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(),
103 observer) == audio_observers_.end());
wu@webrtc.orgb9a088b2014-02-13 23:18:49104 audio_observers_.push_back(observer);
105}
106
107void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 00:37:01108 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49109 audio_observers_.remove(observer);
110}
111
Tommif888bb52015-12-12 00:37:01112void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
113 RTC_DCHECK(main_thread_->IsCurrent());
114 RTC_DCHECK(sink);
115
116 if (state_ != MediaSourceInterface::kLive) {
Mirko Bonadei675513b2017-11-09 10:09:25117 RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
Tommif888bb52015-12-12 00:37:01118 return;
119 }
120
121 rtc::CritScope lock(&sink_lock_);
122 RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end());
123 sinks_.push_back(sink);
124}
125
126void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
127 RTC_DCHECK(main_thread_->IsCurrent());
128 RTC_DCHECK(sink);
129
130 rtc::CritScope lock(&sink_lock_);
131 sinks_.remove(sink);
132}
133
134void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
135 // Called on the externally-owned audio callback thread, via/from webrtc.
136 rtc::CritScope lock(&sink_lock_);
137 for (auto* sink : sinks_) {
138 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
139 audio.samples_per_channel);
140 }
141}
142
Taylor Brandstetterba29c6a2016-06-27 23:30:35143void RemoteAudioSource::OnAudioChannelGone() {
144 // Called when the audio channel is deleted. It may be the worker thread
Tommif888bb52015-12-12 00:37:01145 // in libjingle or may be a different worker thread.
Steve Anton3b80aac2017-10-19 17:17:12146 // This object needs to live long enough for the cleanup logic in OnMessage to
147 // run, so take a reference to it as the data. Sometimes the message may not
148 // be processed (because the thread was destroyed shortly after this call),
149 // but that is fine because the thread destructor will take care of destroying
150 // the message data which will release the reference on RemoteAudioSource.
151 main_thread_->Post(RTC_FROM_HERE, this, 0,
152 new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
Tommif888bb52015-12-12 00:37:01153}
154
155void RemoteAudioSource::OnMessage(rtc::Message* msg) {
156 RTC_DCHECK(main_thread_->IsCurrent());
157 sinks_.clear();
158 state_ = MediaSourceInterface::kEnded;
159 FireOnChanged();
Steve Anton3b80aac2017-10-19 17:17:12160 // Will possibly delete this RemoteAudioSource since it is reference counted
161 // in the message.
162 delete msg->pdata;
Tommif888bb52015-12-12 00:37:01163}
164
wu@webrtc.orgb9a088b2014-02-13 23:18:49165} // namespace webrtc