| 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_frame_impl.h" |
| 11 #include "third_party/WebKit/public/platform/WebString.h" | 11 #include "third_party/WebKit/public/platform/WebString.h" |
| 12 #include "third_party/WebKit/public/platform/WebVector.h" | 12 #include "third_party/WebKit/public/platform/WebVector.h" |
| 13 #include "third_party/WebKit/public/web/WebSpeechGrammar.h" | 13 #include "third_party/WebKit/public/web/WebSpeechGrammar.h" |
| 14 #include "third_party/WebKit/public/web/WebSpeechRecognitionParams.h" | 14 #include "third_party/WebKit/public/web/WebSpeechRecognitionParams.h" |
| 15 #include "third_party/WebKit/public/web/WebSpeechRecognitionResult.h" | 15 #include "third_party/WebKit/public/web/WebSpeechRecognitionResult.h" |
| 16 #include "third_party/WebKit/public/web/WebSpeechRecognizerClient.h" | 16 #include "third_party/WebKit/public/web/WebSpeechRecognizerClient.h" |
| 17 | 17 |
| 18 #if defined(ENABLE_WEBRTC) | 18 #if defined(ENABLE_WEBRTC) |
| 19 #include "content/renderer/media/speech_recognition_audio_sink.h" | 19 #include "content/renderer/media/speech_recognition_audio_sink.h" |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 using blink::WebVector; | 22 using blink::WebVector; |
| 23 using blink::WebString; | 23 using blink::WebString; |
| 24 using blink::WebSpeechGrammar; | 24 using blink::WebSpeechGrammar; |
| 25 using blink::WebSpeechRecognitionHandle; | 25 using blink::WebSpeechRecognitionHandle; |
| 26 using blink::WebSpeechRecognitionResult; | 26 using blink::WebSpeechRecognitionResult; |
| 27 using blink::WebSpeechRecognitionParams; | 27 using blink::WebSpeechRecognitionParams; |
| 28 using blink::WebSpeechRecognizerClient; | 28 using blink::WebSpeechRecognizerClient; |
| 29 | 29 |
| 30 namespace content { | 30 namespace content { |
| 31 | 31 |
| 32 SpeechRecognitionDispatcher::SpeechRecognitionDispatcher( | 32 SpeechRecognitionDispatcher::SpeechRecognitionDispatcher( |
| 33 RenderViewImpl* render_view) | 33 RenderFrame* render_frame) |
| 34 : RenderViewObserver(render_view), | 34 : RenderFrameObserver(render_frame), |
| 35 recognizer_client_(NULL), | 35 recognizer_client_(NULL), |
| 36 next_id_(1) {} | 36 next_id_(1) {} |
| 37 | 37 |
| 38 SpeechRecognitionDispatcher::~SpeechRecognitionDispatcher() {} | 38 SpeechRecognitionDispatcher::~SpeechRecognitionDispatcher() {} |
| 39 | 39 |
| 40 void SpeechRecognitionDispatcher::AbortAllRecognitions() { | |
| 41 ResetAudioSink(); | |
| 42 Send(new SpeechRecognitionHostMsg_AbortAllRequests( | |
| 43 routing_id())); | |
| 44 } | |
| 45 | |
| 46 bool SpeechRecognitionDispatcher::OnMessageReceived( | 40 bool SpeechRecognitionDispatcher::OnMessageReceived( |
| 47 const IPC::Message& message) { | 41 const IPC::Message& message) { |
| 48 bool handled = true; | 42 bool handled = true; |
| 49 IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcher, message) | 43 IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcher, message) |
| 50 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Started, OnRecognitionStarted) | 44 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Started, OnRecognitionStarted) |
| 51 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioStarted, OnAudioStarted) | 45 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioStarted, OnAudioStarted) |
| 52 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundStarted, OnSoundStarted) | 46 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundStarted, OnSoundStarted) |
| 53 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundEnded, OnSoundEnded) | 47 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundEnded, OnSoundEnded) |
| 54 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded) | 48 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded) |
| 55 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred, OnErrorOccurred) | 49 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred, OnErrorOccurred) |
| 56 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded) | 50 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded) |
| 57 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, | 51 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, |
| 58 OnResultsRetrieved) | 52 OnResultsRetrieved) |
| 59 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioReceiverReady, | 53 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioReceiverReady, |
| 60 OnAudioReceiverReady) | 54 OnAudioReceiverReady) |
| 61 IPC_MESSAGE_UNHANDLED(handled = false) | 55 IPC_MESSAGE_UNHANDLED(handled = false) |
| 62 IPC_END_MESSAGE_MAP() | 56 IPC_END_MESSAGE_MAP() |
| 63 return handled; | 57 return handled; |
| 64 } | 58 } |
| 65 | 59 |
| 60 void SpeechRecognitionDispatcher::WasHidden() { |
| 61 #if defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
| 62 ResetAudioSink(); |
| 63 Send(new SpeechRecognitionHostMsg_AbortAllRequests(routing_id())); |
| 64 #endif |
| 65 } |
| 66 |
| 66 void SpeechRecognitionDispatcher::start( | 67 void SpeechRecognitionDispatcher::start( |
| 67 const WebSpeechRecognitionHandle& handle, | 68 const WebSpeechRecognitionHandle& handle, |
| 68 const WebSpeechRecognitionParams& params, | 69 const WebSpeechRecognitionParams& params, |
| 69 WebSpeechRecognizerClient* recognizer_client) { | 70 WebSpeechRecognizerClient* recognizer_client) { |
| 70 DCHECK(!recognizer_client_ || recognizer_client_ == recognizer_client); | 71 DCHECK(!recognizer_client_ || recognizer_client_ == recognizer_client); |
| 71 recognizer_client_ = recognizer_client; | 72 recognizer_client_ = recognizer_client; |
| 72 | 73 |
| 73 #if defined(ENABLE_WEBRTC) | 74 #if defined(ENABLE_WEBRTC) |
| 74 const blink::WebMediaStreamTrack track = params.audioTrack(); | 75 const blink::WebMediaStreamTrack track = params.audioTrack(); |
| 75 if (!track.isNull()) { | 76 if (!track.isNull()) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 97 for (size_t i = 0; i < params.grammars().size(); ++i) { | 98 for (size_t i = 0; i < params.grammars().size(); ++i) { |
| 98 const WebSpeechGrammar& grammar = params.grammars()[i]; | 99 const WebSpeechGrammar& grammar = params.grammars()[i]; |
| 99 msg_params.grammars.push_back( | 100 msg_params.grammars.push_back( |
| 100 SpeechRecognitionGrammar(grammar.src().spec(), grammar.weight())); | 101 SpeechRecognitionGrammar(grammar.src().spec(), grammar.weight())); |
| 101 } | 102 } |
| 102 msg_params.language = base::UTF16ToUTF8(params.language()); | 103 msg_params.language = base::UTF16ToUTF8(params.language()); |
| 103 msg_params.max_hypotheses = static_cast<uint32>(params.maxAlternatives()); | 104 msg_params.max_hypotheses = static_cast<uint32>(params.maxAlternatives()); |
| 104 msg_params.continuous = params.continuous(); | 105 msg_params.continuous = params.continuous(); |
| 105 msg_params.interim_results = params.interimResults(); | 106 msg_params.interim_results = params.interimResults(); |
| 106 msg_params.origin_url = params.origin().toString().utf8(); | 107 msg_params.origin_url = params.origin().toString().utf8(); |
| 107 msg_params.render_view_id = routing_id(); | |
| 108 msg_params.request_id = GetOrCreateIDForHandle(handle); | 108 msg_params.request_id = GetOrCreateIDForHandle(handle); |
| 109 #if defined(ENABLE_WEBRTC) | 109 #if defined(ENABLE_WEBRTC) |
| 110 // Fall back to default input when the track is not allowed. | 110 // Fall back to default input when the track is not allowed. |
| 111 msg_params.using_audio_track = !audio_track_.isNull(); | 111 msg_params.using_audio_track = !audio_track_.isNull(); |
| 112 #else | 112 #else |
| 113 msg_params.using_audio_track = false; | 113 msg_params.using_audio_track = false; |
| 114 #endif | 114 #endif |
| 115 msg_params.render_frame_id = routing_id(); |
| 115 // The handle mapping will be removed in |OnRecognitionEnd|. | 116 // The handle mapping will be removed in |OnRecognitionEnd|. |
| 116 Send(new SpeechRecognitionHostMsg_StartRequest(msg_params)); | 117 Send(new SpeechRecognitionHostMsg_StartRequest(msg_params)); |
| 117 } | 118 } |
| 118 | 119 |
| 119 void SpeechRecognitionDispatcher::stop( | 120 void SpeechRecognitionDispatcher::stop( |
| 120 const WebSpeechRecognitionHandle& handle, | 121 const WebSpeechRecognitionHandle& handle, |
| 121 WebSpeechRecognizerClient* recognizer_client) { | 122 WebSpeechRecognizerClient* recognizer_client) { |
| 122 ResetAudioSink(); | 123 ResetAudioSink(); |
| 123 // Ignore a |stop| issued without a matching |start|. | 124 // Ignore a |stop| issued without a matching |start|. |
| 124 if (recognizer_client_ != recognizer_client || !HandleExists(handle)) | 125 if (recognizer_client_ != recognizer_client || !HandleExists(handle)) |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 } | 308 } |
| 308 | 309 |
| 309 const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID( | 310 const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID( |
| 310 int request_id) { | 311 int request_id) { |
| 311 HandleMap::iterator iter = handle_map_.find(request_id); | 312 HandleMap::iterator iter = handle_map_.find(request_id); |
| 312 DCHECK(iter != handle_map_.end()); | 313 DCHECK(iter != handle_map_.end()); |
| 313 return iter->second; | 314 return iter->second; |
| 314 } | 315 } |
| 315 | 316 |
| 316 } // namespace content | 317 } // namespace content |
| OLD | NEW |