| 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/speech_recognition_manager_impl.h" | 5 #include "content/browser/speech/speech_recognition_manager_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "content/browser/browser_main_loop.h" | 8 #include "content/browser/browser_main_loop.h" |
| 9 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 9 #include "content/browser/speech/google_one_shot_remote_engine.h" | 10 #include "content/browser/speech/google_one_shot_remote_engine.h" |
| 10 #include "content/browser/speech/google_streaming_remote_engine.h" | 11 #include "content/browser/speech/google_streaming_remote_engine.h" |
| 11 #include "content/browser/speech/speech_recognition_engine.h" | 12 #include "content/browser/speech/speech_recognition_engine.h" |
| 12 #include "content/browser/speech/speech_recognizer.h" | 13 #include "content/browser/speech/speech_recognizer.h" |
| 13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/content_browser_client.h" | 15 #include "content/public/browser/content_browser_client.h" |
| 15 #include "content/public/browser/resource_context.h" | 16 #include "content/public/browser/resource_context.h" |
| 16 #include "content/public/browser/speech_recognition_event_listener.h" | 17 #include "content/public/browser/speech_recognition_event_listener.h" |
| 17 #include "content/public/browser/speech_recognition_manager_delegate.h" | 18 #include "content/public/browser/speech_recognition_manager_delegate.h" |
| 18 #include "content/public/browser/speech_recognition_session_config.h" | 19 #include "content/public/browser/speech_recognition_session_config.h" |
| 19 #include "content/public/browser/speech_recognition_session_context.h" | 20 #include "content/public/browser/speech_recognition_session_context.h" |
| 21 #include "content/public/common/speech_recognition_error.h" |
| 20 #include "content/public/common/speech_recognition_result.h" | 22 #include "content/public/common/speech_recognition_result.h" |
| 21 #include "media/audio/audio_manager.h" | 23 #include "media/audio/audio_manager.h" |
| 22 | 24 |
| 23 using base::Callback; | 25 using base::Callback; |
| 24 using content::BrowserMainLoop; | 26 using content::BrowserMainLoop; |
| 25 using content::BrowserThread; | 27 using content::BrowserThread; |
| 26 using content::SpeechRecognitionError; | |
| 27 using content::SpeechRecognitionEventListener; | 28 using content::SpeechRecognitionEventListener; |
| 28 using content::SpeechRecognitionManager; | 29 using content::SpeechRecognitionManager; |
| 29 using content::SpeechRecognitionResult; | 30 using content::SpeechRecognitionResult; |
| 30 using content::SpeechRecognitionSessionContext; | 31 using content::SpeechRecognitionSessionContext; |
| 31 using content::SpeechRecognitionSessionConfig; | 32 using content::SpeechRecognitionSessionConfig; |
| 32 | 33 |
| 33 namespace content { | 34 namespace content { |
| 34 SpeechRecognitionManager* SpeechRecognitionManager::GetInstance() { | 35 SpeechRecognitionManager* SpeechRecognitionManager::GetInstance() { |
| 35 return speech::SpeechRecognitionManagerImpl::GetInstance(); | 36 return speech::SpeechRecognitionManagerImpl::GetInstance(); |
| 36 } | 37 } |
| 37 } // namespace content | 38 } // namespace content |
| 38 | 39 |
| 39 namespace { | 40 namespace { |
| 40 speech::SpeechRecognitionManagerImpl* g_speech_recognition_manager_impl; | 41 speech::SpeechRecognitionManagerImpl* g_speech_recognition_manager_impl; |
| 41 | 42 |
| 42 void ShowAudioInputSettingsOnFileThread() { | 43 void ShowAudioInputSettingsOnFileThread() { |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 44 media::AudioManager* audio_manager = BrowserMainLoop::GetAudioManager(); | 45 media::AudioManager* audio_manager = BrowserMainLoop::GetAudioManager(); |
| 45 DCHECK(audio_manager->CanShowAudioInputSettings()); | 46 DCHECK(audio_manager->CanShowAudioInputSettings()); |
| 46 if (audio_manager->CanShowAudioInputSettings()) | 47 if (audio_manager->CanShowAudioInputSettings()) |
| 47 audio_manager->ShowAudioInputSettings(); | 48 audio_manager->ShowAudioInputSettings(); |
| 48 } | 49 } |
| 49 } // namespace | 50 } // namespace |
| 50 | 51 |
| 51 namespace speech { | 52 namespace speech { |
| 52 | 53 |
| 54 class SpeechRecognitionManagerImpl::PermissionRequest |
| 55 : public media_stream::MediaStreamRequester { |
| 56 public: |
| 57 PermissionRequest(int session_id, |
| 58 const base::Callback<void(bool is_allowed)>& callback) |
| 59 : session_id_(session_id), |
| 60 callback_(callback), |
| 61 started_(false) { |
| 62 } |
| 63 |
| 64 virtual ~PermissionRequest() { |
| 65 if (started_) |
| 66 Abort(); |
| 67 } |
| 68 |
| 69 void Start(int render_process_id, int render_view_id, const GURL& origin) { |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 71 started_ = true; |
| 72 BrowserMainLoop::GetMediaStreamManager()->GenerateStream( |
| 73 this, |
| 74 render_process_id, |
| 75 render_view_id, |
| 76 media_stream::StreamOptions(/*audio=*/true, /*video=*/false), |
| 77 origin, |
| 78 &label_); |
| 79 } |
| 80 |
| 81 void Abort() { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 83 started_ = false; |
| 84 BrowserMainLoop::GetMediaStreamManager()->CancelGenerateStream(label_); |
| 85 } |
| 86 |
| 87 int Session() const { return session_id_; } |
| 88 |
| 89 // MediaStreamRequester methods. |
| 90 virtual void StreamGenerated( |
| 91 const std::string& label, |
| 92 const media_stream::StreamDeviceInfoArray& audio_devices, |
| 93 const media_stream::StreamDeviceInfoArray& video_devices) OVERRIDE { |
| 94 // TODO(hans): One day it would be nice to actually use the generated stream |
| 95 // but right now we only use it to request permission, and then we dump it. |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 97 started_ = false; |
| 98 BrowserThread::PostTask( |
| 99 BrowserThread::IO, |
| 100 FROM_HERE, |
| 101 base::Bind(&media_stream::MediaStreamManager::StopGeneratedStream, |
| 102 base::Unretained(BrowserMainLoop::GetMediaStreamManager()), |
| 103 label)); |
| 104 callback_.Run(true); |
| 105 } |
| 106 |
| 107 virtual void StreamGenerationFailed(const std::string& label) OVERRIDE { |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 109 started_ = false; |
| 110 callback_.Run(false); |
| 111 } |
| 112 |
| 113 // The callbacks below are ignored. |
| 114 virtual void AudioDeviceFailed(const std::string& label, |
| 115 int index) OVERRIDE {} |
| 116 virtual void VideoDeviceFailed(const std::string& label, |
| 117 int index) OVERRIDE {} |
| 118 virtual void DevicesEnumerated( |
| 119 const std::string& label, |
| 120 const media_stream::StreamDeviceInfoArray& devices) OVERRIDE {} |
| 121 virtual void DeviceOpened( |
| 122 const std::string& label, |
| 123 const media_stream::StreamDeviceInfo& device_info) OVERRIDE {} |
| 124 |
| 125 private: |
| 126 int session_id_; |
| 127 base::Callback<void(bool is_allowed)> callback_; |
| 128 std::string label_; |
| 129 bool started_; |
| 130 }; |
| 131 |
| 53 SpeechRecognitionManagerImpl* SpeechRecognitionManagerImpl::GetInstance() { | 132 SpeechRecognitionManagerImpl* SpeechRecognitionManagerImpl::GetInstance() { |
| 54 return g_speech_recognition_manager_impl; | 133 return g_speech_recognition_manager_impl; |
| 55 } | 134 } |
| 56 | 135 |
| 57 SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl() | 136 SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl() |
| 58 : session_id_capturing_audio_(kSessionIDInvalid), | 137 : primary_session_id_(kSessionIDInvalid), |
| 59 last_session_id_(kSessionIDInvalid), | 138 last_session_id_(kSessionIDInvalid), |
| 60 is_dispatching_event_(false), | 139 is_dispatching_event_(false), |
| 61 delegate_(content::GetContentClient()->browser()-> | 140 delegate_(content::GetContentClient()->browser()-> |
| 62 GetSpeechRecognitionManagerDelegate()), | 141 GetSpeechRecognitionManagerDelegate()), |
| 63 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 142 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 64 DCHECK(!g_speech_recognition_manager_impl); | 143 DCHECK(!g_speech_recognition_manager_impl); |
| 65 g_speech_recognition_manager_impl = this; | 144 g_speech_recognition_manager_impl = this; |
| 66 } | 145 } |
| 67 | 146 |
| 68 SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() { | 147 SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 google_remote_engine); | 196 google_remote_engine); |
| 118 return session_id; | 197 return session_id; |
| 119 } | 198 } |
| 120 | 199 |
| 121 void SpeechRecognitionManagerImpl::StartSession(int session_id) { | 200 void SpeechRecognitionManagerImpl::StartSession(int session_id) { |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 123 if (!SessionExists(session_id)) | 202 if (!SessionExists(session_id)) |
| 124 return; | 203 return; |
| 125 | 204 |
| 126 // If there is another active session, abort that. | 205 // If there is another active session, abort that. |
| 127 if (session_id_capturing_audio_ != kSessionIDInvalid && | 206 if (primary_session_id_ != kSessionIDInvalid && |
| 128 session_id_capturing_audio_ != session_id) { | 207 primary_session_id_ != session_id) { |
| 129 AbortSession(session_id_capturing_audio_); | 208 AbortSession(primary_session_id_); |
| 130 } | 209 } |
| 131 | 210 |
| 132 if (delegate_.get()) | 211 primary_session_id_ = session_id; |
| 212 |
| 213 if (delegate_.get()) { |
| 133 delegate_->CheckRecognitionIsAllowed( | 214 delegate_->CheckRecognitionIsAllowed( |
| 134 session_id, | 215 session_id, |
| 135 base::Bind(&SpeechRecognitionManagerImpl::RecognitionAllowedCallback, | 216 base::Bind(&SpeechRecognitionManagerImpl::RecognitionAllowedCallback, |
| 136 weak_factory_.GetWeakPtr())); | 217 weak_factory_.GetWeakPtr(), |
| 218 session_id)); |
| 219 } |
| 137 } | 220 } |
| 138 | 221 |
| 139 void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id, | 222 void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id, |
| 223 bool ask_user, |
| 140 bool is_allowed) { | 224 bool is_allowed) { |
| 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 142 DCHECK(SessionExists(session_id)); | 226 if (!SessionExists(session_id)) |
| 227 return; |
| 228 |
| 229 if (ask_user) { |
| 230 const SpeechRecognitionSessionContext& context = |
| 231 GetSessionContext(session_id); |
| 232 |
| 233 permission_request_.reset(new PermissionRequest( |
| 234 session_id, |
| 235 base::Bind(&SpeechRecognitionManagerImpl::RecognitionAllowedCallback, |
| 236 weak_factory_.GetWeakPtr(), |
| 237 session_id, |
| 238 false))); |
| 239 |
| 240 permission_request_->Start(context.render_process_id, |
| 241 context.render_view_id, |
| 242 GURL(context.context_name)); |
| 243 |
| 244 return; |
| 245 } |
| 246 |
| 247 permission_request_.reset(); |
| 248 |
| 143 if (is_allowed) { | 249 if (is_allowed) { |
| 144 MessageLoop::current()->PostTask(FROM_HERE, | 250 MessageLoop::current()->PostTask(FROM_HERE, |
| 145 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 251 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 146 weak_factory_.GetWeakPtr(), session_id, EVENT_START)); | 252 weak_factory_.GetWeakPtr(), session_id, EVENT_START)); |
| 147 } else { | 253 } else { |
| 148 sessions_.erase(session_id); | 254 OnRecognitionError(session_id, content::SpeechRecognitionError( |
| 255 content::SPEECH_RECOGNITION_ERROR_NOT_ALLOWED)); |
| 256 MessageLoop::current()->PostTask(FROM_HERE, |
| 257 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 258 weak_factory_.GetWeakPtr(), session_id, EVENT_ABORT)); |
| 149 } | 259 } |
| 150 } | 260 } |
| 151 | 261 |
| 152 void SpeechRecognitionManagerImpl::AbortSession(int session_id) { | 262 void SpeechRecognitionManagerImpl::AbortSession(int session_id) { |
| 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 154 if (!SessionExists(session_id)) | 264 if (!SessionExists(session_id)) |
| 155 return; | 265 return; |
| 156 | 266 |
| 267 if (permission_request_.get() && |
| 268 permission_request_->Session() == session_id) { |
| 269 DCHECK(permission_request_.get()); |
| 270 permission_request_->Abort(); |
| 271 } |
| 272 |
| 157 MessageLoop::current()->PostTask(FROM_HERE, | 273 MessageLoop::current()->PostTask(FROM_HERE, |
| 158 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 274 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 159 weak_factory_.GetWeakPtr(), session_id, EVENT_ABORT)); | 275 weak_factory_.GetWeakPtr(), session_id, EVENT_ABORT)); |
| 160 } | 276 } |
| 161 | 277 |
| 162 void SpeechRecognitionManagerImpl::StopAudioCaptureForSession(int session_id) { | 278 void SpeechRecognitionManagerImpl::StopAudioCaptureForSession(int session_id) { |
| 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 164 if (!SessionExists(session_id)) | 280 if (!SessionExists(session_id)) |
| 165 return; | 281 return; |
| 166 | 282 |
| 283 if (permission_request_.get() && |
| 284 permission_request_->Session() == session_id) { |
| 285 DCHECK(permission_request_.get()); |
| 286 permission_request_->Abort(); |
| 287 } |
| 288 |
| 167 MessageLoop::current()->PostTask(FROM_HERE, | 289 MessageLoop::current()->PostTask(FROM_HERE, |
| 168 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 290 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 169 weak_factory_.GetWeakPtr(), session_id, EVENT_STOP_CAPTURE)); | 291 weak_factory_.GetWeakPtr(), session_id, EVENT_STOP_CAPTURE)); |
| 170 } | 292 } |
| 171 | 293 |
| 172 // Here begins the SpeechRecognitionEventListener interface implementation, | 294 // Here begins the SpeechRecognitionEventListener interface implementation, |
| 173 // which will simply relay the events to the proper listener registered for the | 295 // which will simply relay the events to the proper listener registered for the |
| 174 // particular session (most likely InputTagSpeechDispatcherHost) and to the | 296 // particular session (most likely InputTagSpeechDispatcherHost) and to the |
| 175 // catch-all listener provided by the delegate (if any). | 297 // catch-all listener provided by the delegate (if any). |
| 176 | 298 |
| 177 void SpeechRecognitionManagerImpl::OnRecognitionStart(int session_id) { | 299 void SpeechRecognitionManagerImpl::OnRecognitionStart(int session_id) { |
| 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 300 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 179 if (!SessionExists(session_id)) | 301 if (!SessionExists(session_id)) |
| 180 return; | 302 return; |
| 181 | 303 |
| 182 DCHECK_EQ(session_id_capturing_audio_, session_id); | 304 DCHECK_EQ(primary_session_id_, session_id); |
| 183 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) | 305 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) |
| 184 delegate_listener->OnRecognitionStart(session_id); | 306 delegate_listener->OnRecognitionStart(session_id); |
| 185 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) | 307 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) |
| 186 listener->OnRecognitionStart(session_id); | 308 listener->OnRecognitionStart(session_id); |
| 187 } | 309 } |
| 188 | 310 |
| 189 void SpeechRecognitionManagerImpl::OnAudioStart(int session_id) { | 311 void SpeechRecognitionManagerImpl::OnAudioStart(int session_id) { |
| 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 191 if (!SessionExists(session_id)) | 313 if (!SessionExists(session_id)) |
| 192 return; | 314 return; |
| 193 | 315 |
| 194 DCHECK_EQ(session_id_capturing_audio_, session_id); | 316 DCHECK_EQ(primary_session_id_, session_id); |
| 195 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) | 317 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) |
| 196 delegate_listener->OnAudioStart(session_id); | 318 delegate_listener->OnAudioStart(session_id); |
| 197 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) | 319 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) |
| 198 listener->OnAudioStart(session_id); | 320 listener->OnAudioStart(session_id); |
| 199 } | 321 } |
| 200 | 322 |
| 201 void SpeechRecognitionManagerImpl::OnEnvironmentEstimationComplete( | 323 void SpeechRecognitionManagerImpl::OnEnvironmentEstimationComplete( |
| 202 int session_id) { | 324 int session_id) { |
| 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 204 if (!SessionExists(session_id)) | 326 if (!SessionExists(session_id)) |
| 205 return; | 327 return; |
| 206 | 328 |
| 207 DCHECK_EQ(session_id_capturing_audio_, session_id); | 329 DCHECK_EQ(primary_session_id_, session_id); |
| 208 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) | 330 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) |
| 209 delegate_listener->OnEnvironmentEstimationComplete(session_id); | 331 delegate_listener->OnEnvironmentEstimationComplete(session_id); |
| 210 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) | 332 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) |
| 211 listener->OnEnvironmentEstimationComplete(session_id); | 333 listener->OnEnvironmentEstimationComplete(session_id); |
| 212 } | 334 } |
| 213 | 335 |
| 214 void SpeechRecognitionManagerImpl::OnSoundStart(int session_id) { | 336 void SpeechRecognitionManagerImpl::OnSoundStart(int session_id) { |
| 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 216 if (!SessionExists(session_id)) | 338 if (!SessionExists(session_id)) |
| 217 return; | 339 return; |
| 218 | 340 |
| 219 DCHECK_EQ(session_id_capturing_audio_, session_id); | 341 DCHECK_EQ(primary_session_id_, session_id); |
| 220 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) | 342 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) |
| 221 delegate_listener->OnSoundStart(session_id); | 343 delegate_listener->OnSoundStart(session_id); |
| 222 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) | 344 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) |
| 223 listener->OnSoundStart(session_id); | 345 listener->OnSoundStart(session_id); |
| 224 } | 346 } |
| 225 | 347 |
| 226 void SpeechRecognitionManagerImpl::OnSoundEnd(int session_id) { | 348 void SpeechRecognitionManagerImpl::OnSoundEnd(int session_id) { |
| 227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 228 if (!SessionExists(session_id)) | 350 if (!SessionExists(session_id)) |
| 229 return; | 351 return; |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 // completed the transition from CAPTURING_AUDIO to WAITING_FOR_RESULT, thus | 511 // completed the transition from CAPTURING_AUDIO to WAITING_FOR_RESULT, thus |
| 390 // we perceive the AUDIO_ENDED event in WAITING_FOR_RESULT). | 512 // we perceive the AUDIO_ENDED event in WAITING_FOR_RESULT). |
| 391 // This makes the code below a bit tricky but avoids a lot of code for | 513 // This makes the code below a bit tricky but avoids a lot of code for |
| 392 // tracking and reconstructing asynchronously the state of the recognizer. | 514 // tracking and reconstructing asynchronously the state of the recognizer. |
| 393 switch (session_state) { | 515 switch (session_state) { |
| 394 case SESSION_STATE_IDLE: | 516 case SESSION_STATE_IDLE: |
| 395 switch (event) { | 517 switch (event) { |
| 396 case EVENT_START: | 518 case EVENT_START: |
| 397 return SessionStart(session); | 519 return SessionStart(session); |
| 398 case EVENT_ABORT: | 520 case EVENT_ABORT: |
| 521 return SessionAbort(session); |
| 399 case EVENT_RECOGNITION_ENDED: | 522 case EVENT_RECOGNITION_ENDED: |
| 400 return SessionDelete(session); | 523 return SessionDelete(session); |
| 401 case EVENT_STOP_CAPTURE: | 524 case EVENT_STOP_CAPTURE: |
| 525 return SessionStopAudioCapture(session); |
| 402 case EVENT_AUDIO_ENDED: | 526 case EVENT_AUDIO_ENDED: |
| 403 return; | 527 return; |
| 404 } | 528 } |
| 405 break; | 529 break; |
| 406 case SESSION_STATE_CAPTURING_AUDIO: | 530 case SESSION_STATE_CAPTURING_AUDIO: |
| 407 switch (event) { | 531 switch (event) { |
| 408 case EVENT_STOP_CAPTURE: | 532 case EVENT_STOP_CAPTURE: |
| 409 return SessionStopAudioCapture(session); | 533 return SessionStopAudioCapture(session); |
| 410 case EVENT_ABORT: | 534 case EVENT_ABORT: |
| 411 return SessionAbort(session); | 535 return SessionAbort(session); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 441 if (session.recognizer->IsCapturingAudio()) | 565 if (session.recognizer->IsCapturingAudio()) |
| 442 return SESSION_STATE_CAPTURING_AUDIO; | 566 return SESSION_STATE_CAPTURING_AUDIO; |
| 443 return SESSION_STATE_WAITING_FOR_RESULT; | 567 return SESSION_STATE_WAITING_FOR_RESULT; |
| 444 } | 568 } |
| 445 | 569 |
| 446 // ----------- Contract for all the FSM evolution functions below ------------- | 570 // ----------- Contract for all the FSM evolution functions below ------------- |
| 447 // - Are guaranteed to be executed in the IO thread; | 571 // - Are guaranteed to be executed in the IO thread; |
| 448 // - Are guaranteed to be not reentrant (themselves and each other); | 572 // - Are guaranteed to be not reentrant (themselves and each other); |
| 449 | 573 |
| 450 void SpeechRecognitionManagerImpl::SessionStart(const Session& session) { | 574 void SpeechRecognitionManagerImpl::SessionStart(const Session& session) { |
| 451 session_id_capturing_audio_ = session.id; | 575 DCHECK_EQ(primary_session_id_, session.id); |
| 452 session.recognizer->StartRecognition(); | 576 session.recognizer->StartRecognition(); |
| 453 } | 577 } |
| 454 | 578 |
| 455 void SpeechRecognitionManagerImpl::SessionAbort(const Session& session) { | 579 void SpeechRecognitionManagerImpl::SessionAbort(const Session& session) { |
| 456 if (session_id_capturing_audio_ == session.id) | 580 if (primary_session_id_ == session.id) |
| 457 session_id_capturing_audio_ = kSessionIDInvalid; | 581 primary_session_id_ = kSessionIDInvalid; |
| 458 DCHECK(session.recognizer.get() && session.recognizer->IsActive()); | 582 DCHECK(session.recognizer.get()); |
| 459 session.recognizer->AbortRecognition(); | 583 session.recognizer->AbortRecognition(); |
| 460 } | 584 } |
| 461 | 585 |
| 462 void SpeechRecognitionManagerImpl::SessionStopAudioCapture( | 586 void SpeechRecognitionManagerImpl::SessionStopAudioCapture( |
| 463 const Session& session) { | 587 const Session& session) { |
| 464 DCHECK(session.recognizer.get() && session.recognizer->IsCapturingAudio()); | 588 DCHECK(session.recognizer.get()); |
| 465 session.recognizer->StopAudioCapture(); | 589 session.recognizer->StopAudioCapture(); |
| 466 } | 590 } |
| 467 | 591 |
| 468 void SpeechRecognitionManagerImpl::ResetCapturingSessionId( | 592 void SpeechRecognitionManagerImpl::ResetCapturingSessionId( |
| 469 const Session& session) { | 593 const Session& session) { |
| 470 DCHECK_EQ(session_id_capturing_audio_, session.id); | 594 DCHECK_EQ(primary_session_id_, session.id); |
| 471 session_id_capturing_audio_ = kSessionIDInvalid; | 595 primary_session_id_ = kSessionIDInvalid; |
| 472 } | 596 } |
| 473 | 597 |
| 474 void SpeechRecognitionManagerImpl::SessionDelete(const Session& session) { | 598 void SpeechRecognitionManagerImpl::SessionDelete(const Session& session) { |
| 475 DCHECK(session.recognizer == NULL || !session.recognizer->IsActive()); | 599 DCHECK(session.recognizer == NULL || !session.recognizer->IsActive()); |
| 476 if (session_id_capturing_audio_ == session.id) | 600 if (primary_session_id_ == session.id) |
| 477 session_id_capturing_audio_ = kSessionIDInvalid; | 601 primary_session_id_ = kSessionIDInvalid; |
| 478 sessions_.erase(session.id); | 602 sessions_.erase(session.id); |
| 479 } | 603 } |
| 480 | 604 |
| 481 void SpeechRecognitionManagerImpl::NotFeasible(const Session& session, | 605 void SpeechRecognitionManagerImpl::NotFeasible(const Session& session, |
| 482 FSMEvent event) { | 606 FSMEvent event) { |
| 483 NOTREACHED() << "Unfeasible event " << event | 607 NOTREACHED() << "Unfeasible event " << event |
| 484 << " in state " << GetSessionState(session.id) | 608 << " in state " << GetSessionState(session.id) |
| 485 << " for session " << session.id; | 609 << " for session " << session.id; |
| 486 } | 610 } |
| 487 | 611 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 | 666 |
| 543 SpeechRecognitionManagerImpl::Session::Session() | 667 SpeechRecognitionManagerImpl::Session::Session() |
| 544 : id(kSessionIDInvalid), | 668 : id(kSessionIDInvalid), |
| 545 listener_is_active(true) { | 669 listener_is_active(true) { |
| 546 } | 670 } |
| 547 | 671 |
| 548 SpeechRecognitionManagerImpl::Session::~Session() { | 672 SpeechRecognitionManagerImpl::Session::~Session() { |
| 549 } | 673 } |
| 550 | 674 |
| 551 } // namespace speech | 675 } // namespace speech |
| OLD | NEW |