Adds "tts" permission for Input Tools & XKB extension.
[chromium-blink-merge.git] / media / audio / sounds / audio_stream_handler.cc
blobac9c87bcfaf1b98d0560727c545766e2ffae054f
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/audio/sounds/audio_stream_handler.h"
7 #include <string>
9 #include "base/cancelable_callback.h"
10 #include "base/logging.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/synchronization/lock.h"
13 #include "base/time/time.h"
14 #include "media/audio/audio_manager.h"
15 #include "media/audio/audio_manager_base.h"
16 #include "media/base/channel_layout.h"
18 namespace media {
20 namespace {
22 // Volume percent.
23 const double kOutputVolumePercent = 0.8;
25 // The number of frames each OnMoreData() call will request.
26 const int kDefaultFrameCount = 1024;
28 // Keep alive timeout for audio stream.
29 const int kKeepAliveMs = 1500;
31 AudioStreamHandler::TestObserver* g_observer_for_testing = NULL;
32 AudioOutputStream::AudioSourceCallback* g_audio_source_for_testing = NULL;
34 } // namespace
36 class AudioStreamHandler::AudioStreamContainer
37 : public AudioOutputStream::AudioSourceCallback {
38 public:
39 AudioStreamContainer(const WavAudioHandler& wav_audio)
40 : started_(false),
41 stream_(NULL),
42 cursor_(0),
43 delayed_stop_posted_(false),
44 wav_audio_(wav_audio) {}
46 ~AudioStreamContainer() override {
47 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
50 void Play() {
51 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
53 if (!stream_) {
54 const AudioParameters& p = wav_audio_.params();
55 const AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
56 p.channel_layout(),
57 p.sample_rate(),
58 p.bits_per_sample(),
59 kDefaultFrameCount);
60 stream_ = AudioManager::Get()->MakeAudioOutputStreamProxy(params,
61 std::string());
62 if (!stream_ || !stream_->Open()) {
63 LOG(ERROR) << "Failed to open an output stream.";
64 return;
66 stream_->SetVolume(kOutputVolumePercent);
70 base::AutoLock al(state_lock_);
72 delayed_stop_posted_ = false;
73 stop_closure_.Reset(base::Bind(&AudioStreamContainer::StopStream,
74 base::Unretained(this)));
76 if (started_) {
77 if (wav_audio_.AtEnd(cursor_))
78 cursor_ = 0;
79 return;
82 cursor_ = 0;
85 started_ = true;
86 if (g_audio_source_for_testing)
87 stream_->Start(g_audio_source_for_testing);
88 else
89 stream_->Start(this);
91 if (g_observer_for_testing)
92 g_observer_for_testing->OnPlay();
95 void Stop() {
96 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
97 StopStream();
98 if (stream_)
99 stream_->Close();
100 stream_ = NULL;
101 stop_closure_.Cancel();
104 private:
105 // AudioOutputStream::AudioSourceCallback overrides:
106 // Following methods could be called from *ANY* thread.
107 int OnMoreData(AudioBus* dest, uint32 /* total_bytes_delay */) override {
108 base::AutoLock al(state_lock_);
109 size_t bytes_written = 0;
111 if (wav_audio_.AtEnd(cursor_) ||
112 !wav_audio_.CopyTo(dest, cursor_, &bytes_written)) {
113 if (delayed_stop_posted_)
114 return 0;
115 delayed_stop_posted_ = true;
116 AudioManager::Get()->GetTaskRunner()->PostDelayedTask(
117 FROM_HERE,
118 stop_closure_.callback(),
119 base::TimeDelta::FromMilliseconds(kKeepAliveMs));
120 return 0;
122 cursor_ += bytes_written;
123 return dest->frames();
126 void OnError(AudioOutputStream* /* stream */) override {
127 LOG(ERROR) << "Error during system sound reproduction.";
130 void StopStream() {
131 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
133 if (stream_ && started_) {
134 // Do not hold the |state_lock_| while stopping the output stream.
135 stream_->Stop();
136 if (g_observer_for_testing)
137 g_observer_for_testing->OnStop(cursor_);
140 started_ = false;
143 // Must only be accessed on the AudioManager::GetTaskRunner() thread.
144 bool started_;
145 AudioOutputStream* stream_;
147 // All variables below must be accessed under |state_lock_| when |started_|.
148 base::Lock state_lock_;
149 size_t cursor_;
150 bool delayed_stop_posted_;
151 const WavAudioHandler wav_audio_;
152 base::CancelableClosure stop_closure_;
154 DISALLOW_COPY_AND_ASSIGN(AudioStreamContainer);
157 AudioStreamHandler::AudioStreamHandler(const base::StringPiece& wav_data)
158 : wav_audio_(wav_data),
159 initialized_(false) {
160 AudioManager* manager = AudioManager::Get();
161 if (!manager) {
162 LOG(ERROR) << "Can't get access to audio manager.";
163 return;
165 if (!wav_audio_.params().IsValid()) {
166 LOG(ERROR) << "Audio params are invalid.";
167 return;
169 stream_.reset(new AudioStreamContainer(wav_audio_));
170 initialized_ = true;
173 AudioStreamHandler::~AudioStreamHandler() {
174 DCHECK(CalledOnValidThread());
175 AudioManager::Get()->GetTaskRunner()->PostTask(
176 FROM_HERE,
177 base::Bind(&AudioStreamContainer::Stop, base::Unretained(stream_.get())));
178 AudioManager::Get()->GetTaskRunner()->DeleteSoon(FROM_HERE,
179 stream_.release());
182 bool AudioStreamHandler::IsInitialized() const {
183 DCHECK(CalledOnValidThread());
184 return initialized_;
187 bool AudioStreamHandler::Play() {
188 DCHECK(CalledOnValidThread());
190 if (!IsInitialized())
191 return false;
193 AudioManager::Get()->GetTaskRunner()->PostTask(
194 FROM_HERE,
195 base::Bind(base::IgnoreResult(&AudioStreamContainer::Play),
196 base::Unretained(stream_.get())));
197 return true;
200 void AudioStreamHandler::Stop() {
201 DCHECK(CalledOnValidThread());
202 AudioManager::Get()->GetTaskRunner()->PostTask(
203 FROM_HERE,
204 base::Bind(&AudioStreamContainer::Stop, base::Unretained(stream_.get())));
207 // static
208 void AudioStreamHandler::SetObserverForTesting(TestObserver* observer) {
209 g_observer_for_testing = observer;
212 // static
213 void AudioStreamHandler::SetAudioSourceForTesting(
214 AudioOutputStream::AudioSourceCallback* source) {
215 g_audio_source_for_testing = source;
218 } // namespace media