blob: 977995acbd1002e2a16d850a746056f089cefa66 [file] [log] [blame]
Hangyu Kuang71b9fb42019-11-27 10:33:32 -08001/*
2 * Copyright (C) 2019 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_NDEBUG 0
18#define LOG_TAG "MediaTranscodingService"
Chong Zhang182b06a2020-04-09 14:38:05 -070019#include "MediaTranscodingService.h"
20
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080021#include <android/binder_manager.h>
22#include <android/binder_process.h>
Linus Nilsson06fab722020-07-16 15:53:14 -070023#include <binder/IServiceManager.h>
Chong Zhang66469272020-06-04 16:51:55 -070024#include <cutils/properties.h>
25#include <media/TranscoderWrapper.h>
Chong Zhang182b06a2020-04-09 14:38:05 -070026#include <media/TranscodingClientManager.h>
27#include <media/TranscodingJobScheduler.h>
Chong Zhangf9077512020-09-21 21:02:06 -070028#include <media/TranscodingResourcePolicy.h>
Chong Zhangacb33502020-04-20 11:04:48 -070029#include <media/TranscodingUidPolicy.h>
hkuang9c04b8d2020-01-22 10:03:21 -080030#include <private/android_filesystem_config.h>
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080031#include <utils/Log.h>
32#include <utils/Vector.h>
33
Chong Zhang75222182020-04-29 14:43:42 -070034#include "SimulatedTranscoder.h"
35
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080036namespace android {
37
hkuang9c04b8d2020-01-22 10:03:21 -080038// Convenience methods for constructing binder::Status objects for error returns
39#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
40 Status::fromServiceSpecificErrorWithMessage( \
41 errorCode, \
42 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__))
43
44// Can MediaTranscoding service trust the caller based on the calling UID?
45// TODO(hkuang): Add MediaProvider's UID.
46static bool isTrustedCallingUid(uid_t uid) {
47 switch (uid) {
48 case AID_ROOT: // root user
49 case AID_SYSTEM:
50 case AID_SHELL:
51 case AID_MEDIA: // mediaserver
52 return true;
53 default:
54 return false;
55 }
56}
57
Chong Zhang182b06a2020-04-09 14:38:05 -070058MediaTranscodingService::MediaTranscodingService(
Chong Zhang66469272020-06-04 16:51:55 -070059 const std::shared_ptr<TranscoderInterface>& transcoder)
60 : mUidPolicy(new TranscodingUidPolicy()),
Chong Zhangf9077512020-09-21 21:02:06 -070061 mResourcePolicy(new TranscodingResourcePolicy()),
62 mJobScheduler(new TranscodingJobScheduler(transcoder, mUidPolicy, mResourcePolicy)),
Chong Zhang182b06a2020-04-09 14:38:05 -070063 mClientManager(new TranscodingClientManager(mJobScheduler)) {
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080064 ALOGV("MediaTranscodingService is created");
Chong Zhang75222182020-04-29 14:43:42 -070065 transcoder->setCallback(mJobScheduler);
Chong Zhang66469272020-06-04 16:51:55 -070066 mUidPolicy->setCallback(mJobScheduler);
Chong Zhangf9077512020-09-21 21:02:06 -070067 mResourcePolicy->setCallback(mJobScheduler);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080068}
69
70MediaTranscodingService::~MediaTranscodingService() {
71 ALOGE("Should not be in ~MediaTranscodingService");
72}
73
hkuang9c04b8d2020-01-22 10:03:21 -080074binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
75 String8 result;
Linus Nilsson06fab722020-07-16 15:53:14 -070076
Linus Nilssonb65fbaf2020-07-17 12:05:48 -070077 // TODO(b/161549994): Remove libbinder dependencies for mainline.
Linus Nilsson06fab722020-07-16 15:53:14 -070078 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
79 result.format(
80 "Permission Denial: "
81 "can't dump MediaTranscodingService from pid=%d, uid=%d\n",
82 AIBinder_getCallingPid(), AIBinder_getCallingUid());
83 write(fd, result.string(), result.size());
84 return PERMISSION_DENIED;
85 }
86
hkuang9c04b8d2020-01-22 10:03:21 -080087 const size_t SIZE = 256;
88 char buffer[SIZE];
89
90 snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this);
91 result.append(buffer);
92 write(fd, result.string(), result.size());
93
94 Vector<String16> args;
Chong Zhang182b06a2020-04-09 14:38:05 -070095 mClientManager->dumpAllClients(fd, args);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080096 return OK;
97}
98
99//static
100void MediaTranscodingService::instantiate() {
Chong Zhang66469272020-06-04 16:51:55 -0700101 std::shared_ptr<TranscoderInterface> transcoder;
Chong Zhang17495a92020-06-25 10:39:05 -0700102 if (property_get_bool("debug.transcoding.simulated_transcoder", false)) {
Chong Zhang66469272020-06-04 16:51:55 -0700103 transcoder = std::make_shared<SimulatedTranscoder>();
104 } else {
105 transcoder = std::make_shared<TranscoderWrapper>();
106 }
107
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800108 std::shared_ptr<MediaTranscodingService> service =
Chong Zhang66469272020-06-04 16:51:55 -0700109 ::ndk::SharedRefBase::make<MediaTranscodingService>(transcoder);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800110 binder_status_t status =
111 AServiceManager_addService(service->asBinder().get(), getServiceName());
112 if (status != STATUS_OK) {
113 return;
114 }
115}
116
117Status MediaTranscodingService::registerClient(
Chong Zhang182b06a2020-04-09 14:38:05 -0700118 const std::shared_ptr<ITranscodingClientCallback>& in_callback,
119 const std::string& in_clientName, const std::string& in_opPackageName, int32_t in_clientUid,
120 int32_t in_clientPid, std::shared_ptr<ITranscodingClient>* _aidl_return) {
121 if (in_callback == nullptr) {
Chong Zhang8e062632020-03-31 10:56:37 -0700122 *_aidl_return = nullptr;
Chong Zhang15c192a2020-05-05 16:24:00 -0700123 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Client callback cannot be null!");
hkuang9c04b8d2020-01-22 10:03:21 -0800124 }
125
126 int32_t callingPid = AIBinder_getCallingPid();
127 int32_t callingUid = AIBinder_getCallingUid();
128
Chong Zhang8e062632020-03-31 10:56:37 -0700129 // Check if we can trust clientUid. Only privilege caller could forward the
130 // uid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -0800131 if (in_clientUid == USE_CALLING_UID) {
132 in_clientUid = callingUid;
133 } else if (!isTrustedCallingUid(callingUid)) {
134 ALOGE("MediaTranscodingService::registerClient failed (calling PID %d, calling UID %d) "
135 "rejected "
136 "(don't trust clientUid %d)",
137 in_clientPid, in_clientUid, in_clientUid);
138 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
139 "Untrusted caller (calling PID %d, UID %d) trying to "
140 "register client",
141 in_clientPid, in_clientUid);
142 }
143
Chong Zhang8e062632020-03-31 10:56:37 -0700144 // Check if we can trust clientPid. Only privilege caller could forward the
145 // pid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -0800146 if (in_clientPid == USE_CALLING_PID) {
147 in_clientPid = callingPid;
148 } else if (!isTrustedCallingUid(callingUid)) {
149 ALOGE("MediaTranscodingService::registerClient client failed (calling PID %d, calling UID "
150 "%d) rejected "
151 "(don't trust clientPid %d)",
152 in_clientPid, in_clientUid, in_clientPid);
153 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
154 "Untrusted caller (calling PID %d, UID %d) trying to "
155 "register client",
156 in_clientPid, in_clientUid);
157 }
158
hkuang9c04b8d2020-01-22 10:03:21 -0800159 // Creates the client and uses its process id as client id.
Chong Zhang8e062632020-03-31 10:56:37 -0700160 std::shared_ptr<ITranscodingClient> newClient;
161
Chong Zhang182b06a2020-04-09 14:38:05 -0700162 status_t err = mClientManager->addClient(in_callback, in_clientPid, in_clientUid, in_clientName,
163 in_opPackageName, &newClient);
hkuang9c04b8d2020-01-22 10:03:21 -0800164 if (err != OK) {
Chong Zhang8e062632020-03-31 10:56:37 -0700165 *_aidl_return = nullptr;
hkuang9c04b8d2020-01-22 10:03:21 -0800166 return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
167 }
168
Chong Zhang8e062632020-03-31 10:56:37 -0700169 *_aidl_return = newClient;
hkuang9c04b8d2020-01-22 10:03:21 -0800170 return Status::ok();
171}
172
173Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
174 ALOGD("MediaTranscodingService::getNumOfClients");
Chong Zhang182b06a2020-04-09 14:38:05 -0700175 *_aidl_return = mClientManager->getNumOfClients();
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800176 return Status::ok();
177}
178
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800179} // namespace android