1 /*
2  * Copyright (C) 2022 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 #ifndef CPP_EVS_MANAGER_AIDL_WRAPPERS_INCLUDE_AIDLCAMERASTREAM_H
18 #define CPP_EVS_MANAGER_AIDL_WRAPPERS_INCLUDE_AIDLCAMERASTREAM_H
19 
20 #include <aidl/android/hardware/automotive/evs/BnEvsCameraStream.h>
21 #include <aidl/android/hardware/automotive/evs/BufferDesc.h>
22 #include <aidl/android/hardware/automotive/evs/EvsEventDesc.h>
23 #include <android/hardware/automotive/evs/1.1/IEvsCameraStream.h>
24 #include <android/hardware/automotive/evs/1.1/types.h>
25 
26 #include <list>
27 
28 namespace aidl::android::automotive::evs::implementation {
29 
30 namespace aidlevs = ::aidl::android::hardware::automotive::evs;
31 namespace hidlevs = ::android::hardware::automotive::evs;
32 
33 class AidlCameraStream final :
34       public ::aidl::android::hardware::automotive::evs::BnEvsCameraStream {
35 public:
36     // Methods from ::aidl::android::hardware::automotive::evs::IEvsCameraStream follow.
37     ::ndk::ScopedAStatus deliverFrame(const std::vector<aidlevs::BufferDesc>& buffer) override;
38     ::ndk::ScopedAStatus notify(const aidlevs::EvsEventDesc& event) override;
39 
40     explicit AidlCameraStream(const ::android::sp<hidlevs::V1_0::IEvsCameraStream>& stream);
~AidlCameraStream()41     virtual ~AidlCameraStream() { mImpl = nullptr; }
42 
43     bool getBuffer(int id, aidlevs::BufferDesc* _return);
44 
45 private:
46     class IHidlCameraStream;
47     class ImplV0;
48     class ImplV1;
49 
50     std::shared_ptr<IHidlCameraStream> mImpl;
51 };
52 
53 class AidlCameraStream::IHidlCameraStream {
54 public:
55     virtual ::ndk::ScopedAStatus deliverFrame(const std::vector<aidlevs::BufferDesc>& buffer) = 0;
56     virtual ::ndk::ScopedAStatus notify(const aidlevs::EvsEventDesc& event) = 0;
57     virtual bool getBuffer(int id, aidlevs::BufferDesc* _return);
58 
IHidlCameraStream(const::android::sp<hidlevs::V1_0::IEvsCameraStream> & stream)59     explicit IHidlCameraStream(const ::android::sp<hidlevs::V1_0::IEvsCameraStream>& stream) :
60           mStream(stream) {}
~IHidlCameraStream()61     virtual ~IHidlCameraStream() {
62         mStream = nullptr;
63         mBuffers.clear();
64     }
65 
66 protected:
67     ::android::sp<hidlevs::V1_0::IEvsCameraStream> mStream;
68     std::list<aidlevs::BufferDesc> mBuffers;
69 };
70 
71 class AidlCameraStream::ImplV0 final : public IHidlCameraStream {
72 public:
73     ::ndk::ScopedAStatus deliverFrame(const std::vector<aidlevs::BufferDesc>& buffer) override;
74     ::ndk::ScopedAStatus notify(const aidlevs::EvsEventDesc& event) override;
75 
76     explicit ImplV0(const ::android::sp<hidlevs::V1_0::IEvsCameraStream>& stream);
~ImplV0()77     virtual ~ImplV0() { mStream = nullptr; }
78 };
79 
80 class AidlCameraStream::ImplV1 final : public IHidlCameraStream {
81 public:
82     ::ndk::ScopedAStatus deliverFrame(const std::vector<aidlevs::BufferDesc>& buffer) override;
83     ::ndk::ScopedAStatus notify(const aidlevs::EvsEventDesc& event) override;
84 
85     explicit ImplV1(const ::android::sp<hidlevs::V1_1::IEvsCameraStream>& stream);
~ImplV1()86     virtual ~ImplV1() { mStream = nullptr; }
87 
88 private:
89     ::android::sp<hidlevs::V1_1::IEvsCameraStream> mStream;
90 };
91 
92 }  // namespace aidl::android::automotive::evs::implementation
93 
94 #endif  // CPP_EVS_MANAGER_AIDL_WRAPPERS_INCLUDE_AIDLCAMERASTREAM_H
95