Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(669)

Side by Side Diff: content/browser/speech/speech_recognizer_impl.h

Issue 549373003: Binding media stream audio track to speech recognition [browser] Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased and nits Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 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_RECOGNIZER_IMPL_H_ 5 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_
6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_ 6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/shared_memory.h"
11 #include "base/process/process_handle.h"
12 #include "base/sync_socket.h"
13 #include "base/threading/thread.h"
10 #include "content/browser/speech/endpointer/endpointer.h" 14 #include "content/browser/speech/endpointer/endpointer.h"
11 #include "content/browser/speech/speech_recognition_engine.h" 15 #include "content/browser/speech/speech_recognition_engine.h"
12 #include "content/browser/speech/speech_recognizer.h" 16 #include "content/browser/speech/speech_recognizer.h"
13 #include "content/public/common/speech_recognition_error.h" 17 #include "content/public/common/speech_recognition_error.h"
14 #include "content/public/common/speech_recognition_result.h" 18 #include "content/public/common/speech_recognition_result.h"
15 #include "media/audio/audio_input_controller.h" 19 #include "media/audio/audio_input_controller.h"
16 #include "media/audio/audio_logging.h" 20 #include "media/audio/audio_logging.h"
17 #include "net/url_request/url_request_context_getter.h" 21 #include "net/url_request/url_request_context_getter.h"
18 22
19 namespace media { 23 namespace media {
20 class AudioBus; 24 class AudioBus;
21 class AudioManager; 25 class AudioManager;
26 class AudioParameters;
22 } 27 }
23 28
24 namespace content { 29 namespace content {
25 30
26 class SpeechRecognitionEventListener; 31 class SpeechRecognitionEventListener;
27 32
28 // Handles speech recognition for a session (identified by |session_id|), taking 33 // Handles speech recognition for a session (identified by |session_id|), taking
29 // care of audio capture, silence detection/endpointer and interaction with the 34 // care of audio capture, silence detection/endpointer and interaction with the
30 // SpeechRecognitionEngine. 35 // SpeechRecognitionEngine.
31 class CONTENT_EXPORT SpeechRecognizerImpl 36 class CONTENT_EXPORT SpeechRecognizerImpl
32 : public SpeechRecognizer, 37 : public SpeechRecognizer,
33 public media::AudioInputController::EventHandler, 38 public media::AudioInputController::EventHandler,
34 public NON_EXPORTED_BASE(SpeechRecognitionEngineDelegate) { 39 public NON_EXPORTED_BASE(SpeechRecognitionEngineDelegate) {
35 public: 40 public:
36 static const int kAudioSampleRate; 41 static const int kAudioSampleRate;
37 static const media::ChannelLayout kChannelLayout; 42 static const media::ChannelLayout kChannelLayout;
38 static const int kNumBitsPerAudioSample; 43 static const int kNumBitsPerAudioSample;
39 static const int kNoSpeechTimeoutMs; 44 static const int kNoSpeechTimeoutMs;
40 static const int kEndpointerEstimationTimeMs; 45 static const int kEndpointerEstimationTimeMs;
41 46
42 static void SetAudioManagerForTesting(media::AudioManager* audio_manager); 47 static void SetAudioManagerForTesting(media::AudioManager* audio_manager);
43 48 // TODO(burnik): abstract out the process_handle. Pass differently.
44 SpeechRecognizerImpl(SpeechRecognitionEventListener* listener, 49 SpeechRecognizerImpl(SpeechRecognitionEventListener* listener, int session_id,
45 int session_id, 50 bool continuous, bool provisional_results,
46 bool continuous, 51 bool using_audio_track,
47 bool provisional_results, 52 base::ProcessHandle process_handle,
48 SpeechRecognitionEngine* engine); 53 SpeechRecognitionEngine* engine);
49 54
50 virtual void StartRecognition(const std::string& device_id) OVERRIDE; 55 virtual void StartRecognition(const std::string& device_id) OVERRIDE;
51 virtual void AbortRecognition() OVERRIDE; 56 virtual void AbortRecognition() OVERRIDE;
52 virtual void StopAudioCapture() OVERRIDE; 57 virtual void StopAudioCapture() OVERRIDE;
53 virtual bool IsActive() const OVERRIDE; 58 virtual bool IsActive() const OVERRIDE;
54 virtual bool IsCapturingAudio() const OVERRIDE; 59 virtual bool IsCapturingAudio() const OVERRIDE;
55 const SpeechRecognitionEngine& recognition_engine() const; 60 const SpeechRecognitionEngine& recognition_engine() const;
56 61
62 // Whether to use an audio track from the renderer as the input source.
63 void set_using_audio_track(bool using_audio_track) {
tommi (sloooow) - chröme 2014/10/02 11:23:19 thread check? (and move to cc file)
burnik 2014/10/02 16:04:12 Done.
64 using_audio_track_ = using_audio_track;
65 }
66
57 private: 67 private:
58 friend class SpeechRecognizerTest; 68 friend class SpeechRecognizerTest;
59 69
60 enum FSMState { 70 enum FSMState {
61 STATE_IDLE = 0, 71 STATE_IDLE = 0,
62 STATE_STARTING, 72 STATE_STARTING,
63 STATE_ESTIMATING_ENVIRONMENT, 73 STATE_ESTIMATING_ENVIRONMENT,
64 STATE_WAITING_FOR_SPEECH, 74 STATE_WAITING_FOR_SPEECH,
65 STATE_RECOGNIZING, 75 STATE_RECOGNIZING,
66 STATE_WAITING_FINAL_RESULT, 76 STATE_WAITING_FINAL_RESULT,
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 FSMState StopCaptureAndWaitForResult(const FSMEventArgs& event_args); 119 FSMState StopCaptureAndWaitForResult(const FSMEventArgs& event_args);
110 FSMState ProcessIntermediateResult(const FSMEventArgs& event_args); 120 FSMState ProcessIntermediateResult(const FSMEventArgs& event_args);
111 FSMState ProcessFinalResult(const FSMEventArgs& event_args); 121 FSMState ProcessFinalResult(const FSMEventArgs& event_args);
112 FSMState AbortSilently(const FSMEventArgs& event_args); 122 FSMState AbortSilently(const FSMEventArgs& event_args);
113 FSMState AbortWithError(const FSMEventArgs& event_args); 123 FSMState AbortWithError(const FSMEventArgs& event_args);
114 FSMState Abort(const SpeechRecognitionError& error); 124 FSMState Abort(const SpeechRecognitionError& error);
115 FSMState DetectEndOfSpeech(const FSMEventArgs& event_args); 125 FSMState DetectEndOfSpeech(const FSMEventArgs& event_args);
116 FSMState DoNothing(const FSMEventArgs& event_args) const; 126 FSMState DoNothing(const FSMEventArgs& event_args) const;
117 FSMState NotFeasible(const FSMEventArgs& event_args); 127 FSMState NotFeasible(const FSMEventArgs& event_args);
118 128
129 // Called from |StartRecording()| when using audio track from renderer.
130 bool StartRecordingUsingAudioTrack(const media::AudioParameters& params);
131
119 // Returns the time span of captured audio samples since the start of capture. 132 // Returns the time span of captured audio samples since the start of capture.
120 int GetElapsedTimeMs() const; 133 int GetElapsedTimeMs() const;
121 134
122 // Calculates the input volume to be displayed in the UI, triggering the 135 // Calculates the input volume to be displayed in the UI, triggering the
123 // OnAudioLevelsChange event accordingly. 136 // OnAudioLevelsChange event accordingly.
124 void UpdateSignalAndNoiseLevels(const float& rms, bool clip_detected); 137 void UpdateSignalAndNoiseLevels(const float& rms, bool clip_detected);
125 138
126 void CloseAudioControllerAsynchronously(); 139 void CloseAudioControllerAsynchronously();
127 140
128 // Callback called on IO thread by audio_controller->Close(). 141 // Callback called on IO thread by audio_controller->Close().
(...skipping 18 matching lines...) Expand all
147 static media::AudioManager* audio_manager_for_tests_; 160 static media::AudioManager* audio_manager_for_tests_;
148 161
149 scoped_ptr<SpeechRecognitionEngine> recognition_engine_; 162 scoped_ptr<SpeechRecognitionEngine> recognition_engine_;
150 Endpointer endpointer_; 163 Endpointer endpointer_;
151 scoped_refptr<media::AudioInputController> audio_controller_; 164 scoped_refptr<media::AudioInputController> audio_controller_;
152 scoped_ptr<media::AudioLog> audio_log_; 165 scoped_ptr<media::AudioLog> audio_log_;
153 int num_samples_recorded_; 166 int num_samples_recorded_;
154 float audio_level_; 167 float audio_level_;
155 bool is_dispatching_event_; 168 bool is_dispatching_event_;
156 bool provisional_results_; 169 bool provisional_results_;
170 bool using_audio_track_;
171 base::ProcessHandle peer_process_handle_;
157 FSMState state_; 172 FSMState state_;
158 std::string device_id_; 173 std::string device_id_;
174 scoped_ptr<base::CancelableSyncSocket> audio_sync_socket_;
175 scoped_ptr<base::CancelableSyncSocket> foreign_sync_socket_;
176
177 // Runs on separate |audio_sync_thread_| when receiving audio from renderer.
178 virtual void OnAudioTrackData();
159 179
160 class OnDataConverter; 180 class OnDataConverter;
161 181
162 // Converts data between native input format and a WebSpeech specific 182 // Converts data between native input format and a WebSpeech specific
163 // output format. 183 // output format.
164 scoped_ptr<SpeechRecognizerImpl::OnDataConverter> audio_converter_; 184 scoped_ptr<SpeechRecognizerImpl::OnDataConverter> audio_converter_;
165 185
186 // Shared memory for passing audio data from renderer to the browser.
187 scoped_ptr<base::SharedMemory> shared_memory_;
188
189 // Audio bus wrapping the shared memory from the renderer.
190 scoped_ptr<media::AudioBus> audio_track_bus_;
191
192 // Used for synchronization when consuming audio on the |audio_track_bus_|.
193 uint32* buffer_index_;
194
195 // Thread for receiving audio chunks from the renderer.
196 scoped_ptr<base::Thread> audio_sync_thread_;
197
198 // Used to check if |OnAudioTrackData()| is executed on |audio_sync_thread_|.
199 base::ThreadChecker audio_sync_thread_checker_;
200
166 DISALLOW_COPY_AND_ASSIGN(SpeechRecognizerImpl); 201 DISALLOW_COPY_AND_ASSIGN(SpeechRecognizerImpl);
167 }; 202 };
168 203
169 } // namespace content 204 } // namespace content
170 205
171 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_ 206 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698