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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/memory/singleton.h" | |
16 #include "content/public/browser/speech_recognition_event_listener.h" | 15 #include "content/public/browser/speech_recognition_event_listener.h" |
17 #include "content/public/browser/speech_recognition_manager.h" | 16 #include "content/public/browser/speech_recognition_manager.h" |
18 #include "content/public/browser/speech_recognition_session_config.h" | 17 #include "content/public/browser/speech_recognition_session_config.h" |
19 #include "content/public/browser/speech_recognition_session_context.h" | 18 #include "content/public/browser/speech_recognition_session_context.h" |
20 #include "content/public/common/speech_recognition_error.h" | 19 #include "content/public/common/speech_recognition_error.h" |
21 | 20 |
22 namespace content { | 21 namespace content { |
| 22 class BrowserMainLoop; |
23 class SpeechRecognitionManagerDelegate; | 23 class SpeechRecognitionManagerDelegate; |
24 } | 24 } |
25 | 25 |
26 namespace speech { | 26 namespace speech { |
27 | 27 |
28 class SpeechRecognizerImpl; | 28 class SpeechRecognizerImpl; |
29 | 29 |
30 // This is the manager for speech recognition. It is a singleton instance in | 30 // 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 | 31 // the browser process and can serve several requests. Each recognition request |
32 // corresponds to a session, initiated via |CreateSession|. | 32 // corresponds to a session, initiated via |CreateSession|. |
33 // In every moment the manager has at most one session capturing audio, which | 33 // In every moment the manager has at most one session capturing audio, which |
34 // is identified by |session_id_capturing_audio_|. However, multiple sessions | 34 // is identified by |session_id_capturing_audio_|. However, multiple sessions |
35 // can live in parallel in respect of the aforementioned constraint, i.e. while | 35 // can live in parallel in respect of the aforementioned constraint, i.e. while |
36 // waiting for results. | 36 // waiting for results. |
37 // This class does not handle user interface objects (bubbles, tray icon). | 37 // This class does not handle user interface objects (bubbles, tray icon). |
38 // Those are managed by the delegate, which receives a copy of all sessions | 38 // Those are managed by the delegate, which receives a copy of all sessions |
39 // events. | 39 // events. |
40 // | 40 // |
41 // The SpeechRecognitionManager has the following responsibilities: | 41 // The SpeechRecognitionManager has the following responsibilities: |
42 // - Handles requests received from various render views and makes sure only | 42 // - Handles requests received from various render views and makes sure only |
43 // one of them accesses the audio device at any given time. | 43 // one of them accesses the audio device at any given time. |
44 // - Handles the instantiation of SpeechRecognitionEngine objects when | 44 // - Handles the instantiation of SpeechRecognitionEngine objects when |
45 // requested by SpeechRecognitionSessions. | 45 // requested by SpeechRecognitionSessions. |
46 // - Relays recognition results/status/error events of each session to the | 46 // - Relays recognition results/status/error events of each session to the |
47 // corresponding listener (demuxing on the base of their session_id). | 47 // corresponding listener (demuxing on the base of their session_id). |
48 // - Relays also recognition results/status/error events of every session to | 48 // - Relays also recognition results/status/error events of every session to |
49 // the catch-all snoop listener (optionally) provided by the delegate. | 49 // the catch-all snoop listener (optionally) provided by the delegate. |
50 class CONTENT_EXPORT SpeechRecognitionManagerImpl : | 50 class CONTENT_EXPORT SpeechRecognitionManagerImpl : |
51 public NON_EXPORTED_BASE(content::SpeechRecognitionManager), | 51 public NON_EXPORTED_BASE(content::SpeechRecognitionManager), |
52 public content::SpeechRecognitionEventListener { | 52 public content::SpeechRecognitionEventListener { |
53 public: | 53 public: |
| 54 // Returns the current SpeechRecognitionManagerImpl. Can be called only after |
| 55 // the RecognitionMnager has been created (by BrowserMainLoop). |
54 static SpeechRecognitionManagerImpl* GetInstance(); | 56 static SpeechRecognitionManagerImpl* GetInstance(); |
55 | 57 |
56 // SpeechRecognitionManager implementation. | 58 // SpeechRecognitionManager implementation. |
57 virtual int CreateSession( | 59 virtual int CreateSession( |
58 const content::SpeechRecognitionSessionConfig& config) OVERRIDE; | 60 const content::SpeechRecognitionSessionConfig& config) OVERRIDE; |
59 virtual void StartSession(int session_id) OVERRIDE; | 61 virtual void StartSession(int session_id) OVERRIDE; |
60 virtual void AbortSession(int session_id) OVERRIDE; | 62 virtual void AbortSession(int session_id) OVERRIDE; |
61 virtual void AbortAllSessionsForListener( | 63 virtual void AbortAllSessionsForListener( |
62 content::SpeechRecognitionEventListener* listener) OVERRIDE; | 64 content::SpeechRecognitionEventListener* listener) OVERRIDE; |
63 virtual void StopAudioCaptureForSession(int session_id) OVERRIDE; | 65 virtual void StopAudioCaptureForSession(int session_id) OVERRIDE; |
(...skipping 19 matching lines...) Expand all Loading... |
83 virtual void OnAudioEnd(int session_id) OVERRIDE; | 85 virtual void OnAudioEnd(int session_id) OVERRIDE; |
84 virtual void OnRecognitionEnd(int session_id) OVERRIDE; | 86 virtual void OnRecognitionEnd(int session_id) OVERRIDE; |
85 virtual void OnRecognitionResult( | 87 virtual void OnRecognitionResult( |
86 int session_id, const content::SpeechRecognitionResult& result) OVERRIDE; | 88 int session_id, const content::SpeechRecognitionResult& result) OVERRIDE; |
87 virtual void OnRecognitionError( | 89 virtual void OnRecognitionError( |
88 int session_id, const content::SpeechRecognitionError& error) OVERRIDE; | 90 int session_id, const content::SpeechRecognitionError& error) OVERRIDE; |
89 virtual void OnAudioLevelsChange(int session_id, float volume, | 91 virtual void OnAudioLevelsChange(int session_id, float volume, |
90 float noise_volume) OVERRIDE; | 92 float noise_volume) OVERRIDE; |
91 | 93 |
92 protected: | 94 protected: |
93 // Private constructor to enforce singleton. | 95 // BrowserMainLoop is the only one allowed to istantiate and free us. |
94 friend struct DefaultSingletonTraits<SpeechRecognitionManagerImpl>; | 96 friend class content::BrowserMainLoop; |
| 97 friend class scoped_ptr<SpeechRecognitionManagerImpl>; // Needed for dtor. |
95 SpeechRecognitionManagerImpl(); | 98 SpeechRecognitionManagerImpl(); |
96 virtual ~SpeechRecognitionManagerImpl(); | 99 virtual ~SpeechRecognitionManagerImpl(); |
97 | 100 |
98 private: | 101 private: |
99 // Data types for the internal Finite State Machine (FSM). | 102 // Data types for the internal Finite State Machine (FSM). |
100 enum FSMState { | 103 enum FSMState { |
101 SESSION_STATE_IDLE = 0, | 104 SESSION_STATE_IDLE = 0, |
102 SESSION_STATE_CAPTURING_AUDIO, | 105 SESSION_STATE_CAPTURING_AUDIO, |
103 SESSION_STATE_WAITING_FOR_RESULT, | 106 SESSION_STATE_WAITING_FOR_RESULT, |
104 SESSION_STATE_MAX_VALUE = SESSION_STATE_WAITING_FOR_RESULT | 107 SESSION_STATE_MAX_VALUE = SESSION_STATE_WAITING_FOR_RESULT |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 const Session& GetSession(int session_id) const; | 154 const Session& GetSession(int session_id) const; |
152 content::SpeechRecognitionEventListener* GetListener(int session_id) const; | 155 content::SpeechRecognitionEventListener* GetListener(int session_id) const; |
153 content::SpeechRecognitionEventListener* GetDelegateListener() const; | 156 content::SpeechRecognitionEventListener* GetDelegateListener() const; |
154 int GetNextSessionID(); | 157 int GetNextSessionID(); |
155 | 158 |
156 typedef std::map<int, Session> SessionsTable; | 159 typedef std::map<int, Session> SessionsTable; |
157 SessionsTable sessions_; | 160 SessionsTable sessions_; |
158 int session_id_capturing_audio_; | 161 int session_id_capturing_audio_; |
159 int last_session_id_; | 162 int last_session_id_; |
160 bool is_dispatching_event_; | 163 bool is_dispatching_event_; |
161 content::SpeechRecognitionManagerDelegate* delegate_; | 164 scoped_ptr<content::SpeechRecognitionManagerDelegate> delegate_; |
162 }; | 165 }; |
163 | 166 |
164 } // namespace speech | 167 } // namespace speech |
165 | 168 |
166 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_ | 169 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_ |
OLD | NEW |