summaryrefslogtreecommitdiffstats
path: root/dom/media/webspeech/synth/test/nsFakeSynthServices.cpp
blob: 075e8aa87892cc66fb4b64313edd46b46a42d806 (plain)
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
/* -*- 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 "nsISupports.h"
#include "nsFakeSynthServices.h"
#include "nsPrintfCString.h"
#include "SharedBuffer.h"

#include "mozilla/ClearOnShutdown.h"
#include "mozilla/dom/nsSynthVoiceRegistry.h"
#include "mozilla/dom/nsSpeechTask.h"

#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"
#include "prenv.h"
#include "mozilla/Preferences.h"
#include "mozilla/DebugOnly.h"

#define CHANNELS 1
#define SAMPLERATE 1600

namespace mozilla::dom {

StaticRefPtr<nsFakeSynthServices> nsFakeSynthServices::sSingleton;

enum VoiceFlags {
  eSuppressEvents = 1,
  eSuppressEnd = 2,
  eFailAtStart = 4,
  eFail = 8
};

struct VoiceDetails {
  const char* uri;
  const char* name;
  const char* lang;
  bool defaultVoice;
  uint32_t flags;
};

static const VoiceDetails sVoices[] = {
    {"urn:moz-tts:fake:bob", "Bob Marley", "en-JM", true, 0},
    {"urn:moz-tts:fake:amy", "Amy Winehouse", "en-GB", false, 0},
    {"urn:moz-tts:fake:lenny", "Leonard Cohen", "en-CA", false, 0},
    {"urn:moz-tts:fake:celine", "Celine Dion", "fr-CA", false, 0},
    {
        "urn:moz-tts:fake:julie",
        "Julieta Venegas",
        "es-MX",
        false,
    },
    {"urn:moz-tts:fake:zanetta", "Zanetta Farussi", "it-IT", false, 0},
    {"urn:moz-tts:fake:margherita", "Margherita Durastanti",
     "it-IT-noevents-noend", false, eSuppressEvents | eSuppressEnd},
    {"urn:moz-tts:fake:teresa", "Teresa Cornelys", "it-IT-noend", false,
     eSuppressEnd},
    {"urn:moz-tts:fake:cecilia", "Cecilia Bartoli", "it-IT-failatstart", false,
     eFailAtStart},
    {"urn:moz-tts:fake:gottardo", "Gottardo Aldighieri", "it-IT-fail", false,
     eFail},
};

// FakeSynthCallback
class FakeSynthCallback : public nsISpeechTaskCallback {
 public:
  explicit FakeSynthCallback(nsISpeechTask* aTask) : mTask(aTask) {}
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(FakeSynthCallback,
                                           nsISpeechTaskCallback)

  NS_IMETHOD OnPause() override {
    if (mTask) {
      mTask->DispatchPause(1.5, 1);
    }

    return NS_OK;
  }

  NS_IMETHOD OnResume() override {
    if (mTask) {
      mTask->DispatchResume(1.5, 1);
    }

    return NS_OK;
  }

  NS_IMETHOD OnCancel() override {
    if (mTask) {
      mTask->DispatchEnd(1.5, 1);
    }

    return NS_OK;
  }

  NS_IMETHOD OnVolumeChanged(float aVolume) override { return NS_OK; }

 private:
  virtual ~FakeSynthCallback() = default;

  nsCOMPtr<nsISpeechTask> mTask;
};

NS_IMPL_CYCLE_COLLECTION(FakeSynthCallback, mTask);

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FakeSynthCallback)
  NS_INTERFACE_MAP_ENTRY(nsISpeechTaskCallback)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISpeechTaskCallback)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(FakeSynthCallback)
NS_IMPL_CYCLE_COLLECTING_RELEASE(FakeSynthCallback)

// FakeSpeechSynth

class FakeSpeechSynth : public nsISpeechService {
 public:
  FakeSpeechSynth() = default;

  NS_DECL_ISUPPORTS
  NS_DECL_NSISPEECHSERVICE

 private:
  virtual ~FakeSpeechSynth() = default;
};

NS_IMPL_ISUPPORTS(FakeSpeechSynth, nsISpeechService)

NS_IMETHODIMP
FakeSpeechSynth::Speak(const nsAString& aText, const nsAString& aUri,
                       float aVolume, float aRate, float aPitch,
                       nsISpeechTask* aTask) {
  class DispatchStart final : public Runnable {
   public:
    explicit DispatchStart(nsISpeechTask* aTask)
        : mozilla::Runnable("DispatchStart"), mTask(aTask) {}

    NS_IMETHOD Run() override {
      mTask->DispatchStart();

      return NS_OK;
    }

   private:
    nsCOMPtr<nsISpeechTask> mTask;
  };

  class DispatchEnd final : public Runnable {
   public:
    DispatchEnd(nsISpeechTask* aTask, const nsAString& aText)
        : mozilla::Runnable("DispatchEnd"), mTask(aTask), mText(aText) {}

    NS_IMETHOD Run() override {
      mTask->DispatchEnd(mText.Length() / 2, mText.Length());

      return NS_OK;
    }

   private:
    nsCOMPtr<nsISpeechTask> mTask;
    nsString mText;
  };

  class DispatchError final : public Runnable {
   public:
    DispatchError(nsISpeechTask* aTask, const nsAString& aText)
        : mozilla::Runnable("DispatchError"), mTask(aTask), mText(aText) {}

    NS_IMETHOD Run() override {
      mTask->DispatchError(mText.Length() / 2, mText.Length());

      return NS_OK;
    }

   private:
    nsCOMPtr<nsISpeechTask> mTask;
    nsString mText;
  };

  uint32_t flags = 0;
  for (VoiceDetails voice : sVoices) {
    if (aUri.EqualsASCII(voice.uri)) {
      flags = voice.flags;
      break;
    }
  }

  if (flags & eFailAtStart) {
    return NS_ERROR_FAILURE;
  }

  RefPtr<FakeSynthCallback> cb =
      new FakeSynthCallback((flags & eSuppressEvents) ? nullptr : aTask);

  aTask->Setup(cb);

  nsCOMPtr<nsIRunnable> runnable = new DispatchStart(aTask);
  NS_DispatchToMainThread(runnable);

  if (flags & eFail) {
    runnable = new DispatchError(aTask, aText);
    NS_DispatchToMainThread(runnable);
  } else if ((flags & eSuppressEnd) == 0) {
    runnable = new DispatchEnd(aTask, aText);
    NS_DispatchToMainThread(runnable);
  }

  return NS_OK;
}

// nsFakeSynthService

NS_INTERFACE_MAP_BEGIN(nsFakeSynthServices)
  NS_INTERFACE_MAP_ENTRY(nsIObserver)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(nsFakeSynthServices)
NS_IMPL_RELEASE(nsFakeSynthServices)

static void AddVoices(nsISpeechService* aService, const VoiceDetails* aVoices,
                      uint32_t aLength) {
  RefPtr<nsSynthVoiceRegistry> registry = nsSynthVoiceRegistry::GetInstance();
  for (uint32_t i = 0; i < aLength; i++) {
    NS_ConvertUTF8toUTF16 name(aVoices[i].name);
    NS_ConvertUTF8toUTF16 uri(aVoices[i].uri);
    NS_ConvertUTF8toUTF16 lang(aVoices[i].lang);
    // These services can handle more than one utterance at a time and have
    // several speaking simultaniously. So, aQueuesUtterances == false
    registry->AddVoice(aService, uri, name, lang, true, false);
    if (aVoices[i].defaultVoice) {
      registry->SetDefaultVoice(uri, true);
    }
  }

  registry->NotifyVoicesChanged();
}

void nsFakeSynthServices::Init() {
  mSynthService = new FakeSpeechSynth();
  AddVoices(mSynthService, sVoices, ArrayLength(sVoices));
}

// nsIObserver

NS_IMETHODIMP
nsFakeSynthServices::Observe(nsISupports* aSubject, const char* aTopic,
                             const char16_t* aData) {
  MOZ_ASSERT(NS_IsMainThread());
  if (NS_WARN_IF(!(!strcmp(aTopic, "speech-synth-started")))) {
    return NS_ERROR_UNEXPECTED;
  }

  if (Preferences::GetBool("media.webspeech.synth.test")) {
    NS_DispatchToMainThread(NewRunnableMethod(
        "dom::nsFakeSynthServices::Init", this, &nsFakeSynthServices::Init));
  }

  return NS_OK;
}

// static methods

nsFakeSynthServices* nsFakeSynthServices::GetInstance() {
  MOZ_ASSERT(NS_IsMainThread());
  if (!XRE_IsParentProcess()) {
    MOZ_ASSERT(false,
               "nsFakeSynthServices can only be started on main gecko process");
    return nullptr;
  }

  if (!sSingleton) {
    sSingleton = new nsFakeSynthServices();
    ClearOnShutdown(&sSingleton);
  }

  return sSingleton;
}

already_AddRefed<nsFakeSynthServices>
nsFakeSynthServices::GetInstanceForService() {
  RefPtr<nsFakeSynthServices> picoService = GetInstance();
  return picoService.forget();
}

}  // namespace mozilla::dom