Fixed makefiles for loopback jni files

Fixed makefiles for missing shared libraries.
Solved issues with runtime library loading permissions.
Removed dependency to libaudioutils library, but had to copy some utility
code from the framework.

Bug:28431138
Change-Id: I0a856a5fa3aff2686295955618f4f3806b578d42
diff --git a/apps/CtsVerifier/Android.mk b/apps/CtsVerifier/Android.mk
index 41f7b48..a52ea7a 100644
--- a/apps/CtsVerifier/Android.mk
+++ b/apps/CtsVerifier/Android.mk
@@ -42,7 +42,7 @@
 
 LOCAL_PACKAGE_NAME := CtsVerifier
 
-LOCAL_JNI_SHARED_LIBRARIES := libctsverifier_jni
+LOCAL_JNI_SHARED_LIBRARIES := libctsverifier_jni libaudioloopback_jni
 
 LOCAL_PROGUARD_FLAG_FILES := proguard.flags
 
diff --git a/apps/CtsVerifier/jni/audio_loopback/Android.mk b/apps/CtsVerifier/jni/audio_loopback/Android.mk
index b302f44..0223e4f 100644
--- a/apps/CtsVerifier/jni/audio_loopback/Android.mk
+++ b/apps/CtsVerifier/jni/audio_loopback/Android.mk
@@ -3,27 +3,25 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE      := libaudioloopback_jni
+
 LOCAL_MODULE_TAGS := optional
+
 LOCAL_SRC_FILES   := \
 	sles.cpp \
-	jni_sles.c
-
-LOCAL_C_INCLUDES := \
-        system/media/audio_utils/include \
-        frameworks/wilhelm/include
+	jni_sles.c \
+	audio_utils/atomic.c \
+	audio_utils/fifo.c \
+	audio_utils/roundup.c
 
 LOCAL_SHARED_LIBRARIES := \
-	libutils \
-	libcutils \
 	libOpenSLES \
-	libnbaio \
-	liblog \
-	libaudioutils
-
-LOCAL_PRELINK_MODULE := false
+	liblog
 
 LOCAL_LDFLAGS := -Wl,--hash-style=sysv
+
 LOCAL_CFLAGS := -DSTDC_HEADERS \
-    -Werror -Wall
+	-Werror -Wall
+
+LOCAL_SDK_VERSION := 23
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/apps/CtsVerifier/jni/audio_loopback/audio_utils/atomic.c b/apps/CtsVerifier/jni/audio_loopback/audio_utils/atomic.c
new file mode 100644
index 0000000..db2b3fc
--- /dev/null
+++ b/apps/CtsVerifier/jni/audio_loopback/audio_utils/atomic.c
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "atomic.h"
+
+#include <stdatomic.h>
+
+int32_t android_atomic_acquire_load(volatile const int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    return atomic_load_explicit(a, memory_order_acquire);
+}
+
+void android_atomic_release_store(int32_t value, volatile int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    atomic_store_explicit(a, value, memory_order_release);
+}
diff --git a/apps/CtsVerifier/jni/audio_loopback/audio_utils/atomic.h b/apps/CtsVerifier/jni/audio_loopback/audio_utils/atomic.h
new file mode 100644
index 0000000..535c926
--- /dev/null
+++ b/apps/CtsVerifier/jni/audio_loopback/audio_utils/atomic.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_ATOMIC_H
+#define ANDROID_AUDIO_ATOMIC_H
+
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int32_t android_atomic_acquire_load(volatile const int32_t* addr);
+void android_atomic_release_store(int32_t value, volatile int32_t* addr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // ANDROID_AUDIO_ATOMIC_H
diff --git a/apps/CtsVerifier/jni/audio_loopback/audio_utils/fifo.c b/apps/CtsVerifier/jni/audio_loopback/audio_utils/fifo.c
new file mode 100644
index 0000000..ea9a8d1
--- /dev/null
+++ b/apps/CtsVerifier/jni/audio_loopback/audio_utils/fifo.c
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "audio_utils_fifo"
+
+#include <stdlib.h>
+#include <string.h>
+#include "fifo.h"
+#include "roundup.h"
+#include "atomic.h"
+//#include <cutils/log.h>
+#define ALOG_ASSERT(exp)
+
+void audio_utils_fifo_init(struct audio_utils_fifo *fifo, size_t frameCount, size_t frameSize,
+        void *buffer)
+{
+    // We would need a 64-bit roundup to support larger frameCount.
+    ALOG_ASSERT(fifo != NULL && frameCount > 0 && frameSize > 0 && buffer != NULL);
+    fifo->mFrameCount = frameCount;
+    fifo->mFrameCountP2 = roundup(frameCount);
+    fifo->mFudgeFactor = fifo->mFrameCountP2 - fifo->mFrameCount;
+    fifo->mFrameSize = frameSize;
+    fifo->mBuffer = buffer;
+    fifo->mFront = 0;
+    fifo->mRear = 0;
+}
+
+void audio_utils_fifo_deinit(struct audio_utils_fifo *fifo __unused)
+{
+}
+
+// Return a new index as the sum of an old index (either mFront or mRear) and a specified increment.
+static inline int32_t audio_utils_fifo_sum(struct audio_utils_fifo *fifo, int32_t index,
+        uint32_t increment)
+{
+    if (fifo->mFudgeFactor) {
+        uint32_t mask = fifo->mFrameCountP2 - 1;
+        ALOG_ASSERT((index & mask) < fifo->mFrameCount);
+        ALOG_ASSERT(/*0 <= increment &&*/ increment <= fifo->mFrameCountP2);
+        if ((index & mask) + increment >= fifo->mFrameCount) {
+            increment += fifo->mFudgeFactor;
+        }
+        index += increment;
+        ALOG_ASSERT((index & mask) < fifo->mFrameCount);
+        return index;
+    } else {
+        return index + increment;
+    }
+}
+
+// Return the difference between two indices: rear - front, where 0 <= difference <= mFrameCount.
+static inline size_t audio_utils_fifo_diff(struct audio_utils_fifo *fifo, int32_t rear,
+        int32_t front)
+{
+    int32_t diff = rear - front;
+    if (fifo->mFudgeFactor) {
+        uint32_t mask = ~(fifo->mFrameCountP2 - 1);
+        int32_t genDiff = (rear & mask) - (front & mask);
+        if (genDiff != 0) {
+            ALOG_ASSERT(genDiff == (int32_t) fifo->mFrameCountP2);
+            diff -= fifo->mFudgeFactor;
+        }
+    }
+    // FIFO should not be overfull
+    ALOG_ASSERT(0 <= diff && diff <= (int32_t) fifo->mFrameCount);
+    return (size_t) diff;
+}
+
+ssize_t audio_utils_fifo_write(struct audio_utils_fifo *fifo, const void *buffer, size_t count)
+{
+    int32_t front = android_atomic_acquire_load(&fifo->mFront);
+    int32_t rear = fifo->mRear;
+    size_t availToWrite = fifo->mFrameCount - audio_utils_fifo_diff(fifo, rear, front);
+    if (availToWrite > count) {
+        availToWrite = count;
+    }
+    rear &= fifo->mFrameCountP2 - 1;
+    size_t part1 = fifo->mFrameCount - rear;
+    if (part1 > availToWrite) {
+        part1 = availToWrite;
+    }
+    if (part1 > 0) {
+        memcpy((char *) fifo->mBuffer + (rear * fifo->mFrameSize), buffer,
+                part1 * fifo->mFrameSize);
+        size_t part2 = availToWrite - part1;
+        if (part2 > 0) {
+            memcpy(fifo->mBuffer, (char *) buffer + (part1 * fifo->mFrameSize),
+                    part2 * fifo->mFrameSize);
+        }
+        android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mRear, availToWrite),
+                &fifo->mRear);
+    }
+    return availToWrite;
+}
+
+ssize_t audio_utils_fifo_read(struct audio_utils_fifo *fifo, void *buffer, size_t count)
+{
+    int32_t rear = android_atomic_acquire_load(&fifo->mRear);
+    int32_t front = fifo->mFront;
+    size_t availToRead = audio_utils_fifo_diff(fifo, rear, front);
+    if (availToRead > count) {
+        availToRead = count;
+    }
+    front &= fifo->mFrameCountP2 - 1;
+    size_t part1 = fifo->mFrameCount - front;
+    if (part1 > availToRead) {
+        part1 = availToRead;
+    }
+    if (part1 > 0) {
+        memcpy(buffer, (char *) fifo->mBuffer + (front * fifo->mFrameSize),
+                part1 * fifo->mFrameSize);
+        size_t part2 = availToRead - part1;
+        if (part2 > 0) {
+            memcpy((char *) buffer + (part1 * fifo->mFrameSize), fifo->mBuffer,
+                    part2 * fifo->mFrameSize);
+        }
+        android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mFront, availToRead),
+                &fifo->mFront);
+    }
+    return availToRead;
+}
diff --git a/apps/CtsVerifier/jni/audio_loopback/audio_utils/fifo.h b/apps/CtsVerifier/jni/audio_loopback/audio_utils/fifo.h
new file mode 100644
index 0000000..ba4c5c6
--- /dev/null
+++ b/apps/CtsVerifier/jni/audio_loopback/audio_utils/fifo.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_FIFO_H
+#define ANDROID_AUDIO_FIFO_H
+
+#include <stdlib.h>
+
+// FIXME use atomic_int_least32_t and new atomic operations instead of legacy Android ones
+// #include <stdatomic.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Single writer, single reader non-blocking FIFO.
+// Writer and reader must be in same process.
+
+// No user-serviceable parts within.
+struct audio_utils_fifo {
+    // These fields are const after initialization
+    size_t     mFrameCount;   // max number of significant frames to be stored in the FIFO > 0
+    size_t     mFrameCountP2; // roundup(mFrameCount)
+    size_t     mFudgeFactor;  // mFrameCountP2 - mFrameCount, the number of "wasted" frames after
+                              // the end of mBuffer.  Only the indices are wasted, not any memory.
+    size_t     mFrameSize;    // size of each frame in bytes
+    void      *mBuffer;       // pointer to caller-allocated buffer of size mFrameCount frames
+
+    volatile int32_t mFront; // frame index of first frame slot available to read, or read index
+    volatile int32_t mRear;  // frame index of next frame slot available to write, or write index
+};
+
+// Initialize a FIFO object.
+// Input parameters:
+//  fifo        Pointer to the FIFO object.
+//  frameCount  Max number of significant frames to be stored in the FIFO > 0.
+//              If writes and reads always use the same count, and that count is a divisor of
+//              frameCount, then the writes and reads will never do a partial transfer.
+//  frameSize   Size of each frame in bytes.
+//  buffer      Pointer to a caller-allocated buffer of frameCount frames.
+void audio_utils_fifo_init(struct audio_utils_fifo *fifo, size_t frameCount, size_t frameSize,
+        void *buffer);
+
+// De-initialize a FIFO object.
+// Input parameters:
+//  fifo        Pointer to the FIFO object.
+void audio_utils_fifo_deinit(struct audio_utils_fifo *fifo);
+
+// Write to FIFO.
+// Input parameters:
+//  fifo        Pointer to the FIFO object.
+//  buffer      Pointer to source buffer containing 'count' frames of data.
+// Returns actual number of frames written <= count.
+// The actual transfer count may be zero if the FIFO is full,
+// or partial if the FIFO was almost full.
+// A negative return value indicates an error.  Currently there are no errors defined.
+ssize_t audio_utils_fifo_write(struct audio_utils_fifo *fifo, const void *buffer, size_t count);
+
+// Read from FIFO.
+// Input parameters:
+//  fifo        Pointer to the FIFO object.
+//  buffer      Pointer to destination buffer to be filled with up to 'count' frames of data.
+// Returns actual number of frames read <= count.
+// The actual transfer count may be zero if the FIFO is empty,
+// or partial if the FIFO was almost empty.
+// A negative return value indicates an error.  Currently there are no errors defined.
+ssize_t audio_utils_fifo_read(struct audio_utils_fifo *fifo, void *buffer, size_t count);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // !ANDROID_AUDIO_FIFO_H
diff --git a/apps/CtsVerifier/jni/audio_loopback/audio_utils/roundup.c b/apps/CtsVerifier/jni/audio_loopback/audio_utils/roundup.c
new file mode 100644
index 0000000..4f9af6a
--- /dev/null
+++ b/apps/CtsVerifier/jni/audio_loopback/audio_utils/roundup.c
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "roundup.h"
+
+unsigned roundup(unsigned v)
+{
+    // __builtin_clz is undefined for zero input
+    if (v == 0) {
+        v = 1;
+    }
+    int lz = __builtin_clz((int) v);
+    unsigned rounded = ((unsigned) 0x80000000) >> lz;
+    // 0x800000001 and higher are actually rounded _down_ to prevent overflow
+    if (v > rounded && lz > 0) {
+        rounded <<= 1;
+    }
+    return rounded;
+}
diff --git a/apps/CtsVerifier/jni/audio_loopback/audio_utils/roundup.h b/apps/CtsVerifier/jni/audio_loopback/audio_utils/roundup.h
new file mode 100644
index 0000000..ad34289
--- /dev/null
+++ b/apps/CtsVerifier/jni/audio_loopback/audio_utils/roundup.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_ROUNDUP_H
+#define ANDROID_AUDIO_ROUNDUP_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Round up to the next highest power of 2
+unsigned roundup(unsigned v);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // ANDROID_AUDIO_ROUNDUP_H