Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / speech / speech_recognition_manager_impl.cc
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.
4
5 #include "content/browser/speech/speech_recognition_manager_impl.h"
6
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"
26
27 #if defined(OS_ANDROID)
28 #include "content/browser/speech/speech_recognizer_impl_android.h"
29 #endif
30
31 using base::Callback;
32
33 namespace content {
34
35 SpeechRecognitionManager* SpeechRecognitionManager::manager_for_tests_;
36
37 namespace {
38
39 SpeechRecognitionManagerImpl* g_speech_recognition_manager_impl;
40
41 void ShowAudioInputSettingsOnFileThread(media::AudioManager* audio_manager) {
42   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
43   audio_manager->ShowAudioInputSettings();
44 }
45
46 }  // namespace
47
48 SpeechRecognitionManager* SpeechRecognitionManager::GetInstance() {
49   if (manager_for_tests_)
50     return manager_for_tests_;
51   return SpeechRecognitionManagerImpl::GetInstance();
52 }
53
54 void SpeechRecognitionManager::SetManagerForTesting(
55     SpeechRecognitionManager* manager) {
56   manager_for_tests_ = manager;
57 }
58
59 SpeechRecognitionManagerImpl* SpeechRecognitionManagerImpl::GetInstance() {
60   return g_speech_recognition_manager_impl;
61 }
62
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;
76 }
77
78 SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() {
79   DCHECK(g_speech_recognition_manager_impl);
80   g_speech_recognition_manager_impl = NULL;
81
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;
88   }
89   sessions_.clear();
90 }
91
92 int SpeechRecognitionManagerImpl::CreateSession(
93     const SpeechRecognitionSessionConfig& config) {
94   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
95
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;
104
105   std::string hardware_info;
106   bool can_report_metrics = false;
107   if (delegate_)
108     delegate_->GetDiagnosticInformation(&can_report_metrics, &hardware_info);
109
110   // The legacy api cannot use continuous mode.
111   DCHECK(!config.is_legacy_api || !config.continuous);
112
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).
120
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();
135
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());
143   }
144
145   google_remote_engine->SetConfig(remote_engine_config);
146
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;
157 }
158
159 void SpeechRecognitionManagerImpl::StartSession(int session_id) {
160   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
161   if (!SessionExists(session_id))
162     return;
163
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_);
168   }
169
170   primary_session_id_ = session_id;
171
172   if (delegate_) {
173     delegate_->CheckRecognitionIsAllowed(
174         session_id,
175         base::Bind(&SpeechRecognitionManagerImpl::RecognitionAllowedCallback,
176                    weak_factory_.GetWeakPtr(),
177                    session_id));
178   }
179 }
180
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;
187
188   SessionsTable::iterator iter = sessions_.find(session_id);
189   DCHECK(iter != sessions_.end());
190   Session* session = iter->second;
191
192   if (session->abort_requested)
193     return;
194
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;
207   }
208
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));
225   }
226 }
227
228 void SpeechRecognitionManagerImpl::MediaRequestPermissionCallback(
229     int session_id,
230     const MediaStreamDevices& devices,
231     scoped_ptr<MediaStreamUIProxy> stream_ui) {
232   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
233
234   SessionsTable::iterator iter = sessions_.find(session_id);
235   if (iter == sessions_.end())
236     return;
237
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;
242
243     // Save the UI object.
244     iter->second->ui = stream_ui.Pass();
245   }
246
247   // Clear the label to indicate the request has been done.
248   iter->second->context.label.clear();
249
250   // Notify the recognition about the request result.
251   RecognitionAllowedCallback(iter->first, false, is_allowed);
252 }
253
254 void SpeechRecognitionManagerImpl::AbortSession(int session_id) {
255   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
256   if (!SessionExists(session_id))
257     return;
258
259   SessionsTable::iterator iter = sessions_.find(session_id);
260   iter->second->ui.reset();
261
262   if (iter->second->abort_requested)
263     return;
264
265   iter->second->abort_requested = true;
266
267   base::MessageLoop::current()->PostTask(
268       FROM_HERE,
269       base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
270                  weak_factory_.GetWeakPtr(),
271                  session_id,
272                  EVENT_ABORT));
273 }
274
275 void SpeechRecognitionManagerImpl::StopAudioCaptureForSession(int session_id) {
276   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
277   if (!SessionExists(session_id))
278     return;
279
280   SessionsTable::iterator iter = sessions_.find(session_id);
281   iter->second->ui.reset();
282
283   base::MessageLoop::current()->PostTask(
284       FROM_HERE,
285       base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
286                  weak_factory_.GetWeakPtr(),
287                  session_id,
288                  EVENT_STOP_CAPTURE));
289 }
290
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).
295
296 void SpeechRecognitionManagerImpl::OnRecognitionStart(int session_id) {
297   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
298   if (!SessionExists(session_id))
299     return;
300
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());
305   }
306
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);
312 }
313
314 void SpeechRecognitionManagerImpl::OnAudioStart(int session_id) {
315   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
316   if (!SessionExists(session_id))
317     return;
318
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);
324 }
325
326 void SpeechRecognitionManagerImpl::OnEnvironmentEstimationComplete(
327     int session_id) {
328   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
329   if (!SessionExists(session_id))
330     return;
331
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);
337 }
338
339 void SpeechRecognitionManagerImpl::OnSoundStart(int session_id) {
340   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
341   if (!SessionExists(session_id))
342     return;
343
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);
349 }
350
351 void SpeechRecognitionManagerImpl::OnSoundEnd(int session_id) {
352   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
353   if (!SessionExists(session_id))
354     return;
355
356   if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
357     delegate_listener->OnSoundEnd(session_id);
358   if (SpeechRecognitionEventListener* listener = GetListener(session_id))
359     listener->OnSoundEnd(session_id);
360 }
361
362 void SpeechRecognitionManagerImpl::OnAudioEnd(int session_id) {
363   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
364   if (!SessionExists(session_id))
365     return;
366
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));
377 }
378
379 void SpeechRecognitionManagerImpl::OnRecognitionResults(
380     int session_id, const SpeechRecognitionResults& results) {
381   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
382   if (!SessionExists(session_id))
383     return;
384
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);
389 }
390
391 void SpeechRecognitionManagerImpl::OnRecognitionError(
392     int session_id, const SpeechRecognitionError& error) {
393   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
394   if (!SessionExists(session_id))
395     return;
396
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);
401 }
402
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;
408
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);
413 }
414
415 void SpeechRecognitionManagerImpl::OnRecognitionEnd(int session_id) {
416   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
417   if (!SessionExists(session_id))
418     return;
419
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));
430 }
431
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;
443     }
444   }
445   return kSessionIDInvalid;
446 }
447
448 SpeechRecognitionSessionContext
449 SpeechRecognitionManagerImpl::GetSessionContext(int session_id) const {
450   return GetSession(session_id)->context;
451 }
452
453 void SpeechRecognitionManagerImpl::AbortAllSessionsForRenderProcess(
454     int render_process_id) {
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->context.render_process_id == render_process_id) {
463       AbortSession(session->id);
464       session->listener_is_active = false;
465     }
466   }
467 }
468
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);
479     }
480   }
481 }
482
483 // -----------------------  Core FSM implementation ---------------------------
484 void SpeechRecognitionManagerImpl::DispatchEvent(int session_id,
485                                                  FSMEvent event) {
486   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
487
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;
492
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);
497
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;
504 }
505
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;
535       }
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);
548       }
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);
561       }
562       break;
563   }
564   return NotFeasible(*session, event);
565 }
566
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;
575 }
576
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);
580
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;
595   }
596
597   session.recognizer->StartRecognition(device_id);
598 }
599
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();
605 }
606
607 void SpeechRecognitionManagerImpl::SessionStopAudioCapture(
608     const Session& session) {
609   DCHECK(session.recognizer.get());
610   session.recognizer->StopAudioCapture();
611 }
612
613 void SpeechRecognitionManagerImpl::ResetCapturingSessionId(
614     const Session& session) {
615   DCHECK_EQ(primary_session_id_, session.id);
616   primary_session_id_ = kSessionIDInvalid;
617 }
618
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;
627 }
628
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;
634 }
635
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_;
642 }
643
644 bool SpeechRecognitionManagerImpl::SessionExists(int session_id) const {
645   return sessions_.find(session_id) != sessions_.end();
646 }
647
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;
654 }
655
656 SpeechRecognitionEventListener* SpeechRecognitionManagerImpl::GetListener(
657     int session_id) const {
658   Session* session = GetSession(session_id);
659   if (session->listener_is_active && session->config.event_listener)
660     return session->config.event_listener.get();
661   return NULL;
662 }
663
664 SpeechRecognitionEventListener*
665 SpeechRecognitionManagerImpl::GetDelegateListener() const {
666   return delegate_.get() ? delegate_->GetEventListener() : NULL;
667 }
668
669 const SpeechRecognitionSessionConfig&
670 SpeechRecognitionManagerImpl::GetSessionConfig(int session_id) const {
671   return GetSession(session_id)->config;
672 }
673
674 bool SpeechRecognitionManagerImpl::HasAudioInputDevices() {
675   return audio_manager_->HasAudioInputDevices();
676 }
677
678 base::string16 SpeechRecognitionManagerImpl::GetAudioInputDeviceModel() {
679   return audio_manager_->GetAudioInputDeviceModel();
680 }
681
682 void SpeechRecognitionManagerImpl::ShowAudioInputSettings() {
683   // Since AudioManager::ShowAudioInputSettings can potentially launch external
684   // processes, do that in the FILE thread to not block the calling threads.
685   BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
686                           base::Bind(&ShowAudioInputSettingsOnFileThread,
687                                      audio_manager_));
688 }
689
690 SpeechRecognitionManagerImpl::Session::Session()
691   : id(kSessionIDInvalid),
692     abort_requested(false),
693     listener_is_active(true) {
694 }
695
696 SpeechRecognitionManagerImpl::Session::~Session() {
697 }
698
699 }  // namespace content