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/browser/speech/speech_recognition_manager_impl.h" | 5 #include "content/browser/speech/speech_recognition_manager_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "content/browser/browser_main_loop.h" | 8 #include "content/browser/browser_main_loop.h" |
9 #include "content/browser/renderer_host/media/media_stream_manager.h" | 9 #include "content/browser/renderer_host/media/media_stream_manager.h" |
10 #include "content/browser/speech/google_one_shot_remote_engine.h" | 10 #include "content/browser/speech/google_one_shot_remote_engine.h" |
11 #include "content/browser/speech/google_streaming_remote_engine.h" | 11 #include "content/browser/speech/google_streaming_remote_engine.h" |
12 #include "content/browser/speech/speech_recognition_engine.h" | 12 #include "content/browser/speech/speech_recognition_engine.h" |
13 #include "content/browser/speech/speech_recognizer.h" | 13 #include "content/browser/speech/speech_recognizer.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/content_browser_client.h" | 15 #include "content/public/browser/content_browser_client.h" |
16 #include "content/public/browser/resource_context.h" | 16 #include "content/public/browser/resource_context.h" |
17 #include "content/public/browser/speech_recognition_event_listener.h" | 17 #include "content/public/browser/speech_recognition_event_listener.h" |
18 #include "content/public/browser/speech_recognition_manager_delegate.h" | 18 #include "content/public/browser/speech_recognition_manager_delegate.h" |
19 #include "content/public/browser/speech_recognition_session_config.h" | 19 #include "content/public/browser/speech_recognition_session_config.h" |
20 #include "content/public/browser/speech_recognition_session_context.h" | 20 #include "content/public/browser/speech_recognition_session_context.h" |
21 #include "content/public/common/speech_recognition_error.h" | 21 #include "content/public/common/speech_recognition_error.h" |
22 #include "content/public/common/speech_recognition_result.h" | 22 #include "content/public/common/speech_recognition_result.h" |
23 #include "media/audio/audio_manager.h" | 23 #include "media/audio/audio_manager.h" |
24 | 24 |
25 using base::Callback; | 25 using base::Callback; |
26 using content::BrowserMainLoop; | |
27 using content::BrowserThread; | |
28 using content::SpeechRecognitionEventListener; | |
29 using content::SpeechRecognitionManager; | |
30 using content::SpeechRecognitionResult; | |
31 using content::SpeechRecognitionSessionContext; | |
32 using content::SpeechRecognitionSessionConfig; | |
33 | 26 |
34 namespace content { | 27 namespace content { |
35 SpeechRecognitionManager* SpeechRecognitionManager::GetInstance() { | |
36 return speech::SpeechRecognitionManagerImpl::GetInstance(); | |
37 } | |
38 } // namespace content | |
39 | 28 |
40 namespace { | 29 namespace { |
41 speech::SpeechRecognitionManagerImpl* g_speech_recognition_manager_impl; | 30 |
| 31 SpeechRecognitionManagerImpl* g_speech_recognition_manager_impl; |
42 | 32 |
43 void ShowAudioInputSettingsOnFileThread() { | 33 void ShowAudioInputSettingsOnFileThread() { |
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
45 media::AudioManager* audio_manager = BrowserMainLoop::GetAudioManager(); | 35 media::AudioManager* audio_manager = BrowserMainLoop::GetAudioManager(); |
46 DCHECK(audio_manager->CanShowAudioInputSettings()); | 36 DCHECK(audio_manager->CanShowAudioInputSettings()); |
47 if (audio_manager->CanShowAudioInputSettings()) | 37 if (audio_manager->CanShowAudioInputSettings()) |
48 audio_manager->ShowAudioInputSettings(); | 38 audio_manager->ShowAudioInputSettings(); |
49 } | 39 } |
| 40 |
50 } // namespace | 41 } // namespace |
51 | 42 |
52 namespace speech { | 43 SpeechRecognitionManager* SpeechRecognitionManager::GetInstance() { |
| 44 return SpeechRecognitionManagerImpl::GetInstance(); |
| 45 } |
53 | 46 |
54 #if !defined(OS_IOS) | 47 #if !defined(OS_IOS) |
55 class SpeechRecognitionManagerImpl::PermissionRequest | 48 class SpeechRecognitionManagerImpl::PermissionRequest |
56 : public media_stream::MediaStreamRequester { | 49 : public media_stream::MediaStreamRequester { |
57 public: | 50 public: |
58 PermissionRequest(int session_id, | 51 PermissionRequest(int session_id, |
59 const base::Callback<void(bool is_allowed)>& callback) | 52 const base::Callback<void(bool is_allowed)>& callback) |
60 : session_id_(session_id), | 53 : session_id_(session_id), |
61 callback_(callback), | 54 callback_(callback), |
62 started_(false) { | 55 started_(false) { |
63 } | 56 } |
64 | 57 |
65 virtual ~PermissionRequest() { | 58 virtual ~PermissionRequest() { |
66 if (started_) | 59 if (started_) |
67 Abort(); | 60 Abort(); |
68 } | 61 } |
69 | 62 |
70 void Start(int render_process_id, int render_view_id, const GURL& origin) { | 63 void Start(int render_process_id, int render_view_id, const GURL& origin) { |
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
72 started_ = true; | 65 started_ = true; |
73 BrowserMainLoop::GetMediaStreamManager()->GenerateStream( | 66 BrowserMainLoop::GetMediaStreamManager()->GenerateStream( |
74 this, | 67 this, |
75 render_process_id, | 68 render_process_id, |
76 render_view_id, | 69 render_view_id, |
77 media_stream::StreamOptions(content::MEDIA_DEVICE_AUDIO_CAPTURE, | 70 media_stream::StreamOptions(MEDIA_DEVICE_AUDIO_CAPTURE, |
78 content::MEDIA_DEVICE_VIDEO_CAPTURE), | 71 MEDIA_DEVICE_VIDEO_CAPTURE), |
79 origin, | 72 origin, |
80 &label_); | 73 &label_); |
81 } | 74 } |
82 | 75 |
83 void Abort() { | 76 void Abort() { |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
85 started_ = false; | 78 started_ = false; |
86 BrowserMainLoop::GetMediaStreamManager()->CancelRequest(label_); | 79 BrowserMainLoop::GetMediaStreamManager()->CancelRequest(label_); |
87 } | 80 } |
88 | 81 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 #endif // !defined(OS_IOS) | 122 #endif // !defined(OS_IOS) |
130 | 123 |
131 SpeechRecognitionManagerImpl* SpeechRecognitionManagerImpl::GetInstance() { | 124 SpeechRecognitionManagerImpl* SpeechRecognitionManagerImpl::GetInstance() { |
132 return g_speech_recognition_manager_impl; | 125 return g_speech_recognition_manager_impl; |
133 } | 126 } |
134 | 127 |
135 SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl() | 128 SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl() |
136 : primary_session_id_(kSessionIDInvalid), | 129 : primary_session_id_(kSessionIDInvalid), |
137 last_session_id_(kSessionIDInvalid), | 130 last_session_id_(kSessionIDInvalid), |
138 is_dispatching_event_(false), | 131 is_dispatching_event_(false), |
139 delegate_(content::GetContentClient()->browser()-> | 132 delegate_(GetContentClient()->browser()-> |
140 GetSpeechRecognitionManagerDelegate()), | 133 GetSpeechRecognitionManagerDelegate()), |
141 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 134 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
142 DCHECK(!g_speech_recognition_manager_impl); | 135 DCHECK(!g_speech_recognition_manager_impl); |
143 g_speech_recognition_manager_impl = this; | 136 g_speech_recognition_manager_impl = this; |
144 } | 137 } |
145 | 138 |
146 SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() { | 139 SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() { |
147 DCHECK(g_speech_recognition_manager_impl); | 140 DCHECK(g_speech_recognition_manager_impl); |
148 g_speech_recognition_manager_impl = NULL; | 141 g_speech_recognition_manager_impl = NULL; |
149 // Recognition sessions will be aborted by the corresponding destructors. | 142 // Recognition sessions will be aborted by the corresponding destructors. |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 return; | 247 return; |
255 } | 248 } |
256 permission_request_.reset(); | 249 permission_request_.reset(); |
257 #endif // defined(OS_IOS) | 250 #endif // defined(OS_IOS) |
258 | 251 |
259 if (is_allowed) { | 252 if (is_allowed) { |
260 MessageLoop::current()->PostTask(FROM_HERE, | 253 MessageLoop::current()->PostTask(FROM_HERE, |
261 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 254 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
262 weak_factory_.GetWeakPtr(), session_id, EVENT_START)); | 255 weak_factory_.GetWeakPtr(), session_id, EVENT_START)); |
263 } else { | 256 } else { |
264 OnRecognitionError(session_id, content::SpeechRecognitionError( | 257 OnRecognitionError(session_id, SpeechRecognitionError( |
265 content::SPEECH_RECOGNITION_ERROR_NOT_ALLOWED)); | 258 SPEECH_RECOGNITION_ERROR_NOT_ALLOWED)); |
266 MessageLoop::current()->PostTask(FROM_HERE, | 259 MessageLoop::current()->PostTask(FROM_HERE, |
267 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 260 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
268 weak_factory_.GetWeakPtr(), session_id, EVENT_ABORT)); | 261 weak_factory_.GetWeakPtr(), session_id, EVENT_ABORT)); |
269 } | 262 } |
270 } | 263 } |
271 | 264 |
272 void SpeechRecognitionManagerImpl::AbortSession(int session_id) { | 265 void SpeechRecognitionManagerImpl::AbortSession(int session_id) { |
273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
274 if (!SessionExists(session_id)) | 267 if (!SessionExists(session_id)) |
275 return; | 268 return; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) | 371 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) |
379 delegate_listener->OnAudioEnd(session_id); | 372 delegate_listener->OnAudioEnd(session_id); |
380 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) | 373 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) |
381 listener->OnAudioEnd(session_id); | 374 listener->OnAudioEnd(session_id); |
382 MessageLoop::current()->PostTask(FROM_HERE, | 375 MessageLoop::current()->PostTask(FROM_HERE, |
383 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 376 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
384 weak_factory_.GetWeakPtr(), session_id, EVENT_AUDIO_ENDED)); | 377 weak_factory_.GetWeakPtr(), session_id, EVENT_AUDIO_ENDED)); |
385 } | 378 } |
386 | 379 |
387 void SpeechRecognitionManagerImpl::OnRecognitionResult( | 380 void SpeechRecognitionManagerImpl::OnRecognitionResult( |
388 int session_id, const content::SpeechRecognitionResult& result) { | 381 int session_id, const SpeechRecognitionResult& result) { |
389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
390 if (!SessionExists(session_id)) | 383 if (!SessionExists(session_id)) |
391 return; | 384 return; |
392 | 385 |
393 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) | 386 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) |
394 delegate_listener->OnRecognitionResult(session_id, result); | 387 delegate_listener->OnRecognitionResult(session_id, result); |
395 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) | 388 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) |
396 listener->OnRecognitionResult(session_id, result); | 389 listener->OnRecognitionResult(session_id, result); |
397 } | 390 } |
398 | 391 |
399 void SpeechRecognitionManagerImpl::OnRecognitionError( | 392 void SpeechRecognitionManagerImpl::OnRecognitionError( |
400 int session_id, const content::SpeechRecognitionError& error) { | 393 int session_id, const SpeechRecognitionError& error) { |
401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 394 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
402 if (!SessionExists(session_id)) | 395 if (!SessionExists(session_id)) |
403 return; | 396 return; |
404 | 397 |
405 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) | 398 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) |
406 delegate_listener->OnRecognitionError(session_id, error); | 399 delegate_listener->OnRecognitionError(session_id, error); |
407 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) | 400 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) |
408 listener->OnRecognitionError(session_id, error); | 401 listener->OnRecognitionError(session_id, error); |
409 } | 402 } |
410 | 403 |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 } | 672 } |
680 | 673 |
681 SpeechRecognitionManagerImpl::Session::Session() | 674 SpeechRecognitionManagerImpl::Session::Session() |
682 : id(kSessionIDInvalid), | 675 : id(kSessionIDInvalid), |
683 listener_is_active(true) { | 676 listener_is_active(true) { |
684 } | 677 } |
685 | 678 |
686 SpeechRecognitionManagerImpl::Session::~Session() { | 679 SpeechRecognitionManagerImpl::Session::~Session() { |
687 } | 680 } |
688 | 681 |
689 } // namespace speech | 682 } // namespace content |
OLD | NEW |