| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_recognition_manager_impl.h" | 5 #include "content/browser/speech/speech_recognition_manager_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "content/browser/browser_main_loop.h" | 8 #include "content/browser/browser_main_loop.h" |
| 9 #include "content/browser/renderer_host/render_view_host_impl.h" | 9 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 10 #include "content/browser/speech/input_tag_speech_dispatcher_host.h" | 10 #include "content/browser/speech/input_tag_speech_dispatcher_host.h" |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 NOTREACHED(); | 196 NOTREACHED(); |
| 197 return; | 197 return; |
| 198 } | 198 } |
| 199 | 199 |
| 200 // We should not currently be recording for the caller. | 200 // We should not currently be recording for the caller. |
| 201 CHECK(recording_caller_id_ != caller_id); | 201 CHECK(recording_caller_id_ != caller_id); |
| 202 | 202 |
| 203 // If we are currently recording audio for another caller, abort that cleanly. | 203 // If we are currently recording audio for another caller, abort that cleanly. |
| 204 if (recording_caller_id_) | 204 if (recording_caller_id_) |
| 205 CancelRecognitionAndInformDelegate(recording_caller_id_); | 205 CancelRecognitionAndInformDelegate(recording_caller_id_); |
| 206 | 206 recording_caller_id_ = caller_id; |
| 207 if (!HasAudioInputDevices()) { | 207 requests_[caller_id].is_active = true; |
| 208 if (delegate_) { | 208 requests_[caller_id].recognizer->StartRecognition(); |
| 209 delegate_->ShowMicError(caller_id, | 209 if (delegate_) |
| 210 SpeechRecognitionManagerDelegate::MIC_ERROR_NO_DEVICE_AVAILABLE); | 210 delegate_->ShowWarmUp(caller_id); |
| 211 } | |
| 212 } else if (IsCapturingAudio()) { | |
| 213 if (delegate_) { | |
| 214 delegate_->ShowMicError( | |
| 215 caller_id, SpeechRecognitionManagerDelegate::MIC_ERROR_DEVICE_IN_USE); | |
| 216 } | |
| 217 } else { | |
| 218 recording_caller_id_ = caller_id; | |
| 219 requests_[caller_id].is_active = true; | |
| 220 requests_[caller_id].recognizer->StartRecognition(); | |
| 221 if (delegate_) | |
| 222 delegate_->ShowWarmUp(caller_id); | |
| 223 } | |
| 224 } | 211 } |
| 225 | 212 |
| 226 void SpeechRecognitionManagerImpl::CancelRecognitionForRequest(int caller_id) { | 213 void SpeechRecognitionManagerImpl::CancelRecognitionForRequest(int caller_id) { |
| 227 // Ignore if the caller id was not in our active recognizers list because the | 214 // Ignore if the caller id was not in our active recognizers list because the |
| 228 // user might have clicked more than once, or recognition could have been | 215 // user might have clicked more than once, or recognition could have been |
| 229 // ended due to other reasons before the user click was processed. | 216 // ended due to other reasons before the user click was processed. |
| 230 if (!HasPendingRequest(caller_id)) | 217 if (!HasPendingRequest(caller_id)) |
| 231 return; | 218 return; |
| 232 | 219 |
| 233 CancelRecognitionAndInformDelegate(caller_id); | 220 CancelRecognitionAndInformDelegate(caller_id); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 | 268 |
| 282 // -------- SpeechRecognitionEventListener interface implementation. --------- | 269 // -------- SpeechRecognitionEventListener interface implementation. --------- |
| 283 | 270 |
| 284 void SpeechRecognitionManagerImpl::OnRecognitionResult( | 271 void SpeechRecognitionManagerImpl::OnRecognitionResult( |
| 285 int caller_id, const content::SpeechRecognitionResult& result) { | 272 int caller_id, const content::SpeechRecognitionResult& result) { |
| 286 DCHECK(HasPendingRequest(caller_id)); | 273 DCHECK(HasPendingRequest(caller_id)); |
| 287 GetDelegate(caller_id)->SetRecognitionResult(caller_id, result); | 274 GetDelegate(caller_id)->SetRecognitionResult(caller_id, result); |
| 288 } | 275 } |
| 289 | 276 |
| 290 void SpeechRecognitionManagerImpl::OnAudioEnd(int caller_id) { | 277 void SpeechRecognitionManagerImpl::OnAudioEnd(int caller_id) { |
| 278 if (recording_caller_id_ != caller_id) |
| 279 return; |
| 291 DCHECK_EQ(recording_caller_id_, caller_id); | 280 DCHECK_EQ(recording_caller_id_, caller_id); |
| 292 DCHECK(HasPendingRequest(caller_id)); | 281 DCHECK(HasPendingRequest(caller_id)); |
| 282 if (!requests_[caller_id].is_active) |
| 283 return; |
| 293 recording_caller_id_ = 0; | 284 recording_caller_id_ = 0; |
| 294 GetDelegate(caller_id)->DidCompleteRecording(caller_id); | 285 GetDelegate(caller_id)->DidCompleteRecording(caller_id); |
| 295 if (delegate_) | 286 if (delegate_) |
| 296 delegate_->ShowRecognizing(caller_id); | 287 delegate_->ShowRecognizing(caller_id); |
| 297 } | 288 } |
| 298 | 289 |
| 299 void SpeechRecognitionManagerImpl::OnRecognitionEnd(int caller_id) { | 290 void SpeechRecognitionManagerImpl::OnRecognitionEnd(int caller_id) { |
| 291 if (!HasPendingRequest(caller_id) || !requests_[caller_id].is_active) |
| 292 return; |
| 300 GetDelegate(caller_id)->DidCompleteRecognition(caller_id); | 293 GetDelegate(caller_id)->DidCompleteRecognition(caller_id); |
| 301 requests_.erase(caller_id); | 294 requests_.erase(caller_id); |
| 302 if (delegate_) | 295 if (delegate_) |
| 303 delegate_->DoClose(caller_id); | 296 delegate_->DoClose(caller_id); |
| 304 } | 297 } |
| 305 | 298 |
| 306 void SpeechRecognitionManagerImpl::OnSoundStart(int caller_id) { | 299 void SpeechRecognitionManagerImpl::OnSoundStart(int caller_id) { |
| 307 } | 300 } |
| 308 | 301 |
| 309 void SpeechRecognitionManagerImpl::OnSoundEnd(int caller_id) { | 302 void SpeechRecognitionManagerImpl::OnSoundEnd(int caller_id) { |
| 310 } | 303 } |
| 311 | 304 |
| 312 void SpeechRecognitionManagerImpl::OnRecognitionError( | 305 void SpeechRecognitionManagerImpl::OnRecognitionError( |
| 313 int caller_id, const content::SpeechRecognitionErrorCode& error) { | 306 int caller_id, const content::SpeechRecognitionError& error) { |
| 307 DCHECK(HasPendingRequest(caller_id)); |
| 314 if (caller_id == recording_caller_id_) | 308 if (caller_id == recording_caller_id_) |
| 315 recording_caller_id_ = 0; | 309 recording_caller_id_ = 0; |
| 316 requests_[caller_id].is_active = false; | 310 requests_[caller_id].is_active = false; |
| 317 if (delegate_) | 311 if (delegate_) { |
| 318 delegate_->ShowRecognizerError(caller_id, error); | 312 if (error.code == content::SPEECH_RECOGNITION_ERROR_AUDIO && |
| 313 error.details == content::SPEECH_AUDIO_ERROR_DETAILS_NO_MIC) { |
| 314 delegate_->ShowMicError(caller_id, |
| 315 SpeechRecognitionManagerDelegate::MIC_ERROR_NO_DEVICE_AVAILABLE); |
| 316 } else if (error.code == content::SPEECH_RECOGNITION_ERROR_AUDIO && |
| 317 error.details == content::SPEECH_AUDIO_ERROR_DETAILS_IN_USE) { |
| 318 delegate_->ShowMicError( |
| 319 caller_id, SpeechRecognitionManagerDelegate::MIC_ERROR_DEVICE_IN_USE); |
| 320 } else { |
| 321 delegate_->ShowRecognizerError(caller_id, error.code); |
| 322 } |
| 323 } |
| 319 } | 324 } |
| 320 | 325 |
| 321 void SpeechRecognitionManagerImpl::OnAudioStart(int caller_id) { | 326 void SpeechRecognitionManagerImpl::OnAudioStart(int caller_id) { |
| 322 DCHECK(HasPendingRequest(caller_id)); | 327 DCHECK(HasPendingRequest(caller_id)); |
| 323 DCHECK_EQ(recording_caller_id_, caller_id); | 328 DCHECK_EQ(recording_caller_id_, caller_id); |
| 324 if (delegate_) | 329 if (delegate_) |
| 325 delegate_->ShowRecording(caller_id); | 330 delegate_->ShowRecording(caller_id); |
| 326 } | 331 } |
| 327 | 332 |
| 328 void SpeechRecognitionManagerImpl::OnRecognitionStart(int caller_id) { | 333 void SpeechRecognitionManagerImpl::OnRecognitionStart(int caller_id) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 352 | 357 |
| 353 SpeechRecognitionManagerImpl::Request::Request() | 358 SpeechRecognitionManagerImpl::Request::Request() |
| 354 : delegate(NULL), | 359 : delegate(NULL), |
| 355 is_active(false) { | 360 is_active(false) { |
| 356 } | 361 } |
| 357 | 362 |
| 358 SpeechRecognitionManagerImpl::Request::~Request() { | 363 SpeechRecognitionManagerImpl::Request::~Request() { |
| 359 } | 364 } |
| 360 | 365 |
| 361 } // namespace speech | 366 } // namespace speech |
| OLD | NEW |