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