| 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 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_ |
| 6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_ | 6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "content/browser/renderer_host/media/media_stream_requester.h" | 15 #include "content/browser/renderer_host/media/media_stream_requester.h" |
| 16 #include "content/public/browser/speech_recognition_event_listener.h" | 16 #include "content/public/browser/speech_recognition_event_listener.h" |
| 17 #include "content/public/browser/speech_recognition_manager.h" | 17 #include "content/public/browser/speech_recognition_manager.h" |
| 18 #include "content/public/browser/speech_recognition_session_config.h" | 18 #include "content/public/browser/speech_recognition_session_config.h" |
| 19 #include "content/public/browser/speech_recognition_session_context.h" | 19 #include "content/public/browser/speech_recognition_session_context.h" |
| 20 #include "content/public/common/speech_recognition_error.h" | 20 #include "content/public/common/speech_recognition_error.h" |
| 21 | 21 |
| 22 namespace content { | |
| 23 class BrowserMainLoop; | |
| 24 class SpeechRecognitionManagerDelegate; | |
| 25 } | |
| 26 | |
| 27 namespace media_stream { | 22 namespace media_stream { |
| 28 class MediaStreamManager; | 23 class MediaStreamManager; |
| 29 } | 24 } |
| 30 | 25 |
| 31 namespace speech { | 26 namespace content { |
| 32 | 27 |
| 28 class BrowserMainLoop; |
| 29 class SpeechRecognitionManagerDelegate; |
| 33 class SpeechRecognizer; | 30 class SpeechRecognizer; |
| 34 | 31 |
| 35 // This is the manager for speech recognition. It is a single instance in | 32 // This is the manager for speech recognition. It is a single instance in |
| 36 // the browser process and can serve several requests. Each recognition request | 33 // the browser process and can serve several requests. Each recognition request |
| 37 // corresponds to a session, initiated via |CreateSession|. | 34 // corresponds to a session, initiated via |CreateSession|. |
| 38 // | 35 // |
| 39 // In any moment, the manager has a single session known as the primary session, | 36 // In any moment, the manager has a single session known as the primary session, |
| 40 // |primary_session_id_|. | 37 // |primary_session_id_|. |
| 41 // This is the session that is capturing audio, waiting for user permission, | 38 // This is the session that is capturing audio, waiting for user permission, |
| 42 // etc. There may also be other, non-primary, sessions living in parallel that | 39 // etc. There may also be other, non-primary, sessions living in parallel that |
| 43 // are waiting for results but not recording audio. | 40 // are waiting for results but not recording audio. |
| 44 // | 41 // |
| 45 // The SpeechRecognitionManager has the following responsibilities: | 42 // The SpeechRecognitionManager has the following responsibilities: |
| 46 // - Handles requests received from various render views and makes sure only | 43 // - Handles requests received from various render views and makes sure only |
| 47 // one of them accesses the audio device at any given time. | 44 // one of them accesses the audio device at any given time. |
| 48 // - Handles the instantiation of SpeechRecognitionEngine objects when | 45 // - Handles the instantiation of SpeechRecognitionEngine objects when |
| 49 // requested by SpeechRecognitionSessions. | 46 // requested by SpeechRecognitionSessions. |
| 50 // - Relays recognition results/status/error events of each session to the | 47 // - Relays recognition results/status/error events of each session to the |
| 51 // corresponding listener (demuxing on the base of their session_id). | 48 // corresponding listener (demuxing on the base of their session_id). |
| 52 // - Relays also recognition results/status/error events of every session to | 49 // - Relays also recognition results/status/error events of every session to |
| 53 // the catch-all snoop listener (optionally) provided by the delegate. | 50 // the catch-all snoop listener (optionally) provided by the delegate. |
| 54 class CONTENT_EXPORT SpeechRecognitionManagerImpl : | 51 class CONTENT_EXPORT SpeechRecognitionManagerImpl : |
| 55 public NON_EXPORTED_BASE(content::SpeechRecognitionManager), | 52 public NON_EXPORTED_BASE(SpeechRecognitionManager), |
| 56 public content::SpeechRecognitionEventListener { | 53 public SpeechRecognitionEventListener { |
| 57 public: | 54 public: |
| 58 // Returns the current SpeechRecognitionManagerImpl or NULL if the call is | 55 // Returns the current SpeechRecognitionManagerImpl or NULL if the call is |
| 59 // issued when it is not created yet or destroyed (by BrowserMainLoop). | 56 // issued when it is not created yet or destroyed (by BrowserMainLoop). |
| 60 static SpeechRecognitionManagerImpl* GetInstance(); | 57 static SpeechRecognitionManagerImpl* GetInstance(); |
| 61 | 58 |
| 62 // SpeechRecognitionManager implementation. | 59 // SpeechRecognitionManager implementation. |
| 63 virtual int CreateSession( | 60 virtual int CreateSession( |
| 64 const content::SpeechRecognitionSessionConfig& config) OVERRIDE; | 61 const SpeechRecognitionSessionConfig& config) OVERRIDE; |
| 65 virtual void StartSession(int session_id) OVERRIDE; | 62 virtual void StartSession(int session_id) OVERRIDE; |
| 66 virtual void AbortSession(int session_id) OVERRIDE; | 63 virtual void AbortSession(int session_id) OVERRIDE; |
| 67 virtual void AbortAllSessionsForListener( | 64 virtual void AbortAllSessionsForListener( |
| 68 content::SpeechRecognitionEventListener* listener) OVERRIDE; | 65 SpeechRecognitionEventListener* listener) OVERRIDE; |
| 69 virtual void AbortAllSessionsForRenderView(int render_process_id, | 66 virtual void AbortAllSessionsForRenderView(int render_process_id, |
| 70 int render_view_id) OVERRIDE; | 67 int render_view_id) OVERRIDE; |
| 71 virtual void StopAudioCaptureForSession(int session_id) OVERRIDE; | 68 virtual void StopAudioCaptureForSession(int session_id) OVERRIDE; |
| 72 virtual const content::SpeechRecognitionSessionConfig& GetSessionConfig( | 69 virtual const SpeechRecognitionSessionConfig& GetSessionConfig( |
| 73 int session_id) const OVERRIDE; | 70 int session_id) const OVERRIDE; |
| 74 virtual content::SpeechRecognitionSessionContext GetSessionContext( | 71 virtual SpeechRecognitionSessionContext GetSessionContext( |
| 75 int session_id) const OVERRIDE; | 72 int session_id) const OVERRIDE; |
| 76 virtual int GetSession(int render_process_id, | 73 virtual int GetSession(int render_process_id, |
| 77 int render_view_id, | 74 int render_view_id, |
| 78 int request_id) const OVERRIDE; | 75 int request_id) const OVERRIDE; |
| 79 virtual bool HasAudioInputDevices() OVERRIDE; | 76 virtual bool HasAudioInputDevices() OVERRIDE; |
| 80 virtual bool IsCapturingAudio() OVERRIDE; | 77 virtual bool IsCapturingAudio() OVERRIDE; |
| 81 virtual string16 GetAudioInputDeviceModel() OVERRIDE; | 78 virtual string16 GetAudioInputDeviceModel() OVERRIDE; |
| 82 virtual void ShowAudioInputSettings() OVERRIDE; | 79 virtual void ShowAudioInputSettings() OVERRIDE; |
| 83 | 80 |
| 84 // SpeechRecognitionEventListener methods. | 81 // SpeechRecognitionEventListener methods. |
| 85 virtual void OnRecognitionStart(int session_id) OVERRIDE; | 82 virtual void OnRecognitionStart(int session_id) OVERRIDE; |
| 86 virtual void OnAudioStart(int session_id) OVERRIDE; | 83 virtual void OnAudioStart(int session_id) OVERRIDE; |
| 87 virtual void OnEnvironmentEstimationComplete(int session_id) OVERRIDE; | 84 virtual void OnEnvironmentEstimationComplete(int session_id) OVERRIDE; |
| 88 virtual void OnSoundStart(int session_id) OVERRIDE; | 85 virtual void OnSoundStart(int session_id) OVERRIDE; |
| 89 virtual void OnSoundEnd(int session_id) OVERRIDE; | 86 virtual void OnSoundEnd(int session_id) OVERRIDE; |
| 90 virtual void OnAudioEnd(int session_id) OVERRIDE; | 87 virtual void OnAudioEnd(int session_id) OVERRIDE; |
| 91 virtual void OnRecognitionEnd(int session_id) OVERRIDE; | 88 virtual void OnRecognitionEnd(int session_id) OVERRIDE; |
| 92 virtual void OnRecognitionResult( | 89 virtual void OnRecognitionResult( |
| 93 int session_id, const content::SpeechRecognitionResult& result) OVERRIDE; | 90 int session_id, const SpeechRecognitionResult& result) OVERRIDE; |
| 94 virtual void OnRecognitionError( | 91 virtual void OnRecognitionError( |
| 95 int session_id, const content::SpeechRecognitionError& error) OVERRIDE; | 92 int session_id, const SpeechRecognitionError& error) OVERRIDE; |
| 96 virtual void OnAudioLevelsChange(int session_id, float volume, | 93 virtual void OnAudioLevelsChange(int session_id, float volume, |
| 97 float noise_volume) OVERRIDE; | 94 float noise_volume) OVERRIDE; |
| 98 | 95 |
| 99 protected: | 96 protected: |
| 100 // BrowserMainLoop is the only one allowed to istantiate and free us. | 97 // BrowserMainLoop is the only one allowed to istantiate and free us. |
| 101 friend class content::BrowserMainLoop; | 98 friend class BrowserMainLoop; |
| 102 friend class scoped_ptr<SpeechRecognitionManagerImpl>; // Needed for dtor. | 99 friend class scoped_ptr<SpeechRecognitionManagerImpl>; // Needed for dtor. |
| 103 SpeechRecognitionManagerImpl(); | 100 SpeechRecognitionManagerImpl(); |
| 104 virtual ~SpeechRecognitionManagerImpl(); | 101 virtual ~SpeechRecognitionManagerImpl(); |
| 105 | 102 |
| 106 private: | 103 private: |
| 107 // Data types for the internal Finite State Machine (FSM). | 104 // Data types for the internal Finite State Machine (FSM). |
| 108 enum FSMState { | 105 enum FSMState { |
| 109 SESSION_STATE_IDLE = 0, | 106 SESSION_STATE_IDLE = 0, |
| 110 SESSION_STATE_CAPTURING_AUDIO, | 107 SESSION_STATE_CAPTURING_AUDIO, |
| 111 SESSION_STATE_WAITING_FOR_RESULT, | 108 SESSION_STATE_WAITING_FOR_RESULT, |
| 112 SESSION_STATE_MAX_VALUE = SESSION_STATE_WAITING_FOR_RESULT | 109 SESSION_STATE_MAX_VALUE = SESSION_STATE_WAITING_FOR_RESULT |
| 113 }; | 110 }; |
| 114 | 111 |
| 115 enum FSMEvent { | 112 enum FSMEvent { |
| 116 EVENT_ABORT = 0, | 113 EVENT_ABORT = 0, |
| 117 EVENT_START, | 114 EVENT_START, |
| 118 EVENT_STOP_CAPTURE, | 115 EVENT_STOP_CAPTURE, |
| 119 EVENT_AUDIO_ENDED, | 116 EVENT_AUDIO_ENDED, |
| 120 EVENT_RECOGNITION_ENDED, | 117 EVENT_RECOGNITION_ENDED, |
| 121 EVENT_MAX_VALUE = EVENT_RECOGNITION_ENDED | 118 EVENT_MAX_VALUE = EVENT_RECOGNITION_ENDED |
| 122 }; | 119 }; |
| 123 | 120 |
| 124 struct Session { | 121 struct Session { |
| 125 Session(); | 122 Session(); |
| 126 ~Session(); | 123 ~Session(); |
| 127 | 124 |
| 128 int id; | 125 int id; |
| 129 bool listener_is_active; | 126 bool listener_is_active; |
| 130 content::SpeechRecognitionSessionConfig config; | 127 SpeechRecognitionSessionConfig config; |
| 131 content::SpeechRecognitionSessionContext context; | 128 SpeechRecognitionSessionContext context; |
| 132 scoped_refptr<SpeechRecognizer> recognizer; | 129 scoped_refptr<SpeechRecognizer> recognizer; |
| 133 }; | 130 }; |
| 134 | 131 |
| 135 // Callback issued by the SpeechRecognitionManagerDelegate for reporting | 132 // Callback issued by the SpeechRecognitionManagerDelegate for reporting |
| 136 // asynchronously the result of the CheckRecognitionIsAllowed call. | 133 // asynchronously the result of the CheckRecognitionIsAllowed call. |
| 137 void RecognitionAllowedCallback(int session_id, | 134 void RecognitionAllowedCallback(int session_id, |
| 138 bool ask_user, | 135 bool ask_user, |
| 139 bool is_allowed); | 136 bool is_allowed); |
| 140 | 137 |
| 141 // Entry point for pushing any external event into the session handling FSM. | 138 // Entry point for pushing any external event into the session handling FSM. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 152 // The methods below handle transitions of the session handling FSM. | 149 // The methods below handle transitions of the session handling FSM. |
| 153 void SessionStart(const Session& session); | 150 void SessionStart(const Session& session); |
| 154 void SessionAbort(const Session& session); | 151 void SessionAbort(const Session& session); |
| 155 void SessionStopAudioCapture(const Session& session); | 152 void SessionStopAudioCapture(const Session& session); |
| 156 void ResetCapturingSessionId(const Session& session); | 153 void ResetCapturingSessionId(const Session& session); |
| 157 void SessionDelete(const Session& session); | 154 void SessionDelete(const Session& session); |
| 158 void NotFeasible(const Session& session, FSMEvent event); | 155 void NotFeasible(const Session& session, FSMEvent event); |
| 159 | 156 |
| 160 bool SessionExists(int session_id) const; | 157 bool SessionExists(int session_id) const; |
| 161 const Session& GetSession(int session_id) const; | 158 const Session& GetSession(int session_id) const; |
| 162 content::SpeechRecognitionEventListener* GetListener(int session_id) const; | 159 SpeechRecognitionEventListener* GetListener(int session_id) const; |
| 163 content::SpeechRecognitionEventListener* GetDelegateListener() const; | 160 SpeechRecognitionEventListener* GetDelegateListener() const; |
| 164 int GetNextSessionID(); | 161 int GetNextSessionID(); |
| 165 | 162 |
| 166 typedef std::map<int, Session> SessionsTable; | 163 typedef std::map<int, Session> SessionsTable; |
| 167 SessionsTable sessions_; | 164 SessionsTable sessions_; |
| 168 int primary_session_id_; | 165 int primary_session_id_; |
| 169 int last_session_id_; | 166 int last_session_id_; |
| 170 bool is_dispatching_event_; | 167 bool is_dispatching_event_; |
| 171 scoped_ptr<content::SpeechRecognitionManagerDelegate> delegate_; | 168 scoped_ptr<SpeechRecognitionManagerDelegate> delegate_; |
| 172 | 169 |
| 173 // Used for posting asynchronous tasks (on the IO thread) without worrying | 170 // Used for posting asynchronous tasks (on the IO thread) without worrying |
| 174 // about this class being destroyed in the meanwhile (due to browser shutdown) | 171 // about this class being destroyed in the meanwhile (due to browser shutdown) |
| 175 // since tasks pending on a destroyed WeakPtr are automatically discarded. | 172 // since tasks pending on a destroyed WeakPtr are automatically discarded. |
| 176 base::WeakPtrFactory<SpeechRecognitionManagerImpl> weak_factory_; | 173 base::WeakPtrFactory<SpeechRecognitionManagerImpl> weak_factory_; |
| 177 | 174 |
| 178 #if !defined(OS_IOS) | 175 #if !defined(OS_IOS) |
| 179 class PermissionRequest; | 176 class PermissionRequest; |
| 180 scoped_ptr<PermissionRequest> permission_request_; | 177 scoped_ptr<PermissionRequest> permission_request_; |
| 181 #endif // !defined(OS_IOS) | 178 #endif // !defined(OS_IOS) |
| 182 }; | 179 }; |
| 183 | 180 |
| 184 } // namespace speech | 181 } // namespace content |
| 185 | 182 |
| 186 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_ | 183 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_ |
| OLD | NEW |