blob: 2fedc9d0f943c22fb2320bb82652e874ce55da2d [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>
Tommif888bb52015-12-12 01:37:01 +010015#include <utility>
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000016
Henrik Kjellander15583c12016-02-10 10:53:12 +010017#include "webrtc/api/mediastreamprovider.h"
Tommif888bb52015-12-12 01:37:01 +010018#include "webrtc/base/checks.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000019#include "webrtc/base/logging.h"
Tommif888bb52015-12-12 01:37:01 +010020#include "webrtc/base/thread.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000021
22namespace webrtc {
23
Tommif888bb52015-12-12 01:37:01 +010024class RemoteAudioSource::MessageHandler : public rtc::MessageHandler {
25 public:
26 explicit MessageHandler(RemoteAudioSource* source) : source_(source) {}
27
28 private:
29 ~MessageHandler() override {}
30
31 void OnMessage(rtc::Message* msg) override {
32 source_->OnMessage(msg);
33 delete this;
34 }
35
36 const rtc::scoped_refptr<RemoteAudioSource> source_;
37 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MessageHandler);
38};
39
40class RemoteAudioSource::Sink : public AudioSinkInterface {
41 public:
42 explicit Sink(RemoteAudioSource* source) : source_(source) {}
43 ~Sink() override { source_->OnAudioProviderGone(); }
44
45 private:
46 void OnData(const AudioSinkInterface::Data& audio) override {
47 if (source_)
48 source_->OnData(audio);
49 }
50
51 const rtc::scoped_refptr<RemoteAudioSource> source_;
52 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sink);
53};
54
55rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create(
56 uint32_t ssrc,
57 AudioProviderInterface* provider) {
58 rtc::scoped_refptr<RemoteAudioSource> ret(
59 new rtc::RefCountedObject<RemoteAudioSource>());
60 ret->Initialize(ssrc, provider);
61 return ret;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000062}
63
Tommif888bb52015-12-12 01:37:01 +010064RemoteAudioSource::RemoteAudioSource()
65 : main_thread_(rtc::Thread::Current()),
66 state_(MediaSourceInterface::kLive) {
67 RTC_DCHECK(main_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000068}
69
70RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 01:37:01 +010071 RTC_DCHECK(main_thread_->IsCurrent());
72 RTC_DCHECK(audio_observers_.empty());
73 RTC_DCHECK(sinks_.empty());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000074}
75
Tommif888bb52015-12-12 01:37:01 +010076void RemoteAudioSource::Initialize(uint32_t ssrc,
77 AudioProviderInterface* provider) {
78 RTC_DCHECK(main_thread_->IsCurrent());
79 // To make sure we always get notified when the provider goes out of scope,
80 // we register for callbacks here and not on demand in AddSink.
81 if (provider) { // May be null in tests.
deadbeef2d110be2016-01-13 12:00:26 -080082 provider->SetRawAudioSink(
83 ssrc, rtc::scoped_ptr<AudioSinkInterface>(new Sink(this)));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000084 }
85}
86
Tommif888bb52015-12-12 01:37:01 +010087MediaSourceInterface::SourceState RemoteAudioSource::state() const {
88 RTC_DCHECK(main_thread_->IsCurrent());
89 return state_;
90}
91
tommi6eca7e32015-12-15 04:27:11 -080092bool RemoteAudioSource::remote() const {
93 RTC_DCHECK(main_thread_->IsCurrent());
94 return true;
95}
96
Tommif888bb52015-12-12 01:37:01 +010097void RemoteAudioSource::SetVolume(double volume) {
98 RTC_DCHECK(volume >= 0 && volume <= 10);
99 for (auto* observer : audio_observers_)
100 observer->OnSetVolume(volume);
101}
102
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000103void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100104 RTC_DCHECK(observer != NULL);
105 RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(),
106 observer) == audio_observers_.end());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000107 audio_observers_.push_back(observer);
108}
109
110void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100111 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000112 audio_observers_.remove(observer);
113}
114
Tommif888bb52015-12-12 01:37:01 +0100115void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
116 RTC_DCHECK(main_thread_->IsCurrent());
117 RTC_DCHECK(sink);
118
119 if (state_ != MediaSourceInterface::kLive) {
120 LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
121 return;
122 }
123
124 rtc::CritScope lock(&sink_lock_);
125 RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end());
126 sinks_.push_back(sink);
127}
128
129void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
130 RTC_DCHECK(main_thread_->IsCurrent());
131 RTC_DCHECK(sink);
132
133 rtc::CritScope lock(&sink_lock_);
134 sinks_.remove(sink);
135}
136
137void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
138 // Called on the externally-owned audio callback thread, via/from webrtc.
139 rtc::CritScope lock(&sink_lock_);
140 for (auto* sink : sinks_) {
141 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
142 audio.samples_per_channel);
143 }
144}
145
146void RemoteAudioSource::OnAudioProviderGone() {
147 // Called when the data provider is deleted. It may be the worker thread
148 // in libjingle or may be a different worker thread.
149 main_thread_->Post(new MessageHandler(this));
150}
151
152void RemoteAudioSource::OnMessage(rtc::Message* msg) {
153 RTC_DCHECK(main_thread_->IsCurrent());
154 sinks_.clear();
155 state_ = MediaSourceInterface::kEnded;
156 FireOnChanged();
157}
158
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000159} // namespace webrtc