1 /*
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"
19 #include "MediaTranscodingService.h"
20
21 #include <android/binder_manager.h>
22 #include <android/binder_process.h>
23 #include <android/permission_manager.h>
24 #include <cutils/properties.h>
25 #include <media/TranscoderWrapper.h>
26 #include <media/TranscodingClientManager.h>
27 #include <media/TranscodingDefs.h>
28 #include <media/TranscodingLogger.h>
29 #include <media/TranscodingResourcePolicy.h>
30 #include <media/TranscodingSessionController.h>
31 #include <media/TranscodingThermalPolicy.h>
32 #include <media/TranscodingUidPolicy.h>
33 #include <utils/Log.h>
34 #include <utils/Vector.h>
35
36 #include "SimulatedTranscoder.h"
37
38 namespace android {
39
40 // Convenience methods for constructing binder::Status objects for error returns
41 #define STATUS_ERROR_FMT(errorCode, errorString, ...) \
42 Status::fromServiceSpecificErrorWithMessage( \
43 errorCode, \
44 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__))
45
46 static constexpr int64_t kTranscoderHeartBeatIntervalUs = 1000000LL;
47
MediaTranscodingService()48 MediaTranscodingService::MediaTranscodingService()
49 : mUidPolicy(new TranscodingUidPolicy()),
50 mResourcePolicy(new TranscodingResourcePolicy()),
51 mThermalPolicy(new TranscodingThermalPolicy()),
52 mLogger(new TranscodingLogger()) {
53 ALOGV("MediaTranscodingService is created");
54 bool simulated = property_get_bool("debug.transcoding.simulated_transcoder", false);
55 if (simulated) {
56 // Overrid default config params with shorter values for testing.
57 TranscodingSessionController::ControllerConfig config = {
58 .pacerBurstThresholdMs = 500,
59 .pacerBurstCountQuota = 10,
60 .pacerBurstTimeQuotaSeconds = 3,
61 };
62 mSessionController.reset(new TranscodingSessionController(
63 [](const std::shared_ptr<TranscoderCallbackInterface>& cb)
64 -> std::shared_ptr<TranscoderInterface> {
65 return std::make_shared<SimulatedTranscoder>(cb);
66 },
67 mUidPolicy, mResourcePolicy, mThermalPolicy, &config));
68 } else {
69 int32_t overrideBurstCountQuota =
70 property_get_int32("persist.transcoding.burst_count_quota", -1);
71 int32_t pacerBurstTimeQuotaSeconds =
72 property_get_int32("persist.transcoding.burst_time_quota_seconds", -1);
73 // Override default config params with properties if present.
74 TranscodingSessionController::ControllerConfig config;
75 if (overrideBurstCountQuota > 0) {
76 config.pacerBurstCountQuota = overrideBurstCountQuota;
77 }
78 if (pacerBurstTimeQuotaSeconds > 0) {
79 config.pacerBurstTimeQuotaSeconds = pacerBurstTimeQuotaSeconds;
80 }
81 mSessionController.reset(new TranscodingSessionController(
82 [logger = mLogger](const std::shared_ptr<TranscoderCallbackInterface>& cb)
83 -> std::shared_ptr<TranscoderInterface> {
84 return std::make_shared<TranscoderWrapper>(cb, logger,
85 kTranscoderHeartBeatIntervalUs);
86 },
87 mUidPolicy, mResourcePolicy, mThermalPolicy, &config));
88 }
89 mClientManager.reset(new TranscodingClientManager(mSessionController));
90 mUidPolicy->setCallback(mSessionController);
91 mResourcePolicy->setCallback(mSessionController);
92 mThermalPolicy->setCallback(mSessionController);
93 }
94
~MediaTranscodingService()95 MediaTranscodingService::~MediaTranscodingService() {
96 ALOGE("Should not be in ~MediaTranscodingService");
97 }
98
dump(int fd,const char **,uint32_t)99 binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
100 String8 result;
101
102 uid_t callingUid = AIBinder_getCallingUid();
103 pid_t callingPid = AIBinder_getCallingPid();
104 if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
105 int32_t permissionResult;
106 if (APermissionManager_checkPermission("android.permission.DUMP", callingPid, callingUid,
107 &permissionResult) != PERMISSION_MANAGER_STATUS_OK ||
108 permissionResult != PERMISSION_MANAGER_PERMISSION_GRANTED) {
109 result.format(
110 "Permission Denial: "
111 "can't dump MediaTranscodingService from pid=%d, uid=%d\n",
112 AIBinder_getCallingPid(), AIBinder_getCallingUid());
113 write(fd, result.c_str(), result.size());
114 return PERMISSION_DENIED;
115 }
116 }
117
118 const size_t SIZE = 256;
119 char buffer[SIZE];
120
121 snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this);
122 result.append(buffer);
123 write(fd, result.c_str(), result.size());
124
125 Vector<String16> args;
126 mClientManager->dumpAllClients(fd, args);
127 mSessionController->dumpAllSessions(fd, args);
128 return OK;
129 }
130
131 //static
instantiate()132 void MediaTranscodingService::instantiate() {
133 std::shared_ptr<MediaTranscodingService> service =
134 ::ndk::SharedRefBase::make<MediaTranscodingService>();
135 if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
136 // Once service is started, we want it to stay even is client side perished.
137 AServiceManager_forceLazyServicesPersist(true /*persist*/);
138 (void)AServiceManager_registerLazyService(service->asBinder().get(), getServiceName());
139 }
140 }
141
registerClient(const std::shared_ptr<ITranscodingClientCallback> & in_callback,const std::string & in_clientName,const std::string & in_opPackageName,std::shared_ptr<ITranscodingClient> * _aidl_return)142 Status MediaTranscodingService::registerClient(
143 const std::shared_ptr<ITranscodingClientCallback>& in_callback,
144 const std::string& in_clientName, const std::string& in_opPackageName,
145 std::shared_ptr<ITranscodingClient>* _aidl_return) {
146 if (in_callback == nullptr) {
147 *_aidl_return = nullptr;
148 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Client callback cannot be null!");
149 }
150
151 // Creates the client and uses its process id as client id.
152 std::shared_ptr<ITranscodingClient> newClient;
153
154 status_t err =
155 mClientManager->addClient(in_callback, in_clientName, in_opPackageName, &newClient);
156 if (err != OK) {
157 *_aidl_return = nullptr;
158 return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
159 }
160
161 *_aidl_return = newClient;
162 return Status::ok();
163 }
164
getNumOfClients(int32_t * _aidl_return)165 Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
166 ALOGD("MediaTranscodingService::getNumOfClients");
167 *_aidl_return = mClientManager->getNumOfClients();
168 return Status::ok();
169 }
170
171 } // namespace android
172