Merge "WAVExtractor: support ambisonic subformat"
diff --git a/Android.bp b/Android.bp
new file mode 100644
index 0000000..edd3d04
--- /dev/null
+++ b/Android.bp
@@ -0,0 +1,18 @@
+ndk_headers {
+    name: "libcamera2ndk_headers",
+    from: "include/camera/ndk/",
+    to: "camera",
+    srcs: ["include/camera/ndk/**/*.h"],
+}
+
+ndk_headers {
+    name: "libmediandk_headers",
+    from: "include/ndk/",
+    to: "media",
+    srcs: ["include/ndk/**/*.h"],
+}
+
+subdirs = [
+    "camera/ndk",
+    "media/*",
+]
diff --git a/camera/cameraserver/Android.mk b/camera/cameraserver/Android.mk
index 7e36c5e..c0d75f3 100644
--- a/camera/cameraserver/Android.mk
+++ b/camera/cameraserver/Android.mk
@@ -21,6 +21,7 @@
 
 LOCAL_SHARED_LIBRARIES := \
 	libcameraservice \
+	liblog \
 	libcutils \
 	libutils \
 	libbinder \
diff --git a/camera/ndk/Android.bp b/camera/ndk/Android.bp
new file mode 100644
index 0000000..6f2351f
--- /dev/null
+++ b/camera/ndk/Android.bp
@@ -0,0 +1,23 @@
+// Copyright (C) 2016 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.
+
+// Headers module is in frameworks/av/Android.bp because modules are not allowed
+// to refer to headers in parent directories and the headers live in
+// frameworks/av/include.
+
+ndk_library {
+    name: "libcamera2ndk.ndk",
+    symbol_file: "libcamera2ndk.map.txt",
+    first_version: "24",
+}
diff --git a/camera/ndk/impl/ACameraCaptureSession.h b/camera/ndk/impl/ACameraCaptureSession.h
index 58428e6..f56219c 100644
--- a/camera/ndk/impl/ACameraCaptureSession.h
+++ b/camera/ndk/impl/ACameraCaptureSession.h
@@ -24,7 +24,7 @@
 using namespace android;
 
 struct ACaptureSessionOutput {
-    ACaptureSessionOutput(ANativeWindow* window) : mWindow(window) {};
+    explicit ACaptureSessionOutput(ANativeWindow* window) : mWindow(window) {};
 
     bool operator == (const ACaptureSessionOutput& other) const {
         return mWindow == other.mWindow;
diff --git a/camera/ndk/impl/ACameraDevice.h b/camera/ndk/impl/ACameraDevice.h
index 71e364d..051462b 100644
--- a/camera/ndk/impl/ACameraDevice.h
+++ b/camera/ndk/impl/ACameraDevice.h
@@ -65,7 +65,7 @@
     // Callbacks from camera service
     class ServiceCallback : public hardware::camera2::BnCameraDeviceCallbacks {
       public:
-        ServiceCallback(CameraDevice* device) : mDevice(device) {}
+        explicit ServiceCallback(CameraDevice* device) : mDevice(device) {}
         binder::Status onDeviceError(int32_t errorCode,
                            const CaptureResultExtras& resultExtras) override;
         binder::Status onDeviceIdle() override;
diff --git a/camera/ndk/impl/ACameraManager.h b/camera/ndk/impl/ACameraManager.h
index 3f2262f..5b88904 100644
--- a/camera/ndk/impl/ACameraManager.h
+++ b/camera/ndk/impl/ACameraManager.h
@@ -60,7 +60,7 @@
 
     class DeathNotifier : public IBinder::DeathRecipient {
       public:
-        DeathNotifier(CameraManagerGlobal* cm) : mCameraManager(cm) {}
+        explicit DeathNotifier(CameraManagerGlobal* cm) : mCameraManager(cm) {}
       protected:
         // IBinder::DeathRecipient implementation
         virtual void binderDied(const wp<IBinder>& who);
@@ -71,7 +71,7 @@
 
     class CameraServiceListener final : public hardware::BnCameraServiceListener {
       public:
-        CameraServiceListener(CameraManagerGlobal* cm) : mCameraManager(cm) {}
+        explicit CameraServiceListener(CameraManagerGlobal* cm) : mCameraManager(cm) {}
         virtual binder::Status onStatusChanged(int32_t status, int32_t cameraId);
 
         // Torch API not implemented yet
@@ -86,7 +86,7 @@
 
     // Wrapper of ACameraManager_AvailabilityCallbacks so we can store it in std::set
     struct Callback {
-        Callback(const ACameraManager_AvailabilityCallbacks *callback) :
+        explicit Callback(const ACameraManager_AvailabilityCallbacks *callback) :
             mAvailable(callback->onCameraAvailable),
             mUnavailable(callback->onCameraUnavailable),
             mContext(callback->context) {}
diff --git a/camera/ndk/impl/ACaptureRequest.h b/camera/ndk/impl/ACaptureRequest.h
index 6bd8406..3ef6a84 100644
--- a/camera/ndk/impl/ACaptureRequest.h
+++ b/camera/ndk/impl/ACaptureRequest.h
@@ -22,7 +22,7 @@
 using namespace android;
 
 struct ACameraOutputTarget {
-    ACameraOutputTarget(ANativeWindow* window) : mWindow(window) {};
+    explicit ACameraOutputTarget(ANativeWindow* window) : mWindow(window) {};
 
     bool operator == (const ACameraOutputTarget& other) const {
         return mWindow == other.mWindow;
diff --git a/camera/ndk/libcamera2ndk.map.txt b/camera/ndk/libcamera2ndk.map.txt
new file mode 100644
index 0000000..41bb22b
--- /dev/null
+++ b/camera/ndk/libcamera2ndk.map.txt
@@ -0,0 +1,46 @@
+LIBCAMERA2NDK {
+  global:
+    ACameraCaptureSession_abortCaptures;
+    ACameraCaptureSession_capture;
+    ACameraCaptureSession_close;
+    ACameraCaptureSession_getDevice;
+    ACameraCaptureSession_setRepeatingRequest;
+    ACameraCaptureSession_stopRepeating;
+    ACameraDevice_close;
+    ACameraDevice_createCaptureRequest;
+    ACameraDevice_createCaptureSession;
+    ACameraDevice_getId;
+    ACameraManager_create;
+    ACameraManager_delete;
+    ACameraManager_deleteCameraIdList;
+    ACameraManager_getCameraCharacteristics;
+    ACameraManager_getCameraIdList;
+    ACameraManager_openCamera;
+    ACameraManager_registerAvailabilityCallback;
+    ACameraManager_unregisterAvailabilityCallback;
+    ACameraMetadata_copy;
+    ACameraMetadata_free;
+    ACameraMetadata_getAllTags;
+    ACameraMetadata_getConstEntry;
+    ACameraOutputTarget_create;
+    ACameraOutputTarget_free;
+    ACaptureRequest_addTarget;
+    ACaptureRequest_free;
+    ACaptureRequest_getAllTags;
+    ACaptureRequest_getConstEntry;
+    ACaptureRequest_removeTarget;
+    ACaptureRequest_setEntry_double;
+    ACaptureRequest_setEntry_float;
+    ACaptureRequest_setEntry_i32;
+    ACaptureRequest_setEntry_i64;
+    ACaptureRequest_setEntry_rational;
+    ACaptureRequest_setEntry_u8;
+    ACaptureSessionOutputContainer_add;
+    ACaptureSessionOutputContainer_create;
+    ACaptureSessionOutputContainer_free;
+    ACaptureSessionOutputContainer_remove;
+    ACaptureSessionOutput_create;
+    ACaptureSessionOutput_free;
+  local:
+    *;
+};
diff --git a/drm/libmediadrm/Android.mk b/drm/libmediadrm/Android.mk
index 6a2ed31..f1c6b00 100644
--- a/drm/libmediadrm/Android.mk
+++ b/drm/libmediadrm/Android.mk
@@ -23,9 +23,6 @@
 	libstagefright_foundation \
 	libutils
 
-LOCAL_C_INCLUDES := \
-    libcore/include
-
 LOCAL_CFLAGS += -Werror -Wno-error=deprecated-declarations -Wall
 LOCAL_CLANG := true
 
diff --git a/include/camera/Camera.h b/include/camera/Camera.h
index be793a2..57dc228 100644
--- a/include/camera/Camera.h
+++ b/include/camera/Camera.h
@@ -170,7 +170,7 @@
     class RecordingProxy : public BnCameraRecordingProxy
     {
     public:
-        RecordingProxy(const sp<Camera>& camera);
+        explicit RecordingProxy(const sp<Camera>& camera);
 
         // ICameraRecordingProxy interface
         virtual status_t startRecording(const sp<ICameraRecordingProxyListener>& listener);
@@ -183,7 +183,7 @@
     };
 
 protected:
-                        Camera(int cameraId);
+    explicit            Camera(int cameraId);
                         Camera(const Camera&);
                         Camera& operator=(const Camera);
 
diff --git a/include/camera/ndk/NdkCameraCaptureSession.h b/include/camera/ndk/NdkCameraCaptureSession.h
index 7b314e9..d96f538 100644
--- a/include/camera/ndk/NdkCameraCaptureSession.h
+++ b/include/camera/ndk/NdkCameraCaptureSession.h
@@ -32,6 +32,8 @@
  * Do not reference types that are not part of the NDK.
  * Do not #include files that aren't part of the NDK.
  */
+#include <sys/cdefs.h>
+
 #include <android/native_window.h>
 #include "NdkCameraError.h"
 #include "NdkCameraMetadata.h"
@@ -39,9 +41,9 @@
 #ifndef _NDK_CAMERA_CAPTURE_SESSION_H
 #define _NDK_CAMERA_CAPTURE_SESSION_H
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+__BEGIN_DECLS
+
+#if __ANDROID_API__ >= 24
 
 /**
  * ACameraCaptureSession is an opaque type that manages frame captures of a camera device.
@@ -587,11 +589,10 @@
  */
 camera_status_t ACameraCaptureSession_abortCaptures(ACameraCaptureSession* session);
 
+#endif /* __ANDROID_API__ >= 24 */
 
-#ifdef __cplusplus
-} // extern "C"
-#endif
+__END_DECLS
 
-#endif // _NDK_CAMERA_CAPTURE_SESSION_H
+#endif /* _NDK_CAMERA_CAPTURE_SESSION_H */
 
 /** @} */
diff --git a/include/camera/ndk/NdkCameraDevice.h b/include/camera/ndk/NdkCameraDevice.h
index 9011cb6..9b7f6f4 100644
--- a/include/camera/ndk/NdkCameraDevice.h
+++ b/include/camera/ndk/NdkCameraDevice.h
@@ -32,6 +32,7 @@
  * Do not reference types that are not part of the NDK.
  * Do not #include files that aren't part of the NDK.
  */
+#include <sys/cdefs.h>
 
 #include <android/native_window.h>
 #include "NdkCameraError.h"
@@ -41,9 +42,9 @@
 #ifndef _NDK_CAMERA_DEVICE_H
 #define _NDK_CAMERA_DEVICE_H
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+__BEGIN_DECLS
+
+#if __ANDROID_API__ >= 24
 
 /**
  * ACameraDevice is opaque type that provides access to a camera device.
@@ -658,11 +659,11 @@
         const ACameraCaptureSession_stateCallbacks* callbacks,
         /*out*/ACameraCaptureSession** session);
 
-#ifdef __cplusplus
-} // extern "C"
-#endif
+#endif /* __ANDROID_API__ >= 24 */
 
-#endif // _NDK_CAMERA_DEVICE_H
+__END_DECLS
+
+#endif /* _NDK_CAMERA_DEVICE_H */
 
 /** @} */
 
diff --git a/include/camera/ndk/NdkCameraError.h b/include/camera/ndk/NdkCameraError.h
index 36251fc..6b58155 100644
--- a/include/camera/ndk/NdkCameraError.h
+++ b/include/camera/ndk/NdkCameraError.h
@@ -36,9 +36,11 @@
 #ifndef _NDK_CAMERA_ERROR_H
 #define _NDK_CAMERA_ERROR_H
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+#if __ANDROID_API__ >= 24
 
 typedef enum {
     ACAMERA_OK = 0,
@@ -130,11 +132,10 @@
     ACAMERA_ERROR_PERMISSION_DENIED     = ACAMERA_ERROR_BASE - 13,
 } camera_status_t;
 
+#endif /* __ANDROID_API__ >= 24 */
 
-#ifdef __cplusplus
-} // extern "C"
-#endif
+__END_DECLS
 
-#endif // _NDK_CAMERA_ERROR_H
+#endif /* _NDK_CAMERA_ERROR_H */
 
 /** @} */
diff --git a/include/camera/ndk/NdkCameraManager.h b/include/camera/ndk/NdkCameraManager.h
index 9188e94..5b5c98b 100644
--- a/include/camera/ndk/NdkCameraManager.h
+++ b/include/camera/ndk/NdkCameraManager.h
@@ -36,13 +36,15 @@
 #ifndef _NDK_CAMERA_MANAGER_H
 #define _NDK_CAMERA_MANAGER_H
 
+#include <sys/cdefs.h>
+
 #include "NdkCameraError.h"
 #include "NdkCameraMetadata.h"
 #include "NdkCameraDevice.h"
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+__BEGIN_DECLS
+
+#if __ANDROID_API__ >= 24
 
 /**
  * ACameraManager is opaque type that provides access to camera service.
@@ -271,10 +273,10 @@
         ACameraDevice_StateCallbacks* callback,
         /*out*/ACameraDevice** device);
 
-#ifdef __cplusplus
-} // extern "C"
-#endif
+#endif /* __ANDROID_API__ >= 24 */
 
-#endif //_NDK_CAMERA_MANAGER_H
+__END_DECLS
+
+#endif /* _NDK_CAMERA_MANAGER_H */
 
 /** @} */
diff --git a/include/camera/ndk/NdkCameraMetadata.h b/include/camera/ndk/NdkCameraMetadata.h
index d929854..f2aec98 100644
--- a/include/camera/ndk/NdkCameraMetadata.h
+++ b/include/camera/ndk/NdkCameraMetadata.h
@@ -36,12 +36,14 @@
 #ifndef _NDK_CAMERA_METADATA_H
 #define _NDK_CAMERA_METADATA_H
 
+#include <sys/cdefs.h>
+
 #include "NdkCameraError.h"
 #include "NdkCameraMetadataTags.h"
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+__BEGIN_DECLS
+
+#if __ANDROID_API__ >= 24
 
 /**
  * ACameraMetadata is opaque type that provides access to read-only camera metadata like camera
@@ -226,10 +228,10 @@
  */
 void ACameraMetadata_free(ACameraMetadata* metadata);
 
-#ifdef __cplusplus
-} // extern "C"
-#endif
+#endif /* __ANDROID_API__ >= 24 */
 
-#endif //_NDK_CAMERA_METADATA_H
+__END_DECLS
+
+#endif /* _NDK_CAMERA_METADATA_H */
 
 /** @} */
diff --git a/include/camera/ndk/NdkCameraMetadataTags.h b/include/camera/ndk/NdkCameraMetadataTags.h
index e7f6989..0fec983 100644
--- a/include/camera/ndk/NdkCameraMetadataTags.h
+++ b/include/camera/ndk/NdkCameraMetadataTags.h
@@ -36,6 +36,12 @@
 #ifndef _NDK_CAMERA_METADATA_TAGS_H
 #define _NDK_CAMERA_METADATA_TAGS_H
 
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+#if __ANDROID_API__ >= 24
+
 typedef enum acamera_metadata_section {
     ACAMERA_COLOR_CORRECTION,
     ACAMERA_CONTROL,
@@ -6901,8 +6907,10 @@
 
 } acamera_metadata_enum_android_depth_depth_is_exclusive_t;
 
+#endif /* __ANDROID_API__ >= 24 */
 
+__END_DECLS
 
-#endif //_NDK_CAMERA_METADATA_TAGS_H
+#endif /* _NDK_CAMERA_METADATA_TAGS_H */
 
 /** @} */
diff --git a/include/camera/ndk/NdkCaptureRequest.h b/include/camera/ndk/NdkCaptureRequest.h
index cd97f4d..c62ba2c 100644
--- a/include/camera/ndk/NdkCaptureRequest.h
+++ b/include/camera/ndk/NdkCaptureRequest.h
@@ -32,6 +32,9 @@
  * Do not reference types that are not part of the NDK.
  * Do not #include files that aren't part of the NDK.
  */
+
+#include <sys/cdefs.h>
+
 #include <android/native_window.h>
 #include "NdkCameraError.h"
 #include "NdkCameraMetadata.h"
@@ -39,9 +42,9 @@
 #ifndef _NDK_CAPTURE_REQUEST_H
 #define _NDK_CAPTURE_REQUEST_H
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+__BEGIN_DECLS
+
+#if __ANDROID_API__ >= 24
 
 // Container for output targets
 typedef struct ACameraOutputTargets ACameraOutputTargets;
@@ -300,10 +303,10 @@
  */
 void ACaptureRequest_free(ACaptureRequest* request);
 
-#ifdef __cplusplus
-} // extern "C"
-#endif
+#endif /* __ANDROID_API__ >= 24 */
 
-#endif // _NDK_CAPTURE_REQUEST_H
+__END_DECLS
+
+#endif /* _NDK_CAPTURE_REQUEST_H */
 
 /** @} */
diff --git a/include/media/IMediaSource.h b/include/media/IMediaSource.h
index 709f425..4056d54 100644
--- a/include/media/IMediaSource.h
+++ b/include/media/IMediaSource.h
@@ -89,7 +89,7 @@
         SeekMode mSeekMode;
         int64_t mLatenessUs;
         bool mNonBlocking;
-    };
+    } __attribute__((packed)); // sent through Binder
 
     // Returns a new buffer of data. Call blocks until a
     // buffer is available, an error is encountered or the end of the stream
diff --git a/include/media/mediaplayer.h b/include/media/mediaplayer.h
index cec9d99..389ec01 100644
--- a/include/media/mediaplayer.h
+++ b/include/media/mediaplayer.h
@@ -30,7 +30,7 @@
 #include <utils/KeyedVector.h>
 #include <utils/String8.h>
 
-class ANativeWindow;
+struct ANativeWindow;
 
 namespace android {
 
diff --git a/include/media/stagefright/MediaCodec.h b/include/media/stagefright/MediaCodec.h
index 8291cec..497974d 100644
--- a/include/media/stagefright/MediaCodec.h
+++ b/include/media/stagefright/MediaCodec.h
@@ -33,7 +33,7 @@
 struct AReplyToken;
 struct AString;
 struct CodecBase;
-struct IBatteryStats;
+class IBatteryStats;
 struct ICrypto;
 class IMemory;
 struct MemoryDealer;
@@ -41,7 +41,7 @@
 class IResourceManagerService;
 struct PersistentSurface;
 struct SoftwareRenderer;
-struct Surface;
+class Surface;
 
 struct MediaCodec : public AHandler {
     enum ConfigureFlags {
diff --git a/include/ndk/NdkImage.h b/include/ndk/NdkImage.h
index cd0b11e..9a99287 100644
--- a/include/ndk/NdkImage.h
+++ b/include/ndk/NdkImage.h
@@ -36,12 +36,16 @@
 #ifndef _NDK_IMAGE_H
 #define _NDK_IMAGE_H
 
+#include <sys/cdefs.h>
+
 #include "NdkMediaError.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+#if __ANDROID_API__ >= 24
+
 /**
  * AImage is an opaque type that provides access to image generated by {@link AImageReader}.
  */
@@ -604,6 +608,8 @@
         const AImage* image, int planeIdx,
         /*out*/uint8_t** data, /*out*/int* dataLength);
 
+#endif /* __ANDROID_API__ >= 24 */
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
diff --git a/include/ndk/NdkImageReader.h b/include/ndk/NdkImageReader.h
index 7c7ec6a..8d72c28 100644
--- a/include/ndk/NdkImageReader.h
+++ b/include/ndk/NdkImageReader.h
@@ -36,6 +36,8 @@
 #ifndef _NDK_IMAGE_READER_H
 #define _NDK_IMAGE_READER_H
 
+#include <sys/cdefs.h>
+
 #include <android/native_window.h>
 #include "NdkMediaError.h"
 #include "NdkImage.h"
@@ -44,6 +46,8 @@
 extern "C" {
 #endif
 
+#if __ANDROID_API__ >= 24
+
 /**
  * AImage is an opaque type that allows direct application access to image data rendered into a
  * {@link ANativeWindow}.
@@ -294,6 +298,8 @@
 media_status_t AImageReader_setImageListener(
         AImageReader* reader, AImageReader_ImageListener* listener);
 
+#endif /* __ANDROID_API__ >= 24 */
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
diff --git a/include/ndk/NdkMediaCodec.h b/include/ndk/NdkMediaCodec.h
index fcb3a99..ad17321 100644
--- a/include/ndk/NdkMediaCodec.h
+++ b/include/ndk/NdkMediaCodec.h
@@ -27,6 +27,8 @@
 #ifndef _NDK_MEDIA_CODEC_H
 #define _NDK_MEDIA_CODEC_H
 
+#include <sys/cdefs.h>
+
 #include <android/native_window.h>
 
 #include "NdkMediaCrypto.h"
@@ -37,6 +39,7 @@
 extern "C" {
 #endif
 
+#if __ANDROID_API__ >= 21
 
 struct AMediaCodec;
 typedef struct AMediaCodec AMediaCodec;
@@ -254,6 +257,8 @@
  */
 media_status_t AMediaCodecCryptoInfo_getEncryptedBytes(AMediaCodecCryptoInfo*, size_t *dst);
 
+#endif /* __ANDROID_API__ >= 21 */
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
diff --git a/include/ndk/NdkMediaCrypto.h b/include/ndk/NdkMediaCrypto.h
index 90374c5..9236765 100644
--- a/include/ndk/NdkMediaCrypto.h
+++ b/include/ndk/NdkMediaCrypto.h
@@ -28,6 +28,7 @@
 #ifndef _NDK_MEDIA_CRYPTO_H
 #define _NDK_MEDIA_CRYPTO_H
 
+#include <sys/cdefs.h>
 #include <sys/types.h>
 #include <stdbool.h>
 
@@ -35,6 +36,8 @@
 extern "C" {
 #endif
 
+#if __ANDROID_API__ >= 21
+
 struct AMediaCrypto;
 typedef struct AMediaCrypto AMediaCrypto;
 
@@ -48,6 +51,7 @@
 
 void AMediaCrypto_delete(AMediaCrypto* crypto);
 
+#endif /* __ANDROID_API__ >= 21 */
 
 #ifdef __cplusplus
 } // extern "C"
diff --git a/include/ndk/NdkMediaDrm.h b/include/ndk/NdkMediaDrm.h
index 3c312a9..9dd6283 100644
--- a/include/ndk/NdkMediaDrm.h
+++ b/include/ndk/NdkMediaDrm.h
@@ -27,14 +27,17 @@
 #ifndef _NDK_MEDIA_DRM_H
 #define _NDK_MEDIA_DRM_H
 
+#include <stdbool.h>
+#include <stdint.h>
+#include <sys/cdefs.h>
+
 #include "NdkMediaError.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-#include <stdint.h>
-#include <stdbool.h>
+#if __ANDROID_API__ >= 21
 
 struct AMediaDrm;
 typedef struct AMediaDrm AMediaDrm;
@@ -448,6 +451,8 @@
         const char *macAlgorithm, uint8_t *keyId, const uint8_t *message, size_t messageSize,
         const uint8_t *signature, size_t signatureSize);
 
+#endif /* __ANDROID_API__ >= 21 */
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
diff --git a/include/ndk/NdkMediaError.h b/include/ndk/NdkMediaError.h
index 60d401b..fb00b1d 100644
--- a/include/ndk/NdkMediaError.h
+++ b/include/ndk/NdkMediaError.h
@@ -28,10 +28,14 @@
 #ifndef _NDK_MEDIA_ERROR_H
 #define _NDK_MEDIA_ERROR_H
 
+#include <sys/cdefs.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+#if __ANDROID_API__ >= 21
+
 typedef enum {
     AMEDIA_OK = 0,
 
@@ -59,6 +63,7 @@
 
 } media_status_t;
 
+#endif /* __ANDROID_API__ >= 21 */
 
 #ifdef __cplusplus
 } // extern "C"
diff --git a/include/ndk/NdkMediaExtractor.h b/include/ndk/NdkMediaExtractor.h
index 7324d31..c3180dc 100644
--- a/include/ndk/NdkMediaExtractor.h
+++ b/include/ndk/NdkMediaExtractor.h
@@ -28,6 +28,7 @@
 #ifndef _NDK_MEDIA_EXTRACTOR_H
 #define _NDK_MEDIA_EXTRACTOR_H
 
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 #include "NdkMediaCodec.h"
@@ -38,6 +39,8 @@
 extern "C" {
 #endif
 
+#if __ANDROID_API__ >= 21
+
 struct AMediaExtractor;
 typedef struct AMediaExtractor AMediaExtractor;
 
@@ -158,6 +161,8 @@
     AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED = 2,
 };
 
+#endif /* __ANDROID_API__ >= 21 */
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
diff --git a/include/ndk/NdkMediaFormat.h b/include/ndk/NdkMediaFormat.h
index ab29791..a2a21d2 100644
--- a/include/ndk/NdkMediaFormat.h
+++ b/include/ndk/NdkMediaFormat.h
@@ -27,6 +27,7 @@
 #ifndef _NDK_MEDIA_FORMAT_H
 #define _NDK_MEDIA_FORMAT_H
 
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 #include "NdkMediaError.h"
@@ -35,6 +36,8 @@
 extern "C" {
 #endif
 
+#if __ANDROID_API__ >= 21
+
 struct AMediaFormat;
 typedef struct AMediaFormat AMediaFormat;
 
@@ -104,6 +107,8 @@
 extern const char* AMEDIAFORMAT_KEY_WIDTH;
 extern const char* AMEDIAFORMAT_KEY_STRIDE;
 
+#endif /* __ANDROID_API__ >= 21 */
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
diff --git a/include/ndk/NdkMediaMuxer.h b/include/ndk/NdkMediaMuxer.h
index 90d946c..25987a2 100644
--- a/include/ndk/NdkMediaMuxer.h
+++ b/include/ndk/NdkMediaMuxer.h
@@ -28,6 +28,7 @@
 #ifndef _NDK_MEDIA_MUXER_H
 #define _NDK_MEDIA_MUXER_H
 
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
 #include "NdkMediaCodec.h"
@@ -38,6 +39,8 @@
 extern "C" {
 #endif
 
+#if __ANDROID_API__ >= 21
+
 struct AMediaMuxer;
 typedef struct AMediaMuxer AMediaMuxer;
 
@@ -112,6 +115,8 @@
 media_status_t AMediaMuxer_writeSampleData(AMediaMuxer *muxer,
         size_t trackIdx, const uint8_t *data, const AMediaCodecBufferInfo *info);
 
+#endif /* __ANDROID_API__ >= 21 */
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
diff --git a/media/libcpustats/Android.mk b/media/libcpustats/Android.mk
index 57fe527..b2d73ee 100644
--- a/media/libcpustats/Android.mk
+++ b/media/libcpustats/Android.mk
@@ -8,6 +8,6 @@
 
 LOCAL_MODULE := libcpustats
 
-LOCAL_CFLAGS := -std=gnu++11 -Werror -Wall
+LOCAL_CFLAGS := -Werror -Wall
 
 include $(BUILD_STATIC_LIBRARY)
diff --git a/media/libeffects/Android.bp b/media/libeffects/Android.bp
new file mode 100644
index 0000000..ccaa2b4
--- /dev/null
+++ b/media/libeffects/Android.bp
@@ -0,0 +1 @@
+subdirs = ["factory"]
diff --git a/media/libeffects/factory/Android.bp b/media/libeffects/factory/Android.bp
new file mode 100644
index 0000000..0d8b6eb
--- /dev/null
+++ b/media/libeffects/factory/Android.bp
@@ -0,0 +1,13 @@
+// Effect factory library
+cc_library_shared {
+    name: "libeffects",
+    srcs: ["EffectsFactory.c"],
+
+    shared_libs: [
+        "libcutils",
+        "liblog",
+        "libdl",
+    ],
+
+    include_dirs: ["system/media/audio_effects/include"],
+}
diff --git a/media/libeffects/factory/Android.mk b/media/libeffects/factory/Android.mk
deleted file mode 100644
index a932af7..0000000
--- a/media/libeffects/factory/Android.mk
+++ /dev/null
@@ -1,19 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-
-# Effect factory library
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES:= \
-	EffectsFactory.c
-
-LOCAL_SHARED_LIBRARIES := \
-	libcutils liblog
-
-LOCAL_MODULE:= libeffects
-
-LOCAL_SHARED_LIBRARIES += libdl
-
-LOCAL_C_INCLUDES := \
-    $(call include-path-for, audio-effects)
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/media/libeffects/lvm/lib/Bass/src/LVDBE_Control.c b/media/libeffects/lvm/lib/Bass/src/LVDBE_Control.c
index ab2a832..b6632a3 100644
--- a/media/libeffects/lvm/lib/Bass/src/LVDBE_Control.c
+++ b/media/libeffects/lvm/lib/Bass/src/LVDBE_Control.c
@@ -334,10 +334,10 @@
                      pParams);                          /* New parameters */
 
         LVC_Mixer_SetTimeConstant(&pBypassMixer_Instance->MixerStream[0],
-            LVDBE_BYPASS_MIXER_TC,pParams->SampleRate,2);
+            LVDBE_BYPASS_MIXER_TC,(LVM_Fs_en)pParams->SampleRate,2);
 
         LVC_Mixer_SetTimeConstant(&pBypassMixer_Instance->MixerStream[1],
-            LVDBE_BYPASS_MIXER_TC,pParams->SampleRate,2);
+            LVDBE_BYPASS_MIXER_TC,(LVM_Fs_en)pParams->SampleRate,2);
 
 
     }
diff --git a/media/libeffects/lvm/lib/Bass/src/LVDBE_Init.c b/media/libeffects/lvm/lib/Bass/src/LVDBE_Init.c
index 35e5bc8..a3623bc 100644
--- a/media/libeffects/lvm/lib/Bass/src/LVDBE_Init.c
+++ b/media/libeffects/lvm/lib/Bass/src/LVDBE_Init.c
@@ -270,7 +270,7 @@
     pBypassMixer_Instance->MixerStream[0].CallbackSet=0;
     LVC_Mixer_Init(&pBypassMixer_Instance->MixerStream[0],0,0);
     LVC_Mixer_SetTimeConstant(&pBypassMixer_Instance->MixerStream[0],
-        LVDBE_BYPASS_MIXER_TC,pInstance->Params.SampleRate,2);
+        LVDBE_BYPASS_MIXER_TC,(LVM_Fs_en)pInstance->Params.SampleRate,2);
     /*
      * Setup the mixer gain for the unprocessed path
      */
@@ -280,8 +280,7 @@
     pBypassMixer_Instance->MixerStream[1].CallbackSet=0;
     LVC_Mixer_Init(&pBypassMixer_Instance->MixerStream[1],0x00007FFF,0x00007FFF);
     LVC_Mixer_SetTimeConstant(&pBypassMixer_Instance->MixerStream[1],
-        LVDBE_BYPASS_MIXER_TC,pInstance->Params.SampleRate,2);
+        LVDBE_BYPASS_MIXER_TC,(LVM_Fs_en)pInstance->Params.SampleRate,2);
 
     return(LVDBE_SUCCESS);
 }
-
diff --git a/media/libeffects/lvm/wrapper/Android.mk b/media/libeffects/lvm/wrapper/Android.mk
index 68ba34c..4e38e3d 100644
--- a/media/libeffects/lvm/wrapper/Android.mk
+++ b/media/libeffects/lvm/wrapper/Android.mk
@@ -18,6 +18,7 @@
 LOCAL_STATIC_LIBRARIES += libmusicbundle
 
 LOCAL_SHARED_LIBRARIES := \
+     liblog \
      libcutils \
      libdl
 
@@ -47,6 +48,7 @@
 LOCAL_STATIC_LIBRARIES += libreverb
 
 LOCAL_SHARED_LIBRARIES := \
+     liblog \
      libcutils \
      libdl
 
diff --git a/media/libeffects/preprocessing/Android.mk b/media/libeffects/preprocessing/Android.mk
index 4e4b094..bd67aa1 100644
--- a/media/libeffects/preprocessing/Android.mk
+++ b/media/libeffects/preprocessing/Android.mk
@@ -16,8 +16,6 @@
     external/webrtc/webrtc/modules/audio_processing/include \
     $(call include-path-for, audio-effects)
 
-LOCAL_C_INCLUDES += $(call include-path-for, speex)
-
 LOCAL_SHARED_LIBRARIES := \
     libwebrtc_audio_preprocessing \
     libspeexresampler \
diff --git a/media/libmedia/Android.bp b/media/libmedia/Android.bp
new file mode 100644
index 0000000..7fde4b2
--- /dev/null
+++ b/media/libmedia/Android.bp
@@ -0,0 +1,10 @@
+cc_library_static {
+    name: "libmedia_helper",
+    srcs: ["AudioParameter.cpp"],
+    cflags: [
+        "-Werror",
+        "-Wno-error=deprecated-declarations",
+        "-Wall",
+    ],
+    clang: true,
+}
diff --git a/media/libmedia/Android.mk b/media/libmedia/Android.mk
index 1b92123..202b5f8 100644
--- a/media/libmedia/Android.mk
+++ b/media/libmedia/Android.mk
@@ -3,18 +3,6 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES:= \
-    AudioParameter.cpp
-LOCAL_MODULE:= libmedia_helper
-LOCAL_MODULE_TAGS := optional
-
-LOCAL_CFLAGS += -Werror -Wno-error=deprecated-declarations -Wall
-LOCAL_CLANG := true
-
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES:= \
     AudioTrack.cpp \
     AudioTrackShared.cpp \
     IAudioFlinger.cpp \
diff --git a/media/libmedia/MediaUtils.cpp b/media/libmedia/MediaUtils.cpp
index a02ca65..dc2bc82 100644
--- a/media/libmedia/MediaUtils.cpp
+++ b/media/libmedia/MediaUtils.cpp
@@ -31,6 +31,11 @@
     size_t numberOfBytes,
     size_t percentageOfTotalMem) {
 
+    if (running_with_asan()) {
+        ALOGW("Running with ASan, skip enforcing memory limitations.");
+        return;
+    }
+
     long pageSize = sysconf(_SC_PAGESIZE);
     long numPages = sysconf(_SC_PHYS_PAGES);
     size_t maxMem = SIZE_MAX;
diff --git a/media/libmedia/MediaUtils.h b/media/libmedia/MediaUtils.h
index f80dd30..a678bcc 100644
--- a/media/libmedia/MediaUtils.h
+++ b/media/libmedia/MediaUtils.h
@@ -19,6 +19,12 @@
 
 namespace android {
 
+extern "C" void __asan_init(void) __attribute__((weak));
+
+static inline int running_with_asan() {
+    return &__asan_init != 0;
+}
+
 /**
    Limit the amount of memory a process can allocate using setrlimit(RLIMIT_AS).
    The value to use will be read from the specified system property, or if the
diff --git a/media/libmediaplayerservice/Android.mk b/media/libmediaplayerservice/Android.mk
index 31a6992..98ccc42 100644
--- a/media/libmediaplayerservice/Android.mk
+++ b/media/libmediaplayerservice/Android.mk
@@ -55,7 +55,6 @@
     $(TOP)/frameworks/native/include/media/openmax                  \
     $(TOP)/frameworks/native/include/media/hardware                 \
     $(TOP)/external/tremolo/Tremolo                                 \
-    libcore/include                                                 \
 
 LOCAL_CFLAGS += -Werror -Wno-error=deprecated-declarations -Wall
 LOCAL_CLANG := true
diff --git a/media/libmediaplayerservice/nuplayer/GenericSource.h b/media/libmediaplayerservice/nuplayer/GenericSource.h
index 9fe49af..0957778 100644
--- a/media/libmediaplayerservice/nuplayer/GenericSource.h
+++ b/media/libmediaplayerservice/nuplayer/GenericSource.h
@@ -117,7 +117,7 @@
     // When necessary, it will send out buffering events to the player.
     struct BufferingMonitor : public AHandler {
     public:
-        BufferingMonitor(const sp<AMessage> &notify);
+        explicit BufferingMonitor(const sp<AMessage> &notify);
 
         // Set up state.
         void prepare(const sp<NuCachedSource2> &cachedSource,
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerStreamListener.h b/media/libmediaplayerservice/nuplayer/NuPlayerStreamListener.h
index 2de829b..0c6f652 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerStreamListener.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerStreamListener.h
@@ -24,7 +24,7 @@
 
 namespace android {
 
-struct MemoryDealer;
+class MemoryDealer;
 
 struct NuPlayer::NuPlayerStreamListener : public BnStreamListener {
     NuPlayerStreamListener(
diff --git a/media/libmediaplayerservice/nuplayer/StreamingSource.cpp b/media/libmediaplayerservice/nuplayer/StreamingSource.cpp
index c4147e1..47ec3e9 100644
--- a/media/libmediaplayerservice/nuplayer/StreamingSource.cpp
+++ b/media/libmediaplayerservice/nuplayer/StreamingSource.cpp
@@ -228,6 +228,10 @@
     }
 
     sp<MetaData> meta = source->getFormat();
+    if (meta == NULL) {
+        format->setInt32("err", -EWOULDBLOCK);
+        return format;
+    }
     status_t err = convertMetaDataToMessage(meta, &format);
     if (err != OK) {
         format->setInt32("err", err);
diff --git a/media/libnbaio/Android.bp b/media/libnbaio/Android.bp
new file mode 100644
index 0000000..615b541
--- /dev/null
+++ b/media/libnbaio/Android.bp
@@ -0,0 +1,36 @@
+cc_library_shared {
+    name: "libnbaio",
+    srcs: [
+        "AudioBufferProviderSource.cpp",
+        "AudioStreamOutSink.cpp",
+        "AudioStreamInSource.cpp",
+        "NBAIO.cpp",
+        "MonoPipe.cpp",
+        "MonoPipeReader.cpp",
+        "Pipe.cpp",
+        "PipeReader.cpp",
+        "SourceAudioBufferProvider.cpp",
+
+        "NBLog.cpp",
+    ],
+
+    // libsndfile license is incompatible; uncomment to use for local debug only
+    // srcs: [
+    //     "LibsndfileSink.cpp",
+    //     "LibsndfileSource.cpp",
+    // ],
+    // static_libs: ["libsndfile"],
+
+    shared_libs: [
+        "libaudioutils",
+        "libbinder",
+        "libcutils",
+        "libutils",
+        "liblog",
+    ],
+
+    cflags: [
+        "-Werror",
+        "-Wall",
+    ],
+}
diff --git a/media/libnbaio/Android.mk b/media/libnbaio/Android.mk
deleted file mode 100644
index e2f416b..0000000
--- a/media/libnbaio/Android.mk
+++ /dev/null
@@ -1,36 +0,0 @@
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := \
-    AudioBufferProviderSource.cpp   \
-    AudioStreamOutSink.cpp          \
-    AudioStreamInSource.cpp         \
-    NBAIO.cpp                       \
-    MonoPipe.cpp                    \
-    MonoPipeReader.cpp              \
-    Pipe.cpp                        \
-    PipeReader.cpp                  \
-    SourceAudioBufferProvider.cpp
-
-LOCAL_SRC_FILES += NBLog.cpp
-
-# libsndfile license is incompatible; uncomment to use for local debug only
-#LOCAL_SRC_FILES += LibsndfileSink.cpp LibsndfileSource.cpp
-#LOCAL_C_INCLUDES += path/to/libsndfile/src
-#LOCAL_STATIC_LIBRARIES += libsndfile
-
-LOCAL_MODULE := libnbaio
-
-LOCAL_SHARED_LIBRARIES := \
-    libaudioutils \
-    libbinder \
-    libcutils \
-    libutils \
-    liblog
-
-LOCAL_C_INCLUDES := $(call include-path-for, audio-utils)
-
-LOCAL_CFLAGS := -Werror -Wall
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/media/libstagefright/Android.bp b/media/libstagefright/Android.bp
new file mode 100644
index 0000000..3d9341b
--- /dev/null
+++ b/media/libstagefright/Android.bp
@@ -0,0 +1 @@
+subdirs = ["foundation"]
diff --git a/media/libstagefright/MediaCodecList.cpp b/media/libstagefright/MediaCodecList.cpp
index f75706b..8afb7e9 100644
--- a/media/libstagefright/MediaCodecList.cpp
+++ b/media/libstagefright/MediaCodecList.cpp
@@ -1165,7 +1165,9 @@
         CHECK(info != NULL);
         AString componentName = info->getCodecName();
 
-        if (!((flags & kHardwareCodecsOnly) && !isSoftwareCodec(componentName))) {
+        if ((flags & kHardwareCodecsOnly) && isSoftwareCodec(componentName)) {
+            ALOGV("skipping SW codec '%s'", componentName.c_str());
+        } else {
             matches->push(componentName);
             ALOGV("matching '%s'", componentName.c_str());
         }
diff --git a/media/libstagefright/SurfaceMediaSource.cpp b/media/libstagefright/SurfaceMediaSource.cpp
index 15ff569..4f1ef30 100644
--- a/media/libstagefright/SurfaceMediaSource.cpp
+++ b/media/libstagefright/SurfaceMediaSource.cpp
@@ -112,7 +112,7 @@
     Mutex::Autolock lock(mMutex);
 
     result.append(buffer);
-    mConsumer->dump(result, "");
+    mConsumer->dumpState(result, "");
 }
 
 status_t SurfaceMediaSource::setFrameRate(int32_t fps)
diff --git a/media/libstagefright/foundation/Android.bp b/media/libstagefright/foundation/Android.bp
new file mode 100644
index 0000000..f7bd3f2
--- /dev/null
+++ b/media/libstagefright/foundation/Android.bp
@@ -0,0 +1,51 @@
+cc_library_shared {
+    name: "libstagefright_foundation",
+
+    srcs: [
+        "AAtomizer.cpp",
+        "ABitReader.cpp",
+        "ABuffer.cpp",
+        "ADebug.cpp",
+        "AHandler.cpp",
+        "AHierarchicalStateMachine.cpp",
+        "ALooper.cpp",
+        "ALooperRoster.cpp",
+        "AMessage.cpp",
+        "ANetworkSession.cpp",
+        "AString.cpp",
+        "AStringUtils.cpp",
+        "AWakeLock.cpp",
+        "ColorUtils.cpp",
+        "MediaBuffer.cpp",
+        "MediaBufferGroup.cpp",
+        "MetaData.cpp",
+        "ParsedMessage.cpp",
+        "base64.cpp",
+        "hexdump.cpp",
+    ],
+
+    include_dirs: ["frameworks/av/include/media/stagefright/foundation"],
+
+    shared_libs: [
+        "libbinder",
+        "libutils",
+        "libcutils",
+        "liblog",
+        "libpowermanager",
+    ],
+
+    export_shared_lib_headers: ["libbinder"],
+
+    cflags: [
+        "-Wno-multichar",
+        "-Werror",
+        "-Wall",
+    ],
+    clang: true,
+    sanitize: {
+        misc_undefined: [
+            "unsigned-integer-overflow",
+            "signed-integer-overflow",
+        ],
+    },
+}
diff --git a/media/libstagefright/foundation/Android.mk b/media/libstagefright/foundation/Android.mk
deleted file mode 100644
index b977c65..0000000
--- a/media/libstagefright/foundation/Android.mk
+++ /dev/null
@@ -1,46 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES:=                 \
-    AAtomizer.cpp                 \
-    ABitReader.cpp                \
-    ABuffer.cpp                   \
-    ADebug.cpp                    \
-    AHandler.cpp                  \
-    AHierarchicalStateMachine.cpp \
-    ALooper.cpp                   \
-    ALooperRoster.cpp             \
-    AMessage.cpp                  \
-    ANetworkSession.cpp           \
-    AString.cpp                   \
-    AStringUtils.cpp              \
-    AWakeLock.cpp                 \
-    ColorUtils.cpp                \
-    MediaBuffer.cpp               \
-    MediaBufferGroup.cpp          \
-    MetaData.cpp                  \
-    ParsedMessage.cpp             \
-    base64.cpp                    \
-    hexdump.cpp
-
-LOCAL_C_INCLUDES:= \
-    frameworks/av/include/media/stagefright/foundation
-
-LOCAL_SHARED_LIBRARIES := \
-        libbinder         \
-        libutils          \
-        libcutils         \
-        liblog            \
-        libpowermanager
-
-LOCAL_EXPORT_SHARED_LIBRARY_HEADERS := libbinder
-
-LOCAL_CFLAGS += -Wno-multichar -Werror -Wall
-LOCAL_CLANG := true
-LOCAL_SANITIZE := unsigned-integer-overflow signed-integer-overflow
-
-LOCAL_MODULE:= libstagefright_foundation
-
-
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/media/libstagefright/id3/ID3.cpp b/media/libstagefright/id3/ID3.cpp
index 35691b9..86b668c 100644
--- a/media/libstagefright/id3/ID3.cpp
+++ b/media/libstagefright/id3/ID3.cpp
@@ -531,6 +531,9 @@
     }
     size_t n = mFrameSize - getHeaderLength() - 1;
     if (otherdata) {
+        if (n < 5) {
+            return;
+        }
         // skip past the encoding, language, and the 0 separator
         frameData += 4;
         int32_t i = n - 4;
diff --git a/media/libstagefright/omx/GraphicBufferSource.cpp b/media/libstagefright/omx/GraphicBufferSource.cpp
index 422f87f..63424f7 100644
--- a/media/libstagefright/omx/GraphicBufferSource.cpp
+++ b/media/libstagefright/omx/GraphicBufferSource.cpp
@@ -921,11 +921,12 @@
  * frameNum: frame number of the frame being released
  * buffer: GraphicBuffer pointer to release (note this must not be & as we
  *         will clear the original mBufferSlot in persistent case)
+ *         Use NOLINT to supress warning on the copy of 'buffer'.
  * fence: fence of the frame being released
  */
 void GraphicBufferSource::releaseBuffer(
         int &id, uint64_t frameNum,
-        const sp<GraphicBuffer> &buffer, const sp<Fence> &fence) {
+        const sp<GraphicBuffer> buffer, const sp<Fence> &fence) {  // NOLINT
     if (mIsPersistent) {
         mConsumer->detachBuffer(id);
         mBufferSlot[id] = NULL;
diff --git a/media/libstagefright/omx/GraphicBufferSource.h b/media/libstagefright/omx/GraphicBufferSource.h
index 3fcab2b..c8b0e62 100644
--- a/media/libstagefright/omx/GraphicBufferSource.h
+++ b/media/libstagefright/omx/GraphicBufferSource.h
@@ -242,7 +242,7 @@
     // Release buffer to the consumer
     void releaseBuffer(
             int &id, uint64_t frameNum,
-            const sp<GraphicBuffer> &buffer, const sp<Fence> &fence);
+            const sp<GraphicBuffer> buffer, const sp<Fence> &fence);
 
     void setLatestBuffer_l(const BufferItem &item, bool dropped);
     bool repeatLatestBuffer_l();
diff --git a/media/mediaserver/Android.mk b/media/mediaserver/Android.mk
index 1738df8..f3f3b1a 100644
--- a/media/mediaserver/Android.mk
+++ b/media/mediaserver/Android.mk
@@ -19,6 +19,7 @@
 	libcamera_client \
 	libcameraservice \
 	libresourcemanagerservice \
+	liblog \
 	libcutils \
 	libmedia \
 	libmediaplayerservice \
diff --git a/media/ndk/Android.bp b/media/ndk/Android.bp
new file mode 100644
index 0000000..1ac1eeb
--- /dev/null
+++ b/media/ndk/Android.bp
@@ -0,0 +1,23 @@
+// Copyright (C) 2016 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.
+
+// Headers module is in frameworks/av/Android.bp because modules are not allowed
+// to refer to headers in parent directories and the headers live in
+// frameworks/av/include.
+
+ndk_library {
+    name: "libmediandk.ndk",
+    symbol_file: "libmediandk.map.txt",
+    first_version: "21",
+}
diff --git a/media/ndk/NdkImageReaderPriv.h b/media/ndk/NdkImageReaderPriv.h
index 48f0953..8b540fa 100644
--- a/media/ndk/NdkImageReaderPriv.h
+++ b/media/ndk/NdkImageReaderPriv.h
@@ -112,7 +112,7 @@
 
     struct FrameListener : public ConsumerBase::FrameAvailableListener {
       public:
-        FrameListener(AImageReader* parent) : mReader(parent) {}
+        explicit FrameListener(AImageReader* parent) : mReader(parent) {}
 
         void onFrameAvailable(const BufferItem& item) override;
 
diff --git a/media/ndk/libmediandk.map.txt b/media/ndk/libmediandk.map.txt
new file mode 100644
index 0000000..7db4d06
--- /dev/null
+++ b/media/ndk/libmediandk.map.txt
@@ -0,0 +1,141 @@
+LIBMEDIANDK {
+  global:
+    AImageReader_acquireLatestImage; # introduced=24
+    AImageReader_acquireNextImage; # introduced=24
+    AImageReader_delete; # introduced=24
+    AImageReader_getFormat; # introduced=24
+    AImageReader_getHeight; # introduced=24
+    AImageReader_getMaxImages; # introduced=24
+    AImageReader_getWidth; # introduced=24
+    AImageReader_getWindow; # introduced=24
+    AImageReader_new; # introduced=24
+    AImageReader_setImageListener; # introduced=24
+    AImage_delete; # introduced=24
+    AImage_getCropRect; # introduced=24
+    AImage_getFormat; # introduced=24
+    AImage_getHeight; # introduced=24
+    AImage_getNumberOfPlanes; # introduced=24
+    AImage_getPlaneData; # introduced=24
+    AImage_getPlanePixelStride; # introduced=24
+    AImage_getPlaneRowStride; # introduced=24
+    AImage_getTimestamp; # introduced=24
+    AImage_getWidth; # introduced=24
+    AMEDIAFORMAT_KEY_AAC_PROFILE; # var
+    AMEDIAFORMAT_KEY_BIT_RATE; # var
+    AMEDIAFORMAT_KEY_CHANNEL_COUNT; # var
+    AMEDIAFORMAT_KEY_CHANNEL_MASK; # var
+    AMEDIAFORMAT_KEY_COLOR_FORMAT; # var
+    AMEDIAFORMAT_KEY_DURATION; # var
+    AMEDIAFORMAT_KEY_FLAC_COMPRESSION_LEVEL; # var
+    AMEDIAFORMAT_KEY_FRAME_RATE; # var
+    AMEDIAFORMAT_KEY_HEIGHT; # var
+    AMEDIAFORMAT_KEY_IS_ADTS; # var
+    AMEDIAFORMAT_KEY_IS_AUTOSELECT; # var
+    AMEDIAFORMAT_KEY_IS_DEFAULT; # var
+    AMEDIAFORMAT_KEY_IS_FORCED_SUBTITLE; # var
+    AMEDIAFORMAT_KEY_I_FRAME_INTERVAL; # var
+    AMEDIAFORMAT_KEY_LANGUAGE; # var
+    AMEDIAFORMAT_KEY_MAX_HEIGHT; # var
+    AMEDIAFORMAT_KEY_MAX_INPUT_SIZE; # var
+    AMEDIAFORMAT_KEY_MAX_WIDTH; # var
+    AMEDIAFORMAT_KEY_MIME; # var
+    AMEDIAFORMAT_KEY_PUSH_BLANK_BUFFERS_ON_STOP; # var
+    AMEDIAFORMAT_KEY_REPEAT_PREVIOUS_FRAME_AFTER; # var
+    AMEDIAFORMAT_KEY_SAMPLE_RATE; # var
+    AMEDIAFORMAT_KEY_STRIDE; # var
+    AMEDIAFORMAT_KEY_WIDTH; # var
+    AMediaCodecCryptoInfo_delete;
+    AMediaCodecCryptoInfo_getClearBytes;
+    AMediaCodecCryptoInfo_getEncryptedBytes;
+    AMediaCodecCryptoInfo_getIV;
+    AMediaCodecCryptoInfo_getKey;
+    AMediaCodecCryptoInfo_getMode;
+    AMediaCodecCryptoInfo_getNumSubSamples;
+    AMediaCodecCryptoInfo_new;
+    AMediaCodecCryptoInfo_setPattern; # introduced=24
+    AMediaCodec_configure;
+    AMediaCodec_createCodecByName;
+    AMediaCodec_createDecoderByType;
+    AMediaCodec_createEncoderByType;
+    AMediaCodec_delete;
+    AMediaCodec_dequeueInputBuffer;
+    AMediaCodec_dequeueOutputBuffer;
+    AMediaCodec_flush;
+    AMediaCodec_getInputBuffer;
+    AMediaCodec_getOutputBuffer;
+    AMediaCodec_getOutputFormat;
+    AMediaCodec_queueInputBuffer;
+    AMediaCodec_queueSecureInputBuffer;
+    AMediaCodec_releaseOutputBuffer;
+    AMediaCodec_releaseOutputBufferAtTime;
+    AMediaCodec_setOutputSurface; # introduced=24
+    AMediaCodec_start;
+    AMediaCodec_stop;
+    AMediaCrypto_delete;
+    AMediaCrypto_isCryptoSchemeSupported;
+    AMediaCrypto_new;
+    AMediaCrypto_requiresSecureDecoderComponent;
+    AMediaDrm_closeSession;
+    AMediaDrm_createByUUID;
+    AMediaDrm_decrypt;
+    AMediaDrm_encrypt;
+    AMediaDrm_getKeyRequest;
+    AMediaDrm_getPropertyByteArray;
+    AMediaDrm_getPropertyString;
+    AMediaDrm_getProvisionRequest;
+    AMediaDrm_getSecureStops;
+    AMediaDrm_isCryptoSchemeSupported;
+    AMediaDrm_openSession;
+    AMediaDrm_provideKeyResponse;
+    AMediaDrm_provideProvisionResponse;
+    AMediaDrm_queryKeyStatus;
+    AMediaDrm_release;
+    AMediaDrm_releaseSecureStops;
+    AMediaDrm_removeKeys;
+    AMediaDrm_restoreKeys;
+    AMediaDrm_setOnEventListener;
+    AMediaDrm_setPropertyByteArray;
+    AMediaDrm_setPropertyString;
+    AMediaDrm_sign;
+    AMediaDrm_verify;
+    AMediaExtractor_advance;
+    AMediaExtractor_delete;
+    AMediaExtractor_getPsshInfo;
+    AMediaExtractor_getSampleCryptoInfo;
+    AMediaExtractor_getSampleFlags;
+    AMediaExtractor_getSampleTime;
+    AMediaExtractor_getSampleTrackIndex;
+    AMediaExtractor_getTrackCount;
+    AMediaExtractor_getTrackFormat;
+    AMediaExtractor_new;
+    AMediaExtractor_readSampleData;
+    AMediaExtractor_seekTo;
+    AMediaExtractor_selectTrack;
+    AMediaExtractor_setDataSource;
+    AMediaExtractor_setDataSourceFd;
+    AMediaExtractor_unselectTrack;
+    AMediaFormat_delete;
+    AMediaFormat_getBuffer;
+    AMediaFormat_getFloat;
+    AMediaFormat_getInt32;
+    AMediaFormat_getInt64;
+    AMediaFormat_getSize;
+    AMediaFormat_getString;
+    AMediaFormat_new;
+    AMediaFormat_setBuffer;
+    AMediaFormat_setFloat;
+    AMediaFormat_setInt32;
+    AMediaFormat_setInt64;
+    AMediaFormat_setString;
+    AMediaFormat_toString;
+    AMediaMuxer_addTrack;
+    AMediaMuxer_delete;
+    AMediaMuxer_new;
+    AMediaMuxer_setLocation;
+    AMediaMuxer_setOrientationHint;
+    AMediaMuxer_start;
+    AMediaMuxer_stop;
+    AMediaMuxer_writeSampleData;
+  local:
+    *;
+};
diff --git a/radio/IRadioService.cpp b/radio/IRadioService.cpp
index 960aab4..be7d21e 100644
--- a/radio/IRadioService.cpp
+++ b/radio/IRadioService.cpp
@@ -88,7 +88,9 @@
         data.writeInt32(handle);
         data.writeStrongBinder(IInterface::asBinder(client));
         ALOGV("attach() config %p withAudio %d region %d type %d",
-              config == NULL ? 0 : config, withAudio, config->region, config->band.type);
+              config == NULL ? 0 : config, withAudio,
+              config == NULL ? 0 : config->region,
+              config == NULL ? 0 : config->band.type);
         if (config == NULL) {
             data.writeInt32(0);
         } else {
diff --git a/services/audioflinger/Android.mk b/services/audioflinger/Android.mk
index 4f826e5..5388adc 100644
--- a/services/audioflinger/Android.mk
+++ b/services/audioflinger/Android.mk
@@ -34,7 +34,6 @@
 LOCAL_C_INCLUDES := \
     $(TOPDIR)frameworks/av/services/audiopolicy \
     $(TOPDIR)external/sonic \
-    libcore/include \
     $(call include-path-for, audio-effects) \
     $(call include-path-for, audio-utils)
 
diff --git a/services/audioflinger/AudioResamplerDyn.cpp b/services/audioflinger/AudioResamplerDyn.cpp
index e615700..b7ca5d9 100644
--- a/services/audioflinger/AudioResamplerDyn.cpp
+++ b/services/audioflinger/AudioResamplerDyn.cpp
@@ -520,7 +520,7 @@
         //        inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
 
         // check inputIndex overflow
-        ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%d > frameCount%d",
+        ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%zu > frameCount%zu",
                 inputIndex, mBuffer.frameCount);
         // Buffer is empty, fetch a new one if necessary (inFrameCount > 0).
         // We may not fetch a new buffer if the existing data is sufficient.
@@ -591,7 +591,7 @@
         // We arrive here when we're finished or when the input buffer runs out.
         // Regardless we need to release the input buffer if we've acquired it.
         if (inputIndex > 0) {  // we've acquired a buffer (alternatively could check frameCount)
-            ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%d) != frameCount(%d)",
+            ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%zu) != frameCount(%zu)",
                     inputIndex, frameCount);  // must have been fully read.
             inputIndex = 0;
             provider->releaseBuffer(&mBuffer);
@@ -603,7 +603,7 @@
     // inputIndex must be zero in all three cases:
     // (1) the buffer never was been acquired; (2) the buffer was
     // released at "done:"; or (3) getNextBuffer() failed.
-    ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%d frameCount:%d  phaseFraction:%u",
+    ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%zu frameCount:%zu  phaseFraction:%u",
             inputIndex, mBuffer.frameCount, phaseFraction);
     ALOG_ASSERT(mBuffer.frameCount == 0); // there must be no frames in the buffer
     mInBuffer.setImpulse(impulse);
diff --git a/services/audioflinger/AutoPark.h b/services/audioflinger/AutoPark.h
index e539e47..9ac7b65 100644
--- a/services/audioflinger/AutoPark.h
+++ b/services/audioflinger/AutoPark.h
@@ -21,7 +21,7 @@
 public:
 
     // Park the specific FastThread, which can be nullptr, in hot idle if not currently idling
-    AutoPark(const sp<T>& fastThread) : mFastThread(fastThread)
+    explicit AutoPark(const sp<T>& fastThread) : mFastThread(fastThread)
     {
         mPreviousCommand = FastThreadState::HOT_IDLE;
         if (fastThread != nullptr) {
diff --git a/services/audioflinger/LinearMap.h b/services/audioflinger/LinearMap.h
index fca14dd..2220a0c 100644
--- a/services/audioflinger/LinearMap.h
+++ b/services/audioflinger/LinearMap.h
@@ -134,7 +134,7 @@
         FIND_METHOD_START_VALUE,             // No samples in history, using start value
     };
 
-    LinearMap(size_t size)
+    explicit LinearMap(size_t size)
             : mSize(size),
               mPos(0), // a circular buffer, so could start anywhere. the first sample is at 1.
               mSamples(0),
diff --git a/services/audiopolicy/common/managerdefinitions/include/AudioRoute.h b/services/audiopolicy/common/managerdefinitions/include/AudioRoute.h
index 67e197f..df54f48 100644
--- a/services/audiopolicy/common/managerdefinitions/include/AudioRoute.h
+++ b/services/audiopolicy/common/managerdefinitions/include/AudioRoute.h
@@ -36,7 +36,7 @@
 class AudioRoute  : public virtual RefBase
 {
 public:
-    AudioRoute(audio_route_type_t type) : mType(type) {}
+    explicit AudioRoute(audio_route_type_t type) : mType(type) {}
 
     void setSources(const AudioPortVector &sources) { mSources = sources; }
     const AudioPortVector &getSources() const { return mSources; }
diff --git a/services/audiopolicy/common/managerdefinitions/include/IOProfile.h b/services/audiopolicy/common/managerdefinitions/include/IOProfile.h
index e3ad44d..ec04ef7 100644
--- a/services/audiopolicy/common/managerdefinitions/include/IOProfile.h
+++ b/services/audiopolicy/common/managerdefinitions/include/IOProfile.h
@@ -110,13 +110,13 @@
 class InputProfile : public IOProfile
 {
 public:
-    InputProfile(const String8 &name) : IOProfile(name, AUDIO_PORT_ROLE_SINK) {}
+    explicit InputProfile(const String8 &name) : IOProfile(name, AUDIO_PORT_ROLE_SINK) {}
 };
 
 class OutputProfile : public IOProfile
 {
 public:
-    OutputProfile(const String8 &name) : IOProfile(name, AUDIO_PORT_ROLE_SOURCE) {}
+    explicit OutputProfile(const String8 &name) : IOProfile(name, AUDIO_PORT_ROLE_SOURCE) {}
 };
 
 }; // namespace android
diff --git a/services/audiopolicy/service/AudioPolicyClientImplLegacy.cpp b/services/audiopolicy/service/AudioPolicyClientImplLegacy.cpp
index 151d066..dabffe6 100644
--- a/services/audiopolicy/service/AudioPolicyClientImplLegacy.cpp
+++ b/services/audiopolicy/service/AudioPolicyClientImplLegacy.cpp
@@ -56,7 +56,7 @@
     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
     if (af == 0) {
         ALOGW("%s: could not get AudioFlinger", __func__);
-        return 0;
+        return AUDIO_MODULE_HANDLE_NONE;
     }
 
     return af->loadHwModule(name);
diff --git a/services/camera/libcameraservice/device3/Camera3BufferManager.h b/services/camera/libcameraservice/device3/Camera3BufferManager.h
index ab6541e..b5b86a3 100644
--- a/services/camera/libcameraservice/device3/Camera3BufferManager.h
+++ b/services/camera/libcameraservice/device3/Camera3BufferManager.h
@@ -44,7 +44,7 @@
  */
 class Camera3BufferManager: public virtual RefBase {
 public:
-    Camera3BufferManager(const sp<IGraphicBufferAlloc>& allocator = NULL);
+    explicit Camera3BufferManager(const sp<IGraphicBufferAlloc>& allocator = NULL);
 
     virtual ~Camera3BufferManager();
 
@@ -195,7 +195,7 @@
     struct GraphicBufferEntry {
         sp<GraphicBuffer> graphicBuffer;
         int fenceFd;
-        GraphicBufferEntry(const sp<GraphicBuffer>& gb = 0, int fd = -1) :
+        explicit GraphicBufferEntry(const sp<GraphicBuffer>& gb = 0, int fd = -1) :
             graphicBuffer(gb),
             fenceFd(fd) {}
     };
diff --git a/services/camera/libcameraservice/device3/Camera3OutputStream.h b/services/camera/libcameraservice/device3/Camera3OutputStream.h
index 7f7d6d2..46c1465 100644
--- a/services/camera/libcameraservice/device3/Camera3OutputStream.h
+++ b/services/camera/libcameraservice/device3/Camera3OutputStream.h
@@ -46,7 +46,7 @@
     uint32_t combinedUsage;
     size_t totalBufferCount;
     bool isConfigured;
-    StreamInfo(int id = CAMERA3_STREAM_ID_INVALID,
+    explicit StreamInfo(int id = CAMERA3_STREAM_ID_INVALID,
             int setId = CAMERA3_STREAM_SET_ID_INVALID,
             uint32_t w = 0,
             uint32_t h = 0,