Chromium Code Reviews| Index: chrome/browser/ui/app_list/speech_recognizer.cc |
| diff --git a/chrome/browser/ui/app_list/speech_recognizer.cc b/chrome/browser/ui/app_list/speech_recognizer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..39178856c094f51f95c5a893ca6f2b3cefd368f1 |
| --- /dev/null |
| +++ b/chrome/browser/ui/app_list/speech_recognizer.cc |
| @@ -0,0 +1,209 @@ |
| +// Copyright 2014 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 "chrome/browser/ui/app_list/speech_recognizer.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/bind.h" |
| +#include "base/strings/string16.h" |
| +#include "chrome/browser/ui/app_list/speech_recognizer_delegate.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/speech_recognition_manager.h" |
| +#include "content/public/browser/speech_recognition_session_config.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/common/speech_recognition_error.h" |
| + |
| +namespace app_list { |
| + |
| +// 5 second timeout to cancel if there's no speech heard. |
|
Matt Giuca
2014/11/03 02:35:52
"// Length of timeout ..." (don't include the valu
Anand Mistry (off Chromium)
2014/11/03 06:51:55
Done.
|
| +static const int kNoSpeechTimeoutInSeconds = 5; |
| + |
| +SpeechRecognizer::SpeechRecognizer( |
| + SpeechRecognizerDelegate* delegate, |
| + net::URLRequestContextGetter* url_request_context_getter, |
| + const std::string& locale) |
| + : delegate_(delegate), |
| + url_request_context_getter_(url_request_context_getter), |
| + locale_(locale), |
| + speech_timeout_(false, false), |
| + session_(-1), |
| + weak_factory_(this) { |
| +} |
| + |
| +SpeechRecognizer::~SpeechRecognizer() { |
| +} |
| + |
| +void SpeechRecognizer::Start() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + // The speech recognizer checks to see if the request is allowed by looking |
| + // up the renderer process. A renderer containing the app-list is hard-coded |
| + // to be allowed. |
| + content::WebContents* contents = delegate_->GetSpeechContents(); |
| + if (!contents) |
| + return; |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(&SpeechRecognizer::StartOnIOThread, |
| + this, |
| + contents->GetRenderProcessHost()->GetID(), |
| + url_request_context_getter_)); |
| +} |
| + |
| +void SpeechRecognizer::StartOnIOThread( |
| + int render_process_id, |
| + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + if (session_ != -1) |
| + StopOnIOThread(); |
| + |
| + content::SpeechRecognitionSessionConfig config; |
| + config.language = locale_; |
| + config.is_legacy_api = false; |
| + config.continuous = true; |
| + config.interim_results = true; |
| + config.max_hypotheses = 1; |
| + config.filter_profanities = true; |
| + config.url_request_context_getter = url_request_context_getter; |
| + config.event_listener = weak_factory_.GetWeakPtr(); |
| + config.initial_context.render_process_id = render_process_id; |
| + |
| + auto speech_instance = content::SpeechRecognitionManager::GetInstance(); |
| + session_ = speech_instance->CreateSession(config); |
| + speech_instance->StartSession(session_); |
| +} |
| + |
| +void SpeechRecognizer::Stop() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(&SpeechRecognizer::StopOnIOThread, this)); |
| +} |
| + |
| +void SpeechRecognizer::StopOnIOThread() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + if (session_ == -1) |
| + return; |
| + |
| + // Prevent recursion. |
| + int session = session_; |
| + session_ = -1; |
| + StopSpeechTimeout(); |
| + content::SpeechRecognitionManager::GetInstance()->StopAudioCaptureForSession( |
| + session); |
| +} |
| + |
| +void SpeechRecognizer::NotifyRecognitionStateChanged( |
| + SpeechRecognitionState new_state) { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&SpeechRecognizerDelegate::OnSpeechRecognitionStateChanged, |
| + base::Unretained(delegate_), |
| + new_state)); |
| +} |
| + |
| +void SpeechRecognizer::StartSpeechTimeout() { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&base::Timer::Start, |
| + base::Unretained(&speech_timeout_), |
| + FROM_HERE, |
| + base::TimeDelta::FromSeconds(kNoSpeechTimeoutInSeconds), |
| + base::Bind(&SpeechRecognizer::SpeechTimeout, this))); |
| +} |
| + |
| +void SpeechRecognizer::StopSpeechTimeout() { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&base::Timer::Stop, |
| + base::Unretained(&speech_timeout_))); |
| +} |
| + |
| +void SpeechRecognizer::SpeechTimeout() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + Stop(); |
| +} |
| + |
| +void SpeechRecognizer::OnRecognitionStart(int session_id) { |
| + NotifyRecognitionStateChanged(SPEECH_RECOGNITION_RECOGNIZING); |
| +} |
| + |
| +void SpeechRecognizer::OnRecognitionEnd(int session_id) { |
| + StopOnIOThread(); |
| + NotifyRecognitionStateChanged(SPEECH_RECOGNITION_READY); |
| +} |
| + |
| +void SpeechRecognizer::OnRecognitionResults( |
| + int session_id, const content::SpeechRecognitionResults& results) { |
| + base::string16 result_str; |
| + size_t final_count = 0; |
| + for (const auto& result : results) { |
| + if (!result.is_provisional) |
| + final_count++; |
| + result_str += result.hypotheses[0].utterance; |
| + } |
| + StopSpeechTimeout(); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&SpeechRecognizerDelegate::OnSpeechResult, |
| + base::Unretained(delegate_), |
| + result_str, |
| + final_count == results.size())); |
| + |
| + // Stop the moment we have a final result. |
| + if (final_count == results.size()) |
| + StopOnIOThread(); |
| +} |
| + |
| +void SpeechRecognizer::OnRecognitionError( |
| + int session_id, const content::SpeechRecognitionError& error) { |
| + StopOnIOThread(); |
| + if (error.code == content::SPEECH_RECOGNITION_ERROR_NETWORK) { |
| + NotifyRecognitionStateChanged(SPEECH_RECOGNITION_NETWORK_ERROR); |
| + } |
| + NotifyRecognitionStateChanged(SPEECH_RECOGNITION_READY); |
| +} |
| + |
| +void SpeechRecognizer::OnSoundStart(int session_id) { |
| + StartSpeechTimeout(); |
| + NotifyRecognitionStateChanged(SPEECH_RECOGNITION_IN_SPEECH); |
| +} |
| + |
| +void SpeechRecognizer::OnSoundEnd(int session_id) { |
| + StopOnIOThread(); |
| + NotifyRecognitionStateChanged(SPEECH_RECOGNITION_RECOGNIZING); |
| +} |
| + |
| +void SpeechRecognizer::OnAudioLevelsChange( |
| + int session_id, float volume, float noise_volume) { |
| + volume = std::max(0.0f, volume - noise_volume); |
| + // Both |volume| and |noise_volume| are defined to be in the range [0.0, 1.0]. |
| + // See: content/public/browser/speech_recognition_event_listener.h |
| + int16_t sound_level = static_cast<int16_t>(INT16_MAX * volume); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&SpeechRecognizerDelegate::OnSpeechSoundLevelChanged, |
| + base::Unretained(delegate_), |
| + sound_level)); |
| +} |
| + |
| +void SpeechRecognizer::OnEnvironmentEstimationComplete(int session_id) { |
| +} |
| + |
| +void SpeechRecognizer::OnAudioStart(int session_id) { |
| +} |
| + |
| +void SpeechRecognizer::OnAudioEnd(int session_id) { |
| +} |
| + |
| +} // namespace app_list |