blob: 47213fa299015ec3c7e46e3809a698a02a941950 [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
Tommif888bb52015-12-12 01:37:01 +010018#include "webrtc/base/checks.h"
kwiberg4485ffb2016-04-26 08:14:39 -070019#include "webrtc/base/constructormagic.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000020#include "webrtc/base/logging.h"
Tommif888bb52015-12-12 01:37:01 +010021#include "webrtc/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) {
99 RTC_DCHECK(volume >= 0 && volume <= 10);
100 for (auto* observer : audio_observers_)
101 observer->OnSetVolume(volume);
102}
103
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000104void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100105 RTC_DCHECK(observer != NULL);
106 RTC_DCHECK(std::find(audio_observers_.begin(), audio_observers_.end(),
107 observer) == audio_observers_.end());
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000108 audio_observers_.push_back(observer);
109}
110
111void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
Tommif888bb52015-12-12 01:37:01 +0100112 RTC_DCHECK(observer != NULL);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000113 audio_observers_.remove(observer);
114}
115
Tommif888bb52015-12-12 01:37:01 +0100116void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
117 RTC_DCHECK(main_thread_->IsCurrent());
118 RTC_DCHECK(sink);
119
120 if (state_ != MediaSourceInterface::kLive) {
121 LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
122 return;
123 }
124
125 rtc::CritScope lock(&sink_lock_);
126 RTC_DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end());
127 sinks_.push_back(sink);
128}
129
130void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
131 RTC_DCHECK(main_thread_->IsCurrent());
132 RTC_DCHECK(sink);
133
134 rtc::CritScope lock(&sink_lock_);
135 sinks_.remove(sink);
136}
137
138void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
139 // Called on the externally-owned audio callback thread, via/from webrtc.
140 rtc::CritScope lock(&sink_lock_);
141 for (auto* sink : sinks_) {
142 sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
143 audio.samples_per_channel);
144 }
145}
146
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700147void RemoteAudioSource::OnAudioChannelGone() {
148 // Called when the audio channel is deleted. It may be the worker thread
Tommif888bb52015-12-12 01:37:01 +0100149 // in libjingle or may be a different worker thread.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700150 main_thread_->Post(RTC_FROM_HERE, new MessageHandler(this));
Tommif888bb52015-12-12 01:37:01 +0100151}
152
153void RemoteAudioSource::OnMessage(rtc::Message* msg) {
154 RTC_DCHECK(main_thread_->IsCurrent());
155 sinks_.clear();
156 state_ = MediaSourceInterface::kEnded;
157 FireOnChanged();
158}
159
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000160} // namespace webrtc