Index: chrome/browser/ui/app_list/app_list_speech_recognizer.cc |
diff --git a/chrome/browser/ui/app_list/app_list_speech_recognizer.cc b/chrome/browser/ui/app_list/app_list_speech_recognizer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..913436758098f35f35b9943cf66d243f09806ab8 |
--- /dev/null |
+++ b/chrome/browser/ui/app_list/app_list_speech_recognizer.cc |
@@ -0,0 +1,195 @@ |
+// 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/app_list_speech_recognizer.h" |
+ |
+#include <algorithm> |
+ |
+#include "base/bind.h" |
+#include "base/strings/string16.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ui/app_list/start_page_service.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/common/speech_recognition_error.h" |
+#include "ui/app_list/speech_ui_model_observer.h" |
Lei Zhang
2014/10/30 05:15:42
not used?
Anand Mistry (off Chromium)
2014/10/31 00:50:06
For SpeechRecognitionState, but also declared in t
|
+ |
+namespace app_list { |
+ |
+// 5 second timeout to cancel if there's no speech heard. |
+static const int kNoSpeechTimeout = 5; |
Lei Zhang
2014/10/30 05:15:42
Add "InSeconds" to the name so it's obvious you go
Anand Mistry (off Chromium)
2014/10/31 00:50:06
Done.
|
+ |
+AppListSpeechRecognizer::AppListSpeechRecognizer( |
+ StartPageService* start_page_service, |
+ const std::string& locale) |
+ : start_page_service_(start_page_service), |
+ locale_(locale), |
+ speech_timeout_(false, false), |
+ session_(-1), |
+ weak_factory_(this) { |
+} |
+ |
+AppListSpeechRecognizer::~AppListSpeechRecognizer() { |
+} |
+ |
+void AppListSpeechRecognizer::Start() { |
+ // 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. |
+ // TODO(amistry): Somehow eliminate this dependency. |
+ content::WebContents* contents = |
+ start_page_service_->GetSpeechRecognitionContents(); |
+ if (!contents) |
+ return; |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, |
+ FROM_HERE, |
+ base::Bind(&AppListSpeechRecognizer::StartOnIOThread, |
+ this, |
+ contents->GetRenderProcessHost()->GetID(), |
+ make_scoped_refptr( |
+ start_page_service_->profile()->GetRequestContext()))); |
+} |
+ |
+void AppListSpeechRecognizer::StartOnIOThread( |
+ int render_process_id, |
+ scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { |
Lei Zhang
2014/10/30 05:15:42
You should add a DCHECK_CURRENTLY_ON() call here,
Anand Mistry (off Chromium)
2014/10/31 00:50:06
Done.
Lei Zhang
2014/10/31 01:18:04
An alternate strategy that may work is to have Spe
Anand Mistry (off Chromium)
2014/11/03 06:51:55
I've tried to do this, and the lifecycle is more c
|
+ 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 AppListSpeechRecognizer::Stop() { |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, |
+ FROM_HERE, |
+ base::Bind(&AppListSpeechRecognizer::StopOnIOThread, this)); |
+} |
+ |
+void AppListSpeechRecognizer::StopOnIOThread() { |
+ if (session_ == -1) |
+ return; |
+ |
+ // Prevent recursion. |
+ int session = session_; |
+ session_ = -1; |
+ StopSpeechTimeout(); |
+ content::SpeechRecognitionManager::GetInstance()->StopAudioCaptureForSession( |
+ session); |
+} |
+ |
+void AppListSpeechRecognizer::NotifyRecognitionStateChanged( |
+ SpeechRecognitionState new_state) { |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(&StartPageService::OnSpeechRecognitionStateChanged, |
+ base::Unretained(start_page_service_), |
+ new_state)); |
+} |
+ |
+void AppListSpeechRecognizer::StartSpeechTimeout() { |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(&base::Timer::Start, |
+ base::Unretained(&speech_timeout_), |
+ FROM_HERE, |
+ base::TimeDelta::FromSeconds(kNoSpeechTimeout), |
+ base::Bind(&AppListSpeechRecognizer::SpeechTimeout, this))); |
+} |
+ |
+void AppListSpeechRecognizer::StopSpeechTimeout() { |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(&base::Timer::Stop, |
+ base::Unretained(&speech_timeout_))); |
+} |
+ |
+void AppListSpeechRecognizer::SpeechTimeout() { |
+ Stop(); |
+} |
+ |
+void AppListSpeechRecognizer::OnRecognitionStart(int session_id) { |
+ NotifyRecognitionStateChanged(SPEECH_RECOGNITION_RECOGNIZING); |
+} |
+ |
+void AppListSpeechRecognizer::OnRecognitionEnd(int session_id) { |
+ StopOnIOThread(); |
+ NotifyRecognitionStateChanged(SPEECH_RECOGNITION_READY); |
+} |
+ |
+void AppListSpeechRecognizer::OnRecognitionResults( |
+ int session_id, const content::SpeechRecognitionResults& results) { |
+ base::string16 result_str; |
+ unsigned int final_count = 0; |
Lei Zhang
2014/10/30 05:15:42
use size_t
Anand Mistry (off Chromium)
2014/10/31 00:50:06
Done.
|
+ 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(&StartPageService::OnSpeechResult, |
+ base::Unretained(start_page_service_), |
+ result_str, |
+ final_count == results.size())); |
+ |
+ // Stop the moment we have a final result. |
+ if (final_count == results.size()) |
+ StopOnIOThread(); |
+} |
+ |
+void AppListSpeechRecognizer::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 AppListSpeechRecognizer::OnSoundStart(int session_id) { |
+ StartSpeechTimeout(); |
+ NotifyRecognitionStateChanged(SPEECH_RECOGNITION_IN_SPEECH); |
+} |
+ |
+void AppListSpeechRecognizer::OnSoundEnd(int session_id) { |
+ StopOnIOThread(); |
+ NotifyRecognitionStateChanged(SPEECH_RECOGNITION_RECOGNIZING); |
+} |
+ |
+void AppListSpeechRecognizer::OnAudioLevelsChange( |
+ int session_id, float volume, float noise_volume) { |
+ volume = std::max(0.0f, volume - noise_volume); |
+ int16_t sound_level = static_cast<int16_t>(INT16_MAX * volume); |
Lei Zhang
2014/10/30 05:15:42
can't this overflow if |volume| > 1 ?
Anand Mistry (off Chromium)
2014/10/31 00:50:06
It is defined to be in the range [0.0, 1.0]
Lei Zhang
2014/10/31 01:18:04
Add a DCHECK?
Anand Mistry (off Chromium)
2014/11/03 06:51:55
Done.
|
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(&StartPageService::OnSpeechSoundLevelChanged, |
+ base::Unretained(start_page_service_), |
+ sound_level)); |
+} |
+ |
+} // namespace app_list |