| 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/google_one_shot_remote_engine.h" | 5 #include "content/browser/speech/google_one_shot_remote_engine.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 const char* const kDefaultSpeechRecognitionUrl = | 29 const char* const kDefaultSpeechRecognitionUrl = |
| 30 "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&"; | 30 "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&"; |
| 31 const char* const kStatusString = "status"; | 31 const char* const kStatusString = "status"; |
| 32 const char* const kHypothesesString = "hypotheses"; | 32 const char* const kHypothesesString = "hypotheses"; |
| 33 const char* const kUtteranceString = "utterance"; | 33 const char* const kUtteranceString = "utterance"; |
| 34 const char* const kConfidenceString = "confidence"; | 34 const char* const kConfidenceString = "confidence"; |
| 35 const int kWebServiceStatusNoError = 0; | 35 const int kWebServiceStatusNoError = 0; |
| 36 const int kWebServiceStatusNoSpeech = 4; | 36 const int kWebServiceStatusNoSpeech = 4; |
| 37 const int kWebServiceStatusNoMatch = 5; | 37 const int kWebServiceStatusNoMatch = 5; |
| 38 const int kDefaultConfigSampleRate = 8000; | |
| 39 const int kDefaultConfigBitsPerSample = 16; | |
| 40 const speech::AudioEncoder::Codec kDefaultAudioCodec = | 38 const speech::AudioEncoder::Codec kDefaultAudioCodec = |
| 41 speech::AudioEncoder::CODEC_FLAC; | 39 speech::AudioEncoder::CODEC_FLAC; |
| 42 // TODO(satish): Remove this hardcoded value once the page is allowed to | 40 // TODO(satish): Remove this hardcoded value once the page is allowed to |
| 43 // set this via an attribute. | 41 // set this via an attribute. |
| 44 const int kMaxResults = 6; | 42 const int kMaxResults = 6; |
| 45 | 43 |
| 46 bool ParseServerResponse(const std::string& response_body, | 44 bool ParseServerResponse(const std::string& response_body, |
| 47 SpeechRecognitionResult* result, | 45 SpeechRecognitionResult* result, |
| 48 SpeechRecognitionError* error) { | 46 SpeechRecognitionError* error) { |
| 49 if (response_body.empty()) { | 47 if (response_body.empty()) { |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 return true; | 147 return true; |
| 150 } | 148 } |
| 151 | 149 |
| 152 } // namespace | 150 } // namespace |
| 153 | 151 |
| 154 namespace speech { | 152 namespace speech { |
| 155 | 153 |
| 156 const int GoogleOneShotRemoteEngine::kAudioPacketIntervalMs = 100; | 154 const int GoogleOneShotRemoteEngine::kAudioPacketIntervalMs = 100; |
| 157 int GoogleOneShotRemoteEngine::url_fetcher_id_for_tests = 0; | 155 int GoogleOneShotRemoteEngine::url_fetcher_id_for_tests = 0; |
| 158 | 156 |
| 159 GoogleOneShotRemoteEngineConfig::GoogleOneShotRemoteEngineConfig() | |
| 160 : filter_profanities(false), | |
| 161 audio_sample_rate(kDefaultConfigSampleRate), | |
| 162 audio_num_bits_per_sample(kDefaultConfigBitsPerSample) { | |
| 163 } | |
| 164 | |
| 165 GoogleOneShotRemoteEngineConfig::~GoogleOneShotRemoteEngineConfig() {} | |
| 166 | |
| 167 GoogleOneShotRemoteEngine::GoogleOneShotRemoteEngine( | 157 GoogleOneShotRemoteEngine::GoogleOneShotRemoteEngine( |
| 168 net::URLRequestContextGetter* context) | 158 net::URLRequestContextGetter* context) |
| 169 : url_context_(context) { | 159 : url_context_(context) { |
| 170 } | 160 } |
| 171 | 161 |
| 172 GoogleOneShotRemoteEngine::~GoogleOneShotRemoteEngine() {} | 162 GoogleOneShotRemoteEngine::~GoogleOneShotRemoteEngine() {} |
| 173 | 163 |
| 174 void GoogleOneShotRemoteEngine::SetConfig( | 164 void GoogleOneShotRemoteEngine::SetConfig( |
| 175 const GoogleOneShotRemoteEngineConfig& config) { | 165 const SpeechRecognitionEngineConfig& config) { |
| 176 config_ = config; | 166 config_ = config; |
| 177 } | 167 } |
| 178 | 168 |
| 179 void GoogleOneShotRemoteEngine::StartRecognition() { | 169 void GoogleOneShotRemoteEngine::StartRecognition() { |
| 180 DCHECK(delegate()); | 170 DCHECK(delegate()); |
| 181 DCHECK(!url_fetcher_.get()); | 171 DCHECK(!url_fetcher_.get()); |
| 182 std::string lang_param = config_.language; | 172 std::string lang_param = config_.language; |
| 183 | 173 |
| 184 if (lang_param.empty() && url_context_) { | 174 if (lang_param.empty() && url_context_) { |
| 185 // If no language is provided then we use the first from the accepted | 175 // If no language is provided then we use the first from the accepted |
| 186 // language list. If this list is empty then it defaults to "en-US". | 176 // language list. If this list is empty then it defaults to "en-US". |
| 187 // Example of the contents of this list: "es,en-GB;q=0.8", "" | 177 // Example of the contents of this list: "es,en-GB;q=0.8", "" |
| 188 net::URLRequestContext* request_context = | 178 net::URLRequestContext* request_context = |
| 189 url_context_->GetURLRequestContext(); | 179 url_context_->GetURLRequestContext(); |
| 190 DCHECK(request_context); | 180 DCHECK(request_context); |
| 191 std::string accepted_language_list = request_context->accept_language(); | 181 std::string accepted_language_list = request_context->accept_language(); |
| 192 size_t separator = accepted_language_list.find_first_of(",;"); | 182 size_t separator = accepted_language_list.find_first_of(",;"); |
| 193 lang_param = accepted_language_list.substr(0, separator); | 183 lang_param = accepted_language_list.substr(0, separator); |
| 194 } | 184 } |
| 195 | 185 |
| 196 if (lang_param.empty()) | 186 if (lang_param.empty()) |
| 197 lang_param = "en-US"; | 187 lang_param = "en-US"; |
| 198 | 188 |
| 199 std::vector<std::string> parts; | 189 std::vector<std::string> parts; |
| 200 parts.push_back("lang=" + net::EscapeQueryParamValue(lang_param, true)); | 190 parts.push_back("lang=" + net::EscapeQueryParamValue(lang_param, true)); |
| 201 | 191 |
| 202 if (!config_.grammar.empty()) | 192 if (!config_.grammars.empty()) { |
| 203 parts.push_back("lm=" + net::EscapeQueryParamValue(config_.grammar, true)); | 193 DCHECK_EQ(config_.grammars.size(), 1U); |
| 194 parts.push_back("lm=" + net::EscapeQueryParamValue(config_.grammars[0].url, |
| 195 true)); |
| 196 } |
| 204 | 197 |
| 205 if (!config_.hardware_info.empty()) | 198 if (!config_.hardware_info.empty()) |
| 206 parts.push_back("xhw=" + net::EscapeQueryParamValue(config_.hardware_info, | 199 parts.push_back("xhw=" + net::EscapeQueryParamValue(config_.hardware_info, |
| 207 true)); | 200 true)); |
| 208 parts.push_back("maxresults=" + base::IntToString(kMaxResults)); | 201 parts.push_back("maxresults=" + base::IntToString(kMaxResults)); |
| 209 parts.push_back(config_.filter_profanities ? "pfilter=2" : "pfilter=0"); | 202 parts.push_back(config_.filter_profanities ? "pfilter=2" : "pfilter=0"); |
| 210 | 203 |
| 211 GURL url(std::string(kDefaultSpeechRecognitionUrl) + JoinString(parts, '&')); | 204 GURL url(std::string(kDefaultSpeechRecognitionUrl) + JoinString(parts, '&')); |
| 212 | 205 |
| 213 encoder_.reset(AudioEncoder::Create(kDefaultAudioCodec, | 206 encoder_.reset(AudioEncoder::Create(kDefaultAudioCodec, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 | 285 |
| 293 bool GoogleOneShotRemoteEngine::IsRecognitionPending() const { | 286 bool GoogleOneShotRemoteEngine::IsRecognitionPending() const { |
| 294 return url_fetcher_ != NULL; | 287 return url_fetcher_ != NULL; |
| 295 } | 288 } |
| 296 | 289 |
| 297 int GoogleOneShotRemoteEngine::GetDesiredAudioChunkDurationMs() const { | 290 int GoogleOneShotRemoteEngine::GetDesiredAudioChunkDurationMs() const { |
| 298 return kAudioPacketIntervalMs; | 291 return kAudioPacketIntervalMs; |
| 299 } | 292 } |
| 300 | 293 |
| 301 } // namespace speech | 294 } // namespace speech |
| OLD | NEW |