1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
content / browser / speech / speech_recognizer_impl_unittest.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/speech/speech_recognizer_impl.h"
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <vector>
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/numerics/byte_conversions.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread.h"
#include "content/browser/speech/network_speech_recognition_engine_impl.h"
#include "content/public/browser/google_streaming_api.pb.h"
#include "content/public/browser/speech_recognition_audio_forwarder_config.h"
#include "content/public/browser/speech_recognition_event_listener.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_task_environment.h"
#include "media/audio/audio_device_description.h"
#include "media/audio/audio_system_impl.h"
#include "media/audio/fake_audio_input_stream.h"
#include "media/audio/fake_audio_output_stream.h"
#include "media/audio/mock_audio_manager.h"
#include "media/audio/test_audio_thread.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_glitch_info.h"
#include "media/base/test_helpers.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/data_pipe_utils.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"
#include "services/network/public/cpp/url_loader_completion_status.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
using media::AudioInputStream;
using media::AudioOutputStream;
using media::AudioParameters;
namespace content {
namespace {
class MockCapturerSource : public media::AudioCapturerSource {
public:
MockCapturerSource() = default;
MOCK_METHOD2(Initialize,
void(const media::AudioParameters& params,
CaptureCallback* callback));
MOCK_METHOD0(Start, void());
MOCK_METHOD0(Stop, void());
MOCK_METHOD1(SetAutomaticGainControl, void(bool enable));
MOCK_METHOD1(SetVolume, void(double volume));
MOCK_METHOD1(SetOutputDeviceForAec,
void(const std::string& output_device_id));
protected:
~MockCapturerSource() override = default;
};
} // namespace
class SpeechRecognizerImplTest : public SpeechRecognitionEventListener,
public testing::Test {
public:
SpeechRecognizerImplTest()
: audio_capturer_source_(new testing::NiceMock<MockCapturerSource>()),
recognition_started_(false),
recognition_ended_(false),
result_received_(false),
audio_started_(false),
audio_ended_(false),
sound_started_(false),
sound_ended_(false),
error_(media::mojom::SpeechRecognitionErrorCode::kNone),
volume_(-1.0f) {
// This test environment is not set up to support out-of-process services.
feature_list_.InitWithFeatures(
/*enabled_features=*/{},
/*disabled_features=*/{features::kAudioServiceOutOfProcess});
// SpeechRecognizer takes ownership of sr_engine.
std::unique_ptr<NetworkSpeechRecognitionEngineImpl> sr_engine =
std::make_unique<NetworkSpeechRecognitionEngineImpl>(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&url_loader_factory_),
"" /* accept_language */);
NetworkSpeechRecognitionEngineImpl::Config config;
config.audio_num_bits_per_sample =
SpeechRecognizerImpl::kNumBitsPerAudioSample;
config.audio_sample_rate = SpeechRecognizerImpl::kAudioSampleRate;
config.filter_profanities = false;
sr_engine->SetConfig(config);
const int kTestingSessionId = 1;
audio_manager_ = std::make_unique<media::MockAudioManager>(
std::make_unique<media::TestAudioThread>(true));
audio_manager_->SetInputStreamParameters(
media::AudioParameters::UnavailableDeviceParams());
audio_system_ =
std::make_unique<media::AudioSystemImpl>(audio_manager_.get());
SpeechRecognizerImpl::SetAudioEnvironmentForTesting(
audio_system_.get(), audio_capturer_source_.get());
recognizer_ = new SpeechRecognizerImpl(this, audio_system_.get(),
kTestingSessionId, false, false,
std::move(sr_engine), std::nullopt);
int audio_packet_length_bytes =
(SpeechRecognizerImpl::kAudioSampleRate *
NetworkSpeechRecognitionEngineImpl::kAudioPacketIntervalMs *
ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) *
SpeechRecognizerImpl::kNumBitsPerAudioSample) /
(8 * 1000);
audio_packet_.resize(audio_packet_length_bytes);
const int channels =
ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout);
int bytes_per_sample = SpeechRecognizerImpl::kNumBitsPerAudioSample / 8;
const int frames = audio_packet_length_bytes / channels / bytes_per_sample;
audio_bus_ = media::AudioBus::Create(channels, frames);
audio_bus_->Zero();
}
~SpeechRecognizerImplTest() override {
SpeechRecognizerImpl::SetAudioEnvironmentForTesting(nullptr, nullptr);
audio_manager_->Shutdown();
}
[[nodiscard]] bool GetUpstreamRequest(
const network::TestURLLoaderFactory::PendingRequest**
pending_request_out) {
return GetPendingRequest(pending_request_out, "/up");
}
[[nodiscard]] bool GetDownstreamRequest(
const network::TestURLLoaderFactory::PendingRequest**
pending_request_out) {
return GetPendingRequest(pending_request_out, "/down");
}
[[nodiscard]] bool GetPendingRequest(
const network::TestURLLoaderFactory::PendingRequest** pending_request_out,
const char* url_substring) {
for (const auto& pending_request :
*url_loader_factory_.pending_requests()) {
if (pending_request.request.url.spec().find(url_substring) !=
std::string::npos) {
*pending_request_out = &pending_request;
return true;
}
}
return false;
}
void CheckEventsConsistency() {
// Note: "!x || y" == "x implies y".
EXPECT_TRUE(!recognition_ended_ || recognition_started_);
EXPECT_TRUE(!audio_ended_ || audio_started_);
EXPECT_TRUE(!sound_ended_ || sound_started_);
EXPECT_TRUE(!audio_started_ || recognition_started_);
EXPECT_TRUE(!sound_started_ || audio_started_);
EXPECT_TRUE(!audio_ended_ || (sound_ended_ || !sound_started_));
EXPECT_TRUE(!recognition_ended_ || (audio_ended_ || !audio_started_));
}
void CheckFinalEventsConsistency() {
// Note: "!(x ^ y)" == "(x && y) || (!x && !x)".
EXPECT_FALSE(recognition_started_ ^ recognition_ended_);
EXPECT_FALSE(audio_started_ ^ audio_ended_);
EXPECT_FALSE(sound_started_ ^ sound_ended_);
}
// Overridden from SpeechRecognitionEventListener:
void OnAudioStart(int session_id) override {
audio_started_ = true;
CheckEventsConsistency();
}
void OnAudioEnd(int session_id) override {
audio_ended_ = true;
CheckEventsConsistency();
}
void OnRecognitionResults(
int session_id,
const std::vector<media::mojom::WebSpeechRecognitionResultPtr>& results)
override {
result_received_ = true;
}
void OnRecognitionError(
int session_id,
const media::mojom::SpeechRecognitionError& error) override {
EXPECT_TRUE(recognition_started_);
EXPECT_FALSE(recognition_ended_);
error_ = error.code;
}
void OnAudioLevelsChange(int session_id,
float volume,
float noise_volume) override {
volume_ = volume;
noise_volume_ = noise_volume;
}
void OnRecognitionEnd(int session_id) override {
recognition_ended_ = true;
CheckEventsConsistency();
}
void OnRecognitionStart(int session_id) override {
recognition_started_ = true;
CheckEventsConsistency();
}
void OnSoundStart(int session_id) override {
sound_started_ = true;
CheckEventsConsistency();
}
void OnSoundEnd(int session_id) override {
sound_ended_ = true;
CheckEventsConsistency();
}
void CopyPacketToAudioBus() {
static_assert(SpeechRecognizerImpl::kNumBitsPerAudioSample == 16,
"FromInterleaved expects 2 bytes.");
// Copy the created signal into an audio bus in a deinterleaved format.
audio_bus_->FromInterleaved<media::SignedInt16SampleTypeTraits>(
reinterpret_cast<int16_t*>(audio_packet_.data()), audio_bus_->frames());
}
void FillPacketWithTestWaveform() {
// Fill the input with a simple pattern, a 125Hz sawtooth waveform.
for (size_t i = 0; i < audio_packet_.size(); ++i)
audio_packet_[i] = static_cast<uint8_t>(i);
CopyPacketToAudioBus();
}
void FillPacketWithNoise() {
int value = 0;
int factor = 175;
for (size_t i = 0; i < audio_packet_.size(); ++i) {
value += factor;
audio_packet_[i] = value % 100;
}
CopyPacketToAudioBus();
}
void Capture(media::AudioBus* data) {
auto* capture_callback =
static_cast<media::AudioCapturerSource::CaptureCallback*>(
recognizer_.get());
capture_callback->Capture(data, base::TimeTicks::Now(), {}, 0.0, false);
}
void OnCaptureError() {
auto* capture_callback =
static_cast<media::AudioCapturerSource::CaptureCallback*>(
recognizer_.get());
capture_callback->OnCaptureError(
media::AudioCapturerSource::ErrorCode::kUnknown, "");
}
void WaitForAudioThreadToPostDeviceInfo() {
media::WaitableMessageLoopEvent event;
audio_manager_->GetTaskRunner()->PostTaskAndReply(
FROM_HERE, base::DoNothing(), event.GetClosure());
// Runs the loop and waits for the audio thread to call event's closure,
// which means AudioSystem reply containing device parameters is already
// queued on the main thread.
event.RunAndWait();
}
protected:
base::test::ScopedFeatureList feature_list_;
BrowserTaskEnvironment task_environment_;
network::TestURLLoaderFactory url_loader_factory_;
scoped_refptr<SpeechRecognizerImpl> recognizer_;
std::unique_ptr<media::MockAudioManager> audio_manager_;
std::unique_ptr<media::AudioSystem> audio_system_;
scoped_refptr<MockCapturerSource> audio_capturer_source_;
bool recognition_started_;
bool recognition_ended_;
bool result_received_;
bool audio_started_;
bool audio_ended_;
bool sound_started_;
bool sound_ended_;
media::mojom::SpeechRecognitionErrorCode error_;
std::vector<uint8_t> audio_packet_;
std::unique_ptr<media::AudioBus> audio_bus_;
float volume_;
float noise_volume_;
};
TEST_F(SpeechRecognizerImplTest, StartNoInputDevices) {
// Check for callbacks when stopping record before any audio gets recorded.
audio_manager_->SetHasInputDevices(false);
recognizer_->StartRecognition(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle(); // EVENT_START processing.
EXPECT_TRUE(recognition_started_);
EXPECT_FALSE(audio_started_);
EXPECT_FALSE(result_received_);
OnCaptureError();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kAudioCapture, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, StartFakeInputDevice) {
// Check for callbacks when stopping record before any audio gets recorded.
audio_manager_->SetHasInputDevices(false);
recognizer_->StartRecognition(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle(); // EVENT_START processing.
Capture(audio_bus_.get());
recognizer_->StopAudioCapture();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(recognition_started_);
EXPECT_TRUE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNone, error_);
recognizer_->AbortRecognition();
base::RunLoop().RunUntilIdle();
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, StopBeforeDeviceInfoReceived) {
// Check for callbacks when stopping record before reply is received from
// AudioSystem.
base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
// Block audio thread.
audio_manager_->GetTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(&base::WaitableEvent::Wait, base::Unretained(&event)));
recognizer_->StartRecognition(
media::AudioDeviceDescription::kDefaultDeviceId);
recognizer_->StopAudioCapture();
base::RunLoop().RunUntilIdle();
// Release audio thread and receive a callback from it.
event.Signal();
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(recognition_started_);
EXPECT_FALSE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNone, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, CancelBeforeDeviceInfoReceived) {
// Check for callbacks when stopping record before reply is received from
// AudioSystem.
base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
// Block audio thread.
audio_manager_->GetTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(&base::WaitableEvent::Wait, base::Unretained(&event)));
recognizer_->StartRecognition(
media::AudioDeviceDescription::kDefaultDeviceId);
recognizer_->AbortRecognition();
base::RunLoop().RunUntilIdle();
// Release audio thread and receive a callback from it.
event.Signal();
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(recognition_started_);
EXPECT_FALSE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNone, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, StopNoData) {
// Check for callbacks when stopping record before any audio gets recorded.
recognizer_->StartRecognition(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
recognizer_->StopAudioCapture();
base::RunLoop().RunUntilIdle(); // EVENT_START and EVENT_STOP processing.
EXPECT_TRUE(recognition_started_);
EXPECT_FALSE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNone, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, CancelNoData) {
// Check for callbacks when canceling recognition before any audio gets
// recorded.
recognizer_->StartRecognition(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
recognizer_->AbortRecognition();
base::RunLoop().RunUntilIdle(); // EVENT_START and EVENT_ABORT processing.
EXPECT_TRUE(recognition_started_);
EXPECT_FALSE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kAborted, error_);
CheckFinalEventsConsistency();
}
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(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle(); // EVENT_START processing.
// Try sending 5 chunks of mock audio data and verify that each of them
// resulted immediately in a packet sent out via the network. This verifies
// that we are streaming out encoded data as chunks without waiting for the
// full recording to complete.
const size_t kNumChunks = 5;
mojo::Remote<network::mojom::ChunkedDataPipeGetter> chunked_data_pipe_getter;
mojo::ScopedDataPipeProducerHandle producer_handle;
mojo::ScopedDataPipeConsumerHandle consumer_handle;
ASSERT_EQ(mojo::CreateDataPipe(nullptr, producer_handle, consumer_handle),
MOJO_RESULT_OK);
for (size_t i = 0; i < kNumChunks; ++i) {
Capture(audio_bus_.get());
if (i == 0) {
// Set up data channel to read chunked upload data. Must be done after the
// first OnData() call.
base::RunLoop().RunUntilIdle();
const network::TestURLLoaderFactory::PendingRequest* upstream_request;
ASSERT_TRUE(GetUpstreamRequest(&upstream_request));
ASSERT_TRUE(upstream_request->request.request_body);
ASSERT_EQ(1u, upstream_request->request.request_body->elements()->size());
auto& element =
(*upstream_request->request.request_body->elements_mutable())[0];
ASSERT_EQ(network::DataElement::Tag::kChunkedDataPipe, element.type());
chunked_data_pipe_getter.Bind(
element.As<network::DataElementChunkedDataPipe>()
.ReleaseChunkedDataPipeGetter());
chunked_data_pipe_getter->StartReading(std::move(producer_handle));
}
std::string data;
while (true) {
base::RunLoop().RunUntilIdle();
base::span<const uint8_t> buffer;
MojoResult result =
consumer_handle->BeginReadData(MOJO_READ_DATA_FLAG_NONE, buffer);
if (result == MOJO_RESULT_OK) {
data.append(base::as_string_view(buffer));
consumer_handle->EndReadData(buffer.size());
continue;
}
if (result == MOJO_RESULT_SHOULD_WAIT) {
// Some data has already been read, assume there's no more to read.
if (!data.empty())
break;
continue;
}
FAIL() << "Mojo pipe closed unexpectedly";
}
EXPECT_FALSE(data.empty());
}
recognizer_->StopAudioCapture();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(audio_started_);
EXPECT_TRUE(audio_ended_);
EXPECT_FALSE(recognition_ended_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNone, error_);
// Create a response string.
proto::SpeechRecognitionEvent proto_event;
proto_event.set_status(proto::SpeechRecognitionEvent::STATUS_SUCCESS);
proto::SpeechRecognitionResult* proto_result = proto_event.add_result();
proto_result->set_final(true);
proto::SpeechRecognitionAlternative* proto_alternative =
proto_result->add_alternative();
proto_alternative->set_confidence(0.5f);
proto_alternative->set_transcript("123");
std::string msg_string;
proto_event.SerializeToString(&msg_string);
msg_string.insert(0u, base::as_string_view(base::U32ToBigEndian(
base::checked_cast<uint32_t>(msg_string.size()))));
// Issue the network callback to complete the process.
const network::TestURLLoaderFactory::PendingRequest* downstream_request;
ASSERT_TRUE(GetDownstreamRequest(&downstream_request));
url_loader_factory_.AddResponse(downstream_request->request.url.spec(),
msg_string);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(recognition_ended_);
EXPECT_TRUE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNone, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, CancelWithData) {
// Start recording, give some data and then cancel.
recognizer_->StartRecognition(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle(); // EVENT_START processing.
Capture(audio_bus_.get());
base::RunLoop().RunUntilIdle();
recognizer_->AbortRecognition();
base::RunLoop().RunUntilIdle();
// There should be both upstream and downstream pending requests.
ASSERT_EQ(2u, url_loader_factory_.pending_requests()->size());
EXPECT_TRUE(recognition_started_);
EXPECT_TRUE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kAborted, error_);
CheckFinalEventsConsistency();
}
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(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle(); // EVENT_START processing.
Capture(audio_bus_.get());
base::RunLoop().RunUntilIdle();
// There should be both upstream and downstream pending requests.
ASSERT_EQ(2u, url_loader_factory_.pending_requests()->size());
recognizer_->StopAudioCapture();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(audio_started_);
EXPECT_TRUE(audio_ended_);
EXPECT_FALSE(recognition_ended_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNone, error_);
// Issue the network callback to complete the process.
const network::TestURLLoaderFactory::PendingRequest* pending_request;
ASSERT_TRUE(GetUpstreamRequest(&pending_request));
url_loader_factory_.AddResponse(
pending_request->request.url, network::mojom::URLResponseHead::New(), "",
network::URLLoaderCompletionStatus(net::ERR_CONNECTION_REFUSED));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(recognition_ended_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNetwork, error_);
CheckFinalEventsConsistency();
}
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(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle(); // EVENT_START processing.
Capture(audio_bus_.get());
base::RunLoop().RunUntilIdle();
// There should be both upstream and downstream pending requests.
ASSERT_EQ(2u, url_loader_factory_.pending_requests()->size());
recognizer_->StopAudioCapture();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(audio_started_);
EXPECT_TRUE(audio_ended_);
EXPECT_FALSE(recognition_ended_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNone, error_);
const network::TestURLLoaderFactory::PendingRequest* pending_request;
ASSERT_TRUE(GetUpstreamRequest(&pending_request));
auto response = network::mojom::URLResponseHead::New();
const char kHeaders[] = "HTTP/1.0 500 Internal Server Error";
response->headers = base::MakeRefCounted<net::HttpResponseHeaders>(
net::HttpUtil::AssembleRawHeaders(kHeaders));
url_loader_factory_.AddResponse(pending_request->request.url,
std::move(response), "",
network::URLLoaderCompletionStatus());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(recognition_ended_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNetwork, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, OnCaptureError_PropagatesError) {
// Check if things tear down properly if AudioInputController threw an error.
recognizer_->StartRecognition(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle(); // EVENT_START processing.
OnCaptureError();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(recognition_started_);
EXPECT_FALSE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kAudioCapture, error_);
CheckFinalEventsConsistency();
}
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(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle(); // EVENT_START processing.
int num_packets =
(SpeechRecognizerImpl::kNoSpeechTimeoutMs) /
NetworkSpeechRecognitionEngineImpl::kAudioPacketIntervalMs +
1;
// The vector is already filled with zero value samples on create.
for (int i = 0; i < num_packets; ++i) {
Capture(audio_bus_.get());
}
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(recognition_started_);
EXPECT_TRUE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNoSpeech, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) {
// Start recording and give a lot of packets with audio samples set to zero
// 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(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle(); // EVENT_START processing.
int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) /
NetworkSpeechRecognitionEngineImpl::kAudioPacketIntervalMs;
// The vector is already filled with zero value samples on create.
for (int i = 0; i < num_packets / 2; ++i) {
Capture(audio_bus_.get());
}
FillPacketWithTestWaveform();
for (int i = 0; i < num_packets / 2; ++i) {
Capture(audio_bus_.get());
}
base::RunLoop().RunUntilIdle();
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNone, error_);
EXPECT_TRUE(audio_started_);
EXPECT_FALSE(audio_ended_);
EXPECT_FALSE(recognition_ended_);
recognizer_->AbortRecognition();
base::RunLoop().RunUntilIdle();
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) {
// Start recording and give a lot of packets with audio samples set to zero
// 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(
media::AudioDeviceDescription::kDefaultDeviceId);
base::RunLoop().RunUntilIdle(); // EVENT_PREPARE processing.
WaitForAudioThreadToPostDeviceInfo();
base::RunLoop().RunUntilIdle(); // EVENT_START processing.
// Feed some samples to begin with for the endpointer to do noise estimation.
int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs /
NetworkSpeechRecognitionEngineImpl::kAudioPacketIntervalMs;
FillPacketWithNoise();
for (int i = 0; i < num_packets; ++i) {
Capture(audio_bus_.get());
}
base::RunLoop().RunUntilIdle();
EXPECT_EQ(-1.0f, volume_); // No audio volume set yet.
// The vector is already filled with zero value samples on create.
Capture(audio_bus_.get());
base::RunLoop().RunUntilIdle();
EXPECT_FLOAT_EQ(0.74939233f, volume_);
FillPacketWithTestWaveform();
Capture(audio_bus_.get());
base::RunLoop().RunUntilIdle();
EXPECT_NEAR(0.89926866f, volume_, 0.00001f);
EXPECT_FLOAT_EQ(0.75071919f, noise_volume_);
EXPECT_EQ(media::mojom::SpeechRecognitionErrorCode::kNone, error_);
EXPECT_FALSE(audio_ended_);
EXPECT_FALSE(recognition_ended_);
recognizer_->AbortRecognition();
base::RunLoop().RunUntilIdle();
CheckFinalEventsConsistency();
}
} // namespace content