Androidmedia plugin fails to load

I’m trying to load the androidmedia plugin for an Android app and it fails to load and there’s no documentation on how to handle any of this.

07-04 19:39:07.143 13932  I Plugin  : [GStreamer] plugin "(NULL)" looks good
07-04 19:39:07.144 13932 13967 I Plugin  : [GStreamer] Could not find 'JNI_CreateJavaVM' in '(NULL)': 'JNI_CreateJavaVM': undefined symbol: JNI_CreateJavaVM
07-04 19:39:07.145 13932 13967 I Plugin  : [GStreamer] Failed to locate required JNI symbols in '(NULL)': 'JNI_GetCreatedJavaVMs': undefined symbol: JNI_GetCreatedJavaVMs
07-04 19:39:07.146 13932 13967 I Plugin  : [GStreamer] Failed to load Java module 'libdvm': dlopen failed: library "libdvm.so" not found
07-04 19:39:07.146 13932 13967 I Plugin  : [GStreamer] plugin "(NULL)" failed to initialise

Cmakelists

cmake_minimum_required(VERSION 3.18)

project(gstplugin LANGUAGES C CXX)

set(GSTREAMER_ROOT_ANDROID "{redacted}")

set(ANDROID_ABI "arm64-v8a")
set(ANDROID_PLATFORM 21)

if(NOT DEFINED ANDROID_TOOLCHAIN)
  set(ANDROID_TOOLCHAIN "${ANDROID_NDK}/build/cmake/android.toolchain.cmake")
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(gstplugin SHARED gst_plugin.cpp)

target_include_directories(gstplugin PRIVATE
  "${GSTREAMER_ROOT_ANDROID}/include/gstreamer-1.0"
  "${GSTREAMER_ROOT_ANDROID}/include/glib-2.0"
  "${GSTREAMER_ROOT_ANDROID}/lib/glib-2.0/include"
)s

target_link_libraries(gstplugin
  "${GSTREAMER_ROOT_ANDROID}/lib/libgobject-2.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libglib-2.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgmodule-2.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libintl.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libpcre2-8.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libpcre2-posix.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libffi.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libiconv.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgstvideo-1.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libz.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgstnet-1.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgstaudio-1.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/liborc-0.4.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgsttag-1.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgstpbutils-1.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgstcodecparsers-1.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgstgl-1.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgstphotography-1.0.a"
  log
  android
  EGL
  GLESv2

  "${GSTREAMER_ROOT_ANDROID}/lib/libgstbase-1.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgstapp-1.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgstreamer-1.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgstrtp-1.0.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/libgio-2.0.a"

  "${GSTREAMER_ROOT_ANDROID}/lib/gstreamer-1.0/libgstudp.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/gstreamer-1.0/libgstrtp.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/gstreamer-1.0/libgstandroidmedia.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/gstreamer-1.0/libgstvideoconvertscale.a"
  "${GSTREAMER_ROOT_ANDROID}/lib/gstreamer-1.0/libgstvideoparsersbad.a"
)


target_compile_options(gstplugin PRIVATE -fno-exceptions -fno-rtti)

target_compile_definitions(gstplugin PRIVATE
  GST_PLUGIN_BUILD_STATIC
)

I don’t know where these JNI functions are supposed to come from. All the docs and tutorials reference a libgstreamer_android.so which doesn’t exist in the tarballs nor a cerbero build. Where do I get these JNI imports from and where is this documented?

There’s an Android player example in subprojects/gst-examples/playback/player/android · main · GStreamer / gstreamer · GitLab that includes an example jni layer. You end up building the libgstreamer_android.so for your app with only the plugins that you need

Thanks but I don’t think this contains anything anywhere close to the required solution. The key file is gst-plugins-bad/sys/androidmedia/gstjniutils.c at master · GStreamer/gst-plugins-bad · GitHub and the actual solution that worked was:

Forcefully add this symbol to my CMakeLists

target_link_options(gstplugin PRIVATE -u gst_android_get_application_class_loader)

As this contains the JNI_OnLoad to setup the Java VM on the GStreamer side… can’t find any documentation about this.

The error then changes to

"Could not retrieve class org.freedesktop.gstreamer.GStreamer. " "Please copy GStreamer.java file into your project: " "https://gitlab.freedesktop.org/gstreamer/gstreamer/-/tree/master/data/android/GStreamer.java");

Which of course doesn’t work. The file is located at https://gitlab.freedesktop.org/gstreamer/gstreamer/-/blob/main/subprojects/gstreamer/data/android/GStreamer.java not where it tells you to go.

androidmedia finally loads after 2 days of searching through source code.