Move Codec2-related code from hardware/google/av

Test: None
Bug: 112362730
Change-Id: Ie2f8ff431d65c40333f267ab9877d47089adeea4
diff --git a/media/codec2/hidl/services/Android.bp b/media/codec2/hidl/services/Android.bp
new file mode 100644
index 0000000..7a835ab
--- /dev/null
+++ b/media/codec2/hidl/services/Android.bp
@@ -0,0 +1,94 @@
+cc_binary {
+    name: "hardware.google.media.c2@1.0-service",
+    defaults: ["hidl_defaults"],
+    soc_specific: true,
+    relative_install_path: "hw",
+    srcs: [
+        "vendor.cpp",
+    ],
+
+    init_rc: ["hardware.google.media.c2@1.0-service.rc"],
+
+    shared_libs: [
+        "android.hardware.media.omx@1.0",
+        "hardware.google.media.c2@1.0",
+        "libavservices_minijail_vendor",
+        "libbinder",
+        "libcodec2_hidl_utils@1.0",
+        "libhidlbase",
+        "libhidltransport",
+        "libhwbinder",
+        "liblog",
+        "libstagefright_codec2_vndk",
+        "libstagefright_omx",
+        "libstagefright_xmlparser",
+        "libutils",
+    ],
+
+    arch: {
+        arm: {
+            required: ["codec2.vendor.base.policy"],
+        },
+        x86: {
+            required: ["codec2.vendor.base.policy"],
+        },
+    },
+
+    compile_multilib: "32",
+}
+
+cc_library_shared {
+    name: "libmedia_codecserviceregistrant",
+    // need vendor version for update packaging, system version may have more dependencies
+    vendor_available: true,
+    srcs: [
+        "C2SoftwareCodecServiceRegistrant.cpp",
+    ],
+
+    header_libs: [
+        "libmedia_headers",
+    ],
+
+    shared_libs: [
+        "hardware.google.media.c2@1.0",
+        "liblog",
+        "libcodec2_hidl_utils@1.0",
+        "libstagefright_codec2_vndk",
+        "libutils",
+    ],
+
+    // Codecs
+    runtime_libs: [
+        "libstagefright_soft_c2avcdec",
+        "libstagefright_soft_c2avcenc",
+        "libstagefright_soft_c2aacdec",
+        "libstagefright_soft_c2aacenc",
+        "libstagefright_soft_c2amrnbdec",
+        "libstagefright_soft_c2amrnbenc",
+        "libstagefright_soft_c2amrwbdec",
+        "libstagefright_soft_c2amrwbenc",
+        "libstagefright_soft_c2hevcdec",
+        "libstagefright_soft_c2g711alawdec",
+        "libstagefright_soft_c2g711mlawdec",
+        "libstagefright_soft_c2mpeg2dec",
+        "libstagefright_soft_c2h263dec",
+        "libstagefright_soft_c2h263enc",
+        "libstagefright_soft_c2mpeg4dec",
+        "libstagefright_soft_c2mpeg4enc",
+        "libstagefright_soft_c2mp3dec",
+        "libstagefright_soft_c2vorbisdec",
+        "libstagefright_soft_c2opusdec",
+        "libstagefright_soft_c2vp8dec",
+        "libstagefright_soft_c2vp9dec",
+        "libstagefright_soft_c2vp8enc",
+        "libstagefright_soft_c2vp9enc",
+        "libstagefright_soft_c2rawdec",
+        "libstagefright_soft_c2flacdec",
+        "libstagefright_soft_c2flacenc",
+        "libstagefright_soft_c2gsmdec",
+        "libstagefright_soft_c2xaacdec",
+    ],
+
+    compile_multilib: "32",
+}
+
diff --git a/media/codec2/hidl/services/Android.mk b/media/codec2/hidl/services/Android.mk
new file mode 100644
index 0000000..d9b28e7
--- /dev/null
+++ b/media/codec2/hidl/services/Android.mk
@@ -0,0 +1,24 @@
+LOCAL_PATH := $(call my-dir)
+
+# vendor service seccomp policy
+ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH), x86 x86_64 arm arm64))
+include $(CLEAR_VARS)
+LOCAL_MODULE := codec2.vendor.base.policy
+LOCAL_VENDOR_MODULE := true
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR)/etc/seccomp_policy
+LOCAL_REQUIRED_MODULES := crash_dump.policy
+ifdef TARGET_2ND_ARCH
+    ifneq ($(TARGET_TRANSLATE_2ND_ARCH),true)
+        LOCAL_SRC_FILES := seccomp_policy/codec2.vendor.base-$(TARGET_2ND_ARCH).policy
+    else
+        LOCAL_SRC_FILES := seccomp_policy/codec2.vendor.base-$(TARGET_ARCH).policy
+    endif
+else
+    LOCAL_SRC_FILES := seccomp_policy/codec2.vendor.base-$(TARGET_ARCH).policy
+endif
+include $(BUILD_PREBUILT)
+endif
+
+include $(call all-makefiles-under, $(LOCAL_PATH))
+
diff --git a/media/codec2/hidl/services/C2SoftwareCodecServiceRegistrant.cpp b/media/codec2/hidl/services/C2SoftwareCodecServiceRegistrant.cpp
new file mode 100644
index 0000000..04efa44
--- /dev/null
+++ b/media/codec2/hidl/services/C2SoftwareCodecServiceRegistrant.cpp
@@ -0,0 +1,41 @@
+/*
+ * 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_NDEBUG 0
+#define LOG_TAG "C2SoftwareCodecServiceRegistrant"
+
+#include <C2PlatformSupport.h>
+#include <codec2/hidl/1.0/ComponentStore.h>
+#include <media/CodecServiceRegistrant.h>
+#include <log/log.h>
+
+extern "C" void RegisterCodecServices() {
+    using namespace ::hardware::google::media::c2::V1_0;
+    android::sp<IComponentStore> store =
+        new utils::ComponentStore(
+                android::GetCodec2PlatformComponentStore());
+    if (store == nullptr) {
+        ALOGE("Cannot create Codec2's IComponentStore software service.");
+    } else {
+        if (store->registerAsService("software") != android::OK) {
+            ALOGE("Cannot register Codec2's "
+                    "IComponentStore software service.");
+        } else {
+            ALOGI("Codec2's IComponentStore software service created.");
+        }
+    }
+}
+
diff --git a/media/codec2/hidl/services/hardware.google.media.c2@1.0-service.rc b/media/codec2/hidl/services/hardware.google.media.c2@1.0-service.rc
new file mode 100644
index 0000000..0640e6a
--- /dev/null
+++ b/media/codec2/hidl/services/hardware.google.media.c2@1.0-service.rc
@@ -0,0 +1,7 @@
+service hardware-google-media-c2-hal-1-0 /vendor/bin/hw/hardware.google.media.c2@1.0-service
+    class hal
+    user mediacodec
+    group camera mediadrm drmrpc
+    ioprio rt 4
+    writepid /dev/cpuset/foreground/tasks
+
diff --git a/media/codec2/hidl/services/seccomp_policy/codec2.software.base-arm.policy b/media/codec2/hidl/services/seccomp_policy/codec2.software.base-arm.policy
new file mode 100644
index 0000000..d5871d1
--- /dev/null
+++ b/media/codec2/hidl/services/seccomp_policy/codec2.software.base-arm.policy
@@ -0,0 +1,73 @@
+# Copyright (C) 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.
+
+# Organized by frequency of systemcall - in descending order for
+# best performance.
+futex: 1
+ioctl: 1
+write: 1
+prctl: 1
+clock_gettime: 1
+getpriority: 1
+read: 1
+close: 1
+writev: 1
+dup: 1
+ppoll: 1
+mmap2: 1
+getrandom: 1
+
+# mremap: Ensure |flags| are (MREMAP_MAYMOVE | MREMAP_FIXED) TODO: Once minijail
+# parser support for '<' is in this needs to be modified to also prevent
+# |old_address| and |new_address| from touching the exception vector page, which
+# on ARM is statically loaded at 0xffff 0000. See
+# http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0211h/Babfeega.html
+# for more details.
+mremap: arg3 == 3
+munmap: 1
+mprotect: 1
+madvise: 1
+openat: 1
+sigaltstack: 1
+clone: 1
+setpriority: 1
+getuid32: 1
+fstat64: 1
+fstatfs64: 1
+pread64: 1
+faccessat: 1
+readlinkat: 1
+exit: 1
+rt_sigprocmask: 1
+set_tid_address: 1
+restart_syscall: 1
+exit_group: 1
+rt_sigreturn: 1
+pipe2: 1
+gettimeofday: 1
+sched_yield: 1
+nanosleep: 1
+lseek: 1
+_llseek: 1
+sched_get_priority_max: 1
+sched_get_priority_min: 1
+statfs64: 1
+sched_setscheduler: 1
+fstatat64: 1
+ugetrlimit: 1
+getdents64: 1
+getrandom: 1
+
+@include /system/etc/seccomp_policy/crash_dump.arm.policy
+
diff --git a/media/codec2/hidl/services/seccomp_policy/codec2.software.base-x86.policy b/media/codec2/hidl/services/seccomp_policy/codec2.software.base-x86.policy
new file mode 100644
index 0000000..20c7625
--- /dev/null
+++ b/media/codec2/hidl/services/seccomp_policy/codec2.software.base-x86.policy
@@ -0,0 +1,57 @@
+# Copyright (C) 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.
+
+read: 1
+mprotect: 1
+prctl: 1
+openat: 1
+getuid32: 1
+writev: 1
+ioctl: 1
+close: 1
+mmap2: 1
+fstat64: 1
+madvise: 1
+fstatat64: 1
+futex: 1
+munmap: 1
+faccessat: 1
+_llseek: 1
+lseek: 1
+clone: 1
+sigaltstack: 1
+setpriority: 1
+restart_syscall: 1
+exit: 1
+exit_group: 1
+rt_sigreturn: 1
+ugetrlimit: 1
+readlinkat: 1
+_llseek: 1
+fstatfs64: 1
+pread64: 1
+mremap: 1
+dup: 1
+set_tid_address: 1
+write: 1
+nanosleep: 1
+
+# Required by AddressSanitizer
+gettid: 1
+sched_yield: 1
+getpid: 1
+gettid: 1
+
+@include /system/etc/seccomp_policy/crash_dump.x86.policy
+
diff --git a/media/codec2/hidl/services/seccomp_policy/codec2.vendor.base-arm.policy b/media/codec2/hidl/services/seccomp_policy/codec2.vendor.base-arm.policy
new file mode 100644
index 0000000..d5871d1
--- /dev/null
+++ b/media/codec2/hidl/services/seccomp_policy/codec2.vendor.base-arm.policy
@@ -0,0 +1,73 @@
+# Copyright (C) 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.
+
+# Organized by frequency of systemcall - in descending order for
+# best performance.
+futex: 1
+ioctl: 1
+write: 1
+prctl: 1
+clock_gettime: 1
+getpriority: 1
+read: 1
+close: 1
+writev: 1
+dup: 1
+ppoll: 1
+mmap2: 1
+getrandom: 1
+
+# mremap: Ensure |flags| are (MREMAP_MAYMOVE | MREMAP_FIXED) TODO: Once minijail
+# parser support for '<' is in this needs to be modified to also prevent
+# |old_address| and |new_address| from touching the exception vector page, which
+# on ARM is statically loaded at 0xffff 0000. See
+# http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0211h/Babfeega.html
+# for more details.
+mremap: arg3 == 3
+munmap: 1
+mprotect: 1
+madvise: 1
+openat: 1
+sigaltstack: 1
+clone: 1
+setpriority: 1
+getuid32: 1
+fstat64: 1
+fstatfs64: 1
+pread64: 1
+faccessat: 1
+readlinkat: 1
+exit: 1
+rt_sigprocmask: 1
+set_tid_address: 1
+restart_syscall: 1
+exit_group: 1
+rt_sigreturn: 1
+pipe2: 1
+gettimeofday: 1
+sched_yield: 1
+nanosleep: 1
+lseek: 1
+_llseek: 1
+sched_get_priority_max: 1
+sched_get_priority_min: 1
+statfs64: 1
+sched_setscheduler: 1
+fstatat64: 1
+ugetrlimit: 1
+getdents64: 1
+getrandom: 1
+
+@include /system/etc/seccomp_policy/crash_dump.arm.policy
+
diff --git a/media/codec2/hidl/services/seccomp_policy/codec2.vendor.base-x86.policy b/media/codec2/hidl/services/seccomp_policy/codec2.vendor.base-x86.policy
new file mode 100644
index 0000000..20c7625
--- /dev/null
+++ b/media/codec2/hidl/services/seccomp_policy/codec2.vendor.base-x86.policy
@@ -0,0 +1,57 @@
+# Copyright (C) 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.
+
+read: 1
+mprotect: 1
+prctl: 1
+openat: 1
+getuid32: 1
+writev: 1
+ioctl: 1
+close: 1
+mmap2: 1
+fstat64: 1
+madvise: 1
+fstatat64: 1
+futex: 1
+munmap: 1
+faccessat: 1
+_llseek: 1
+lseek: 1
+clone: 1
+sigaltstack: 1
+setpriority: 1
+restart_syscall: 1
+exit: 1
+exit_group: 1
+rt_sigreturn: 1
+ugetrlimit: 1
+readlinkat: 1
+_llseek: 1
+fstatfs64: 1
+pread64: 1
+mremap: 1
+dup: 1
+set_tid_address: 1
+write: 1
+nanosleep: 1
+
+# Required by AddressSanitizer
+gettid: 1
+sched_yield: 1
+getpid: 1
+gettid: 1
+
+@include /system/etc/seccomp_policy/crash_dump.x86.policy
+
diff --git a/media/codec2/hidl/services/vendor.cpp b/media/codec2/hidl/services/vendor.cpp
new file mode 100644
index 0000000..149d4f9
--- /dev/null
+++ b/media/codec2/hidl/services/vendor.cpp
@@ -0,0 +1,150 @@
+/*
+ * 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_NDEBUG 0
+#define LOG_TAG "hardware.google.media.c2@1.0-service"
+
+#include <codec2/hidl/1.0/ComponentStore.h>
+#include <hidl/HidlTransportSupport.h>
+#include <binder/ProcessState.h>
+#include <minijail.h>
+
+#include <C2Component.h>
+
+// OmxStore is added for visibility by dumpstate.
+#include <media/stagefright/omx/1.0/OmxStore.h>
+
+// This is created by module "codec2.vendor.base.policy". This can be modified.
+static constexpr char kBaseSeccompPolicyPath[] =
+        "/vendor/etc/seccomp_policy/codec2.vendor.base.policy";
+
+// Additional device-specific seccomp permissions can be added in this file.
+static constexpr char kExtSeccompPolicyPath[] =
+        "/vendor/etc/seccomp_policy/codec2.vendor.ext.policy";
+
+class DummyC2Store : public C2ComponentStore {
+public:
+    DummyC2Store() = default;
+
+    virtual ~DummyC2Store() override = default;
+
+    virtual C2String getName() const override {
+        return "default";
+    }
+
+    virtual c2_status_t createComponent(
+            C2String /*name*/,
+            std::shared_ptr<C2Component>* const /*component*/) override {
+        return C2_NOT_FOUND;
+    }
+
+    virtual c2_status_t createInterface(
+            C2String /* name */,
+            std::shared_ptr<C2ComponentInterface>* const /* interface */) override {
+        return C2_NOT_FOUND;
+    }
+
+    virtual std::vector<std::shared_ptr<const C2Component::Traits>>
+            listComponents() override {
+        return {};
+    }
+
+    virtual c2_status_t copyBuffer(
+            std::shared_ptr<C2GraphicBuffer> /* src */,
+            std::shared_ptr<C2GraphicBuffer> /* dst */) override {
+        return C2_OMITTED;
+    }
+
+    virtual c2_status_t query_sm(
+        const std::vector<C2Param*>& /* stackParams */,
+        const std::vector<C2Param::Index>& /* heapParamIndices */,
+        std::vector<std::unique_ptr<C2Param>>* const /* heapParams */) const override {
+        return C2_OMITTED;
+    }
+
+    virtual c2_status_t config_sm(
+            const std::vector<C2Param*>& /* params */,
+            std::vector<std::unique_ptr<C2SettingResult>>* const /* failures */) override {
+        return C2_OMITTED;
+    }
+
+    virtual std::shared_ptr<C2ParamReflector> getParamReflector() const override {
+        return nullptr;
+    }
+
+    virtual c2_status_t querySupportedParams_nb(
+            std::vector<std::shared_ptr<C2ParamDescriptor>>* const /* params */) const override {
+        return C2_OMITTED;
+    }
+
+    virtual c2_status_t querySupportedValues_sm(
+            std::vector<C2FieldSupportedValuesQuery>& /* fields */) const override {
+        return C2_OMITTED;
+    }
+};
+
+int main(int /* argc */, char** /* argv */) {
+    ALOGD("hardware.google.media.c2@1.0-service starting...");
+
+    signal(SIGPIPE, SIG_IGN);
+    android::SetUpMinijail(kBaseSeccompPolicyPath, kExtSeccompPolicyPath);
+
+    // vndbinder is needed by BufferQueue.
+    android::ProcessState::initWithDriver("/dev/vndbinder");
+    android::ProcessState::self()->startThreadPool();
+
+    // Extra threads may be needed to handle a stacked IPC sequence that
+    // contains alternating binder and hwbinder calls. (See b/35283480.)
+    android::hardware::configureRpcThreadpool(8, true /* callerWillJoin */);
+
+    // Create IComponentStore service.
+    {
+        using namespace ::hardware::google::media::c2::V1_0;
+        android::sp<IComponentStore> store;
+
+        // Vendor's TODO: Replace this with
+        // store = new utils::ComponentStore(
+        //         /* implementation of C2ComponentStore */);
+        ALOGD("Instantiating Codec2's dummy IComponentStore service...");
+        store = new utils::ComponentStore(
+                std::make_shared<DummyC2Store>());
+
+        if (store == nullptr) {
+            ALOGE("Cannot create Codec2's IComponentStore service.");
+        } else {
+            if (store->registerAsService("default") != android::OK) {
+                ALOGE("Cannot register Codec2's "
+                        "IComponentStore service.");
+            } else {
+                ALOGI("Codec2's IComponentStore service created.");
+            }
+        }
+    }
+
+    // Register IOmxStore service.
+    {
+        using namespace ::android::hardware::media::omx::V1_0;
+        android::sp<IOmxStore> omxStore = new implementation::OmxStore();
+        if (omxStore == nullptr) {
+            ALOGE("Cannot create IOmxStore HAL service.");
+        } else if (omxStore->registerAsService() != android::OK) {
+            ALOGE("Cannot register IOmxStore HAL service.");
+        }
+    }
+
+    android::hardware::joinRpcThreadpool();
+    return 0;
+}