Skip to content

Commit

Permalink
CMake: GStreamer Setup Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Sep 26, 2024
1 parent daf0344 commit 134759f
Show file tree
Hide file tree
Showing 51 changed files with 3,651 additions and 1,016 deletions.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
cmake_minimum_required(VERSION 3.22.1)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
list(APPEND CMAKE_MODULE_PATH
${CMAKE_SOURCE_DIR}/cmake
${CMAKE_SOURCE_DIR}/cmake/modules
${CMAKE_SOURCE_DIR}/cmake/find-modules
)

#######################################################
# Custom Build Configuration
Expand Down Expand Up @@ -60,6 +64,7 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_OPTIMIZE_DEPENDENCIES ON)

set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug;Release")
if(NOT CMAKE_BUILD_TYPE)
Expand Down Expand Up @@ -106,6 +111,10 @@ endif()

set(BUILD_SHARED_LIBS OFF)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_definitions(QGC_INSTALL_RELEASE)
endif()

#######################################################
# Qt6 Configuration
#######################################################
Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
57 changes: 38 additions & 19 deletions android/src/AndroidInit.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "AndroidInterface.h"
#ifndef NO_SERIAL_LINK
#include "AndroidSerial.h"
#include "AndroidSerial.h"
#endif
#ifdef QGC_GST_STREAMING
#include "AndroidVideo.h"
#endif
#include "JoystickAndroid.h"
#include <QGCLoggingCategory.h>
Expand All @@ -10,20 +13,10 @@
#include <QtCore/QLoggingCategory>

QGC_LOGGING_CATEGORY(AndroidInitLog, "qgc.android.init");

static jobject _context = nullptr;
static jobject _class_loader = nullptr;

#ifdef QGC_GST_STREAMING
extern "C"
{
void gst_amc_jni_set_java_vm(JavaVM *java_vm);

jobject gst_android_get_application_class_loader(void)
{
return _class_loader;
}
}
#endif
static JavaVM *_java_vm = nullptr;

static void jniInit(JNIEnv *env, jobject context)
{
Expand Down Expand Up @@ -92,19 +85,45 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
return JNI_ERR;
}

#ifdef QGC_GST_STREAMING
gst_amc_jni_set_java_vm(vm);
#endif
#ifdef QGC_GST_STREAMING
AndroidVideo::setNativeMethods();
#endif

AndroidInterface::setNativeMethods();

#ifndef NO_SERIAL_LINK
AndroidSerial::setNativeMethods();
#endif
#ifndef NO_SERIAL_LINK
AndroidSerial::setNativeMethods();
#endif

JoystickAndroid::setNativeMethods();

QNativeInterface::QAndroidApplication::hideSplashScreen(333);

_java_vm = vm;

return JNI_VERSION_1_6;
}

void JNI_OnUnload(JavaVM *vm, void *reversed)
{
Q_UNUSED(reversed);

qCDebug(AndroidInitLog) << Q_FUNC_INFO;

JNIEnv *env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
}

if (_context) {
*env->DeleteGlobalRef(env, _context);
_context = NULL;
}

if (_class_loader) {
*env->DeleteGlobalRef(env, _class_loader);
_class_loader = NULL;
}

_java_vm = NULL;
}
158 changes: 158 additions & 0 deletions android/src/AndroidVideo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include "AndroidVideo.h"
#include "AndroidInterface.h"
#include "QGCLoggingCategory.h"

QGC_LOGGING_CATEGORY(AndroidVideoLog, "qgc.android.androidvideo")

static jobject _context = NULL;
static jobject _class_loader = NULL;
static JavaVM *_java_vm = NULL;
static GstClockTime _priv_gst_info_start_time;

static void gst_android_load_gio_modules()
{

}

GST_API jobject gst_android_get_application_context();
GST_API jobject gst_android_get_application_class_loader();
GST_API JavaVM* gst_android_get_java_vm();

jobject gst_android_get_application_context()
{
return _context;
}

jobject gst_android_get_application_class_loader()
{
return _class_loader;
}

JavaVM *gst_android_get_java_vm()
{
return _java_vm;
}

static void gst_android_init(JNIEnv *env, jobject context)
{
gchar* cache_dir;
gchar* files_dir;
gchar* registry;
GError* error = NULL;

if (!initialize(env, context)) {
qCDebug(AndroidVideoLog) << "GStreamer failed to initialize";
return;
}

if (gst_is_initialized()) {
qCDebug(AndroidVideoLog) << "GStreamer already initialized";
return;
}

/*if ( !get_application_dirs(env, context, &cache_dir, &files_dir) ) {
__android_log_print (
ANDROID_LOG_ERROR,
"GStreamer",
"Failed to get application dirs"
);
}*/

/*if (cache_dir) {
g_setenv("TMP", cache_dir, TRUE);
g_setenv("TEMP", cache_dir, TRUE);
g_setenv("TMPDIR", cache_dir, TRUE);
g_setenv("XDG_RUNTIME_DIR", cache_dir, TRUE);
g_setenv("XDG_CACHE_HOME", cache_dir, TRUE);
registry = g_build_filename(cache_dir, "registry.bin", NULL);
g_setenv("GST_REGISTRY", registry, TRUE);
g_free(registry);
g_setenv("GST_REGISTRY_REUSE_PLUGIN_SCANNER", "no", TRUE);
// GST_PLUGIN_SCANNER
// GST_PLUGIN_SYSTEM_PATH_1_0
// GST_PLUGIN_PATH_1_0
}*/

/*if (files_dir) {
gchar *fontconfig, *certs;
g_setenv("HOME", files_dir, TRUE);
g_setenv("XDG_DATA_DIRS", files_dir, TRUE);
g_setenv("XDG_CONFIG_DIRS", files_dir, TRUE);
g_setenv("XDG_CONFIG_HOME", files_dir, TRUE);
g_setenv("XDG_DATA_HOME", files_dir, TRUE);
fontconfig = g_build_filename(files_dir, "fontconfig", NULL);
g_setenv("FONTCONFIG_PATH", fontconfig, TRUE);
g_free(fontconfig);
certs = g_build_filename(
files_dir,
"ssl",
"certs",
"ca-certificates.crt",
NULL
);
g_setenv ("CA_CERTIFICATES", certs, TRUE);
g_free (certs);
}*/

// g_free(cache_dir);
// g_free(files_dir);

/* Set GLib print handlers */
// g_set_print_handler (glib_print_handler);
// g_set_printerr_handler (glib_printerr_handler);
// g_log_set_default_handler (glib_log_handler, NULL);

/* Set GStreamer log handlers */
gst_debug_remove_log_function(NULL);

/* Set desired log-level here */
gst_debug_set_default_threshold(GST_LEVEL_ERROR);

gst_debug_add_log_function((GstLogFunction)gst_debug_logcat, NULL, NULL);

/* get time we started for debugging messages */
_priv_gst_info_start_time = gst_util_get_timestamp();
}

static void gst_android_init_jni(JNIEnv* env, jobject gstreamer, jobject context)
{
gst_android_init(env, context);
}

namespace AndroidVideo
{

jint setNativeMethods()
{
qCDebug(GStreamerLog) << Q_FUNC_INFO;

static constexpr JNINativeMethod javaMethods[] {
{"nativeInit", "(Landroid/content/Context;)V", (void *) gst_android_init_jni}
};

QJniEnvironment jniEnv;
(void) jniEnv.checkAndClearExceptions();

jclass objectClass = jniEnv->FindClass("org/freedesktop/gstreamer/GStreamer");
if (!objectClass) {
qCWarning(GStreamerLog) << "Couldn't find class:" << "org/freedesktop/gstreamer/GStreamer";
return JNI_ERR;
}

const jint val = jniEnv->RegisterNatives(objectClass, javaMethods, sizeof(javaMethods) / sizeof(javaMethods[0]));
if (val < 0) {
qCWarning(GStreamerLog) << "Error registering methods:" << val;
} else {
qCDebug(GStreamerLog) << "Main Native Functions Registered";
}

(void) jniEnv.checkAndClearExceptions();

return JNI_OK;
}

}
13 changes: 13 additions & 0 deletions android/src/AndroidVideo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <QtCore/QString>
#include <QtCore/QByteArray>
#include <QtCore/QLoggingCategory>

#include <jni.h>

Q_DECLARE_LOGGING_CATEGORY(AndroidVideoLog);

namespace AndroidVideo {
jint setNativeMethods();
}
Loading

0 comments on commit 134759f

Please sign in to comment.