Skip to content

Commit efa2de7

Browse files
committed
Revert c++ exception code
1 parent 4c526ae commit efa2de7

File tree

4 files changed

+102
-118
lines changed

4 files changed

+102
-118
lines changed

packages/flutter_tts/tizen/inc/flutter_tts_tizen_plugin.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
2-
// Use of this source code is governed by a BSD-style license that can be
3-
// found in the LICENSE file.
4-
51
#ifndef FLUTTER_PLUGIN_FLUTTER_TTS_TIZEN_PLUGIN_H_
62
#define FLUTTER_PLUGIN_FLUTTER_TTS_TIZEN_PLUGIN_H_
73

packages/flutter_tts/tizen/src/flutter_tts_tizen_plugin.cc

Lines changed: 44 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
3636
});
3737

3838
tts_ = std::make_unique<TextToSpeech>();
39-
try {
40-
tts_->Initialize();
41-
} catch (const TextToSpeechError &error) {
39+
if (!tts_->Initialize()) {
4240
// TODO : Handle initialization failure cases
4341
// Rarely, initializing TextToSpeech can fail. we should consider catching
4442
// the exception and propagating it to the flutter side. however, I think
4543
// this is optional because flutter side is not expecting any errors.
46-
LOG_ERROR("Operation failed : %s", error.GetErrorString().c_str());
4744
tts_ = nullptr;
4845
return;
4946
}
@@ -89,6 +86,13 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
8986
void HandleMethodCall(
9087
const flutter::MethodCall<flutter::EncodableValue> &method_call,
9188
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
89+
// Keep in sync with the return values implemented in:
90+
// https://github.com/dlutton/flutter_tts/blob/master/android/src/main/java/com/tundralabs/fluttertts/FlutterTtsPlugin.java.
91+
// In principle, MethodResult was designed to call MethodResult.Error() to
92+
// notify the dart code of any method call failures from the host platform.
93+
// However, in the case of flutter_tts, it expects a return value 0 on
94+
// failure(and value 1 on success). Therefore in the scope of this plugin,
95+
// we call SendResult(flutter::EncodableValue(0)); to notify errors.
9296
const auto method_name = method_call.method_name();
9397
const auto &arguments = *method_call.arguments();
9498

@@ -128,62 +132,58 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
128132
SendResult(flutter::EncodableValue(1));
129133
return;
130134
}
131-
SendErrorResult("Invalid argument", "Argument is invaild.");
135+
SendResult(flutter::EncodableValue(0));
132136
}
133137

134138
void OnSpeak(const flutter::EncodableValue &arguments) {
135-
try {
136-
if (tts_->GetState() == TtsState::kPlaying) {
137-
SendErrorResult("Operation cancelled",
138-
"You cannot speak again while speaking.");
139-
return;
139+
TtsState state = tts_->GetState();
140+
if (state == TtsState::kPlaying || state == TtsState::kError) {
141+
if (state == TtsState::kPlaying) {
142+
LOG_ERROR("[TTS] : You cannot speak again while speaking.");
140143
}
144+
SendResult(flutter::EncodableValue(0));
145+
return;
146+
}
141147

142-
if (std::holds_alternative<std::string>(arguments)) {
143-
std::string text = std::get<std::string>(arguments);
144-
tts_->AddText(text);
148+
if (std::holds_alternative<std::string>(arguments)) {
149+
std::string text = std::get<std::string>(arguments);
150+
if (!tts_->AddText(text)) {
151+
SendResult(flutter::EncodableValue(0));
152+
return;
145153
}
146-
147-
tts_->Speak();
148-
149-
} catch (const TextToSpeechError &error) {
150-
SendErrorResult("Operation failed", error.GetErrorString());
151154
}
152155

153-
if (await_speak_completion_ && !result_for_await_speak_completion_) {
154-
LOG_DEBUG("Store result ptr for await speak completion");
155-
result_for_await_speak_completion_ = std::move(result_);
156+
if (tts_->Speak()) {
157+
if (await_speak_completion_ && !result_for_await_speak_completion_) {
158+
LOG_DEBUG("Store result ptr for await speak completion");
159+
result_for_await_speak_completion_ = std::move(result_);
160+
} else {
161+
SendResult(flutter::EncodableValue(1));
162+
}
156163
} else {
157-
SendResult(flutter::EncodableValue(1));
164+
SendResult(flutter::EncodableValue(0));
158165
}
159166
}
160167

161168
void OnStop() {
162-
try {
163-
tts_->Stop();
164-
} catch (const TextToSpeechError &error) {
165-
SendErrorResult("Operation failed", error.GetErrorString());
169+
if (tts_->Stop()) {
170+
SendResult(flutter::EncodableValue(1));
171+
} else {
172+
SendResult(flutter::EncodableValue(0));
166173
}
167-
SendResult(flutter::EncodableValue(1));
168174
}
169175

170176
void OnPause() {
171-
try {
172-
tts_->Pause();
173-
} catch (const TextToSpeechError &error) {
174-
SendErrorResult("Operation failed", error.GetErrorString());
177+
if (tts_->Pause()) {
178+
SendResult(flutter::EncodableValue(1));
179+
} else {
180+
SendResult(flutter::EncodableValue(0));
175181
}
176-
SendResult(flutter::EncodableValue(1));
177182
}
178183

179184
void OnGetSpeechRateValidRange() {
180185
int min = 0, normal = 0, max = 0;
181-
try {
182-
tts_->GetSpeedRange(&min, &normal, &max);
183-
} catch (const TextToSpeechError &error) {
184-
SendErrorResult("Operation failed", error.GetErrorString());
185-
return;
186-
}
186+
tts_->GetSpeedRange(&min, &normal, &max);
187187
flutter::EncodableMap map;
188188
map.insert(std::pair<flutter::EncodableValue, flutter::EncodableValue>(
189189
"min", min));
@@ -203,7 +203,7 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
203203
SendResult(flutter::EncodableValue(1));
204204
return;
205205
}
206-
SendErrorResult("Invalid argument", "SpeechRate is invaild.");
206+
SendResult(flutter::EncodableValue(0));
207207
}
208208

209209
void OnSetLanguage(const flutter::EncodableValue &arguments) {
@@ -213,7 +213,7 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
213213
SendResult(flutter::EncodableValue(1));
214214
return;
215215
}
216-
SendErrorResult("Invalid argument", "Language is invaild.");
216+
SendResult(flutter::EncodableValue(0));
217217
}
218218

219219
void OnGetLanguage() {
@@ -226,15 +226,13 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
226226

227227
void OnSetVolume(const flutter::EncodableValue &arguments) {
228228
if (std::holds_alternative<double>(arguments)) {
229-
double volume = std::get<double>(arguments);
230-
try {
231-
tts_->SetVolume(volume);
232-
} catch (const TextToSpeechError &error) {
233-
SendErrorResult("Operation failed", error.GetErrorString());
229+
double rate = std::get<double>(arguments);
230+
if (tts_->SetVolume(rate)) {
231+
SendResult(flutter::EncodableValue(1));
234232
return;
235233
}
236234
}
237-
SendErrorResult("Invalid argument", "Volume is invaild.");
235+
SendResult(flutter::EncodableValue(0));
238236
}
239237

240238
void SendResult(const flutter::EncodableValue &result) {
@@ -245,23 +243,6 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
245243
result_ = nullptr;
246244
}
247245

248-
void SendErrorResult(const std::string &error_code,
249-
const std::string &error_message) {
250-
// Keep in sync with the return values implemented in:
251-
// https://github.com/dlutton/flutter_tts/blob/master/android/src/main/java/com/tundralabs/fluttertts/FlutterTtsPlugin.java.
252-
// In principle, MethodResult was designed to call MethodResult.Error() to
253-
// notify the dart code of any method call failures from the host platform.
254-
// However, in the case of flutter_tts, it expects a return value 0 on
255-
// failure(and value 1 on success). Therefore in the scope of this plugin,
256-
// we call result_->Success(flutter::EncodableValue(0)) to notify errors.
257-
if (!result_) {
258-
return;
259-
}
260-
LOG_ERROR("%s", std::string(error_code + " : " + error_message).c_str());
261-
result_->Success(flutter::EncodableValue(0));
262-
result_ = nullptr;
263-
}
264-
265246
void HandleAwaitSpeakCompletion(int value) {
266247
if (await_speak_completion_) {
267248
LOG_DEBUG("Send result for await speak completion[%d]", value);

packages/flutter_tts/tizen/src/text_to_speech.cc

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,24 @@ void OnError(tts_h tts, int utt_id, tts_error_e reason, void *user_data) {
3030

3131
} // namespace
3232

33-
void TextToSpeech::Initialize() {
34-
TtsCreate();
33+
bool TextToSpeech::Initialize() {
34+
if (!TtsCreate()) {
35+
return false;
36+
}
3537
RegisterTtsCallback();
3638
Prepare();
39+
40+
return true;
3741
}
3842

39-
void TextToSpeech::TtsCreate() {
43+
bool TextToSpeech::TtsCreate() {
4044
int ret = tts_create(&tts_);
4145
if (ret != TTS_ERROR_NONE) {
46+
LOG_ERROR("[TTS] tts_create failed: %s", get_error_message(ret));
4247
tts_ = nullptr;
43-
throw TextToSpeechError("tts_create", ret);
48+
return false;
4449
}
50+
return true;
4551
}
4652

4753
void TextToSpeech::TtsDestroy() {
@@ -62,14 +68,15 @@ void TextToSpeech::Prepare() {
6268
}
6369
ret = sound_manager_get_max_volume(SOUND_TYPE_VOICE, &system_max_volume_);
6470
if (ret != SOUND_MANAGER_ERROR_NONE) {
65-
throw TextToSpeechError("sound_manager_get_max_volume", ret);
71+
LOG_ERROR("[SOUNDMANAGER] sound_manager_get_max_volume failed: %s",
72+
get_error_message(ret));
6673
}
6774
system_volume_ = GetSpeechVolumeInternal();
6875
tts_volume_ = system_volume_;
6976

7077
ret = tts_prepare(tts_);
7178
if (ret != TTS_ERROR_NONE) {
72-
throw TextToSpeechError("tts_prepare", ret);
79+
LOG_ERROR("[TTS] tts_prepare failed: %s", get_error_message(ret));
7380
}
7481
}
7582

@@ -118,7 +125,8 @@ TtsState TextToSpeech::GetState() {
118125
tts_state_e state;
119126
int ret = tts_get_state(tts_, &state);
120127
if (ret != TTS_ERROR_NONE) {
121-
throw TextToSpeechError(ret);
128+
LOG_ERROR("[TTS] tts_get_state failed: %s", get_error_message(ret));
129+
return TtsState::kError;
122130
}
123131
switch (state) {
124132
case TTS_STATE_CREATED:
@@ -133,48 +141,61 @@ TtsState TextToSpeech::GetState() {
133141
}
134142
}
135143

136-
void TextToSpeech::AddText(std::string text) {
144+
bool TextToSpeech::AddText(std::string text) {
137145
int ret = tts_add_text(tts_, text.c_str(), default_language_.c_str(),
138146
default_voice_type_, tts_speed_, &utt_id_);
139147
if (ret != TTS_ERROR_NONE) {
140-
throw TextToSpeechError("tts_add_text", ret);
148+
LOG_ERROR("[TTS] tts_add_text failed: %s", get_error_message(ret));
149+
return false;
141150
}
151+
return true;
142152
}
143153

144-
void TextToSpeech::Speak() {
154+
bool TextToSpeech::Speak() {
145155
int ret = tts_play(tts_);
146156
if (ret != TTS_ERROR_NONE) {
147-
throw TextToSpeechError("tts_play", ret);
157+
LOG_ERROR("[TTS] tts_play failed: %s", get_error_message(ret));
158+
return false;
148159
}
160+
return true;
149161
}
150162

151-
void TextToSpeech::Stop() {
163+
bool TextToSpeech::Stop() {
152164
int ret = tts_stop(tts_);
153165
if (ret != TTS_ERROR_NONE) {
154-
throw TextToSpeechError("tts_stop", ret);
166+
LOG_ERROR("[TTS] tts_stop failed: %s", get_error_message(ret));
167+
return false;
155168
}
169+
return true;
156170
}
157171

158-
void TextToSpeech::Pause() {
172+
bool TextToSpeech::Pause() {
159173
int ret = tts_pause(tts_);
160174
if (ret != TTS_ERROR_NONE) {
161-
throw TextToSpeechError("tts_pause", ret);
175+
LOG_ERROR("[TTS] tts_pause failed: %s", get_error_message(ret));
176+
return false;
162177
}
178+
return true;
163179
}
164180

165-
void TextToSpeech::SetVolume(double volume_rate) {
181+
bool TextToSpeech::SetVolume(double volume_rate) {
166182
tts_volume_ = static_cast<int>(system_max_volume_ * volume_rate);
167183
// Change volume instantly when tts is playing.
168184
if (GetState() == TtsState::kPlaying) {
169-
SetSpeechVolumeInternal(tts_volume_);
185+
if (!SetSpeechVolumeInternal(tts_volume_)) {
186+
return false;
187+
}
170188
}
189+
return true;
171190
}
172191

173-
void TextToSpeech::GetSpeedRange(int *min, int *normal, int *max) {
192+
bool TextToSpeech::GetSpeedRange(int *min, int *normal, int *max) {
174193
int ret = tts_get_speed_range(tts_, min, normal, max);
175194
if (ret != TTS_ERROR_NONE) {
176-
throw TextToSpeechError(ret);
195+
LOG_ERROR("[TTS] tts_get_speed_range failed: %s", get_error_message(ret));
196+
return false;
177197
}
198+
return true;
178199
}
179200

180201
void TextToSpeech::SwitchVolumeOnStateChange(tts_state_e previous,
@@ -196,14 +217,19 @@ int TextToSpeech::GetSpeechVolumeInternal() {
196217
int volume;
197218
int ret = sound_manager_get_volume(SOUND_TYPE_VOICE, &volume);
198219
if (ret != SOUND_MANAGER_ERROR_NONE) {
199-
throw TextToSpeechError("sound_manager_get_volume", ret);
220+
LOG_ERROR("[SOUNDMANAGER] sound_manager_get_volume failed: %s",
221+
get_error_message(ret));
222+
volume = 0;
200223
}
201224
return volume;
202225
}
203226

204-
void TextToSpeech::SetSpeechVolumeInternal(int volume) {
227+
bool TextToSpeech::SetSpeechVolumeInternal(int volume) {
205228
int ret = sound_manager_set_volume(SOUND_TYPE_VOICE, volume);
206229
if (ret != SOUND_MANAGER_ERROR_NONE) {
207-
throw TextToSpeechError("sound_manager_set_volume", ret);
230+
LOG_ERROR("[SOUNDMANAGER] sound_manager_set_volume failed: %s",
231+
get_error_message(ret));
232+
return false;
208233
}
234+
return true;
209235
}

0 commit comments

Comments
 (0)