blob: 8b641340d3dae02c82941beb40a894864a3f2e72 [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 Zhangefeabbd2020-11-18 09:31:58 -080023#include <android/permission_manager.h>
Chong Zhang66469272020-06-04 16:51:55 -070024#include <cutils/properties.h>
25#include <media/TranscoderWrapper.h>
Chong Zhang182b06a2020-04-09 14:38:05 -070026#include <media/TranscodingClientManager.h>
Linus Nilssona99f4042021-02-25 15:49:43 -080027#include <media/TranscodingLogger.h>
Chong Zhangf9077512020-09-21 21:02:06 -070028#include <media/TranscodingResourcePolicy.h>
Chong Zhangbc062482020-10-14 16:43:53 -070029#include <media/TranscodingSessionController.h>
Chong Zhang8677f1f2021-01-21 20:37:35 +000030#include <media/TranscodingThermalPolicy.h>
Chong Zhangacb33502020-04-20 11:04:48 -070031#include <media/TranscodingUidPolicy.h>
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080032#include <utils/Log.h>
33#include <utils/Vector.h>
34
Chong Zhang75222182020-04-29 14:43:42 -070035#include "SimulatedTranscoder.h"
36
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080037namespace android {
38
hkuang9c04b8d2020-01-22 10:03:21 -080039// 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 Zhang87d199c2021-03-01 19:02:18 -080045static constexpr int64_t kTranscoderHeartBeatIntervalUs = 1000000LL;
46
47MediaTranscodingService::MediaTranscodingService()
Chong Zhang66469272020-06-04 16:51:55 -070048 : mUidPolicy(new TranscodingUidPolicy()),
Chong Zhangf9077512020-09-21 21:02:06 -070049 mResourcePolicy(new TranscodingResourcePolicy()),
Linus Nilssona99f4042021-02-25 15:49:43 -080050 mThermalPolicy(new TranscodingThermalPolicy()),
51 mLogger(new TranscodingLogger()) {
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080052 ALOGV("MediaTranscodingService is created");
Chong Zhang87d199c2021-03-01 19:02:18 -080053 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 Zhang457c6892021-02-01 15:34:20 -080088 mClientManager.reset(new TranscodingClientManager(mSessionController));
Chong Zhangbc062482020-10-14 16:43:53 -070089 mUidPolicy->setCallback(mSessionController);
90 mResourcePolicy->setCallback(mSessionController);
Chong Zhang8677f1f2021-01-21 20:37:35 +000091 mThermalPolicy->setCallback(mSessionController);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080092}
93
94MediaTranscodingService::~MediaTranscodingService() {
95 ALOGE("Should not be in ~MediaTranscodingService");
96}
97
hkuang9c04b8d2020-01-22 10:03:21 -080098binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
99 String8 result;
Linus Nilsson06fab722020-07-16 15:53:14 -0700100
Chong Zhangefeabbd2020-11-18 09:31:58 -0800101 uid_t callingUid = AIBinder_getCallingUid();
102 pid_t callingPid = AIBinder_getCallingPid();
Chong Zhang5c504ee2021-01-21 18:53:19 -0800103 if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
Jiyong Parkff998612021-01-15 16:01:38 +0900104 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 Nilsson06fab722020-07-16 15:53:14 -0700115 }
116
hkuang9c04b8d2020-01-22 10:03:21 -0800117 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 Zhang182b06a2020-04-09 14:38:05 -0700125 mClientManager->dumpAllClients(fd, args);
Chong Zhangbc062482020-10-14 16:43:53 -0700126 mSessionController->dumpAllSessions(fd, args);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800127 return OK;
128}
129
130//static
131void MediaTranscodingService::instantiate() {
132 std::shared_ptr<MediaTranscodingService> service =
Chong Zhang87d199c2021-03-01 19:02:18 -0800133 ::ndk::SharedRefBase::make<MediaTranscodingService>();
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800134 binder_status_t status =
135 AServiceManager_addService(service->asBinder().get(), getServiceName());
136 if (status != STATUS_OK) {
137 return;
138 }
139}
140
141Status MediaTranscodingService::registerClient(
Chong Zhang182b06a2020-04-09 14:38:05 -0700142 const std::shared_ptr<ITranscodingClientCallback>& in_callback,
Chong Zhang3f23e982020-09-24 14:03:41 -0700143 const std::string& in_clientName, const std::string& in_opPackageName,
144 std::shared_ptr<ITranscodingClient>* _aidl_return) {
Chong Zhang182b06a2020-04-09 14:38:05 -0700145 if (in_callback == nullptr) {
Chong Zhang8e062632020-03-31 10:56:37 -0700146 *_aidl_return = nullptr;
Chong Zhang15c192a2020-05-05 16:24:00 -0700147 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Client callback cannot be null!");
hkuang9c04b8d2020-01-22 10:03:21 -0800148 }
149
hkuang9c04b8d2020-01-22 10:03:21 -0800150 // Creates the client and uses its process id as client id.
Chong Zhang8e062632020-03-31 10:56:37 -0700151 std::shared_ptr<ITranscodingClient> newClient;
152
Chong Zhang3f23e982020-09-24 14:03:41 -0700153 status_t err =
154 mClientManager->addClient(in_callback, in_clientName, in_opPackageName, &newClient);
hkuang9c04b8d2020-01-22 10:03:21 -0800155 if (err != OK) {
Chong Zhang8e062632020-03-31 10:56:37 -0700156 *_aidl_return = nullptr;
hkuang9c04b8d2020-01-22 10:03:21 -0800157 return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
158 }
159
Chong Zhang8e062632020-03-31 10:56:37 -0700160 *_aidl_return = newClient;
hkuang9c04b8d2020-01-22 10:03:21 -0800161 return Status::ok();
162}
163
164Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
165 ALOGD("MediaTranscodingService::getNumOfClients");
Chong Zhang182b06a2020-04-09 14:38:05 -0700166 *_aidl_return = mClientManager->getNumOfClients();
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800167 return Status::ok();
168}
169
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800170} // namespace android