blob: b0ea0db1935c5ce6b5ca83faeb7a7644efc7ea2d [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 Zhang66469272020-06-04 16:51:55 -070023#include <cutils/properties.h>
24#include <media/TranscoderWrapper.h>
Chong Zhang182b06a2020-04-09 14:38:05 -070025#include <media/TranscodingClientManager.h>
26#include <media/TranscodingJobScheduler.h>
Chong Zhangacb33502020-04-20 11:04:48 -070027#include <media/TranscodingUidPolicy.h>
hkuang9c04b8d2020-01-22 10:03:21 -080028#include <private/android_filesystem_config.h>
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080029#include <utils/Log.h>
30#include <utils/Vector.h>
31
Chong Zhang75222182020-04-29 14:43:42 -070032#include "SimulatedTranscoder.h"
33
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080034namespace android {
35
hkuang9c04b8d2020-01-22 10:03:21 -080036// Convenience methods for constructing binder::Status objects for error returns
37#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
38 Status::fromServiceSpecificErrorWithMessage( \
39 errorCode, \
40 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__))
41
42// Can MediaTranscoding service trust the caller based on the calling UID?
43// TODO(hkuang): Add MediaProvider's UID.
44static bool isTrustedCallingUid(uid_t uid) {
45 switch (uid) {
46 case AID_ROOT: // root user
47 case AID_SYSTEM:
48 case AID_SHELL:
49 case AID_MEDIA: // mediaserver
50 return true;
51 default:
52 return false;
53 }
54}
55
Chong Zhang182b06a2020-04-09 14:38:05 -070056MediaTranscodingService::MediaTranscodingService(
Chong Zhang66469272020-06-04 16:51:55 -070057 const std::shared_ptr<TranscoderInterface>& transcoder)
58 : mUidPolicy(new TranscodingUidPolicy()),
59 mJobScheduler(new TranscodingJobScheduler(transcoder, mUidPolicy)),
Chong Zhang182b06a2020-04-09 14:38:05 -070060 mClientManager(new TranscodingClientManager(mJobScheduler)) {
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080061 ALOGV("MediaTranscodingService is created");
Chong Zhang75222182020-04-29 14:43:42 -070062 transcoder->setCallback(mJobScheduler);
Chong Zhang66469272020-06-04 16:51:55 -070063 mUidPolicy->setCallback(mJobScheduler);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080064}
65
66MediaTranscodingService::~MediaTranscodingService() {
67 ALOGE("Should not be in ~MediaTranscodingService");
68}
69
hkuang9c04b8d2020-01-22 10:03:21 -080070binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
71 String8 result;
72 const size_t SIZE = 256;
73 char buffer[SIZE];
74
75 snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this);
76 result.append(buffer);
77 write(fd, result.string(), result.size());
78
79 Vector<String16> args;
Chong Zhang182b06a2020-04-09 14:38:05 -070080 mClientManager->dumpAllClients(fd, args);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080081 return OK;
82}
83
84//static
85void MediaTranscodingService::instantiate() {
Chong Zhang66469272020-06-04 16:51:55 -070086 std::shared_ptr<TranscoderInterface> transcoder;
Chong Zhang17495a92020-06-25 10:39:05 -070087 if (property_get_bool("debug.transcoding.simulated_transcoder", false)) {
Chong Zhang66469272020-06-04 16:51:55 -070088 transcoder = std::make_shared<SimulatedTranscoder>();
89 } else {
90 transcoder = std::make_shared<TranscoderWrapper>();
91 }
92
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080093 std::shared_ptr<MediaTranscodingService> service =
Chong Zhang66469272020-06-04 16:51:55 -070094 ::ndk::SharedRefBase::make<MediaTranscodingService>(transcoder);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080095 binder_status_t status =
96 AServiceManager_addService(service->asBinder().get(), getServiceName());
97 if (status != STATUS_OK) {
98 return;
99 }
100}
101
102Status MediaTranscodingService::registerClient(
Chong Zhang182b06a2020-04-09 14:38:05 -0700103 const std::shared_ptr<ITranscodingClientCallback>& in_callback,
104 const std::string& in_clientName, const std::string& in_opPackageName, int32_t in_clientUid,
105 int32_t in_clientPid, std::shared_ptr<ITranscodingClient>* _aidl_return) {
106 if (in_callback == nullptr) {
Chong Zhang8e062632020-03-31 10:56:37 -0700107 *_aidl_return = nullptr;
Chong Zhang15c192a2020-05-05 16:24:00 -0700108 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Client callback cannot be null!");
hkuang9c04b8d2020-01-22 10:03:21 -0800109 }
110
111 int32_t callingPid = AIBinder_getCallingPid();
112 int32_t callingUid = AIBinder_getCallingUid();
113
Chong Zhang8e062632020-03-31 10:56:37 -0700114 // Check if we can trust clientUid. Only privilege caller could forward the
115 // uid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -0800116 if (in_clientUid == USE_CALLING_UID) {
117 in_clientUid = callingUid;
118 } else if (!isTrustedCallingUid(callingUid)) {
119 ALOGE("MediaTranscodingService::registerClient failed (calling PID %d, calling UID %d) "
120 "rejected "
121 "(don't trust clientUid %d)",
122 in_clientPid, in_clientUid, in_clientUid);
123 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
124 "Untrusted caller (calling PID %d, UID %d) trying to "
125 "register client",
126 in_clientPid, in_clientUid);
127 }
128
Chong Zhang8e062632020-03-31 10:56:37 -0700129 // Check if we can trust clientPid. Only privilege caller could forward the
130 // pid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -0800131 if (in_clientPid == USE_CALLING_PID) {
132 in_clientPid = callingPid;
133 } else if (!isTrustedCallingUid(callingUid)) {
134 ALOGE("MediaTranscodingService::registerClient client failed (calling PID %d, calling UID "
135 "%d) rejected "
136 "(don't trust clientPid %d)",
137 in_clientPid, in_clientUid, in_clientPid);
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
hkuang9c04b8d2020-01-22 10:03:21 -0800144 // Creates the client and uses its process id as client id.
Chong Zhang8e062632020-03-31 10:56:37 -0700145 std::shared_ptr<ITranscodingClient> newClient;
146
Chong Zhang182b06a2020-04-09 14:38:05 -0700147 status_t err = mClientManager->addClient(in_callback, in_clientPid, in_clientUid, in_clientName,
148 in_opPackageName, &newClient);
hkuang9c04b8d2020-01-22 10:03:21 -0800149 if (err != OK) {
Chong Zhang8e062632020-03-31 10:56:37 -0700150 *_aidl_return = nullptr;
hkuang9c04b8d2020-01-22 10:03:21 -0800151 return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
152 }
153
Chong Zhang8e062632020-03-31 10:56:37 -0700154 *_aidl_return = newClient;
hkuang9c04b8d2020-01-22 10:03:21 -0800155 return Status::ok();
156}
157
158Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
159 ALOGD("MediaTranscodingService::getNumOfClients");
Chong Zhang182b06a2020-04-09 14:38:05 -0700160 *_aidl_return = mClientManager->getNumOfClients();
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800161 return Status::ok();
162}
163
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800164} // namespace android