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
content / browser / speech / tts_android.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/tts_android.h"
#include <string>
#include "base/android/jni_string.h"
#include "base/functional/bind.h"
#include "base/memory/singleton.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/speech/tts_controller_impl.h"
#include "content/browser/speech/tts_environment_android_impl.h"
#include "content/common/buildflags.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "content/public/android/content_jni_headers/TtsPlatformImpl_jni.h"
using base::android::AttachCurrentThread;
using base::android::JavaParamRef;
namespace content {
TtsPlatformImplAndroid::TtsPlatformImplAndroid() : utterance_id_(0) {
environment_android_ =
GetContentClient()->browser()->CreateTtsEnvironmentAndroid();
if (!environment_android_)
environment_android_ = std::make_unique<TtsEnvironmentAndroidImpl>();
TtsControllerImpl::GetInstance()->SetStopSpeakingWhenHidden(
!environment_android_->CanSpeakUtterancesFromHiddenWebContents());
environment_android_->SetCanSpeakNowChangedCallback(base::BindRepeating(
&TtsPlatformImplAndroid::OnCanSpeakNowChanged, base::Unretained(this)));
JNIEnv* env = AttachCurrentThread();
java_ref_.Reset(
Java_TtsPlatformImpl_create(env, reinterpret_cast<intptr_t>(this)));
}
TtsPlatformImplAndroid::~TtsPlatformImplAndroid() {
JNIEnv* env = AttachCurrentThread();
Java_TtsPlatformImpl_destroy(env, java_ref_);
}
bool TtsPlatformImplAndroid::PlatformImplSupported() {
return true;
}
bool TtsPlatformImplAndroid::PlatformImplInitialized() {
return true;
}
void TtsPlatformImplAndroid::Speak(
int utterance_id,
const std::string& utterance,
const std::string& lang,
const VoiceData& voice,
const UtteranceContinuousParameters& params,
base::OnceCallback<void(bool)> did_start_speaking_callback) {
// Parse SSML and process speech.
TtsController::GetInstance()->StripSSML(
utterance,
base::BindOnce(&TtsPlatformImplAndroid::ProcessSpeech,
weak_factory_.GetWeakPtr(), utterance_id, lang, voice,
params, std::move(did_start_speaking_callback)));
}
void TtsPlatformImplAndroid::ProcessSpeech(
int utterance_id,
const std::string& lang,
const VoiceData& voice,
const UtteranceContinuousParameters& params,
base::OnceCallback<void(bool)> did_start_speaking_callback,
const std::string& parsed_utterance) {
std::move(did_start_speaking_callback)
.Run(StartSpeakingNow(utterance_id, lang, params, parsed_utterance,
voice.engine_id));
}
bool TtsPlatformImplAndroid::StartSpeakingNow(
int utterance_id,
const std::string& lang,
const UtteranceContinuousParameters& params,
const std::string& parsed_utterance,
const std::string& engine_id) {
if (!environment_android_->CanSpeakNow())
return false;
JNIEnv* env = AttachCurrentThread();
const bool did_start = Java_TtsPlatformImpl_speak(
env, java_ref_, utterance_id,
base::android::ConvertUTF8ToJavaString(env, parsed_utterance),
base::android::ConvertUTF8ToJavaString(env, lang),
base::android::ConvertUTF8ToJavaString(env, engine_id), params.rate,
params.pitch, params.volume);
if (!did_start)
return false;
utterance_ = parsed_utterance;
utterance_id_ = utterance_id;
return true;
}
bool TtsPlatformImplAndroid::StopSpeaking() {
JNIEnv* env = AttachCurrentThread();
Java_TtsPlatformImpl_stop(env, java_ref_);
utterance_id_ = 0;
utterance_.clear();
return true;
}
void TtsPlatformImplAndroid::Pause() {
StopSpeaking();
}
void TtsPlatformImplAndroid::Resume() {}
bool TtsPlatformImplAndroid::IsSpeaking() {
return (utterance_id_ != 0);
}
void TtsPlatformImplAndroid::GetVoices(std::vector<VoiceData>* out_voices) {
JNIEnv* env = AttachCurrentThread();
if (!Java_TtsPlatformImpl_isInitialized(env, java_ref_))
return;
int count = Java_TtsPlatformImpl_getVoiceCount(env, java_ref_);
for (int i = 0; i < count; ++i) {
out_voices->push_back(VoiceData());
VoiceData& data = out_voices->back();
data.native = true;
data.name = base::android::ConvertJavaStringToUTF8(
Java_TtsPlatformImpl_getVoiceName(env, java_ref_, i));
data.lang = base::android::ConvertJavaStringToUTF8(
Java_TtsPlatformImpl_getVoiceLanguage(env, java_ref_, i));
data.events.insert(TTS_EVENT_START);
data.events.insert(TTS_EVENT_END);
data.events.insert(TTS_EVENT_ERROR);
}
}
void TtsPlatformImplAndroid::VoicesChanged(JNIEnv* env) {
TtsController::GetInstance()->VoicesChanged();
}
void TtsPlatformImplAndroid::OnEndEvent(JNIEnv* env,
jint utterance_id) {
SendFinalTtsEvent(utterance_id, TTS_EVENT_END,
static_cast<int>(utterance_.size()));
}
void TtsPlatformImplAndroid::OnErrorEvent(JNIEnv* env,
jint utterance_id) {
SendFinalTtsEvent(utterance_id, TTS_EVENT_ERROR, 0);
}
void TtsPlatformImplAndroid::OnStartEvent(JNIEnv* env,
jint utterance_id) {
if (utterance_id != utterance_id_)
return;
TtsController::GetInstance()->OnTtsEvent(utterance_id_, TTS_EVENT_START, 0,
utterance_.size(), std::string());
}
void TtsPlatformImplAndroid::SendFinalTtsEvent(int utterance_id,
TtsEventType event_type,
int char_index) {
if (utterance_id != utterance_id_)
return;
TtsController::GetInstance()->OnTtsEvent(utterance_id_, event_type,
char_index, -1, std::string());
utterance_id_ = 0;
utterance_.clear();
}
// static
TtsPlatformImplAndroid* TtsPlatformImplAndroid::GetInstance() {
return base::Singleton<
TtsPlatformImplAndroid,
base::LeakySingletonTraits<TtsPlatformImplAndroid>>::get();
}
// static
TtsPlatformImpl* TtsPlatformImpl::GetInstance() {
return TtsPlatformImplAndroid::GetInstance();
}
void TtsPlatformImplAndroid::OnCanSpeakNowChanged() {
if (!environment_android_->CanSpeakNow())
TtsController::GetInstance()->Stop();
}
} // namespace content