blob: b510ae80ed6a3f0e31466b62cf38dcdf345daed2 [file] [log] [blame]
[email protected]64d09222012-05-25 10:10:341// 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.
4
5#include "content/renderer/speech_recognition_dispatcher.h"
6
7#include "base/basictypes.h"
[email protected]74ebfb12013-06-07 20:48:008#include "base/strings/utf_string_conversions.h"
[email protected]64d09222012-05-25 10:10:349#include "content/common/speech_recognition_messages.h"
10#include "content/renderer/render_view_impl.h"
[email protected]5c30b5e02013-05-30 03:46:0811#include "third_party/WebKit/public/platform/WebString.h"
12#include "third_party/WebKit/public/platform/WebVector.h"
[email protected]2255a9332013-06-17 05:12:3113#include "third_party/WebKit/public/web/WebSpeechGrammar.h"
14#include "third_party/WebKit/public/web/WebSpeechRecognitionParams.h"
15#include "third_party/WebKit/public/web/WebSpeechRecognitionResult.h"
16#include "third_party/WebKit/public/web/WebSpeechRecognizerClient.h"
[email protected]64d09222012-05-25 10:10:3417
burnik2eeb4662014-10-09 21:30:1618#if defined(ENABLE_WEBRTC)
19#include "content/renderer/media/speech_recognition_audio_sink.h"
20#endif
21
[email protected]180ef242013-11-07 06:50:4622using blink::WebVector;
23using blink::WebString;
24using blink::WebSpeechGrammar;
25using blink::WebSpeechRecognitionHandle;
26using blink::WebSpeechRecognitionResult;
27using blink::WebSpeechRecognitionParams;
28using blink::WebSpeechRecognizerClient;
[email protected]64d09222012-05-25 10:10:3429
[email protected]e9ff79c2012-10-19 21:31:2630namespace content {
31
[email protected]64d09222012-05-25 10:10:3432SpeechRecognitionDispatcher::SpeechRecognitionDispatcher(
33 RenderViewImpl* render_view)
[email protected]e9ff79c2012-10-19 21:31:2634 : RenderViewObserver(render_view),
[email protected]64d09222012-05-25 10:10:3435 recognizer_client_(NULL),
burnik2eeb4662014-10-09 21:30:1636 next_id_(1) {}
[email protected]64d09222012-05-25 10:10:3437
burnik2eeb4662014-10-09 21:30:1638SpeechRecognitionDispatcher::~SpeechRecognitionDispatcher() {}
[email protected]64d09222012-05-25 10:10:3439
[email protected]e976c3c52014-07-24 17:41:5540void SpeechRecognitionDispatcher::AbortAllRecognitions() {
burnik2eeb4662014-10-09 21:30:1641 ResetAudioSink();
[email protected]e976c3c52014-07-24 17:41:5542 Send(new SpeechRecognitionHostMsg_AbortAllRequests(
43 routing_id()));
44}
45
[email protected]64d09222012-05-25 10:10:3446bool SpeechRecognitionDispatcher::OnMessageReceived(
47 const IPC::Message& message) {
48 bool handled = true;
49 IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcher, message)
50 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Started, OnRecognitionStarted)
51 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioStarted, OnAudioStarted)
52 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundStarted, OnSoundStarted)
53 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundEnded, OnSoundEnded)
54 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded)
55 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred, OnErrorOccurred)
56 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded)
[email protected]fc88c1e2012-12-04 09:54:3657 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved,
58 OnResultsRetrieved)
burnik2eeb4662014-10-09 21:30:1659 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioReceiverReady,
60 OnAudioReceiverReady)
[email protected]64d09222012-05-25 10:10:3461 IPC_MESSAGE_UNHANDLED(handled = false)
62 IPC_END_MESSAGE_MAP()
63 return handled;
64}
65
66void SpeechRecognitionDispatcher::start(
67 const WebSpeechRecognitionHandle& handle,
68 const WebSpeechRecognitionParams& params,
69 WebSpeechRecognizerClient* recognizer_client) {
[email protected]64d09222012-05-25 10:10:3470 DCHECK(!recognizer_client_ || recognizer_client_ == recognizer_client);
71 recognizer_client_ = recognizer_client;
72
burnik2eeb4662014-10-09 21:30:1673#if defined(ENABLE_WEBRTC)
74 const blink::WebMediaStreamTrack track = params.audioTrack();
75 if (!track.isNull()) {
76 // Check if this type of track is allowed by implemented policy.
77 if (SpeechRecognitionAudioSink::IsSupportedTrack(track)) {
78 audio_track_.assign(track);
79 } else {
80 audio_track_.reset();
81 // Notify user that the track used is not supported.
82 recognizer_client_->didReceiveError(
83 handle,
84 WebString("Provided audioTrack is not supported."),
85 WebSpeechRecognizerClient::AudioCaptureError);
86
87 return;
88 }
89 }
90
91 // Destroy any previous instance to detach from the audio track.
92 // Each new session should reinstantiate the provider once the track is ready.
93 ResetAudioSink();
94#endif
95
[email protected]64d09222012-05-25 10:10:3496 SpeechRecognitionHostMsg_StartRequest_Params msg_params;
97 for (size_t i = 0; i < params.grammars().size(); ++i) {
98 const WebSpeechGrammar& grammar = params.grammars()[i];
99 msg_params.grammars.push_back(
[email protected]e9ff79c2012-10-19 21:31:26100 SpeechRecognitionGrammar(grammar.src().spec(), grammar.weight()));
[email protected]64d09222012-05-25 10:10:34101 }
[email protected]32956122013-12-25 07:29:24102 msg_params.language = base::UTF16ToUTF8(params.language());
[email protected]2b0f67f2012-06-27 17:48:54103 msg_params.max_hypotheses = static_cast<uint32>(params.maxAlternatives());
[email protected]60a63042012-10-04 19:48:43104 msg_params.continuous = params.continuous();
105 msg_params.interim_results = params.interimResults();
[email protected]831a2592012-06-15 16:07:54106 msg_params.origin_url = params.origin().toString().utf8();
[email protected]64d09222012-05-25 10:10:34107 msg_params.render_view_id = routing_id();
[email protected]c766aa92012-06-22 16:57:14108 msg_params.request_id = GetOrCreateIDForHandle(handle);
burnik2eeb4662014-10-09 21:30:16109#if defined(ENABLE_WEBRTC)
110 // Fall back to default input when the track is not allowed.
111 msg_params.using_audio_track = !audio_track_.isNull();
112#else
113 msg_params.using_audio_track = false;
114#endif
[email protected]c766aa92012-06-22 16:57:14115 // The handle mapping will be removed in |OnRecognitionEnd|.
[email protected]64d09222012-05-25 10:10:34116 Send(new SpeechRecognitionHostMsg_StartRequest(msg_params));
117}
118
119void SpeechRecognitionDispatcher::stop(
120 const WebSpeechRecognitionHandle& handle,
121 WebSpeechRecognizerClient* recognizer_client) {
burnik2eeb4662014-10-09 21:30:16122 ResetAudioSink();
[email protected]c766aa92012-06-22 16:57:14123 // Ignore a |stop| issued without a matching |start|.
124 if (recognizer_client_ != recognizer_client || !HandleExists(handle))
125 return;
126 Send(new SpeechRecognitionHostMsg_StopCaptureRequest(
127 routing_id(), GetOrCreateIDForHandle(handle)));
[email protected]64d09222012-05-25 10:10:34128}
129
130void SpeechRecognitionDispatcher::abort(
131 const WebSpeechRecognitionHandle& handle,
132 WebSpeechRecognizerClient* recognizer_client) {
burnik2eeb4662014-10-09 21:30:16133 ResetAudioSink();
[email protected]c766aa92012-06-22 16:57:14134 // Ignore an |abort| issued without a matching |start|.
135 if (recognizer_client_ != recognizer_client || !HandleExists(handle))
136 return;
137 Send(new SpeechRecognitionHostMsg_AbortRequest(
138 routing_id(), GetOrCreateIDForHandle(handle)));
[email protected]64d09222012-05-25 10:10:34139}
140
141void SpeechRecognitionDispatcher::OnRecognitionStarted(int request_id) {
142 recognizer_client_->didStart(GetHandleFromID(request_id));
143}
144
145void SpeechRecognitionDispatcher::OnAudioStarted(int request_id) {
146 recognizer_client_->didStartAudio(GetHandleFromID(request_id));
147}
148
149void SpeechRecognitionDispatcher::OnSoundStarted(int request_id) {
150 recognizer_client_->didStartSound(GetHandleFromID(request_id));
151}
152
153void SpeechRecognitionDispatcher::OnSoundEnded(int request_id) {
154 recognizer_client_->didEndSound(GetHandleFromID(request_id));
155}
156
157void SpeechRecognitionDispatcher::OnAudioEnded(int request_id) {
158 recognizer_client_->didEndAudio(GetHandleFromID(request_id));
159}
160
[email protected]9c4ca672012-08-02 11:32:45161static WebSpeechRecognizerClient::ErrorCode WebKitErrorCode(
[email protected]e9ff79c2012-10-19 21:31:26162 SpeechRecognitionErrorCode e) {
[email protected]9c4ca672012-08-02 11:32:45163 switch (e) {
[email protected]e9ff79c2012-10-19 21:31:26164 case SPEECH_RECOGNITION_ERROR_NONE:
[email protected]9c4ca672012-08-02 11:32:45165 NOTREACHED();
166 return WebSpeechRecognizerClient::OtherError;
djmix.kim99db7e02015-04-22 07:23:08167 case SPEECH_RECOGNITION_ERROR_NO_SPEECH:
168 return WebSpeechRecognizerClient::NoSpeechError;
[email protected]e9ff79c2012-10-19 21:31:26169 case SPEECH_RECOGNITION_ERROR_ABORTED:
[email protected]9c4ca672012-08-02 11:32:45170 return WebSpeechRecognizerClient::AbortedError;
[email protected]e9ff79c2012-10-19 21:31:26171 case SPEECH_RECOGNITION_ERROR_AUDIO:
[email protected]9c4ca672012-08-02 11:32:45172 return WebSpeechRecognizerClient::AudioCaptureError;
[email protected]e9ff79c2012-10-19 21:31:26173 case SPEECH_RECOGNITION_ERROR_NETWORK:
[email protected]9c4ca672012-08-02 11:32:45174 return WebSpeechRecognizerClient::NetworkError;
[email protected]e9ff79c2012-10-19 21:31:26175 case SPEECH_RECOGNITION_ERROR_NOT_ALLOWED:
[email protected]2af35c502012-09-13 20:14:43176 return WebSpeechRecognizerClient::NotAllowedError;
djmix.kim99db7e02015-04-22 07:23:08177 case SPEECH_RECOGNITION_ERROR_SERVICE_NOT_ALLOWED:
178 return WebSpeechRecognizerClient::ServiceNotAllowedError;
179 case SPEECH_RECOGNITION_ERROR_BAD_GRAMMAR:
180 return WebSpeechRecognizerClient::BadGrammarError;
181 case SPEECH_RECOGNITION_ERROR_LANGUAGE_NOT_SUPPORTED:
182 return WebSpeechRecognizerClient::LanguageNotSupportedError;
[email protected]e9ff79c2012-10-19 21:31:26183 case SPEECH_RECOGNITION_ERROR_NO_MATCH:
[email protected]9c4ca672012-08-02 11:32:45184 NOTREACHED();
185 return WebSpeechRecognizerClient::OtherError;
[email protected]9c4ca672012-08-02 11:32:45186 }
187 NOTREACHED();
188 return WebSpeechRecognizerClient::OtherError;
189}
190
[email protected]64d09222012-05-25 10:10:34191void SpeechRecognitionDispatcher::OnErrorOccurred(
192 int request_id, const SpeechRecognitionError& error) {
[email protected]e9ff79c2012-10-19 21:31:26193 if (error.code == SPEECH_RECOGNITION_ERROR_NO_MATCH) {
[email protected]64d09222012-05-25 10:10:34194 recognizer_client_->didReceiveNoMatch(GetHandleFromID(request_id),
195 WebSpeechRecognitionResult());
196 } else {
burnik2eeb4662014-10-09 21:30:16197 ResetAudioSink();
[email protected]0a8d4275e2013-01-04 22:21:26198 recognizer_client_->didReceiveError(
199 GetHandleFromID(request_id),
200 WebString(), // TODO(primiano): message?
201 WebKitErrorCode(error.code));
[email protected]64d09222012-05-25 10:10:34202 }
203}
204
205void SpeechRecognitionDispatcher::OnRecognitionEnded(int request_id) {
[email protected]fc88c1e2012-12-04 09:54:36206 // TODO(tommi): It is possible that the handle isn't found in the array if
207 // the user just refreshed the page. It seems that we then get a notification
208 // for the previously loaded instance of the page.
209 HandleMap::iterator iter = handle_map_.find(request_id);
210 if (iter == handle_map_.end()) {
211 DLOG(ERROR) << "OnRecognitionEnded called for a handle that doesn't exist";
212 } else {
213 WebSpeechRecognitionHandle handle = iter->second;
214 // Note: we need to erase the handle from the map *before* calling didEnd.
215 // didEnd may call back synchronously to start a new recognition session,
216 // and we don't want to delete the handle from the map after that happens.
217 handle_map_.erase(request_id);
burnik2eeb4662014-10-09 21:30:16218 ResetAudioSink();
[email protected]fc88c1e2012-12-04 09:54:36219 recognizer_client_->didEnd(handle);
220 }
[email protected]64d09222012-05-25 10:10:34221}
222
[email protected]fc88c1e2012-12-04 09:54:36223void SpeechRecognitionDispatcher::OnResultsRetrieved(
224 int request_id, const SpeechRecognitionResults& results) {
225 size_t provisional_count = 0;
226 SpeechRecognitionResults::const_iterator it = results.begin();
227 for (; it != results.end(); ++it) {
228 if (it->is_provisional)
229 ++provisional_count;
[email protected]64d09222012-05-25 10:10:34230 }
[email protected]fc88c1e2012-12-04 09:54:36231
232 WebVector<WebSpeechRecognitionResult> provisional(provisional_count);
233 WebVector<WebSpeechRecognitionResult> final(
234 results.size() - provisional_count);
235
236 int provisional_index = 0, final_index = 0;
237 for (it = results.begin(); it != results.end(); ++it) {
238 const SpeechRecognitionResult& result = (*it);
239 WebSpeechRecognitionResult* webkit_result = result.is_provisional ?
240 &provisional[provisional_index++] : &final[final_index++];
241
242 const size_t num_hypotheses = result.hypotheses.size();
243 WebVector<WebString> transcripts(num_hypotheses);
244 WebVector<float> confidences(num_hypotheses);
245 for (size_t i = 0; i < num_hypotheses; ++i) {
246 transcripts[i] = result.hypotheses[i].utterance;
247 confidences[i] = static_cast<float>(result.hypotheses[i].confidence);
248 }
249 webkit_result->assign(transcripts, confidences, !result.is_provisional);
250 }
251
252 recognizer_client_->didReceiveResults(
253 GetHandleFromID(request_id), final, provisional);
[email protected]64d09222012-05-25 10:10:34254}
255
burnik2eeb4662014-10-09 21:30:16256void SpeechRecognitionDispatcher::OnAudioReceiverReady(
257 int request_id,
258 const media::AudioParameters& params,
259 const base::SharedMemoryHandle memory,
260 const base::SyncSocket::TransitDescriptor descriptor) {
261#if defined(ENABLE_WEBRTC)
262 DCHECK(!speech_audio_sink_.get());
263 if (audio_track_.isNull()) {
264 ResetAudioSink();
265 return;
266 }
267
268 // The instantiation and type of SyncSocket is up to the client since it
269 // is dependency injected to the SpeechRecognitionAudioSink.
270 scoped_ptr<base::SyncSocket> socket(new base::CancelableSyncSocket(
271 base::SyncSocket::UnwrapHandle(descriptor)));
272
273 speech_audio_sink_.reset(new SpeechRecognitionAudioSink(
274 audio_track_, params, memory, socket.Pass(),
275 base::Bind(&SpeechRecognitionDispatcher::ResetAudioSink,
276 base::Unretained(this))));
277#endif
278}
[email protected]c766aa92012-06-22 16:57:14279
280int SpeechRecognitionDispatcher::GetOrCreateIDForHandle(
[email protected]64d09222012-05-25 10:10:34281 const WebSpeechRecognitionHandle& handle) {
282 // Search first for an existing mapping.
283 for (HandleMap::iterator iter = handle_map_.begin();
284 iter != handle_map_.end();
285 ++iter) {
286 if (iter->second.equals(handle))
287 return iter->first;
288 }
289 // If no existing mapping found, create a new one.
290 const int new_id = next_id_;
291 handle_map_[new_id] = handle;
292 ++next_id_;
293 return new_id;
294}
295
[email protected]c766aa92012-06-22 16:57:14296bool SpeechRecognitionDispatcher::HandleExists(
297 const WebSpeechRecognitionHandle& handle) {
298 for (HandleMap::iterator iter = handle_map_.begin();
299 iter != handle_map_.end();
300 ++iter) {
301 if (iter->second.equals(handle))
302 return true;
303 }
304 return false;
305}
306
burnik2eeb4662014-10-09 21:30:16307void SpeechRecognitionDispatcher::ResetAudioSink() {
308#if defined(ENABLE_WEBRTC)
309 speech_audio_sink_.reset();
310#endif
311}
312
[email protected]64d09222012-05-25 10:10:34313const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID(
314 int request_id) {
315 HandleMap::iterator iter = handle_map_.find(request_id);
316 DCHECK(iter != handle_map_.end());
317 return iter->second;
318}
[email protected]e9ff79c2012-10-19 21:31:26319
320} // namespace content