Add media.codec.update service
This is done similarly to how we update the extractors.
media.swcodec will start a service "media.codec.update"
to get notified of the update apk location, then updated
version of the component store will be loaded.
Since the update interface is exactly the same, we reuse
IMediaExtractorUpdateService to avoid adding new an aidl
interface. Besides this service will likely be removed
after we switch to APEX.)
Bug: 111407413
Bug: 117290290
Change-Id: I739ba0071806beed99302ccd3da7a6f73a437f06
diff --git a/media/libmedia/Android.bp b/media/libmedia/Android.bp
index 93fb667..25d28ff 100644
--- a/media/libmedia/Android.bp
+++ b/media/libmedia/Android.bp
@@ -153,7 +153,7 @@
filegroup {
name: "mediaupdateservice_aidl",
srcs: [
- "aidl/android/media/IMediaExtractorUpdateService.aidl",
+ "aidl/android/media/IMediaUpdateService.aidl",
],
}
diff --git a/media/libmedia/aidl/android/media/IMediaExtractorUpdateService.aidl b/media/libmedia/aidl/android/media/IMediaUpdateService.aidl
similarity index 84%
rename from media/libmedia/aidl/android/media/IMediaExtractorUpdateService.aidl
rename to media/libmedia/aidl/android/media/IMediaUpdateService.aidl
index 57b1bc9..4777969 100644
--- a/media/libmedia/aidl/android/media/IMediaExtractorUpdateService.aidl
+++ b/media/libmedia/aidl/android/media/IMediaUpdateService.aidl
@@ -17,9 +17,9 @@
package android.media;
/**
- * Service to reload extractor plugins when update package is installed/uninstalled.
+ * Service to reload media component plugins when update package is installed/uninstalled.
* @hide
*/
-interface IMediaExtractorUpdateService {
+interface IMediaUpdateService {
void loadPlugins(@utf8InCpp String apkPath);
}
diff --git a/services/mediacodec/Android.mk b/services/mediacodec/Android.mk
index 314ff01..fe05e95 100644
--- a/services/mediacodec/Android.mk
+++ b/services/mediacodec/Android.mk
@@ -72,7 +72,27 @@
LOCAL_REQUIRED_MODULES_arm := crash_dump.policy mediacodec.policy
LOCAL_REQUIRED_MODULES_x86 := crash_dump.policy mediacodec.policy
endif
-LOCAL_SRC_FILES := main_swcodecservice.cpp
+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 \
@@ -89,6 +109,9 @@
LOCAL_INIT_RC := mediaswcodec.rc
LOCAL_32_BIT_ONLY := true
+sanitizer_runtime_libraries :=
+llndk_libraries :=
+
include $(BUILD_EXECUTABLE)
####################################################################
diff --git a/services/mediacodec/MediaCodecUpdateService.cpp b/services/mediacodec/MediaCodecUpdateService.cpp
new file mode 100644
index 0000000..a2bb469
--- /dev/null
+++ b/services/mediacodec/MediaCodecUpdateService.cpp
@@ -0,0 +1,136 @@
+/*
+ * 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 <android-base/logging.h>
+#include <android-base/strings.h>
+#include <dirent.h>
+#include <dlfcn.h>
+#include <media/CodecServiceRegistrant.h>
+#include <utils/Log.h>
+#include <ziparchive/zip_archive.h>
+#include <cutils/properties.h>
+
+#include "MediaCodecUpdateService.h"
+
+// Copied from GraphicsEnv.cpp
+// TODO(b/37049319) Get this from a header once one exists
+extern "C" {
+ android_namespace_t* android_create_namespace(const char* name,
+ const char* ld_library_path,
+ const char* default_library_path,
+ uint64_t type,
+ const char* permitted_when_isolated_path,
+ android_namespace_t* parent);
+ bool android_link_namespaces(android_namespace_t* from,
+ android_namespace_t* to,
+ const char* shared_libs_sonames);
+ enum {
+ ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
+ };
+}
+
+namespace android {
+namespace media {
+
+binder::Status MediaCodecUpdateService::loadPlugins(const ::std::string& apkPath) {
+ ALOGV("loadPlugins %s", apkPath.c_str());
+
+ ZipArchiveHandle zipHandle;
+ void *registrantLib = NULL;
+ int32_t ret = OpenArchive(apkPath.c_str(), &zipHandle);
+
+ if (ret == 0) {
+ char abilist32[PROPERTY_VALUE_MAX];
+ property_get("ro.product.cpu.abilist32", abilist32, "armeabi-v7a");
+
+ auto abis = base::Split(abilist32, ",");
+ if (abis.empty()) {
+ ALOGW("abilist is empty, trying armeabi-v7a ...");
+ abis.push_back("armeabi-v7a");
+ }
+
+ // TODO: Only try the first entry in abilist32 for now.
+ // We probably should try the next if it fails.
+ String8 libPathInApk = String8("lib/") + String8(abis[0].c_str());
+ String8 defaultLibPath = String8(apkPath.c_str()) + "!/" + libPathInApk;
+ String8 libPath = defaultLibPath + "/libmedia_codecserviceregistrant.so";
+
+ ZipEntry entry;
+ ZipString name(libPathInApk + "/libmedia_codecserviceregistrant.so");
+ ret = FindEntry(zipHandle, name, &entry);
+
+ if (ret == 0) {
+ android_namespace_t *codecNs = android_create_namespace("codecs",
+ nullptr, // ld_library_path
+ defaultLibPath.c_str(),
+ ANDROID_NAMESPACE_TYPE_ISOLATED,
+ nullptr, // permitted_when_isolated_path
+ nullptr); // parent
+
+ if (codecNs != nullptr) {
+ String8 linked_libraries(LINKED_LIBRARIES);
+ if (android_link_namespaces(
+ codecNs, nullptr, linked_libraries.c_str())) {
+ const android_dlextinfo dlextinfo = {
+ .flags = ANDROID_DLEXT_USE_NAMESPACE,
+ .library_namespace = codecNs,
+ };
+
+ registrantLib = android_dlopen_ext(
+ libPath.string(),
+ RTLD_NOW | RTLD_LOCAL, &dlextinfo);
+
+ if (registrantLib == NULL) {
+ ALOGE("Failed to load lib from archive: %s", dlerror());
+ }
+ } else {
+ ALOGE("Failed to link namespace");
+ }
+ } else {
+ ALOGE("Failed to create codec namespace");
+ }
+ } else {
+ ALOGE("Failed to find entry (ret=%d)", ret);
+ }
+
+ CloseArchive(zipHandle);
+ } else {
+ ALOGE("Failed to open archive (ret=%d)", ret);
+ }
+
+ if (registrantLib) {
+ RegisterCodecServicesFunc registerCodecServices =
+ reinterpret_cast<RegisterCodecServicesFunc>(
+ dlsym(registrantLib, "RegisterCodecServices"));
+ if (registerCodecServices) {
+ registerCodecServices();
+ } else {
+ LOG(WARNING) << "Cannot register codec services "
+ "-- corrupted library.";
+ }
+ } else {
+ LOG(ERROR) << "Cannot find codec service registrant.";
+ }
+
+ return binder::Status::ok();
+}
+
+} // namespace media
+} // namespace android
diff --git a/services/mediacodec/MediaCodecUpdateService.h b/services/mediacodec/MediaCodecUpdateService.h
new file mode 100644
index 0000000..7b7cee9
--- /dev/null
+++ b/services/mediacodec/MediaCodecUpdateService.h
@@ -0,0 +1,40 @@
+/*
+ * 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
+
+#include <binder/BinderService.h>
+#include <android/media/BnMediaUpdateService.h>
+
+namespace android {
+namespace media {
+
+class MediaCodecUpdateService
+ : public BinderService<MediaCodecUpdateService>, public BnMediaUpdateService
+{
+ friend class BinderService<MediaCodecUpdateService>;
+public:
+ MediaCodecUpdateService() : BnMediaUpdateService() { }
+ virtual ~MediaCodecUpdateService() { }
+ static const char* getServiceName() { return "media.codec.update"; }
+ binder::Status loadPlugins(const ::std::string& apkPath);
+};
+
+} // namespace media
+} // namespace android
+
+#endif // ANDROID_MEDIA_CODEC_UPDATE_SERVICE_H
diff --git a/services/mediacodec/main_swcodecservice.cpp b/services/mediacodec/main_swcodecservice.cpp
index 1729c3f..386abb2 100644
--- a/services/mediacodec/main_swcodecservice.cpp
+++ b/services/mediacodec/main_swcodecservice.cpp
@@ -20,10 +20,13 @@
// from LOCAL_C_INCLUDES
#include "minijail.h"
+#include <android-base/properties.h>
#include <binder/ProcessState.h>
+#include <dlfcn.h>
#include <hidl/HidlTransportSupport.h>
#include <media/CodecServiceRegistrant.h>
-#include <dlfcn.h>
+
+#include "MediaCodecUpdateService.h"
using namespace android;
@@ -40,6 +43,11 @@
signal(SIGPIPE, SIG_IGN);
SetUpMinijail(kSystemSeccompPolicyPath, kVendorSeccompPolicyPath);
+ std::string value = base::GetProperty("ro.build.type", "unknown");
+ if (value == "userdebug" || value == "eng") {
+ media::MediaCodecUpdateService::instantiate();
+ }
+
android::ProcessState::self()->startThreadPool();
::android::hardware::configureRpcThreadpool(64, false);
diff --git a/services/mediaextractor/MediaExtractorUpdateService.h b/services/mediaextractor/MediaExtractorUpdateService.h
index 4115f6d..ea34c9d 100644
--- a/services/mediaextractor/MediaExtractorUpdateService.h
+++ b/services/mediaextractor/MediaExtractorUpdateService.h
@@ -18,17 +18,17 @@
#define ANDROID_MEDIA_EXTRACTOR_UPDATE_SERVICE_H
#include <binder/BinderService.h>
-#include <android/media/BnMediaExtractorUpdateService.h>
+#include <android/media/BnMediaUpdateService.h>
namespace android {
namespace media {
class MediaExtractorUpdateService
- : public BinderService<MediaExtractorUpdateService>, public BnMediaExtractorUpdateService
+ : public BinderService<MediaExtractorUpdateService>, public BnMediaUpdateService
{
friend class BinderService<MediaExtractorUpdateService>;
public:
- MediaExtractorUpdateService() : BnMediaExtractorUpdateService() { }
+ MediaExtractorUpdateService() : BnMediaUpdateService() { }
virtual ~MediaExtractorUpdateService() { }
static const char* getServiceName() { return "media.extractor.update"; }
binder::Status loadPlugins(const ::std::string& apkPath);