| 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/memory/scoped_ptr.h" | |
| 11 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 12 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 13 #include "base/values.h" | 12 #include "base/values.h" |
| 14 #include "content/browser/speech/audio_buffer.h" | 13 #include "content/browser/speech/audio_buffer.h" |
| 15 #include "content/common/net/url_fetcher_impl.h" | 14 #include "content/common/net/url_fetcher_impl.h" |
| 16 #include "content/public/common/speech_recognition_error.h" | 15 #include "content/public/common/speech_recognition_error.h" |
| 17 #include "content/public/common/speech_recognition_result.h" | 16 #include "content/public/common/speech_recognition_result.h" |
| 18 #include "net/base/escape.h" | 17 #include "net/base/escape.h" |
| 19 #include "net/base/load_flags.h" | 18 #include "net/base/load_flags.h" |
| 20 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 | 234 |
| 236 void GoogleOneShotRemoteEngine::EndRecognition() { | 235 void GoogleOneShotRemoteEngine::EndRecognition() { |
| 237 url_fetcher_.reset(); | 236 url_fetcher_.reset(); |
| 238 } | 237 } |
| 239 | 238 |
| 240 void GoogleOneShotRemoteEngine::TakeAudioChunk(const AudioChunk& data) { | 239 void GoogleOneShotRemoteEngine::TakeAudioChunk(const AudioChunk& data) { |
| 241 DCHECK(url_fetcher_.get()); | 240 DCHECK(url_fetcher_.get()); |
| 242 DCHECK(encoder_.get()); | 241 DCHECK(encoder_.get()); |
| 243 DCHECK_EQ(data.bytes_per_sample(), config_.audio_num_bits_per_sample / 8); | 242 DCHECK_EQ(data.bytes_per_sample(), config_.audio_num_bits_per_sample / 8); |
| 244 encoder_->Encode(data); | 243 encoder_->Encode(data); |
| 245 scoped_ptr<AudioChunk> encoded_data(encoder_->GetEncodedDataAndClear()); | 244 scoped_refptr<AudioChunk> encoded_data(encoder_->GetEncodedDataAndClear()); |
| 246 url_fetcher_->AppendChunkToUpload(encoded_data->AsString(), false); | 245 url_fetcher_->AppendChunkToUpload(encoded_data->AsString(), false); |
| 247 } | 246 } |
| 248 | 247 |
| 249 void GoogleOneShotRemoteEngine::AudioChunksEnded() { | 248 void GoogleOneShotRemoteEngine::AudioChunksEnded() { |
| 250 DCHECK(url_fetcher_.get()); | 249 DCHECK(url_fetcher_.get()); |
| 251 DCHECK(encoder_.get()); | 250 DCHECK(encoder_.get()); |
| 252 | 251 |
| 253 // UploadAudioChunk requires a non-empty final buffer. So we encode a packet | 252 // UploadAudioChunk requires a non-empty final buffer. So we encode a packet |
| 254 // of silence in case encoder had no data already. | 253 // of silence in case encoder had no data already. |
| 255 std::vector<int16> samples( | 254 std::vector<int16> samples( |
| 256 config_.audio_sample_rate * kAudioPacketIntervalMs / 1000); | 255 config_.audio_sample_rate * kAudioPacketIntervalMs / 1000); |
| 257 AudioChunk dummy_chunk(reinterpret_cast<uint8*>(&samples[0]), | 256 scoped_refptr<AudioChunk> dummy_chunk( |
| 258 samples.size() * sizeof(int16), | 257 new AudioChunk(reinterpret_cast<uint8*>(&samples[0]), |
| 259 encoder_->bits_per_sample() / 8); | 258 samples.size() * sizeof(int16), |
| 260 encoder_->Encode(dummy_chunk); | 259 encoder_->bits_per_sample() / 8)); |
| 260 encoder_->Encode(*dummy_chunk); |
| 261 encoder_->Flush(); | 261 encoder_->Flush(); |
| 262 scoped_ptr<AudioChunk> encoded_dummy_data(encoder_->GetEncodedDataAndClear()); | 262 scoped_refptr<AudioChunk> encoded_dummy_data( |
| 263 encoder_->GetEncodedDataAndClear()); |
| 263 DCHECK(!encoded_dummy_data->IsEmpty()); | 264 DCHECK(!encoded_dummy_data->IsEmpty()); |
| 264 encoder_.reset(); | 265 encoder_.reset(); |
| 265 | 266 |
| 266 url_fetcher_->AppendChunkToUpload(encoded_dummy_data->AsString(), true); | 267 url_fetcher_->AppendChunkToUpload(encoded_dummy_data->AsString(), true); |
| 267 } | 268 } |
| 268 | 269 |
| 269 void GoogleOneShotRemoteEngine::OnURLFetchComplete( | 270 void GoogleOneShotRemoteEngine::OnURLFetchComplete( |
| 270 const content::URLFetcher* source) { | 271 const content::URLFetcher* source) { |
| 271 DCHECK_EQ(url_fetcher_.get(), source); | 272 DCHECK_EQ(url_fetcher_.get(), source); |
| 272 SpeechRecognitionResult result; | 273 SpeechRecognitionResult result; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 291 | 292 |
| 292 bool GoogleOneShotRemoteEngine::IsRecognitionPending() const { | 293 bool GoogleOneShotRemoteEngine::IsRecognitionPending() const { |
| 293 return url_fetcher_ != NULL; | 294 return url_fetcher_ != NULL; |
| 294 } | 295 } |
| 295 | 296 |
| 296 int GoogleOneShotRemoteEngine::GetDesiredAudioChunkDurationMs() const { | 297 int GoogleOneShotRemoteEngine::GetDesiredAudioChunkDurationMs() const { |
| 297 return kAudioPacketIntervalMs; | 298 return kAudioPacketIntervalMs; |
| 298 } | 299 } |
| 299 | 300 |
| 300 } // namespace speech | 301 } // namespace speech |
| OLD | NEW |