Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/speech/speech_input_manager.h" | 5 #include "content/browser/speech/speech_input_manager.h" |
| 6 | 6 |
| 7 #include "content/browser/browser_thread.h" | 7 #include "content/browser/browser_thread.h" |
| 8 #include "media/audio/audio_manager.h" | 8 #include "media/audio/audio_manager.h" |
| 9 | 9 |
| 10 namespace speech_input { | 10 namespace speech_input { |
| 11 | 11 |
| 12 SpeechInputManager::SpeechInputManager() : censor_results_(true) { | 12 SpeechInputManager::SpeechInputManager() |
| 13 : can_report_metrics_(false), | |
| 14 recording_caller_id_(0), | |
| 15 censor_results_(true) { | |
|
Satish
2011/09/12 12:23:05
in http://crbug.com/92366 it appears we should be
allanwoj
2011/09/19 18:30:38
I'll do this in a separate cl.
| |
| 13 } | 16 } |
| 14 | 17 |
| 15 SpeechInputManager::~SpeechInputManager() { | 18 SpeechInputManager::~SpeechInputManager() { |
| 19 while (requests_.begin() != requests_.end()) | |
| 20 CancelRecognition(requests_.begin()->first); | |
| 21 } | |
| 22 | |
| 23 bool SpeechInputManager::HasPendingRequest(int caller_id) const { | |
| 24 return requests_.find(caller_id) != requests_.end(); | |
| 25 } | |
| 26 | |
| 27 SpeechInputManagerDelegate* SpeechInputManager::GetDelegate( | |
| 28 int caller_id) const { | |
| 29 return requests_.find(caller_id)->second.delegate; | |
| 16 } | 30 } |
| 17 | 31 |
| 18 void SpeechInputManager::ShowAudioInputSettings() { | 32 void SpeechInputManager::ShowAudioInputSettings() { |
| 19 // Since AudioManager::ShowAudioInputSettings can potentially launch external | 33 // Since AudioManager::ShowAudioInputSettings can potentially launch external |
| 20 // processes, do that in the FILE thread to not block the calling threads. | 34 // processes, do that in the FILE thread to not block the calling threads. |
| 21 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 35 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
| 22 BrowserThread::PostTask( | 36 BrowserThread::PostTask( |
| 23 BrowserThread::FILE, FROM_HERE, | 37 BrowserThread::FILE, FROM_HERE, |
| 24 NewRunnableFunction(&SpeechInputManager::ShowAudioInputSettings)); | 38 NewRunnableFunction(&SpeechInputManager::ShowAudioInputSettings)); |
| 25 return; | 39 return; |
| 26 } | 40 } |
| 27 | 41 |
| 28 DCHECK(AudioManager::GetAudioManager()->CanShowAudioInputSettings()); | 42 DCHECK(AudioManager::GetAudioManager()->CanShowAudioInputSettings()); |
| 29 if (AudioManager::GetAudioManager()->CanShowAudioInputSettings()) | 43 if (AudioManager::GetAudioManager()->CanShowAudioInputSettings()) |
| 30 AudioManager::GetAudioManager()->ShowAudioInputSettings(); | 44 AudioManager::GetAudioManager()->ShowAudioInputSettings(); |
| 31 } | 45 } |
| 32 | 46 |
| 47 void SpeechInputManager::StartRecognition( | |
| 48 SpeechInputManagerDelegate* delegate, | |
| 49 int caller_id, | |
| 50 int render_process_id, | |
| 51 int render_view_id, | |
| 52 const gfx::Rect& element_rect, | |
| 53 const std::string& language, | |
| 54 const std::string& grammar, | |
| 55 const std::string& origin_url) { | |
| 56 DCHECK(!HasPendingRequest(caller_id)); | |
| 57 | |
| 58 CreateBubble(caller_id, render_process_id, render_view_id, element_rect); | |
| 59 FetchRequestInfo(); | |
| 60 | |
| 61 SpeechInputRequest* request = &requests_[caller_id]; | |
| 62 request->delegate = delegate; | |
| 63 request->recognizer = new SpeechRecognizer( | |
| 64 this, caller_id, language, grammar, censor_results(), | |
| 65 request_info_, | |
| 66 can_report_metrics_ ? origin_url : ""); | |
| 67 request->is_active = false; | |
| 68 | |
| 69 StartRecognitionForRequest(caller_id); | |
| 70 } | |
| 71 | |
| 72 void SpeechInputManager::StartRecognitionForRequest(int caller_id) { | |
| 73 DCHECK(HasPendingRequest(caller_id)); | |
| 74 | |
| 75 // If we are currently recording audio for another caller, abort that cleanly. | |
| 76 if (recording_caller_id_) | |
| 77 CancelRecognitionAndInformDelegate(recording_caller_id_); | |
| 78 | |
| 79 if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) { | |
| 80 NoMicBubbleMessage(caller_id); | |
| 81 } else { | |
| 82 recording_caller_id_ = caller_id; | |
| 83 requests_[caller_id].is_active = true; | |
| 84 requests_[caller_id].recognizer->StartRecording(); | |
| 85 SetBubbleWarmUpMode(caller_id); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void SpeechInputManager::CancelRecognition(int caller_id) { | |
| 90 DCHECK(HasPendingRequest(caller_id)); | |
| 91 if (requests_[caller_id].is_active) | |
| 92 requests_[caller_id].recognizer->CancelRecognition(); | |
| 93 requests_.erase(caller_id); | |
| 94 if (recording_caller_id_ == caller_id) | |
| 95 recording_caller_id_ = 0; | |
| 96 CloseBubble(caller_id); | |
| 97 } | |
| 98 | |
| 99 void SpeechInputManager::CancelAllRequestsWithDelegate( | |
| 100 SpeechInputManagerDelegate* delegate) { | |
| 101 SpeechRecognizerMap::iterator it = requests_.begin(); | |
| 102 while (it != requests_.end()) { | |
| 103 if (it->second.delegate == delegate) { | |
| 104 CancelRecognition(it->first); | |
| 105 // This map will have very few elements so it is simpler to restart. | |
| 106 it = requests_.begin(); | |
| 107 } else { | |
| 108 ++it; | |
| 109 } | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void SpeechInputManager::StopRecording(int caller_id) { | |
| 114 DCHECK(HasPendingRequest(caller_id)); | |
| 115 requests_[caller_id].recognizer->StopRecording(); | |
| 116 } | |
| 117 | |
| 118 void SpeechInputManager::SetRecognitionResult( | |
| 119 int caller_id, bool error, const SpeechInputResultArray& result) { | |
| 120 DCHECK(HasPendingRequest(caller_id)); | |
| 121 GetDelegate(caller_id)->SetRecognitionResult(caller_id, result); | |
| 122 } | |
| 123 | |
| 124 void SpeechInputManager::DidCompleteRecording(int caller_id) { | |
| 125 DCHECK(recording_caller_id_ == caller_id); | |
| 126 DCHECK(HasPendingRequest(caller_id)); | |
| 127 recording_caller_id_ = 0; | |
| 128 GetDelegate(caller_id)->DidCompleteRecording(caller_id); | |
| 129 SetBubbleRecognizingMode(caller_id); | |
| 130 } | |
| 131 | |
| 132 void SpeechInputManager::DidCompleteRecognition(int caller_id) { | |
| 133 GetDelegate(caller_id)->DidCompleteRecognition(caller_id); | |
| 134 requests_.erase(caller_id); | |
| 135 CloseBubble(caller_id); | |
| 136 } | |
| 137 | |
| 138 void SpeechInputManager::OnRecognizerError( | |
| 139 int caller_id, SpeechRecognizer::ErrorCode error) { | |
| 140 if (caller_id == recording_caller_id_) | |
| 141 recording_caller_id_ = 0; | |
| 142 | |
| 143 requests_[caller_id].is_active = false; | |
| 144 | |
| 145 HandleRecognizerError(caller_id, error); | |
| 146 } | |
| 147 | |
| 148 void SpeechInputManager::DidStartReceivingAudio(int caller_id) { | |
| 149 DCHECK(HasPendingRequest(caller_id)); | |
| 150 DCHECK(recording_caller_id_ == caller_id); | |
| 151 SetBubbleRecordingMode(caller_id); | |
| 152 } | |
| 153 | |
| 154 void SpeechInputManager::DidCompleteEnvironmentEstimation(int caller_id) { | |
| 155 DCHECK(HasPendingRequest(caller_id)); | |
| 156 DCHECK(recording_caller_id_ == caller_id); | |
| 157 } | |
| 158 | |
| 159 void SpeechInputManager::SetInputVolume(int caller_id, float volume, | |
| 160 float noise_volume) { | |
| 161 DCHECK(HasPendingRequest(caller_id)); | |
| 162 DCHECK_EQ(recording_caller_id_, caller_id); | |
| 163 | |
| 164 SetBubbleInputVolume(caller_id, volume, noise_volume); | |
| 165 } | |
| 166 | |
| 167 void SpeechInputManager::CancelRecognitionAndInformDelegate( | |
| 168 int caller_id) { | |
| 169 SpeechInputManagerDelegate* cur_delegate = GetDelegate(caller_id); | |
| 170 CancelRecognition(caller_id); | |
| 171 cur_delegate->DidCompleteRecording(caller_id); | |
| 172 cur_delegate->DidCompleteRecognition(caller_id); | |
| 173 } | |
| 174 | |
| 175 void SpeechInputManager::OnFocusChanged(int caller_id) { | |
| 176 // Ignore if the caller id was not in our active recognizers list because the | |
| 177 // user might have clicked more than once, or recognition could have been | |
| 178 // ended due to other reasons before the user click was processed. | |
| 179 if (HasPendingRequest(caller_id)) { | |
| 180 // If this is an ongoing recording or if we were displaying an error message | |
| 181 // to the user, abort it since user has switched focus. Otherwise | |
| 182 // recognition has started and keep that going so user can start speaking to | |
| 183 // another element while this gets the results in parallel. | |
| 184 if (recording_caller_id_ == caller_id || !requests_[caller_id].is_active) { | |
| 185 CancelRecognitionAndInformDelegate(caller_id); | |
| 186 } | |
| 187 } | |
| 188 } | |
| 189 | |
| 33 } // namespace speech_input | 190 } // namespace speech_input |
| OLD | NEW |