Skip to content

Commit

Permalink
Reimplement jni prefixing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidliu committed Jun 10, 2024
1 parent 3d96890 commit 63703d3
Showing 1 changed file with 124 additions and 35 deletions.
159 changes: 124 additions & 35 deletions build/patches/jni_prefix.patch
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ index d1be684cbb..812c3eb5dc 100644
env, env->NewObject(factory_class.obj(), factory_constructor,
nullptr /* shared_context */));
diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn
index 494b43f6d3..1a63c66558 100644
index 7c05290942..7f9b9012d3 100644
--- a/sdk/android/BUILD.gn
+++ b/sdk/android/BUILD.gn
@@ -1308,6 +1308,7 @@ if (current_os == "linux" || is_android) {
@@ -1333,6 +1333,7 @@ if (current_os == "linux" || is_android) {
sources = [ "api/org/webrtc/Metrics.java" ]
namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
+ package_prefix = android_package_prefix
}

# Generated JNI for public JNI targets, matching order of targets
@@ -1322,6 +1323,7 @@ if (current_os == "linux" || is_android) {
@@ -1347,6 +1348,7 @@ if (current_os == "linux" || is_android) {
]
namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
+ package_prefix = android_package_prefix
}

generate_jni("generated_video_jni") {
@@ -1352,12 +1354,14 @@ if (current_os == "linux" || is_android) {
@@ -1377,12 +1379,14 @@ if (current_os == "linux" || is_android) {
]
namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
Expand All @@ -61,47 +61,47 @@ index 494b43f6d3..1a63c66558 100644
}

generate_jni("generated_libvpx_vp8_jni") {
@@ -1368,6 +1372,7 @@ if (current_os == "linux" || is_android) {
@@ -1393,6 +1397,7 @@ if (current_os == "linux" || is_android) {

namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
+ package_prefix = android_package_prefix
}

generate_jni("generated_libvpx_vp9_jni") {
@@ -1378,6 +1383,7 @@ if (current_os == "linux" || is_android) {
@@ -1403,6 +1408,7 @@ if (current_os == "linux" || is_android) {

namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
+ package_prefix = android_package_prefix
}

generate_jni("generated_libaom_av1_encoder_jni") {
@@ -1385,6 +1391,7 @@ if (current_os == "linux" || is_android) {
@@ -1410,6 +1416,7 @@ if (current_os == "linux" || is_android) {

namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
+ package_prefix = android_package_prefix
}

generate_jni("generated_dav1d_jni") {
@@ -1392,6 +1399,7 @@ if (current_os == "linux" || is_android) {
@@ -1417,6 +1424,7 @@ if (current_os == "linux" || is_android) {

namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
+ package_prefix = android_package_prefix
}

generate_jni("generated_swcodecs_jni") {
@@ -1402,6 +1410,7 @@ if (current_os == "linux" || is_android) {
@@ -1427,6 +1435,7 @@ if (current_os == "linux" || is_android) {

namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
+ package_prefix = android_package_prefix
}

generate_jni("generated_peerconnection_jni") {
@@ -1443,12 +1452,14 @@ if (current_os == "linux" || is_android) {
@@ -1469,12 +1478,14 @@ if (current_os == "linux" || is_android) {
]
namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
Expand All @@ -116,23 +116,23 @@ index 494b43f6d3..1a63c66558 100644
}

generate_jni("generated_builtin_audio_codecs_jni") {
@@ -1458,6 +1469,7 @@ if (current_os == "linux" || is_android) {
@@ -1484,6 +1495,7 @@ if (current_os == "linux" || is_android) {
]
namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
+ package_prefix = android_package_prefix
}

# Generated JNI for native API targets
@@ -1469,6 +1481,7 @@ if (current_os == "linux" || is_android) {
@@ -1496,6 +1508,7 @@ if (current_os == "linux" || is_android) {
]
namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
+ package_prefix = android_package_prefix
}

# Generated JNI for internal targets.
@@ -1476,12 +1489,14 @@ if (current_os == "linux" || is_android) {
@@ -1503,12 +1516,14 @@ if (current_os == "linux" || is_android) {
generate_jni("generated_logging_jni") {
sources = [ "src/java/org/webrtc/JNILogging.java" ]
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
Expand All @@ -147,7 +147,7 @@ index 494b43f6d3..1a63c66558 100644
}

generate_jni("generated_java_audio_device_module_native_jni") {
@@ -1491,6 +1506,7 @@ if (current_os == "linux" || is_android) {
@@ -1518,6 +1533,7 @@ if (current_os == "linux" || is_android) {
]
namespace = "webrtc::jni"
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
Expand All @@ -156,10 +156,10 @@ index 494b43f6d3..1a63c66558 100644
}

diff --git a/sdk/android/api/org/webrtc/PeerConnectionFactory.java b/sdk/android/api/org/webrtc/PeerConnectionFactory.java
index 1817d41beb..1c7bfbf480 100644
index 762ad3b678..6cb2000788 100644
--- a/sdk/android/api/org/webrtc/PeerConnectionFactory.java
+++ b/sdk/android/api/org/webrtc/PeerConnectionFactory.java
@@ -89,7 +89,7 @@ public class PeerConnectionFactory {
@@ -91,7 +91,7 @@ public class PeerConnectionFactory {
private String fieldTrials = "";
private boolean enableInternalTracer;
private NativeLibraryLoader nativeLibraryLoader = new NativeLibrary.DefaultLoader();
Expand All @@ -186,10 +186,10 @@ index 4e5781027a..5d17a87c50 100644

namespace webrtc {
diff --git a/sdk/android/src/jni/simulcast_video_encoder.cc b/sdk/android/src/jni/simulcast_video_encoder.cc
index da31fbbfa5..a9385d8b6c 100644
index 6874c1821b..1cf8cceda5 100644
--- a/sdk/android/src/jni/simulcast_video_encoder.cc
+++ b/sdk/android/src/jni/simulcast_video_encoder.cc
@@ -15,7 +15,13 @@ extern "C" {
@@ -15,7 +15,14 @@ extern "C" {
#endif

// (VideoEncoderFactory primary, VideoEncoderFactory fallback, VideoCodecInfo info)
Expand All @@ -198,40 +198,129 @@ index da31fbbfa5..a9385d8b6c 100644
+ SimulcastVideoEncoder_nativeCreateEncoder,
+ JNIEnv *env,
+ jclass klass,
+ jlong webrtcEnvRef,
+ jobject primary,
+ jobject fallback,
+ jobject info) {
RTC_LOG(LS_INFO) << "Create simulcast video encoder";
JavaParamRef<jobject> info_ref(info);
SdpVideoFormat format = VideoCodecInfoToSdpVideoFormat(env, info_ref);
diff --git a/tools_webrtc/android/build_aar.py b/tools_webrtc/android/build_aar.py
index d910b39a7c..d0e970cfaa 100755
index b442358283..5900e11935 100755
--- a/tools_webrtc/android/build_aar.py
+++ b/tools_webrtc/android/build_aar.py
@@ -250,7 +250,7 @@ def BuildAar(archs,
Collect(aar_file, build_dir, arch, unstripped)
@@ -252,7 +252,7 @@ def BuildAar(archs,
Collect(aar_file, build_dir, arch, unstripped)

license_dir = os.path.dirname(os.path.realpath(output_file))
- GenerateLicenses(license_dir, build_dir, archs)
+ #GenerateLicenses(license_dir, build_dir, archs)
license_dir = os.path.dirname(os.path.realpath(output_file))
- GenerateLicenses(license_dir, build_dir, archs)
+ #GenerateLicenses(license_dir, build_dir, archs)

if not ext_build_dir:
shutil.rmtree(build_dir, True)
if not ext_build_dir:
shutil.rmtree(build_dir, True)
diff --git a/webrtc.gni b/webrtc.gni
index 5e4a5d3c81..c9cb039432 100644
index 969cdbd2be..16095bbd4f 100644
--- a/webrtc.gni
+++ b/webrtc.gni
@@ -254,6 +254,10 @@ declare_args() {
# "warn": RTC_LOGs a message with LS_WARNING severity if the field trial
@@ -255,6 +255,10 @@ declare_args() {
# hasn't been registered.
rtc_strict_field_trials = ""
+
+ if (is_android) {
+ android_package_prefix = "livekit"
+ }

# If different from "", symbols exported with RTC_OBJC_EXPORT will be prefixed
# with this string.
# See the definition of RTC_OBJC_TYPE_PREFIX in the code.
+
# If different from "", symbols exported with RTC_OBJC_EXPORT will be prefixed
# with this string.
# See the definition of RTC_OBJC_TYPE_PREFIX in the code.
diff --git a/third_party/jni_zero/codegen/header_common.py b/third_party/jni_zero/codegen/header_common.py
index be0ee6ca43b..8f90d4f6c33 100644
--- a/third_party/jni_zero/codegen/header_common.py
+++ b/third_party/jni_zero/codegen/header_common.py
@@ -13,7 +13,8 @@ def class_accessors(java_classes, module_name):
for java_class in java_classes:
if java_class in (java_types.OBJECT_CLASS, java_types.STRING_CLASS):
continue
- escaped_name = common.escape_class_name(java_class.full_name_with_slashes)
+ print("header accessor: ", java_class.class_without_prefix.full_name_with_slashes)
+ escaped_name = common.escape_class_name(java_class.class_without_prefix.full_name_with_slashes)
# #ifdef needed when multple .h files are #included that common classes.
sb.append(f"""\
#ifndef {escaped_name}_clazz_defined
@@ -42,7 +43,7 @@ def class_accessor_expression(java_class):
if java_class == java_types.STRING_CLASS:
return 'jni_zero::g_string_class'

- escaped_name = common.escape_class_name(java_class.full_name_with_slashes)
+ escaped_name = common.escape_class_name(java_class.class_without_prefix.full_name_with_slashes)
return f'{escaped_name}_clazz(env)'


@@ -52,7 +53,7 @@ def header_preamble(script_name,
user_includes,
header_guard=None):
if header_guard is None:
- header_guard = java_class.full_name_with_slashes.replace('/', '_') + '_JNI'
+ header_guard = java_class.class_without_prefix.full_name_with_slashes.replace('/', '_') + '_JNI'
sb = []
sb.append(f"""\
// This file was generated by
diff --git a/third_party/jni_zero/java_types.py b/third_party/jni_zero/java_types.py
index 06708f70ab3..65c1b29ee8d 100644
--- a/third_party/jni_zero/java_types.py
+++ b/third_party/jni_zero/java_types.py
@@ -129,7 +129,7 @@ class JavaClass:
return JavaClass(f'{prefix}/{self._fqn}', self)

def make_nested(self, name):
- return JavaClass(f'{self._fqn}${name}')
+ return JavaClass(f'{self.class_without_prefix._fqn}${name}')


@dataclasses.dataclass(frozen=True)
diff --git a/third_party/jni_zero/jni_zero.gni b/third_party/jni_zero/jni_zero.gni
index 46d9619789e..4b7e721f716 100644
--- a/third_party/jni_zero/jni_zero.gni
+++ b/third_party/jni_zero/jni_zero.gni
@@ -100,6 +100,7 @@ template("_invoke_jni_zero") {
# require_native_mocks: Enforce that any native calls using
# org.jni_zero.NativeMethods must have a mock set
# (optional).
+# package_prefix: package prefix to add to jni
# enable_native_mocks: Allow native calls using
# org.jni_zero.NativeMethods to be mocked in tests
# (optional).
@@ -267,6 +268,7 @@ template("generate_jni_impl") {
"deps",
"metadata",
"public_deps",
+ "package_prefix",
])
if (!defined(public_deps)) {
public_deps = []
@@ -343,6 +345,10 @@ template("generate_jni_impl") {
args += [ "--split-name=${invoker.split_name}" ]
}

+ if (defined(package_prefix)) {
+ args += [ "--package-prefix=${package_prefix}" ]
+ }
+
foreach(_name, _input_names) {
_name = get_path_info(_name, "name") + "_jni.h"
outputs += [ "$_jni_output_dir/$_name" ]
diff --git a/third_party/jni_zero/parse.py b/third_party/jni_zero/parse.py
index 33203b9848e..f627ed0f8ee 100644
--- a/third_party/jni_zero/parse.py
+++ b/third_party/jni_zero/parse.py
@@ -382,7 +382,10 @@ def _do_parse(filename, *, package_prefix):

if package_prefix:
outer_class = outer_class.make_prefixed(package_prefix)
+ print("inner classes: ")
nested_classes = [c.make_prefixed(package_prefix) for c in nested_classes]
+ for c in nested_classes:
+ print(c.full_name_with_slashes, c.class_without_prefix.full_name_with_slashes)

if (!build_with_mozilla) {
type_resolver = java_types.TypeResolver(outer_class)
for java_class in _parse_imports(contents):

0 comments on commit 63703d3

Please sign in to comment.