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> |
Marco Nelissen | cb57022 | 2019-02-06 09:12:03 -0800 | [diff] [blame] | 23 | #include <nativeloader/dlext_namespaces.h> |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 24 | #include <utils/Log.h> |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 25 | #include <utils/String8.h> |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 26 | |
| 27 | #include "MediaCodecUpdateService.h" |
| 28 | |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 29 | namespace android { |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 30 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 31 | void loadFromApex(const char *libDirPath) { |
| 32 | ALOGV("loadFromApex: path=%s", libDirPath); |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 33 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 34 | String8 libPath = String8(libDirPath) + "/libmedia_codecserviceregistrant.so"; |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 35 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 36 | 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 Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 42 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 43 | if (codecNs == nullptr) { |
| 44 | ALOGE("Failed to create codec namespace"); |
| 45 | return; |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 48 | 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 Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Chong Zhang | d9cf3a2 | 2019-01-09 13:12:00 -0800 | [diff] [blame] | 54 | 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 Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Chong Zhang | ea788f7 | 2018-10-12 14:44:24 -0700 | [diff] [blame] | 79 | } // namespace android |