Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 1 | /* |
| 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 Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 21 | #include <dlfcn.h> |
| 22 | #include <media/CodecServiceRegistrant.h> |
| 23 | #include <utils/Log.h> |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 24 | #include <utils/String8.h> |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 25 | |
| 26 | #include "MediaCodecUpdateService.h" |
| 27 | |
| 28 | // Copied from GraphicsEnv.cpp |
| 29 | // TODO(b/37049319) Get this from a header once one exists |
| 30 | extern "C" { |
| 31 | android_namespace_t* android_create_namespace(const char* name, |
| 32 | const char* ld_library_path, |
| 33 | const char* default_library_path, |
| 34 | uint64_t type, |
| 35 | const char* permitted_when_isolated_path, |
| 36 | android_namespace_t* parent); |
| 37 | bool android_link_namespaces(android_namespace_t* from, |
| 38 | android_namespace_t* to, |
| 39 | const char* shared_libs_sonames); |
| 40 | enum { |
| 41 | ANDROID_NAMESPACE_TYPE_ISOLATED = 1, |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | namespace android { |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 46 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 47 | void loadFromApex(const char *libDirPath) { |
| 48 | ALOGV("loadFromApex: path=%s", libDirPath); |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 49 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 50 | String8 libPath = String8(libDirPath) + "/libmedia_codecserviceregistrant.so"; |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 51 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 52 | android_namespace_t *codecNs = android_create_namespace("codecs", |
| 53 | nullptr, // ld_library_path |
| 54 | libDirPath, |
| 55 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 56 | nullptr, // permitted_when_isolated_path |
| 57 | nullptr); // parent |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 58 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 59 | if (codecNs == nullptr) { |
| 60 | ALOGE("Failed to create codec namespace"); |
| 61 | return; |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 64 | String8 linked_libraries(LINKED_LIBRARIES); |
| 65 | if (!android_link_namespaces(codecNs, nullptr, linked_libraries.c_str())) { |
| 66 | ALOGE("Failed to link namespace"); |
| 67 | return; |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 70 | const android_dlextinfo dlextinfo = { |
| 71 | .flags = ANDROID_DLEXT_USE_NAMESPACE, |
| 72 | .library_namespace = codecNs, |
| 73 | }; |
| 74 | |
| 75 | void *registrantLib = android_dlopen_ext( |
| 76 | libPath.string(), |
| 77 | RTLD_NOW | RTLD_LOCAL, &dlextinfo); |
| 78 | |
| 79 | if (registrantLib == nullptr) { |
| 80 | ALOGE("Failed to load lib from archive: %s", dlerror()); |
| 81 | } |
| 82 | |
| 83 | RegisterCodecServicesFunc registerCodecServices = |
| 84 | reinterpret_cast<RegisterCodecServicesFunc>( |
| 85 | dlsym(registrantLib, "RegisterCodecServices")); |
| 86 | |
| 87 | if (registerCodecServices == nullptr) { |
| 88 | ALOGE("Cannot register codec services -- corrupted library."); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | registerCodecServices(); |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 95 | } // namespace android |