blob: a260be89fd750f60af7eb7dacfe42e15f1b159ca [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
Tommif888bb52015-12-12 01:37:01 +010026class RemoteAudioSource::Sink : public AudioSinkInterface {
27 public:
28 explicit Sink(RemoteAudioSource* source) : source_(source) {}
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070029 ~Sink() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 01:37:01 +010030
31 private:
32 void OnData(const AudioSinkInterface::Data& audio) override {
33 if (source_)
34 source_->OnData(audio);
35 }
36
37 const rtc::scoped_refptr<RemoteAudioSource> source_;
38 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sink);
39};
40
41rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create(
Steve Anton60776752018-01-10 11:51:34 -080042 rtc::Thread* worker_thread,
43 cricket::VoiceMediaChannel* media_channel,
44 uint32_t ssrc) {
Tommif888bb52015-12-12 01:37:01 +010045 rtc::scoped_refptr<RemoteAudioSource> ret(
46 new rtc::RefCountedObject<RemoteAudioSource>());
Steve Anton60776752018-01-10 11:51:34 -080047 ret->Initialize(worker_thread, media_channel, ssrc);
Tommif888bb52015-12-12 01:37:01 +010048 return ret;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000049}
50
Tommif888bb52015-12-12 01:37:01 +010051RemoteAudioSource::RemoteAudioSource()
52 : main_thread_(rtc::Thread::Current()),
53 state_(MediaSourceInterface::kLive) {
54 RTC_DCHECK(main_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000055}
56
57RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 01:37:01 +010058 RTC_DCHECK(main_thread_->IsCurrent());
59 RTC_DCHECK(audio_observers_.empty());
60 RTC_DCHECK(sinks_.empty());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000061}
62
Steve Anton60776752018-01-10 11:51:34 -080063void RemoteAudioSource::Initialize(rtc::Thread* worker_thread,
64 cricket::VoiceMediaChannel* media_channel,
65 uint32_t ssrc) {
Tommif888bb52015-12-12 01:37:01 +010066 RTC_DCHECK(main_thread_->IsCurrent());
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070067 // To make sure we always get notified when the channel goes out of scope,
Tommif888bb52015-12-12 01:37:01 +010068 // we register for callbacks here and not on demand in AddSink.
Steve Anton60776752018-01-10 11:51:34 -080069 if (media_channel) { // May be null in tests.
70 worker_thread->Invoke<void>(RTC_FROM_HERE, [&] {
71 media_channel->SetRawAudioSink(ssrc, rtc::MakeUnique<Sink>(this));
72 });
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000073 }
74}
75
Tommif888bb52015-12-12 01:37:01 +010076MediaSourceInterface::SourceState RemoteAudioSource::state() const {
77 RTC_DCHECK(main_thread_->IsCurrent());
78 return state_;
79}
80
tommi6eca7e32015-12-15 04:27:11 -080081bool RemoteAudioSource::remote() const {
82 RTC_DCHECK(main_thread_->IsCurrent());
83 return true;
84}
85
Tommif888bb52015-12-12 01:37:01 +010086void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -070087 RTC_DCHECK_GE(volume, 0);
88 RTC_DCHECK_LE(volume, 10);
Tommif888bb52015-12-12 01:37:01 +010089 for (auto* observer : audio_observers_)
90 observer->OnSetVolume(volume);
91}
92
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000093void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +010094 RTC_DCHECK(observer != NULL);
95 RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(),
96 observer) == audio_observers_.end());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000097 audio_observers_.push_back(observer);
98}
99
100void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100101 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000102 audio_observers_.remove(observer);
103}
104
Tommif888bb52015-12-12 01:37:01 +0100105void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
106 RTC_DCHECK(main_thread_->IsCurrent());
107 RTC_DCHECK(sink);
108
109 if (state_ != MediaSourceInterface::kLive) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100110 RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
Tommif888bb52015-12-12 01:37:01 +0100111 return;
112 }
113
114 rtc::CritScope lock(&sink_lock_);
115 RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end());
116 sinks_.push_back(sink);
117}
118
119void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
120 RTC_DCHECK(main_thread_->IsCurrent());
121 RTC_DCHECK(sink);
122
123 rtc::CritScope lock(&sink_lock_);
124 sinks_.remove(sink);
125}
126
127void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
128 // Called on the externally-owned audio callback thread, via/from webrtc.
129 rtc::CritScope lock(&sink_lock_);
130 for (auto* sink : sinks_) {
131 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
132 audio.samples_per_channel);
133 }
134}
135
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700136void RemoteAudioSource::OnAudioChannelGone() {
137 // Called when the audio channel is deleted. It may be the worker thread
Tommif888bb52015-12-12 01:37:01 +0100138 // in libjingle or may be a different worker thread.
Steve Anton3b80aac2017-10-19 10:17:12 -0700139 // This object needs to live long enough for the cleanup logic in OnMessage to
140 // run, so take a reference to it as the data. Sometimes the message may not
141 // be processed (because the thread was destroyed shortly after this call),
142 // but that is fine because the thread destructor will take care of destroying
143 // the message data which will release the reference on RemoteAudioSource.
144 main_thread_->Post(RTC_FROM_HERE, this, 0,
145 new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
Tommif888bb52015-12-12 01:37:01 +0100146}
147
148void RemoteAudioSource::OnMessage(rtc::Message* msg) {
149 RTC_DCHECK(main_thread_->IsCurrent());
150 sinks_.clear();
151 state_ = MediaSourceInterface::kEnded;
152 FireOnChanged();
Steve Anton3b80aac2017-10-19 10:17:12 -0700153 // Will possibly delete this RemoteAudioSource since it is reference counted
154 // in the message.
155 delete msg->pdata;
Tommif888bb52015-12-12 01:37:01 +0100156}
157
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000158} // namespace webrtc