1 /*
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 // A fake ECOServiceStatsProvider for testing ECOService and ECOSession.
18 
19 #include <aidl/android/media/eco/BnECOServiceStatsProvider.h>
20 #include <android-base/unique_fd.h>
21 #include <android/binder_auto_utils.h>
22 #include <android/binder_parcel.h>
23 #include <cutils/ashmem.h>
24 #include <gtest/gtest.h>
25 #include <math.h>
26 #include <stdlib.h>
27 #include <sys/mman.h>
28 #include <utils/Log.h>
29 
30 #include <condition_variable>
31 #include <deque>
32 #include <list>
33 #include <memory>
34 #include <mutex>
35 #include <thread>
36 
37 #include "eco/ECOData.h"
38 #include "eco/ECODataKey.h"
39 #include "eco/ECOService.h"
40 
41 namespace android {
42 namespace media {
43 namespace eco {
44 
45 using ::ndk::ScopedAStatus;
46 
47 /**
48  * A fake ECOServiceStatsProvider.
49  *
50  * FakeECOServiceStatsProvider is a fake ECOServiceStatsProvider that used for testing.
51  */
52 
53 class FakeECOServiceStatsProvider : public BnECOServiceStatsProvider {
54 public:
55     FakeECOServiceStatsProvider(int32_t width, int32_t height, bool isCameraRecording,
56                                 float frameRate, std::shared_ptr<IECOSession> session);
57 
58     FakeECOServiceStatsProvider(int32_t width, int32_t height, bool isCameraRecording,
59                                 float frameRate);
60 
setECOSession(std::shared_ptr<IECOSession> session)61     void setECOSession(std::shared_ptr<IECOSession> session) { mECOSession = session; }
62 
63     // Helper function to inject session stats to the FakeECOServiceStatsProvider so provider
64     // could push to the service.
65     bool injectSessionStats(const ECOData& stats);
66 
67     // Helper function to inject each frame's stats to the FakeECOServiceStatsProvider so provider
68     // could push to the service.
69     bool injectFrameStats(const ECOData& stats);
70 
71     /* Starts the FakeECOServiceStatsProvider */
72     void start();
73 
74     /* Stops FakeECOServiceStatsProvider */
75     void stop();
76 
77     virtual ~FakeECOServiceStatsProvider();
78 
79     virtual ScopedAStatus getType(int32_t* _aidl_return);
80     virtual ScopedAStatus getName(std::string* _aidl_return);
81     virtual ScopedAStatus getECOSession(::ndk::SpAIBinder* _aidl_return);
82 
83     // IBinder::DeathRecipient implementation
84     virtual void binderDied(const std::weak_ptr<AIBinder>& who);
85 
86 private:
87     int32_t mWidth;
88     int32_t mHeight;
89     bool mIsCameraRecording;
90     float mFrameRate;
91     uint32_t mFrameNumber;
92 
93     std::shared_ptr<IECOSession> mECOSession;
94 };
95 
96 }  // namespace eco
97 }  // namespace media
98 }  // namespace android
99