Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: content/browser/speech/speech_recognizer_impl_unittest.cc

Issue 2624403002: Refactor AudioInputController and related interfaces. (Closed)
Patch Set: Address comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/speech/speech_recognizer_impl.cc ('k') | media/audio/audio_input_controller.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 void FillPacketWithNoise() { 176 void FillPacketWithNoise() {
177 int value = 0; 177 int value = 0;
178 int factor = 175; 178 int factor = 175;
179 for (size_t i = 0; i < audio_packet_.size(); ++i) { 179 for (size_t i = 0; i < audio_packet_.size(); ++i) {
180 value += factor; 180 value += factor;
181 audio_packet_[i] = value % 100; 181 audio_packet_[i] = value % 100;
182 } 182 }
183 CopyPacketToAudioBus(); 183 CopyPacketToAudioBus();
184 } 184 }
185 185
186 void OnData(media::AudioBus* data) {
187 auto* writer =
188 static_cast<AudioInputController::SyncWriter*>(recognizer_.get());
189 writer->Write(data, 0.0, false, 0);
190 }
191
186 protected: 192 protected:
187 TestBrowserThreadBundle thread_bundle_; 193 TestBrowserThreadBundle thread_bundle_;
188 scoped_refptr<SpeechRecognizerImpl> recognizer_; 194 scoped_refptr<SpeechRecognizerImpl> recognizer_;
189 media::ScopedAudioManagerPtr audio_manager_; 195 media::ScopedAudioManagerPtr audio_manager_;
190 bool recognition_started_; 196 bool recognition_started_;
191 bool recognition_ended_; 197 bool recognition_ended_;
192 bool result_received_; 198 bool result_received_;
193 bool audio_started_; 199 bool audio_started_;
194 bool audio_ended_; 200 bool audio_ended_;
195 bool sound_started_; 201 bool sound_started_;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 TestAudioInputController* controller = 246 TestAudioInputController* controller =
241 audio_input_controller_factory_.controller(); 247 audio_input_controller_factory_.controller();
242 ASSERT_TRUE(controller); 248 ASSERT_TRUE(controller);
243 249
244 // Try sending 5 chunks of mock audio data and verify that each of them 250 // Try sending 5 chunks of mock audio data and verify that each of them
245 // resulted immediately in a packet sent out via the network. This verifies 251 // resulted immediately in a packet sent out via the network. This verifies
246 // that we are streaming out encoded data as chunks without waiting for the 252 // that we are streaming out encoded data as chunks without waiting for the
247 // full recording to complete. 253 // full recording to complete.
248 const size_t kNumChunks = 5; 254 const size_t kNumChunks = 5;
249 for (size_t i = 0; i < kNumChunks; ++i) { 255 for (size_t i = 0; i < kNumChunks; ++i) {
250 controller->event_handler()->OnData(controller, audio_bus_.get()); 256 OnData(audio_bus_.get());
251 base::RunLoop().RunUntilIdle(); 257 base::RunLoop().RunUntilIdle();
252 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 258 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
253 ASSERT_TRUE(fetcher); 259 ASSERT_TRUE(fetcher);
254 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); 260 EXPECT_EQ(i + 1, fetcher->upload_chunks().size());
255 } 261 }
256 262
257 recognizer_->StopAudioCapture(); 263 recognizer_->StopAudioCapture();
258 base::RunLoop().RunUntilIdle(); 264 base::RunLoop().RunUntilIdle();
259 EXPECT_TRUE(audio_started_); 265 EXPECT_TRUE(audio_started_);
260 EXPECT_TRUE(audio_ended_); 266 EXPECT_TRUE(audio_ended_);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 } 301 }
296 302
297 TEST_F(SpeechRecognizerImplTest, CancelWithData) { 303 TEST_F(SpeechRecognizerImplTest, CancelWithData) {
298 // Start recording, give some data and then cancel. 304 // Start recording, give some data and then cancel.
299 recognizer_->StartRecognition( 305 recognizer_->StartRecognition(
300 media::AudioDeviceDescription::kDefaultDeviceId); 306 media::AudioDeviceDescription::kDefaultDeviceId);
301 base::RunLoop().RunUntilIdle(); 307 base::RunLoop().RunUntilIdle();
302 TestAudioInputController* controller = 308 TestAudioInputController* controller =
303 audio_input_controller_factory_.controller(); 309 audio_input_controller_factory_.controller();
304 ASSERT_TRUE(controller); 310 ASSERT_TRUE(controller);
305 controller->event_handler()->OnData(controller, audio_bus_.get()); 311 OnData(audio_bus_.get());
306 base::RunLoop().RunUntilIdle(); 312 base::RunLoop().RunUntilIdle();
307 recognizer_->AbortRecognition(); 313 recognizer_->AbortRecognition();
308 base::RunLoop().RunUntilIdle(); 314 base::RunLoop().RunUntilIdle();
309 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); 315 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0));
310 EXPECT_TRUE(recognition_started_); 316 EXPECT_TRUE(recognition_started_);
311 EXPECT_TRUE(audio_started_); 317 EXPECT_TRUE(audio_started_);
312 EXPECT_FALSE(result_received_); 318 EXPECT_FALSE(result_received_);
313 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); 319 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_);
314 CheckFinalEventsConsistency(); 320 CheckFinalEventsConsistency();
315 } 321 }
316 322
317 TEST_F(SpeechRecognizerImplTest, ConnectionError) { 323 TEST_F(SpeechRecognizerImplTest, ConnectionError) {
318 // Start recording, give some data and then stop. Issue the network callback 324 // Start recording, give some data and then stop. Issue the network callback
319 // with a connection error and verify that the recognizer bubbles the error up 325 // with a connection error and verify that the recognizer bubbles the error up
320 recognizer_->StartRecognition( 326 recognizer_->StartRecognition(
321 media::AudioDeviceDescription::kDefaultDeviceId); 327 media::AudioDeviceDescription::kDefaultDeviceId);
322 base::RunLoop().RunUntilIdle(); 328 base::RunLoop().RunUntilIdle();
323 TestAudioInputController* controller = 329 TestAudioInputController* controller =
324 audio_input_controller_factory_.controller(); 330 audio_input_controller_factory_.controller();
325 ASSERT_TRUE(controller); 331 ASSERT_TRUE(controller);
326 controller->event_handler()->OnData(controller, audio_bus_.get()); 332 OnData(audio_bus_.get());
327 base::RunLoop().RunUntilIdle(); 333 base::RunLoop().RunUntilIdle();
328 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 334 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
329 ASSERT_TRUE(fetcher); 335 ASSERT_TRUE(fetcher);
330 336
331 recognizer_->StopAudioCapture(); 337 recognizer_->StopAudioCapture();
332 base::RunLoop().RunUntilIdle(); 338 base::RunLoop().RunUntilIdle();
333 EXPECT_TRUE(audio_started_); 339 EXPECT_TRUE(audio_started_);
334 EXPECT_TRUE(audio_ended_); 340 EXPECT_TRUE(audio_ended_);
335 EXPECT_FALSE(recognition_ended_); 341 EXPECT_FALSE(recognition_ended_);
336 EXPECT_FALSE(result_received_); 342 EXPECT_FALSE(result_received_);
(...skipping 15 matching lines...) Expand all
352 358
353 TEST_F(SpeechRecognizerImplTest, ServerError) { 359 TEST_F(SpeechRecognizerImplTest, ServerError) {
354 // Start recording, give some data and then stop. Issue the network callback 360 // Start recording, give some data and then stop. Issue the network callback
355 // with a 500 error and verify that the recognizer bubbles the error up 361 // with a 500 error and verify that the recognizer bubbles the error up
356 recognizer_->StartRecognition( 362 recognizer_->StartRecognition(
357 media::AudioDeviceDescription::kDefaultDeviceId); 363 media::AudioDeviceDescription::kDefaultDeviceId);
358 base::RunLoop().RunUntilIdle(); 364 base::RunLoop().RunUntilIdle();
359 TestAudioInputController* controller = 365 TestAudioInputController* controller =
360 audio_input_controller_factory_.controller(); 366 audio_input_controller_factory_.controller();
361 ASSERT_TRUE(controller); 367 ASSERT_TRUE(controller);
362 controller->event_handler()->OnData(controller, audio_bus_.get()); 368 OnData(audio_bus_.get());
363 base::RunLoop().RunUntilIdle(); 369 base::RunLoop().RunUntilIdle();
364 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 370 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
365 ASSERT_TRUE(fetcher); 371 ASSERT_TRUE(fetcher);
366 372
367 recognizer_->StopAudioCapture(); 373 recognizer_->StopAudioCapture();
368 base::RunLoop().RunUntilIdle(); 374 base::RunLoop().RunUntilIdle();
369 EXPECT_TRUE(audio_started_); 375 EXPECT_TRUE(audio_started_);
370 EXPECT_TRUE(audio_ended_); 376 EXPECT_TRUE(audio_ended_);
371 EXPECT_FALSE(recognition_ended_); 377 EXPECT_FALSE(recognition_ended_);
372 EXPECT_FALSE(result_received_); 378 EXPECT_FALSE(result_received_);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 411
406 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { 412 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) {
407 // Check if things tear down properly if AudioInputController threw an error 413 // Check if things tear down properly if AudioInputController threw an error
408 // after giving some audio data. 414 // after giving some audio data.
409 recognizer_->StartRecognition( 415 recognizer_->StartRecognition(
410 media::AudioDeviceDescription::kDefaultDeviceId); 416 media::AudioDeviceDescription::kDefaultDeviceId);
411 base::RunLoop().RunUntilIdle(); 417 base::RunLoop().RunUntilIdle();
412 TestAudioInputController* controller = 418 TestAudioInputController* controller =
413 audio_input_controller_factory_.controller(); 419 audio_input_controller_factory_.controller();
414 ASSERT_TRUE(controller); 420 ASSERT_TRUE(controller);
415 controller->event_handler()->OnData(controller, audio_bus_.get()); 421 OnData(audio_bus_.get());
416 controller->event_handler()->OnError(controller, 422 controller->event_handler()->OnError(controller,
417 AudioInputController::UNKNOWN_ERROR); 423 AudioInputController::UNKNOWN_ERROR);
418 base::RunLoop().RunUntilIdle(); 424 base::RunLoop().RunUntilIdle();
419 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); 425 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0));
420 EXPECT_TRUE(recognition_started_); 426 EXPECT_TRUE(recognition_started_);
421 EXPECT_TRUE(audio_started_); 427 EXPECT_TRUE(audio_started_);
422 EXPECT_FALSE(result_received_); 428 EXPECT_FALSE(result_received_);
423 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_); 429 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_);
424 CheckFinalEventsConsistency(); 430 CheckFinalEventsConsistency();
425 } 431 }
426 432
427 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { 433 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) {
428 // Start recording and give a lot of packets with audio samples set to zero. 434 // Start recording and give a lot of packets with audio samples set to zero.
429 // This should trigger the no-speech detector and issue a callback. 435 // This should trigger the no-speech detector and issue a callback.
430 recognizer_->StartRecognition( 436 recognizer_->StartRecognition(
431 media::AudioDeviceDescription::kDefaultDeviceId); 437 media::AudioDeviceDescription::kDefaultDeviceId);
432 base::RunLoop().RunUntilIdle(); 438 base::RunLoop().RunUntilIdle();
433 TestAudioInputController* controller = 439 TestAudioInputController* controller =
434 audio_input_controller_factory_.controller(); 440 audio_input_controller_factory_.controller();
435 ASSERT_TRUE(controller); 441 ASSERT_TRUE(controller);
436 442
437 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / 443 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) /
438 SpeechRecognitionEngine::kAudioPacketIntervalMs + 1; 444 SpeechRecognitionEngine::kAudioPacketIntervalMs + 1;
439 // The vector is already filled with zero value samples on create. 445 // The vector is already filled with zero value samples on create.
440 for (int i = 0; i < num_packets; ++i) { 446 for (int i = 0; i < num_packets; ++i) {
441 controller->event_handler()->OnData(controller, audio_bus_.get()); 447 OnData(audio_bus_.get());
442 } 448 }
443 base::RunLoop().RunUntilIdle(); 449 base::RunLoop().RunUntilIdle();
444 EXPECT_TRUE(recognition_started_); 450 EXPECT_TRUE(recognition_started_);
445 EXPECT_TRUE(audio_started_); 451 EXPECT_TRUE(audio_started_);
446 EXPECT_FALSE(result_received_); 452 EXPECT_FALSE(result_received_);
447 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); 453 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_);
448 CheckFinalEventsConsistency(); 454 CheckFinalEventsConsistency();
449 } 455 }
450 456
451 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { 457 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) {
452 // Start recording and give a lot of packets with audio samples set to zero 458 // Start recording and give a lot of packets with audio samples set to zero
453 // and then some more with reasonably loud audio samples. This should be 459 // and then some more with reasonably loud audio samples. This should be
454 // treated as normal speech input and the no-speech detector should not get 460 // treated as normal speech input and the no-speech detector should not get
455 // triggered. 461 // triggered.
456 recognizer_->StartRecognition( 462 recognizer_->StartRecognition(
457 media::AudioDeviceDescription::kDefaultDeviceId); 463 media::AudioDeviceDescription::kDefaultDeviceId);
458 base::RunLoop().RunUntilIdle(); 464 base::RunLoop().RunUntilIdle();
459 TestAudioInputController* controller = 465 TestAudioInputController* controller =
460 audio_input_controller_factory_.controller(); 466 audio_input_controller_factory_.controller();
461 ASSERT_TRUE(controller); 467 ASSERT_TRUE(controller);
462 controller = audio_input_controller_factory_.controller(); 468 controller = audio_input_controller_factory_.controller();
463 ASSERT_TRUE(controller); 469 ASSERT_TRUE(controller);
464 470
465 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / 471 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) /
466 SpeechRecognitionEngine::kAudioPacketIntervalMs; 472 SpeechRecognitionEngine::kAudioPacketIntervalMs;
467 473
468 // The vector is already filled with zero value samples on create. 474 // The vector is already filled with zero value samples on create.
469 for (int i = 0; i < num_packets / 2; ++i) { 475 for (int i = 0; i < num_packets / 2; ++i) {
470 controller->event_handler()->OnData(controller, audio_bus_.get()); 476 OnData(audio_bus_.get());
471 } 477 }
472 478
473 FillPacketWithTestWaveform(); 479 FillPacketWithTestWaveform();
474 for (int i = 0; i < num_packets / 2; ++i) { 480 for (int i = 0; i < num_packets / 2; ++i) {
475 controller->event_handler()->OnData(controller, audio_bus_.get()); 481 OnData(audio_bus_.get());
476 } 482 }
477 483
478 base::RunLoop().RunUntilIdle(); 484 base::RunLoop().RunUntilIdle();
479 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); 485 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
480 EXPECT_TRUE(audio_started_); 486 EXPECT_TRUE(audio_started_);
481 EXPECT_FALSE(audio_ended_); 487 EXPECT_FALSE(audio_ended_);
482 EXPECT_FALSE(recognition_ended_); 488 EXPECT_FALSE(recognition_ended_);
483 recognizer_->AbortRecognition(); 489 recognizer_->AbortRecognition();
484 base::RunLoop().RunUntilIdle(); 490 base::RunLoop().RunUntilIdle();
485 CheckFinalEventsConsistency(); 491 CheckFinalEventsConsistency();
(...skipping 11 matching lines...) Expand all
497 audio_input_controller_factory_.controller(); 503 audio_input_controller_factory_.controller();
498 ASSERT_TRUE(controller); 504 ASSERT_TRUE(controller);
499 controller = audio_input_controller_factory_.controller(); 505 controller = audio_input_controller_factory_.controller();
500 ASSERT_TRUE(controller); 506 ASSERT_TRUE(controller);
501 507
502 // Feed some samples to begin with for the endpointer to do noise estimation. 508 // Feed some samples to begin with for the endpointer to do noise estimation.
503 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / 509 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs /
504 SpeechRecognitionEngine::kAudioPacketIntervalMs; 510 SpeechRecognitionEngine::kAudioPacketIntervalMs;
505 FillPacketWithNoise(); 511 FillPacketWithNoise();
506 for (int i = 0; i < num_packets; ++i) { 512 for (int i = 0; i < num_packets; ++i) {
507 controller->event_handler()->OnData(controller, audio_bus_.get()); 513 OnData(audio_bus_.get());
508 } 514 }
509 base::RunLoop().RunUntilIdle(); 515 base::RunLoop().RunUntilIdle();
510 EXPECT_EQ(-1.0f, volume_); // No audio volume set yet. 516 EXPECT_EQ(-1.0f, volume_); // No audio volume set yet.
511 517
512 // The vector is already filled with zero value samples on create. 518 // The vector is already filled with zero value samples on create.
513 controller->event_handler()->OnData(controller, audio_bus_.get()); 519 OnData(audio_bus_.get());
514 base::RunLoop().RunUntilIdle(); 520 base::RunLoop().RunUntilIdle();
515 EXPECT_FLOAT_EQ(0.74939233f, volume_); 521 EXPECT_FLOAT_EQ(0.74939233f, volume_);
516 522
517 FillPacketWithTestWaveform(); 523 FillPacketWithTestWaveform();
518 controller->event_handler()->OnData(controller, audio_bus_.get()); 524 OnData(audio_bus_.get());
519 base::RunLoop().RunUntilIdle(); 525 base::RunLoop().RunUntilIdle();
520 EXPECT_NEAR(0.89926866f, volume_, 0.00001f); 526 EXPECT_NEAR(0.89926866f, volume_, 0.00001f);
521 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); 527 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_);
522 528
523 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); 529 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
524 EXPECT_FALSE(audio_ended_); 530 EXPECT_FALSE(audio_ended_);
525 EXPECT_FALSE(recognition_ended_); 531 EXPECT_FALSE(recognition_ended_);
526 recognizer_->AbortRecognition(); 532 recognizer_->AbortRecognition();
527 base::RunLoop().RunUntilIdle(); 533 base::RunLoop().RunUntilIdle();
528 CheckFinalEventsConsistency(); 534 CheckFinalEventsConsistency();
529 } 535 }
530 536
531 } // namespace content 537 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/speech/speech_recognizer_impl.cc ('k') | media/audio/audio_input_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698