| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/speech_recognizer_impl.h" | 5 #include "content/browser/speech/speech_recognizer_impl.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/memory/shared_memory.h" |
| 9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 10 #include "content/browser/browser_main_loop.h" | 11 #include "content/browser/browser_main_loop.h" |
| 11 #include "content/browser/media/media_internals.h" | 12 #include "content/browser/media/media_internals.h" |
| 12 #include "content/browser/speech/audio_buffer.h" | 13 #include "content/browser/speech/audio_buffer.h" |
| 13 #include "content/browser/speech/google_one_shot_remote_engine.h" | 14 #include "content/browser/speech/google_one_shot_remote_engine.h" |
| 15 #include "content/browser/speech/speech_recognition_audio_receiver.h" |
| 16 #include "content/public/browser/browser_message_filter.h" |
| 14 #include "content/public/browser/speech_recognition_event_listener.h" | 17 #include "content/public/browser/speech_recognition_event_listener.h" |
| 15 #include "media/base/audio_converter.h" | 18 #include "media/base/audio_converter.h" |
| 16 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
| 17 | 20 |
| 18 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
| 19 #include "media/audio/win/core_audio_util_win.h" | 22 #include "media/audio/win/core_audio_util_win.h" |
| 20 #endif | 23 #endif |
| 21 | 24 |
| 22 using media::AudioBus; | 25 using media::AudioBus; |
| 23 using media::AudioConverter; | 26 using media::AudioConverter; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 34 class SpeechRecognizerImpl::OnDataConverter | 37 class SpeechRecognizerImpl::OnDataConverter |
| 35 : public media::AudioConverter::InputCallback { | 38 : public media::AudioConverter::InputCallback { |
| 36 public: | 39 public: |
| 37 OnDataConverter(const AudioParameters& input_params, | 40 OnDataConverter(const AudioParameters& input_params, |
| 38 const AudioParameters& output_params); | 41 const AudioParameters& output_params); |
| 39 virtual ~OnDataConverter(); | 42 virtual ~OnDataConverter(); |
| 40 | 43 |
| 41 // Converts input audio |data| bus into an AudioChunk where the input format | 44 // Converts input audio |data| bus into an AudioChunk where the input format |
| 42 // is given by |input_parameters_| and the output format by | 45 // is given by |input_parameters_| and the output format by |
| 43 // |output_parameters_|. | 46 // |output_parameters_|. |
| 47 // Skips conversion on |audio_converter_| when input and output formats match. |
| 44 scoped_refptr<AudioChunk> Convert(const AudioBus* data); | 48 scoped_refptr<AudioChunk> Convert(const AudioBus* data); |
| 45 | 49 |
| 46 private: | 50 private: |
| 47 // media::AudioConverter::InputCallback implementation. | 51 // media::AudioConverter::InputCallback implementation. |
| 48 virtual double ProvideInput(AudioBus* dest, | 52 virtual double ProvideInput(AudioBus* dest, |
| 49 base::TimeDelta buffer_delay) override; | 53 base::TimeDelta buffer_delay) override; |
| 50 | 54 |
| 51 // Handles resampling, buffering, and channel mixing between input and output | 55 // Handles resampling, buffering, and channel mixing between input and output |
| 52 // parameters. | 56 // parameters. |
| 53 AudioConverter audio_converter_; | 57 AudioConverter audio_converter_; |
| 54 | 58 |
| 55 scoped_ptr<AudioBus> input_bus_; | 59 scoped_ptr<AudioBus> input_bus_; |
| 56 scoped_ptr<AudioBus> output_bus_; | 60 scoped_ptr<AudioBus> output_bus_; |
| 57 const AudioParameters input_parameters_; | 61 const AudioParameters input_parameters_; |
| 58 const AudioParameters output_parameters_; | 62 const AudioParameters output_parameters_; |
| 59 bool waiting_for_input_; | 63 bool waiting_for_input_; |
| 64 bool need_conversion_; |
| 60 scoped_ptr<uint8[]> converted_data_; | 65 scoped_ptr<uint8[]> converted_data_; |
| 61 | 66 |
| 62 DISALLOW_COPY_AND_ASSIGN(OnDataConverter); | 67 DISALLOW_COPY_AND_ASSIGN(OnDataConverter); |
| 63 }; | 68 }; |
| 64 | 69 |
| 65 namespace { | 70 namespace { |
| 66 | 71 |
| 67 // The following constants are related to the volume level indicator shown in | 72 // The following constants are related to the volume level indicator shown in |
| 68 // the UI for recorded audio. | 73 // the UI for recorded audio. |
| 69 // Multiplier used when new volume is greater than previous level. | 74 // Multiplier used when new volume is greater than previous level. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 SpeechRecognizerImpl::OnDataConverter::OnDataConverter( | 122 SpeechRecognizerImpl::OnDataConverter::OnDataConverter( |
| 118 const AudioParameters& input_params, const AudioParameters& output_params) | 123 const AudioParameters& input_params, const AudioParameters& output_params) |
| 119 : audio_converter_(input_params, output_params, false), | 124 : audio_converter_(input_params, output_params, false), |
| 120 input_bus_(AudioBus::Create(input_params)), | 125 input_bus_(AudioBus::Create(input_params)), |
| 121 output_bus_(AudioBus::Create(output_params)), | 126 output_bus_(AudioBus::Create(output_params)), |
| 122 input_parameters_(input_params), | 127 input_parameters_(input_params), |
| 123 output_parameters_(output_params), | 128 output_parameters_(output_params), |
| 124 waiting_for_input_(false), | 129 waiting_for_input_(false), |
| 125 converted_data_(new uint8[output_parameters_.GetBytesPerBuffer()]) { | 130 converted_data_(new uint8[output_parameters_.GetBytesPerBuffer()]) { |
| 126 audio_converter_.AddInput(this); | 131 audio_converter_.AddInput(this); |
| 132 need_conversion_ = !input_parameters_.Equals(output_parameters_); |
| 127 } | 133 } |
| 128 | 134 |
| 129 SpeechRecognizerImpl::OnDataConverter::~OnDataConverter() { | 135 SpeechRecognizerImpl::OnDataConverter::~OnDataConverter() { |
| 130 // It should now be safe to unregister the converter since no more OnData() | 136 // It should now be safe to unregister the converter since no more OnData() |
| 131 // callbacks are outstanding at this point. | 137 // callbacks are outstanding at this point. |
| 132 audio_converter_.RemoveInput(this); | 138 audio_converter_.RemoveInput(this); |
| 133 } | 139 } |
| 134 | 140 |
| 135 scoped_refptr<AudioChunk> SpeechRecognizerImpl::OnDataConverter::Convert( | 141 scoped_refptr<AudioChunk> SpeechRecognizerImpl::OnDataConverter::Convert( |
| 136 const AudioBus* data) { | 142 const AudioBus* data) { |
| 137 CHECK_EQ(data->frames(), input_parameters_.frames_per_buffer()); | 143 CHECK_EQ(data->frames(), input_parameters_.frames_per_buffer()); |
| 138 | 144 |
| 139 data->CopyTo(input_bus_.get()); | 145 // Do conversion if needed - only when |input_parameters_| differ from |
| 140 | 146 // |output_parameters_|. |
| 141 waiting_for_input_ = true; | 147 if (need_conversion_) { |
| 142 audio_converter_.Convert(output_bus_.get()); | 148 data->CopyTo(input_bus_.get()); |
| 149 waiting_for_input_ = true; |
| 150 audio_converter_.Convert(output_bus_.get()); |
| 151 } else { |
| 152 data->CopyTo(output_bus_.get()); |
| 153 } |
| 143 | 154 |
| 144 output_bus_->ToInterleaved( | 155 output_bus_->ToInterleaved( |
| 145 output_bus_->frames(), output_parameters_.bits_per_sample() / 8, | 156 output_bus_->frames(), output_parameters_.bits_per_sample() / 8, |
| 146 converted_data_.get()); | 157 converted_data_.get()); |
| 147 | 158 |
| 148 // TODO(primiano): Refactor AudioChunk to avoid the extra-copy here | 159 // TODO(primiano): Refactor AudioChunk to avoid the extra-copy here |
| 149 // (see http://crbug.com/249316 for details). | 160 // (see http://crbug.com/249316 for details). |
| 150 return scoped_refptr<AudioChunk>(new AudioChunk( | 161 return scoped_refptr<AudioChunk>(new AudioChunk( |
| 151 converted_data_.get(), | 162 converted_data_.get(), |
| 152 output_parameters_.GetBytesPerBuffer(), | 163 output_parameters_.GetBytesPerBuffer(), |
| (...skipping 15 matching lines...) Expand all Loading... |
| 168 return 1; | 179 return 1; |
| 169 } | 180 } |
| 170 | 181 |
| 171 // SpeechRecognizerImpl implementation | 182 // SpeechRecognizerImpl implementation |
| 172 | 183 |
| 173 SpeechRecognizerImpl::SpeechRecognizerImpl( | 184 SpeechRecognizerImpl::SpeechRecognizerImpl( |
| 174 SpeechRecognitionEventListener* listener, | 185 SpeechRecognitionEventListener* listener, |
| 175 int session_id, | 186 int session_id, |
| 176 bool continuous, | 187 bool continuous, |
| 177 bool provisional_results, | 188 bool provisional_results, |
| 189 bool using_audio_receiver, |
| 190 const base::ProcessHandle peer_process_handle, |
| 178 SpeechRecognitionEngine* engine) | 191 SpeechRecognitionEngine* engine) |
| 179 : SpeechRecognizer(listener, session_id), | 192 : SpeechRecognizer(listener, session_id), |
| 180 recognition_engine_(engine), | 193 recognition_engine_(engine), |
| 181 endpointer_(kAudioSampleRate), | 194 endpointer_(kAudioSampleRate), |
| 182 audio_log_(MediaInternals::GetInstance()->CreateAudioLog( | 195 audio_log_(MediaInternals::GetInstance()->CreateAudioLog( |
| 183 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER)), | 196 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER)), |
| 184 is_dispatching_event_(false), | 197 is_dispatching_event_(false), |
| 185 provisional_results_(provisional_results), | 198 provisional_results_(provisional_results), |
| 199 using_audio_receiver_(using_audio_receiver), |
| 200 peer_process_handle_(peer_process_handle), |
| 186 state_(STATE_IDLE) { | 201 state_(STATE_IDLE) { |
| 187 DCHECK(recognition_engine_ != NULL); | 202 DCHECK(recognition_engine_ != NULL); |
| 188 if (!continuous) { | 203 if (!continuous) { |
| 189 // In single shot (non-continous) recognition, | 204 // In single shot (non-continous) recognition, |
| 190 // the session is automatically ended after: | 205 // the session is automatically ended after: |
| 191 // - 0.5 seconds of silence if time < 3 seconds | 206 // - 0.5 seconds of silence if time < 3 seconds |
| 192 // - 1 seconds of silence if time >= 3 seconds | 207 // - 1 seconds of silence if time >= 3 seconds |
| 193 endpointer_.set_speech_input_complete_silence_length( | 208 endpointer_.set_speech_input_complete_silence_length( |
| 194 base::Time::kMicrosecondsPerSecond / 2); | 209 base::Time::kMicrosecondsPerSecond / 2); |
| 195 endpointer_.set_long_speech_input_complete_silence_length( | 210 endpointer_.set_long_speech_input_complete_silence_length( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 207 } | 222 } |
| 208 | 223 |
| 209 // ------- Methods that trigger Finite State Machine (FSM) events ------------ | 224 // ------- Methods that trigger Finite State Machine (FSM) events ------------ |
| 210 | 225 |
| 211 // NOTE:all the external events and requests should be enqueued (PostTask), even | 226 // NOTE:all the external events and requests should be enqueued (PostTask), even |
| 212 // if they come from the same (IO) thread, in order to preserve the relationship | 227 // if they come from the same (IO) thread, in order to preserve the relationship |
| 213 // of causality between events and avoid interleaved event processing due to | 228 // of causality between events and avoid interleaved event processing due to |
| 214 // synchronous callbacks. | 229 // synchronous callbacks. |
| 215 | 230 |
| 216 void SpeechRecognizerImpl::StartRecognition(const std::string& device_id) { | 231 void SpeechRecognizerImpl::StartRecognition(const std::string& device_id) { |
| 217 DCHECK(!device_id.empty()); | 232 DCHECK(!device_id.empty() ^ using_audio_receiver_); |
| 218 device_id_ = device_id; | 233 device_id_ = device_id; |
| 219 | 234 |
| 220 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 235 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 221 base::Bind(&SpeechRecognizerImpl::DispatchEvent, | 236 base::Bind(&SpeechRecognizerImpl::DispatchEvent, |
| 222 this, FSMEventArgs(EVENT_START))); | 237 this, FSMEventArgs(EVENT_START))); |
| 223 } | 238 } |
| 224 | 239 |
| 225 void SpeechRecognizerImpl::AbortRecognition() { | 240 void SpeechRecognizerImpl::AbortRecognition() { |
| 226 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 241 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 227 base::Bind(&SpeechRecognizerImpl::DispatchEvent, | 242 base::Bind(&SpeechRecognizerImpl::DispatchEvent, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 238 // Checking the FSM state from another thread (thus, while the FSM is | 253 // Checking the FSM state from another thread (thus, while the FSM is |
| 239 // potentially concurrently evolving) is meaningless. | 254 // potentially concurrently evolving) is meaningless. |
| 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 241 return state_ != STATE_IDLE && state_ != STATE_ENDED; | 256 return state_ != STATE_IDLE && state_ != STATE_ENDED; |
| 242 } | 257 } |
| 243 | 258 |
| 244 bool SpeechRecognizerImpl::IsCapturingAudio() const { | 259 bool SpeechRecognizerImpl::IsCapturingAudio() const { |
| 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); // See IsActive(). | 260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); // See IsActive(). |
| 246 const bool is_capturing_audio = state_ >= STATE_STARTING && | 261 const bool is_capturing_audio = state_ >= STATE_STARTING && |
| 247 state_ <= STATE_RECOGNIZING; | 262 state_ <= STATE_RECOGNIZING; |
| 248 DCHECK((is_capturing_audio && (audio_controller_.get() != NULL)) || | 263 DCHECK((is_capturing_audio && |
| 264 (using_audio_receiver_ || (audio_controller_.get() != NULL))) || |
| 249 (!is_capturing_audio && audio_controller_.get() == NULL)); | 265 (!is_capturing_audio && audio_controller_.get() == NULL)); |
| 250 return is_capturing_audio; | 266 return is_capturing_audio; |
| 251 } | 267 } |
| 252 | 268 |
| 253 const SpeechRecognitionEngine& | 269 const SpeechRecognitionEngine& |
| 254 SpeechRecognizerImpl::recognition_engine() const { | 270 SpeechRecognizerImpl::recognition_engine() const { |
| 255 return *(recognition_engine_.get()); | 271 return *(recognition_engine_.get()); |
| 256 } | 272 } |
| 257 | 273 |
| 258 SpeechRecognizerImpl::~SpeechRecognizerImpl() { | 274 SpeechRecognizerImpl::~SpeechRecognizerImpl() { |
| 259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 260 endpointer_.EndSession(); | 276 endpointer_.EndSession(); |
| 261 if (audio_controller_.get()) { | 277 if (audio_controller_.get()) { |
| 262 audio_controller_->Close( | 278 audio_controller_->Close( |
| 263 base::Bind(&KeepAudioControllerRefcountedForDtor, audio_controller_)); | 279 base::Bind(&KeepAudioControllerRefcountedForDtor, audio_controller_)); |
| 264 audio_log_->OnClosed(0); | 280 audio_log_->OnClosed(0); |
| 281 audio_receiver_.reset(); |
| 265 } | 282 } |
| 266 } | 283 } |
| 267 | 284 |
| 268 // Invoked in the audio thread. | 285 // Invoked in the audio thread. |
| 269 void SpeechRecognizerImpl::OnError(AudioInputController* controller, | 286 void SpeechRecognizerImpl::OnError(AudioInputController* controller, |
| 270 media::AudioInputController::ErrorCode error_code) { | 287 media::AudioInputController::ErrorCode error_code) { |
| 271 FSMEventArgs event_args(EVENT_AUDIO_ERROR); | 288 FSMEventArgs event_args(EVENT_AUDIO_ERROR); |
| 272 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 289 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 273 base::Bind(&SpeechRecognizerImpl::DispatchEvent, | 290 base::Bind(&SpeechRecognizerImpl::DispatchEvent, |
| 274 this, event_args)); | 291 this, event_args)); |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 state_ <= STATE_RECOGNIZING; | 492 state_ <= STATE_RECOGNIZING; |
| 476 const bool clip_detected = DetectClipping(raw_audio); | 493 const bool clip_detected = DetectClipping(raw_audio); |
| 477 float rms = 0.0f; | 494 float rms = 0.0f; |
| 478 | 495 |
| 479 num_samples_recorded_ += raw_audio.NumSamples(); | 496 num_samples_recorded_ += raw_audio.NumSamples(); |
| 480 | 497 |
| 481 if (route_to_endpointer) | 498 if (route_to_endpointer) |
| 482 endpointer_.ProcessAudio(raw_audio, &rms); | 499 endpointer_.ProcessAudio(raw_audio, &rms); |
| 483 | 500 |
| 484 if (route_to_vumeter) { | 501 if (route_to_vumeter) { |
| 485 DCHECK(route_to_endpointer); // Depends on endpointer due to |rms|. | 502 DCHECK(route_to_endpointer); // Depends on endpointer due to |rms|. |
| 486 UpdateSignalAndNoiseLevels(rms, clip_detected); | 503 UpdateSignalAndNoiseLevels(rms, clip_detected); |
| 487 } | 504 } |
| 488 if (route_to_sr_engine) { | 505 if (route_to_sr_engine) { |
| 489 DCHECK(recognition_engine_.get() != NULL); | 506 DCHECK(recognition_engine_.get() != NULL); |
| 490 recognition_engine_->TakeAudioChunk(raw_audio); | 507 recognition_engine_->TakeAudioChunk(raw_audio); |
| 491 } | 508 } |
| 492 } | 509 } |
| 493 | 510 |
| 494 SpeechRecognizerImpl::FSMState | 511 SpeechRecognizerImpl::FSMState |
| 495 SpeechRecognizerImpl::StartRecording(const FSMEventArgs&) { | 512 SpeechRecognizerImpl::StartRecording(const FSMEventArgs&) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 522 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO)); | 539 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO)); |
| 523 } | 540 } |
| 524 | 541 |
| 525 // Audio converter shall provide audio based on these parameters as output. | 542 // Audio converter shall provide audio based on these parameters as output. |
| 526 // Hard coded, WebSpeech specific parameters are utilized here. | 543 // Hard coded, WebSpeech specific parameters are utilized here. |
| 527 int frames_per_buffer = (kAudioSampleRate * chunk_duration_ms) / 1000; | 544 int frames_per_buffer = (kAudioSampleRate * chunk_duration_ms) / 1000; |
| 528 AudioParameters output_parameters = AudioParameters( | 545 AudioParameters output_parameters = AudioParameters( |
| 529 AudioParameters::AUDIO_PCM_LOW_LATENCY, kChannelLayout, kAudioSampleRate, | 546 AudioParameters::AUDIO_PCM_LOW_LATENCY, kChannelLayout, kAudioSampleRate, |
| 530 kNumBitsPerAudioSample, frames_per_buffer); | 547 kNumBitsPerAudioSample, frames_per_buffer); |
| 531 | 548 |
| 549 // Check if using audio receiver as source (audio data processed on renderer). |
| 550 // If |StartRecordingUsingAudioReceiver()| fails, we abort the recognition. |
| 551 if (using_audio_receiver_) { |
| 552 return StartRecordingUsingAudioReceiver(output_parameters) |
| 553 ? STATE_STARTING |
| 554 : Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO)); |
| 555 } |
| 556 |
| 557 // Get audio data from microphone on the browser. |
| 558 |
| 532 // Audio converter will receive audio based on these parameters as input. | 559 // Audio converter will receive audio based on these parameters as input. |
| 533 // On Windows we start by verifying that Core Audio is supported. If not, | 560 // On Windows we start by verifying that Core Audio is supported. If not, |
| 534 // the WaveIn API is used and we might as well avoid all audio conversations | 561 // the WaveIn API is used and we might as well avoid all audio conversations |
| 535 // since WaveIn does the conversion for us. | 562 // since WaveIn does the conversion for us. |
| 536 // TODO(henrika): this code should be moved to platform dependent audio | 563 // TODO(henrika): this code should be moved to platform dependent audio |
| 537 // managers. | 564 // managers. |
| 538 bool use_native_audio_params = true; | 565 bool use_native_audio_params = true; |
| 539 #if defined(OS_WIN) | 566 #if defined(OS_WIN) |
| 540 use_native_audio_params = media::CoreAudioUtil::IsSupported(); | 567 use_native_audio_params = media::CoreAudioUtil::IsSupported(); |
| 541 DVLOG_IF(1, !use_native_audio_params) << "Reverting to WaveIn for WebSpeech"; | 568 DVLOG_IF(1, !use_native_audio_params) << "Reverting to WaveIn for WebSpeech"; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 // The endpointer needs to estimate the environment/background noise before | 605 // The endpointer needs to estimate the environment/background noise before |
| 579 // starting to treat the audio as user input. We wait in the state | 606 // starting to treat the audio as user input. We wait in the state |
| 580 // ESTIMATING_ENVIRONMENT until such interval has elapsed before switching | 607 // ESTIMATING_ENVIRONMENT until such interval has elapsed before switching |
| 581 // to user input mode. | 608 // to user input mode. |
| 582 endpointer_.SetEnvironmentEstimationMode(); | 609 endpointer_.SetEnvironmentEstimationMode(); |
| 583 audio_controller_->Record(); | 610 audio_controller_->Record(); |
| 584 audio_log_->OnStarted(0); | 611 audio_log_->OnStarted(0); |
| 585 return STATE_STARTING; | 612 return STATE_STARTING; |
| 586 } | 613 } |
| 587 | 614 |
| 615 bool SpeechRecognizerImpl::StartRecordingUsingAudioReceiver( |
| 616 const media::AudioParameters& params) { |
| 617 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 618 |
| 619 // Start receiving audio, incoming data will be posted to |OnAudioTrackData|. |
| 620 base::SharedMemoryHandle foreign_memory_handle; |
| 621 base::SyncSocket::TransitDescriptor foreign_socket_descriptor; |
| 622 audio_receiver_.reset(SpeechRecognitionAudioReceiver::Create( |
| 623 params, |
| 624 peer_process_handle_, |
| 625 this, // Trigger OnData() and OnError() events here. |
| 626 NULL, // Use a new audio thread. |
| 627 &foreign_memory_handle, |
| 628 &foreign_socket_descriptor)); |
| 629 |
| 630 // Check if successfully created the receiver. |
| 631 if (!audio_receiver_.get()) |
| 632 return false; |
| 633 |
| 634 // Reusing the audio converter only to create chunks from the bus. |
| 635 // Same input and output format indicate no additional conversion here. |
| 636 audio_converter_.reset(new OnDataConverter(params, params)); |
| 637 |
| 638 // The endpointer needs to estimate the environment/background noise before |
| 639 // starting to treat the audio as user input. We wait in the state |
| 640 // ESTIMATING_ENVIRONMENT until such interval has elapsed before switching |
| 641 // to user input mode. |
| 642 endpointer_.SetEnvironmentEstimationMode(); |
| 643 |
| 644 // Start listening on the receiver's audio thread. |
| 645 audio_receiver_->Listen(); |
| 646 |
| 647 listener()->OnAudioReceiverReady(session_id(), |
| 648 params, |
| 649 foreign_memory_handle, |
| 650 foreign_socket_descriptor); |
| 651 |
| 652 return true; |
| 653 } |
| 654 |
| 588 SpeechRecognizerImpl::FSMState | 655 SpeechRecognizerImpl::FSMState |
| 589 SpeechRecognizerImpl::StartRecognitionEngine(const FSMEventArgs& event_args) { | 656 SpeechRecognizerImpl::StartRecognitionEngine(const FSMEventArgs& event_args) { |
| 590 // This is the first audio packet captured, so the recognition engine is | 657 // This is the first audio packet captured, so the recognition engine is |
| 591 // started and the delegate notified about the event. | 658 // started and the delegate notified about the event. |
| 592 DCHECK(recognition_engine_.get() != NULL); | 659 DCHECK(recognition_engine_.get() != NULL); |
| 593 recognition_engine_->StartRecognition(); | 660 recognition_engine_->StartRecognition(); |
| 594 listener()->OnAudioStart(session_id()); | 661 listener()->OnAudioStart(session_id()); |
| 595 | 662 |
| 596 // This is a little hack, since TakeAudioChunk() is already called by | 663 // This is a little hack, since TakeAudioChunk() is already called by |
| 597 // ProcessAudioPipeline(). It is the best tradeoff, unless we allow dropping | 664 // ProcessAudioPipeline(). It is the best tradeoff, unless we allow dropping |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 return Abort(event_args.engine_error); | 727 return Abort(event_args.engine_error); |
| 661 } | 728 } |
| 662 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_ABORTED)); | 729 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_ABORTED)); |
| 663 } | 730 } |
| 664 | 731 |
| 665 SpeechRecognizerImpl::FSMState SpeechRecognizerImpl::Abort( | 732 SpeechRecognizerImpl::FSMState SpeechRecognizerImpl::Abort( |
| 666 const SpeechRecognitionError& error) { | 733 const SpeechRecognitionError& error) { |
| 667 if (IsCapturingAudio()) | 734 if (IsCapturingAudio()) |
| 668 CloseAudioControllerAsynchronously(); | 735 CloseAudioControllerAsynchronously(); |
| 669 | 736 |
| 737 |
| 670 DVLOG(1) << "SpeechRecognizerImpl canceling recognition. "; | 738 DVLOG(1) << "SpeechRecognizerImpl canceling recognition. "; |
| 671 | 739 |
| 672 // The recognition engine is initialized only after STATE_STARTING. | 740 // The recognition engine is initialized only after STATE_STARTING. |
| 673 if (state_ > STATE_STARTING) { | 741 if (state_ > STATE_STARTING) { |
| 674 DCHECK(recognition_engine_.get() != NULL); | 742 DCHECK(recognition_engine_.get() != NULL); |
| 675 recognition_engine_->EndRecognition(); | 743 recognition_engine_->EndRecognition(); |
| 676 } | 744 } |
| 677 | 745 |
| 678 if (state_ > STATE_WAITING_FOR_SPEECH && state_ < STATE_WAITING_FINAL_RESULT) | 746 if (state_ > STATE_WAITING_FOR_SPEECH && state_ < STATE_WAITING_FINAL_RESULT) |
| 679 listener()->OnSoundEnd(session_id()); | 747 listener()->OnSoundEnd(session_id()); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 763 | 831 |
| 764 SpeechRecognizerImpl::FSMState | 832 SpeechRecognizerImpl::FSMState |
| 765 SpeechRecognizerImpl::NotFeasible(const FSMEventArgs& event_args) { | 833 SpeechRecognizerImpl::NotFeasible(const FSMEventArgs& event_args) { |
| 766 NOTREACHED() << "Unfeasible event " << event_args.event | 834 NOTREACHED() << "Unfeasible event " << event_args.event |
| 767 << " in state " << state_; | 835 << " in state " << state_; |
| 768 return state_; | 836 return state_; |
| 769 } | 837 } |
| 770 | 838 |
| 771 void SpeechRecognizerImpl::CloseAudioControllerAsynchronously() { | 839 void SpeechRecognizerImpl::CloseAudioControllerAsynchronously() { |
| 772 DCHECK(IsCapturingAudio()); | 840 DCHECK(IsCapturingAudio()); |
| 841 |
| 842 if (using_audio_receiver_) { |
| 843 audio_receiver_.reset(); |
| 844 return; |
| 845 } |
| 846 |
| 773 DVLOG(1) << "SpeechRecognizerImpl closing audio controller."; | 847 DVLOG(1) << "SpeechRecognizerImpl closing audio controller."; |
| 774 // Issues a Close on the audio controller, passing an empty callback. The only | 848 // Issues a Close on the audio controller, passing an empty callback. The only |
| 775 // purpose of such callback is to keep the audio controller refcounted until | 849 // purpose of such callback is to keep the audio controller refcounted until |
| 776 // Close has completed (in the audio thread) and automatically destroy it | 850 // Close has completed (in the audio thread) and automatically destroy it |
| 777 // afterwards (upon return from OnAudioClosed). | 851 // afterwards (upon return from OnAudioClosed). |
| 778 audio_controller_->Close(base::Bind(&SpeechRecognizerImpl::OnAudioClosed, | 852 audio_controller_->Close(base::Bind(&SpeechRecognizerImpl::OnAudioClosed, |
| 779 this, audio_controller_)); | 853 this, audio_controller_)); |
| 780 audio_controller_ = NULL; // The controller is still refcounted by Bind. | 854 audio_controller_ = NULL; // The controller is still refcounted by Bind. |
| 781 audio_log_->OnClosed(0); | 855 audio_log_->OnClosed(0); |
| 782 } | 856 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 815 SpeechRecognizerImpl::FSMEventArgs::FSMEventArgs(FSMEvent event_value) | 889 SpeechRecognizerImpl::FSMEventArgs::FSMEventArgs(FSMEvent event_value) |
| 816 : event(event_value), | 890 : event(event_value), |
| 817 audio_data(NULL), | 891 audio_data(NULL), |
| 818 engine_error(SPEECH_RECOGNITION_ERROR_NONE) { | 892 engine_error(SPEECH_RECOGNITION_ERROR_NONE) { |
| 819 } | 893 } |
| 820 | 894 |
| 821 SpeechRecognizerImpl::FSMEventArgs::~FSMEventArgs() { | 895 SpeechRecognizerImpl::FSMEventArgs::~FSMEventArgs() { |
| 822 } | 896 } |
| 823 | 897 |
| 824 } // namespace content | 898 } // namespace content |
| OLD | NEW |