From b2df91f3a157172578d771f2835757daf72bca49 Mon Sep 17 00:00:00 2001 From: Yongjoo Ahn Date: Mon, 15 Nov 2021 18:55:23 +0900 Subject: [PATCH] [android/mqtt] Add MQTT support - Add MQTT support - It requires paho.mqtt.c prebuilt lib Signed-off-by: Yongjoo Ahn --- .../nnstreamer/src/main/jni/Android-nnstreamer.mk | 36 ++++++++++++++++++++++ java/android/nnstreamer/src/main/jni/Android.mk | 3 ++ .../src/main/jni/nnstreamer-native-api.c | 8 +++++ java/build-nnstreamer-android.sh | 25 +++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/java/android/nnstreamer/src/main/jni/Android-nnstreamer.mk b/java/android/nnstreamer/src/main/jni/Android-nnstreamer.mk index f0c4248..d03836b 100644 --- a/java/android/nnstreamer/src/main/jni/Android-nnstreamer.mk +++ b/java/android/nnstreamer/src/main/jni/Android-nnstreamer.mk @@ -37,15 +37,51 @@ NNSTREAMER_SRC_FILES += \ $(NNSTREAMER_DECODER_PE_SRCS) \ $(NNSTREAMER_DECODER_IS_SRCS) \ $(NNSTREAMER_JOIN_SRCS) + +# TODO: Add nnsquery prebuilt-lib and enable mqtt-hybrid +ifeq ($(ENABLE_MQTT), true) +NNSTREAMER_SRC_FILES += \ + $(NNSTREAMER_MQTT_SRCS) + +NNS_API_FLAGS += -DENABLE_MQTT=1 + +PAHO_MQTT_C_DIR := $(LOCAL_PATH)/paho-mqtt-c +PAHO_MQTT_C_INCLUDES := $(PAHO_MQTT_C_DIR)/include + +ifeq ($(TARGET_ARCH_ABI),arm64-v8a) +PAHO_MQTT_C_LIB_PATH := $(PAHO_MQTT_C_DIR)/lib +else +$(error For MQTT, target arch ABI not supported: $(TARGET_ARCH_ABI)) endif include $(CLEAR_VARS) +LOCAL_MODULE := paho-mqtt3a +LOCAL_SRC_FILES := $(PAHO_MQTT_C_LIB_PATH)/libpaho-mqtt3a.a +include $(PREBUILT_STATIC_LIBRARY) + +include $(CLEAR_VARS) +LOCAL_MODULE := paho-mqtt3c +LOCAL_SRC_FILES := $(PAHO_MQTT_C_LIB_PATH)/libpaho-mqtt3c.a +include $(PREBUILT_STATIC_LIBRARY) +endif # ifeq ($(ENABLE_MQTT), true) + +endif # ifneq ($(NNSTREAMER_API_OPTION),single) + +include $(CLEAR_VARS) LOCAL_MODULE := nnstreamer LOCAL_SRC_FILES := $(sort $(NNSTREAMER_SRC_FILES)) LOCAL_C_INCLUDES := $(NNSTREAMER_INCLUDES) $(GST_HEADERS_COMMON) $(NNSTREAMER_CAPI_INCLUDES) +ifeq ($(ENABLE_MQTT), true) +LOCAL_C_INCLUDES += $(PAHO_MQTT_C_INCLUDES) +endif + LOCAL_EXPORT_C_INCLUDES := $(NNSTREAMER_CAPI_INCLUDES) LOCAL_CFLAGS := -O3 -fPIC $(NNS_API_FLAGS) LOCAL_CXXFLAGS := -O3 -fPIC -frtti -fexceptions $(NNS_API_FLAGS) +ifeq ($(ENABLE_MQTT), true) +LOCAL_STATIC_LIBRARIES := paho-mqtt3a paho-mqtt3c +endif + include $(BUILD_STATIC_LIBRARY) diff --git a/java/android/nnstreamer/src/main/jni/Android.mk b/java/android/nnstreamer/src/main/jni/Android.mk index 7e00cbf..7abf3e1 100644 --- a/java/android/nnstreamer/src/main/jni/Android.mk +++ b/java/android/nnstreamer/src/main/jni/Android.mk @@ -55,6 +55,9 @@ ENABLE_PYTORCH := false # Converter/decoder sub-plugin for flatbuffers support ENABLE_FLATBUF := false +# MQTT (paho.mqtt.c) support +ENABLE_MQTT := false + ifeq ($(ENABLE_SNAP),true) ifeq ($(ENABLE_SNPE),true) $(error DO NOT enable SNAP and SNPE both. The app would fail to use DSP or NPU runtime.) diff --git a/java/android/nnstreamer/src/main/jni/nnstreamer-native-api.c b/java/android/nnstreamer/src/main/jni/nnstreamer-native-api.c index 9502134..9d684d1 100644 --- a/java/android/nnstreamer/src/main/jni/nnstreamer-native-api.c +++ b/java/android/nnstreamer/src/main/jni/nnstreamer-native-api.c @@ -18,6 +18,9 @@ GST_PLUGIN_STATIC_DECLARE (nnstreamer); GST_PLUGIN_STATIC_DECLARE (amcsrc); GST_PLUGIN_STATIC_DECLARE (join); +#if defined (ENABLE_MQTT) +GST_PLUGIN_STATIC_DECLARE (mqtt); +#endif extern void init_dv (void); extern void init_bb (void); extern void init_il (void); @@ -768,6 +771,11 @@ nnstreamer_native_initialize (JNIEnv * env, jobject context) /* GStreamer join element */ GST_PLUGIN_STATIC_REGISTER (join); +#if defined (ENABLE_MQTT) + /* GStreamer MQTT element */ + GST_PLUGIN_STATIC_REGISTER (mqtt); +#endif + /* tensor-decoder sub-plugins */ init_dv (); init_bb (); diff --git a/java/build-nnstreamer-android.sh b/java/build-nnstreamer-android.sh index 0214f44..2b4e87b 100644 --- a/java/build-nnstreamer-android.sh +++ b/java/build-nnstreamer-android.sh @@ -68,6 +68,11 @@ ##@@ 'yes' : [default] ##@@ 'no' : build without the sub-plugin for FlatBuffers and FlexBuffers ##@@ +##@@ options for mqtt: +##@@ --enable_mqtt=(yes|no) +##@@ 'yes' : [default] build with paho.mqtt.c prebuilt libs. This option supports the mqtt plugin +##@@ 'no' : build without the mqtt support +##@@ ##@@ For example, to build library with core plugins for arm64-v8a ##@@ ./build-nnstreamer-android.sh --api_option=lite --target_abi=arm64-v8a @@ -113,6 +118,10 @@ enable_tflite="yes" enable_flatbuf="yes" flatbuf_ver="1.12.0" +# Enable option for MQTT +enable_mqtt="no" +paho_mqtt_c_ver="1.3.7" + # Set tensorflow-lite version (available: 1.9.0 / 1.13.1 / 1.15.2 / 2.3.0) tf_lite_ver="2.3.0" tf_lite_vers_support="1.9.0 1.13.1 1.15.2 2.3.0" @@ -219,6 +228,9 @@ for arg in "$@"; do --enable_flatbuf=*) enable_flatbuf=${arg#*=} ;; + --enable_mqtt=*) + enable_mqtt=${arg#*=} + ;; esac done @@ -296,6 +308,10 @@ if [[ $enable_flatbuf == "yes" ]]; then echo "Build with flatbuffers v$flatbuf_ver for the converter/decoder sub-plugin" fi +if [[ $enable_mqtt == "yes" ]]; then + echo "Build with paho.mqtt.c-v$paho_mqtt_c_ver for the mqtt plugin" +fi + # Set library name nnstreamer_lib_name="nnstreamer" @@ -413,6 +429,10 @@ if [[ $enable_flatbuf == "yes" ]]; then wget --directory-prefix=./$build_dir/external https://github.com/nnstreamer/nnstreamer-android-resource/raw/master/external/flatbuffers-${flatbuf_ver}.tar.xz fi +if [[ $enable_mqtt == "yes" ]]; then + wget --directory-prefix=./$build_dir/external https://github.com/nnstreamer/nnstreamer-android-resource/raw/master/external/paho-mqtt-c-${paho_mqtt_c_ver}.tar.xz +fi + pushd ./$build_dir # Update target ABI @@ -506,6 +526,11 @@ if [[ $enable_flatbuf == "yes" ]]; then tar -xJf ./external/flatbuffers-${flatbuf_ver}.tar.xz -C ./nnstreamer/src/main/jni fi +if [[ $enable_mqtt == "yes" ]]; then + sed -i "s|ENABLE_MQTT := false|ENABLE_MQTT := true|" nnstreamer/src/main/jni/Android.mk + tar -xJf ./external/paho-mqtt-c-${paho_mqtt_c_ver}.tar.xz -C ./nnstreamer/src/main/jni +fi + # If build option is single-shot only, remove unnecessary files. if [[ $nnstreamer_api_option == "single" ]]; then rm ./nnstreamer/src/main/java/org/nnsuite/nnstreamer/CustomFilter.java -- 2.7.4