blob: cca36fb7481b5dacb97740b68ac6ea8fa8116748 [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>
Chong Zhangf9077512020-09-21 21:02:06 -070027#include <media/TranscodingResourcePolicy.h>
Chong Zhangbc062482020-10-14 16:43:53 -070028#include <media/TranscodingSessionController.h>
Chong Zhang8677f1f2021-01-21 20:37:35 +000029#include <media/TranscodingThermalPolicy.h>
Chong Zhangacb33502020-04-20 11:04:48 -070030#include <media/TranscodingUidPolicy.h>
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080031#include <utils/Log.h>
32#include <utils/Vector.h>
33
Chong Zhang75222182020-04-29 14:43:42 -070034#include "SimulatedTranscoder.h"
35
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080036namespace android {
37
hkuang9c04b8d2020-01-22 10:03:21 -080038// Convenience methods for constructing binder::Status objects for error returns
39#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
40 Status::fromServiceSpecificErrorWithMessage( \
41 errorCode, \
42 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__))
43
Chong Zhang457c6892021-02-01 15:34:20 -080044MediaTranscodingService::MediaTranscodingService(bool simulated)
Chong Zhang66469272020-06-04 16:51:55 -070045 : mUidPolicy(new TranscodingUidPolicy()),
Chong Zhangf9077512020-09-21 21:02:06 -070046 mResourcePolicy(new TranscodingResourcePolicy()),
Chong Zhang457c6892021-02-01 15:34:20 -080047 mThermalPolicy(new TranscodingThermalPolicy()) {
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080048 ALOGV("MediaTranscodingService is created");
Chong Zhang457c6892021-02-01 15:34:20 -080049 mSessionController.reset(new TranscodingSessionController(
50 [simulated](const std::shared_ptr<TranscoderCallbackInterface>& cb,
51 int64_t heartBeatUs) -> std::shared_ptr<TranscoderInterface> {
52 if (simulated) {
53 return std::make_shared<SimulatedTranscoder>(cb, heartBeatUs);
54 }
55 return std::make_shared<TranscoderWrapper>(cb, heartBeatUs);
56 },
57 mUidPolicy, mResourcePolicy, mThermalPolicy));
58 mClientManager.reset(new TranscodingClientManager(mSessionController));
Chong Zhangbc062482020-10-14 16:43:53 -070059 mUidPolicy->setCallback(mSessionController);
60 mResourcePolicy->setCallback(mSessionController);
Chong Zhang8677f1f2021-01-21 20:37:35 +000061 mThermalPolicy->setCallback(mSessionController);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080062}
63
64MediaTranscodingService::~MediaTranscodingService() {
65 ALOGE("Should not be in ~MediaTranscodingService");
66}
67
hkuang9c04b8d2020-01-22 10:03:21 -080068binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
69 String8 result;
Linus Nilsson06fab722020-07-16 15:53:14 -070070
Chong Zhangefeabbd2020-11-18 09:31:58 -080071 uid_t callingUid = AIBinder_getCallingUid();
72 pid_t callingPid = AIBinder_getCallingPid();
Chong Zhang5c504ee2021-01-21 18:53:19 -080073 if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
Jiyong Parkff998612021-01-15 16:01:38 +090074 int32_t permissionResult;
75 if (APermissionManager_checkPermission("android.permission.DUMP", callingPid, callingUid,
76 &permissionResult) != PERMISSION_MANAGER_STATUS_OK ||
77 permissionResult != PERMISSION_MANAGER_PERMISSION_GRANTED) {
78 result.format(
79 "Permission Denial: "
80 "can't dump MediaTranscodingService from pid=%d, uid=%d\n",
81 AIBinder_getCallingPid(), AIBinder_getCallingUid());
82 write(fd, result.string(), result.size());
83 return PERMISSION_DENIED;
84 }
Linus Nilsson06fab722020-07-16 15:53:14 -070085 }
86
hkuang9c04b8d2020-01-22 10:03:21 -080087 const size_t SIZE = 256;
88 char buffer[SIZE];
89
90 snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this);
91 result.append(buffer);
92 write(fd, result.string(), result.size());
93
94 Vector<String16> args;
Chong Zhang182b06a2020-04-09 14:38:05 -070095 mClientManager->dumpAllClients(fd, args);
Chong Zhangbc062482020-10-14 16:43:53 -070096 mSessionController->dumpAllSessions(fd, args);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080097 return OK;
98}
99
100//static
101void MediaTranscodingService::instantiate() {
102 std::shared_ptr<MediaTranscodingService> service =
Chong Zhang457c6892021-02-01 15:34:20 -0800103 ::ndk::SharedRefBase::make<MediaTranscodingService>(
104 property_get_bool("debug.transcoding.simulated_transcoder", false));
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800105 binder_status_t status =
106 AServiceManager_addService(service->asBinder().get(), getServiceName());
107 if (status != STATUS_OK) {
108 return;
109 }
110}
111
112Status MediaTranscodingService::registerClient(
Chong Zhang182b06a2020-04-09 14:38:05 -0700113 const std::shared_ptr<ITranscodingClientCallback>& in_callback,
Chong Zhang3f23e982020-09-24 14:03:41 -0700114 const std::string& in_clientName, const std::string& in_opPackageName,
115 std::shared_ptr<ITranscodingClient>* _aidl_return) {
Chong Zhang182b06a2020-04-09 14:38:05 -0700116 if (in_callback == nullptr) {
Chong Zhang8e062632020-03-31 10:56:37 -0700117 *_aidl_return = nullptr;
Chong Zhang15c192a2020-05-05 16:24:00 -0700118 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Client callback cannot be null!");
hkuang9c04b8d2020-01-22 10:03:21 -0800119 }
120
hkuang9c04b8d2020-01-22 10:03:21 -0800121 // Creates the client and uses its process id as client id.
Chong Zhang8e062632020-03-31 10:56:37 -0700122 std::shared_ptr<ITranscodingClient> newClient;
123
Chong Zhang3f23e982020-09-24 14:03:41 -0700124 status_t err =
125 mClientManager->addClient(in_callback, in_clientName, in_opPackageName, &newClient);
hkuang9c04b8d2020-01-22 10:03:21 -0800126 if (err != OK) {
Chong Zhang8e062632020-03-31 10:56:37 -0700127 *_aidl_return = nullptr;
hkuang9c04b8d2020-01-22 10:03:21 -0800128 return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
129 }
130
Chong Zhang8e062632020-03-31 10:56:37 -0700131 *_aidl_return = newClient;
hkuang9c04b8d2020-01-22 10:03:21 -0800132 return Status::ok();
133}
134
135Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
136 ALOGD("MediaTranscodingService::getNumOfClients");
Chong Zhang182b06a2020-04-09 14:38:05 -0700137 *_aidl_return = mClientManager->getNumOfClients();
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800138 return Status::ok();
139}
140
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800141} // namespace android