diff options
-rw-r--r-- | content/browser/speech/speech_recognition_manager_impl.cc | 6 | ||||
-rw-r--r-- | content/browser/speech/speech_recognizer.h | 2 | ||||
-rw-r--r-- | content/browser/speech/speech_recognizer_impl.cc | 11 | ||||
-rw-r--r-- | content/browser/speech/speech_recognizer_impl.h | 3 | ||||
-rw-r--r-- | content/browser/speech/speech_recognizer_impl_android.cc | 4 | ||||
-rw-r--r-- | content/browser/speech/speech_recognizer_impl_android.h | 2 | ||||
-rw-r--r-- | content/browser/speech/speech_recognizer_impl_unittest.cc | 23 | ||||
-rw-r--r-- | media/audio/audio_input_controller.cc | 6 | ||||
-rw-r--r-- | media/audio/audio_input_controller.h | 5 | ||||
-rw-r--r-- | media/audio/audio_input_controller_unittest.cc | 16 |
10 files changed, 49 insertions, 29 deletions
diff --git a/content/browser/speech/speech_recognition_manager_impl.cc b/content/browser/speech/speech_recognition_manager_impl.cc index dffd6f3..82aa13a 100644 --- a/content/browser/speech/speech_recognition_manager_impl.cc +++ b/content/browser/speech/speech_recognition_manager_impl.cc @@ -564,7 +564,11 @@ SpeechRecognitionManagerImpl::GetSessionState(int session_id) const { void SpeechRecognitionManagerImpl::SessionStart(const Session& session) { DCHECK_EQ(primary_session_id_, session.id); - session.recognizer->StartRecognition(); + const MediaStreamDevices& devices = session.context.devices; + DCHECK_EQ(1u, devices.size()); + DCHECK_EQ(MEDIA_DEVICE_AUDIO_CAPTURE, devices.front().type); + + session.recognizer->StartRecognition(devices.front().id); } void SpeechRecognitionManagerImpl::SessionAbort(const Session& session) { diff --git a/content/browser/speech/speech_recognizer.h b/content/browser/speech/speech_recognizer.h index 4727109..92feebb 100644 --- a/content/browser/speech/speech_recognizer.h +++ b/content/browser/speech/speech_recognizer.h @@ -23,7 +23,7 @@ class CONTENT_EXPORT SpeechRecognizer DCHECK(listener_); } - virtual void StartRecognition() = 0; + virtual void StartRecognition(const std::string& device_id) = 0; virtual void AbortRecognition() = 0; virtual void StopAudioCapture() = 0; virtual bool IsActive() const = 0; diff --git a/content/browser/speech/speech_recognizer_impl.cc b/content/browser/speech/speech_recognizer_impl.cc index ddb255e..aefeda3 100644 --- a/content/browser/speech/speech_recognizer_impl.cc +++ b/content/browser/speech/speech_recognizer_impl.cc @@ -209,7 +209,10 @@ SpeechRecognizerImpl::SpeechRecognizerImpl( // of causality between events and avoid interleaved event processing due to // synchronous callbacks. -void SpeechRecognizerImpl::StartRecognition() { +void SpeechRecognizerImpl::StartRecognition(const std::string& device_id) { + DCHECK(!device_id.empty()); + device_id_ = device_id; + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(&SpeechRecognizerImpl::DispatchEvent, this, FSMEventArgs(EVENT_START))); @@ -499,6 +502,8 @@ SpeechRecognizerImpl::StartRecording(const FSMEventArgs&) { audio_level_ = 0; listener()->OnRecognitionStart(session_id()); + // TODO(xians): Check if the OS has the device with |device_id_|, return + // |SPEECH_AUDIO_ERROR_DETAILS_NO_MIC| if the target device does not exist. if (!audio_manager->HasAudioInputDevices()) { return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO, SPEECH_AUDIO_ERROR_DETAILS_NO_MIC)); @@ -508,7 +513,7 @@ SpeechRecognizerImpl::StartRecording(const FSMEventArgs&) { // TODO(xians): use the correct input device here. AudioParameters in_params = audio_manager->GetInputStreamParameters( - media::AudioManagerBase::kDefaultDeviceId); + device_id_); if (!in_params.IsValid() && !unit_test_is_active) { DLOG(ERROR) << "Invalid native audio input parameters"; return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO)); @@ -561,7 +566,7 @@ SpeechRecognizerImpl::StartRecording(const FSMEventArgs&) { // TODO(xians): use the correct input device here. audio_controller_ = AudioInputController::Create( - audio_manager, this, input_parameters); + audio_manager, this, input_parameters, device_id_); if (!audio_controller_) { return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO)); diff --git a/content/browser/speech/speech_recognizer_impl.h b/content/browser/speech/speech_recognizer_impl.h index e75560e..4945132 100644 --- a/content/browser/speech/speech_recognizer_impl.h +++ b/content/browser/speech/speech_recognizer_impl.h @@ -44,7 +44,7 @@ class CONTENT_EXPORT SpeechRecognizerImpl bool is_single_shot, SpeechRecognitionEngine* engine); - virtual void StartRecognition() OVERRIDE; + virtual void StartRecognition(const std::string& device_id) OVERRIDE; virtual void AbortRecognition() OVERRIDE; virtual void StopAudioCapture() OVERRIDE; virtual bool IsActive() const OVERRIDE; @@ -148,6 +148,7 @@ class CONTENT_EXPORT SpeechRecognizerImpl bool is_dispatching_event_; bool is_single_shot_; FSMState state_; + std::string device_id_; class OnDataConverter; diff --git a/content/browser/speech/speech_recognizer_impl_android.cc b/content/browser/speech/speech_recognizer_impl_android.cc index 21aa9de..bb422cc 100644 --- a/content/browser/speech/speech_recognizer_impl_android.cc +++ b/content/browser/speech/speech_recognizer_impl_android.cc @@ -34,8 +34,10 @@ SpeechRecognizerImplAndroid::SpeechRecognizerImplAndroid( SpeechRecognizerImplAndroid::~SpeechRecognizerImplAndroid() { } -void SpeechRecognizerImplAndroid::StartRecognition() { +void SpeechRecognizerImplAndroid::StartRecognition( + const std::string& device_id) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + // TODO(xians): Open the correct device for speech on Android. BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( &SpeechRecognitionEventListener::OnRecognitionStart, base::Unretained(listener()), diff --git a/content/browser/speech/speech_recognizer_impl_android.h b/content/browser/speech/speech_recognizer_impl_android.h index 6565e73..a9629a8 100644 --- a/content/browser/speech/speech_recognizer_impl_android.h +++ b/content/browser/speech/speech_recognizer_impl_android.h @@ -24,7 +24,7 @@ class CONTENT_EXPORT SpeechRecognizerImplAndroid : public SpeechRecognizer { int session_id); // SpeechRecognizer methods. - virtual void StartRecognition() OVERRIDE; + virtual void StartRecognition(const std::string& device_id) OVERRIDE; virtual void AbortRecognition() OVERRIDE; virtual void StopAudioCapture() OVERRIDE; virtual bool IsActive() const OVERRIDE; diff --git a/content/browser/speech/speech_recognizer_impl_unittest.cc b/content/browser/speech/speech_recognizer_impl_unittest.cc index 058f87e..dcd4d5b 100644 --- a/content/browser/speech/speech_recognizer_impl_unittest.cc +++ b/content/browser/speech/speech_recognizer_impl_unittest.cc @@ -8,6 +8,7 @@ #include "content/browser/speech/google_one_shot_remote_engine.h" #include "content/browser/speech/speech_recognizer_impl.h" #include "content/public/browser/speech_recognition_event_listener.h" +#include "media/audio/audio_manager_base.h" #include "media/audio/fake_audio_input_stream.h" #include "media/audio/fake_audio_output_stream.h" #include "media/audio/mock_audio_manager.h" @@ -184,7 +185,7 @@ class SpeechRecognizerImplTest : public SpeechRecognitionEventListener, TEST_F(SpeechRecognizerImplTest, StopNoData) { // Check for callbacks when stopping record before any audio gets recorded. - recognizer_->StartRecognition(); + recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId); recognizer_->StopAudioCapture(); base::MessageLoop::current()->RunUntilIdle(); EXPECT_TRUE(recognition_started_); @@ -197,7 +198,7 @@ TEST_F(SpeechRecognizerImplTest, StopNoData) { TEST_F(SpeechRecognizerImplTest, CancelNoData) { // Check for callbacks when canceling recognition before any audio gets // recorded. - recognizer_->StartRecognition(); + recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId); recognizer_->AbortRecognition(); base::MessageLoop::current()->RunUntilIdle(); EXPECT_TRUE(recognition_started_); @@ -210,7 +211,7 @@ TEST_F(SpeechRecognizerImplTest, CancelNoData) { TEST_F(SpeechRecognizerImplTest, StopWithData) { // Start recording, give some data and then stop. This should wait for the // network callback to arrive before completion. - recognizer_->StartRecognition(); + recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId); base::MessageLoop::current()->RunUntilIdle(); TestAudioInputController* controller = audio_input_controller_factory_.controller(); @@ -259,7 +260,7 @@ TEST_F(SpeechRecognizerImplTest, StopWithData) { TEST_F(SpeechRecognizerImplTest, CancelWithData) { // Start recording, give some data and then cancel. - recognizer_->StartRecognition(); + recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId); base::MessageLoop::current()->RunUntilIdle(); TestAudioInputController* controller = audio_input_controller_factory_.controller(); @@ -280,7 +281,7 @@ TEST_F(SpeechRecognizerImplTest, CancelWithData) { TEST_F(SpeechRecognizerImplTest, ConnectionError) { // Start recording, give some data and then stop. Issue the network callback // with a connection error and verify that the recognizer bubbles the error up - recognizer_->StartRecognition(); + recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId); base::MessageLoop::current()->RunUntilIdle(); TestAudioInputController* controller = audio_input_controller_factory_.controller(); @@ -318,7 +319,7 @@ TEST_F(SpeechRecognizerImplTest, ConnectionError) { TEST_F(SpeechRecognizerImplTest, ServerError) { // Start recording, give some data and then stop. Issue the network callback // with a 500 error and verify that the recognizer bubbles the error up - recognizer_->StartRecognition(); + recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId); base::MessageLoop::current()->RunUntilIdle(); TestAudioInputController* controller = audio_input_controller_factory_.controller(); @@ -354,7 +355,7 @@ TEST_F(SpeechRecognizerImplTest, ServerError) { TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { // Check if things tear down properly if AudioInputController threw an error. - recognizer_->StartRecognition(); + recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId); base::MessageLoop::current()->RunUntilIdle(); TestAudioInputController* controller = audio_input_controller_factory_.controller(); @@ -371,7 +372,7 @@ TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { // Check if things tear down properly if AudioInputController threw an error // after giving some audio data. - recognizer_->StartRecognition(); + recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId); base::MessageLoop::current()->RunUntilIdle(); TestAudioInputController* controller = audio_input_controller_factory_.controller(); @@ -391,7 +392,7 @@ TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { // Start recording and give a lot of packets with audio samples set to zero. // This should trigger the no-speech detector and issue a callback. - recognizer_->StartRecognition(); + recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId); base::MessageLoop::current()->RunUntilIdle(); TestAudioInputController* controller = audio_input_controller_factory_.controller(); @@ -417,7 +418,7 @@ TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { // and then some more with reasonably loud audio samples. This should be // treated as normal speech input and the no-speech detector should not get // triggered. - recognizer_->StartRecognition(); + recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId); base::MessageLoop::current()->RunUntilIdle(); TestAudioInputController* controller = audio_input_controller_factory_.controller(); @@ -455,7 +456,7 @@ TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) { // and then some more with reasonably loud audio samples. Check that we don't // get the callback during estimation phase, then get zero for the silence // samples and proper volume for the loud audio. - recognizer_->StartRecognition(); + recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId); base::MessageLoop::current()->RunUntilIdle(); TestAudioInputController* controller = audio_input_controller_factory_.controller(); diff --git a/media/audio/audio_input_controller.cc b/media/audio/audio_input_controller.cc index 36371cc..31e137e 100644 --- a/media/audio/audio_input_controller.cc +++ b/media/audio/audio_input_controller.cc @@ -55,7 +55,8 @@ AudioInputController::~AudioInputController() { scoped_refptr<AudioInputController> AudioInputController::Create( AudioManager* audio_manager, EventHandler* event_handler, - const AudioParameters& params) { + const AudioParameters& params, + const std::string& device_id) { DCHECK(audio_manager); if (!params.IsValid() || (params.channels() > kMaxInputChannels)) @@ -70,8 +71,7 @@ scoped_refptr<AudioInputController> AudioInputController::Create( controller->message_loop_ = audio_manager->GetMessageLoop(); // Create and open a new audio input stream from the existing - // audio-device thread. Use the default audio-input device. - std::string device_id = AudioManagerBase::kDefaultDeviceId; + // audio-device thread. if (!controller->message_loop_->PostTask(FROM_HERE, base::Bind(&AudioInputController::DoCreate, controller, base::Unretained(audio_manager), params, device_id))) { diff --git a/media/audio/audio_input_controller.h b/media/audio/audio_input_controller.h index 1200aa8..6b1d1e7 100644 --- a/media/audio/audio_input_controller.h +++ b/media/audio/audio_input_controller.h @@ -122,11 +122,12 @@ class MEDIA_EXPORT AudioInputController // Factory method for creating an AudioInputController. // The audio device will be created on the audio thread, and when that is // done, the event handler will receive an OnCreated() call from that same - // thread. + // thread. |device_id| is the unique ID of the audio device to be opened. static scoped_refptr<AudioInputController> Create( AudioManager* audio_manager, EventHandler* event_handler, - const AudioParameters& params); + const AudioParameters& params, + const std::string& device_id); // Sets the factory used by the static method Create(). AudioInputController // does not take ownership of |factory|. A value of NULL results in an diff --git a/media/audio/audio_input_controller_unittest.cc b/media/audio/audio_input_controller_unittest.cc index 50d5328..1042530 100644 --- a/media/audio/audio_input_controller_unittest.cc +++ b/media/audio/audio_input_controller_unittest.cc @@ -8,6 +8,7 @@ #include "base/synchronization/waitable_event.h" #include "base/test/test_timeouts.h" #include "media/audio/audio_input_controller.h" +#include "media/audio/audio_manager_base.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -83,7 +84,8 @@ TEST_F(AudioInputControllerTest, CreateAndClose) { AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, kSampleRate, kBitsPerSample, kSamplesPerPacket); scoped_refptr<AudioInputController> controller = - AudioInputController::Create(audio_manager.get(), &event_handler, params); + AudioInputController::Create(audio_manager.get(), &event_handler, params, + AudioManagerBase::kDefaultDeviceId); ASSERT_TRUE(controller.get()); // Wait for OnCreated() to fire. @@ -118,7 +120,8 @@ TEST_F(AudioInputControllerTest, RecordAndClose) { // Creating the AudioInputController should render an OnCreated() call. scoped_refptr<AudioInputController> controller = - AudioInputController::Create(audio_manager.get(), &event_handler, params); + AudioInputController::Create(audio_manager.get(), &event_handler, params, + AudioManagerBase::kDefaultDeviceId); ASSERT_TRUE(controller.get()); // Start recording and trigger one OnRecording() call. @@ -164,7 +167,8 @@ TEST_F(AudioInputControllerTest, RecordAndError) { // Creating the AudioInputController should render an OnCreated() call. scoped_refptr<AudioInputController> controller = - AudioInputController::Create(audio_manager.get(), &event_handler, params); + AudioInputController::Create(audio_manager.get(), &event_handler, params, + AudioManagerBase::kDefaultDeviceId); ASSERT_TRUE(controller.get()); // Start recording and trigger one OnRecording() call. @@ -195,7 +199,8 @@ TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) { AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, kSampleRate, kBitsPerSample, kSamplesPerPacket * 1000); scoped_refptr<AudioInputController> controller = - AudioInputController::Create(audio_manager.get(), &event_handler, params); + AudioInputController::Create(audio_manager.get(), &event_handler, params, + AudioManagerBase::kDefaultDeviceId); ASSERT_FALSE(controller.get()); } @@ -214,7 +219,8 @@ TEST_F(AudioInputControllerTest, CloseTwice) { AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, kSampleRate, kBitsPerSample, kSamplesPerPacket); scoped_refptr<AudioInputController> controller = - AudioInputController::Create(audio_manager.get(), &event_handler, params); + AudioInputController::Create(audio_manager.get(), &event_handler, params, + AudioManagerBase::kDefaultDeviceId); ASSERT_TRUE(controller.get()); controller->Record(); |