-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiosourceprovider_impl.cc
182 lines (152 loc) · 5.66 KB
/
audiosourceprovider_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
//
#include "chromium_media_lib/audiosourceprovider_impl.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "media/audio/null_audio_sink.h"
#include "media/base/audio_timestamp_helper.h"
#include "media/base/bind_to_current_loop.h"
#include "media/base/media_log.h"
namespace media {
// TeeFilter is a RenderCallback implementation that allows for a client to get
// a copy of the data being rendered by the |renderer_| on Render(). This class
// also holds on to the necessary audio parameters.
class AudioSourceProviderImpl::TeeFilter
: public AudioRendererSink::RenderCallback {
public:
TeeFilter() : renderer_(nullptr), channels_(0), sample_rate_(0) {}
~TeeFilter() override {}
void Initialize(AudioRendererSink::RenderCallback* renderer,
int channels,
int sample_rate) {
DCHECK(renderer);
renderer_ = renderer;
channels_ = channels;
sample_rate_ = sample_rate;
}
// AudioRendererSink::RenderCallback implementation.
// These are forwarders to |renderer_| and are here to allow for a client to
// get a copy of the rendered audio by SetCopyAudioCallback().
int Render(base::TimeDelta delay,
base::TimeTicks delay_timestamp,
int prior_frames_skipped,
AudioBus* dest) override;
void OnRenderError() override;
bool IsInitialized() const { return !!renderer_; }
int channels() const { return channels_; }
int sample_rate() const { return sample_rate_; }
void set_copy_audio_bus_callback(const CopyAudioCB& callback) {
copy_audio_bus_callback_ = callback;
}
private:
AudioRendererSink::RenderCallback* renderer_;
int channels_;
int sample_rate_;
CopyAudioCB copy_audio_bus_callback_;
DISALLOW_COPY_AND_ASSIGN(TeeFilter);
};
AudioSourceProviderImpl::AudioSourceProviderImpl(
scoped_refptr<SwitchableAudioRendererSink> sink,
MediaLog* media_log)
: volume_(1.0),
sink_(std::move(sink)),
tee_filter_(new TeeFilter()),
media_log_(media_log),
weak_factory_(this) {}
AudioSourceProviderImpl::~AudioSourceProviderImpl() {}
void AudioSourceProviderImpl::Initialize(const AudioParameters& params,
RenderCallback* renderer) {
base::AutoLock auto_lock(sink_lock_);
OutputDeviceStatus device_status =
sink_ ? sink_->GetOutputDeviceInfo().device_status()
: OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND;
if (device_status != OUTPUT_DEVICE_STATUS_OK) {
// Since null sink is always OK, we will fall back to it once and forever.
if (sink_)
sink_->Stop();
sink_ = CreateFallbackSink();
MEDIA_LOG(ERROR, media_log_)
<< "Output device error, falling back to null sink";
}
tee_filter_->Initialize(renderer, params.channels(), params.sample_rate());
sink_->Initialize(params, tee_filter_.get());
}
void AudioSourceProviderImpl::Start() {
base::AutoLock auto_lock(sink_lock_);
DCHECK(tee_filter_);
sink_->Start();
}
void AudioSourceProviderImpl::Stop() {
base::AutoLock auto_lock(sink_lock_);
sink_->Stop();
}
void AudioSourceProviderImpl::Play() {
base::AutoLock auto_lock(sink_lock_);
sink_->Play();
}
void AudioSourceProviderImpl::Pause() {
base::AutoLock auto_lock(sink_lock_);
sink_->Pause();
}
bool AudioSourceProviderImpl::SetVolume(double volume) {
base::AutoLock auto_lock(sink_lock_);
volume_ = volume;
sink_->SetVolume(volume);
return true;
}
OutputDeviceInfo AudioSourceProviderImpl::GetOutputDeviceInfo() {
base::AutoLock auto_lock(sink_lock_);
return sink_ ? sink_->GetOutputDeviceInfo()
: OutputDeviceInfo(OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND);
}
bool AudioSourceProviderImpl::IsOptimizedForHardwareParameters() {
base::AutoLock auto_lock(sink_lock_);
return true;
}
bool AudioSourceProviderImpl::CurrentThreadIsRenderingThread() {
NOTIMPLEMENTED();
return false;
}
void AudioSourceProviderImpl::SwitchOutputDevice(
const std::string& device_id,
const url::Origin& security_origin,
const OutputDeviceStatusCB& callback) {
if (!sink_)
callback.Run(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL);
else
sink_->SwitchOutputDevice(device_id, security_origin, callback);
}
scoped_refptr<SwitchableAudioRendererSink>
AudioSourceProviderImpl::CreateFallbackSink() {
// Assuming it is called on media thread.
return new NullAudioSink(base::ThreadTaskRunnerHandle::Get());
}
int AudioSourceProviderImpl::TeeFilter::Render(base::TimeDelta delay,
base::TimeTicks delay_timestamp,
int prior_frames_skipped,
AudioBus* audio_bus) {
DCHECK(IsInitialized());
const int num_rendered_frames = renderer_->Render(
delay, delay_timestamp, prior_frames_skipped, audio_bus);
if (!copy_audio_bus_callback_.is_null()) {
const int64_t frames_delayed =
AudioTimestampHelper::TimeToFrames(delay, sample_rate_);
std::unique_ptr<AudioBus> bus_copy =
AudioBus::Create(audio_bus->channels(), audio_bus->frames());
audio_bus->CopyTo(bus_copy.get());
copy_audio_bus_callback_.Run(std::move(bus_copy), frames_delayed,
sample_rate_);
}
return num_rendered_frames;
}
void AudioSourceProviderImpl::TeeFilter::OnRenderError() {
DCHECK(IsInitialized());
renderer_->OnRenderError();
}
} // namespace media