Move mediaswcodec service to APEX

bug: 127499775
test:
- adb shell lshal debug android.hardware.media.c2@1.0::IComponentStore/software
check all software c2 codecs are still listed
- clean-built image shouldn't have mediaswcodec in /system/bin
- atest CtsMediaTestCases -- --module-arg CtsMediaTestCases:size:small
- atest media_swcodec_e2e_tests
Change-Id: I96df803c29aa595dfd2b51d5a68d6db2c08726f3
diff --git a/services/mediacodec/Android.bp b/services/mediacodec/Android.bp
new file mode 100644
index 0000000..25c36fa
--- /dev/null
+++ b/services/mediacodec/Android.bp
@@ -0,0 +1,62 @@
+cc_binary {
+    name: "mediaswcodec",
+    vendor_available: true,
+
+    srcs: [
+        "main_swcodecservice.cpp",
+    ],
+
+    shared_libs: [
+        "libavservices_minijail",
+        "libbase",
+        "libhidltransport",
+        "libhwbinder",
+        "liblog",
+        "libmedia_codecserviceregistrant",
+    ],
+
+    target: {
+        vendor: {
+            exclude_shared_libs: ["libavservices_minijail"],
+            shared_libs: ["libavservices_minijail_vendor"],
+        },
+    },
+
+    header_libs: [
+        "libmedia_headers",
+    ],
+
+    init_rc: ["mediaswcodec.rc"],
+
+    required: ["mediaswcodec.policy"],
+
+    cflags: [
+        "-Werror",
+        "-Wall",
+        "-Wno-error=deprecated-declarations",
+    ],
+
+    sanitize: {
+        scudo: true,
+    },
+}
+
+prebuilt_etc {
+    name: "mediaswcodec.policy",
+    sub_dir: "seccomp_policy",
+    arch: {
+        arm: {
+            src: "seccomp_policy/mediaswcodec-arm.policy",
+        },
+        arm64: {
+            src: "seccomp_policy/mediaswcodec-arm64.policy",
+        },
+        x86: {
+            src: "seccomp_policy/mediacodec-x86.policy",
+        },
+        x86_64: {
+            src: "seccomp_policy/mediacodec-x86.policy",
+        },
+    },
+    required: ["crash_dump.policy"],
+}
diff --git a/services/mediacodec/Android.mk b/services/mediacodec/Android.mk
index f78c671..9cc19a3 100644
--- a/services/mediacodec/Android.mk
+++ b/services/mediacodec/Android.mk
@@ -27,8 +27,8 @@
 include $(CLEAR_VARS)
 # seccomp is not required for coverage build.
 ifneq ($(NATIVE_COVERAGE),true)
-LOCAL_REQUIRED_MODULES_arm := crash_dump.policy mediacodec.policy
-LOCAL_REQUIRED_MODULES_x86 := crash_dump.policy mediacodec.policy
+LOCAL_REQUIRED_MODULES_arm := mediacodec.policy
+LOCAL_REQUIRED_MODULES_x86 := mediacodec.policy
 endif
 LOCAL_SRC_FILES := main_codecservice.cpp
 LOCAL_SHARED_LIBRARIES := \
@@ -65,74 +65,13 @@
 
 ####################################################################
 
-# service executable
-include $(CLEAR_VARS)
-# seccomp is not required for coverage build.
-ifneq ($(NATIVE_COVERAGE),true)
-LOCAL_REQUIRED_MODULES_arm := crash_dump.policy mediaswcodec.policy
-LOCAL_REQUIRED_MODULES_arm64 := crash_dump.policy mediaswcodec.policy
-LOCAL_REQUIRED_MODULES_x86 := crash_dump.policy mediaswcodec.policy
-LOCAL_REQUIRED_MODULES_x86_64 := crash_dump.policy mediaswcodec.policy
-endif
-
-LOCAL_SRC_FILES := \
-    main_swcodecservice.cpp \
-    MediaCodecUpdateService.cpp \
-
-sanitizer_runtime_libraries := $(call normalize-path-list,$(addsuffix .so,\
-  $(ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
-  $(UBSAN_RUNTIME_LIBRARY) \
-  $(TSAN_RUNTIME_LIBRARY) \
-  $(2ND_ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
-  $(2ND_UBSAN_RUNTIME_LIBRARY) \
-  $(2ND_TSAN_RUNTIME_LIBRARY)))
-
-# $(info Sanitizer:  $(sanitizer_runtime_libraries))
-
-llndk_libraries := $(call normalize-path-list,$(addsuffix .so,\
-  $(LLNDK_LIBRARIES)))
-
-# $(info LLNDK:  $(llndk_libraries))
-
-LOCAL_CFLAGS := -DLINKED_LIBRARIES='"$(sanitizer_runtime_libraries):$(llndk_libraries)"'
-
-LOCAL_SHARED_LIBRARIES := \
-    libavservices_minijail \
-    libbase \
-    libbinder \
-    libcutils \
-    libhidltransport \
-    libhwbinder \
-    liblog \
-    libmedia \
-    libutils \
-    libziparchive \
-
-LOCAL_HEADER_LIBRARIES := \
-    libnativeloader-dummy-headers \
-
-LOCAL_MODULE := mediaswcodec
-LOCAL_INIT_RC := mediaswcodec.rc
-LOCAL_SANITIZE := scudo
-ifeq ($(TARGET_ARCH), $(filter $(TARGET_ARCH), x86_64 arm64))
-  LOCAL_MULTILIB := both
-  LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
-  LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)
-endif
-
-sanitizer_runtime_libraries :=
-llndk_libraries :=
-
-include $(BUILD_EXECUTABLE)
-
-####################################################################
-
 # service seccomp policy
 ifeq ($(TARGET_ARCH), $(filter $(TARGET_ARCH), x86 x86_64 arm arm64))
 include $(CLEAR_VARS)
 LOCAL_MODULE := mediacodec.policy
 LOCAL_MODULE_CLASS := ETC
 LOCAL_MODULE_PATH := $(TARGET_OUT)/etc/seccomp_policy
+LOCAL_REQUIRED_MODULES := crash_dump.policy
 # mediacodec runs in 32-bit combatibility mode. For 64 bit architectures,
 # use the 32 bit policy
 ifdef TARGET_2ND_ARCH
@@ -149,14 +88,5 @@
 
 ####################################################################
 
-# sw service seccomp policy
-ifeq ($(TARGET_ARCH), $(filter $(TARGET_ARCH), x86 x86_64 arm arm64))
-include $(CLEAR_VARS)
-LOCAL_MODULE := mediaswcodec.policy
-LOCAL_MODULE_CLASS := ETC
-LOCAL_MODULE_PATH := $(TARGET_OUT)/etc/seccomp_policy
-LOCAL_SRC_FILES := seccomp_policy/mediaswcodec-$(TARGET_ARCH).policy
-include $(BUILD_PREBUILT)
-endif
 
 include $(call all-makefiles-under, $(LOCAL_PATH))
diff --git a/services/mediacodec/MediaCodecUpdateService.cpp b/services/mediacodec/MediaCodecUpdateService.cpp
deleted file mode 100644
index 50ccbce..0000000
--- a/services/mediacodec/MediaCodecUpdateService.cpp
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Copyright 2018 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_TAG "MediaCodecUpdateService"
-//#define LOG_NDEBUG 0
-
-#include <android/dlext.h>
-#include <dlfcn.h>
-#include <media/CodecServiceRegistrant.h>
-#include <nativeloader/dlext_namespaces.h>
-#include <utils/Log.h>
-#include <utils/String8.h>
-
-#include "MediaCodecUpdateService.h"
-
-namespace android {
-
-void loadFromApex(const char *libDirPath) {
-    ALOGV("loadFromApex: path=%s", libDirPath);
-
-    String8 libPath = String8(libDirPath) + "/libmedia_codecserviceregistrant.so";
-
-    android_namespace_t *codecNs = android_create_namespace("codecs",
-            nullptr,  // ld_library_path
-            libDirPath,
-            ANDROID_NAMESPACE_TYPE_ISOLATED,
-            nullptr,  // permitted_when_isolated_path
-            nullptr); // parent
-
-    if (codecNs == nullptr) {
-        ALOGE("Failed to create codec namespace");
-        return;
-    }
-
-    String8 linked_libraries(LINKED_LIBRARIES);
-    if (!android_link_namespaces(codecNs, nullptr, linked_libraries.c_str())) {
-        ALOGE("Failed to link namespace");
-        return;
-    }
-
-    const android_dlextinfo dlextinfo = {
-            .flags = ANDROID_DLEXT_USE_NAMESPACE,
-            .library_namespace = codecNs,
-    };
-
-    void *registrantLib = android_dlopen_ext(
-            libPath.string(),
-            RTLD_NOW | RTLD_LOCAL, &dlextinfo);
-
-    if (registrantLib == nullptr) {
-        ALOGE("Failed to load lib from archive: %s", dlerror());
-    }
-
-    RegisterCodecServicesFunc registerCodecServices =
-            reinterpret_cast<RegisterCodecServicesFunc>(
-            dlsym(registrantLib, "RegisterCodecServices"));
-
-    if (registerCodecServices == nullptr) {
-        ALOGE("Cannot register codec services -- corrupted library.");
-        return;
-    }
-
-    registerCodecServices();
-}
-
-}   // namespace android
diff --git a/services/mediacodec/MediaCodecUpdateService.h b/services/mediacodec/MediaCodecUpdateService.h
deleted file mode 100644
index 09d6dbe..0000000
--- a/services/mediacodec/MediaCodecUpdateService.h
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Copyright 2018 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_MEDIA_CODEC_UPDATE_SERVICE_H
-#define ANDROID_MEDIA_CODEC_UPDATE_SERVICE_H
-
-namespace android {
-
-void loadFromApex(const char *libDirPath);
-
-}   // namespace android
-
-#endif  // ANDROID_MEDIA_CODEC_UPDATE_SERVICE_H
diff --git a/services/mediacodec/main_swcodecservice.cpp b/services/mediacodec/main_swcodecservice.cpp
index 05b5695..c44be28 100644
--- a/services/mediacodec/main_swcodecservice.cpp
+++ b/services/mediacodec/main_swcodecservice.cpp
@@ -19,24 +19,26 @@
 
 // from LOCAL_C_INCLUDES
 #include "minijail.h"
-
 #include <hidl/HidlTransportSupport.h>
 
-#include "MediaCodecUpdateService.h"
-
 using namespace android;
 
+// kSystemSeccompPolicyPath points to the policy for the swcodecs themselves and
+// is part of the updates. kVendorSeccompPolicyPath points to any additional
+// policies that the vendor may need for the device.
 static const char kSystemSeccompPolicyPath[] =
-        "/system/etc/seccomp_policy/mediaswcodec.policy";
+        "/apex/com.android.media.swcodec/etc/seccomp_policy/mediaswcodec.policy";
 static const char kVendorSeccompPolicyPath[] =
         "/vendor/etc/seccomp_policy/mediaswcodec.policy";
 
 // Disable Scudo's mismatch allocation check, as it is being triggered
 // by some third party code.
 extern "C" const char *__scudo_default_options() {
-  return "DeallocationTypeMismatch=false";
+    return "DeallocationTypeMismatch=false";
 }
 
+extern "C" void RegisterCodecServices();
+
 int main(int argc __unused, char** /*argv*/)
 {
     LOG(INFO) << "media swcodec service starting";
@@ -45,11 +47,7 @@
 
     ::android::hardware::configureRpcThreadpool(64, false);
 
-#ifdef __LP64__
-    loadFromApex("/apex/com.android.media.swcodec/lib64");
-#else
-    loadFromApex("/apex/com.android.media.swcodec/lib");
-#endif
+    RegisterCodecServices();
 
     ::android::hardware::joinRpcThreadpool();
 }