blob: d9752f3052ac7c66b27242602452912e87a4f0a3 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/remote_audio_source.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
Yves Gerey3e707812018-11-28 16:47:49 +010016#include <string>
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000017
Steve Anton64b626b2019-01-28 17:25:26 -080018#include "absl/algorithm/container.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010019#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/constructor_magic.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "rtc_base/location.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/logging.h"
Ruslan Burakov7ea46052019-02-16 02:07:05 +010024#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "rtc_base/thread_checker.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000027
28namespace webrtc {
29
Steve Antond3679212018-01-17 17:41:02 -080030// This proxy is passed to the underlying media engine to receive audio data as
31// they come in. The data will then be passed back up to the RemoteAudioSource
32// which will fan it out to all the sinks that have been added to it.
33class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
Tommif888bb52015-12-12 01:37:01 +010034 public:
Steve Antond3679212018-01-17 17:41:02 -080035 explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
36 RTC_DCHECK(source);
37 }
38 ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 01:37:01 +010039
Steve Antond3679212018-01-17 17:41:02 -080040 // AudioSinkInterface implementation.
Tommif888bb52015-12-12 01:37:01 +010041 void OnData(const AudioSinkInterface::Data& audio) override {
Steve Antond3679212018-01-17 17:41:02 -080042 source_->OnData(audio);
Tommif888bb52015-12-12 01:37:01 +010043 }
44
Steve Antond3679212018-01-17 17:41:02 -080045 private:
Tommif888bb52015-12-12 01:37:01 +010046 const rtc::scoped_refptr<RemoteAudioSource> source_;
Steve Antond3679212018-01-17 17:41:02 -080047
48 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDataProxy);
Tommif888bb52015-12-12 01:37:01 +010049};
50
Steve Antond3679212018-01-17 17:41:02 -080051RemoteAudioSource::RemoteAudioSource(rtc::Thread* worker_thread)
Tommif888bb52015-12-12 01:37:01 +010052 : main_thread_(rtc::Thread::Current()),
Steve Antond3679212018-01-17 17:41:02 -080053 worker_thread_(worker_thread),
Ruslan Burakov428dcb22019-04-18 17:49:49 +020054 state_(MediaSourceInterface::kLive) {
Tommif888bb52015-12-12 01:37:01 +010055 RTC_DCHECK(main_thread_);
Steve Antond3679212018-01-17 17:41:02 -080056 RTC_DCHECK(worker_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000057}
58
59RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 01:37:01 +010060 RTC_DCHECK(main_thread_->IsCurrent());
61 RTC_DCHECK(audio_observers_.empty());
62 RTC_DCHECK(sinks_.empty());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000063}
64
Steve Antond3679212018-01-17 17:41:02 -080065void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
66 uint32_t ssrc) {
67 RTC_DCHECK_RUN_ON(main_thread_);
68 RTC_DCHECK(media_channel);
Ruslan Burakov7ea46052019-02-16 02:07:05 +010069
Steve Antond3679212018-01-17 17:41:02 -080070 // Register for callbacks immediately before AddSink so that we always get
71 // notified when a channel goes out of scope (signaled when "AudioDataProxy"
72 // is destroyed).
73 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
Karl Wiberg918f50c2018-07-05 11:40:33 +020074 media_channel->SetRawAudioSink(ssrc,
Mirko Bonadei317a1f02019-09-17 17:06:18 +020075 std::make_unique<AudioDataProxy>(this));
Steve Antond3679212018-01-17 17:41:02 -080076 });
77}
78
79void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
80 uint32_t ssrc) {
81 RTC_DCHECK_RUN_ON(main_thread_);
82 RTC_DCHECK(media_channel);
Ruslan Burakov7ea46052019-02-16 02:07:05 +010083
Steve Antond3679212018-01-17 17:41:02 -080084 worker_thread_->Invoke<void>(
85 RTC_FROM_HERE, [&] { media_channel->SetRawAudioSink(ssrc, nullptr); });
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000086}
87
Tommif888bb52015-12-12 01:37:01 +010088MediaSourceInterface::SourceState RemoteAudioSource::state() const {
89 RTC_DCHECK(main_thread_->IsCurrent());
90 return state_;
91}
92
tommi6eca7e32015-12-15 04:27:11 -080093bool RemoteAudioSource::remote() const {
94 RTC_DCHECK(main_thread_->IsCurrent());
95 return true;
96}
97
Tommif888bb52015-12-12 01:37:01 +010098void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -070099 RTC_DCHECK_GE(volume, 0);
100 RTC_DCHECK_LE(volume, 10);
Steve Antond3679212018-01-17 17:41:02 -0800101 for (auto* observer : audio_observers_) {
Tommif888bb52015-12-12 01:37:01 +0100102 observer->OnSetVolume(volume);
Steve Antond3679212018-01-17 17:41:02 -0800103 }
Tommif888bb52015-12-12 01:37:01 +0100104}
105
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000106void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100107 RTC_DCHECK(observer != NULL);
Steve Anton64b626b2019-01-28 17:25:26 -0800108 RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000109 audio_observers_.push_back(observer);
110}
111
112void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100113 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000114 audio_observers_.remove(observer);
115}
116
Tommif888bb52015-12-12 01:37:01 +0100117void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
118 RTC_DCHECK(main_thread_->IsCurrent());
119 RTC_DCHECK(sink);
120
121 if (state_ != MediaSourceInterface::kLive) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100122 RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
Tommif888bb52015-12-12 01:37:01 +0100123 return;
124 }
125
126 rtc::CritScope lock(&sink_lock_);
Steve Anton3d023842019-01-28 19:48:28 -0800127 RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
Tommif888bb52015-12-12 01:37:01 +0100128 sinks_.push_back(sink);
129}
130
131void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
132 RTC_DCHECK(main_thread_->IsCurrent());
133 RTC_DCHECK(sink);
134
135 rtc::CritScope lock(&sink_lock_);
136 sinks_.remove(sink);
137}
138
139void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
140 // Called on the externally-owned audio callback thread, via/from webrtc.
141 rtc::CritScope lock(&sink_lock_);
142 for (auto* sink : sinks_) {
143 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
144 audio.samples_per_channel);
145 }
146}
147
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700148void RemoteAudioSource::OnAudioChannelGone() {
149 // Called when the audio channel is deleted. It may be the worker thread
Tommif888bb52015-12-12 01:37:01 +0100150 // in libjingle or may be a different worker thread.
Steve Anton3b80aac2017-10-19 10:17:12 -0700151 // This object needs to live long enough for the cleanup logic in OnMessage to
152 // run, so take a reference to it as the data. Sometimes the message may not
153 // be processed (because the thread was destroyed shortly after this call),
154 // but that is fine because the thread destructor will take care of destroying
155 // the message data which will release the reference on RemoteAudioSource.
156 main_thread_->Post(RTC_FROM_HERE, this, 0,
157 new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
Tommif888bb52015-12-12 01:37:01 +0100158}
159
160void RemoteAudioSource::OnMessage(rtc::Message* msg) {
161 RTC_DCHECK(main_thread_->IsCurrent());
162 sinks_.clear();
163 state_ = MediaSourceInterface::kEnded;
164 FireOnChanged();
Steve Anton3b80aac2017-10-19 10:17:12 -0700165 // Will possibly delete this RemoteAudioSource since it is reference counted
166 // in the message.
167 delete msg->pdata;
Tommif888bb52015-12-12 01:37:01 +0100168}
169
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000170} // namespace webrtc