blob: 45e2e9ec8af610ed4f40e2e4ffe1b3405641c7d0 [file] [log] [blame]
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2014 Google Inc.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/remoteaudiosource.h"
29
30#include <algorithm>
31#include <functional>
Tommif888bb52015-12-12 01:37:01 +010032#include <utility>
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000033
Tommif888bb52015-12-12 01:37:01 +010034#include "talk/app/webrtc/mediastreamprovider.h"
35#include "webrtc/base/checks.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/logging.h"
Tommif888bb52015-12-12 01:37:01 +010037#include "webrtc/base/thread.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000038
39namespace webrtc {
40
Tommif888bb52015-12-12 01:37:01 +010041class RemoteAudioSource::MessageHandler : public rtc::MessageHandler {
42 public:
43 explicit MessageHandler(RemoteAudioSource* source) : source_(source) {}
44
45 private:
46 ~MessageHandler() override {}
47
48 void OnMessage(rtc::Message* msg) override {
49 source_->OnMessage(msg);
50 delete this;
51 }
52
53 const rtc::scoped_refptr<RemoteAudioSource> source_;
54 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MessageHandler);
55};
56
57class RemoteAudioSource::Sink : public AudioSinkInterface {
58 public:
59 explicit Sink(RemoteAudioSource* source) : source_(source) {}
60 ~Sink() override { source_->OnAudioProviderGone(); }
61
62 private:
63 void OnData(const AudioSinkInterface::Data& audio) override {
64 if (source_)
65 source_->OnData(audio);
66 }
67
68 const rtc::scoped_refptr<RemoteAudioSource> source_;
69 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sink);
70};
71
72rtc::scoped_refptr<RemoteAudioSource> RemoteAudioSource::Create(
73 uint32_t ssrc,
74 AudioProviderInterface* provider) {
75 rtc::scoped_refptr<RemoteAudioSource> ret(
76 new rtc::RefCountedObject<RemoteAudioSource>());
77 ret->Initialize(ssrc, provider);
78 return ret;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000079}
80
Tommif888bb52015-12-12 01:37:01 +010081RemoteAudioSource::RemoteAudioSource()
82 : main_thread_(rtc::Thread::Current()),
83 state_(MediaSourceInterface::kLive) {
84 RTC_DCHECK(main_thread_);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000085}
86
87RemoteAudioSource::~RemoteAudioSource() {
Tommif888bb52015-12-12 01:37:01 +010088 RTC_DCHECK(main_thread_->IsCurrent());
89 RTC_DCHECK(audio_observers_.empty());
90 RTC_DCHECK(sinks_.empty());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000091}
92
Tommif888bb52015-12-12 01:37:01 +010093void RemoteAudioSource::Initialize(uint32_t ssrc,
94 AudioProviderInterface* provider) {
95 RTC_DCHECK(main_thread_->IsCurrent());
96 // To make sure we always get notified when the provider goes out of scope,
97 // we register for callbacks here and not on demand in AddSink.
98 if (provider) { // May be null in tests.
deadbeefe591f932016-01-12 16:44:59 -080099 provider->SetRawAudioSink(ssrc, new rtc::RefCountedObject<Sink>(this));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000100 }
101}
102
Tommif888bb52015-12-12 01:37:01 +0100103MediaSourceInterface::SourceState RemoteAudioSource::state() const {
104 RTC_DCHECK(main_thread_->IsCurrent());
105 return state_;
106}
107
tommi6eca7e32015-12-15 04:27:11 -0800108bool RemoteAudioSource::remote() const {
109 RTC_DCHECK(main_thread_->IsCurrent());
110 return true;
111}
112
Tommif888bb52015-12-12 01:37:01 +0100113void RemoteAudioSource::SetVolume(double volume) {
114 RTC_DCHECK(volume >= 0 && volume <= 10);
115 for (auto* observer : audio_observers_)
116 observer->OnSetVolume(volume);
117}
118
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000119void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100120 RTC_DCHECK(observer != NULL);
121 RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(),
122 observer) == audio_observers_.end());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000123 audio_observers_.push_back(observer);
124}
125
126void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100127 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000128 audio_observers_.remove(observer);
129}
130
Tommif888bb52015-12-12 01:37:01 +0100131void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
132 RTC_DCHECK(main_thread_->IsCurrent());
133 RTC_DCHECK(sink);
134
135 if (state_ != MediaSourceInterface::kLive) {
136 LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
137 return;
138 }
139
140 rtc::CritScope lock(&sink_lock_);
141 RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end());
142 sinks_.push_back(sink);
143}
144
145void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
146 RTC_DCHECK(main_thread_->IsCurrent());
147 RTC_DCHECK(sink);
148
149 rtc::CritScope lock(&sink_lock_);
150 sinks_.remove(sink);
151}
152
153void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
154 // Called on the externally-owned audio callback thread, via/from webrtc.
155 rtc::CritScope lock(&sink_lock_);
156 for (auto* sink : sinks_) {
157 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
158 audio.samples_per_channel);
159 }
160}
161
162void RemoteAudioSource::OnAudioProviderGone() {
163 // Called when the data provider is deleted. It may be the worker thread
164 // in libjingle or may be a different worker thread.
165 main_thread_->Post(new MessageHandler(this));
166}
167
168void RemoteAudioSource::OnMessage(rtc::Message* msg) {
169 RTC_DCHECK(main_thread_->IsCurrent());
170 sinks_.clear();
171 state_ = MediaSourceInterface::kEnded;
172 FireOnChanged();
173}
174
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000175} // namespace webrtc