OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "content/browser/speech/speech_input_manager.h" | 5 #include "content/browser/speech/speech_input_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "content/browser/renderer_host/render_view_host.h" | 8 #include "content/browser/renderer_host/render_view_host.h" |
9 #include "content/browser/renderer_host/render_view_host_delegate.h" | 9 #include "content/browser/renderer_host/render_view_host_delegate.h" |
10 #include "content/browser/speech/speech_input_preferences.h" | 10 #include "content/browser/speech/speech_input_preferences.h" |
11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 #include "content/public/common/view_type.h" | 12 #include "content/public/common/view_type.h" |
13 #include "media/audio/audio_manager.h" | 13 #include "media/audio/audio_manager.h" |
14 | 14 |
15 using content::BrowserThread; | 15 using content::BrowserThread; |
16 | 16 |
17 namespace speech_input { | 17 namespace speech_input { |
18 | 18 |
19 struct SpeechInputManager::SpeechInputParams { | 19 struct SpeechInputManager::SpeechInputParams { |
20 SpeechInputParams(Delegate* delegate, | 20 SpeechInputParams(Delegate* delegate, |
21 int caller_id, | 21 int caller_id, |
22 int render_process_id, | 22 int render_process_id, |
23 int render_view_id, | 23 int render_view_id, |
24 const gfx::Rect& element_rect, | 24 const gfx::Rect& element_rect, |
25 const std::string& language, | 25 const std::string& language, |
26 const std::string& grammar, | 26 const std::string& grammar, |
27 const std::string& origin_url, | 27 const std::string& origin_url, |
28 net::URLRequestContextGetter* context_getter, | 28 net::URLRequestContextGetter* context_getter, |
29 SpeechInputPreferences* speech_input_prefs) | 29 SpeechInputPreferences* speech_input_prefs, |
| 30 AudioManager* audio_manager) |
30 : delegate(delegate), | 31 : delegate(delegate), |
31 caller_id(caller_id), | 32 caller_id(caller_id), |
32 render_process_id(render_process_id), | 33 render_process_id(render_process_id), |
33 render_view_id(render_view_id), | 34 render_view_id(render_view_id), |
34 element_rect(element_rect), | 35 element_rect(element_rect), |
35 language(language), | 36 language(language), |
36 grammar(grammar), | 37 grammar(grammar), |
37 origin_url(origin_url), | 38 origin_url(origin_url), |
38 context_getter(context_getter), | 39 context_getter(context_getter), |
39 speech_input_prefs(speech_input_prefs) { | 40 speech_input_prefs(speech_input_prefs), |
| 41 audio_manager_(audio_manager) { |
40 } | 42 } |
41 | 43 |
42 Delegate* delegate; | 44 Delegate* delegate; |
43 int caller_id; | 45 int caller_id; |
44 int render_process_id; | 46 int render_process_id; |
45 int render_view_id; | 47 int render_view_id; |
46 gfx::Rect element_rect; | 48 gfx::Rect element_rect; |
47 std::string language; | 49 std::string language; |
48 std::string grammar; | 50 std::string grammar; |
49 std::string origin_url; | 51 std::string origin_url; |
50 net::URLRequestContextGetter* context_getter; | 52 net::URLRequestContextGetter* context_getter; |
51 SpeechInputPreferences* speech_input_prefs; | 53 SpeechInputPreferences* speech_input_prefs; |
| 54 scoped_refptr<AudioManager> audio_manager_; |
52 }; | 55 }; |
53 | 56 |
54 SpeechInputManager::SpeechInputManager() | 57 SpeechInputManager::SpeechInputManager() |
55 : can_report_metrics_(false), | 58 : can_report_metrics_(false), |
56 recording_caller_id_(0) { | 59 recording_caller_id_(0) { |
57 } | 60 } |
58 | 61 |
59 SpeechInputManager::~SpeechInputManager() { | 62 SpeechInputManager::~SpeechInputManager() { |
60 while (requests_.begin() != requests_.end()) | 63 while (requests_.begin() != requests_.end()) |
61 CancelRecognition(requests_.begin()->first); | 64 CancelRecognition(requests_.begin()->first); |
62 } | 65 } |
63 | 66 |
64 bool SpeechInputManager::HasPendingRequest(int caller_id) const { | 67 bool SpeechInputManager::HasPendingRequest(int caller_id) const { |
65 return requests_.find(caller_id) != requests_.end(); | 68 return requests_.find(caller_id) != requests_.end(); |
66 } | 69 } |
67 | 70 |
68 SpeechInputManagerDelegate* SpeechInputManager::GetDelegate( | 71 SpeechInputManagerDelegate* SpeechInputManager::GetDelegate( |
69 int caller_id) const { | 72 int caller_id) const { |
70 return requests_.find(caller_id)->second.delegate; | 73 return requests_.find(caller_id)->second.delegate; |
71 } | 74 } |
72 | 75 |
73 void SpeechInputManager::ShowAudioInputSettings() { | 76 void SpeechInputManager::ShowAudioInputSettings(AudioManager* audio_manager) { |
74 // Since AudioManager::ShowAudioInputSettings can potentially launch external | 77 // Since AudioManager::ShowAudioInputSettings can potentially launch external |
75 // processes, do that in the FILE thread to not block the calling threads. | 78 // processes, do that in the FILE thread to not block the calling threads. |
76 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 79 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
77 BrowserThread::PostTask( | 80 BrowserThread::PostTask( |
78 BrowserThread::FILE, FROM_HERE, | 81 BrowserThread::FILE, FROM_HERE, |
79 base::Bind(&SpeechInputManager::ShowAudioInputSettings)); | 82 base::Bind(&SpeechInputManager::ShowAudioInputSettings, |
| 83 base::Unretained(audio_manager))); |
80 return; | 84 return; |
81 } | 85 } |
82 | 86 |
83 DCHECK(AudioManager::GetAudioManager()->CanShowAudioInputSettings()); | 87 DCHECK(audio_manager->CanShowAudioInputSettings()); |
84 if (AudioManager::GetAudioManager()->CanShowAudioInputSettings()) | 88 if (audio_manager->CanShowAudioInputSettings()) |
85 AudioManager::GetAudioManager()->ShowAudioInputSettings(); | 89 audio_manager->ShowAudioInputSettings(); |
86 } | 90 } |
87 | 91 |
88 void SpeechInputManager::StartRecognition( | 92 void SpeechInputManager::StartRecognition( |
89 SpeechInputManagerDelegate* delegate, | 93 SpeechInputManagerDelegate* delegate, |
90 int caller_id, | 94 int caller_id, |
91 int render_process_id, | 95 int render_process_id, |
92 int render_view_id, | 96 int render_view_id, |
93 const gfx::Rect& element_rect, | 97 const gfx::Rect& element_rect, |
94 const std::string& language, | 98 const std::string& language, |
95 const std::string& grammar, | 99 const std::string& grammar, |
96 const std::string& origin_url, | 100 const std::string& origin_url, |
97 net::URLRequestContextGetter* context_getter, | 101 net::URLRequestContextGetter* context_getter, |
98 SpeechInputPreferences* speech_input_prefs) { | 102 SpeechInputPreferences* speech_input_prefs, |
| 103 AudioManager* audio_manager) { |
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
100 BrowserThread::PostTask( | 105 BrowserThread::PostTask( |
101 BrowserThread::UI, FROM_HERE, | 106 BrowserThread::UI, FROM_HERE, |
102 base::Bind(&SpeechInputManager::CheckRenderViewTypeAndStartRecognition, | 107 base::Bind(&SpeechInputManager::CheckRenderViewTypeAndStartRecognition, |
103 base::Unretained(this), SpeechInputParams(delegate, caller_id, | 108 base::Unretained(this), SpeechInputParams(delegate, caller_id, |
104 render_process_id, render_view_id, element_rect, language, grammar, | 109 render_process_id, render_view_id, element_rect, language, grammar, |
105 origin_url, context_getter, speech_input_prefs))); | 110 origin_url, context_getter, speech_input_prefs, audio_manager))); |
106 } | 111 } |
107 | 112 |
108 void SpeechInputManager::CheckRenderViewTypeAndStartRecognition( | 113 void SpeechInputManager::CheckRenderViewTypeAndStartRecognition( |
109 const SpeechInputParams& params) { | 114 const SpeechInputParams& params) { |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
111 | 116 |
112 RenderViewHost* render_view_host = RenderViewHost::FromID( | 117 RenderViewHost* render_view_host = RenderViewHost::FromID( |
113 params.render_process_id, params.render_view_id); | 118 params.render_process_id, params.render_view_id); |
114 if (!render_view_host || !render_view_host->delegate()) | 119 if (!render_view_host || !render_view_host->delegate()) |
115 return; | 120 return; |
(...skipping 20 matching lines...) Expand all Loading... |
136 | 141 |
137 ShowRecognitionRequested( | 142 ShowRecognitionRequested( |
138 params.caller_id, params.render_process_id, params.render_view_id, | 143 params.caller_id, params.render_process_id, params.render_view_id, |
139 params.element_rect); | 144 params.element_rect); |
140 GetRequestInfo(&can_report_metrics_, &request_info_); | 145 GetRequestInfo(&can_report_metrics_, &request_info_); |
141 | 146 |
142 SpeechInputRequest* request = &requests_[params.caller_id]; | 147 SpeechInputRequest* request = &requests_[params.caller_id]; |
143 request->delegate = params.delegate; | 148 request->delegate = params.delegate; |
144 request->recognizer = new SpeechRecognizer( | 149 request->recognizer = new SpeechRecognizer( |
145 this, params.caller_id, params.language, params.grammar, | 150 this, params.caller_id, params.language, params.grammar, |
146 params.context_getter, params.speech_input_prefs->filter_profanities(), | 151 params.context_getter, params.audio_manager_.get(), |
| 152 params.speech_input_prefs->filter_profanities(), |
147 request_info_, can_report_metrics_ ? params.origin_url : ""); | 153 request_info_, can_report_metrics_ ? params.origin_url : ""); |
148 request->is_active = false; | 154 request->is_active = false; |
149 | 155 |
150 StartRecognitionForRequest(params.caller_id); | 156 StartRecognitionForRequest(params.caller_id); |
151 } | 157 } |
152 | 158 |
153 void SpeechInputManager::StartRecognitionForRequest(int caller_id) { | 159 void SpeechInputManager::StartRecognitionForRequest(int caller_id) { |
154 DCHECK(HasPendingRequest(caller_id)); | 160 SpeechRecognizerMap::iterator request = requests_.find(caller_id); |
| 161 if (request == requests_.end()) { |
| 162 NOTREACHED(); |
| 163 return; |
| 164 } |
155 | 165 |
156 // If we are currently recording audio for another caller, abort that cleanly. | 166 // If we are currently recording audio for another caller, abort that cleanly. |
157 if (recording_caller_id_) | 167 if (recording_caller_id_) |
158 CancelRecognitionAndInformDelegate(recording_caller_id_); | 168 CancelRecognitionAndInformDelegate(recording_caller_id_); |
159 | 169 |
160 if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) { | 170 AudioManager* audio_man = request->second.recognizer->audio_manager(); |
| 171 if (!audio_man->HasAudioInputDevices()) { |
161 ShowMicError(caller_id, kNoDeviceAvailable); | 172 ShowMicError(caller_id, kNoDeviceAvailable); |
162 } else if (AudioManager::GetAudioManager()->IsRecordingInProcess()) { | 173 } else if (audio_man->IsRecordingInProcess()) { |
163 ShowMicError(caller_id, kDeviceInUse); | 174 ShowMicError(caller_id, kDeviceInUse); |
164 } else { | 175 } else { |
165 recording_caller_id_ = caller_id; | 176 recording_caller_id_ = caller_id; |
166 requests_[caller_id].is_active = true; | 177 requests_[caller_id].is_active = true; |
167 requests_[caller_id].recognizer->StartRecording(); | 178 requests_[caller_id].recognizer->StartRecording(); |
168 ShowWarmUp(caller_id); | 179 ShowWarmUp(caller_id); |
169 } | 180 } |
170 } | 181 } |
171 | 182 |
172 void SpeechInputManager::CancelRecognition(int caller_id) { | 183 void SpeechInputManager::CancelRecognition(int caller_id) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 | 289 |
279 SpeechInputManager::SpeechInputRequest::SpeechInputRequest() | 290 SpeechInputManager::SpeechInputRequest::SpeechInputRequest() |
280 : delegate(NULL), | 291 : delegate(NULL), |
281 is_active(false) { | 292 is_active(false) { |
282 } | 293 } |
283 | 294 |
284 SpeechInputManager::SpeechInputRequest::~SpeechInputRequest() { | 295 SpeechInputManager::SpeechInputRequest::~SpeechInputRequest() { |
285 } | 296 } |
286 | 297 |
287 } // namespace speech_input | 298 } // namespace speech_input |
OLD | NEW |