Skip to content

Commit

Permalink
Migrate JNI to jni_generator for class CobaltTextToSpeechHelper (#4710)
Browse files Browse the repository at this point in the history
This PR is a syntax migration for the CobaltTextToSpeechHelper class to
unblock the larger migration of StarboardBridge, which depends on
CobaltTextToSpeechHelper.

Parts of this class, or even the entire class, might no longer be
necessary. We will need to investigate and clean it up further under bug
b/392178584.

No tests have been conducted because the class is currently not wired up
in Chrobalt.

b/389117681
  • Loading branch information
haozheng-cobalt authored Jan 25, 2025
1 parent 7299dad commit 8289686
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 49 deletions.
1 change: 1 addition & 0 deletions cobalt/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jinja_template("cobalt_manifest") {

generate_jni("jni_headers") {
sources = [
"apk/app/src/main/java/dev/cobalt/coat/CobaltTextToSpeechHelper.java",
"apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.java",
"apk/app/src/main/java/dev/cobalt/media/MediaCodecBridge.java",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
import android.speech.tts.TextToSpeech;
import android.view.accessibility.AccessibilityManager;
import dev.cobalt.util.Log;
import dev.cobalt.util.UsedByNative;
import java.util.ArrayList;
import java.util.List;
import org.chromium.base.annotations.CalledByNative;
// import org.chromium.base.annotations.NativeMethods;

/**
* Helper class to implement the SbSpeechSynthesis* Starboard API for Audio accessibility.
Expand Down Expand Up @@ -92,7 +93,7 @@ public void run() {

/** Returns whether a screen reader is currently enabled */
@SuppressWarnings("unused")
@UsedByNative
@CalledByNative
public boolean isScreenReaderEnabled() {
AccessibilityManager am =
(AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
Expand Down Expand Up @@ -127,9 +128,8 @@ public void run() {
* of Starboard's SbSpeechSynthesisSpeak.
*/
@SuppressWarnings("unused")
@UsedByNative
@CalledByNative
void speak(final String text) {

handler.post(
new Runnable() {
@Override
Expand Down Expand Up @@ -162,7 +162,7 @@ public void run() {

/** Cancels all speaking. Java-layer implementation of Starboard's SbSpeechSynthesisCancel. */
@SuppressWarnings("unused")
@UsedByNative
@CalledByNative
void cancel() {
handler.post(
new Runnable() {
Expand Down Expand Up @@ -199,9 +199,14 @@ public void onTouchExplorationStateChanged(boolean enabled) {
private void finishIfScreenReaderChanged() {
if (wasScreenReaderEnabled != isScreenReaderEnabled()) {
wasScreenReaderEnabled = isScreenReaderEnabled();
nativeSendTTSChangedEvent();
// TODO: (cobalt b/392178584) clean up speech synthesis code, investigate if this is still needed.
// CobaltTextToSpeechHelperJni.get().sendTTSChangedEvent();
}
}

private native void nativeSendTTSChangedEvent();
// TODO: (cobalt b/392178584) clean up speech synthesis code, investigate if this is still needed.
// @NativeMethods
// interface Natives {
// void sendTTSChangedEvent();
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ protected String getCacheAbsolutePath() {
// TODO: (cobalt b/372559388) remove or migrate JNI?
// Used in starboard/android/shared/speech_synthesis_speak.cc
@SuppressWarnings("unused")
@UsedByNative
@CalledByNative
CobaltTextToSpeechHelper getTextToSpeechHelper() {
if (ttsHelper == null) {
throw new IllegalArgumentException("ttsHelper cannot be null for native code");
Expand Down
1 change: 1 addition & 0 deletions starboard/android/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ static_library("starboard_platform") {
# "posix_emu/stat.cc",
"sanitizer_options.cc",

# TODO: (cobalt b/392178584) clean up speech synthesis code.
#"speech_synthesis_cancel.cc",
#"speech_synthesis_internal.cc",
#"speech_synthesis_is_supported.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "starboard/android/shared/jni_env_ext.h"
#include "starboard/android/shared/jni_utils.h"
#include "starboard/android/shared/accessibility_extension.h"

#include "starboard/android/shared/starboard_bridge.h"
#include "starboard/common/memory.h"

#include "starboard/android/shared/accessibility_extension.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "cobalt/android/jni_headers/CobaltTextToSpeechHelper_jni.h"

namespace starboard {
namespace android {
namespace shared {
namespace accessibility {

using starboard::android::shared::JniEnvExt;
using starboard::android::shared::ScopedLocalJavaRef;
// TODO: (cobalt b/372559388) Update namespace to jni_zero.
using base::android::AttachCurrentThread;
using base::android::ScopedJavaLocalRef;

bool GetTextToSpeechSettings(SbAccessibilityTextToSpeechSettings* out_setting) {
if (!out_setting ||
Expand All @@ -33,15 +36,15 @@ bool GetTextToSpeechSettings(SbAccessibilityTextToSpeechSettings* out_setting) {
return false;
}

JniEnvExt* env = JniEnvExt::Get();
JNIEnv* env = AttachCurrentThread();

out_setting->has_text_to_speech_setting = true;
ScopedLocalJavaRef<jobject> j_tts_helper(
env->CallStarboardObjectMethodOrAbort(
"getTextToSpeechHelper",
"()Ldev/cobalt/coat/CobaltTextToSpeechHelper;"));
out_setting->is_text_to_speech_enabled = env->CallBooleanMethodOrAbort(
j_tts_helper.Get(), "isScreenReaderEnabled", "()Z");
ScopedJavaLocalRef<jobject> j_tts_helper =
starboard::android::shared::StarboardBridge::GetInstance()
->GetTextToSpeechHelper(env);

out_setting->is_text_to_speech_enabled =
Java_CobaltTextToSpeechHelper_isScreenReaderEnabled(env, j_tts_helper);

return true;
}
Expand Down
23 changes: 13 additions & 10 deletions starboard/android/shared/speech_synthesis_cancel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@

#include "starboard/speech_synthesis.h"

#include "starboard/android/shared/jni_env_ext.h"
#include "starboard/android/shared/jni_utils.h"
#include "starboard/android/shared/starboard_bridge.h"

using starboard::android::shared::JniEnvExt;
using starboard::android::shared::ScopedLocalJavaRef;
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "cobalt/android/jni_headers/CobaltTextToSpeechHelper_jni.h"

// TODO: (cobalt b/372559388) Update namespace to jni_zero.
using base::android::AttachCurrentThread;
using base::android::ScopedJavaLocalRef;

void SbSpeechSynthesisCancel() {
JniEnvExt* env = JniEnvExt::Get();
JNIEnv* env = AttachCurrentThread();

ScopedJavaLocalRef<jobject> j_tts_helper =
starboard::android::shared::StarboardBridge::GetInstance()
->GetTextToSpeechHelper(env);

ScopedLocalJavaRef<jobject> j_tts_helper(
env->CallStarboardObjectMethodOrAbort(
"getTextToSpeechHelper",
"()Ldev/cobalt/coat/CobaltTextToSpeechHelper;"));
env->CallVoidMethodOrAbort(j_tts_helper.Get(), "cancel", "()V");
Java_CobaltTextToSpeechHelper_cancel(env, j_tts_helper);
}
12 changes: 6 additions & 6 deletions starboard/android/shared/speech_synthesis_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
#include <android/native_activity.h>

#include "starboard/android/shared/application_android.h"
#include "starboard/android/shared/jni_env_ext.h"
#include "starboard/android/shared/jni_utils.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "cobalt/android/jni_headers/CobaltTextToSpeechHelper_jni.h"

namespace starboard {
namespace android {
namespace shared {

extern "C" SB_EXPORT_PLATFORM void
Java_dev_cobalt_coat_CobaltTextToSpeechHelper_nativeSendTTSChangedEvent(
JniEnvExt* env,
jobject unused_this) {
ApplicationAndroid::Get()->SendTTSChangedEvent();
JNI_CobaltTextToSpeechHelper_SendTTSChangedEvent(JNIEnv* env) {
// TODO: (cobalt b/392178584) clean up speech synthesis code, investigate if
// this is still needed. ApplicationAndroid::Get()->SendTTSChangedEvent();
}

} // namespace shared
Expand Down
30 changes: 17 additions & 13 deletions starboard/android/shared/speech_synthesis_speak.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@

#include "starboard/speech_synthesis.h"

#include "starboard/android/shared/jni_env_ext.h"
#include "starboard/android/shared/jni_utils.h"
#include "starboard/android/shared/starboard_bridge.h"

using starboard::android::shared::JniEnvExt;
using starboard::android::shared::ScopedLocalJavaRef;
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "cobalt/android/jni_headers/CobaltTextToSpeechHelper_jni.h"

// TODO: (cobalt b/372559388) Update namespace to jni_zero.
using base::android::AttachCurrentThread;
using base::android::ScopedJavaLocalRef;

void SbSpeechSynthesisSpeak(const char* text) {
if (!text) {
return;
}
JniEnvExt* env = JniEnvExt::Get();
ScopedLocalJavaRef<jobject> j_tts_helper(
env->CallStarboardObjectMethodOrAbort(
"getTextToSpeechHelper",
"()Ldev/cobalt/coat/CobaltTextToSpeechHelper;"));
ScopedLocalJavaRef<jstring> j_text_string(
env->NewStringStandardUTFOrAbort(text));
env->CallVoidMethodOrAbort(j_tts_helper.Get(), "speak",
"(Ljava/lang/String;)V", j_text_string.Get());
JNIEnv* env = AttachCurrentThread();

ScopedJavaLocalRef<jobject> j_tts_helper =
starboard::android::shared::StarboardBridge::GetInstance()
->GetTextToSpeechHelper(env);

jstring text_string = env->NewStringUTF(text);
ScopedJavaLocalRef<jstring> j_text_string(env, text_string);

Java_CobaltTextToSpeechHelper_speak(env, j_tts_helper, j_text_string);
}
6 changes: 6 additions & 0 deletions starboard/android/shared/starboard_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ std::string StarboardBridge::GetCacheAbsolutePath(JNIEnv* env) {
std::string file_path = ConvertJavaStringToUTF8(env, file_path_java);
return file_path;
}

ScopedJavaLocalRef<jobject> StarboardBridge::GetTextToSpeechHelper(
JNIEnv* env) {
SB_DCHECK(env);
return Java_StarboardBridge_getTextToSpeechHelper(env, j_starboard_bridge_);
}
} // namespace shared
} // namespace android
} // namespace starboard
2 changes: 2 additions & 0 deletions starboard/android/shared/starboard_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class StarboardBridge {

std::string GetCacheAbsolutePath(JNIEnv* env);

ScopedJavaLocalRef<jobject> GetTextToSpeechHelper(JNIEnv* env);

private:
StarboardBridge() = default;
~StarboardBridge() = default;
Expand Down

0 comments on commit 8289686

Please sign in to comment.