blob: 0e6892d6456d7105f10983bd1914b6094ded91ef [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>
23#include <utils/Log.h>
Chong Zhangd9cf3a22019-01-09 13:12:00 -080024#include <utils/String8.h>
Chong Zhangea788f72018-10-12 14:44:24 -070025
26#include "MediaCodecUpdateService.h"
27
28// Copied from GraphicsEnv.cpp
29// TODO(b/37049319) Get this from a header once one exists
30extern "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
45namespace android {
Chong Zhangea788f72018-10-12 14:44:24 -070046
Chong Zhangd9cf3a22019-01-09 13:12:00 -080047void loadFromApex(const char *libDirPath) {
48 ALOGV("loadFromApex: path=%s", libDirPath);
Chong Zhangea788f72018-10-12 14:44:24 -070049
Chong Zhangd9cf3a22019-01-09 13:12:00 -080050 String8 libPath = String8(libDirPath) + "/libmedia_codecserviceregistrant.so";
Chong Zhangea788f72018-10-12 14:44:24 -070051
Chong Zhangd9cf3a22019-01-09 13:12:00 -080052 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 Zhangea788f72018-10-12 14:44:24 -070058
Chong Zhangd9cf3a22019-01-09 13:12:00 -080059 if (codecNs == nullptr) {
60 ALOGE("Failed to create codec namespace");
61 return;
Chong Zhangea788f72018-10-12 14:44:24 -070062 }
63
Chong Zhangd9cf3a22019-01-09 13:12:00 -080064 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 Zhangea788f72018-10-12 14:44:24 -070068 }
69
Chong Zhangd9cf3a22019-01-09 13:12:00 -080070 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 Zhangea788f72018-10-12 14:44:24 -070093}
94
Chong Zhangea788f72018-10-12 14:44:24 -070095} // namespace android