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