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 #include "content/renderer/speech_recognition_dispatcher.h" | 5 #include "content/renderer/speech_recognition_dispatcher.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "content/common/speech_recognition_messages.h" | 9 #include "content/common/speech_recognition_messages.h" |
10 #include "content/renderer/media/speech_recognition_audio_source_provider.h" | |
10 #include "content/renderer/render_view_impl.h" | 11 #include "content/renderer/render_view_impl.h" |
11 #include "third_party/WebKit/public/platform/WebString.h" | 12 #include "third_party/WebKit/public/platform/WebString.h" |
12 #include "third_party/WebKit/public/platform/WebVector.h" | 13 #include "third_party/WebKit/public/platform/WebVector.h" |
13 #include "third_party/WebKit/public/web/WebSpeechGrammar.h" | 14 #include "third_party/WebKit/public/web/WebSpeechGrammar.h" |
14 #include "third_party/WebKit/public/web/WebSpeechRecognitionParams.h" | 15 #include "third_party/WebKit/public/web/WebSpeechRecognitionParams.h" |
15 #include "third_party/WebKit/public/web/WebSpeechRecognitionResult.h" | 16 #include "third_party/WebKit/public/web/WebSpeechRecognitionResult.h" |
16 #include "third_party/WebKit/public/web/WebSpeechRecognizerClient.h" | 17 #include "third_party/WebKit/public/web/WebSpeechRecognizerClient.h" |
17 | 18 |
18 using blink::WebVector; | 19 using blink::WebVector; |
19 using blink::WebString; | 20 using blink::WebString; |
20 using blink::WebSpeechGrammar; | 21 using blink::WebSpeechGrammar; |
21 using blink::WebSpeechRecognitionHandle; | 22 using blink::WebSpeechRecognitionHandle; |
22 using blink::WebSpeechRecognitionResult; | 23 using blink::WebSpeechRecognitionResult; |
23 using blink::WebSpeechRecognitionParams; | 24 using blink::WebSpeechRecognitionParams; |
24 using blink::WebSpeechRecognizerClient; | 25 using blink::WebSpeechRecognizerClient; |
25 | 26 |
26 namespace content { | 27 namespace content { |
27 | 28 |
28 SpeechRecognitionDispatcher::SpeechRecognitionDispatcher( | 29 SpeechRecognitionDispatcher::SpeechRecognitionDispatcher( |
29 RenderViewImpl* render_view) | 30 RenderViewImpl* render_view) |
30 : RenderViewObserver(render_view), | 31 : RenderViewObserver(render_view), |
31 recognizer_client_(NULL), | 32 recognizer_client_(NULL), |
32 next_id_(1) { | 33 next_id_(1) { } |
tommi (sloooow) - chröme
2014/09/24 09:52:00
{}
burnik
2014/09/24 11:54:22
Done.
| |
33 } | |
34 | 34 |
35 SpeechRecognitionDispatcher::~SpeechRecognitionDispatcher() { | 35 SpeechRecognitionDispatcher::~SpeechRecognitionDispatcher() { |
36 } | 36 } |
37 | 37 |
38 void SpeechRecognitionDispatcher::AbortAllRecognitions() { | 38 void SpeechRecognitionDispatcher::AbortAllRecognitions() { |
39 audio_source_provider_.reset(); | |
tommi (sloooow) - chröme
2014/09/24 09:52:00
on which thread does this execute?
burnik
2014/09/24 11:54:22
Same as constructor would be my best guess.
Actual
| |
39 Send(new SpeechRecognitionHostMsg_AbortAllRequests( | 40 Send(new SpeechRecognitionHostMsg_AbortAllRequests( |
40 routing_id())); | 41 routing_id())); |
41 } | 42 } |
42 | 43 |
43 bool SpeechRecognitionDispatcher::OnMessageReceived( | 44 bool SpeechRecognitionDispatcher::OnMessageReceived( |
44 const IPC::Message& message) { | 45 const IPC::Message& message) { |
45 bool handled = true; | 46 bool handled = true; |
46 IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcher, message) | 47 IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcher, message) |
47 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Started, OnRecognitionStarted) | 48 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Started, OnRecognitionStarted) |
48 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioStarted, OnAudioStarted) | 49 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioStarted, OnAudioStarted) |
49 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundStarted, OnSoundStarted) | 50 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundStarted, OnSoundStarted) |
50 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundEnded, OnSoundEnded) | 51 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundEnded, OnSoundEnded) |
51 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded) | 52 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded) |
52 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred, OnErrorOccurred) | 53 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred, OnErrorOccurred) |
53 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded) | 54 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded) |
54 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, | 55 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, |
55 OnResultsRetrieved) | 56 OnResultsRetrieved) |
57 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioTrackReady, OnAudioTrackReady) | |
56 IPC_MESSAGE_UNHANDLED(handled = false) | 58 IPC_MESSAGE_UNHANDLED(handled = false) |
57 IPC_END_MESSAGE_MAP() | 59 IPC_END_MESSAGE_MAP() |
58 return handled; | 60 return handled; |
59 } | 61 } |
60 | 62 |
61 void SpeechRecognitionDispatcher::start( | 63 void SpeechRecognitionDispatcher::start( |
62 const WebSpeechRecognitionHandle& handle, | 64 const WebSpeechRecognitionHandle& handle, |
63 const WebSpeechRecognitionParams& params, | 65 const WebSpeechRecognitionParams& params, |
64 WebSpeechRecognizerClient* recognizer_client) { | 66 WebSpeechRecognizerClient* recognizer_client) { |
65 DCHECK(!recognizer_client_ || recognizer_client_ == recognizer_client); | 67 DCHECK(!recognizer_client_ || recognizer_client_ == recognizer_client); |
66 recognizer_client_ = recognizer_client; | 68 recognizer_client_ = recognizer_client; |
67 | 69 |
70 const blink::WebMediaStreamTrack track = params.audioTrack(); | |
71 if (!track.isNull()) { | |
72 // Check if this type of track is allowed by implemented policy. | |
73 if (SpeechRecognitionAudioSourceProvider::IsSupportedTrack(track)) { | |
74 audio_track_.assign(track); | |
75 } else { | |
76 audio_track_.reset(); | |
77 // Notify user that the track used is not supported. | |
78 recognizer_client_->didReceiveError( | |
79 handle, | |
80 WebString("Provided audioTrack is not supported. Ignoring track."), | |
81 WebSpeechRecognizerClient::NotAllowedError); | |
82 | |
83 return; | |
84 } | |
85 } | |
86 | |
87 // Destroy any previous instance to detach from the audio track. | |
88 // Each new session should reinstantiate the provider once the track is ready. | |
89 audio_source_provider_.reset(); | |
90 | |
68 SpeechRecognitionHostMsg_StartRequest_Params msg_params; | 91 SpeechRecognitionHostMsg_StartRequest_Params msg_params; |
69 for (size_t i = 0; i < params.grammars().size(); ++i) { | 92 for (size_t i = 0; i < params.grammars().size(); ++i) { |
70 const WebSpeechGrammar& grammar = params.grammars()[i]; | 93 const WebSpeechGrammar& grammar = params.grammars()[i]; |
71 msg_params.grammars.push_back( | 94 msg_params.grammars.push_back( |
72 SpeechRecognitionGrammar(grammar.src().spec(), grammar.weight())); | 95 SpeechRecognitionGrammar(grammar.src().spec(), grammar.weight())); |
73 } | 96 } |
74 msg_params.language = base::UTF16ToUTF8(params.language()); | 97 msg_params.language = base::UTF16ToUTF8(params.language()); |
75 msg_params.max_hypotheses = static_cast<uint32>(params.maxAlternatives()); | 98 msg_params.max_hypotheses = static_cast<uint32>(params.maxAlternatives()); |
76 msg_params.continuous = params.continuous(); | 99 msg_params.continuous = params.continuous(); |
77 msg_params.interim_results = params.interimResults(); | 100 msg_params.interim_results = params.interimResults(); |
78 msg_params.origin_url = params.origin().toString().utf8(); | 101 msg_params.origin_url = params.origin().toString().utf8(); |
79 msg_params.render_view_id = routing_id(); | 102 msg_params.render_view_id = routing_id(); |
80 msg_params.request_id = GetOrCreateIDForHandle(handle); | 103 msg_params.request_id = GetOrCreateIDForHandle(handle); |
104 // fall back to default input when the track is not allowed | |
105 msg_params.using_audio_track = !audio_track_.isNull(); | |
81 // The handle mapping will be removed in |OnRecognitionEnd|. | 106 // The handle mapping will be removed in |OnRecognitionEnd|. |
82 Send(new SpeechRecognitionHostMsg_StartRequest(msg_params)); | 107 Send(new SpeechRecognitionHostMsg_StartRequest(msg_params)); |
83 } | 108 } |
84 | 109 |
85 void SpeechRecognitionDispatcher::stop( | 110 void SpeechRecognitionDispatcher::stop( |
86 const WebSpeechRecognitionHandle& handle, | 111 const WebSpeechRecognitionHandle& handle, |
87 WebSpeechRecognizerClient* recognizer_client) { | 112 WebSpeechRecognizerClient* recognizer_client) { |
113 audio_source_provider_.reset(); | |
88 // Ignore a |stop| issued without a matching |start|. | 114 // Ignore a |stop| issued without a matching |start|. |
89 if (recognizer_client_ != recognizer_client || !HandleExists(handle)) | 115 if (recognizer_client_ != recognizer_client || !HandleExists(handle)) |
90 return; | 116 return; |
91 Send(new SpeechRecognitionHostMsg_StopCaptureRequest( | 117 Send(new SpeechRecognitionHostMsg_StopCaptureRequest( |
92 routing_id(), GetOrCreateIDForHandle(handle))); | 118 routing_id(), GetOrCreateIDForHandle(handle))); |
93 } | 119 } |
94 | 120 |
95 void SpeechRecognitionDispatcher::abort( | 121 void SpeechRecognitionDispatcher::abort( |
96 const WebSpeechRecognitionHandle& handle, | 122 const WebSpeechRecognitionHandle& handle, |
97 WebSpeechRecognizerClient* recognizer_client) { | 123 WebSpeechRecognizerClient* recognizer_client) { |
124 audio_source_provider_.reset(); | |
98 // Ignore an |abort| issued without a matching |start|. | 125 // Ignore an |abort| issued without a matching |start|. |
99 if (recognizer_client_ != recognizer_client || !HandleExists(handle)) | 126 if (recognizer_client_ != recognizer_client || !HandleExists(handle)) |
100 return; | 127 return; |
101 Send(new SpeechRecognitionHostMsg_AbortRequest( | 128 Send(new SpeechRecognitionHostMsg_AbortRequest( |
102 routing_id(), GetOrCreateIDForHandle(handle))); | 129 routing_id(), GetOrCreateIDForHandle(handle))); |
103 } | 130 } |
104 | 131 |
105 void SpeechRecognitionDispatcher::OnRecognitionStarted(int request_id) { | 132 void SpeechRecognitionDispatcher::OnRecognitionStarted(int request_id) { |
106 recognizer_client_->didStart(GetHandleFromID(request_id)); | 133 recognizer_client_->didStart(GetHandleFromID(request_id)); |
107 } | 134 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 NOTREACHED(); | 174 NOTREACHED(); |
148 return WebSpeechRecognizerClient::OtherError; | 175 return WebSpeechRecognizerClient::OtherError; |
149 } | 176 } |
150 | 177 |
151 void SpeechRecognitionDispatcher::OnErrorOccurred( | 178 void SpeechRecognitionDispatcher::OnErrorOccurred( |
152 int request_id, const SpeechRecognitionError& error) { | 179 int request_id, const SpeechRecognitionError& error) { |
153 if (error.code == SPEECH_RECOGNITION_ERROR_NO_MATCH) { | 180 if (error.code == SPEECH_RECOGNITION_ERROR_NO_MATCH) { |
154 recognizer_client_->didReceiveNoMatch(GetHandleFromID(request_id), | 181 recognizer_client_->didReceiveNoMatch(GetHandleFromID(request_id), |
155 WebSpeechRecognitionResult()); | 182 WebSpeechRecognitionResult()); |
156 } else { | 183 } else { |
184 audio_source_provider_.reset(); | |
157 recognizer_client_->didReceiveError( | 185 recognizer_client_->didReceiveError( |
158 GetHandleFromID(request_id), | 186 GetHandleFromID(request_id), |
159 WebString(), // TODO(primiano): message? | 187 WebString(), // TODO(primiano): message? |
160 WebKitErrorCode(error.code)); | 188 WebKitErrorCode(error.code)); |
161 } | 189 } |
162 } | 190 } |
163 | 191 |
164 void SpeechRecognitionDispatcher::OnRecognitionEnded(int request_id) { | 192 void SpeechRecognitionDispatcher::OnRecognitionEnded(int request_id) { |
165 // TODO(tommi): It is possible that the handle isn't found in the array if | 193 // TODO(tommi): It is possible that the handle isn't found in the array if |
166 // the user just refreshed the page. It seems that we then get a notification | 194 // the user just refreshed the page. It seems that we then get a notification |
167 // for the previously loaded instance of the page. | 195 // for the previously loaded instance of the page. |
168 HandleMap::iterator iter = handle_map_.find(request_id); | 196 HandleMap::iterator iter = handle_map_.find(request_id); |
169 if (iter == handle_map_.end()) { | 197 if (iter == handle_map_.end()) { |
170 DLOG(ERROR) << "OnRecognitionEnded called for a handle that doesn't exist"; | 198 DLOG(ERROR) << "OnRecognitionEnded called for a handle that doesn't exist"; |
171 } else { | 199 } else { |
172 WebSpeechRecognitionHandle handle = iter->second; | 200 WebSpeechRecognitionHandle handle = iter->second; |
173 // Note: we need to erase the handle from the map *before* calling didEnd. | 201 // Note: we need to erase the handle from the map *before* calling didEnd. |
174 // didEnd may call back synchronously to start a new recognition session, | 202 // didEnd may call back synchronously to start a new recognition session, |
175 // and we don't want to delete the handle from the map after that happens. | 203 // and we don't want to delete the handle from the map after that happens. |
176 handle_map_.erase(request_id); | 204 handle_map_.erase(request_id); |
205 audio_source_provider_.reset(); | |
177 recognizer_client_->didEnd(handle); | 206 recognizer_client_->didEnd(handle); |
178 } | 207 } |
179 } | 208 } |
180 | 209 |
181 void SpeechRecognitionDispatcher::OnResultsRetrieved( | 210 void SpeechRecognitionDispatcher::OnResultsRetrieved( |
182 int request_id, const SpeechRecognitionResults& results) { | 211 int request_id, const SpeechRecognitionResults& results) { |
183 size_t provisional_count = 0; | 212 size_t provisional_count = 0; |
184 SpeechRecognitionResults::const_iterator it = results.begin(); | 213 SpeechRecognitionResults::const_iterator it = results.begin(); |
185 for (; it != results.end(); ++it) { | 214 for (; it != results.end(); ++it) { |
186 if (it->is_provisional) | 215 if (it->is_provisional) |
(...skipping 17 matching lines...) Expand all Loading... | |
204 transcripts[i] = result.hypotheses[i].utterance; | 233 transcripts[i] = result.hypotheses[i].utterance; |
205 confidences[i] = static_cast<float>(result.hypotheses[i].confidence); | 234 confidences[i] = static_cast<float>(result.hypotheses[i].confidence); |
206 } | 235 } |
207 webkit_result->assign(transcripts, confidences, !result.is_provisional); | 236 webkit_result->assign(transcripts, confidences, !result.is_provisional); |
208 } | 237 } |
209 | 238 |
210 recognizer_client_->didReceiveResults( | 239 recognizer_client_->didReceiveResults( |
211 GetHandleFromID(request_id), final, provisional); | 240 GetHandleFromID(request_id), final, provisional); |
212 } | 241 } |
213 | 242 |
243 void SpeechRecognitionDispatcher::OnAudioTrackReady( | |
244 int request_id, | |
245 const media::AudioParameters& params, | |
246 base::SharedMemoryHandle memory, | |
247 base::SyncSocket::TransitDescriptor descriptor) { | |
248 DCHECK(!audio_source_provider_.get()); | |
249 if (audio_track_.isNull()) { | |
250 audio_source_provider_.reset(); | |
251 return; | |
252 } | |
253 | |
254 // Create socket here and pass ownership to the |audio_source_provider_|. | |
255 scoped_ptr<base::CancelableSyncSocket> socket(new base::CancelableSyncSocket( | |
256 base::SyncSocket::UnwrapHandle(descriptor))); | |
257 | |
258 audio_source_provider_.reset(new SpeechRecognitionAudioSourceProvider( | |
259 audio_track_, params, memory, socket.release(), | |
260 base::Bind(&SpeechRecognitionDispatcher::OnAudioTrackStopped, | |
261 base::Unretained(this)))); | |
262 } | |
214 | 263 |
215 int SpeechRecognitionDispatcher::GetOrCreateIDForHandle( | 264 int SpeechRecognitionDispatcher::GetOrCreateIDForHandle( |
216 const WebSpeechRecognitionHandle& handle) { | 265 const WebSpeechRecognitionHandle& handle) { |
217 // Search first for an existing mapping. | 266 // Search first for an existing mapping. |
218 for (HandleMap::iterator iter = handle_map_.begin(); | 267 for (HandleMap::iterator iter = handle_map_.begin(); |
219 iter != handle_map_.end(); | 268 iter != handle_map_.end(); |
220 ++iter) { | 269 ++iter) { |
221 if (iter->second.equals(handle)) | 270 if (iter->second.equals(handle)) |
222 return iter->first; | 271 return iter->first; |
223 } | 272 } |
224 // If no existing mapping found, create a new one. | 273 // If no existing mapping found, create a new one. |
225 const int new_id = next_id_; | 274 const int new_id = next_id_; |
226 handle_map_[new_id] = handle; | 275 handle_map_[new_id] = handle; |
227 ++next_id_; | 276 ++next_id_; |
228 return new_id; | 277 return new_id; |
229 } | 278 } |
230 | 279 |
231 bool SpeechRecognitionDispatcher::HandleExists( | 280 bool SpeechRecognitionDispatcher::HandleExists( |
232 const WebSpeechRecognitionHandle& handle) { | 281 const WebSpeechRecognitionHandle& handle) { |
233 for (HandleMap::iterator iter = handle_map_.begin(); | 282 for (HandleMap::iterator iter = handle_map_.begin(); |
234 iter != handle_map_.end(); | 283 iter != handle_map_.end(); |
235 ++iter) { | 284 ++iter) { |
236 if (iter->second.equals(handle)) | 285 if (iter->second.equals(handle)) |
237 return true; | 286 return true; |
238 } | 287 } |
239 return false; | 288 return false; |
240 } | 289 } |
241 | 290 |
291 void SpeechRecognitionDispatcher::OnAudioTrackStopped() { | |
292 audio_track_.reset(); | |
293 } | |
294 | |
242 const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID( | 295 const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID( |
243 int request_id) { | 296 int request_id) { |
244 HandleMap::iterator iter = handle_map_.find(request_id); | 297 HandleMap::iterator iter = handle_map_.find(request_id); |
245 DCHECK(iter != handle_map_.end()); | 298 DCHECK(iter != handle_map_.end()); |
246 return iter->second; | 299 return iter->second; |
247 } | 300 } |
248 | 301 |
249 } // namespace content | 302 } // namespace content |
OLD | NEW |