Home | History | Annotate | Download | only in speech
      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::SetManagerForTests(
     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       google_remote_engine);
    152 #else
    153   session->recognizer = new SpeechRecognizerImplAndroid(this, session_id);
    154 #endif
    155   return session_id;
    156 }
    157 
    158 void SpeechRecognitionManagerImpl::StartSession(int session_id) {
    159   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    160   if (!SessionExists(session_id))
    161     return;
    162 
    163   // If there is another active session, abort that.
    164   if (primary_session_id_ != kSessionIDInvalid &&
    165       primary_session_id_ != session_id) {
    166     AbortSession(primary_session_id_);
    167   }
    168 
    169   primary_session_id_ = session_id;
    170 
    171   if (delegate_) {
    172     delegate_->CheckRecognitionIsAllowed(
    173         session_id,
    174         base::Bind(&SpeechRecognitionManagerImpl::RecognitionAllowedCallback,
    175                    weak_factory_.GetWeakPtr(),
    176                    session_id));
    177   }
    178 }
    179 
    180 void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id,
    181                                                               bool ask_user,
    182                                                               bool is_allowed) {
    183   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    184   if (!SessionExists(session_id))
    185     return;
    186 
    187   if (ask_user) {
    188     SessionsTable::iterator iter = sessions_.find(session_id);
    189     DCHECK(iter != sessions_.end());
    190     SpeechRecognitionSessionContext& context = iter->second->context;
    191     context.label = media_stream_manager_->MakeMediaAccessRequest(
    192         context.render_process_id,
    193         context.render_view_id,
    194         context.request_id,
    195         StreamOptions(MEDIA_DEVICE_AUDIO_CAPTURE, MEDIA_NO_SERVICE),
    196         GURL(context.context_name),
    197         base::Bind(
    198             &SpeechRecognitionManagerImpl::MediaRequestPermissionCallback,
    199             weak_factory_.GetWeakPtr(), session_id));
    200     return;
    201   }
    202 
    203   if (is_allowed) {
    204     base::MessageLoop::current()->PostTask(
    205         FROM_HERE,
    206         base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
    207                    weak_factory_.GetWeakPtr(),
    208                    session_id,
    209                    EVENT_START));
    210   } else {
    211     OnRecognitionError(session_id, SpeechRecognitionError(
    212         SPEECH_RECOGNITION_ERROR_NOT_ALLOWED));
    213     base::MessageLoop::current()->PostTask(
    214         FROM_HERE,
    215         base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
    216                    weak_factory_.GetWeakPtr(),
    217                    session_id,
    218                    EVENT_ABORT));
    219   }
    220 }
    221 
    222 void SpeechRecognitionManagerImpl::MediaRequestPermissionCallback(
    223     int session_id,
    224     const MediaStreamDevices& devices,
    225     scoped_ptr<MediaStreamUIProxy> stream_ui) {
    226   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    227 
    228   SessionsTable::iterator iter = sessions_.find(session_id);
    229   if (iter == sessions_.end())
    230     return;
    231 
    232   bool is_allowed = !devices.empty();
    233   if (is_allowed) {
    234     // Copy the approved devices array to the context for UI indication.
    235     iter->second->context.devices = devices;
    236 
    237     // Save the UI object.
    238     iter->second->ui = stream_ui.Pass();
    239   }
    240 
    241   // Clear the label to indicate the request has been done.
    242   iter->second->context.label.clear();
    243 
    244   // Notify the recognition about the request result.
    245   RecognitionAllowedCallback(iter->first, false, is_allowed);
    246 }
    247 
    248 void SpeechRecognitionManagerImpl::AbortSession(int session_id) {
    249   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    250   if (!SessionExists(session_id))
    251     return;
    252 
    253   SessionsTable::iterator iter = sessions_.find(session_id);
    254   iter->second->ui.reset();
    255 
    256   base::MessageLoop::current()->PostTask(
    257       FROM_HERE,
    258       base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
    259                  weak_factory_.GetWeakPtr(),
    260                  session_id,
    261                  EVENT_ABORT));
    262 }
    263 
    264 void SpeechRecognitionManagerImpl::StopAudioCaptureForSession(int session_id) {
    265   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    266   if (!SessionExists(session_id))
    267     return;
    268 
    269   SessionsTable::iterator iter = sessions_.find(session_id);
    270   iter->second->ui.reset();
    271 
    272   base::MessageLoop::current()->PostTask(
    273       FROM_HERE,
    274       base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
    275                  weak_factory_.GetWeakPtr(),
    276                  session_id,
    277                  EVENT_STOP_CAPTURE));
    278 }
    279 
    280 // Here begins the SpeechRecognitionEventListener interface implementation,
    281 // which will simply relay the events to the proper listener registered for the
    282 // particular session (most likely InputTagSpeechDispatcherHost) and to the
    283 // catch-all listener provided by the delegate (if any).
    284 
    285 void SpeechRecognitionManagerImpl::OnRecognitionStart(int session_id) {
    286   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    287   if (!SessionExists(session_id))
    288     return;
    289 
    290   SessionsTable::iterator iter = sessions_.find(session_id);
    291   if (iter->second->ui) {
    292     // Notify the UI that the devices are being used.
    293     iter->second->ui->OnStarted(base::Closure());
    294   }
    295 
    296   DCHECK_EQ(primary_session_id_, session_id);
    297   if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
    298     delegate_listener->OnRecognitionStart(session_id);
    299   if (SpeechRecognitionEventListener* listener = GetListener(session_id))
    300     listener->OnRecognitionStart(session_id);
    301 }
    302 
    303 void SpeechRecognitionManagerImpl::OnAudioStart(int session_id) {
    304   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    305   if (!SessionExists(session_id))
    306     return;
    307 
    308   DCHECK_EQ(primary_session_id_, session_id);
    309   if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
    310     delegate_listener->OnAudioStart(session_id);
    311   if (SpeechRecognitionEventListener* listener = GetListener(session_id))
    312     listener->OnAudioStart(session_id);
    313 }
    314 
    315 void SpeechRecognitionManagerImpl::OnEnvironmentEstimationComplete(
    316     int session_id) {
    317   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    318   if (!SessionExists(session_id))
    319     return;
    320 
    321   DCHECK_EQ(primary_session_id_, session_id);
    322   if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
    323     delegate_listener->OnEnvironmentEstimationComplete(session_id);
    324   if (SpeechRecognitionEventListener* listener = GetListener(session_id))
    325     listener->OnEnvironmentEstimationComplete(session_id);
    326 }
    327 
    328 void SpeechRecognitionManagerImpl::OnSoundStart(int session_id) {
    329   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    330   if (!SessionExists(session_id))
    331     return;
    332 
    333   DCHECK_EQ(primary_session_id_, session_id);
    334   if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
    335     delegate_listener->OnSoundStart(session_id);
    336   if (SpeechRecognitionEventListener* listener = GetListener(session_id))
    337     listener->OnSoundStart(session_id);
    338 }
    339 
    340 void SpeechRecognitionManagerImpl::OnSoundEnd(int session_id) {
    341   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    342   if (!SessionExists(session_id))
    343     return;
    344 
    345   if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
    346     delegate_listener->OnSoundEnd(session_id);
    347   if (SpeechRecognitionEventListener* listener = GetListener(session_id))
    348     listener->OnSoundEnd(session_id);
    349 }
    350 
    351 void SpeechRecognitionManagerImpl::OnAudioEnd(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->OnAudioEnd(session_id);
    358   if (SpeechRecognitionEventListener* listener = GetListener(session_id))
    359     listener->OnAudioEnd(session_id);
    360   base::MessageLoop::current()->PostTask(
    361       FROM_HERE,
    362       base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
    363                  weak_factory_.GetWeakPtr(),
    364                  session_id,
    365                  EVENT_AUDIO_ENDED));
    366 }
    367 
    368 void SpeechRecognitionManagerImpl::OnRecognitionResults(
    369     int session_id, const SpeechRecognitionResults& results) {
    370   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    371   if (!SessionExists(session_id))
    372     return;
    373 
    374   if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
    375     delegate_listener->OnRecognitionResults(session_id, results);
    376   if (SpeechRecognitionEventListener* listener = GetListener(session_id))
    377     listener->OnRecognitionResults(session_id, results);
    378 }
    379 
    380 void SpeechRecognitionManagerImpl::OnRecognitionError(
    381     int session_id, const SpeechRecognitionError& error) {
    382   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    383   if (!SessionExists(session_id))
    384     return;
    385 
    386   if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
    387     delegate_listener->OnRecognitionError(session_id, error);
    388   if (SpeechRecognitionEventListener* listener = GetListener(session_id))
    389     listener->OnRecognitionError(session_id, error);
    390 }
    391 
    392 void SpeechRecognitionManagerImpl::OnAudioLevelsChange(
    393     int session_id, float volume, float noise_volume) {
    394   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    395   if (!SessionExists(session_id))
    396     return;
    397 
    398   if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
    399     delegate_listener->OnAudioLevelsChange(session_id, volume, noise_volume);
    400   if (SpeechRecognitionEventListener* listener = GetListener(session_id))
    401     listener->OnAudioLevelsChange(session_id, volume, noise_volume);
    402 }
    403 
    404 void SpeechRecognitionManagerImpl::OnRecognitionEnd(int session_id) {
    405   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    406   if (!SessionExists(session_id))
    407     return;
    408 
    409   if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
    410     delegate_listener->OnRecognitionEnd(session_id);
    411   if (SpeechRecognitionEventListener* listener = GetListener(session_id))
    412     listener->OnRecognitionEnd(session_id);
    413   base::MessageLoop::current()->PostTask(
    414       FROM_HERE,
    415       base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
    416                  weak_factory_.GetWeakPtr(),
    417                  session_id,
    418                  EVENT_RECOGNITION_ENDED));
    419 }
    420 
    421 int SpeechRecognitionManagerImpl::GetSession(
    422     int render_process_id, int render_view_id, int request_id) const {
    423   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    424   SessionsTable::const_iterator iter;
    425   for(iter = sessions_.begin(); iter != sessions_.end(); ++iter) {
    426     const int session_id = iter->first;
    427     const SpeechRecognitionSessionContext& context = iter->second->context;
    428     if (context.render_process_id == render_process_id &&
    429         context.render_view_id == render_view_id &&
    430         context.request_id == request_id) {
    431       return session_id;
    432     }
    433   }
    434   return kSessionIDInvalid;
    435 }
    436 
    437 SpeechRecognitionSessionContext
    438 SpeechRecognitionManagerImpl::GetSessionContext(int session_id) const {
    439   return GetSession(session_id)->context;
    440 }
    441 
    442 void SpeechRecognitionManagerImpl::AbortAllSessionsForListener(
    443     SpeechRecognitionEventListener* listener) {
    444   // This method gracefully destroys sessions for the listener. However, since
    445   // the listener itself is likely to be destroyed after this call, we avoid
    446   // dispatching further events to it, marking the |listener_is_active| flag.
    447   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    448   for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end();
    449        ++it) {
    450     Session* session = it->second;
    451     if (session->config.event_listener == listener) {
    452       AbortSession(session->id);
    453       session->listener_is_active = false;
    454     }
    455   }
    456 }
    457 
    458 void SpeechRecognitionManagerImpl::AbortAllSessionsForRenderView(
    459     int render_process_id,
    460     int render_view_id) {
    461   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    462   for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end();
    463        ++it) {
    464     Session* session = it->second;
    465     if (session->context.render_process_id == render_process_id &&
    466         session->context.render_view_id == render_view_id) {
    467       AbortSession(session->id);
    468     }
    469   }
    470 }
    471 
    472 // -----------------------  Core FSM implementation ---------------------------
    473 void SpeechRecognitionManagerImpl::DispatchEvent(int session_id,
    474                                                  FSMEvent event) {
    475   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    476 
    477   // There are some corner cases in which the session might be deleted (due to
    478   // an EndRecognition event) between a request (e.g. Abort) and its dispatch.
    479   if (!SessionExists(session_id))
    480     return;
    481 
    482   Session* session = GetSession(session_id);
    483   FSMState session_state = GetSessionState(session_id);
    484   DCHECK_LE(session_state, SESSION_STATE_MAX_VALUE);
    485   DCHECK_LE(event, EVENT_MAX_VALUE);
    486 
    487   // Event dispatching must be sequential, otherwise it will break all the rules
    488   // and the assumptions of the finite state automata model.
    489   DCHECK(!is_dispatching_event_);
    490   is_dispatching_event_ = true;
    491   ExecuteTransitionAndGetNextState(session, session_state, event);
    492   is_dispatching_event_ = false;
    493 }
    494 
    495 // This FSM handles the evolution of each session, from the viewpoint of the
    496 // interaction with the user (that may be either the browser end-user which
    497 // interacts with UI bubbles, or JS developer intracting with JS methods).
    498 // All the events received by the SpeechRecognizer instances (one for each
    499 // session) are always routed to the SpeechRecognitionEventListener(s)
    500 // regardless the choices taken in this FSM.
    501 void SpeechRecognitionManagerImpl::ExecuteTransitionAndGetNextState(
    502     Session* session, FSMState session_state, FSMEvent event) {
    503   // Note: since we're not tracking the state of the recognizer object, rather
    504   // we're directly retrieving it (through GetSessionState), we see its events
    505   // (that are AUDIO_ENDED and RECOGNITION_ENDED) after its state evolution
    506   // (e.g., when we receive the AUDIO_ENDED event, the recognizer has just
    507   // completed the transition from CAPTURING_AUDIO to WAITING_FOR_RESULT, thus
    508   // we perceive the AUDIO_ENDED event in WAITING_FOR_RESULT).
    509   // This makes the code below a bit tricky but avoids a lot of code for
    510   // tracking and reconstructing asynchronously the state of the recognizer.
    511   switch (session_state) {
    512     case SESSION_STATE_IDLE:
    513       switch (event) {
    514         case EVENT_START:
    515           return SessionStart(*session);
    516         case EVENT_ABORT:
    517           return SessionAbort(*session);
    518         case EVENT_RECOGNITION_ENDED:
    519           return SessionDelete(session);
    520         case EVENT_STOP_CAPTURE:
    521           return SessionStopAudioCapture(*session);
    522         case EVENT_AUDIO_ENDED:
    523           return;
    524       }
    525       break;
    526     case SESSION_STATE_CAPTURING_AUDIO:
    527       switch (event) {
    528         case EVENT_STOP_CAPTURE:
    529           return SessionStopAudioCapture(*session);
    530         case EVENT_ABORT:
    531           return SessionAbort(*session);
    532         case EVENT_START:
    533           return;
    534         case EVENT_AUDIO_ENDED:
    535         case EVENT_RECOGNITION_ENDED:
    536           return NotFeasible(*session, event);
    537       }
    538       break;
    539     case SESSION_STATE_WAITING_FOR_RESULT:
    540       switch (event) {
    541         case EVENT_ABORT:
    542           return SessionAbort(*session);
    543         case EVENT_AUDIO_ENDED:
    544           return ResetCapturingSessionId(*session);
    545         case EVENT_START:
    546         case EVENT_STOP_CAPTURE:
    547           return;
    548         case EVENT_RECOGNITION_ENDED:
    549           return NotFeasible(*session, event);
    550       }
    551       break;
    552   }
    553   return NotFeasible(*session, event);
    554 }
    555 
    556 SpeechRecognitionManagerImpl::FSMState
    557 SpeechRecognitionManagerImpl::GetSessionState(int session_id) const {
    558   Session* session = GetSession(session_id);
    559   if (!session->recognizer.get() || !session->recognizer->IsActive())
    560     return SESSION_STATE_IDLE;
    561   if (session->recognizer->IsCapturingAudio())
    562     return SESSION_STATE_CAPTURING_AUDIO;
    563   return SESSION_STATE_WAITING_FOR_RESULT;
    564 }
    565 
    566 // ----------- Contract for all the FSM evolution functions below -------------
    567 //  - Are guaranteed to be executed in the IO thread;
    568 //  - Are guaranteed to be not reentrant (themselves and each other);
    569 
    570 void SpeechRecognitionManagerImpl::SessionStart(const Session& session) {
    571   DCHECK_EQ(primary_session_id_, session.id);
    572   const MediaStreamDevices& devices = session.context.devices;
    573   std::string device_id;
    574   if (devices.empty()) {
    575     // From the ask_user=false path, use the default device.
    576     // TODO(xians): Abort the session after we do not need to support this path
    577     // anymore.
    578     device_id = media::AudioManagerBase::kDefaultDeviceId;
    579   } else {
    580     // From the ask_user=true path, use the selected device.
    581     DCHECK_EQ(1u, devices.size());
    582     DCHECK_EQ(MEDIA_DEVICE_AUDIO_CAPTURE, devices.front().type);
    583     device_id = devices.front().id;
    584   }
    585 
    586   session.recognizer->StartRecognition(device_id);
    587 }
    588 
    589 void SpeechRecognitionManagerImpl::SessionAbort(const Session& session) {
    590   if (primary_session_id_ == session.id)
    591     primary_session_id_ = kSessionIDInvalid;
    592   DCHECK(session.recognizer.get());
    593   session.recognizer->AbortRecognition();
    594 }
    595 
    596 void SpeechRecognitionManagerImpl::SessionStopAudioCapture(
    597     const Session& session) {
    598   DCHECK(session.recognizer.get());
    599   session.recognizer->StopAudioCapture();
    600 }
    601 
    602 void SpeechRecognitionManagerImpl::ResetCapturingSessionId(
    603     const Session& session) {
    604   DCHECK_EQ(primary_session_id_, session.id);
    605   primary_session_id_ = kSessionIDInvalid;
    606 }
    607 
    608 void SpeechRecognitionManagerImpl::SessionDelete(Session* session) {
    609   DCHECK(session->recognizer.get() == NULL || !session->recognizer->IsActive());
    610   if (primary_session_id_ == session->id)
    611     primary_session_id_ = kSessionIDInvalid;
    612   sessions_.erase(session->id);
    613   delete session;
    614 }
    615 
    616 void SpeechRecognitionManagerImpl::NotFeasible(const Session& session,
    617                                                FSMEvent event) {
    618   NOTREACHED() << "Unfeasible event " << event
    619                << " in state " << GetSessionState(session.id)
    620                << " for session " << session.id;
    621 }
    622 
    623 int SpeechRecognitionManagerImpl::GetNextSessionID() {
    624   ++last_session_id_;
    625   // Deal with wrapping of last_session_id_. (How civilized).
    626   if (last_session_id_ <= 0)
    627     last_session_id_ = 1;
    628   return last_session_id_;
    629 }
    630 
    631 bool SpeechRecognitionManagerImpl::SessionExists(int session_id) const {
    632   return sessions_.find(session_id) != sessions_.end();
    633 }
    634 
    635 SpeechRecognitionManagerImpl::Session*
    636 SpeechRecognitionManagerImpl::GetSession(int session_id) const {
    637   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    638   SessionsTable::const_iterator iter = sessions_.find(session_id);
    639   DCHECK(iter != sessions_.end());
    640   return iter->second;
    641 }
    642 
    643 SpeechRecognitionEventListener* SpeechRecognitionManagerImpl::GetListener(
    644     int session_id) const {
    645   Session* session = GetSession(session_id);
    646   return session->listener_is_active ? session->config.event_listener : NULL;
    647 }
    648 
    649 SpeechRecognitionEventListener*
    650 SpeechRecognitionManagerImpl::GetDelegateListener() const {
    651   return delegate_.get() ? delegate_->GetEventListener() : NULL;
    652 }
    653 
    654 const SpeechRecognitionSessionConfig&
    655 SpeechRecognitionManagerImpl::GetSessionConfig(int session_id) const {
    656   return GetSession(session_id)->config;
    657 }
    658 
    659 bool SpeechRecognitionManagerImpl::HasAudioInputDevices() {
    660   return audio_manager_->HasAudioInputDevices();
    661 }
    662 
    663 string16 SpeechRecognitionManagerImpl::GetAudioInputDeviceModel() {
    664   return audio_manager_->GetAudioInputDeviceModel();
    665 }
    666 
    667 void SpeechRecognitionManagerImpl::ShowAudioInputSettings() {
    668   // Since AudioManager::ShowAudioInputSettings can potentially launch external
    669   // processes, do that in the FILE thread to not block the calling threads.
    670   BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
    671                           base::Bind(&ShowAudioInputSettingsOnFileThread,
    672                                      audio_manager_));
    673 }
    674 
    675 SpeechRecognitionManagerImpl::Session::Session()
    676   : id(kSessionIDInvalid),
    677     listener_is_active(true) {
    678 }
    679 
    680 SpeechRecognitionManagerImpl::Session::~Session() {
    681 }
    682 
    683 }  // namespace content
    684