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
183
184
185
186
187
188
|
// Copyright 2013 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/sounds/audio_stream_handler.h"
#include <string>
#include "base/logging.h"
#include "base/message_loop/message_loop_proxy.h"
#include "media/audio/audio_manager.h"
#include "media/audio/audio_manager_base.h"
#include "media/base/channel_layout.h"
namespace media {
namespace {
// Volume percent.
const double kOutputVolumePercent = 0.8;
// The number of frames each OnMoreData() call will request.
const int kDefaultFrameCount = 1024;
AudioStreamHandler::TestObserver* g_observer_for_testing = NULL;
AudioOutputStream::AudioSourceCallback* g_audio_source_for_testing = NULL;
} // namespace
class AudioStreamHandler::AudioStreamContainer
: public AudioOutputStream::AudioSourceCallback {
public:
AudioStreamContainer(const WavAudioHandler& wav_audio,
const AudioParameters& params)
: stream_(NULL),
wav_audio_(wav_audio),
params_(params),
cursor_(0) {
}
virtual ~AudioStreamContainer() {
DCHECK(AudioManager::Get()->GetMessageLoop()->BelongsToCurrentThread());
}
void Play() {
DCHECK(AudioManager::Get()->GetMessageLoop()->BelongsToCurrentThread());
if (!stream_) {
stream_ = AudioManager::Get()->MakeAudioOutputStreamProxy(params_,
std::string(),
std::string());
if (!stream_ || !stream_->Open()) {
LOG(ERROR) << "Failed to open an output stream.";
return;
}
stream_->SetVolume(kOutputVolumePercent);
} else {
// TODO (ygorshenin@): implement smart stream rewind.
stream_->Stop();
}
cursor_ = 0;
if (g_audio_source_for_testing)
stream_->Start(g_audio_source_for_testing);
else
stream_->Start(this);
if (g_observer_for_testing)
g_observer_for_testing->OnPlay();
}
void Stop() {
DCHECK(AudioManager::Get()->GetMessageLoop()->BelongsToCurrentThread());
if (!stream_)
return;
stream_->Stop();
stream_->Close();
stream_ = NULL;
if (g_observer_for_testing)
g_observer_for_testing->OnStop(cursor_);
}
private:
// AudioOutputStream::AudioSourceCallback overrides:
// Following methods could be called from *ANY* thread.
virtual int OnMoreData(AudioBus* dest,
AudioBuffersState /* state */) OVERRIDE {
size_t bytes_written = 0;
if (wav_audio_.AtEnd(cursor_) ||
!wav_audio_.CopyTo(dest, cursor_, &bytes_written)) {
AudioManager::Get()->GetMessageLoop()->PostTask(
FROM_HERE,
base::Bind(&AudioStreamContainer::Stop, base::Unretained(this)));
return 0;
}
cursor_ += bytes_written;
return dest->frames();
}
virtual int OnMoreIOData(AudioBus* /* source */,
AudioBus* dest,
AudioBuffersState state) OVERRIDE {
return OnMoreData(dest, state);
}
virtual void OnError(AudioOutputStream* /* stream */) OVERRIDE {
LOG(ERROR) << "Error during system sound reproduction.";
}
AudioOutputStream* stream_;
const WavAudioHandler wav_audio_;
const AudioParameters params_;
size_t cursor_;
DISALLOW_COPY_AND_ASSIGN(AudioStreamContainer);
};
AudioStreamHandler::AudioStreamHandler(const base::StringPiece& wav_data)
: wav_audio_(wav_data),
initialized_(false) {
AudioManager* manager = AudioManager::Get();
if (!manager) {
LOG(ERROR) << "Can't get access to audio manager.";
return;
}
AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
GuessChannelLayout(wav_audio_.num_channels()),
wav_audio_.sample_rate(),
wav_audio_.bits_per_sample(),
kDefaultFrameCount);
if (!params.IsValid()) {
LOG(ERROR) << "Audio params are invalid.";
return;
}
stream_.reset(new AudioStreamContainer(wav_audio_, params));
initialized_ = true;
}
AudioStreamHandler::~AudioStreamHandler() {
DCHECK(CalledOnValidThread());
AudioManager::Get()->GetMessageLoop()->PostTask(
FROM_HERE,
base::Bind(&AudioStreamContainer::Stop, base::Unretained(stream_.get())));
AudioManager::Get()->GetMessageLoop()->DeleteSoon(FROM_HERE,
stream_.release());
}
bool AudioStreamHandler::IsInitialized() const {
DCHECK(CalledOnValidThread());
return initialized_;
}
bool AudioStreamHandler::Play() {
DCHECK(CalledOnValidThread());
if (!IsInitialized())
return false;
AudioManager::Get()->GetMessageLoop()->PostTask(
FROM_HERE,
base::Bind(base::IgnoreResult(&AudioStreamContainer::Play),
base::Unretained(stream_.get())));
return true;
}
void AudioStreamHandler::Stop() {
DCHECK(CalledOnValidThread());
AudioManager::Get()->GetMessageLoop()->PostTask(
FROM_HERE,
base::Bind(&AudioStreamContainer::Stop, base::Unretained(stream_.get())));
}
// static
void AudioStreamHandler::SetObserverForTesting(TestObserver* observer) {
g_observer_for_testing = observer;
}
// static
void AudioStreamHandler::SetAudioSourceForTesting(
AudioOutputStream::AudioSourceCallback* source) {
g_audio_source_for_testing = source;
}
} // namespace media
|