blob: 53a972619abdb1f717cea5989a01ddb4f915e385 [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
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000014#include <algorithm>
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <string>
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000016
Karl Wiberg918f50c2018-07-05 11:40:33 +020017#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "rtc_base/constructormagic.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "rtc_base/location.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "rtc_base/scoped_ref_ptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "rtc_base/thread_checker.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000025
26namespace webrtc {
27
Steve Antond3679212018-01-17 17:41:02 -080028// This proxy is passed to the underlying media engine to receive audio data as
29// they come in. The data will then be passed back up to the RemoteAudioSource
30// which will fan it out to all the sinks that have been added to it.
31class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
Tommif888bb52015-12-12 01:37:01 +010032 public:
Steve Antond3679212018-01-17 17:41:02 -080033 explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
34 RTC_DCHECK(source);
35 }
36 ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 01:37:01 +010037
Steve Antond3679212018-01-17 17:41:02 -080038 // AudioSinkInterface implementation.
Tommif888bb52015-12-12 01:37:01 +010039 void OnData(const AudioSinkInterface::Data& audio) override {
Steve Antond3679212018-01-17 17:41:02 -080040 source_->OnData(audio);
Tommif888bb52015-12-12 01:37:01 +010041 }
42
Steve Antond3679212018-01-17 17:41:02 -080043 private:
Tommif888bb52015-12-12 01:37:01 +010044 const rtc::scoped_refptr<RemoteAudioSource> source_;
Steve Antond3679212018-01-17 17:41:02 -080045
46 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDataProxy);
Tommif888bb52015-12-12 01:37:01 +010047};
48
Steve Antond3679212018-01-17 17:41:02 -080049RemoteAudioSource::RemoteAudioSource(rtc::Thread* worker_thread)
Tommif888bb52015-12-12 01:37:01 +010050 : main_thread_(rtc::Thread::Current()),
Steve Antond3679212018-01-17 17:41:02 -080051 worker_thread_(worker_thread),
Tommif888bb52015-12-12 01:37:01 +010052 state_(MediaSourceInterface::kLive) {
53 RTC_DCHECK(main_thread_);
Steve Antond3679212018-01-17 17:41:02 -080054 RTC_DCHECK(worker_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 Antond3679212018-01-17 17:41:02 -080063void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
64 uint32_t ssrc) {
65 RTC_DCHECK_RUN_ON(main_thread_);
66 RTC_DCHECK(media_channel);
67 // Register for callbacks immediately before AddSink so that we always get
68 // notified when a channel goes out of scope (signaled when "AudioDataProxy"
69 // is destroyed).
70 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
Karl Wiberg918f50c2018-07-05 11:40:33 +020071 media_channel->SetRawAudioSink(ssrc,
72 absl::make_unique<AudioDataProxy>(this));
Steve Antond3679212018-01-17 17:41:02 -080073 });
74}
75
76void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
77 uint32_t ssrc) {
78 RTC_DCHECK_RUN_ON(main_thread_);
79 RTC_DCHECK(media_channel);
80 worker_thread_->Invoke<void>(
81 RTC_FROM_HERE, [&] { media_channel->SetRawAudioSink(ssrc, nullptr); });
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000082}
83
Tommif888bb52015-12-12 01:37:01 +010084MediaSourceInterface::SourceState RemoteAudioSource::state() const {
85 RTC_DCHECK(main_thread_->IsCurrent());
86 return state_;
87}
88
tommi6eca7e32015-12-15 04:27:11 -080089bool RemoteAudioSource::remote() const {
90 RTC_DCHECK(main_thread_->IsCurrent());
91 return true;
92}
93
Tommif888bb52015-12-12 01:37:01 +010094void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -070095 RTC_DCHECK_GE(volume, 0);
96 RTC_DCHECK_LE(volume, 10);
Steve Antond3679212018-01-17 17:41:02 -080097 for (auto* observer : audio_observers_) {
Tommif888bb52015-12-12 01:37:01 +010098 observer->OnSetVolume(volume);
Steve Antond3679212018-01-17 17:41:02 -080099 }
Tommif888bb52015-12-12 01:37:01 +0100100}
101
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000102void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100103 RTC_DCHECK(observer != NULL);
104 RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(),
105 observer) == audio_observers_.end());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000106 audio_observers_.push_back(observer);
107}
108
109void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100110 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000111 audio_observers_.remove(observer);
112}
113
Tommif888bb52015-12-12 01:37:01 +0100114void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
115 RTC_DCHECK(main_thread_->IsCurrent());
116 RTC_DCHECK(sink);
117
118 if (state_ != MediaSourceInterface::kLive) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100119 RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
Tommif888bb52015-12-12 01:37:01 +0100120 return;
121 }
122
123 rtc::CritScope lock(&sink_lock_);
124 RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end());
125 sinks_.push_back(sink);
126}
127
128void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
129 RTC_DCHECK(main_thread_->IsCurrent());
130 RTC_DCHECK(sink);
131
132 rtc::CritScope lock(&sink_lock_);
133 sinks_.remove(sink);
134}
135
136void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
137 // Called on the externally-owned audio callback thread, via/from webrtc.
138 rtc::CritScope lock(&sink_lock_);
139 for (auto* sink : sinks_) {
140 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
141 audio.samples_per_channel);
142 }
143}
144
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700145void RemoteAudioSource::OnAudioChannelGone() {
146 // Called when the audio channel is deleted. It may be the worker thread
Tommif888bb52015-12-12 01:37:01 +0100147 // in libjingle or may be a different worker thread.
Steve Anton3b80aac2017-10-19 10:17:12 -0700148 // This object needs to live long enough for the cleanup logic in OnMessage to
149 // run, so take a reference to it as the data. Sometimes the message may not
150 // be processed (because the thread was destroyed shortly after this call),
151 // but that is fine because the thread destructor will take care of destroying
152 // the message data which will release the reference on RemoteAudioSource.
153 main_thread_->Post(RTC_FROM_HERE, this, 0,
154 new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
Tommif888bb52015-12-12 01:37:01 +0100155}
156
157void RemoteAudioSource::OnMessage(rtc::Message* msg) {
158 RTC_DCHECK(main_thread_->IsCurrent());
159 sinks_.clear();
160 state_ = MediaSourceInterface::kEnded;
161 FireOnChanged();
Steve Anton3b80aac2017-10-19 10:17:12 -0700162 // Will possibly delete this RemoteAudioSource since it is reference counted
163 // in the message.
164 delete msg->pdata;
Tommif888bb52015-12-12 01:37:01 +0100165}
166
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000167} // namespace webrtc