blob: c484095fbf1e9f2ca0040e89defeab7f86836905 [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"
21#include "rtc_base/thread.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000022
23namespace webrtc {
24
Tommif888bb52015-12-12 01:37:01 +010025class RemoteAudioSource::Sink : public AudioSinkInterface {
26 public:
27 explicit Sink(RemoteAudioSource* source) : source_(source) {}
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070028 ~Sink() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 01:37:01 +010029
30 private:
31 void OnData(const AudioSinkInterface::Data& audio) override {
32 if (source_)
33 source_->OnData(audio);
34 }
35
36 const rtc::scoped_refptr<RemoteAudioSource> source_;
37 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sink);
38};
39
40rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create(
41 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070042 cricket::VoiceChannel* channel) {
Tommif888bb52015-12-12 01:37:01 +010043 rtc::scoped_refptr<RemoteAudioSource> ret(
44 new rtc::RefCountedObject<RemoteAudioSource>());
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070045 ret->Initialize(ssrc, channel);
Tommif888bb52015-12-12 01:37:01 +010046 return ret;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000047}
48
Tommif888bb52015-12-12 01:37:01 +010049RemoteAudioSource::RemoteAudioSource()
50 : main_thread_(rtc::Thread::Current()),
51 state_(MediaSourceInterface::kLive) {
52 RTC_DCHECK(main_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
Tommif888bb52015-12-12 01:37:01 +010061void RemoteAudioSource::Initialize(uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070062 cricket::VoiceChannel* channel) {
Tommif888bb52015-12-12 01:37:01 +010063 RTC_DCHECK(main_thread_->IsCurrent());
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070064 // To make sure we always get notified when the channel goes out of scope,
Tommif888bb52015-12-12 01:37:01 +010065 // we register for callbacks here and not on demand in AddSink.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070066 if (channel) { // May be null in tests.
67 channel->SetRawAudioSink(
kwibergd1fe2812016-04-27 06:47:29 -070068 ssrc, std::unique_ptr<AudioSinkInterface>(new Sink(this)));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000069 }
70}
71
Tommif888bb52015-12-12 01:37:01 +010072MediaSourceInterface::SourceState RemoteAudioSource::state() const {
73 RTC_DCHECK(main_thread_->IsCurrent());
74 return state_;
75}
76
tommi6eca7e32015-12-15 04:27:11 -080077bool RemoteAudioSource::remote() const {
78 RTC_DCHECK(main_thread_->IsCurrent());
79 return true;
80}
81
Tommif888bb52015-12-12 01:37:01 +010082void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -070083 RTC_DCHECK_GE(volume, 0);
84 RTC_DCHECK_LE(volume, 10);
Tommif888bb52015-12-12 01:37:01 +010085 for (auto* observer : audio_observers_)
86 observer->OnSetVolume(volume);
87}
88
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000089void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +010090 RTC_DCHECK(observer != NULL);
91 RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(),
92 observer) == audio_observers_.end());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000093 audio_observers_.push_back(observer);
94}
95
96void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +010097 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000098 audio_observers_.remove(observer);
99}
100
Tommif888bb52015-12-12 01:37:01 +0100101void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
102 RTC_DCHECK(main_thread_->IsCurrent());
103 RTC_DCHECK(sink);
104
105 if (state_ != MediaSourceInterface::kLive) {
106 LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
107 return;
108 }
109
110 rtc::CritScope lock(&sink_lock_);
111 RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end());
112 sinks_.push_back(sink);
113}
114
115void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
116 RTC_DCHECK(main_thread_->IsCurrent());
117 RTC_DCHECK(sink);
118
119 rtc::CritScope lock(&sink_lock_);
120 sinks_.remove(sink);
121}
122
123void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
124 // Called on the externally-owned audio callback thread, via/from webrtc.
125 rtc::CritScope lock(&sink_lock_);
126 for (auto* sink : sinks_) {
127 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
128 audio.samples_per_channel);
129 }
130}
131
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700132void RemoteAudioSource::OnAudioChannelGone() {
133 // Called when the audio channel is deleted. It may be the worker thread
Tommif888bb52015-12-12 01:37:01 +0100134 // in libjingle or may be a different worker thread.
Steve Anton3b80aac2017-10-19 10:17:12 -0700135 // This object needs to live long enough for the cleanup logic in OnMessage to
136 // run, so take a reference to it as the data. Sometimes the message may not
137 // be processed (because the thread was destroyed shortly after this call),
138 // but that is fine because the thread destructor will take care of destroying
139 // the message data which will release the reference on RemoteAudioSource.
140 main_thread_->Post(RTC_FROM_HERE, this, 0,
141 new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
Tommif888bb52015-12-12 01:37:01 +0100142}
143
144void RemoteAudioSource::OnMessage(rtc::Message* msg) {
145 RTC_DCHECK(main_thread_->IsCurrent());
146 sinks_.clear();
147 state_ = MediaSourceInterface::kEnded;
148 FireOnChanged();
Steve Anton3b80aac2017-10-19 10:17:12 -0700149 // Will possibly delete this RemoteAudioSource since it is reference counted
150 // in the message.
151 delete msg->pdata;
Tommif888bb52015-12-12 01:37:01 +0100152}
153
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000154} // namespace webrtc