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