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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "SpeechRecognition.h"
#include "nsCOMPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "mozilla/dom/AudioStreamTrack.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/SpeechRecognitionBinding.h"
#include "mozilla/dom/MediaStreamTrackBinding.h"
#include "mozilla/dom/MediaStreamError.h"
#include "mozilla/dom/RootedDictionary.h"
#include "mozilla/dom/SpeechGrammar.h"
#include "mozilla/MediaManager.h"
#include "mozilla/Preferences.h"
#include "mozilla/ResultVariant.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPrefs_media.h"
#include "mozilla/AbstractThread.h"
#include "VideoUtils.h"
#include "AudioSegment.h"
#include "MediaEnginePrefs.h"
#include "endpointer.h"
#include "mozilla/dom/SpeechRecognitionEvent.h"
#include "nsComponentManagerUtils.h"
#include "nsContentUtils.h"
#include "mozilla/dom/Document.h"
#include "nsIObserverService.h"
#include "nsIPermissionManager.h"
#include "nsIPrincipal.h"
#include "nsPIDOMWindow.h"
#include "nsServiceManagerUtils.h"
#include "nsQueryObject.h"
#include "SpeechTrackListener.h"
#include <algorithm>
// Undo the windows.h damage
#if defined(XP_WIN) && defined(GetMessage)
# undef GetMessage
#endif
namespace mozilla::dom {
#define PREFERENCE_DEFAULT_RECOGNITION_SERVICE "media.webspeech.service.default"
#define DEFAULT_RECOGNITION_SERVICE "online"
#define PREFERENCE_ENDPOINTER_SILENCE_LENGTH "media.webspeech.silence_length"
#define PREFERENCE_ENDPOINTER_LONG_SILENCE_LENGTH \
"media.webspeech.long_silence_length"
#define PREFERENCE_ENDPOINTER_LONG_SPEECH_LENGTH \
"media.webspeech.long_speech_length"
#define PREFERENCE_SPEECH_DETECTION_TIMEOUT_MS \
"media.webspeech.recognition.timeout"
static const uint32_t kSAMPLE_RATE = 16000;
// number of frames corresponding to 300ms of audio to send to endpointer while
// it's in environment estimation mode
// kSAMPLE_RATE frames = 1s, kESTIMATION_FRAMES frames = 300ms
static const uint32_t kESTIMATION_SAMPLES = 300 * kSAMPLE_RATE / 1000;
LogModule* GetSpeechRecognitionLog() {
static LazyLogModule sLog("SpeechRecognition");
return sLog;
}
#define SR_LOG(...) \
MOZ_LOG(GetSpeechRecognitionLog(), mozilla::LogLevel::Debug, (__VA_ARGS__))
namespace {
class SpeechRecognitionShutdownBlocker : public media::ShutdownBlocker {
public:
SpeechRecognitionShutdownBlocker(SpeechRecognition* aRecognition,
const nsString& aName)
: media::ShutdownBlocker(aName), mRecognition(aRecognition) {}
NS_IMETHOD BlockShutdown(nsIAsyncShutdownClient*) override {
MOZ_ASSERT(NS_IsMainThread());
// AbortSilently will eventually clear the blocker.
mRecognition->Abort();
return NS_OK;
}
private:
const RefPtr<SpeechRecognition> mRecognition;
};
enum class ServiceCreationError {
ServiceNotFound,
};
Result<nsCOMPtr<nsISpeechRecognitionService>, ServiceCreationError>
CreateSpeechRecognitionService(nsPIDOMWindowInner* aWindow,
SpeechRecognition* aRecognition,
const nsAString& aLang) {
nsAutoCString speechRecognitionServiceCID;
nsAutoCString prefValue;
Preferences::GetCString(PREFERENCE_DEFAULT_RECOGNITION_SERVICE, prefValue);
nsAutoCString speechRecognitionService;
if (!prefValue.IsEmpty()) {
speechRecognitionService = prefValue;
} else {
speechRecognitionService = DEFAULT_RECOGNITION_SERVICE;
}
if (StaticPrefs::media_webspeech_test_fake_recognition_service()) {
speechRecognitionServiceCID =
NS_SPEECH_RECOGNITION_SERVICE_CONTRACTID_PREFIX "fake";
} else {
speechRecognitionServiceCID =
nsLiteralCString(NS_SPEECH_RECOGNITION_SERVICE_CONTRACTID_PREFIX) +
speechRecognitionService;
}
nsresult rv;
nsCOMPtr<nsISpeechRecognitionService> recognitionService;
recognitionService =
do_CreateInstance(speechRecognitionServiceCID.get(), &rv);
if (!recognitionService) {
return Err(ServiceCreationError::ServiceNotFound);
}
return recognitionService;
}
} // namespace
NS_IMPL_CYCLE_COLLECTION_WEAK_PTR_INHERITED(SpeechRecognition,
DOMEventTargetHelper, mStream,
mTrack, mRecognitionService,
mSpeechGrammarList)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SpeechRecognition)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(SpeechRecognition, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(SpeechRecognition, DOMEventTargetHelper)
SpeechRecognition::SpeechRecognition(nsPIDOMWindowInner* aOwnerWindow)
: DOMEventTargetHelper(aOwnerWindow),
mEndpointer(kSAMPLE_RATE),
mAudioSamplesPerChunk(mEndpointer.FrameSize()),
mSpeechDetectionTimer(NS_NewTimer()),
mSpeechGrammarList(new SpeechGrammarList(GetOwner())),
mContinuous(false),
mInterimResults(false),
mMaxAlternatives(1) {
SR_LOG("created SpeechRecognition");
if (StaticPrefs::media_webspeech_test_enable()) {
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
obs->AddObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC, false);
obs->AddObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC, false);
}
mEndpointer.set_speech_input_complete_silence_length(
Preferences::GetInt(PREFERENCE_ENDPOINTER_SILENCE_LENGTH, 1250000));
mEndpointer.set_long_speech_input_complete_silence_length(
Preferences::GetInt(PREFERENCE_ENDPOINTER_LONG_SILENCE_LENGTH, 2500000));
mEndpointer.set_long_speech_length(
Preferences::GetInt(PREFERENCE_ENDPOINTER_SILENCE_LENGTH, 3 * 1000000));
mSpeechDetectionTimeoutMs =
Preferences::GetInt(PREFERENCE_SPEECH_DETECTION_TIMEOUT_MS, 10000);
Reset();
}
SpeechRecognition::~SpeechRecognition() = default;
bool SpeechRecognition::StateBetween(FSMState begin, FSMState end) {
return mCurrentState >= begin && mCurrentState <= end;
}
void SpeechRecognition::SetState(FSMState state) {
mCurrentState = state;
SR_LOG("Transitioned to state %s", GetName(mCurrentState));
}
JSObject* SpeechRecognition::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return SpeechRecognition_Binding::Wrap(aCx, this, aGivenProto);
}
bool SpeechRecognition::IsAuthorized(JSContext* aCx, JSObject* aGlobal) {
nsCOMPtr<nsIPrincipal> principal = nsContentUtils::ObjectPrincipal(aGlobal);
nsresult rv;
nsCOMPtr<nsIPermissionManager> mgr =
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return false;
}
uint32_t speechRecognition = nsIPermissionManager::UNKNOWN_ACTION;
rv = mgr->TestExactPermissionFromPrincipal(principal, "speech-recognition"_ns,
&speechRecognition);
if (NS_WARN_IF(NS_FAILED(rv))) {
return false;
}
bool hasPermission =
(speechRecognition == nsIPermissionManager::ALLOW_ACTION);
return (hasPermission ||
StaticPrefs::media_webspeech_recognition_force_enable() ||
StaticPrefs::media_webspeech_test_enable()) &&
StaticPrefs::media_webspeech_recognition_enable();
}
already_AddRefed<SpeechRecognition> SpeechRecognition::Constructor(
const GlobalObject& aGlobal, ErrorResult& aRv) {
nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aGlobal.GetAsSupports());
if (!win) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<SpeechRecognition> object = new SpeechRecognition(win);
return object.forget();
}
void SpeechRecognition::ProcessEvent(SpeechEvent* aEvent) {
SR_LOG("Processing %s, current state is %s", GetName(aEvent),
GetName(mCurrentState));
if (mAborted && aEvent->mType != EVENT_ABORT) {
// ignore all events while aborting
return;
}
Transition(aEvent);
}
void SpeechRecognition::Transition(SpeechEvent* aEvent) {
switch (mCurrentState) {
case STATE_IDLE:
switch (aEvent->mType) {
case EVENT_START:
// TODO: may want to time out if we wait too long
// for user to approve
WaitForAudioData(aEvent);
break;
case EVENT_STOP:
case EVENT_ABORT:
case EVENT_AUDIO_DATA:
case EVENT_RECOGNITIONSERVICE_INTERMEDIATE_RESULT:
case EVENT_RECOGNITIONSERVICE_FINAL_RESULT:
DoNothing(aEvent);
break;
case EVENT_AUDIO_ERROR:
case EVENT_RECOGNITIONSERVICE_ERROR:
AbortError(aEvent);
break;
default:
MOZ_CRASH("Invalid event");
}
break;
case STATE_STARTING:
switch (aEvent->mType) {
case EVENT_AUDIO_DATA:
StartedAudioCapture(aEvent);
break;
case EVENT_AUDIO_ERROR:
case EVENT_RECOGNITIONSERVICE_ERROR:
AbortError(aEvent);
break;
case EVENT_ABORT:
AbortSilently(aEvent);
break;
case EVENT_STOP:
ResetAndEnd();
break;
case EVENT_RECOGNITIONSERVICE_INTERMEDIATE_RESULT:
case EVENT_RECOGNITIONSERVICE_FINAL_RESULT:
DoNothing(aEvent);
break;
case EVENT_START:
SR_LOG("STATE_STARTING: Unhandled event %s", GetName(aEvent));
MOZ_CRASH();
default:
MOZ_CRASH("Invalid event");
}
break;
case STATE_ESTIMATING:
switch (aEvent->mType) {
case EVENT_AUDIO_DATA:
WaitForEstimation(aEvent);
break;
case EVENT_STOP:
StopRecordingAndRecognize(aEvent);
break;
case EVENT_ABORT:
AbortSilently(aEvent);
break;
case EVENT_RECOGNITIONSERVICE_INTERMEDIATE_RESULT:
case EVENT_RECOGNITIONSERVICE_FINAL_RESULT:
case EVENT_RECOGNITIONSERVICE_ERROR:
DoNothing(aEvent);
break;
case EVENT_AUDIO_ERROR:
AbortError(aEvent);
break;
case EVENT_START:
SR_LOG("STATE_ESTIMATING: Unhandled event %d", aEvent->mType);
MOZ_CRASH();
default:
MOZ_CRASH("Invalid event");
}
break;
case STATE_WAITING_FOR_SPEECH:
switch (aEvent->mType) {
case EVENT_AUDIO_DATA:
DetectSpeech(aEvent);
break;
case EVENT_STOP:
StopRecordingAndRecognize(aEvent);
break;
case EVENT_ABORT:
AbortSilently(aEvent);
break;
case EVENT_AUDIO_ERROR:
AbortError(aEvent);
break;
case EVENT_RECOGNITIONSERVICE_INTERMEDIATE_RESULT:
case EVENT_RECOGNITIONSERVICE_FINAL_RESULT:
case EVENT_RECOGNITIONSERVICE_ERROR:
DoNothing(aEvent);
break;
case EVENT_START:
SR_LOG("STATE_STARTING: Unhandled event %s", GetName(aEvent));
MOZ_CRASH();
default:
MOZ_CRASH("Invalid event");
}
break;
case STATE_RECOGNIZING:
switch (aEvent->mType) {
case EVENT_AUDIO_DATA:
WaitForSpeechEnd(aEvent);
break;
case EVENT_STOP:
StopRecordingAndRecognize(aEvent);
break;
case EVENT_AUDIO_ERROR:
case EVENT_RECOGNITIONSERVICE_ERROR:
AbortError(aEvent);
break;
case EVENT_ABORT:
AbortSilently(aEvent);
break;
case EVENT_RECOGNITIONSERVICE_FINAL_RESULT:
case EVENT_RECOGNITIONSERVICE_INTERMEDIATE_RESULT:
DoNothing(aEvent);
break;
case EVENT_START:
SR_LOG("STATE_RECOGNIZING: Unhandled aEvent %s", GetName(aEvent));
MOZ_CRASH();
default:
MOZ_CRASH("Invalid event");
}
break;
case STATE_WAITING_FOR_RESULT:
switch (aEvent->mType) {
case EVENT_STOP:
DoNothing(aEvent);
break;
case EVENT_AUDIO_ERROR:
case EVENT_RECOGNITIONSERVICE_ERROR:
AbortError(aEvent);
break;
case EVENT_RECOGNITIONSERVICE_FINAL_RESULT:
NotifyFinalResult(aEvent);
break;
case EVENT_AUDIO_DATA:
DoNothing(aEvent);
break;
case EVENT_ABORT:
AbortSilently(aEvent);
break;
case EVENT_START:
case EVENT_RECOGNITIONSERVICE_INTERMEDIATE_RESULT:
SR_LOG("STATE_WAITING_FOR_RESULT: Unhandled aEvent %s",
GetName(aEvent));
MOZ_CRASH();
default:
MOZ_CRASH("Invalid event");
}
break;
case STATE_ABORTING:
switch (aEvent->mType) {
case EVENT_STOP:
case EVENT_ABORT:
case EVENT_AUDIO_DATA:
case EVENT_AUDIO_ERROR:
case EVENT_RECOGNITIONSERVICE_INTERMEDIATE_RESULT:
case EVENT_RECOGNITIONSERVICE_FINAL_RESULT:
case EVENT_RECOGNITIONSERVICE_ERROR:
DoNothing(aEvent);
break;
case EVENT_START:
SR_LOG("STATE_ABORTING: Unhandled aEvent %s", GetName(aEvent));
MOZ_CRASH();
default:
MOZ_CRASH("Invalid event");
}
break;
default:
MOZ_CRASH("Invalid state");
}
}
/*
* Handle a segment of recorded audio data.
* Returns the number of samples that were processed.
*/
uint32_t SpeechRecognition::ProcessAudioSegment(AudioSegment* aSegment,
TrackRate aTrackRate) {
AudioSegment::ChunkIterator iterator(*aSegment);
uint32_t samples = 0;
while (!iterator.IsEnded()) {
float out;
mEndpointer.ProcessAudio(*iterator, &out);
samples += iterator->GetDuration();
iterator.Next();
}
// we need to call the nsISpeechRecognitionService::ProcessAudioSegment
// in a separate thread so that any eventual encoding or pre-processing
// of the audio does not block the main thread
nsresult rv = mEncodeTaskQueue->Dispatch(NS_NewRunnableFunction(
"nsISpeechRecognitionService::ProcessAudioSegment",
[=, service = mRecognitionService,
segment = std::move(*aSegment)]() mutable {
service->ProcessAudioSegment(&segment, aTrackRate);
}));
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
Unused << rv;
return samples;
}
/****************************************************************************
* FSM Transition functions
*
* If a transition function may cause a DOM event to be fired,
* it may also be re-entered, since the event handler may cause the
* event loop to spin and new SpeechEvents to be processed.
*
* Rules:
* 1) These methods should call SetState as soon as possible.
* 2) If these methods dispatch DOM events, or call methods that dispatch
* DOM events, that should be done as late as possible.
* 3) If anything must happen after dispatching a DOM event, make sure
* the state is still what the method expected it to be.
****************************************************************************/
void SpeechRecognition::Reset() {
SetState(STATE_IDLE);
// This breaks potential ref-cycles.
mRecognitionService = nullptr;
++mStreamGeneration;
if (mStream) {
mStream->UnregisterTrackListener(this);
mStream = nullptr;
}
mTrack = nullptr;
mTrackIsOwned = false;
mStopRecordingPromise = nullptr;
mEncodeTaskQueue = nullptr;
mEstimationSamples = 0;
mBufferedSamples = 0;
mSpeechDetectionTimer->Cancel();
mAborted = false;
}
void SpeechRecognition::ResetAndEnd() {
Reset();
DispatchTrustedEvent(u"end"_ns);
}
void SpeechRecognition::WaitForAudioData(SpeechEvent* aEvent) {
SetState(STATE_STARTING);
}
void SpeechRecognition::StartedAudioCapture(SpeechEvent* aEvent) {
SetState(STATE_ESTIMATING);
mEndpointer.SetEnvironmentEstimationMode();
mEstimationSamples +=
ProcessAudioSegment(aEvent->mAudioSegment, aEvent->mTrackRate);
DispatchTrustedEvent(u"audiostart"_ns);
if (mCurrentState == STATE_ESTIMATING) {
DispatchTrustedEvent(u"start"_ns);
}
}
void SpeechRecognition::StopRecordingAndRecognize(SpeechEvent* aEvent) {
SetState(STATE_WAITING_FOR_RESULT);
MOZ_ASSERT(mRecognitionService, "Service deleted before recording done");
// This will run SoundEnd on the service just before StopRecording begins
// shutting the encode thread down.
mSpeechListener->mRemovedPromise->Then(
GetCurrentSerialEventTarget(), __func__,
[service = mRecognitionService] { service->SoundEnd(); });
StopRecording();
}
void SpeechRecognition::WaitForEstimation(SpeechEvent* aEvent) {
SetState(STATE_ESTIMATING);
mEstimationSamples +=
ProcessAudioSegment(aEvent->mAudioSegment, aEvent->mTrackRate);
if (mEstimationSamples > kESTIMATION_SAMPLES) {
mEndpointer.SetUserInputMode();
SetState(STATE_WAITING_FOR_SPEECH);
}
}
void SpeechRecognition::DetectSpeech(SpeechEvent* aEvent) {
SetState(STATE_WAITING_FOR_SPEECH);
ProcessAudioSegment(aEvent->mAudioSegment, aEvent->mTrackRate);
if (mEndpointer.DidStartReceivingSpeech()) {
mSpeechDetectionTimer->Cancel();
SetState(STATE_RECOGNIZING);
DispatchTrustedEvent(u"speechstart"_ns);
}
}
void SpeechRecognition::WaitForSpeechEnd(SpeechEvent* aEvent) {
SetState(STATE_RECOGNIZING);
ProcessAudioSegment(aEvent->mAudioSegment, aEvent->mTrackRate);
if (mEndpointer.speech_input_complete()) {
DispatchTrustedEvent(u"speechend"_ns);
if (mCurrentState == STATE_RECOGNIZING) {
// FIXME: StopRecordingAndRecognize should only be called for single
// shot services for continuous we should just inform the service
StopRecordingAndRecognize(aEvent);
}
}
}
void SpeechRecognition::NotifyFinalResult(SpeechEvent* aEvent) {
ResetAndEnd();
RootedDictionary<SpeechRecognitionEventInit> init(RootingCx());
init.mBubbles = true;
init.mCancelable = false;
// init.mResultIndex = 0;
init.mResults = aEvent->mRecognitionResultList;
init.mInterpretation = JS::NullValue();
// init.mEmma = nullptr;
RefPtr<SpeechRecognitionEvent> event =
SpeechRecognitionEvent::Constructor(this, u"result"_ns, init);
event->SetTrusted(true);
DispatchEvent(*event);
}
void SpeechRecognition::DoNothing(SpeechEvent* aEvent) {}
void SpeechRecognition::AbortSilently(SpeechEvent* aEvent) {
if (mRecognitionService) {
if (mTrack) {
// This will run Abort on the service just before StopRecording begins
// shutting the encode thread down.
mSpeechListener->mRemovedPromise->Then(
GetCurrentSerialEventTarget(), __func__,
[service = mRecognitionService] { service->Abort(); });
} else {
// Recording hasn't started yet. We can just call Abort().
mRecognitionService->Abort();
}
}
StopRecording()->Then(
GetCurrentSerialEventTarget(), __func__,
[self = RefPtr<SpeechRecognition>(this), this] { ResetAndEnd(); });
SetState(STATE_ABORTING);
}
void SpeechRecognition::AbortError(SpeechEvent* aEvent) {
AbortSilently(aEvent);
NotifyError(aEvent);
}
void SpeechRecognition::NotifyError(SpeechEvent* aEvent) {
aEvent->mError->SetTrusted(true);
DispatchEvent(*aEvent->mError);
}
/**************************************
* Event triggers and other functions *
**************************************/
NS_IMETHODIMP
SpeechRecognition::StartRecording(RefPtr<AudioStreamTrack>& aTrack) {
// hold a reference so that the underlying track doesn't get collected.
mTrack = aTrack;
MOZ_ASSERT(!mTrack->Ended());
mSpeechListener = SpeechTrackListener::Create(this);
mTrack->AddListener(mSpeechListener);
nsString blockerName;
blockerName.AppendPrintf("SpeechRecognition %p shutdown", this);
mShutdownBlocker =
MakeAndAddRef<SpeechRecognitionShutdownBlocker>(this, blockerName);
media::MustGetShutdownBarrier()->AddBlocker(
mShutdownBlocker, NS_LITERAL_STRING_FROM_CSTRING(__FILE__), __LINE__,
u"SpeechRecognition shutdown"_ns);
mEndpointer.StartSession();
return mSpeechDetectionTimer->Init(this, mSpeechDetectionTimeoutMs,
nsITimer::TYPE_ONE_SHOT);
}
RefPtr<GenericNonExclusivePromise> SpeechRecognition::StopRecording() {
if (!mTrack) {
// Recording wasn't started, or has already been stopped.
if (mStream) {
// Ensure we don't start recording because a track became available
// before we get reset.
mStream->UnregisterTrackListener(this);
}
return GenericNonExclusivePromise::CreateAndResolve(true, __func__);
}
if (mStopRecordingPromise) {
return mStopRecordingPromise;
}
mTrack->RemoveListener(mSpeechListener);
if (mTrackIsOwned) {
mTrack->Stop();
}
mEndpointer.EndSession();
DispatchTrustedEvent(u"audioend"_ns);
// Block shutdown until the speech track listener has been removed from the
// MSG, as it holds a reference to us, and we reference the world, which we
// don't want to leak.
mStopRecordingPromise =
mSpeechListener->mRemovedPromise
->Then(
GetCurrentSerialEventTarget(), __func__,
[self = RefPtr<SpeechRecognition>(this), this] {
SR_LOG("Shutting down encoding thread");
return mEncodeTaskQueue->BeginShutdown();
},
[] {
MOZ_CRASH("Unexpected rejection");
return ShutdownPromise::CreateAndResolve(false, __func__);
})
->Then(
GetCurrentSerialEventTarget(), __func__,
[self = RefPtr<SpeechRecognition>(this), this] {
media::MustGetShutdownBarrier()->RemoveBlocker(
mShutdownBlocker);
mShutdownBlocker = nullptr;
MOZ_DIAGNOSTIC_ASSERT(mCurrentState != STATE_IDLE);
return GenericNonExclusivePromise::CreateAndResolve(true,
__func__);
},
[] {
MOZ_CRASH("Unexpected rejection");
return GenericNonExclusivePromise::CreateAndResolve(false,
__func__);
});
return mStopRecordingPromise;
}
NS_IMETHODIMP
SpeechRecognition::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
MOZ_ASSERT(NS_IsMainThread(), "Observer invoked off the main thread");
if (!strcmp(aTopic, NS_TIMER_CALLBACK_TOPIC) &&
StateBetween(STATE_IDLE, STATE_WAITING_FOR_SPEECH)) {
DispatchError(SpeechRecognition::EVENT_AUDIO_ERROR,
SpeechRecognitionErrorCode::No_speech,
"No speech detected (timeout)");
} else if (!strcmp(aTopic, SPEECH_RECOGNITION_TEST_END_TOPIC)) {
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC);
obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC);
} else if (StaticPrefs::media_webspeech_test_fake_fsm_events() &&
!strcmp(aTopic, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC)) {
ProcessTestEventRequest(aSubject, nsDependentString(aData));
}
return NS_OK;
}
void SpeechRecognition::ProcessTestEventRequest(nsISupports* aSubject,
const nsAString& aEventName) {
if (aEventName.EqualsLiteral("EVENT_ABORT")) {
Abort();
} else if (aEventName.EqualsLiteral("EVENT_AUDIO_ERROR")) {
DispatchError(
SpeechRecognition::EVENT_AUDIO_ERROR,
SpeechRecognitionErrorCode::Audio_capture, // TODO different codes?
"AUDIO_ERROR test event");
} else {
NS_ASSERTION(StaticPrefs::media_webspeech_test_fake_recognition_service(),
"Got request for fake recognition service event, but "
"media.webspeech.test.fake_recognition_service is unset");
// let the fake recognition service handle the request
}
}
already_AddRefed<SpeechGrammarList> SpeechRecognition::Grammars() const {
RefPtr<SpeechGrammarList> speechGrammarList = mSpeechGrammarList;
return speechGrammarList.forget();
}
void SpeechRecognition::SetGrammars(SpeechGrammarList& aArg) {
mSpeechGrammarList = &aArg;
}
void SpeechRecognition::GetLang(nsString& aRetVal) const { aRetVal = mLang; }
void SpeechRecognition::SetLang(const nsAString& aArg) { mLang = aArg; }
bool SpeechRecognition::GetContinuous(ErrorResult& aRv) const {
return mContinuous;
}
void SpeechRecognition::SetContinuous(bool aArg, ErrorResult& aRv) {
mContinuous = aArg;
}
bool SpeechRecognition::InterimResults() const { return mInterimResults; }
void SpeechRecognition::SetInterimResults(bool aArg) { mInterimResults = aArg; }
uint32_t SpeechRecognition::MaxAlternatives() const { return mMaxAlternatives; }
void SpeechRecognition::SetMaxAlternatives(uint32_t aArg) {
mMaxAlternatives = aArg;
}
void SpeechRecognition::GetServiceURI(nsString& aRetVal,
ErrorResult& aRv) const {
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
}
void SpeechRecognition::SetServiceURI(const nsAString& aArg, ErrorResult& aRv) {
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
}
void SpeechRecognition::Start(const Optional<NonNull<DOMMediaStream>>& aStream,
CallerType aCallerType, ErrorResult& aRv) {
if (mCurrentState != STATE_IDLE) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return;
}
if (!SetRecognitionService(aRv)) {
return;
}
if (!ValidateAndSetGrammarList(aRv)) {
return;
}
mEncodeTaskQueue =
TaskQueue::Create(GetMediaThreadPool(MediaThreadType::WEBRTC_WORKER),
"WebSpeechEncoderThread");
nsresult rv;
rv = mRecognitionService->Initialize(this);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
MediaStreamConstraints constraints;
constraints.mAudio.SetAsBoolean() = true;
if (aStream.WasPassed()) {
mStream = &aStream.Value();
mTrackIsOwned = false;
mStream->RegisterTrackListener(this);
nsTArray<RefPtr<AudioStreamTrack>> tracks;
mStream->GetAudioTracks(tracks);
for (const RefPtr<AudioStreamTrack>& track : tracks) {
if (!track->Ended()) {
NotifyTrackAdded(track);
break;
}
}
} else {
mTrackIsOwned = true;
nsPIDOMWindowInner* win = GetOwner();
if (!win || !win->IsFullyActive()) {
aRv.ThrowInvalidStateError("The document is not fully active.");
return;
}
AutoNoJSAPI nojsapi;
RefPtr<SpeechRecognition> self(this);
MediaManager::Get()
->GetUserMedia(win, constraints, aCallerType)
->Then(
GetCurrentSerialEventTarget(), __func__,
[this, self,
generation = mStreamGeneration](RefPtr<DOMMediaStream>&& aStream) {
nsTArray<RefPtr<AudioStreamTrack>> tracks;
aStream->GetAudioTracks(tracks);
if (mAborted || mCurrentState != STATE_STARTING ||
mStreamGeneration != generation) {
// We were probably aborted. Exit early.
for (const RefPtr<AudioStreamTrack>& track : tracks) {
track->Stop();
}
return;
}
mStream = std::move(aStream);
mStream->RegisterTrackListener(this);
for (const RefPtr<AudioStreamTrack>& track : tracks) {
if (!track->Ended()) {
NotifyTrackAdded(track);
}
}
},
[this, self,
generation = mStreamGeneration](RefPtr<MediaMgrError>&& error) {
if (mAborted || mCurrentState != STATE_STARTING ||
mStreamGeneration != generation) {
// We were probably aborted. Exit early.
return;
}
SpeechRecognitionErrorCode errorCode;
if (error->mName == MediaMgrError::Name::NotAllowedError) {
errorCode = SpeechRecognitionErrorCode::Not_allowed;
} else {
errorCode = SpeechRecognitionErrorCode::Audio_capture;
}
DispatchError(SpeechRecognition::EVENT_AUDIO_ERROR, errorCode,
error->mMessage);
});
}
RefPtr<SpeechEvent> event = new SpeechEvent(this, EVENT_START);
NS_DispatchToMainThread(event);
}
bool SpeechRecognition::SetRecognitionService(ErrorResult& aRv) {
if (!GetOwner()) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return false;
}
// See:
// https://dvcs.w3.org/hg/speech-api/raw-file/tip/webspeechapi.html#dfn-lang
nsAutoString lang;
if (!mLang.IsEmpty()) {
lang = mLang;
} else {
nsCOMPtr<Document> document = GetOwner()->GetExtantDoc();
if (!document) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return false;
}
nsCOMPtr<Element> element = document->GetRootElement();
if (!element) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return false;
}
nsAutoString lang;
element->GetLang(lang);
}
auto result = CreateSpeechRecognitionService(GetOwner(), this, lang);
if (result.isErr()) {
switch (result.unwrapErr()) {
case ServiceCreationError::ServiceNotFound:
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
break;
default:
MOZ_CRASH("Unknown error");
}
return false;
}
mRecognitionService = result.unwrap();
MOZ_DIAGNOSTIC_ASSERT(mRecognitionService);
return true;
}
bool SpeechRecognition::ValidateAndSetGrammarList(ErrorResult& aRv) {
if (!mSpeechGrammarList) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return false;
}
uint32_t grammarListLength = mSpeechGrammarList->Length();
for (uint32_t count = 0; count < grammarListLength; ++count) {
RefPtr<SpeechGrammar> speechGrammar = mSpeechGrammarList->Item(count, aRv);
if (aRv.Failed()) {
return false;
}
if (NS_FAILED(mRecognitionService->ValidateAndSetGrammarList(
speechGrammar.get(), nullptr))) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return false;
}
}
return true;
}
void SpeechRecognition::Stop() {
RefPtr<SpeechEvent> event = new SpeechEvent(this, EVENT_STOP);
NS_DispatchToMainThread(event);
}
void SpeechRecognition::Abort() {
if (mAborted) {
return;
}
mAborted = true;
RefPtr<SpeechEvent> event = new SpeechEvent(this, EVENT_ABORT);
NS_DispatchToMainThread(event);
}
void SpeechRecognition::NotifyTrackAdded(
const RefPtr<MediaStreamTrack>& aTrack) {
if (mTrack) {
return;
}
RefPtr<AudioStreamTrack> audioTrack = aTrack->AsAudioStreamTrack();
if (!audioTrack) {
return;
}
if (audioTrack->Ended()) {
return;
}
StartRecording(audioTrack);
}
void SpeechRecognition::DispatchError(EventType aErrorType,
SpeechRecognitionErrorCode aErrorCode,
const nsACString& aMessage) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aErrorType == EVENT_RECOGNITIONSERVICE_ERROR ||
aErrorType == EVENT_AUDIO_ERROR,
"Invalid error type!");
RefPtr<SpeechRecognitionError> srError =
new SpeechRecognitionError(nullptr, nullptr, nullptr);
srError->InitSpeechRecognitionError(u"error"_ns, true, false, aErrorCode,
aMessage);
RefPtr<SpeechEvent> event = new SpeechEvent(this, aErrorType);
event->mError = srError;
NS_DispatchToMainThread(event);
}
/*
* Buffer audio samples into mAudioSamplesBuffer until aBufferSize.
* Updates mBufferedSamples and returns the number of samples that were
* buffered.
*/
uint32_t SpeechRecognition::FillSamplesBuffer(const int16_t* aSamples,
uint32_t aSampleCount) {
MOZ_ASSERT(mBufferedSamples < mAudioSamplesPerChunk);
MOZ_ASSERT(mAudioSamplesBuffer);
int16_t* samplesBuffer = static_cast<int16_t*>(mAudioSamplesBuffer->Data());
size_t samplesToCopy =
std::min(aSampleCount, mAudioSamplesPerChunk - mBufferedSamples);
PodCopy(samplesBuffer + mBufferedSamples, aSamples, samplesToCopy);
mBufferedSamples += samplesToCopy;
return samplesToCopy;
}
/*
* Split a samples buffer starting of a given size into
* chunks of equal size. The chunks are stored in the array
* received as argument.
* Returns the offset of the end of the last chunk that was
* created.
*/
uint32_t SpeechRecognition::SplitSamplesBuffer(
const int16_t* aSamplesBuffer, uint32_t aSampleCount,
nsTArray<RefPtr<SharedBuffer>>& aResult) {
uint32_t chunkStart = 0;
while (chunkStart + mAudioSamplesPerChunk <= aSampleCount) {
CheckedInt<size_t> bufferSize(sizeof(int16_t));
bufferSize *= mAudioSamplesPerChunk;
RefPtr<SharedBuffer> chunk = SharedBuffer::Create(bufferSize);
PodCopy(static_cast<short*>(chunk->Data()), aSamplesBuffer + chunkStart,
mAudioSamplesPerChunk);
aResult.AppendElement(chunk.forget());
chunkStart += mAudioSamplesPerChunk;
}
return chunkStart;
}
AudioSegment* SpeechRecognition::CreateAudioSegment(
nsTArray<RefPtr<SharedBuffer>>& aChunks) {
AudioSegment* segment = new AudioSegment();
for (uint32_t i = 0; i < aChunks.Length(); ++i) {
RefPtr<SharedBuffer> buffer = aChunks[i];
const int16_t* chunkData = static_cast<const int16_t*>(buffer->Data());
AutoTArray<const int16_t*, 1> channels;
channels.AppendElement(chunkData);
segment->AppendFrames(buffer.forget(), channels, mAudioSamplesPerChunk,
PRINCIPAL_HANDLE_NONE);
}
return segment;
}
void SpeechRecognition::FeedAudioData(
nsMainThreadPtrHandle<SpeechRecognition>& aRecognition,
already_AddRefed<SharedBuffer> aSamples, uint32_t aDuration,
MediaTrackListener* aProvider, TrackRate aTrackRate) {
NS_ASSERTION(!NS_IsMainThread(),
"FeedAudioData should not be called in the main thread");
// Endpointer expects to receive samples in chunks whose size is a
// multiple of its frame size.
// Since we can't assume we will receive the frames in appropriate-sized
// chunks, we must buffer and split them in chunks of mAudioSamplesPerChunk
// (a multiple of Endpointer's frame size) before feeding to Endpointer.
// ensure aSamples is deleted
RefPtr<SharedBuffer> refSamples = aSamples;
uint32_t samplesIndex = 0;
const int16_t* samples = static_cast<int16_t*>(refSamples->Data());
AutoTArray<RefPtr<SharedBuffer>, 5> chunksToSend;
// fill up our buffer and make a chunk out of it, if possible
if (mBufferedSamples > 0) {
samplesIndex += FillSamplesBuffer(samples, aDuration);
if (mBufferedSamples == mAudioSamplesPerChunk) {
chunksToSend.AppendElement(mAudioSamplesBuffer.forget());
mBufferedSamples = 0;
}
}
// create sample chunks of correct size
if (samplesIndex < aDuration) {
samplesIndex += SplitSamplesBuffer(samples + samplesIndex,
aDuration - samplesIndex, chunksToSend);
}
// buffer remaining samples
if (samplesIndex < aDuration) {
mBufferedSamples = 0;
CheckedInt<size_t> bufferSize(sizeof(int16_t));
bufferSize *= mAudioSamplesPerChunk;
mAudioSamplesBuffer = SharedBuffer::Create(bufferSize);
FillSamplesBuffer(samples + samplesIndex, aDuration - samplesIndex);
}
AudioSegment* segment = CreateAudioSegment(chunksToSend);
RefPtr<SpeechEvent> event = new SpeechEvent(aRecognition, EVENT_AUDIO_DATA);
event->mAudioSegment = segment;
event->mProvider = aProvider;
event->mTrackRate = aTrackRate;
NS_DispatchToMainThread(event);
}
const char* SpeechRecognition::GetName(FSMState aId) {
static const char* names[] = {
"STATE_IDLE", "STATE_STARTING",
"STATE_ESTIMATING", "STATE_WAITING_FOR_SPEECH",
"STATE_RECOGNIZING", "STATE_WAITING_FOR_RESULT",
"STATE_ABORTING",
};
MOZ_ASSERT(aId < STATE_COUNT);
MOZ_ASSERT(ArrayLength(names) == STATE_COUNT);
return names[aId];
}
const char* SpeechRecognition::GetName(SpeechEvent* aEvent) {
static const char* names[] = {"EVENT_START",
"EVENT_STOP",
"EVENT_ABORT",
"EVENT_AUDIO_DATA",
"EVENT_AUDIO_ERROR",
"EVENT_RECOGNITIONSERVICE_INTERMEDIATE_RESULT",
"EVENT_RECOGNITIONSERVICE_FINAL_RESULT",
"EVENT_RECOGNITIONSERVICE_ERROR"};
MOZ_ASSERT(aEvent->mType < EVENT_COUNT);
MOZ_ASSERT(ArrayLength(names) == EVENT_COUNT);
return names[aEvent->mType];
}
TaskQueue* SpeechRecognition::GetTaskQueueForEncoding() const {
MOZ_ASSERT(NS_IsMainThread());
return mEncodeTaskQueue;
}
SpeechEvent::SpeechEvent(SpeechRecognition* aRecognition,
SpeechRecognition::EventType aType)
: Runnable("dom::SpeechEvent"),
mAudioSegment(nullptr),
mRecognitionResultList(nullptr),
mError(nullptr),
mRecognition(new nsMainThreadPtrHolder<SpeechRecognition>(
"SpeechEvent::SpeechEvent", aRecognition)),
mType(aType),
mTrackRate(0) {}
SpeechEvent::SpeechEvent(nsMainThreadPtrHandle<SpeechRecognition>& aRecognition,
SpeechRecognition::EventType aType)
: Runnable("dom::SpeechEvent"),
mAudioSegment(nullptr),
mRecognitionResultList(nullptr),
mError(nullptr),
mRecognition(aRecognition),
mType(aType),
mTrackRate(0) {}
SpeechEvent::~SpeechEvent() { delete mAudioSegment; }
NS_IMETHODIMP
SpeechEvent::Run() {
mRecognition->ProcessEvent(this);
return NS_OK;
}
} // namespace mozilla::dom
|