1 /*
2  * Copyright (C) 2020 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 "ECOServiceStatsProvider"
19 #include <eco/ECOServiceStatsProvider.h>
20 
21 namespace android {
22 namespace media {
23 namespace eco {
24 
ECOServiceStatsProvider(int32_t width,int32_t height,bool isCameraRecording,android::sp<IECOSession> & session,const char * name)25 ECOServiceStatsProvider::ECOServiceStatsProvider(
26     int32_t width, int32_t height, bool isCameraRecording,
27     android::sp<IECOSession>& session, const char* name)
28       : BnECOServiceStatsProvider(),
29         mWidth(width),
30         mHeight(height),
31         mIsCameraRecording(isCameraRecording),
32         mECOSession(session),
33         mProviderName(name) {
34     ALOGD("%s, construct with w: %d, h: %d, isCameraRecording: %d, ProviderName:%s",
35             __func__, width, height, isCameraRecording, name);
36 }
37 
getType(int32_t * _aidl_return)38 Status ECOServiceStatsProvider::getType(int32_t* _aidl_return) {
39     *_aidl_return = STATS_PROVIDER_TYPE_VIDEO_ENCODER;
40     return Status::ok();
41 }
42 
getName(::android::String16 * _aidl_return)43 Status ECOServiceStatsProvider::getName(::android::String16* _aidl_return) {
44     *_aidl_return = String16(mProviderName);
45     return Status::ok();
46 }
47 
getECOSession(android::sp<::android::IBinder> * _aidl_return)48 Status ECOServiceStatsProvider::getECOSession(android::sp<::android::IBinder>* _aidl_return) {
49     *_aidl_return = IInterface::asBinder(mECOSession);
50     return Status::ok();
51 }
52 
isCameraRecording(bool * _aidl_return)53 Status ECOServiceStatsProvider::isCameraRecording(bool* _aidl_return) {
54     *_aidl_return = mIsCameraRecording;
55     return Status::ok();
56 }
57 
binderDied(const wp<IBinder> &)58 void ECOServiceStatsProvider::binderDied(const wp<IBinder>& /* who */) {}
59 
updateStats(const ECOData & data)60 bool ECOServiceStatsProvider::updateStats(const ECOData& data) {
61     bool ret = false;
62     if (mECOSession) {
63         Status status = mECOSession->pushNewStats(data, &ret);
64         return ret;
65     }
66     return ret;
67 }
68 
addProvider()69 bool ECOServiceStatsProvider::addProvider() {
70     bool ret = false;
71     if (mECOSession) {
72         ECOData providerConfig(ECOData::DATA_TYPE_STATS_PROVIDER_CONFIG, systemTime() / 1000);
73         mECOSession->addStatsProvider(this, providerConfig, &ret);
74         return ret;
75     }
76     return ret;
77 }
78 
removeProvider()79 bool ECOServiceStatsProvider::removeProvider() {
80     bool ret = false;
81     if (mECOSession) {
82         mECOSession->removeStatsProvider(this, &ret);
83         return ret;
84     }
85     return ret;
86 }
87 
create(int32_t width,int32_t height,bool isCameraRecording,const char * name)88 android::sp<ECOServiceStatsProvider> ECOServiceStatsProvider::create(
89     int32_t width, int32_t height, bool isCameraRecording, const char* name) {
90 
91     android::sp<android::IServiceManager> sm = android::defaultServiceManager();
92     android::sp<android::IBinder> binder = sm->getService(String16("media.ecoservice"));
93 
94     if (binder == nullptr) {
95         ALOGE("Failed to connect to ecoservice");
96         return nullptr;
97     }
98 
99     android::sp<IECOService> service = android::interface_cast<IECOService>(binder);
100     ALOGI("Connected to ecoservice");
101 
102     // Obtain the ECOSession and add the listener to the service.
103     android::sp<IECOSession> session = nullptr;
104 
105     service->obtainSession(width, height, isCameraRecording, &session);
106 
107     if (session == nullptr) {
108         ALOGE("Failed to obtain an ECO session");
109         return nullptr;
110     }
111     ALOGI("Obtained an ECO session");
112 
113     return new ECOServiceStatsProvider(width, height, isCameraRecording, session, name);
114 }
115 
getFramerate(int64_t currTimestamp)116 float ECOServiceStatsProvider::getFramerate(int64_t currTimestamp) {
117     float framerate;
118     int64_t timeInterval = currTimestamp - mLastFrameTimestamp;
119     if (timeInterval == 0) {
120         framerate = 0.0;
121     } else {
122         framerate = 1E6 / (currTimestamp - mLastFrameTimestamp);
123     }
124     mLastFrameTimestamp = currTimestamp;
125     return framerate;
126 }
127 
128 }  // namespace eco
129 }  // namespace media
130 }  // namespace android
131