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 #ifndef CPP_EVS_MANAGER_1_1_VIRTUALCAMERA_H_
18 #define CPP_EVS_MANAGER_1_1_VIRTUALCAMERA_H_
19 
20 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h>
21 #include <android/hardware/automotive/evs/1.1/IEvsCameraStream.h>
22 #include <android/hardware/automotive/evs/1.1/IEvsDisplay.h>
23 #include <android/hardware/automotive/evs/1.1/types.h>
24 #include <utils/Mutex.h>
25 
26 #include <deque>
27 #include <set>
28 #include <thread>  // NOLINT
29 #include <unordered_map>
30 
31 namespace android::automotive::evs::V1_1::implementation {
32 
33 class HalCamera;  // From HalCamera.h
34 
35 // This class represents an EVS camera to the client application.  As such it presents
36 // the IEvsCamera interface, and also proxies the frame delivery to the client's
37 // IEvsCameraStream object.
38 class VirtualCamera : public hardware::automotive::evs::V1_1::IEvsCamera {
39 public:
40     explicit VirtualCamera(const std::vector<sp<HalCamera>>& halCameras);
41     virtual ~VirtualCamera();
42 
getAllowedBuffers()43     unsigned getAllowedBuffers() { return mFramesAllowed; }
isStreaming()44     bool isStreaming() { return mStreamState == RUNNING; }
getVersion()45     bool getVersion() const { return static_cast<int>(mStream_1_1 != nullptr); }
46     std::vector<sp<HalCamera>> getHalCameras();
setDescriptor(hardware::automotive::evs::V1_1::CameraDesc * desc)47     void setDescriptor(hardware::automotive::evs::V1_1::CameraDesc* desc) { mDesc = desc; }
48 
49     // Proxy to receive frames and forward them to the client's stream
50     bool notify(const hardware::automotive::evs::V1_1::EvsEventDesc& event);
51     bool deliverFrame(const hardware::automotive::evs::V1_1::BufferDesc& bufDesc);
52 
53     // Methods from hardware::automotive::evs::V1_0::IEvsCamera follow.
54     hardware::Return<void> getCameraInfo(getCameraInfo_cb _hidl_cb) override;
55     hardware::Return<hardware::automotive::evs::V1_0::EvsResult> setMaxFramesInFlight(
56             uint32_t bufferCount) override;
57     hardware::Return<hardware::automotive::evs::V1_0::EvsResult> startVideoStream(
58             const ::android::sp<hardware::automotive::evs::V1_0::IEvsCameraStream>& stream)
59             override;
60     hardware::Return<void> doneWithFrame(
61             const hardware::automotive::evs::V1_0::BufferDesc& buffer) override;
62     hardware::Return<void> stopVideoStream() override;
63     hardware::Return<int32_t> getExtendedInfo(uint32_t opaqueIdentifier) override;
64     hardware::Return<hardware::automotive::evs::V1_0::EvsResult> setExtendedInfo(
65             uint32_t opaqueIdentifier, int32_t opaqueValue) override;
66 
67     // Methods from hardware::automotive::evs::V1_1::IEvsCamera follow.
68     hardware::Return<void> getCameraInfo_1_1(getCameraInfo_1_1_cb _hidl_cb) override;
69     hardware::Return<void> getPhysicalCameraInfo(const hardware::hidl_string& deviceId,
70                                                  getPhysicalCameraInfo_cb _hidl_cb) override;
71     hardware::Return<hardware::automotive::evs::V1_0::EvsResult> doneWithFrame_1_1(
72             const hardware::hidl_vec<hardware::automotive::evs::V1_1::BufferDesc>& buffer) override;
pauseVideoStream()73     hardware::Return<hardware::automotive::evs::V1_0::EvsResult> pauseVideoStream() override {
74         return hardware::automotive::evs::V1_0::EvsResult::UNDERLYING_SERVICE_ERROR;
75     }
resumeVideoStream()76     hardware::Return<hardware::automotive::evs::V1_0::EvsResult> resumeVideoStream() override {
77         return hardware::automotive::evs::V1_0::EvsResult::UNDERLYING_SERVICE_ERROR;
78     }
79     hardware::Return<hardware::automotive::evs::V1_0::EvsResult> setMaster() override;
80     hardware::Return<hardware::automotive::evs::V1_0::EvsResult> forceMaster(
81             const sp<hardware::automotive::evs::V1_0::IEvsDisplay>& display) override;
82     hardware::Return<hardware::automotive::evs::V1_0::EvsResult> unsetMaster() override;
83     hardware::Return<void> getParameterList(getParameterList_cb _hidl_cb) override;
84     hardware::Return<void> getIntParameterRange(hardware::automotive::evs::V1_1::CameraParam id,
85                                                 getIntParameterRange_cb _hidl_cb) override;
86     hardware::Return<void> setIntParameter(hardware::automotive::evs::V1_1::CameraParam id,
87                                            int32_t value, setIntParameter_cb _hidl_cb) override;
88     hardware::Return<void> getIntParameter(hardware::automotive::evs::V1_1::CameraParam id,
89                                            getIntParameter_cb _hidl_cb) override;
90     hardware::Return<hardware::automotive::evs::V1_0::EvsResult> setExtendedInfo_1_1(
91             uint32_t opaqueIdentifier, const hardware::hidl_vec<uint8_t>& opaqueValue) override;
92     hardware::Return<void> getExtendedInfo_1_1(uint32_t opaqueIdentifier,
93                                                getExtendedInfo_1_1_cb _hidl_cb) override;
94     hardware::Return<void> importExternalBuffers(
95             const hardware::hidl_vec<hardware::automotive::evs::V1_1::BufferDesc>& buffers,
96             importExternalBuffers_cb _hidl_cb) override;
97 
98     // Dump current status to a given file descriptor
99     std::string toString(const char* indent = "") const;
100 
101 private:
102     void shutdown();
103 
104     // The low level camera interface that backs this proxy
105     // Use by camera thread, once constructed, do not update/change it
106     // until camera thread quits.
107     std::unordered_map<std::string, wp<HalCamera>> mHalCamera;
108 
109     sp<hardware::automotive::evs::V1_0::IEvsCameraStream> mStream;
110     sp<hardware::automotive::evs::V1_1::IEvsCameraStream> mStream_1_1;
111 
112     unsigned mFramesAllowed = 1;
113 
114     // Access by camera thread also, use with cautions for race conditions.
115     enum {
116         STOPPED,
117         RUNNING,
118         STOPPING,
119     } mStreamState;
120     mutable std::recursive_mutex mFramesHeldMutex;
121     std::unordered_map<std::string, std::deque<hardware::automotive::evs::V1_1::BufferDesc>>
122             mFramesHeld;
123     hardware::automotive::evs::V1_1::CameraDesc* mDesc;
124     std::thread mCaptureThread;
125 
126     mutable std::mutex mFrameDeliveryMutex;
127     std::condition_variable mFramesReadySignal;
128     std::set<std::string> mSourceCameras GUARDED_BY(mFrameDeliveryMutex);
129 };
130 
131 }  // namespace android::automotive::evs::V1_1::implementation
132 
133 #endif  // CPP_EVS_MANAGER_1_1_VIRTUALCAMERA_H_
134