blob: 6fcc2aba828e9d02a26384611b0233c146d47433 [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::MessageHandler : public rtc::MessageHandler {
26 public:
27 explicit MessageHandler(RemoteAudioSource* source) : source_(source) {}
28
29 private:
30 ~MessageHandler() override {}
31
32 void OnMessage(rtc::Message* msg) override {
33 source_->OnMessage(msg);
34 delete this;
35 }
36
37 const rtc::scoped_refptr<RemoteAudioSource> source_;
38 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MessageHandler);
39};
40
41class RemoteAudioSource::Sink : public AudioSinkInterface {
42 public:
43 explicit Sink(RemoteAudioSource* source) : source_(source) {}
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070044 ~Sink() override { source_->OnAudioChannelGone(); }
Tommif888bb52015-12-12 01:37:01 +010045
46 private:
47 void OnData(const AudioSinkInterface::Data& audio) override {
48 if (source_)
49 source_->OnData(audio);
50 }
51
52 const rtc::scoped_refptr<RemoteAudioSource> source_;
53 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sink);
54};
55
56rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create(
57 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070058 cricket::VoiceChannel* channel) {
Tommif888bb52015-12-12 01:37:01 +010059 rtc::scoped_refptr<RemoteAudioSource> ret(
60 new rtc::RefCountedObject<RemoteAudioSource>());
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070061 ret->Initialize(ssrc, channel);
Tommif888bb52015-12-12 01:37:01 +010062 return ret;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000063}
64
Tommif888bb52015-12-12 01:37:01 +010065RemoteAudioSource::RemoteAudioSource()
66 : main_thread_(rtc::Thread::Current()),
67 state_(MediaSourceInterface::kLive) {
68 RTC_DCHECK(main_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000069}
70
71RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 01:37:01 +010072 RTC_DCHECK(main_thread_->IsCurrent());
73 RTC_DCHECK(audio_observers_.empty());
74 RTC_DCHECK(sinks_.empty());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000075}
76
Tommif888bb52015-12-12 01:37:01 +010077void RemoteAudioSource::Initialize(uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070078 cricket::VoiceChannel* channel) {
Tommif888bb52015-12-12 01:37:01 +010079 RTC_DCHECK(main_thread_->IsCurrent());
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070080 // To make sure we always get notified when the channel goes out of scope,
Tommif888bb52015-12-12 01:37:01 +010081 // we register for callbacks here and not on demand in AddSink.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070082 if (channel) { // May be null in tests.
83 channel->SetRawAudioSink(
kwibergd1fe2812016-04-27 06:47:29 -070084 ssrc, std::unique_ptr<AudioSinkInterface>(new Sink(this)));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000085 }
86}
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);
Tommif888bb52015-12-12 01:37:01 +0100101 for (auto* observer : audio_observers_)
102 observer->OnSetVolume(volume);
103}
104
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000105void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100106 RTC_DCHECK(observer != NULL);
107 RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(),
108 observer) == audio_observers_.end());
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) {
122 LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
123 return;
124 }
125
126 rtc::CritScope lock(&sink_lock_);
127 RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end());
128 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.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700151 main_thread_->Post(RTC_FROM_HERE, new MessageHandler(this));
Tommif888bb52015-12-12 01:37:01 +0100152}
153
154void RemoteAudioSource::OnMessage(rtc::Message* msg) {
155 RTC_DCHECK(main_thread_->IsCurrent());
156 sinks_.clear();
157 state_ = MediaSourceInterface::kEnded;
158 FireOnChanged();
159}
160
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000161} // namespace webrtc