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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_
6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/weak_ptr.h"
15 #include "content/browser/renderer_host/media/media_stream_requester.h"
16 #include "content/public/browser/speech_recognition_event_listener.h"
17 #include "content/public/browser/speech_recognition_manager.h"
18 #include "content/public/browser/speech_recognition_session_config.h"
19 #include "content/public/browser/speech_recognition_session_context.h"
20 #include "content/public/common/speech_recognition_error.h"
27 class BrowserMainLoop
;
28 class MediaStreamManager
;
29 class MediaStreamUIProxy
;
30 class SpeechRecognitionManagerDelegate
;
31 class SpeechRecognizer
;
33 // This is the manager for speech recognition. It is a single instance in
34 // the browser process and can serve several requests. Each recognition request
35 // corresponds to a session, initiated via |CreateSession|.
37 // In any moment, the manager has a single session known as the primary session,
38 // |primary_session_id_|.
39 // This is the session that is capturing audio, waiting for user permission,
40 // etc. There may also be other, non-primary, sessions living in parallel that
41 // are waiting for results but not recording audio.
43 // The SpeechRecognitionManager has the following responsibilities:
44 // - Handles requests received from various render views and makes sure only
45 // one of them accesses the audio device at any given time.
46 // - Handles the instantiation of SpeechRecognitionEngine objects when
47 // requested by SpeechRecognitionSessions.
48 // - Relays recognition results/status/error events of each session to the
49 // corresponding listener (demuxing on the base of their session_id).
50 // - Relays also recognition results/status/error events of every session to
51 // the catch-all snoop listener (optionally) provided by the delegate.
52 class CONTENT_EXPORT SpeechRecognitionManagerImpl
:
53 public NON_EXPORTED_BASE(SpeechRecognitionManager
),
54 public SpeechRecognitionEventListener
{
56 // Returns the current SpeechRecognitionManagerImpl or NULL if the call is
57 // issued when it is not created yet or destroyed (by BrowserMainLoop).
58 static SpeechRecognitionManagerImpl
* GetInstance();
60 // SpeechRecognitionManager implementation.
61 int CreateSession(const SpeechRecognitionSessionConfig
& config
) override
;
62 void StartSession(int session_id
) override
;
63 void AbortSession(int session_id
) override
;
64 void AbortAllSessionsForRenderProcess(int render_process_id
) override
;
65 void AbortAllSessionsForRenderView(int render_process_id
,
66 int render_view_id
) override
;
67 void StopAudioCaptureForSession(int session_id
) override
;
68 const SpeechRecognitionSessionConfig
& GetSessionConfig(
69 int session_id
) const override
;
70 SpeechRecognitionSessionContext
GetSessionContext(
71 int session_id
) const override
;
72 int GetSession(int render_process_id
,
74 int request_id
) const override
;
75 bool HasAudioInputDevices() override
;
76 base::string16
GetAudioInputDeviceModel() override
;
77 void ShowAudioInputSettings() override
;
79 // SpeechRecognitionEventListener methods.
80 void OnRecognitionStart(int session_id
) override
;
81 void OnAudioStart(int session_id
) override
;
82 void OnEnvironmentEstimationComplete(int session_id
) override
;
83 void OnSoundStart(int session_id
) override
;
84 void OnSoundEnd(int session_id
) override
;
85 void OnAudioEnd(int session_id
) override
;
86 void OnRecognitionEnd(int session_id
) override
;
87 void OnRecognitionResults(int session_id
,
88 const SpeechRecognitionResults
& result
) override
;
89 void OnRecognitionError(int session_id
,
90 const SpeechRecognitionError
& error
) override
;
91 void OnAudioLevelsChange(int session_id
,
93 float noise_volume
) override
;
95 SpeechRecognitionManagerDelegate
* delegate() const { return delegate_
.get(); }
98 // BrowserMainLoop is the only one allowed to istantiate and free us.
99 friend class BrowserMainLoop
;
101 friend struct base::DefaultDeleter
<SpeechRecognitionManagerImpl
>;
102 SpeechRecognitionManagerImpl(media::AudioManager
* audio_manager
,
103 MediaStreamManager
* media_stream_manager
);
104 ~SpeechRecognitionManagerImpl() override
;
107 // Data types for the internal Finite State Machine (FSM).
109 SESSION_STATE_IDLE
= 0,
110 SESSION_STATE_CAPTURING_AUDIO
,
111 SESSION_STATE_WAITING_FOR_RESULT
,
112 SESSION_STATE_MAX_VALUE
= SESSION_STATE_WAITING_FOR_RESULT
120 EVENT_RECOGNITION_ENDED
,
121 EVENT_MAX_VALUE
= EVENT_RECOGNITION_ENDED
129 bool abort_requested
;
130 bool listener_is_active
;
131 SpeechRecognitionSessionConfig config
;
132 SpeechRecognitionSessionContext context
;
133 scoped_refptr
<SpeechRecognizer
> recognizer
;
134 scoped_ptr
<MediaStreamUIProxy
> ui
;
137 // Callback issued by the SpeechRecognitionManagerDelegate for reporting
138 // asynchronously the result of the CheckRecognitionIsAllowed call.
139 void RecognitionAllowedCallback(int session_id
,
143 // Callback to get back the result of a media request. |devices| is an array
144 // of devices approved to be used for the request, |devices| is empty if the
145 // users deny the request.
146 void MediaRequestPermissionCallback(int session_id
,
147 const MediaStreamDevices
& devices
,
148 scoped_ptr
<MediaStreamUIProxy
> stream_ui
);
150 // Entry point for pushing any external event into the session handling FSM.
151 void DispatchEvent(int session_id
, FSMEvent event
);
153 // Defines the behavior of the session handling FSM, selecting the appropriate
154 // transition according to the session, its current state and the event.
155 void ExecuteTransitionAndGetNextState(Session
* session
,
156 FSMState session_state
,
159 // Retrieves the state of the session, enquiring directly the recognizer.
160 FSMState
GetSessionState(int session_id
) const;
162 // The methods below handle transitions of the session handling FSM.
163 void SessionStart(const Session
& session
);
164 void SessionAbort(const Session
& session
);
165 void SessionStopAudioCapture(const Session
& session
);
166 void ResetCapturingSessionId(const Session
& session
);
167 void SessionDelete(Session
* session
);
168 void NotFeasible(const Session
& session
, FSMEvent event
);
170 bool SessionExists(int session_id
) const;
171 Session
* GetSession(int session_id
) const;
172 SpeechRecognitionEventListener
* GetListener(int session_id
) const;
173 SpeechRecognitionEventListener
* GetDelegateListener() const;
174 int GetNextSessionID();
176 media::AudioManager
* audio_manager_
;
177 MediaStreamManager
* media_stream_manager_
;
178 typedef std::map
<int, Session
*> SessionsTable
;
179 SessionsTable sessions_
;
180 int primary_session_id_
;
181 int last_session_id_
;
182 bool is_dispatching_event_
;
183 scoped_ptr
<SpeechRecognitionManagerDelegate
> delegate_
;
185 // Used for posting asynchronous tasks (on the IO thread) without worrying
186 // about this class being destroyed in the meanwhile (due to browser shutdown)
187 // since tasks pending on a destroyed WeakPtr are automatically discarded.
188 base::WeakPtrFactory
<SpeechRecognitionManagerImpl
> weak_factory_
;
191 } // namespace content
193 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_