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