blob: 6ae630f3d08d93cf4088477024f7d5a75d66471b [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 Zhangacb33502020-04-20 11:04:48 -070028#include <media/TranscodingUidPolicy.h>
hkuang9c04b8d2020-01-22 10:03:21 -080029#include <private/android_filesystem_config.h>
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080030#include <utils/Log.h>
31#include <utils/Vector.h>
32
Chong Zhang75222182020-04-29 14:43:42 -070033#include "SimulatedTranscoder.h"
34
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080035namespace android {
36
hkuang9c04b8d2020-01-22 10:03:21 -080037// Convenience methods for constructing binder::Status objects for error returns
38#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
39 Status::fromServiceSpecificErrorWithMessage( \
40 errorCode, \
41 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__))
42
43// Can MediaTranscoding service trust the caller based on the calling UID?
44// TODO(hkuang): Add MediaProvider's UID.
45static bool isTrustedCallingUid(uid_t uid) {
46 switch (uid) {
47 case AID_ROOT: // root user
48 case AID_SYSTEM:
49 case AID_SHELL:
50 case AID_MEDIA: // mediaserver
51 return true;
52 default:
53 return false;
54 }
55}
56
Chong Zhang182b06a2020-04-09 14:38:05 -070057MediaTranscodingService::MediaTranscodingService(
Chong Zhang66469272020-06-04 16:51:55 -070058 const std::shared_ptr<TranscoderInterface>& transcoder)
59 : mUidPolicy(new TranscodingUidPolicy()),
60 mJobScheduler(new TranscodingJobScheduler(transcoder, mUidPolicy)),
Chong Zhang182b06a2020-04-09 14:38:05 -070061 mClientManager(new TranscodingClientManager(mJobScheduler)) {
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080062 ALOGV("MediaTranscodingService is created");
Chong Zhang75222182020-04-29 14:43:42 -070063 transcoder->setCallback(mJobScheduler);
Chong Zhang66469272020-06-04 16:51:55 -070064 mUidPolicy->setCallback(mJobScheduler);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080065}
66
67MediaTranscodingService::~MediaTranscodingService() {
68 ALOGE("Should not be in ~MediaTranscodingService");
69}
70
hkuang9c04b8d2020-01-22 10:03:21 -080071binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
72 String8 result;
Linus Nilsson06fab722020-07-16 15:53:14 -070073
74 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
75 result.format(
76 "Permission Denial: "
77 "can't dump MediaTranscodingService from pid=%d, uid=%d\n",
78 AIBinder_getCallingPid(), AIBinder_getCallingUid());
79 write(fd, result.string(), result.size());
80 return PERMISSION_DENIED;
81 }
82
hkuang9c04b8d2020-01-22 10:03:21 -080083 const size_t SIZE = 256;
84 char buffer[SIZE];
85
86 snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this);
87 result.append(buffer);
88 write(fd, result.string(), result.size());
89
90 Vector<String16> args;
Chong Zhang182b06a2020-04-09 14:38:05 -070091 mClientManager->dumpAllClients(fd, args);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080092 return OK;
93}
94
95//static
96void MediaTranscodingService::instantiate() {
Chong Zhang66469272020-06-04 16:51:55 -070097 std::shared_ptr<TranscoderInterface> transcoder;
Chong Zhang17495a92020-06-25 10:39:05 -070098 if (property_get_bool("debug.transcoding.simulated_transcoder", false)) {
Chong Zhang66469272020-06-04 16:51:55 -070099 transcoder = std::make_shared<SimulatedTranscoder>();
100 } else {
101 transcoder = std::make_shared<TranscoderWrapper>();
102 }
103
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800104 std::shared_ptr<MediaTranscodingService> service =
Chong Zhang66469272020-06-04 16:51:55 -0700105 ::ndk::SharedRefBase::make<MediaTranscodingService>(transcoder);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800106 binder_status_t status =
107 AServiceManager_addService(service->asBinder().get(), getServiceName());
108 if (status != STATUS_OK) {
109 return;
110 }
111}
112
113Status MediaTranscodingService::registerClient(
Chong Zhang182b06a2020-04-09 14:38:05 -0700114 const std::shared_ptr<ITranscodingClientCallback>& in_callback,
115 const std::string& in_clientName, const std::string& in_opPackageName, int32_t in_clientUid,
116 int32_t in_clientPid, std::shared_ptr<ITranscodingClient>* _aidl_return) {
117 if (in_callback == nullptr) {
Chong Zhang8e062632020-03-31 10:56:37 -0700118 *_aidl_return = nullptr;
Chong Zhang15c192a2020-05-05 16:24:00 -0700119 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Client callback cannot be null!");
hkuang9c04b8d2020-01-22 10:03:21 -0800120 }
121
122 int32_t callingPid = AIBinder_getCallingPid();
123 int32_t callingUid = AIBinder_getCallingUid();
124
Chong Zhang8e062632020-03-31 10:56:37 -0700125 // Check if we can trust clientUid. Only privilege caller could forward the
126 // uid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -0800127 if (in_clientUid == USE_CALLING_UID) {
128 in_clientUid = callingUid;
129 } else if (!isTrustedCallingUid(callingUid)) {
130 ALOGE("MediaTranscodingService::registerClient failed (calling PID %d, calling UID %d) "
131 "rejected "
132 "(don't trust clientUid %d)",
133 in_clientPid, in_clientUid, in_clientUid);
134 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
135 "Untrusted caller (calling PID %d, UID %d) trying to "
136 "register client",
137 in_clientPid, in_clientUid);
138 }
139
Chong Zhang8e062632020-03-31 10:56:37 -0700140 // Check if we can trust clientPid. Only privilege caller could forward the
141 // pid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -0800142 if (in_clientPid == USE_CALLING_PID) {
143 in_clientPid = callingPid;
144 } else if (!isTrustedCallingUid(callingUid)) {
145 ALOGE("MediaTranscodingService::registerClient client failed (calling PID %d, calling UID "
146 "%d) rejected "
147 "(don't trust clientPid %d)",
148 in_clientPid, in_clientUid, in_clientPid);
149 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
150 "Untrusted caller (calling PID %d, UID %d) trying to "
151 "register client",
152 in_clientPid, in_clientUid);
153 }
154
hkuang9c04b8d2020-01-22 10:03:21 -0800155 // Creates the client and uses its process id as client id.
Chong Zhang8e062632020-03-31 10:56:37 -0700156 std::shared_ptr<ITranscodingClient> newClient;
157
Chong Zhang182b06a2020-04-09 14:38:05 -0700158 status_t err = mClientManager->addClient(in_callback, in_clientPid, in_clientUid, in_clientName,
159 in_opPackageName, &newClient);
hkuang9c04b8d2020-01-22 10:03:21 -0800160 if (err != OK) {
Chong Zhang8e062632020-03-31 10:56:37 -0700161 *_aidl_return = nullptr;
hkuang9c04b8d2020-01-22 10:03:21 -0800162 return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
163 }
164
Chong Zhang8e062632020-03-31 10:56:37 -0700165 *_aidl_return = newClient;
hkuang9c04b8d2020-01-22 10:03:21 -0800166 return Status::ok();
167}
168
169Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
170 ALOGD("MediaTranscodingService::getNumOfClients");
Chong Zhang182b06a2020-04-09 14:38:05 -0700171 *_aidl_return = mClientManager->getNumOfClients();
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800172 return Status::ok();
173}
174
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800175} // namespace android