blob: 50ccbcef396b5b281fa82e31f1d644eebbde0ebf [file] [log] [blame]
Chong Zhangea788f72018-10-12 14:44:24 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "MediaCodecUpdateService"
18//#define LOG_NDEBUG 0
19
20#include <android/dlext.h>
Chong Zhangea788f72018-10-12 14:44:24 -070021#include <dlfcn.h>
22#include <media/CodecServiceRegistrant.h>
Marco Nelissencb570222019-02-06 09:12:03 -080023#include <nativeloader/dlext_namespaces.h>
Chong Zhangea788f72018-10-12 14:44:24 -070024#include <utils/Log.h>
Chong Zhangd9cf3a22019-01-09 13:12:00 -080025#include <utils/String8.h>
Chong Zhangea788f72018-10-12 14:44:24 -070026
27#include "MediaCodecUpdateService.h"
28
Chong Zhangea788f72018-10-12 14:44:24 -070029namespace android {
Chong Zhangea788f72018-10-12 14:44:24 -070030
Chong Zhangd9cf3a22019-01-09 13:12:00 -080031void loadFromApex(const char *libDirPath) {
32 ALOGV("loadFromApex: path=%s", libDirPath);
Chong Zhangea788f72018-10-12 14:44:24 -070033
Chong Zhangd9cf3a22019-01-09 13:12:00 -080034 String8 libPath = String8(libDirPath) + "/libmedia_codecserviceregistrant.so";
Chong Zhangea788f72018-10-12 14:44:24 -070035
Chong Zhangd9cf3a22019-01-09 13:12:00 -080036 android_namespace_t *codecNs = android_create_namespace("codecs",
37 nullptr, // ld_library_path
38 libDirPath,
39 ANDROID_NAMESPACE_TYPE_ISOLATED,
40 nullptr, // permitted_when_isolated_path
41 nullptr); // parent
Chong Zhangea788f72018-10-12 14:44:24 -070042
Chong Zhangd9cf3a22019-01-09 13:12:00 -080043 if (codecNs == nullptr) {
44 ALOGE("Failed to create codec namespace");
45 return;
Chong Zhangea788f72018-10-12 14:44:24 -070046 }
47
Chong Zhangd9cf3a22019-01-09 13:12:00 -080048 String8 linked_libraries(LINKED_LIBRARIES);
49 if (!android_link_namespaces(codecNs, nullptr, linked_libraries.c_str())) {
50 ALOGE("Failed to link namespace");
51 return;
Chong Zhangea788f72018-10-12 14:44:24 -070052 }
53
Chong Zhangd9cf3a22019-01-09 13:12:00 -080054 const android_dlextinfo dlextinfo = {
55 .flags = ANDROID_DLEXT_USE_NAMESPACE,
56 .library_namespace = codecNs,
57 };
58
59 void *registrantLib = android_dlopen_ext(
60 libPath.string(),
61 RTLD_NOW | RTLD_LOCAL, &dlextinfo);
62
63 if (registrantLib == nullptr) {
64 ALOGE("Failed to load lib from archive: %s", dlerror());
65 }
66
67 RegisterCodecServicesFunc registerCodecServices =
68 reinterpret_cast<RegisterCodecServicesFunc>(
69 dlsym(registrantLib, "RegisterCodecServices"));
70
71 if (registerCodecServices == nullptr) {
72 ALOGE("Cannot register codec services -- corrupted library.");
73 return;
74 }
75
76 registerCodecServices();
Chong Zhangea788f72018-10-12 14:44:24 -070077}
78
Chong Zhangea788f72018-10-12 14:44:24 -070079} // namespace android