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
|
/* -*- 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 "AudioChannelService.h"
#include "AudioSegment.h"
#include "nsSpeechTask.h"
#include "nsSynthVoiceRegistry.h"
#include "nsXULAppAPI.h"
#include "SharedBuffer.h"
#include "SpeechSynthesis.h"
#undef LOG
extern mozilla::LogModule* GetSpeechSynthLog();
#define LOG(type, msg) MOZ_LOG(GetSpeechSynthLog(), type, msg)
#define AUDIO_TRACK 1
namespace mozilla::dom {
// nsSpeechTask
NS_IMPL_CYCLE_COLLECTION_WEAK(nsSpeechTask, mSpeechSynthesis, mUtterance,
mCallback)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsSpeechTask)
NS_INTERFACE_MAP_ENTRY(nsISpeechTask)
NS_INTERFACE_MAP_ENTRY(nsIAudioChannelAgentCallback)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISpeechTask)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsSpeechTask)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsSpeechTask)
nsSpeechTask::nsSpeechTask(SpeechSynthesisUtterance* aUtterance, bool aIsChrome)
: mUtterance(aUtterance),
mInited(false),
mPrePaused(false),
mPreCanceled(false),
mCallback(nullptr),
mIsChrome(aIsChrome),
mState(STATE_PENDING) {
mText = aUtterance->mText;
mVolume = aUtterance->Volume();
}
nsSpeechTask::nsSpeechTask(float aVolume, const nsAString& aText,
bool aIsChrome)
: mUtterance(nullptr),
mVolume(aVolume),
mText(aText),
mInited(false),
mPrePaused(false),
mPreCanceled(false),
mCallback(nullptr),
mIsChrome(aIsChrome),
mState(STATE_PENDING) {}
nsSpeechTask::~nsSpeechTask() { LOG(LogLevel::Debug, ("~nsSpeechTask")); }
void nsSpeechTask::Init() { mInited = true; }
void nsSpeechTask::SetChosenVoiceURI(const nsAString& aUri) {
mChosenVoiceURI = aUri;
}
NS_IMETHODIMP
nsSpeechTask::Setup(nsISpeechTaskCallback* aCallback) {
MOZ_ASSERT(XRE_IsParentProcess());
LOG(LogLevel::Debug, ("nsSpeechTask::Setup"));
mCallback = aCallback;
return NS_OK;
}
NS_IMETHODIMP
nsSpeechTask::DispatchStart() {
nsSynthVoiceRegistry::GetInstance()->SetIsSpeaking(true);
return DispatchStartImpl();
}
nsresult nsSpeechTask::DispatchStartImpl() {
return DispatchStartImpl(mChosenVoiceURI);
}
nsresult nsSpeechTask::DispatchStartImpl(const nsAString& aUri) {
LOG(LogLevel::Debug, ("nsSpeechTask::DispatchStartImpl"));
MOZ_ASSERT(mUtterance);
if (NS_WARN_IF(mState != STATE_PENDING)) {
return NS_ERROR_NOT_AVAILABLE;
}
CreateAudioChannelAgent();
mState = STATE_SPEAKING;
mUtterance->mChosenVoiceURI = aUri;
mUtterance->DispatchSpeechSynthesisEvent(u"start"_ns, 0, nullptr, 0, u""_ns);
return NS_OK;
}
NS_IMETHODIMP
nsSpeechTask::DispatchEnd(float aElapsedTime, uint32_t aCharIndex) {
// After we end, no callback functions should go through.
mCallback = nullptr;
if (!mPreCanceled) {
nsSynthVoiceRegistry::GetInstance()->SpeakNext();
}
return DispatchEndImpl(aElapsedTime, aCharIndex);
}
nsresult nsSpeechTask::DispatchEndImpl(float aElapsedTime,
uint32_t aCharIndex) {
LOG(LogLevel::Debug, ("nsSpeechTask::DispatchEndImpl"));
DestroyAudioChannelAgent();
MOZ_ASSERT(mUtterance);
if (NS_WARN_IF(mState == STATE_ENDED)) {
return NS_ERROR_NOT_AVAILABLE;
}
RefPtr<SpeechSynthesisUtterance> utterance = mUtterance;
if (mSpeechSynthesis) {
mSpeechSynthesis->OnEnd(this);
}
mState = STATE_ENDED;
utterance->DispatchSpeechSynthesisEvent(u"end"_ns, aCharIndex, nullptr,
aElapsedTime, u""_ns);
return NS_OK;
}
NS_IMETHODIMP
nsSpeechTask::DispatchPause(float aElapsedTime, uint32_t aCharIndex) {
return DispatchPauseImpl(aElapsedTime, aCharIndex);
}
nsresult nsSpeechTask::DispatchPauseImpl(float aElapsedTime,
uint32_t aCharIndex) {
LOG(LogLevel::Debug, ("nsSpeechTask::DispatchPauseImpl"));
MOZ_ASSERT(mUtterance);
if (NS_WARN_IF(mUtterance->mPaused)) {
return NS_ERROR_NOT_AVAILABLE;
}
if (NS_WARN_IF(mState == STATE_ENDED)) {
return NS_ERROR_NOT_AVAILABLE;
}
mUtterance->mPaused = true;
if (mState == STATE_SPEAKING) {
mUtterance->DispatchSpeechSynthesisEvent(u"pause"_ns, aCharIndex, nullptr,
aElapsedTime, u""_ns);
}
return NS_OK;
}
NS_IMETHODIMP
nsSpeechTask::DispatchResume(float aElapsedTime, uint32_t aCharIndex) {
return DispatchResumeImpl(aElapsedTime, aCharIndex);
}
nsresult nsSpeechTask::DispatchResumeImpl(float aElapsedTime,
uint32_t aCharIndex) {
LOG(LogLevel::Debug, ("nsSpeechTask::DispatchResumeImpl"));
MOZ_ASSERT(mUtterance);
if (NS_WARN_IF(!(mUtterance->mPaused))) {
return NS_ERROR_NOT_AVAILABLE;
}
if (NS_WARN_IF(mState == STATE_ENDED)) {
return NS_ERROR_NOT_AVAILABLE;
}
mUtterance->mPaused = false;
if (mState == STATE_SPEAKING) {
mUtterance->DispatchSpeechSynthesisEvent(u"resume"_ns, aCharIndex, nullptr,
aElapsedTime, u""_ns);
}
return NS_OK;
}
void nsSpeechTask::ForceError(float aElapsedTime, uint32_t aCharIndex) {
DispatchError(aElapsedTime, aCharIndex);
}
NS_IMETHODIMP
nsSpeechTask::DispatchError(float aElapsedTime, uint32_t aCharIndex) {
if (!mPreCanceled) {
nsSynthVoiceRegistry::GetInstance()->SpeakNext();
}
return DispatchErrorImpl(aElapsedTime, aCharIndex);
}
nsresult nsSpeechTask::DispatchErrorImpl(float aElapsedTime,
uint32_t aCharIndex) {
LOG(LogLevel::Debug, ("nsSpeechTask::DispatchErrorImpl"));
DestroyAudioChannelAgent();
MOZ_ASSERT(mUtterance);
if (NS_WARN_IF(mState == STATE_ENDED)) {
return NS_ERROR_NOT_AVAILABLE;
}
if (mSpeechSynthesis) {
mSpeechSynthesis->OnEnd(this);
}
mState = STATE_ENDED;
mUtterance->DispatchSpeechSynthesisEvent(u"error"_ns, aCharIndex, nullptr,
aElapsedTime, u""_ns);
return NS_OK;
}
NS_IMETHODIMP
nsSpeechTask::DispatchBoundary(const nsAString& aName, float aElapsedTime,
uint32_t aCharIndex, uint32_t aCharLength,
uint8_t argc) {
return DispatchBoundaryImpl(aName, aElapsedTime, aCharIndex, aCharLength,
argc);
}
nsresult nsSpeechTask::DispatchBoundaryImpl(const nsAString& aName,
float aElapsedTime,
uint32_t aCharIndex,
uint32_t aCharLength,
uint8_t argc) {
MOZ_ASSERT(mUtterance);
if (NS_WARN_IF(mState != STATE_SPEAKING)) {
return NS_ERROR_NOT_AVAILABLE;
}
mUtterance->DispatchSpeechSynthesisEvent(
u"boundary"_ns, aCharIndex,
argc ? static_cast<Nullable<uint32_t> >(aCharLength) : nullptr,
aElapsedTime, aName);
return NS_OK;
}
NS_IMETHODIMP
nsSpeechTask::DispatchMark(const nsAString& aName, float aElapsedTime,
uint32_t aCharIndex) {
return DispatchMarkImpl(aName, aElapsedTime, aCharIndex);
}
nsresult nsSpeechTask::DispatchMarkImpl(const nsAString& aName,
float aElapsedTime,
uint32_t aCharIndex) {
MOZ_ASSERT(mUtterance);
if (NS_WARN_IF(mState != STATE_SPEAKING)) {
return NS_ERROR_NOT_AVAILABLE;
}
mUtterance->DispatchSpeechSynthesisEvent(u"mark"_ns, aCharIndex, nullptr,
aElapsedTime, aName);
return NS_OK;
}
void nsSpeechTask::Pause() {
MOZ_ASSERT(XRE_IsParentProcess());
if (mCallback) {
DebugOnly<nsresult> rv = mCallback->OnPause();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Unable to call onPause() callback");
}
if (!mInited) {
mPrePaused = true;
}
}
void nsSpeechTask::Resume() {
MOZ_ASSERT(XRE_IsParentProcess());
if (mCallback) {
DebugOnly<nsresult> rv = mCallback->OnResume();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Unable to call onResume() callback");
}
if (mPrePaused) {
mPrePaused = false;
nsSynthVoiceRegistry::GetInstance()->ResumeQueue();
}
}
void nsSpeechTask::Cancel() {
MOZ_ASSERT(XRE_IsParentProcess());
LOG(LogLevel::Debug, ("nsSpeechTask::Cancel"));
if (mCallback) {
DebugOnly<nsresult> rv = mCallback->OnCancel();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Unable to call onCancel() callback");
}
if (!mInited) {
mPreCanceled = true;
}
}
void nsSpeechTask::ForceEnd() {
if (!mInited) {
mPreCanceled = true;
}
DispatchEnd(0, 0);
}
void nsSpeechTask::SetSpeechSynthesis(SpeechSynthesis* aSpeechSynthesis) {
mSpeechSynthesis = aSpeechSynthesis;
}
void nsSpeechTask::CreateAudioChannelAgent() {
if (!mUtterance) {
return;
}
if (mAudioChannelAgent) {
mAudioChannelAgent->NotifyStoppedPlaying();
}
mAudioChannelAgent = new AudioChannelAgent();
mAudioChannelAgent->InitWithWeakCallback(mUtterance->GetOwner(), this);
nsresult rv = mAudioChannelAgent->NotifyStartedPlaying(
AudioChannelService::AudibleState::eAudible);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
mAudioChannelAgent->PullInitialUpdate();
}
void nsSpeechTask::DestroyAudioChannelAgent() {
if (mAudioChannelAgent) {
mAudioChannelAgent->NotifyStoppedPlaying();
mAudioChannelAgent = nullptr;
}
}
NS_IMETHODIMP
nsSpeechTask::WindowVolumeChanged(float aVolume, bool aMuted) {
SetAudioOutputVolume(aMuted ? 0.0 : mVolume * aVolume);
return NS_OK;
}
NS_IMETHODIMP
nsSpeechTask::WindowSuspendChanged(nsSuspendedTypes aSuspend) {
if (!mUtterance) {
return NS_OK;
}
if (aSuspend == nsISuspendedTypes::NONE_SUSPENDED && mUtterance->mPaused) {
Resume();
} else if (aSuspend != nsISuspendedTypes::NONE_SUSPENDED &&
!mUtterance->mPaused) {
Pause();
}
return NS_OK;
}
NS_IMETHODIMP
nsSpeechTask::WindowAudioCaptureChanged(bool aCapture) {
// This is not supported yet.
return NS_OK;
}
void nsSpeechTask::SetAudioOutputVolume(float aVolume) {
if (mCallback) {
mCallback->OnVolumeChanged(aVolume);
}
}
} // namespace mozilla::dom
|