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
189
190
191
192
193
194
195
|
// Copyright (c) 2011 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 "content/browser/speech/speech_input_manager.h"
#include "content/browser/browser_thread.h"
#include "media/audio/audio_manager.h"
namespace speech_input {
SpeechInputManager::SpeechInputManager()
: can_report_metrics_(false),
censor_results_(true),
recording_caller_id_(0) {
}
SpeechInputManager::~SpeechInputManager() {
while (requests_.begin() != requests_.end())
CancelRecognition(requests_.begin()->first);
}
bool SpeechInputManager::HasPendingRequest(int caller_id) const {
return requests_.find(caller_id) != requests_.end();
}
SpeechInputManagerDelegate* SpeechInputManager::GetDelegate(
int caller_id) const {
return requests_.find(caller_id)->second.delegate;
}
void SpeechInputManager::ShowAudioInputSettings() {
// Since AudioManager::ShowAudioInputSettings can potentially launch external
// processes, do that in the FILE thread to not block the calling threads.
if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
NewRunnableFunction(&SpeechInputManager::ShowAudioInputSettings));
return;
}
DCHECK(AudioManager::GetAudioManager()->CanShowAudioInputSettings());
if (AudioManager::GetAudioManager()->CanShowAudioInputSettings())
AudioManager::GetAudioManager()->ShowAudioInputSettings();
}
void SpeechInputManager::StartRecognition(
SpeechInputManagerDelegate* delegate,
int caller_id,
int render_process_id,
int render_view_id,
const gfx::Rect& element_rect,
const std::string& language,
const std::string& grammar,
const std::string& origin_url) {
DCHECK(!HasPendingRequest(caller_id));
ShowRecognitionRequested(
caller_id, render_process_id, render_view_id, element_rect);
GetRequestInfo(&can_report_metrics_, &request_info_);
SpeechInputRequest* request = &requests_[caller_id];
request->delegate = delegate;
request->recognizer = new SpeechRecognizer(
this, caller_id, language, grammar, censor_results(),
request_info_, can_report_metrics_ ? origin_url : "");
request->is_active = false;
StartRecognitionForRequest(caller_id);
}
void SpeechInputManager::StartRecognitionForRequest(int caller_id) {
DCHECK(HasPendingRequest(caller_id));
// If we are currently recording audio for another caller, abort that cleanly.
if (recording_caller_id_)
CancelRecognitionAndInformDelegate(recording_caller_id_);
if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) {
ShowNoMicError(caller_id);
} else {
recording_caller_id_ = caller_id;
requests_[caller_id].is_active = true;
requests_[caller_id].recognizer->StartRecording();
ShowWarmUp(caller_id);
}
}
void SpeechInputManager::CancelRecognition(int caller_id) {
DCHECK(HasPendingRequest(caller_id));
if (requests_[caller_id].is_active)
requests_[caller_id].recognizer->CancelRecognition();
requests_.erase(caller_id);
if (recording_caller_id_ == caller_id)
recording_caller_id_ = 0;
DoClose(caller_id);
}
void SpeechInputManager::CancelAllRequestsWithDelegate(
SpeechInputManagerDelegate* delegate) {
SpeechRecognizerMap::iterator it = requests_.begin();
while (it != requests_.end()) {
if (it->second.delegate == delegate) {
CancelRecognition(it->first);
// This map will have very few elements so it is simpler to restart.
it = requests_.begin();
} else {
++it;
}
}
}
void SpeechInputManager::StopRecording(int caller_id) {
DCHECK(HasPendingRequest(caller_id));
requests_[caller_id].recognizer->StopRecording();
}
void SpeechInputManager::SetRecognitionResult(
int caller_id, bool error, const SpeechInputResultArray& result) {
DCHECK(HasPendingRequest(caller_id));
GetDelegate(caller_id)->SetRecognitionResult(caller_id, result);
}
void SpeechInputManager::DidCompleteRecording(int caller_id) {
DCHECK(recording_caller_id_ == caller_id);
DCHECK(HasPendingRequest(caller_id));
recording_caller_id_ = 0;
GetDelegate(caller_id)->DidCompleteRecording(caller_id);
ShowRecognizing(caller_id);
}
void SpeechInputManager::DidCompleteRecognition(int caller_id) {
GetDelegate(caller_id)->DidCompleteRecognition(caller_id);
requests_.erase(caller_id);
DoClose(caller_id);
}
void SpeechInputManager::OnRecognizerError(
int caller_id, SpeechRecognizer::ErrorCode error) {
if (caller_id == recording_caller_id_)
recording_caller_id_ = 0;
requests_[caller_id].is_active = false;
ShowRecognizerError(caller_id, error);
}
void SpeechInputManager::DidStartReceivingAudio(int caller_id) {
DCHECK(HasPendingRequest(caller_id));
DCHECK(recording_caller_id_ == caller_id);
ShowRecording(caller_id);
}
void SpeechInputManager::DidCompleteEnvironmentEstimation(int caller_id) {
DCHECK(HasPendingRequest(caller_id));
DCHECK(recording_caller_id_ == caller_id);
}
void SpeechInputManager::SetInputVolume(int caller_id, float volume,
float noise_volume) {
DCHECK(HasPendingRequest(caller_id));
DCHECK_EQ(recording_caller_id_, caller_id);
ShowInputVolume(caller_id, volume, noise_volume);
}
void SpeechInputManager::CancelRecognitionAndInformDelegate(
int caller_id) {
SpeechInputManagerDelegate* cur_delegate = GetDelegate(caller_id);
CancelRecognition(caller_id);
cur_delegate->DidCompleteRecording(caller_id);
cur_delegate->DidCompleteRecognition(caller_id);
}
void SpeechInputManager::OnFocusChanged(int caller_id) {
// Ignore if the caller id was not in our active recognizers list because the
// user might have clicked more than once, or recognition could have been
// ended due to other reasons before the user click was processed.
if (HasPendingRequest(caller_id)) {
// If this is an ongoing recording or if we were displaying an error message
// to the user, abort it since user has switched focus. Otherwise
// recognition has started and keep that going so user can start speaking to
// another element while this gets the results in parallel.
if (recording_caller_id_ == caller_id || !requests_[caller_id].is_active) {
CancelRecognitionAndInformDelegate(caller_id);
}
}
}
SpeechInputManager::SpeechInputRequest::SpeechInputRequest()
: delegate(NULL),
is_active(false) {
}
SpeechInputManager::SpeechInputRequest::~SpeechInputRequest() {
}
} // namespace speech_input
|