1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #ifndef COMPUTEPIPE_EXAMPLE_FACE_TRACKER_H
15 #define COMPUTEPIPE_EXAMPLE_FACE_TRACKER_H
16 
17 #include <aidl/android/automotive/computepipe/registry/BnClientInfo.h>
18 #include <aidl/android/automotive/computepipe/registry/IPipeQuery.h>
19 #include <aidl/android/automotive/computepipe/registry/IPipeRegistration.h>
20 #include <aidl/android/automotive/computepipe/runner/BnPipeStateCallback.h>
21 #include <aidl/android/automotive/computepipe/runner/BnPipeStream.h>
22 #include <aidl/android/automotive/computepipe/runner/PipeState.h>
23 
24 #include <condition_variable>
25 #include <iostream>
26 #include <memory>
27 
28 #include "FaceOutput.pb.h"
29 
30 namespace android {
31 namespace automotive {
32 namespace computepipe {
33 
34 using ::aidl::android::automotive::computepipe::registry::BnClientInfo;
35 using ::aidl::android::automotive::computepipe::registry::IPipeQuery;
36 using ::aidl::android::automotive::computepipe::runner::BnPipeStateCallback;
37 using ::aidl::android::automotive::computepipe::runner::BnPipeStream;
38 using ::aidl::android::automotive::computepipe::runner::IPipeRunner;
39 using ::aidl::android::automotive::computepipe::runner::IPipeStream;
40 using ::aidl::android::automotive::computepipe::runner::PacketDescriptor;
41 using ::aidl::android::automotive::computepipe::runner::PipeState;
42 using ::android::automotive::computepipe::example::BoundingBox;
43 
44 class RemoteState {
45   public:
46     explicit RemoteState(std::function<void(bool, std::string)>& cb);
47     PipeState GetCurrentState();
48     void UpdateCurrentState(const PipeState& state);
49 
50   private:
51     bool hasChanged = false;
52     PipeState mState = PipeState::RESET;
53     std::mutex mStateLock;
54     std::condition_variable mWait;
55     std::function<void(bool, std::string)> mTerminationCb;
56 };
57 
58 class ClientInfo : public BnClientInfo {
59   public:
getClientName(std::string * _aidl_return)60     ndk::ScopedAStatus getClientName(std::string* _aidl_return) override {
61         if (_aidl_return) {
62             *_aidl_return = "FaceTrackerClient";
63             return ndk::ScopedAStatus::ok();
64         }
65         return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
66     }
67 };
68 
69 class StreamCallback : public BnPipeStream {
70   public:
71     StreamCallback() = default;
72     ndk::ScopedAStatus deliverPacket(const PacketDescriptor& in_packet) override;
73 
74   private:
75     BoundingBox mLastBox;
76 };
77 
78 class StateCallback : public BnPipeStateCallback {
79   public:
80     explicit StateCallback(std::shared_ptr<RemoteState> s);
81     ndk::ScopedAStatus handleState(PipeState state) override;
82 
83   private:
84     std::shared_ptr<RemoteState> mStateTracker = nullptr;
85 };
86 
87 class FaceTracker {
88   public:
89     FaceTracker() = default;
90     ndk::ScopedAStatus init(std::function<void(bool, std::string)>&& termination);
91     void start();
92     void stop();
93 
94   private:
95     ndk::ScopedAStatus setupConfig();
96     std::shared_ptr<IPipeRunner> mPipeRunner = nullptr;
97     std::shared_ptr<ClientInfo> mClientInfo = nullptr;
98     std::shared_ptr<StreamCallback> mStreamCallback = nullptr;
99     std::shared_ptr<StateCallback> mStateCallback = nullptr;
100     std::shared_ptr<RemoteState> mRemoteState = nullptr;
101 };
102 
103 }  // namespace computepipe
104 }  // namespace automotive
105 }  // namespace android
106 #endif
107