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