blob: 0289613bceb564539e431b3ebec71fc88b7ed5f4 [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>
hkuang9c04b8d2020-01-22 10:03:21 -080025#include <private/android_filesystem_config.h>
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080026#include <utils/Log.h>
27#include <utils/Vector.h>
28
29namespace android {
30
hkuang9c04b8d2020-01-22 10:03:21 -080031// Convenience methods for constructing binder::Status objects for error returns
32#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
33 Status::fromServiceSpecificErrorWithMessage( \
34 errorCode, \
35 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__))
36
37// Can MediaTranscoding service trust the caller based on the calling UID?
38// TODO(hkuang): Add MediaProvider's UID.
39static bool isTrustedCallingUid(uid_t uid) {
40 switch (uid) {
41 case AID_ROOT: // root user
42 case AID_SYSTEM:
43 case AID_SHELL:
44 case AID_MEDIA: // mediaserver
45 return true;
46 default:
47 return false;
48 }
49}
50
Chong Zhang182b06a2020-04-09 14:38:05 -070051// DummyTranscoder and DummyProcessInfo are currently used to instantiate
52// MediaTranscodingService on service side for testing, so that we could
53// actually test the IPC calls of MediaTranscodingService to expose some
54// issues that's observable only over IPC.
55class DummyTranscoder : public TranscoderInterface {
56 void start(int64_t clientId, int32_t jobId) override {
57 (void)clientId;
58 (void)jobId;
59 }
60 void pause(int64_t clientId, int32_t jobId) override {
61 (void)clientId;
62 (void)jobId;
63 }
64 void resume(int64_t clientId, int32_t jobId) override {
65 (void)clientId;
66 (void)jobId;
67 }
68};
69
70class DummyProcessInfo : public ProcessInfoInterface {
71 bool isProcessOnTop(int32_t pid) override {
72 (void)pid;
73 return true;
74 }
75};
76
hkuang5172cab2020-01-31 12:40:28 -080077MediaTranscodingService::MediaTranscodingService()
Chong Zhang182b06a2020-04-09 14:38:05 -070078 : MediaTranscodingService(std::make_shared<DummyTranscoder>(),
79 std::make_shared<DummyProcessInfo>()) {}
80
81MediaTranscodingService::MediaTranscodingService(
82 const std::shared_ptr<TranscoderInterface>& transcoder,
83 const std::shared_ptr<ProcessInfoInterface>& procInfo)
84 : mJobScheduler(new TranscodingJobScheduler(transcoder, procInfo)),
85 mClientManager(new TranscodingClientManager(mJobScheduler)) {
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080086 ALOGV("MediaTranscodingService is created");
87}
88
89MediaTranscodingService::~MediaTranscodingService() {
90 ALOGE("Should not be in ~MediaTranscodingService");
91}
92
hkuang9c04b8d2020-01-22 10:03:21 -080093binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
94 String8 result;
95 const size_t SIZE = 256;
96 char buffer[SIZE];
97
98 snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this);
99 result.append(buffer);
100 write(fd, result.string(), result.size());
101
102 Vector<String16> args;
Chong Zhang182b06a2020-04-09 14:38:05 -0700103 mClientManager->dumpAllClients(fd, args);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800104 return OK;
105}
106
107//static
108void MediaTranscodingService::instantiate() {
109 std::shared_ptr<MediaTranscodingService> service =
110 ::ndk::SharedRefBase::make<MediaTranscodingService>();
111 binder_status_t status =
112 AServiceManager_addService(service->asBinder().get(), getServiceName());
113 if (status != STATUS_OK) {
114 return;
115 }
116}
117
118Status MediaTranscodingService::registerClient(
Chong Zhang182b06a2020-04-09 14:38:05 -0700119 const std::shared_ptr<ITranscodingClientCallback>& in_callback,
120 const std::string& in_clientName, const std::string& in_opPackageName, int32_t in_clientUid,
121 int32_t in_clientPid, std::shared_ptr<ITranscodingClient>* _aidl_return) {
122 if (in_callback == nullptr) {
123 ALOGE("Client callback can not be null");
Chong Zhang8e062632020-03-31 10:56:37 -0700124 *_aidl_return = nullptr;
hkuang9c04b8d2020-01-22 10:03:21 -0800125 return Status::fromServiceSpecificError(ERROR_ILLEGAL_ARGUMENT);
126 }
127
128 int32_t callingPid = AIBinder_getCallingPid();
129 int32_t callingUid = AIBinder_getCallingUid();
130
Chong Zhang8e062632020-03-31 10:56:37 -0700131 // Check if we can trust clientUid. Only privilege caller could forward the
132 // uid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -0800133 if (in_clientUid == USE_CALLING_UID) {
134 in_clientUid = callingUid;
135 } else if (!isTrustedCallingUid(callingUid)) {
136 ALOGE("MediaTranscodingService::registerClient failed (calling PID %d, calling UID %d) "
137 "rejected "
138 "(don't trust clientUid %d)",
139 in_clientPid, in_clientUid, in_clientUid);
140 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
141 "Untrusted caller (calling PID %d, UID %d) trying to "
142 "register client",
143 in_clientPid, in_clientUid);
144 }
145
Chong Zhang8e062632020-03-31 10:56:37 -0700146 // Check if we can trust clientPid. Only privilege caller could forward the
147 // pid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -0800148 if (in_clientPid == USE_CALLING_PID) {
149 in_clientPid = callingPid;
150 } else if (!isTrustedCallingUid(callingUid)) {
151 ALOGE("MediaTranscodingService::registerClient client failed (calling PID %d, calling UID "
152 "%d) rejected "
153 "(don't trust clientPid %d)",
154 in_clientPid, in_clientUid, in_clientPid);
155 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
156 "Untrusted caller (calling PID %d, UID %d) trying to "
157 "register client",
158 in_clientPid, in_clientUid);
159 }
160
hkuang9c04b8d2020-01-22 10:03:21 -0800161 // Creates the client and uses its process id as client id.
Chong Zhang8e062632020-03-31 10:56:37 -0700162 std::shared_ptr<ITranscodingClient> newClient;
163
Chong Zhang182b06a2020-04-09 14:38:05 -0700164 status_t err = mClientManager->addClient(in_callback, in_clientPid, in_clientUid, in_clientName,
165 in_opPackageName, &newClient);
hkuang9c04b8d2020-01-22 10:03:21 -0800166 if (err != OK) {
Chong Zhang8e062632020-03-31 10:56:37 -0700167 *_aidl_return = nullptr;
hkuang9c04b8d2020-01-22 10:03:21 -0800168 return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
169 }
170
Chong Zhang8e062632020-03-31 10:56:37 -0700171 *_aidl_return = newClient;
hkuang9c04b8d2020-01-22 10:03:21 -0800172 return Status::ok();
173}
174
175Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
176 ALOGD("MediaTranscodingService::getNumOfClients");
Chong Zhang182b06a2020-04-09 14:38:05 -0700177 *_aidl_return = mClientManager->getNumOfClients();
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800178 return Status::ok();
179}
180
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800181} // namespace android