blob: 56f327ed95ffba6f7dfdd1482cedc9f748f68d8b [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>
Linus Nilsson06fab722020-07-16 15:53:14 -070023#include <binder/IServiceManager.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 Zhangacb33502020-04-20 11:04:48 -070029#include <media/TranscodingUidPolicy.h>
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080030#include <utils/Log.h>
31#include <utils/Vector.h>
32
Chong Zhang75222182020-04-29 14:43:42 -070033#include "SimulatedTranscoder.h"
34
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080035namespace android {
36
hkuang9c04b8d2020-01-22 10:03:21 -080037// Convenience methods for constructing binder::Status objects for error returns
38#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
39 Status::fromServiceSpecificErrorWithMessage( \
40 errorCode, \
41 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__))
42
Chong Zhang182b06a2020-04-09 14:38:05 -070043MediaTranscodingService::MediaTranscodingService(
Chong Zhang66469272020-06-04 16:51:55 -070044 const std::shared_ptr<TranscoderInterface>& transcoder)
45 : mUidPolicy(new TranscodingUidPolicy()),
Chong Zhangf9077512020-09-21 21:02:06 -070046 mResourcePolicy(new TranscodingResourcePolicy()),
Chong Zhangbc062482020-10-14 16:43:53 -070047 mSessionController(
48 new TranscodingSessionController(transcoder, mUidPolicy, mResourcePolicy)),
49 mClientManager(new TranscodingClientManager(mSessionController)) {
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080050 ALOGV("MediaTranscodingService is created");
Chong Zhangbc062482020-10-14 16:43:53 -070051 transcoder->setCallback(mSessionController);
52 mUidPolicy->setCallback(mSessionController);
53 mResourcePolicy->setCallback(mSessionController);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080054}
55
56MediaTranscodingService::~MediaTranscodingService() {
57 ALOGE("Should not be in ~MediaTranscodingService");
58}
59
hkuang9c04b8d2020-01-22 10:03:21 -080060binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
61 String8 result;
Linus Nilsson06fab722020-07-16 15:53:14 -070062
Linus Nilssonb65fbaf2020-07-17 12:05:48 -070063 // TODO(b/161549994): Remove libbinder dependencies for mainline.
Linus Nilsson06fab722020-07-16 15:53:14 -070064 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
65 result.format(
66 "Permission Denial: "
67 "can't dump MediaTranscodingService from pid=%d, uid=%d\n",
68 AIBinder_getCallingPid(), AIBinder_getCallingUid());
69 write(fd, result.string(), result.size());
70 return PERMISSION_DENIED;
71 }
72
hkuang9c04b8d2020-01-22 10:03:21 -080073 const size_t SIZE = 256;
74 char buffer[SIZE];
75
76 snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this);
77 result.append(buffer);
78 write(fd, result.string(), result.size());
79
80 Vector<String16> args;
Chong Zhang182b06a2020-04-09 14:38:05 -070081 mClientManager->dumpAllClients(fd, args);
Chong Zhangbc062482020-10-14 16:43:53 -070082 mSessionController->dumpAllSessions(fd, args);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080083 return OK;
84}
85
86//static
87void MediaTranscodingService::instantiate() {
Chong Zhang66469272020-06-04 16:51:55 -070088 std::shared_ptr<TranscoderInterface> transcoder;
Chong Zhang17495a92020-06-25 10:39:05 -070089 if (property_get_bool("debug.transcoding.simulated_transcoder", false)) {
Chong Zhang66469272020-06-04 16:51:55 -070090 transcoder = std::make_shared<SimulatedTranscoder>();
91 } else {
92 transcoder = std::make_shared<TranscoderWrapper>();
93 }
94
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080095 std::shared_ptr<MediaTranscodingService> service =
Chong Zhang66469272020-06-04 16:51:55 -070096 ::ndk::SharedRefBase::make<MediaTranscodingService>(transcoder);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080097 binder_status_t status =
98 AServiceManager_addService(service->asBinder().get(), getServiceName());
99 if (status != STATUS_OK) {
100 return;
101 }
102}
103
104Status MediaTranscodingService::registerClient(
Chong Zhang182b06a2020-04-09 14:38:05 -0700105 const std::shared_ptr<ITranscodingClientCallback>& in_callback,
Chong Zhang3f23e982020-09-24 14:03:41 -0700106 const std::string& in_clientName, const std::string& in_opPackageName,
107 std::shared_ptr<ITranscodingClient>* _aidl_return) {
Chong Zhang182b06a2020-04-09 14:38:05 -0700108 if (in_callback == nullptr) {
Chong Zhang8e062632020-03-31 10:56:37 -0700109 *_aidl_return = nullptr;
Chong Zhang15c192a2020-05-05 16:24:00 -0700110 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Client callback cannot be null!");
hkuang9c04b8d2020-01-22 10:03:21 -0800111 }
112
hkuang9c04b8d2020-01-22 10:03:21 -0800113 // Creates the client and uses its process id as client id.
Chong Zhang8e062632020-03-31 10:56:37 -0700114 std::shared_ptr<ITranscodingClient> newClient;
115
Chong Zhang3f23e982020-09-24 14:03:41 -0700116 status_t err =
117 mClientManager->addClient(in_callback, in_clientName, in_opPackageName, &newClient);
hkuang9c04b8d2020-01-22 10:03:21 -0800118 if (err != OK) {
Chong Zhang8e062632020-03-31 10:56:37 -0700119 *_aidl_return = nullptr;
hkuang9c04b8d2020-01-22 10:03:21 -0800120 return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
121 }
122
Chong Zhang8e062632020-03-31 10:56:37 -0700123 *_aidl_return = newClient;
hkuang9c04b8d2020-01-22 10:03:21 -0800124 return Status::ok();
125}
126
127Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
128 ALOGD("MediaTranscodingService::getNumOfClients");
Chong Zhang182b06a2020-04-09 14:38:05 -0700129 *_aidl_return = mClientManager->getNumOfClients();
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800130 return Status::ok();
131}
132
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800133} // namespace android