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