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/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 const char* const kDefaultSpeechRecognitionUrl = | 28 const char* const kDefaultSpeechRecognitionUrl = |
29 "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&"; | 29 "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&"; |
30 const char* const kStatusString = "status"; | 30 const char* const kStatusString = "status"; |
31 const char* const kHypothesesString = "hypotheses"; | 31 const char* const kHypothesesString = "hypotheses"; |
32 const char* const kUtteranceString = "utterance"; | 32 const char* const kUtteranceString = "utterance"; |
33 const char* const kConfidenceString = "confidence"; | 33 const char* const kConfidenceString = "confidence"; |
34 const int kWebServiceStatusNoError = 0; | 34 const int kWebServiceStatusNoError = 0; |
35 const int kWebServiceStatusNoSpeech = 4; | 35 const int kWebServiceStatusNoSpeech = 4; |
36 const int kWebServiceStatusNoMatch = 5; | 36 const int kWebServiceStatusNoMatch = 5; |
37 const AudioEncoder::Codec kDefaultAudioCodec = AudioEncoder::CODEC_FLAC; | |
38 | 37 |
39 bool ParseServerResponse(const std::string& response_body, | 38 bool ParseServerResponse(const std::string& response_body, |
40 SpeechRecognitionResult* result, | 39 SpeechRecognitionResult* result, |
41 SpeechRecognitionError* error) { | 40 SpeechRecognitionError* error) { |
42 if (response_body.empty()) { | 41 if (response_body.empty()) { |
43 LOG(WARNING) << "ParseServerResponse: Response was empty."; | 42 LOG(WARNING) << "ParseServerResponse: Response was empty."; |
44 return false; | 43 return false; |
45 } | 44 } |
46 DVLOG(1) << "ParseServerResponse: Parsing response " << response_body; | 45 DVLOG(1) << "ParseServerResponse: Parsing response " << response_body; |
47 | 46 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 true)); | 200 true)); |
202 parts.push_back("maxresults=" + base::UintToString(config_.max_hypotheses)); | 201 parts.push_back("maxresults=" + base::UintToString(config_.max_hypotheses)); |
203 parts.push_back(config_.filter_profanities ? "pfilter=2" : "pfilter=0"); | 202 parts.push_back(config_.filter_profanities ? "pfilter=2" : "pfilter=0"); |
204 | 203 |
205 std::string api_key = google_apis::GetAPIKey(); | 204 std::string api_key = google_apis::GetAPIKey(); |
206 parts.push_back("key=" + net::EscapeQueryParamValue(api_key, true)); | 205 parts.push_back("key=" + net::EscapeQueryParamValue(api_key, true)); |
207 | 206 |
208 GURL url(std::string(kDefaultSpeechRecognitionUrl) + | 207 GURL url(std::string(kDefaultSpeechRecognitionUrl) + |
209 base::JoinString(parts, "&")); | 208 base::JoinString(parts, "&")); |
210 | 209 |
211 encoder_.reset(AudioEncoder::Create(kDefaultAudioCodec, | 210 encoder_.reset(AudioEncoder::Create(config_.audio_sample_rate, |
212 config_.audio_sample_rate, | |
213 config_.audio_num_bits_per_sample)); | 211 config_.audio_num_bits_per_sample)); |
214 DCHECK(encoder_.get()); | 212 DCHECK(encoder_.get()); |
215 url_fetcher_ = net::URLFetcher::Create(url_fetcher_id_for_tests, url, | 213 url_fetcher_ = net::URLFetcher::Create(url_fetcher_id_for_tests, url, |
216 net::URLFetcher::POST, this); | 214 net::URLFetcher::POST, this); |
217 url_fetcher_->SetChunkedUpload(encoder_->mime_type()); | 215 url_fetcher_->SetChunkedUpload(encoder_->mime_type()); |
218 url_fetcher_->SetRequestContext(url_context_.get()); | 216 url_fetcher_->SetRequestContext(url_context_.get()); |
219 url_fetcher_->SetReferrer(config_.origin_url); | 217 url_fetcher_->SetReferrer(config_.origin_url); |
220 | 218 |
221 // The speech recognition API does not require user identification as part | 219 // The speech recognition API does not require user identification as part |
222 // of requests, so we don't send cookies or auth data for these requests to | 220 // of requests, so we don't send cookies or auth data for these requests to |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 | 286 |
289 bool GoogleOneShotRemoteEngine::IsRecognitionPending() const { | 287 bool GoogleOneShotRemoteEngine::IsRecognitionPending() const { |
290 return url_fetcher_ != NULL; | 288 return url_fetcher_ != NULL; |
291 } | 289 } |
292 | 290 |
293 int GoogleOneShotRemoteEngine::GetDesiredAudioChunkDurationMs() const { | 291 int GoogleOneShotRemoteEngine::GetDesiredAudioChunkDurationMs() const { |
294 return kAudioPacketIntervalMs; | 292 return kAudioPacketIntervalMs; |
295 } | 293 } |
296 | 294 |
297 } // namespace content | 295 } // namespace content |
OLD | NEW |