Clang roll script: fix error when GYP_DEFINES isn't defined
[chromium-blink-merge.git] / content / browser / speech / speech_recognition_manager_impl.cc
blobec137976cdef3c18d9b943991094f2f92c1ce139
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
3 // found in the LICENSE file.
5 #include "content/browser/speech/speech_recognition_manager_impl.h"
7 #include "base/bind.h"
8 #include "content/browser/browser_main_loop.h"
9 #include "content/browser/renderer_host/media/media_stream_manager.h"
10 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h"
11 #include "content/browser/speech/google_one_shot_remote_engine.h"
12 #include "content/browser/speech/google_streaming_remote_engine.h"
13 #include "content/browser/speech/speech_recognition_engine.h"
14 #include "content/browser/speech/speech_recognizer_impl.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/content_browser_client.h"
17 #include "content/public/browser/resource_context.h"
18 #include "content/public/browser/speech_recognition_event_listener.h"
19 #include "content/public/browser/speech_recognition_manager_delegate.h"
20 #include "content/public/browser/speech_recognition_session_config.h"
21 #include "content/public/browser/speech_recognition_session_context.h"
22 #include "content/public/common/speech_recognition_error.h"
23 #include "content/public/common/speech_recognition_result.h"
24 #include "media/audio/audio_manager.h"
25 #include "media/audio/audio_manager_base.h"
27 #if defined(OS_ANDROID)
28 #include "content/browser/speech/speech_recognizer_impl_android.h"
29 #endif
31 using base::Callback;
33 namespace content {
35 SpeechRecognitionManager* SpeechRecognitionManager::manager_for_tests_;
37 namespace {
39 SpeechRecognitionManagerImpl* g_speech_recognition_manager_impl;
41 void ShowAudioInputSettingsOnFileThread(media::AudioManager* audio_manager) {
42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
43 audio_manager->ShowAudioInputSettings();
46 } // namespace
48 SpeechRecognitionManager* SpeechRecognitionManager::GetInstance() {
49 if (manager_for_tests_)
50 return manager_for_tests_;
51 return SpeechRecognitionManagerImpl::GetInstance();
54 void SpeechRecognitionManager::SetManagerForTesting(
55 SpeechRecognitionManager* manager) {
56 manager_for_tests_ = manager;
59 SpeechRecognitionManagerImpl* SpeechRecognitionManagerImpl::GetInstance() {
60 return g_speech_recognition_manager_impl;
63 SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl(
64 media::AudioManager* audio_manager,
65 MediaStreamManager* media_stream_manager)
66 : audio_manager_(audio_manager),
67 media_stream_manager_(media_stream_manager),
68 primary_session_id_(kSessionIDInvalid),
69 last_session_id_(kSessionIDInvalid),
70 is_dispatching_event_(false),
71 delegate_(GetContentClient()->browser()->
72 GetSpeechRecognitionManagerDelegate()),
73 weak_factory_(this) {
74 DCHECK(!g_speech_recognition_manager_impl);
75 g_speech_recognition_manager_impl = this;
78 SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() {
79 DCHECK(g_speech_recognition_manager_impl);
80 g_speech_recognition_manager_impl = NULL;
82 for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end();
83 ++it) {
84 // MediaStreamUIProxy must be deleted on the IO thread.
85 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
86 it->second->ui.release());
87 delete it->second;
89 sessions_.clear();
92 int SpeechRecognitionManagerImpl::CreateSession(
93 const SpeechRecognitionSessionConfig& config) {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
96 const int session_id = GetNextSessionID();
97 DCHECK(!SessionExists(session_id));
98 // Set-up the new session.
99 Session* session = new Session();
100 sessions_[session_id] = session;
101 session->id = session_id;
102 session->config = config;
103 session->context = config.initial_context;
105 std::string hardware_info;
106 bool can_report_metrics = false;
107 if (delegate_)
108 delegate_->GetDiagnosticInformation(&can_report_metrics, &hardware_info);
110 // The legacy api cannot use continuous mode.
111 DCHECK(!config.is_legacy_api || !config.continuous);
113 #if !defined(OS_ANDROID)
114 // A SpeechRecognitionEngine (and corresponding Config) is required only
115 // when using SpeechRecognizerImpl, which performs the audio capture and
116 // endpointing in the browser. This is not the case of Android where, not
117 // only the speech recognition, but also the audio capture and endpointing
118 // activities performed outside of the browser (delegated via JNI to the
119 // Android API implementation).
121 SpeechRecognitionEngineConfig remote_engine_config;
122 remote_engine_config.language = config.language;
123 remote_engine_config.grammars = config.grammars;
124 remote_engine_config.audio_sample_rate =
125 SpeechRecognizerImpl::kAudioSampleRate;
126 remote_engine_config.audio_num_bits_per_sample =
127 SpeechRecognizerImpl::kNumBitsPerAudioSample;
128 remote_engine_config.filter_profanities = config.filter_profanities;
129 remote_engine_config.continuous = config.continuous;
130 remote_engine_config.interim_results = config.interim_results;
131 remote_engine_config.max_hypotheses = config.max_hypotheses;
132 remote_engine_config.hardware_info = hardware_info;
133 remote_engine_config.origin_url =
134 can_report_metrics ? config.origin_url : std::string();
136 SpeechRecognitionEngine* google_remote_engine;
137 if (config.is_legacy_api) {
138 google_remote_engine =
139 new GoogleOneShotRemoteEngine(config.url_request_context_getter.get());
140 } else {
141 google_remote_engine = new GoogleStreamingRemoteEngine(
142 config.url_request_context_getter.get());
145 google_remote_engine->SetConfig(remote_engine_config);
147 session->recognizer = new SpeechRecognizerImpl(
148 this,
149 session_id,
150 config.continuous,
151 config.interim_results,
152 google_remote_engine);
153 #else
154 session->recognizer = new SpeechRecognizerImplAndroid(this, session_id);
155 #endif
156 return session_id;
159 void SpeechRecognitionManagerImpl::StartSession(int session_id) {
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
161 if (!SessionExists(session_id))
162 return;
164 // If there is another active session, abort that.
165 if (primary_session_id_ != kSessionIDInvalid &&
166 primary_session_id_ != session_id) {
167 AbortSession(primary_session_id_);
170 primary_session_id_ = session_id;
172 if (delegate_) {
173 delegate_->CheckRecognitionIsAllowed(
174 session_id,
175 base::Bind(&SpeechRecognitionManagerImpl::RecognitionAllowedCallback,
176 weak_factory_.GetWeakPtr(),
177 session_id));
181 void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id,
182 bool ask_user,
183 bool is_allowed) {
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
185 if (!SessionExists(session_id))
186 return;
188 SessionsTable::iterator iter = sessions_.find(session_id);
189 DCHECK(iter != sessions_.end());
190 Session* session = iter->second;
192 if (session->abort_requested)
193 return;
195 if (ask_user) {
196 SpeechRecognitionSessionContext& context = session->context;
197 context.label = media_stream_manager_->MakeMediaAccessRequest(
198 context.render_process_id,
199 context.render_view_id,
200 context.request_id,
201 StreamOptions(true, false),
202 GURL(context.context_name),
203 base::Bind(
204 &SpeechRecognitionManagerImpl::MediaRequestPermissionCallback,
205 weak_factory_.GetWeakPtr(), session_id));
206 return;
209 if (is_allowed) {
210 base::MessageLoop::current()->PostTask(
211 FROM_HERE,
212 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
213 weak_factory_.GetWeakPtr(),
214 session_id,
215 EVENT_START));
216 } else {
217 OnRecognitionError(session_id, SpeechRecognitionError(
218 SPEECH_RECOGNITION_ERROR_NOT_ALLOWED));
219 base::MessageLoop::current()->PostTask(
220 FROM_HERE,
221 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
222 weak_factory_.GetWeakPtr(),
223 session_id,
224 EVENT_ABORT));
228 void SpeechRecognitionManagerImpl::MediaRequestPermissionCallback(
229 int session_id,
230 const MediaStreamDevices& devices,
231 scoped_ptr<MediaStreamUIProxy> stream_ui) {
232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
234 SessionsTable::iterator iter = sessions_.find(session_id);
235 if (iter == sessions_.end())
236 return;
238 bool is_allowed = !devices.empty();
239 if (is_allowed) {
240 // Copy the approved devices array to the context for UI indication.
241 iter->second->context.devices = devices;
243 // Save the UI object.
244 iter->second->ui = stream_ui.Pass();
247 // Clear the label to indicate the request has been done.
248 iter->second->context.label.clear();
250 // Notify the recognition about the request result.
251 RecognitionAllowedCallback(iter->first, false, is_allowed);
254 void SpeechRecognitionManagerImpl::AbortSession(int session_id) {
255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
256 if (!SessionExists(session_id))
257 return;
259 SessionsTable::iterator iter = sessions_.find(session_id);
260 iter->second->ui.reset();
262 if (iter->second->abort_requested)
263 return;
265 iter->second->abort_requested = true;
267 base::MessageLoop::current()->PostTask(
268 FROM_HERE,
269 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
270 weak_factory_.GetWeakPtr(),
271 session_id,
272 EVENT_ABORT));
275 void SpeechRecognitionManagerImpl::StopAudioCaptureForSession(int session_id) {
276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
277 if (!SessionExists(session_id))
278 return;
280 SessionsTable::iterator iter = sessions_.find(session_id);
281 iter->second->ui.reset();
283 base::MessageLoop::current()->PostTask(
284 FROM_HERE,
285 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
286 weak_factory_.GetWeakPtr(),
287 session_id,
288 EVENT_STOP_CAPTURE));
291 // Here begins the SpeechRecognitionEventListener interface implementation,
292 // which will simply relay the events to the proper listener registered for the
293 // particular session (most likely InputTagSpeechDispatcherHost) and to the
294 // catch-all listener provided by the delegate (if any).
296 void SpeechRecognitionManagerImpl::OnRecognitionStart(int session_id) {
297 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
298 if (!SessionExists(session_id))
299 return;
301 SessionsTable::iterator iter = sessions_.find(session_id);
302 if (iter->second->ui) {
303 // Notify the UI that the devices are being used.
304 iter->second->ui->OnStarted(base::Closure());
307 DCHECK_EQ(primary_session_id_, session_id);
308 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
309 delegate_listener->OnRecognitionStart(session_id);
310 if (SpeechRecognitionEventListener* listener = GetListener(session_id))
311 listener->OnRecognitionStart(session_id);
314 void SpeechRecognitionManagerImpl::OnAudioStart(int session_id) {
315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
316 if (!SessionExists(session_id))
317 return;
319 DCHECK_EQ(primary_session_id_, session_id);
320 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
321 delegate_listener->OnAudioStart(session_id);
322 if (SpeechRecognitionEventListener* listener = GetListener(session_id))
323 listener->OnAudioStart(session_id);
326 void SpeechRecognitionManagerImpl::OnEnvironmentEstimationComplete(
327 int session_id) {
328 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
329 if (!SessionExists(session_id))
330 return;
332 DCHECK_EQ(primary_session_id_, session_id);
333 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
334 delegate_listener->OnEnvironmentEstimationComplete(session_id);
335 if (SpeechRecognitionEventListener* listener = GetListener(session_id))
336 listener->OnEnvironmentEstimationComplete(session_id);
339 void SpeechRecognitionManagerImpl::OnSoundStart(int session_id) {
340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
341 if (!SessionExists(session_id))
342 return;
344 DCHECK_EQ(primary_session_id_, session_id);
345 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
346 delegate_listener->OnSoundStart(session_id);
347 if (SpeechRecognitionEventListener* listener = GetListener(session_id))
348 listener->OnSoundStart(session_id);
351 void SpeechRecognitionManagerImpl::OnSoundEnd(int session_id) {
352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
353 if (!SessionExists(session_id))
354 return;
356 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
357 delegate_listener->OnSoundEnd(session_id);
358 if (SpeechRecognitionEventListener* listener = GetListener(session_id))
359 listener->OnSoundEnd(session_id);
362 void SpeechRecognitionManagerImpl::OnAudioEnd(int session_id) {
363 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
364 if (!SessionExists(session_id))
365 return;
367 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
368 delegate_listener->OnAudioEnd(session_id);
369 if (SpeechRecognitionEventListener* listener = GetListener(session_id))
370 listener->OnAudioEnd(session_id);
371 base::MessageLoop::current()->PostTask(
372 FROM_HERE,
373 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
374 weak_factory_.GetWeakPtr(),
375 session_id,
376 EVENT_AUDIO_ENDED));
379 void SpeechRecognitionManagerImpl::OnRecognitionResults(
380 int session_id, const SpeechRecognitionResults& results) {
381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
382 if (!SessionExists(session_id))
383 return;
385 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
386 delegate_listener->OnRecognitionResults(session_id, results);
387 if (SpeechRecognitionEventListener* listener = GetListener(session_id))
388 listener->OnRecognitionResults(session_id, results);
391 void SpeechRecognitionManagerImpl::OnRecognitionError(
392 int session_id, const SpeechRecognitionError& error) {
393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
394 if (!SessionExists(session_id))
395 return;
397 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
398 delegate_listener->OnRecognitionError(session_id, error);
399 if (SpeechRecognitionEventListener* listener = GetListener(session_id))
400 listener->OnRecognitionError(session_id, error);
403 void SpeechRecognitionManagerImpl::OnAudioLevelsChange(
404 int session_id, float volume, float noise_volume) {
405 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
406 if (!SessionExists(session_id))
407 return;
409 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
410 delegate_listener->OnAudioLevelsChange(session_id, volume, noise_volume);
411 if (SpeechRecognitionEventListener* listener = GetListener(session_id))
412 listener->OnAudioLevelsChange(session_id, volume, noise_volume);
415 void SpeechRecognitionManagerImpl::OnRecognitionEnd(int session_id) {
416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
417 if (!SessionExists(session_id))
418 return;
420 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
421 delegate_listener->OnRecognitionEnd(session_id);
422 if (SpeechRecognitionEventListener* listener = GetListener(session_id))
423 listener->OnRecognitionEnd(session_id);
424 base::MessageLoop::current()->PostTask(
425 FROM_HERE,
426 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
427 weak_factory_.GetWeakPtr(),
428 session_id,
429 EVENT_RECOGNITION_ENDED));
432 int SpeechRecognitionManagerImpl::GetSession(
433 int render_process_id, int render_view_id, int request_id) const {
434 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
435 SessionsTable::const_iterator iter;
436 for(iter = sessions_.begin(); iter != sessions_.end(); ++iter) {
437 const int session_id = iter->first;
438 const SpeechRecognitionSessionContext& context = iter->second->context;
439 if (context.render_process_id == render_process_id &&
440 context.render_view_id == render_view_id &&
441 context.request_id == request_id) {
442 return session_id;
445 return kSessionIDInvalid;
448 SpeechRecognitionSessionContext
449 SpeechRecognitionManagerImpl::GetSessionContext(int session_id) const {
450 return GetSession(session_id)->context;
453 void SpeechRecognitionManagerImpl::AbortAllSessionsForListener(
454 SpeechRecognitionEventListener* listener) {
455 // This method gracefully destroys sessions for the listener. However, since
456 // the listener itself is likely to be destroyed after this call, we avoid
457 // dispatching further events to it, marking the |listener_is_active| flag.
458 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
459 for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end();
460 ++it) {
461 Session* session = it->second;
462 if (session->config.event_listener == listener) {
463 AbortSession(session->id);
464 session->listener_is_active = false;
469 void SpeechRecognitionManagerImpl::AbortAllSessionsForRenderView(
470 int render_process_id,
471 int render_view_id) {
472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
473 for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end();
474 ++it) {
475 Session* session = it->second;
476 if (session->context.render_process_id == render_process_id &&
477 session->context.render_view_id == render_view_id) {
478 AbortSession(session->id);
483 // ----------------------- Core FSM implementation ---------------------------
484 void SpeechRecognitionManagerImpl::DispatchEvent(int session_id,
485 FSMEvent event) {
486 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
488 // There are some corner cases in which the session might be deleted (due to
489 // an EndRecognition event) between a request (e.g. Abort) and its dispatch.
490 if (!SessionExists(session_id))
491 return;
493 Session* session = GetSession(session_id);
494 FSMState session_state = GetSessionState(session_id);
495 DCHECK_LE(session_state, SESSION_STATE_MAX_VALUE);
496 DCHECK_LE(event, EVENT_MAX_VALUE);
498 // Event dispatching must be sequential, otherwise it will break all the rules
499 // and the assumptions of the finite state automata model.
500 DCHECK(!is_dispatching_event_);
501 is_dispatching_event_ = true;
502 ExecuteTransitionAndGetNextState(session, session_state, event);
503 is_dispatching_event_ = false;
506 // This FSM handles the evolution of each session, from the viewpoint of the
507 // interaction with the user (that may be either the browser end-user which
508 // interacts with UI bubbles, or JS developer intracting with JS methods).
509 // All the events received by the SpeechRecognizer instances (one for each
510 // session) are always routed to the SpeechRecognitionEventListener(s)
511 // regardless the choices taken in this FSM.
512 void SpeechRecognitionManagerImpl::ExecuteTransitionAndGetNextState(
513 Session* session, FSMState session_state, FSMEvent event) {
514 // Note: since we're not tracking the state of the recognizer object, rather
515 // we're directly retrieving it (through GetSessionState), we see its events
516 // (that are AUDIO_ENDED and RECOGNITION_ENDED) after its state evolution
517 // (e.g., when we receive the AUDIO_ENDED event, the recognizer has just
518 // completed the transition from CAPTURING_AUDIO to WAITING_FOR_RESULT, thus
519 // we perceive the AUDIO_ENDED event in WAITING_FOR_RESULT).
520 // This makes the code below a bit tricky but avoids a lot of code for
521 // tracking and reconstructing asynchronously the state of the recognizer.
522 switch (session_state) {
523 case SESSION_STATE_IDLE:
524 switch (event) {
525 case EVENT_START:
526 return SessionStart(*session);
527 case EVENT_ABORT:
528 return SessionAbort(*session);
529 case EVENT_RECOGNITION_ENDED:
530 return SessionDelete(session);
531 case EVENT_STOP_CAPTURE:
532 return SessionStopAudioCapture(*session);
533 case EVENT_AUDIO_ENDED:
534 return;
536 break;
537 case SESSION_STATE_CAPTURING_AUDIO:
538 switch (event) {
539 case EVENT_STOP_CAPTURE:
540 return SessionStopAudioCapture(*session);
541 case EVENT_ABORT:
542 return SessionAbort(*session);
543 case EVENT_START:
544 return;
545 case EVENT_AUDIO_ENDED:
546 case EVENT_RECOGNITION_ENDED:
547 return NotFeasible(*session, event);
549 break;
550 case SESSION_STATE_WAITING_FOR_RESULT:
551 switch (event) {
552 case EVENT_ABORT:
553 return SessionAbort(*session);
554 case EVENT_AUDIO_ENDED:
555 return ResetCapturingSessionId(*session);
556 case EVENT_START:
557 case EVENT_STOP_CAPTURE:
558 return;
559 case EVENT_RECOGNITION_ENDED:
560 return NotFeasible(*session, event);
562 break;
564 return NotFeasible(*session, event);
567 SpeechRecognitionManagerImpl::FSMState
568 SpeechRecognitionManagerImpl::GetSessionState(int session_id) const {
569 Session* session = GetSession(session_id);
570 if (!session->recognizer.get() || !session->recognizer->IsActive())
571 return SESSION_STATE_IDLE;
572 if (session->recognizer->IsCapturingAudio())
573 return SESSION_STATE_CAPTURING_AUDIO;
574 return SESSION_STATE_WAITING_FOR_RESULT;
577 // ----------- Contract for all the FSM evolution functions below -------------
578 // - Are guaranteed to be executed in the IO thread;
579 // - Are guaranteed to be not reentrant (themselves and each other);
581 void SpeechRecognitionManagerImpl::SessionStart(const Session& session) {
582 DCHECK_EQ(primary_session_id_, session.id);
583 const MediaStreamDevices& devices = session.context.devices;
584 std::string device_id;
585 if (devices.empty()) {
586 // From the ask_user=false path, use the default device.
587 // TODO(xians): Abort the session after we do not need to support this path
588 // anymore.
589 device_id = media::AudioManagerBase::kDefaultDeviceId;
590 } else {
591 // From the ask_user=true path, use the selected device.
592 DCHECK_EQ(1u, devices.size());
593 DCHECK_EQ(MEDIA_DEVICE_AUDIO_CAPTURE, devices.front().type);
594 device_id = devices.front().id;
597 session.recognizer->StartRecognition(device_id);
600 void SpeechRecognitionManagerImpl::SessionAbort(const Session& session) {
601 if (primary_session_id_ == session.id)
602 primary_session_id_ = kSessionIDInvalid;
603 DCHECK(session.recognizer.get());
604 session.recognizer->AbortRecognition();
607 void SpeechRecognitionManagerImpl::SessionStopAudioCapture(
608 const Session& session) {
609 DCHECK(session.recognizer.get());
610 session.recognizer->StopAudioCapture();
613 void SpeechRecognitionManagerImpl::ResetCapturingSessionId(
614 const Session& session) {
615 DCHECK_EQ(primary_session_id_, session.id);
616 primary_session_id_ = kSessionIDInvalid;
619 void SpeechRecognitionManagerImpl::SessionDelete(Session* session) {
620 DCHECK(session->recognizer.get() == NULL || !session->recognizer->IsActive());
621 if (primary_session_id_ == session->id)
622 primary_session_id_ = kSessionIDInvalid;
623 if (!session->context.label.empty())
624 media_stream_manager_->CancelRequest(session->context.label);
625 sessions_.erase(session->id);
626 delete session;
629 void SpeechRecognitionManagerImpl::NotFeasible(const Session& session,
630 FSMEvent event) {
631 NOTREACHED() << "Unfeasible event " << event
632 << " in state " << GetSessionState(session.id)
633 << " for session " << session.id;
636 int SpeechRecognitionManagerImpl::GetNextSessionID() {
637 ++last_session_id_;
638 // Deal with wrapping of last_session_id_. (How civilized).
639 if (last_session_id_ <= 0)
640 last_session_id_ = 1;
641 return last_session_id_;
644 bool SpeechRecognitionManagerImpl::SessionExists(int session_id) const {
645 return sessions_.find(session_id) != sessions_.end();
648 SpeechRecognitionManagerImpl::Session*
649 SpeechRecognitionManagerImpl::GetSession(int session_id) const {
650 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
651 SessionsTable::const_iterator iter = sessions_.find(session_id);
652 DCHECK(iter != sessions_.end());
653 return iter->second;
656 SpeechRecognitionEventListener* SpeechRecognitionManagerImpl::GetListener(
657 int session_id) const {
658 Session* session = GetSession(session_id);
659 return session->listener_is_active ? session->config.event_listener : NULL;
662 SpeechRecognitionEventListener*
663 SpeechRecognitionManagerImpl::GetDelegateListener() const {
664 return delegate_.get() ? delegate_->GetEventListener() : NULL;
667 const SpeechRecognitionSessionConfig&
668 SpeechRecognitionManagerImpl::GetSessionConfig(int session_id) const {
669 return GetSession(session_id)->config;
672 bool SpeechRecognitionManagerImpl::HasAudioInputDevices() {
673 return audio_manager_->HasAudioInputDevices();
676 base::string16 SpeechRecognitionManagerImpl::GetAudioInputDeviceModel() {
677 return audio_manager_->GetAudioInputDeviceModel();
680 void SpeechRecognitionManagerImpl::ShowAudioInputSettings() {
681 // Since AudioManager::ShowAudioInputSettings can potentially launch external
682 // processes, do that in the FILE thread to not block the calling threads.
683 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
684 base::Bind(&ShowAudioInputSettingsOnFileThread,
685 audio_manager_));
688 SpeechRecognitionManagerImpl::Session::Session()
689 : id(kSessionIDInvalid),
690 abort_requested(false),
691 listener_is_active(true) {
694 SpeechRecognitionManagerImpl::Session::~Session() {
697 } // namespace content