Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 104cac76 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add media.codec.update service"

parents cd154933 ea788f7e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -153,7 +153,7 @@ cc_library_static {
filegroup {
    name: "mediaupdateservice_aidl",
    srcs: [
        "aidl/android/media/IMediaExtractorUpdateService.aidl",
        "aidl/android/media/IMediaUpdateService.aidl",
    ],
}

+2 −2
Original line number Diff line number Diff line
@@ -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);
}
+24 −1
Original line number Diff line number Diff line
@@ -72,7 +72,27 @@ ifneq ($(NATIVE_COVERAGE),true)
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_MODULE := mediaswcodec
LOCAL_INIT_RC := mediaswcodec.rc
LOCAL_32_BIT_ONLY := true

sanitizer_runtime_libraries :=
llndk_libraries :=

include $(BUILD_EXECUTABLE)

####################################################################
+136 −0
Original line number Diff line number Diff line
/*
 * 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
+40 −0
Original line number Diff line number Diff line
/*
 * 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
Loading