blob: f58e23d8288d078b088ef5a3b8be1f3cca8ee958 [file] [log] [blame]
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2014 The WebRTC project authors. All Rights Reserved.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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:49 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/remoteaudiosource.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000012
13#include <algorithm>
14#include <functional>
kwibergd1fe2812016-04-27 06:47:29 -070015#include <memory>
Tommif888bb52015-12-12 01:37:01 +010016#include <utility>
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "rtc_base/constructormagic.h"
20#include "rtc_base/logging.h"
Steve Anton60776752018-01-10 11:51:34 -080021#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/thread.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000023
24namespace webrtc {
25
Steve Antond3679212018-01-17 17:41:02 -080026// 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 01:37:01 +010030 public:
Steve Antond3679212018-01-17 17:41:02 -080031 explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
32 RTC_DCHECK(source);
33 }
34 ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 01:37:01 +010035
Steve Antond3679212018-01-17 17:41:02 -080036 // AudioSinkInterface implementation.
Tommif888bb52015-12-12 01:37:01 +010037 void OnData(const AudioSinkInterface::Data& audio) override {
Steve Antond3679212018-01-17 17:41:02 -080038 source_->OnData(audio);
Tommif888bb52015-12-12 01:37:01 +010039 }
40
Steve Antond3679212018-01-17 17:41:02 -080041 private:
Tommif888bb52015-12-12 01:37:01 +010042 const rtc::scoped_refptr<RemoteAudioSource> source_;
Steve Antond3679212018-01-17 17:41:02 -080043
44 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDataProxy);
Tommif888bb52015-12-12 01:37:01 +010045};
46
Steve Antond3679212018-01-17 17:41:02 -080047RemoteAudioSource::RemoteAudioSource(rtc::Thread* worker_thread)
Tommif888bb52015-12-12 01:37:01 +010048 : main_thread_(rtc::Thread::Current()),
Steve Antond3679212018-01-17 17:41:02 -080049 worker_thread_(worker_thread),
Tommif888bb52015-12-12 01:37:01 +010050 state_(MediaSourceInterface::kLive) {
51 RTC_DCHECK(main_thread_);
Steve Antond3679212018-01-17 17:41:02 -080052 RTC_DCHECK(worker_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000053}
54
55RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 01:37:01 +010056 RTC_DCHECK(main_thread_->IsCurrent());
57 RTC_DCHECK(audio_observers_.empty());
58 RTC_DCHECK(sinks_.empty());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000059}
60
Steve Antond3679212018-01-17 17:41:02 -080061void 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, [&] {
69 media_channel->SetRawAudioSink(ssrc, rtc::MakeUnique<AudioDataProxy>(this));
70 });
71}
72
73void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
74 uint32_t ssrc) {
75 RTC_DCHECK_RUN_ON(main_thread_);
76 RTC_DCHECK(media_channel);
77 worker_thread_->Invoke<void>(
78 RTC_FROM_HERE, [&] { media_channel->SetRawAudioSink(ssrc, nullptr); });
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000079}
80
Tommif888bb52015-12-12 01:37:01 +010081MediaSourceInterface::SourceState RemoteAudioSource::state() const {
82 RTC_DCHECK(main_thread_->IsCurrent());
83 return state_;
84}
85
tommi6eca7e32015-12-15 04:27:11 -080086bool RemoteAudioSource::remote() const {
87 RTC_DCHECK(main_thread_->IsCurrent());
88 return true;
89}
90
Tommif888bb52015-12-12 01:37:01 +010091void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -070092 RTC_DCHECK_GE(volume, 0);
93 RTC_DCHECK_LE(volume, 10);
Steve Antond3679212018-01-17 17:41:02 -080094 for (auto* observer : audio_observers_) {
Tommif888bb52015-12-12 01:37:01 +010095 observer->OnSetVolume(volume);
Steve Antond3679212018-01-17 17:41:02 -080096 }
Tommif888bb52015-12-12 01:37:01 +010097}
98
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000099void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100100 RTC_DCHECK(observer != NULL);
101 RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(),
102 observer) == audio_observers_.end());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000103 audio_observers_.push_back(observer);
104}
105
106void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100107 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000108 audio_observers_.remove(observer);
109}
110
Tommif888bb52015-12-12 01:37:01 +0100111void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
112 RTC_DCHECK(main_thread_->IsCurrent());
113 RTC_DCHECK(sink);
114
115 if (state_ != MediaSourceInterface::kLive) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
Tommif888bb52015-12-12 01:37:01 +0100117 return;
118 }
119
120 rtc::CritScope lock(&sink_lock_);
121 RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end());
122 sinks_.push_back(sink);
123}
124
125void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
126 RTC_DCHECK(main_thread_->IsCurrent());
127 RTC_DCHECK(sink);
128
129 rtc::CritScope lock(&sink_lock_);
130 sinks_.remove(sink);
131}
132
133void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
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, 16, audio.sample_rate, audio.channels,
138 audio.samples_per_channel);
139 }
140}
141
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700142void RemoteAudioSource::OnAudioChannelGone() {
143 // Called when the audio channel is deleted. It may be the worker thread
Tommif888bb52015-12-12 01:37:01 +0100144 // in libjingle or may be a different worker thread.
Steve Anton3b80aac2017-10-19 10:17:12 -0700145 // This object needs to live long enough for the cleanup logic in OnMessage to
146 // run, so take a reference to it as the data. Sometimes the message may not
147 // be processed (because the thread was destroyed shortly after this call),
148 // but that is fine because the thread destructor will take care of destroying
149 // the message data which will release the reference on RemoteAudioSource.
150 main_thread_->Post(RTC_FROM_HERE, this, 0,
151 new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
Tommif888bb52015-12-12 01:37:01 +0100152}
153
154void RemoteAudioSource::OnMessage(rtc::Message* msg) {
155 RTC_DCHECK(main_thread_->IsCurrent());
156 sinks_.clear();
157 state_ = MediaSourceInterface::kEnded;
158 FireOnChanged();
Steve Anton3b80aac2017-10-19 10:17:12 -0700159 // Will possibly delete this RemoteAudioSource since it is reference counted
160 // in the message.
161 delete msg->pdata;
Tommif888bb52015-12-12 01:37:01 +0100162}
163
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000164} // namespace webrtc