From f99a5f32897b8e651e9409010e41ef00dc645161 Mon Sep 17 00:00:00 2001
From: Felix Abecassis <felix.abecassis@gmail.com>
Date: Fri, 14 Mar 2014 19:37:39 +0100
Subject: [PATCH] gprof: vlc-android

---
 Makefile                    |  5 +++--
 compile.sh                  | 10 ++++++++--
 configure.sh                |  2 +-
 vlc-android/jni/Android.mk  | 13 +++++++++----
 vlc-android/jni/libvlcjni.c | 15 +++++++++++++++
 5 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/Makefile b/Makefile
index 73f4409..7afc66a 100644
--- a/Makefile
+++ b/Makefile
@@ -64,7 +64,7 @@ $(LIBVLCJNI_H):
 	printf "/* Autogenerated from the list of modules */\n $$DEFINITION\n $$BUILTINS\n" > $@
 
 $(PRIVATE_LIBDIR)/%.so: $(PRIVATE_LIBDIR)/%.c
-	$(GEN)$(TARGET_TUPLE)-gcc $< -shared -o $@ --sysroot=$(ANDROID_NDK)/platforms/android-9/arch-$(PLATFORM_SHORT_ARCH)
+	$(GEN)$(TARGET_TUPLE)-gcc $< -shared -pg -o $@ --sysroot=$(ANDROID_NDK)/platforms/android-9/arch-$(PLATFORM_SHORT_ARCH)
 
 $(PRIVATE_LIBDIR)/%.c: $(PRIVATE_LIBDIR)/%.symbols
 	$(VERBOSE)rm -f $@
@@ -91,7 +91,8 @@ $(LIBVLCJNI): $(JNI_SOURCES) $(LIBVLCJNI_H) $(PRIVATE_LIBS)
 		VLC_BUILD_DIR="$$VLC_BUILD_DIR" \
 		VLC_MODULES="$$vlc_modules" \
 		NDK_DEBUG=$(NDK_DEBUG) \
-		TARGET_CFLAGS="$$VLC_EXTRA_CFLAGS"
+		TARGET_CFLAGS="$$VLC_EXTRA_CFLAGS" \
+		NDK_MODULE_PATH=$(ANDROID_NDK)/../tools
 
 apkclean:
 	rm -f $(VLC_APK)
diff --git a/compile.sh b/compile.sh
index 8d37be1..f410e93 100755
--- a/compile.sh
+++ b/compile.sh
@@ -149,6 +149,7 @@ fi
 if [ ${ANDROID_ABI} = "armeabi-v7a" ] ; then
     EXTRA_CFLAGS="-mfpu=vfpv3-d16 -mcpu=cortex-a8"
     EXTRA_CFLAGS="${EXTRA_CFLAGS} -mthumb -mfloat-abi=softfp"
+    LDFLAGS="${LDFLAGS} -Wl,-L${ANDROID_NDK}/../tools/android-ndk-profiler/armeabi-v7a -Wl,--whole-archive -Wl,-landroid-ndk-profiler -Wl,-llog -Wl,--no-whole-archive"
 elif [ ${ANDROID_ABI} = "armeabi" ] ; then
     if [ -n "${NO_ARMV6}" ]; then
         EXTRA_CFLAGS="-march=armv5te -mtune=arm9tdmi -msoft-float"
@@ -171,7 +172,7 @@ else
     exit 2
 fi
 
-EXTRA_CFLAGS="${EXTRA_CFLAGS} -O2"
+EXTRA_CFLAGS="${EXTRA_CFLAGS} -O2 -fno-omit-frame-pointer -pg"
 
 EXTRA_CFLAGS="${EXTRA_CFLAGS} -I${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++${CXXSTL}/include"
 EXTRA_CFLAGS="${EXTRA_CFLAGS} -I${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++${CXXSTL}/libs/${ANDROID_ABI}/include"
@@ -211,6 +212,10 @@ gen_pc_file GLESv2 2
 
 cd contrib/android
 ../bootstrap --host=${TARGET_TUPLE} --disable-disc --disable-sout \
+    --disable-a52 \
+    --disable-gmp \
+    --disable-nettle \
+    --disable-gnutls \
     --disable-dca \
     --disable-goom \
     --disable-chromaprint \
@@ -242,7 +247,7 @@ cd contrib/android
 
 # Release or not?
 if [ $# -ne 0 ] && [ "$1" = "release" ]; then
-    OPTS=""
+    OPTS="--disable-a52"
     EXTRA_CFLAGS="${EXTRA_CFLAGS} -DNDEBUG "
     RELEASE=1
 else
@@ -251,6 +256,7 @@ else
 fi
 
 echo "EXTRA_CFLAGS= -g ${EXTRA_CFLAGS}" >> config.mak
+echo "LDFLAGS= ${LDFLAGS}" >> config.mak
 export VLC_EXTRA_CFLAGS="${EXTRA_CFLAGS}"
 
 make fetch
diff --git a/configure.sh b/configure.sh
index 8dae0aa..c4a379a 100755
--- a/configure.sh
+++ b/configure.sh
@@ -26,7 +26,7 @@ LDFLAGS="-Wl,-Bdynamic,-dynamic-linker=/system/bin/linker -Wl,--no-undefined"
 if [ -n "$HAVE_ARM" ]; then
     if [ ${ANDROID_ABI} = "armeabi-v7a" ]; then
         EXTRA_PARAMS=" --enable-neon"
-        LDFLAGS="$LDFLAGS -Wl,--fix-cortex-a8"
+        LDFLAGS="$LDFLAGS -Wl,--fix-cortex-a8 -Wl,-L${ANDROID_NDK}/../tools/android-ndk-profiler/armeabi-v7a -Wl,--whole-archive -Wl,-landroid-ndk-profiler -Wl,-llog -Wl,--no-whole-archive"
     fi
 fi
 
diff --git a/vlc-android/jni/Android.mk b/vlc-android/jni/Android.mk
index 2fc58b6..d45899a 100644
--- a/vlc-android/jni/Android.mk
+++ b/vlc-android/jni/Android.mk
@@ -44,7 +44,7 @@ ARCH=$(ANDROID_ABI)
 
 CPP_STATIC=$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++$(CXXSTL)/libs/$(ARCH)/libgnustl_static.a
 
-LOCAL_CFLAGS := -std=gnu99
+LOCAL_CFLAGS := -std=gnu99 -fno-omit-frame-pointer -pg
 ifeq ($(ARCH), armeabi)
 	LOCAL_CFLAGS += -DHAVE_ARMEABI
 	# Needed by ARMv6 Thumb1 (the System Control coprocessor/CP15 is mandatory on ARMv6)
@@ -62,14 +62,14 @@ LOCAL_LDLIBS := -L$(VLC_CONTRIB)/lib \
 	-ldl -lz -lm -llog \
 	-ldvbpsi -lebml -lmatroska -ltag \
 	-logg -lFLAC -ltheora -lvorbis \
-	-lmpeg2 -la52 \
+	-lmpeg2 \
 	-lavformat -lavcodec -lswscale -lavutil -lpostproc -lgsm -lopenjpeg \
 	-lliveMedia -lUsageEnvironment -lBasicUsageEnvironment -lgroupsock \
 	-lspeex -lspeexdsp \
-	-lxml2 -lpng -lgnutls -lgcrypt -lgpg-error \
-	-lnettle -lhogweed -lgmp \
+	-lxml2 -lpng -lgcrypt -lgpg-error \
 	-lfreetype -liconv -lass -lfribidi -lopus \
 	-lEGL -lGLESv2 -ljpeg \
+	-Wl,-L${ANDROID_NDK}/../tools/android-ndk-profiler/armeabi-v7a -Wl,--whole-archive -Wl,-landroid-ndk-profiler -Wl,-llog -Wl,--no-whole-archive \
 	$(CPP_STATIC)
 
 include $(BUILD_SHARED_LIBRARY)
@@ -82,6 +82,7 @@ LOCAL_SRC_FILES  := ../$(VLC_SRC_DIR)/modules/codec/omxil/iomx.cpp
 LOCAL_C_INCLUDES := $(VLC_SRC_DIR)/modules/codec/omxil $(ANDROID_SYS_HEADERS_GINGERBREAD)/frameworks/base/include $(ANDROID_SYS_HEADERS_GINGERBREAD)/system/core/include
 LOCAL_CFLAGS     := -Wno-psabi
 LOCAL_LDLIBS     := -L$(ANDROID_LIBS) -lgcc -lstagefright -lmedia -lutils -lbinder
+LOCAL_STATIC_LIBRARIES := android-ndk-profiler
 
 include $(BUILD_SHARED_LIBRARY)
 
@@ -92,6 +93,7 @@ LOCAL_SRC_FILES  := ../$(VLC_SRC_DIR)/modules/codec/omxil/iomx.cpp
 LOCAL_C_INCLUDES := $(VLC_SRC_DIR)/modules/codec/omxil $(ANDROID_SYS_HEADERS_HC)/frameworks/base/include $(ANDROID_SYS_HEADERS_HC)/frameworks/base/native/include $(ANDROID_SYS_HEADERS_HC)/system/core/include $(ANDROID_SYS_HEADERS_HC)/hardware/libhardware/include
 LOCAL_CFLAGS     := -Wno-psabi
 LOCAL_LDLIBS     := -L$(ANDROID_LIBS) -lgcc -lstagefright -lmedia -lutils -lbinder
+LOCAL_STATIC_LIBRARIES := android-ndk-profiler
 
 include $(BUILD_SHARED_LIBRARY)
 
@@ -102,5 +104,8 @@ LOCAL_SRC_FILES  := ../$(VLC_SRC_DIR)/modules/codec/omxil/iomx.cpp
 LOCAL_C_INCLUDES := $(VLC_SRC_DIR)/modules/codec/omxil $(ANDROID_SYS_HEADERS_ICS)/frameworks/base/include $(ANDROID_SYS_HEADERS_ICS)/frameworks/base/native/include $(ANDROID_SYS_HEADERS_ICS)/system/core/include $(ANDROID_SYS_HEADERS_ICS)/hardware/libhardware/include
 LOCAL_CFLAGS     := -Wno-psabi
 LOCAL_LDLIBS     := -L$(ANDROID_LIBS) -lgcc -lstagefright -lmedia -lutils -lbinder
+LOCAL_STATIC_LIBRARIES := android-ndk-profiler
 
 include $(BUILD_SHARED_LIBRARY)
+
+$(call import-module,android-ndk-profiler)
diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index db69733..9318e56 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -49,6 +49,8 @@
 #define LOG_TAG "VLC/JNI/main"
 #include "log.h"
 
+static int monstatus = 0;
+
 libvlc_media_t *new_media(jlong instance, JNIEnv *env, jobject thiz, jstring fileLocation, bool noOmx, bool noVideo)
 {
     libvlc_instance_t *libvlc = (libvlc_instance_t*)(intptr_t)instance;
@@ -341,6 +343,9 @@ void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv *env, jobject thiz, jlong in
     /* Release previous media player, if any */
     releaseMediaPlayer(env, thiz);
 
+    if (++monstatus == 1)
+        monstartup("libvlcjni.so");
+
     /* Create a media player playing environment */
     libvlc_media_player_t *mp = libvlc_media_player_new((libvlc_instance_t*)(intptr_t)instance);
     libvlc_media_player_set_video_title_display(mp, libvlc_position_disable, 0);
@@ -450,6 +455,11 @@ void Java_org_videolan_libvlc_LibVLC_pause(JNIEnv *env, jobject thiz)
     libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
     if (mp)
         libvlc_media_player_pause(mp);
+    if (monstatus == 1)
+    {
+        ++monstatus;
+        moncleanup();
+    }
 }
 
 void Java_org_videolan_libvlc_LibVLC_stop(JNIEnv *env, jobject thiz)
@@ -457,6 +467,11 @@ void Java_org_videolan_libvlc_LibVLC_stop(JNIEnv *env, jobject thiz)
     libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
     if (mp)
         libvlc_media_player_stop(mp);
+    if (monstatus == 1)
+    {
+        ++monstatus;
+        moncleanup();
+    }
 }
 
 jint Java_org_videolan_libvlc_LibVLC_getVolume(JNIEnv *env, jobject thiz)
-- 
1.8.3.2

