Thursday, May 28, 2015

Using libyuv prebuilt shared library in Android

In my previous post I explained how to build libyuv shared library  now I would like to explain how to write a JNI wrapper and access the libyuv functions available.

Please go  through the above post before proceeding, as I'll be frequently referring to it.

From the post above, If you have successfully built the libyuv shared library then you should see the .so's generated at the location :-
LRD/libs/armeabi/libyuv_shared.so
LRD/libs/armeabi-v7a/libyuv_shared.so
LRD/libs/mips/libyuv_shared.so
LRD/libs/x86/libyuv_shared.so

We'll be needing the .so's, for the JNI wrapper will be accessing the functions present in it.



Create the following directory structures in your workspace:-


LibYUVDemo/prebuilt/
LibYUVDemo/include/
LibYUVDemo/jni/



Under the LibYUVDemo/prebuilt/ directoy paste the libyuv_shared.so files in the respective order :-

LibYUVDemo/prebuilt/armeabi/libyuv_shared.so
LibYUVDemo/prebuilt/armeabi-v7a/libyuv_shared.so
LibYUVDemo/prebuilt/mips/libyuv_shared.so
LibYUVDemo/prebuilt/x86/libyuv_shared.so

Copy the contents of include folder from libyuv source and paste it under
LibYUVDemo/include/ 

Now the actual JNI wrapper, create LibYUVDemo.cpp under
LibYUVDemo/jni/ and write a jni function as follows :-

#include <jni.h>
#include <string.h>
#include <stdlib.h>
#include "libyuv.h"
#include <android/log.h>

#define LOG_TAG "YUVDemo"
#define LOGI(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define printf(...) LOGI(__VA_ARGS__)

using namespace libyuv;

extern "C" {
JNIEXPORT void Java_com_demo_libyuvdemo_MainActivity_callLibYUV(JNIEnv* env, jobject thiz) {

libyuv::RotationMode mode = libyuv::kRotate180;

/*libyuv::ConvertToI420(const unsigned char *, unsigned int, unsigned char *, int, unsigned char *, int, unsigned char *, int, int, int, int, int, int, int, enum libyuv::RotationMode, unsigned int);
 */
    printf("rotation mode selected = %d",mode);
}

Create an Android.mk file under LibYUVDemo/jni/
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := yuv_shared
LOCAL_SRC_FILES := ../prebuilt/$(TARGET_ARCH_ABI)/libyuv_shared.so
ifneq (,$(wildcard $(LOCAL_PATH)/$(LOCAL_SRC_FILES)))
include $(PREBUILT_SHARED_LIBRARY)
endif


include $(CLEAR_VARS)

LOCAL_MODULE    := YUVDemo
LOCAL_SRC_FILES := LibYUVDemo.cpp
#LOCAL_SRC_FILES := LibYUVDemo.c


#
# Header Includes
#
LOCAL_C_INCLUDES := \
            $(LOCAL_PATH)/../include
#
# Compile Flags and Link Libraries
#
LOCAL_CFLAGS := -DANDROID_NDK

LOCAL_LDLIBS := -llog
LOCAL_SHARED_LIBRARIES := yuv_shared

include $(BUILD_SHARED_LIBRARY)
Finally create an Application.mk file under LibYUVDemo/jni/

APP_ABI := all
APP_PLATFORM := android-9
APP_STL := stlport_shared

Now navigate to LibYUVDemo/jni/ from command prompt and trigger ndk-build, the JNI should get compiled and you must have the new .so geneated at LibYUVDemo/libs/ for the all the architectures.


A complete working app is available at github.

Thursday, May 21, 2015

Building libyuv shared library for Android

         Recently for my project I wanted to rotate NV21 raw data obtained from device camera when the device is held in portrait mode. Sadly android does not provide any API that alters the raw data it provides through the onPreviewFrame callback, so was looking for a way to rotate the NV21 by 90 degrees and libyuv came to the rescue.

Prerequisites:-
  1. NDK setup must be done, and you must be able to compile sample ndk-apps provided in the android-ndk.
  2. Download the libyuv codebase, do an SVN checkout from here.
The directory that gets created once you do SVN checkout is the libyuv root directory, am going to refer it as LRD (LIBYUV_ROOT_DIR).
LRD contains an Android.mk file which we are going to use to build our shared library (of-course some modifications would be needed).

Once the above prerequisites have been met, then proceed with the following steps to build the shared library:-
  • Create a jni folder in LRD.
  • Copy the Android.mk file to it.
  • Create an Application.mk file and add the following lines to it. 
APP_PLATFORM := android-9
APP_ABI := all
  •  Modify the Android.mk file as below to point to the source code correctly
# This is the Android makefile for libyuv for both platform and NDK.
LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_CPP_EXTENSION := .cc

LOCAL_SRC_FILES := \
    $(LOCAL_PATH)/../source/compare.cc           \
    $(LOCAL_PATH)/../source/compare_common.cc    \
    $(LOCAL_PATH)/../source/compare_neon64.cc    \
    $(LOCAL_PATH)/../source/compare_posix.cc     \
    $(LOCAL_PATH)/../source/convert.cc           \
    $(LOCAL_PATH)/../source/convert_argb.cc      \
    $(LOCAL_PATH)/../source/convert_from.cc      \
    $(LOCAL_PATH)/../source/convert_from_argb.cc \
    $(LOCAL_PATH)/../source/convert_to_argb.cc   \
    $(LOCAL_PATH)/../source/convert_to_i420.cc   \
    $(LOCAL_PATH)/../source/cpu_id.cc            \
    $(LOCAL_PATH)/../source/planar_functions.cc  \
    $(LOCAL_PATH)/../source/rotate.cc            \
    $(LOCAL_PATH)/../source/rotate_argb.cc       \
    $(LOCAL_PATH)/../source/rotate_mips.cc       \
    $(LOCAL_PATH)/../source/rotate_neon64.cc     \
    $(LOCAL_PATH)/../source/row_any.cc           \
    $(LOCAL_PATH)/../source/row_common.cc        \
    $(LOCAL_PATH)/../source/row_mips.cc          \
    $(LOCAL_PATH)/../source/row_neon64.cc        \
    $(LOCAL_PATH)/../source/row_posix.cc            \
    $(LOCAL_PATH)/../source/scale.cc             \
    $(LOCAL_PATH)/../source/scale_any.cc         \
    $(LOCAL_PATH)/../source/scale_argb.cc        \
    $(LOCAL_PATH)/../source/scale_common.cc      \
    $(LOCAL_PATH)/../source/scale_mips.cc        \
    $(LOCAL_PATH)/../source/scale_neon64.cc      \
    $(LOCAL_PATH)/../source/scale_posix.cc       \
    $(LOCAL_PATH)/../source/video_common.cc

# TODO(fbarchard): Enable mjpeg encoder.
#   source/mjpeg_decoder.cc
#   source/convert_jpeg.cc
#   source/mjpeg_validate.cc

ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
    LOCAL_CFLAGS += -DLIBYUV_NEON
    LOCAL_SRC_FILES += \
        $(LOCAL_PATH)/../source/compare_neon.cc.neon    \
        $(LOCAL_PATH)/../source/rotate_neon.cc.neon     \
        $(LOCAL_PATH)/../source/row_neon.cc.neon        \
        $(LOCAL_PATH)/../source/scale_neon.cc.neon
endif

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../include

LOCAL_MODULE := libyuv_static
LOCAL_MODULE_TAGS := optional

include $(BUILD_STATIC_LIBRARY)
  • Open command prompt (on Windows) or Terminal in Linux or MAC, navigate to LRD/jni and type ndk-build
  • This should trigger the build process and if all goes well the libyuv_static.a (a static library) will get created inside LRD/obj/local/armeabi/
  • But we cannot use a static library with android  so we need to generate a shared library (.so),  for that simply modify the following tags as below (Android.mk file)
LOCAL_MODULE := libyuv_shared
include $(BUILD_SHARED_LIBRARY)
  • Again trigger ndk-build to generate the .so at LRD/libs/
Now the shared library obtained above has all the functions exposed like ConvertToI420,I420Rotate etc......


To check the functions available use the following command on Linux or MSYS(for windows)
 nm -C -D libyuv_shared.so

In my next post I'll try to show how to write a JNI wrapper to use the libyuv shared library.


Thursday, May 7, 2015

Building WebRTC libraries for Android

   After banging my head almost 2 weeks trying to build WebRTC libraries on Windows and MAC machine, as of today I have successfully compiled WebRTC binaries for Android using this awesome tutorial from here http://www.khirman.com/building-webrtc-libraries-android/

   Anyone reading this post, I would like to suggest don't waste time trying to build on Windows or MAC, it doesn't help, only surges your frustration.

   Linux is the only way out to compile WebRTC, I used Ubuntu 14.04.2 LTS setup in VirtualBox on a MAC.
  
   The following is the list of commands (taken from above site) that I used to compile the libraries.


# assuming we are starting from "virgin" Ubuntu installation
sudo apt-get install git
sudo apt-get install g++
sudo apt-get install subversion
sudo apt-get install git-svn
sudo apt-get install openjdk-7-jdk
sudo apt-get install ant
sudo apt-get install lib32stdc++6 lib32z1
#
# get google build tools
#
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
export PATH=`pwd`/depot_tools:"$PATH"
#
# we'll need Java
#
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
#
# now we are setting our target for compilation
#
export GYP_DEFINES="OS=android"
#
# New home for WebCRT
#
mkdir WebCRT
cd WebCRT
#
# Time to pull sources ( including all dependencies derived from chromium project)
# will take quite a time
#
fetch webrtc_android
#
# Now time to build
#
cd src
#
# setup Android cross compiler build environment
#
. build/android/envsetup.sh
export GYP_DEFINES="build_with_chromium=0  $GYP_DEFINES"
export GYP_DEFINES="build_with_libjingle=1 $GYP_DEFINES"
export GYP_DEFINES="libjingle_java=1 $GYP_DEFINES"
# send WEBRTC_LOGGING to Android's logcat
export GYP_DEFINES="enable_tracing=1 $GYP_DEFINES"
#
# Generate .ninja files
#
gclient runhooks
#
# and now is an actual build....
#

#
# Build AppRTCDemo application – demonstrates interoperability of native Android
# client and Chrome JavaScript client.
# source code / project root : src/talk/examples/android/
# resulting APK : src/out/Debug/AppRTCDemo-debug.apk
#
ninja -C out/Debug AppRTCDemo
#
# Build WebRTCDemo application – exchange video+audio between two
# android clients.
# source code / project root : src/webrtc/examples/android/media_demo/
# resulting APK : src/out/Debug/WebRTCDemo-debug.apk
#
ninja -C out/Debug WebRTCDemo

One small addition, post the gclient  runhooks command in list above use this command to generate all the libraries and apks

ninja -C out/Debug

I am re-posting all the commands here just for my future reference, but might help someone else trying to build the libs.