blob: 2d0785a08f1c9a10de8ec24274c64b445222db05 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/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
Henrik Kjellander15583c12016-02-10 10:53:12 +010018#include "webrtc/api/mediastreamprovider.h"
Tommif888bb52015-12-12 01:37:01 +010019#include "webrtc/base/checks.h"
kwiberg4485ffb2016-04-26 08:14:39 -070020#include "webrtc/base/constructormagic.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000021#include "webrtc/base/logging.h"
Tommif888bb52015-12-12 01:37:01 +010022#include "webrtc/base/thread.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000023
24namespace webrtc {
25
Tommif888bb52015-12-12 01:37:01 +010026class RemoteAudioSource::MessageHandler : public rtc::MessageHandler {
27 public:
28 explicit MessageHandler(RemoteAudioSource* source) : source_(source) {}
29
30 private:
31 ~MessageHandler() override {}
32
33 void OnMessage(rtc::Message* msg) override {
34 source_->OnMessage(msg);
35 delete this;
36 }
37
38 const rtc::scoped_refptr<RemoteAudioSource> source_;
39 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MessageHandler);
40};
41
42class RemoteAudioSource::Sink : public AudioSinkInterface {
43 public:
44 explicit Sink(RemoteAudioSource* source) : source_(source) {}
45 ~Sink() override { source_->OnAudioProviderGone(); }
46
47 private:
48 void OnData(const AudioSinkInterface::Data& audio) override {
49 if (source_)
50 source_->OnData(audio);
51 }
52
53 const rtc::scoped_refptr<RemoteAudioSource> source_;
54 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sink);
55};
56
57rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create(
58 uint32_t ssrc,
59 AudioProviderInterface* provider) {
60 rtc::scoped_refptr<RemoteAudioSource> ret(
61 new rtc::RefCountedObject<RemoteAudioSource>());
62 ret->Initialize(ssrc, provider);
63 return ret;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000064}
65
Tommif888bb52015-12-12 01:37:01 +010066RemoteAudioSource::RemoteAudioSource()
67 : main_thread_(rtc::Thread::Current()),
68 state_(MediaSourceInterface::kLive) {
69 RTC_DCHECK(main_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000070}
71
72RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 01:37:01 +010073 RTC_DCHECK(main_thread_->IsCurrent());
74 RTC_DCHECK(audio_observers_.empty());
75 RTC_DCHECK(sinks_.empty());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000076}
77
Tommif888bb52015-12-12 01:37:01 +010078void RemoteAudioSource::Initialize(uint32_t ssrc,
79 AudioProviderInterface* provider) {
80 RTC_DCHECK(main_thread_->IsCurrent());
81 // To make sure we always get notified when the provider goes out of scope,
82 // we register for callbacks here and not on demand in AddSink.
83 if (provider) { // May be null in tests.
deadbeef2d110be2016-01-13 12:00:26 -080084 provider->SetRawAudioSink(
kwibergd1fe2812016-04-27 06:47:29 -070085 ssrc, std::unique_ptr<AudioSinkInterface>(new Sink(this)));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000086 }
87}
88
Tommif888bb52015-12-12 01:37:01 +010089MediaSourceInterface::SourceState RemoteAudioSource::state() const {
90 RTC_DCHECK(main_thread_->IsCurrent());
91 return state_;
92}
93
tommi6eca7e32015-12-15 04:27:11 -080094bool RemoteAudioSource::remote() const {
95 RTC_DCHECK(main_thread_->IsCurrent());
96 return true;
97}
98
Tommif888bb52015-12-12 01:37:01 +010099void RemoteAudioSource::SetVolume(double volume) {
100 RTC_DCHECK(volume >= 0 && volume <= 10);
101 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
148void RemoteAudioSource::OnAudioProviderGone() {
149 // Called when the data provider is deleted. It may be the worker thread
150 // in libjingle or may be a different worker thread.
151 main_thread_->Post(new MessageHandler(this));
152}
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