Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #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/public/browser/speech_recognition_event_listener.h" | 16 #include "content/public/browser/speech_recognition_event_listener.h" |
| 16 #include "content/public/browser/speech_recognition_manager.h" | 17 #include "content/public/browser/speech_recognition_manager.h" |
| 17 #include "content/public/browser/speech_recognition_session_config.h" | 18 #include "content/public/browser/speech_recognition_session_config.h" |
| 18 #include "content/public/browser/speech_recognition_session_context.h" | 19 #include "content/public/browser/speech_recognition_session_context.h" |
| 19 #include "content/public/common/speech_recognition_error.h" | 20 #include "content/public/common/speech_recognition_error.h" |
| 20 | 21 |
| 21 namespace content { | 22 namespace content { |
| 22 class BrowserMainLoop; | 23 class BrowserMainLoop; |
| 23 class SpeechRecognitionManagerDelegate; | 24 class SpeechRecognitionManagerDelegate; |
| 24 } | 25 } |
| 25 | 26 |
| 27 namespace media_stream { | |
| 28 class MediaStreamManager; | |
| 29 } | |
| 30 | |
| 26 namespace speech { | 31 namespace speech { |
| 27 | 32 |
| 28 class SpeechRecognizer; | 33 class SpeechRecognizer; |
| 29 | 34 |
| 30 // This is the manager for speech recognition. It is a single instance in | 35 // This is the manager for speech recognition. It is a single instance in |
| 31 // the browser process and can serve several requests. Each recognition request | 36 // the browser process and can serve several requests. Each recognition request |
| 32 // corresponds to a session, initiated via |CreateSession|. | 37 // corresponds to a session, initiated via |CreateSession|. |
| 33 // In every moment the manager has at most one session capturing audio, which | 38 // |
| 34 // is identified by |session_id_capturing_audio_|. However, multiple sessions | 39 // In any moment, the manager has a single session known as the primary session, |
| 35 // can live in parallel in respect of the aforementioned constraint, i.e. while | 40 // |primary_session_id_|. |
| 36 // waiting for results. | 41 // This is the session that is capturing audio, waiting for user permission, |
| 37 // This class does not handle user interface objects (bubbles, tray icon). | 42 // etc. There may also be other, non-primary, sessions living in parallel that |
| 38 // Those are managed by the delegate, which receives a copy of all sessions | 43 // are waiting for results but not recording audio. |
| 39 // events. | |
| 40 // | 44 // |
| 41 // The SpeechRecognitionManager has the following responsibilities: | 45 // The SpeechRecognitionManager has the following responsibilities: |
| 42 // - Handles requests received from various render views and makes sure only | 46 // - Handles requests received from various render views and makes sure only |
| 43 // one of them accesses the audio device at any given time. | 47 // one of them accesses the audio device at any given time. |
| 44 // - Handles the instantiation of SpeechRecognitionEngine objects when | 48 // - Handles the instantiation of SpeechRecognitionEngine objects when |
| 45 // requested by SpeechRecognitionSessions. | 49 // requested by SpeechRecognitionSessions. |
| 46 // - Relays recognition results/status/error events of each session to the | 50 // - Relays recognition results/status/error events of each session to the |
| 47 // corresponding listener (demuxing on the base of their session_id). | 51 // corresponding listener (demuxing on the base of their session_id). |
| 48 // - Relays also recognition results/status/error events of every session to | 52 // - Relays also recognition results/status/error events of every session to |
| 49 // the catch-all snoop listener (optionally) provided by the delegate. | 53 // the catch-all snoop listener (optionally) provided by the delegate. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 EVENT_RECOGNITION_ENDED, | 120 EVENT_RECOGNITION_ENDED, |
| 117 EVENT_MAX_VALUE = EVENT_RECOGNITION_ENDED | 121 EVENT_MAX_VALUE = EVENT_RECOGNITION_ENDED |
| 118 }; | 122 }; |
| 119 | 123 |
| 120 struct Session { | 124 struct Session { |
| 121 Session(); | 125 Session(); |
| 122 ~Session(); | 126 ~Session(); |
| 123 | 127 |
| 124 int id; | 128 int id; |
| 125 bool listener_is_active; | 129 bool listener_is_active; |
| 130 bool is_waiting_for_permission; | |
|
Satish
2012/09/12 06:15:09
instead of this flag in each Session object, how a
hans
2012/09/12 09:46:58
Done.
| |
| 126 content::SpeechRecognitionSessionConfig config; | 131 content::SpeechRecognitionSessionConfig config; |
| 127 content::SpeechRecognitionSessionContext context; | 132 content::SpeechRecognitionSessionContext context; |
| 128 scoped_refptr<SpeechRecognizer> recognizer; | 133 scoped_refptr<SpeechRecognizer> recognizer; |
| 129 }; | 134 }; |
| 130 | 135 |
| 131 // Callback issued by the SpeechRecognitionManagerDelegate for reporting | 136 // Callback issued by the SpeechRecognitionManagerDelegate for reporting |
| 132 // asynchronously the result of the CheckRecognitionIsAllowed call. | 137 // asynchronously the result of the CheckRecognitionIsAllowed call. |
| 133 void RecognitionAllowedCallback(int session_id, bool is_allowed); | 138 void RecognitionAllowedCallback(int session_id, |
| 139 bool ask_user, | |
| 140 bool is_allowed); | |
| 134 | 141 |
| 135 // Entry point for pushing any external event into the session handling FSM. | 142 // Entry point for pushing any external event into the session handling FSM. |
| 136 void DispatchEvent(int session_id, FSMEvent event); | 143 void DispatchEvent(int session_id, FSMEvent event); |
| 137 | 144 |
| 138 // Defines the behavior of the session handling FSM, selecting the appropriate | 145 // Defines the behavior of the session handling FSM, selecting the appropriate |
| 139 // transition according to the session, its current state and the event. | 146 // transition according to the session, its current state and the event. |
| 140 void ExecuteTransitionAndGetNextState( | 147 void ExecuteTransitionAndGetNextState( |
| 141 const Session& session, FSMState session_state, FSMEvent event); | 148 const Session& session, FSMState session_state, FSMEvent event); |
| 142 | 149 |
| 143 // Retrieves the state of the session, enquiring directly the recognizer. | 150 // Retrieves the state of the session, enquiring directly the recognizer. |
| 144 FSMState GetSessionState(int session_id) const; | 151 FSMState GetSessionState(int session_id) const; |
| 145 | 152 |
| 146 // The methods below handle transitions of the session handling FSM. | 153 // The methods below handle transitions of the session handling FSM. |
| 147 void SessionStart(const Session& session); | 154 void SessionStart(const Session& session); |
| 148 void SessionAbort(const Session& session); | 155 void SessionAbort(const Session& session); |
| 149 void SessionStopAudioCapture(const Session& session); | 156 void SessionStopAudioCapture(const Session& session); |
| 150 void ResetCapturingSessionId(const Session& session); | 157 void ResetCapturingSessionId(const Session& session); |
| 151 void SessionDelete(const Session& session); | 158 void SessionDelete(const Session& session); |
| 152 void NotFeasible(const Session& session, FSMEvent event); | 159 void NotFeasible(const Session& session, FSMEvent event); |
| 153 | 160 |
| 154 bool SessionExists(int session_id) const; | 161 bool SessionExists(int session_id) const; |
| 155 const Session& GetSession(int session_id) const; | 162 const Session& GetSession(int session_id) const; |
| 156 content::SpeechRecognitionEventListener* GetListener(int session_id) const; | 163 content::SpeechRecognitionEventListener* GetListener(int session_id) const; |
| 157 content::SpeechRecognitionEventListener* GetDelegateListener() const; | 164 content::SpeechRecognitionEventListener* GetDelegateListener() const; |
| 158 int GetNextSessionID(); | 165 int GetNextSessionID(); |
| 159 | 166 |
| 160 typedef std::map<int, Session> SessionsTable; | 167 typedef std::map<int, Session> SessionsTable; |
| 161 SessionsTable sessions_; | 168 SessionsTable sessions_; |
| 162 int session_id_capturing_audio_; | 169 int primary_session_id_; |
| 163 int last_session_id_; | 170 int last_session_id_; |
| 164 bool is_dispatching_event_; | 171 bool is_dispatching_event_; |
| 165 scoped_ptr<content::SpeechRecognitionManagerDelegate> delegate_; | 172 scoped_ptr<content::SpeechRecognitionManagerDelegate> delegate_; |
| 166 | 173 |
| 167 // Used for posting asynchronous tasks (on the IO thread) without worrying | 174 // Used for posting asynchronous tasks (on the IO thread) without worrying |
| 168 // about this class being destroyed in the meanwhile (due to browser shutdown) | 175 // about this class being destroyed in the meanwhile (due to browser shutdown) |
| 169 // since tasks pending on a destroyed WeakPtr are automatically discarded. | 176 // since tasks pending on a destroyed WeakPtr are automatically discarded. |
| 170 base::WeakPtrFactory<SpeechRecognitionManagerImpl> weak_factory_; | 177 base::WeakPtrFactory<SpeechRecognitionManagerImpl> weak_factory_; |
| 178 | |
| 179 class PermissionRequest : public media_stream::MediaStreamRequester { | |
|
Satish
2012/09/12 06:15:09
can this be forward declared in this header and th
hans
2012/09/12 09:46:58
No, because it inherits from another class it can'
hans
2012/09/12 09:46:58
Done.
| |
| 180 public: | |
| 181 PermissionRequest(media_stream::MediaStreamManager* media_stream_manager, | |
| 182 const base::Callback<void(bool is_allowed)>& callback); | |
| 183 virtual ~PermissionRequest(); | |
| 184 | |
| 185 void Start(int render_process_id, int render_view_id, const GURL& origin); | |
| 186 void Abort(); | |
| 187 | |
| 188 // MediaStreamRequester methods. | |
| 189 virtual void StreamGenerated( | |
| 190 const std::string& label, | |
| 191 const media_stream::StreamDeviceInfoArray& audio_devices, | |
| 192 const media_stream::StreamDeviceInfoArray& video_devices) OVERRIDE; | |
| 193 virtual void StreamGenerationFailed(const std::string& label) OVERRIDE; | |
| 194 | |
| 195 // The callbacks below are ignored. | |
| 196 virtual void AudioDeviceFailed(const std::string& label, | |
| 197 int index) OVERRIDE {} | |
| 198 virtual void VideoDeviceFailed(const std::string& label, | |
| 199 int index) OVERRIDE {} | |
| 200 virtual void DevicesEnumerated( | |
| 201 const std::string& label, | |
| 202 const media_stream::StreamDeviceInfoArray& devices) OVERRIDE {} | |
| 203 virtual void DeviceOpened( | |
| 204 const std::string& label, | |
| 205 const media_stream::StreamDeviceInfo& device_info) OVERRIDE {} | |
| 206 | |
| 207 private: | |
| 208 media_stream::MediaStreamManager* media_stream_manager_; | |
| 209 base::Callback<void(bool is_allowed)> callback_; | |
| 210 std::string label_; | |
| 211 }; | |
| 212 scoped_ptr<PermissionRequest> permission_request_; | |
| 171 }; | 213 }; |
| 172 | 214 |
| 173 } // namespace speech | 215 } // namespace speech |
| 174 | 216 |
| 175 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_ | 217 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_ |
| OLD | NEW |