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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/audio/virtual_audio_output_stream.h"
#include "base/logging.h"
#include "media/audio/virtual_audio_input_stream.h"
namespace media {
VirtualAudioOutputStream::VirtualAudioOutputStream(
const AudioParameters& params, VirtualAudioInputStream* target,
const AfterCloseCallback& after_close_cb)
: params_(params), target_input_stream_(target),
after_close_cb_(after_close_cb), callback_(NULL), volume_(1.0f) {
DCHECK(params_.IsValid());
DCHECK(target);
// VAOS can be constructed on any thread, but will DCHECK that all
// AudioOutputStream methods are called from the same thread.
thread_checker_.DetachFromThread();
}
VirtualAudioOutputStream::~VirtualAudioOutputStream() {
DCHECK(!callback_);
}
bool VirtualAudioOutputStream::Open() {
DCHECK(thread_checker_.CalledOnValidThread());
return true;
}
void VirtualAudioOutputStream::Start(AudioSourceCallback* callback) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback_);
callback_ = callback;
target_input_stream_->AddOutputStream(this, params_);
}
void VirtualAudioOutputStream::Stop() {
DCHECK(thread_checker_.CalledOnValidThread());
if (callback_) {
target_input_stream_->RemoveOutputStream(this, params_);
callback_ = NULL;
}
}
void VirtualAudioOutputStream::Close() {
DCHECK(thread_checker_.CalledOnValidThread());
Stop();
// If a non-null AfterCloseCallback was provided to the constructor, invoke it
// here. The callback is moved to a stack-local first since |this| could be
// destroyed during Run().
if (!after_close_cb_.is_null()) {
const AfterCloseCallback cb = after_close_cb_;
after_close_cb_.Reset();
cb.Run(this);
}
}
void VirtualAudioOutputStream::SetVolume(double volume) {
DCHECK(thread_checker_.CalledOnValidThread());
volume_ = volume;
}
void VirtualAudioOutputStream::GetVolume(double* volume) {
DCHECK(thread_checker_.CalledOnValidThread());
*volume = volume_;
}
double VirtualAudioOutputStream::ProvideInput(AudioBus* audio_bus,
base::TimeDelta buffer_delay) {
// Note: This method may be invoked on any one thread, depending on the
// platform.
DCHECK(callback_);
const int frames = callback_->OnMoreData(audio_bus, AudioBuffersState());
if (frames < audio_bus->frames())
audio_bus->ZeroFramesPartial(frames, audio_bus->frames() - frames);
return frames > 0 ? volume_ : 0;
}
} // namespace media
|