| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/sync_socket.h" |
| 8 #include "content/browser/browser_main_loop.h" | 9 #include "content/browser/browser_main_loop.h" |
| 9 #include "content/browser/renderer_host/media/media_stream_manager.h" | 10 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 10 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | 11 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" |
| 11 #include "content/browser/speech/google_one_shot_remote_engine.h" | 12 #include "content/browser/speech/google_one_shot_remote_engine.h" |
| 12 #include "content/browser/speech/google_streaming_remote_engine.h" | 13 #include "content/browser/speech/google_streaming_remote_engine.h" |
| 13 #include "content/browser/speech/speech_recognition_engine.h" | 14 #include "content/browser/speech/speech_recognition_engine.h" |
| 14 #include "content/browser/speech/speech_recognizer_impl.h" | 15 #include "content/browser/speech/speech_recognizer_impl.h" |
| 15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/content_browser_client.h" | 17 #include "content/public/browser/content_browser_client.h" |
| 17 #include "content/public/browser/resource_context.h" | 18 #include "content/public/browser/resource_context.h" |
| 18 #include "content/public/browser/speech_recognition_event_listener.h" | 19 #include "content/public/browser/speech_recognition_event_listener.h" |
| 19 #include "content/public/browser/speech_recognition_manager_delegate.h" | 20 #include "content/public/browser/speech_recognition_manager_delegate.h" |
| 20 #include "content/public/browser/speech_recognition_session_config.h" | 21 #include "content/public/browser/speech_recognition_session_config.h" |
| 21 #include "content/public/browser/speech_recognition_session_context.h" | 22 #include "content/public/browser/speech_recognition_session_context.h" |
| 22 #include "content/public/common/speech_recognition_error.h" | 23 #include "content/public/common/speech_recognition_error.h" |
| 23 #include "content/public/common/speech_recognition_result.h" | 24 #include "content/public/common/speech_recognition_result.h" |
| 24 #include "media/audio/audio_manager.h" | 25 #include "media/audio/audio_manager.h" |
| 25 #include "media/audio/audio_manager_base.h" | 26 #include "media/audio/audio_manager_base.h" |
| 27 #include "media/audio/audio_parameters.h" |
| 26 | 28 |
| 27 #if defined(OS_ANDROID) | 29 #if defined(OS_ANDROID) |
| 28 #include "content/browser/speech/speech_recognizer_impl_android.h" | 30 #include "content/browser/speech/speech_recognizer_impl_android.h" |
| 29 #endif | 31 #endif |
| 30 | 32 |
| 31 using base::Callback; | 33 using base::Callback; |
| 32 | 34 |
| 33 namespace content { | 35 namespace content { |
| 34 | 36 |
| 35 SpeechRecognitionManager* SpeechRecognitionManager::manager_for_tests_; | 37 SpeechRecognitionManager* SpeechRecognitionManager::manager_for_tests_; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 config.url_request_context_getter.get()); | 144 config.url_request_context_getter.get()); |
| 143 } | 145 } |
| 144 | 146 |
| 145 google_remote_engine->SetConfig(remote_engine_config); | 147 google_remote_engine->SetConfig(remote_engine_config); |
| 146 | 148 |
| 147 session->recognizer = new SpeechRecognizerImpl( | 149 session->recognizer = new SpeechRecognizerImpl( |
| 148 this, | 150 this, |
| 149 session_id, | 151 session_id, |
| 150 config.continuous, | 152 config.continuous, |
| 151 config.interim_results, | 153 config.interim_results, |
| 154 config.using_audio_track, |
| 155 config.peer_process_handle, |
| 152 google_remote_engine); | 156 google_remote_engine); |
| 153 #else | 157 #else |
| 154 session->recognizer = new SpeechRecognizerImplAndroid(this, session_id); | 158 session->recognizer = new SpeechRecognizerImplAndroid(this, session_id); |
| 155 #endif | 159 #endif |
| 156 return session_id; | 160 return session_id; |
| 157 } | 161 } |
| 158 | 162 |
| 159 void SpeechRecognitionManagerImpl::StartSession(int session_id) { | 163 void SpeechRecognitionManagerImpl::StartSession(int session_id) { |
| 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 161 if (!SessionExists(session_id)) | 165 if (!SessionExists(session_id)) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 185 if (!SessionExists(session_id)) | 189 if (!SessionExists(session_id)) |
| 186 return; | 190 return; |
| 187 | 191 |
| 188 SessionsTable::iterator iter = sessions_.find(session_id); | 192 SessionsTable::iterator iter = sessions_.find(session_id); |
| 189 DCHECK(iter != sessions_.end()); | 193 DCHECK(iter != sessions_.end()); |
| 190 Session* session = iter->second; | 194 Session* session = iter->second; |
| 191 | 195 |
| 192 if (session->abort_requested) | 196 if (session->abort_requested) |
| 193 return; | 197 return; |
| 194 | 198 |
| 195 if (ask_user) { | 199 if (ask_user && !session->config.using_audio_track) { |
| 196 SpeechRecognitionSessionContext& context = session->context; | 200 SpeechRecognitionSessionContext& context = session->context; |
| 197 context.label = media_stream_manager_->MakeMediaAccessRequest( | 201 context.label = media_stream_manager_->MakeMediaAccessRequest( |
| 198 context.render_process_id, | 202 context.render_process_id, |
| 199 context.render_frame_id, | 203 context.render_frame_id, |
| 200 context.request_id, | 204 context.request_id, |
| 201 StreamOptions(true, false), | 205 StreamOptions(true, false), |
| 202 GURL(context.context_name), | 206 GURL(context.context_name), |
| 203 base::Bind( | 207 base::Bind( |
| 204 &SpeechRecognitionManagerImpl::MediaRequestPermissionCallback, | 208 &SpeechRecognitionManagerImpl::MediaRequestPermissionCallback, |
| 205 weak_factory_.GetWeakPtr(), session_id)); | 209 weak_factory_.GetWeakPtr(), session_id)); |
| 206 return; | 210 return; |
| 207 } | 211 } |
| 208 | 212 |
| 209 if (is_allowed) { | 213 if (is_allowed || session->config.using_audio_track) { |
| 210 base::MessageLoop::current()->PostTask( | 214 base::MessageLoop::current()->PostTask( |
| 211 FROM_HERE, | 215 FROM_HERE, |
| 212 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 216 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 213 weak_factory_.GetWeakPtr(), | 217 weak_factory_.GetWeakPtr(), |
| 214 session_id, | 218 session_id, |
| 215 EVENT_START)); | 219 EVENT_START)); |
| 216 } else { | 220 } else { |
| 217 OnRecognitionError(session_id, SpeechRecognitionError( | 221 OnRecognitionError(session_id, SpeechRecognitionError( |
| 218 SPEECH_RECOGNITION_ERROR_NOT_ALLOWED)); | 222 SPEECH_RECOGNITION_ERROR_NOT_ALLOWED)); |
| 219 base::MessageLoop::current()->PostTask( | 223 base::MessageLoop::current()->PostTask( |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) | 427 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) |
| 424 listener->OnRecognitionEnd(session_id); | 428 listener->OnRecognitionEnd(session_id); |
| 425 base::MessageLoop::current()->PostTask( | 429 base::MessageLoop::current()->PostTask( |
| 426 FROM_HERE, | 430 FROM_HERE, |
| 427 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 431 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 428 weak_factory_.GetWeakPtr(), | 432 weak_factory_.GetWeakPtr(), |
| 429 session_id, | 433 session_id, |
| 430 EVENT_RECOGNITION_ENDED)); | 434 EVENT_RECOGNITION_ENDED)); |
| 431 } | 435 } |
| 432 | 436 |
| 437 void SpeechRecognitionManagerImpl::OnAudioReceiverReady( |
| 438 int session_id, |
| 439 const media::AudioParameters& params, |
| 440 const base::SharedMemoryHandle memory, |
| 441 const base::SyncSocket::TransitDescriptor socket) { |
| 442 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 443 if (!SessionExists(session_id)) |
| 444 return; |
| 445 |
| 446 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) |
| 447 listener->OnAudioReceiverReady(session_id, params, memory, socket); |
| 448 } |
| 449 |
| 433 int SpeechRecognitionManagerImpl::GetSession( | 450 int SpeechRecognitionManagerImpl::GetSession( |
| 434 int render_process_id, int render_view_id, int request_id) const { | 451 int render_process_id, int render_view_id, int request_id) const { |
| 435 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 452 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 436 SessionsTable::const_iterator iter; | 453 SessionsTable::const_iterator iter; |
| 437 for(iter = sessions_.begin(); iter != sessions_.end(); ++iter) { | 454 for (iter = sessions_.begin(); iter != sessions_.end(); ++iter) { |
| 438 const int session_id = iter->first; | 455 const int session_id = iter->first; |
| 439 const SpeechRecognitionSessionContext& context = iter->second->context; | 456 const SpeechRecognitionSessionContext& context = iter->second->context; |
| 440 if (context.render_process_id == render_process_id && | 457 if (context.render_process_id == render_process_id && |
| 441 context.render_view_id == render_view_id && | 458 context.render_view_id == render_view_id && |
| 442 context.request_id == request_id) { | 459 context.request_id == request_id) { |
| 443 return session_id; | 460 return session_id; |
| 444 } | 461 } |
| 445 } | 462 } |
| 446 return kSessionIDInvalid; | 463 return kSessionIDInvalid; |
| 447 } | 464 } |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 } | 593 } |
| 577 | 594 |
| 578 // ----------- Contract for all the FSM evolution functions below ------------- | 595 // ----------- Contract for all the FSM evolution functions below ------------- |
| 579 // - Are guaranteed to be executed in the IO thread; | 596 // - Are guaranteed to be executed in the IO thread; |
| 580 // - Are guaranteed to be not reentrant (themselves and each other); | 597 // - Are guaranteed to be not reentrant (themselves and each other); |
| 581 | 598 |
| 582 void SpeechRecognitionManagerImpl::SessionStart(const Session& session) { | 599 void SpeechRecognitionManagerImpl::SessionStart(const Session& session) { |
| 583 DCHECK_EQ(primary_session_id_, session.id); | 600 DCHECK_EQ(primary_session_id_, session.id); |
| 584 const MediaStreamDevices& devices = session.context.devices; | 601 const MediaStreamDevices& devices = session.context.devices; |
| 585 std::string device_id; | 602 std::string device_id; |
| 586 if (devices.empty()) { | 603 if (!session.config.using_audio_track) { |
| 587 // From the ask_user=false path, use the default device. | 604 if (devices.empty()) { |
| 588 // TODO(xians): Abort the session after we do not need to support this path | 605 // From the ask_user=false path, use the default device. |
| 589 // anymore. | 606 // TODO(xians): Abort the session after we do not need to support this |
| 590 device_id = media::AudioManagerBase::kDefaultDeviceId; | 607 // path anymore. |
| 591 } else { | 608 device_id = media::AudioManagerBase::kDefaultDeviceId; |
| 592 // From the ask_user=true path, use the selected device. | 609 } else { |
| 593 DCHECK_EQ(1u, devices.size()); | 610 // From the ask_user=true path, use the selected device. |
| 594 DCHECK_EQ(MEDIA_DEVICE_AUDIO_CAPTURE, devices.front().type); | 611 DCHECK_EQ(1u, devices.size()); |
| 595 device_id = devices.front().id; | 612 DCHECK_EQ(MEDIA_DEVICE_AUDIO_CAPTURE, devices.front().type); |
| 613 device_id = devices.front().id; |
| 614 } |
| 596 } | 615 } |
| 597 | 616 |
| 598 session.recognizer->StartRecognition(device_id); | 617 session.recognizer->StartRecognition(device_id); |
| 599 } | 618 } |
| 600 | 619 |
| 601 void SpeechRecognitionManagerImpl::SessionAbort(const Session& session) { | 620 void SpeechRecognitionManagerImpl::SessionAbort(const Session& session) { |
| 602 if (primary_session_id_ == session.id) | 621 if (primary_session_id_ == session.id) |
| 603 primary_session_id_ = kSessionIDInvalid; | 622 primary_session_id_ = kSessionIDInvalid; |
| 604 DCHECK(session.recognizer.get()); | 623 DCHECK(session.recognizer.get()); |
| 605 session.recognizer->AbortRecognition(); | 624 session.recognizer->AbortRecognition(); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 SpeechRecognitionManagerImpl::Session::Session() | 710 SpeechRecognitionManagerImpl::Session::Session() |
| 692 : id(kSessionIDInvalid), | 711 : id(kSessionIDInvalid), |
| 693 abort_requested(false), | 712 abort_requested(false), |
| 694 listener_is_active(true) { | 713 listener_is_active(true) { |
| 695 } | 714 } |
| 696 | 715 |
| 697 SpeechRecognitionManagerImpl::Session::~Session() { | 716 SpeechRecognitionManagerImpl::Session::~Session() { |
| 698 } | 717 } |
| 699 | 718 |
| 700 } // namespace content | 719 } // namespace content |
| OLD | NEW |