blob: 3cdceda9064ead853bfcfe01eeea4adb2959c542 [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"
henrikac6cf9022020-07-29 17:20:13 +020025#include "rtc_base/strings/string_format.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "rtc_base/thread_checker.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000028
29namespace webrtc {
30
Steve Antond3679212018-01-17 17:41:02 -080031// This proxy is passed to the underlying media engine to receive audio data as
32// they come in. The data will then be passed back up to the RemoteAudioSource
33// which will fan it out to all the sinks that have been added to it.
34class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
Tommif888bb52015-12-12 01:37:01 +010035 public:
Steve Antond3679212018-01-17 17:41:02 -080036 explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
37 RTC_DCHECK(source);
38 }
39 ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 01:37:01 +010040
Steve Antond3679212018-01-17 17:41:02 -080041 // AudioSinkInterface implementation.
Tommif888bb52015-12-12 01:37:01 +010042 void OnData(const AudioSinkInterface::Data& audio) override {
Steve Antond3679212018-01-17 17:41:02 -080043 source_->OnData(audio);
Tommif888bb52015-12-12 01:37:01 +010044 }
45
Steve Antond3679212018-01-17 17:41:02 -080046 private:
Tommif888bb52015-12-12 01:37:01 +010047 const rtc::scoped_refptr<RemoteAudioSource> source_;
Steve Antond3679212018-01-17 17:41:02 -080048
49 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDataProxy);
Tommif888bb52015-12-12 01:37:01 +010050};
51
Steve Antond3679212018-01-17 17:41:02 -080052RemoteAudioSource::RemoteAudioSource(rtc::Thread* worker_thread)
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +020053 : MessageHandler(false),
54 main_thread_(rtc::Thread::Current()),
Steve Antond3679212018-01-17 17:41:02 -080055 worker_thread_(worker_thread),
Ruslan Burakov428dcb22019-04-18 17:49:49 +020056 state_(MediaSourceInterface::kLive) {
Tommif888bb52015-12-12 01:37:01 +010057 RTC_DCHECK(main_thread_);
Steve Antond3679212018-01-17 17:41:02 -080058 RTC_DCHECK(worker_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000059}
60
61RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 01:37:01 +010062 RTC_DCHECK(main_thread_->IsCurrent());
63 RTC_DCHECK(audio_observers_.empty());
64 RTC_DCHECK(sinks_.empty());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000065}
66
Steve Antond3679212018-01-17 17:41:02 -080067void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
Saurav Das749f6602019-12-04 09:31:36 -080068 absl::optional<uint32_t> ssrc) {
Steve Antond3679212018-01-17 17:41:02 -080069 RTC_DCHECK_RUN_ON(main_thread_);
70 RTC_DCHECK(media_channel);
Ruslan Burakov7ea46052019-02-16 02:07:05 +010071
Steve Antond3679212018-01-17 17:41:02 -080072 // Register for callbacks immediately before AddSink so that we always get
73 // notified when a channel goes out of scope (signaled when "AudioDataProxy"
74 // is destroyed).
75 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
Saurav Das749f6602019-12-04 09:31:36 -080076 ssrc ? media_channel->SetRawAudioSink(
77 *ssrc, std::make_unique<AudioDataProxy>(this))
78 : media_channel->SetDefaultRawAudioSink(
79 std::make_unique<AudioDataProxy>(this));
Steve Antond3679212018-01-17 17:41:02 -080080 });
81}
82
83void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
Saurav Das749f6602019-12-04 09:31:36 -080084 absl::optional<uint32_t> ssrc) {
Steve Antond3679212018-01-17 17:41:02 -080085 RTC_DCHECK_RUN_ON(main_thread_);
86 RTC_DCHECK(media_channel);
Ruslan Burakov7ea46052019-02-16 02:07:05 +010087
Saurav Das749f6602019-12-04 09:31:36 -080088 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
89 ssrc ? media_channel->SetRawAudioSink(*ssrc, nullptr)
90 : media_channel->SetDefaultRawAudioSink(nullptr);
91 });
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000092}
93
Tommif888bb52015-12-12 01:37:01 +010094MediaSourceInterface::SourceState RemoteAudioSource::state() const {
95 RTC_DCHECK(main_thread_->IsCurrent());
96 return state_;
97}
98
tommi6eca7e32015-12-15 04:27:11 -080099bool RemoteAudioSource::remote() const {
100 RTC_DCHECK(main_thread_->IsCurrent());
101 return true;
102}
103
Tommif888bb52015-12-12 01:37:01 +0100104void RemoteAudioSource::SetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -0700105 RTC_DCHECK_GE(volume, 0);
106 RTC_DCHECK_LE(volume, 10);
henrikac6cf9022020-07-29 17:20:13 +0200107 RTC_LOG(LS_INFO) << rtc::StringFormat("RAS::%s({volume=%.2f})", __func__,
108 volume);
Steve Antond3679212018-01-17 17:41:02 -0800109 for (auto* observer : audio_observers_) {
Tommif888bb52015-12-12 01:37:01 +0100110 observer->OnSetVolume(volume);
Steve Antond3679212018-01-17 17:41:02 -0800111 }
Tommif888bb52015-12-12 01:37:01 +0100112}
113
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000114void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100115 RTC_DCHECK(observer != NULL);
Steve Anton64b626b2019-01-28 17:25:26 -0800116 RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000117 audio_observers_.push_back(observer);
118}
119
120void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100121 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000122 audio_observers_.remove(observer);
123}
124
Tommif888bb52015-12-12 01:37:01 +0100125void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
126 RTC_DCHECK(main_thread_->IsCurrent());
127 RTC_DCHECK(sink);
128
129 if (state_ != MediaSourceInterface::kLive) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100130 RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
Tommif888bb52015-12-12 01:37:01 +0100131 return;
132 }
133
Markus Handell6fcd0f82020-07-07 19:08:53 +0200134 MutexLock lock(&sink_lock_);
Steve Anton3d023842019-01-28 19:48:28 -0800135 RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
Tommif888bb52015-12-12 01:37:01 +0100136 sinks_.push_back(sink);
137}
138
139void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
140 RTC_DCHECK(main_thread_->IsCurrent());
141 RTC_DCHECK(sink);
142
Markus Handell6fcd0f82020-07-07 19:08:53 +0200143 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 01:37:01 +0100144 sinks_.remove(sink);
145}
146
147void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
148 // Called on the externally-owned audio callback thread, via/from webrtc.
Markus Handell6fcd0f82020-07-07 19:08:53 +0200149 MutexLock lock(&sink_lock_);
Tommif888bb52015-12-12 01:37:01 +0100150 for (auto* sink : sinks_) {
Minyue Li99d6d812020-01-29 10:25:12 +0100151 // When peerconnection acts as an audio source, it should not provide
152 // absolute capture timestamp.
Tommif888bb52015-12-12 01:37:01 +0100153 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
Minyue Li99d6d812020-01-29 10:25:12 +0100154 audio.samples_per_channel,
155 /*absolute_capture_timestamp_ms=*/absl::nullopt);
Tommif888bb52015-12-12 01:37:01 +0100156 }
157}
158
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700159void RemoteAudioSource::OnAudioChannelGone() {
160 // Called when the audio channel is deleted. It may be the worker thread
Tommif888bb52015-12-12 01:37:01 +0100161 // in libjingle or may be a different worker thread.
Steve Anton3b80aac2017-10-19 10:17:12 -0700162 // This object needs to live long enough for the cleanup logic in OnMessage to
163 // run, so take a reference to it as the data. Sometimes the message may not
164 // be processed (because the thread was destroyed shortly after this call),
165 // but that is fine because the thread destructor will take care of destroying
166 // the message data which will release the reference on RemoteAudioSource.
167 main_thread_->Post(RTC_FROM_HERE, this, 0,
168 new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
Tommif888bb52015-12-12 01:37:01 +0100169}
170
171void RemoteAudioSource::OnMessage(rtc::Message* msg) {
172 RTC_DCHECK(main_thread_->IsCurrent());
173 sinks_.clear();
174 state_ = MediaSourceInterface::kEnded;
175 FireOnChanged();
Steve Anton3b80aac2017-10-19 10:17:12 -0700176 // Will possibly delete this RemoteAudioSource since it is reference counted
177 // in the message.
178 delete msg->pdata;
Tommif888bb52015-12-12 01:37:01 +0100179}
180
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000181} // namespace webrtc