blob: b843967f1114a211104e99e3c43226b1e17c2eee [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>
Chong Zhang182b06a2020-04-09 14:38:05 -070023#include <media/TranscodingClientManager.h>
24#include <media/TranscodingJobScheduler.h>
Chong Zhangacb33502020-04-20 11:04:48 -070025#include <media/TranscodingUidPolicy.h>
hkuang9c04b8d2020-01-22 10:03:21 -080026#include <private/android_filesystem_config.h>
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080027#include <utils/Log.h>
28#include <utils/Vector.h>
29
Chong Zhang75222182020-04-29 14:43:42 -070030#include "SimulatedTranscoder.h"
31
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080032namespace android {
33
hkuang9c04b8d2020-01-22 10:03:21 -080034// Convenience methods for constructing binder::Status objects for error returns
35#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
36 Status::fromServiceSpecificErrorWithMessage( \
37 errorCode, \
38 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__))
39
40// Can MediaTranscoding service trust the caller based on the calling UID?
41// TODO(hkuang): Add MediaProvider's UID.
42static bool isTrustedCallingUid(uid_t uid) {
43 switch (uid) {
44 case AID_ROOT: // root user
45 case AID_SYSTEM:
46 case AID_SHELL:
47 case AID_MEDIA: // mediaserver
48 return true;
49 default:
50 return false;
51 }
52}
53
hkuang5172cab2020-01-31 12:40:28 -080054MediaTranscodingService::MediaTranscodingService()
Chong Zhang75222182020-04-29 14:43:42 -070055 : MediaTranscodingService(std::make_shared<SimulatedTranscoder>(),
Chong Zhangacb33502020-04-20 11:04:48 -070056 std::make_shared<TranscodingUidPolicy>()) {}
Chong Zhang182b06a2020-04-09 14:38:05 -070057
58MediaTranscodingService::MediaTranscodingService(
59 const std::shared_ptr<TranscoderInterface>& transcoder,
Chong Zhang7ae4e2f2020-04-17 15:24:34 -070060 const std::shared_ptr<UidPolicyInterface>& uidPolicy)
61 : mJobScheduler(new TranscodingJobScheduler(transcoder, uidPolicy)),
Chong Zhang182b06a2020-04-09 14:38:05 -070062 mClientManager(new TranscodingClientManager(mJobScheduler)) {
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080063 ALOGV("MediaTranscodingService is created");
Chong Zhang75222182020-04-29 14:43:42 -070064
65 transcoder->setCallback(mJobScheduler);
Chong Zhangacb33502020-04-20 11:04:48 -070066 uidPolicy->setCallback(mJobScheduler);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080067}
68
69MediaTranscodingService::~MediaTranscodingService() {
70 ALOGE("Should not be in ~MediaTranscodingService");
71}
72
hkuang9c04b8d2020-01-22 10:03:21 -080073binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
74 String8 result;
75 const size_t SIZE = 256;
76 char buffer[SIZE];
77
78 snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this);
79 result.append(buffer);
80 write(fd, result.string(), result.size());
81
82 Vector<String16> args;
Chong Zhang182b06a2020-04-09 14:38:05 -070083 mClientManager->dumpAllClients(fd, args);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080084 return OK;
85}
86
87//static
88void MediaTranscodingService::instantiate() {
89 std::shared_ptr<MediaTranscodingService> service =
90 ::ndk::SharedRefBase::make<MediaTranscodingService>();
91 binder_status_t status =
92 AServiceManager_addService(service->asBinder().get(), getServiceName());
93 if (status != STATUS_OK) {
94 return;
95 }
96}
97
98Status MediaTranscodingService::registerClient(
Chong Zhang182b06a2020-04-09 14:38:05 -070099 const std::shared_ptr<ITranscodingClientCallback>& in_callback,
100 const std::string& in_clientName, const std::string& in_opPackageName, int32_t in_clientUid,
101 int32_t in_clientPid, std::shared_ptr<ITranscodingClient>* _aidl_return) {
102 if (in_callback == nullptr) {
Chong Zhang8e062632020-03-31 10:56:37 -0700103 *_aidl_return = nullptr;
Chong Zhang15c192a2020-05-05 16:24:00 -0700104 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Client callback cannot be null!");
hkuang9c04b8d2020-01-22 10:03:21 -0800105 }
106
107 int32_t callingPid = AIBinder_getCallingPid();
108 int32_t callingUid = AIBinder_getCallingUid();
109
Chong Zhang8e062632020-03-31 10:56:37 -0700110 // Check if we can trust clientUid. Only privilege caller could forward the
111 // uid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -0800112 if (in_clientUid == USE_CALLING_UID) {
113 in_clientUid = callingUid;
114 } else if (!isTrustedCallingUid(callingUid)) {
115 ALOGE("MediaTranscodingService::registerClient failed (calling PID %d, calling UID %d) "
116 "rejected "
117 "(don't trust clientUid %d)",
118 in_clientPid, in_clientUid, in_clientUid);
119 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
120 "Untrusted caller (calling PID %d, UID %d) trying to "
121 "register client",
122 in_clientPid, in_clientUid);
123 }
124
Chong Zhang8e062632020-03-31 10:56:37 -0700125 // Check if we can trust clientPid. Only privilege caller could forward the
126 // pid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -0800127 if (in_clientPid == USE_CALLING_PID) {
128 in_clientPid = callingPid;
129 } else if (!isTrustedCallingUid(callingUid)) {
130 ALOGE("MediaTranscodingService::registerClient client failed (calling PID %d, calling UID "
131 "%d) rejected "
132 "(don't trust clientPid %d)",
133 in_clientPid, in_clientUid, in_clientPid);
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
hkuang9c04b8d2020-01-22 10:03:21 -0800140 // Creates the client and uses its process id as client id.
Chong Zhang8e062632020-03-31 10:56:37 -0700141 std::shared_ptr<ITranscodingClient> newClient;
142
Chong Zhang182b06a2020-04-09 14:38:05 -0700143 status_t err = mClientManager->addClient(in_callback, in_clientPid, in_clientUid, in_clientName,
144 in_opPackageName, &newClient);
hkuang9c04b8d2020-01-22 10:03:21 -0800145 if (err != OK) {
Chong Zhang8e062632020-03-31 10:56:37 -0700146 *_aidl_return = nullptr;
hkuang9c04b8d2020-01-22 10:03:21 -0800147 return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
148 }
149
Chong Zhang8e062632020-03-31 10:56:37 -0700150 *_aidl_return = newClient;
hkuang9c04b8d2020-01-22 10:03:21 -0800151 return Status::ok();
152}
153
154Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
155 ALOGD("MediaTranscodingService::getNumOfClients");
Chong Zhang182b06a2020-04-09 14:38:05 -0700156 *_aidl_return = mClientManager->getNumOfClients();
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800157 return Status::ok();
158}
159
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800160} // namespace android