1 /* 2 * Copyright 2020 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_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSEMULATEDCAMERA_H 18 #define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSEMULATEDCAMERA_H 19 20 #include "VideoCapture.h" 21 22 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h> 23 #include <android/hardware/automotive/evs/1.1/IEvsCameraStream.h> 24 #include <android/hardware/automotive/evs/1.1/IEvsDisplay.h> 25 #include <android/hardware/automotive/evs/1.1/types.h> 26 #include <android/hardware/camera/device/3.2/ICameraDevice.h> 27 #include <ui/GraphicBuffer.h> 28 29 #include <functional> 30 #include <unordered_map> 31 32 namespace android { 33 namespace automotive { 34 namespace evs { 35 namespace V1_1 { 36 namespace implementation { 37 38 using ::android::hardware::hidl_string; 39 using ::android::hardware::hidl_vec; 40 using ::android::hardware::Return; 41 using ::android::hardware::automotive::evs::V1_0::EvsResult; 42 using ::android::hardware::automotive::evs::V1_1::CameraDesc; 43 using ::android::hardware::automotive::evs::V1_1::CameraParam; 44 using ::android::hardware::camera::device::V3_2::Stream; 45 46 using IEvsDisplay_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsDisplay; 47 using IEvsDisplay_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsDisplay; 48 using BufferDesc_1_0 = ::android::hardware::automotive::evs::V1_0::BufferDesc; 49 using BufferDesc_1_1 = ::android::hardware::automotive::evs::V1_1::BufferDesc; 50 using IEvsCamera_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsCamera; 51 using IEvsCameraStream_1_0 = ::android::hardware::automotive::evs::V1_0::IEvsCameraStream; 52 using IEvsCameraStream_1_1 = ::android::hardware::automotive::evs::V1_1::IEvsCameraStream; 53 54 struct EmulatedCameraDesc { 55 int width; // Camera output width in pixels 56 int height; // Camera output height in pixels 57 std::string path; // Path to the directory where source files are stored 58 std::chrono::nanoseconds interval; // Interval between two consecutive frames; this is 59 // a reciprocal of the framerate 60 }; 61 62 class EvsEmulatedCamera : public IEvsCamera_1_1 { 63 public: 64 // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow. 65 Return<void> getCameraInfo(getCameraInfo_cb _hidl_cb) override; 66 Return<EvsResult> setMaxFramesInFlight(uint32_t bufferCount) override; 67 Return<EvsResult> startVideoStream(const ::android::sp<IEvsCameraStream_1_0>& stream) override; 68 Return<void> doneWithFrame(const BufferDesc_1_0& buffer) override; 69 Return<void> stopVideoStream() override; 70 Return<int32_t> getExtendedInfo(uint32_t opaqueIdentifier) override; 71 Return<EvsResult> setExtendedInfo(uint32_t opaqueIdentifier, int32_t opaqueValue) override; 72 73 // Methods from ::android::hardware::automotive::evs::V1_1::IEvsCamera follow. 74 Return<void> getCameraInfo_1_1(getCameraInfo_1_1_cb _hidl_cb) override; 75 Return<void> getPhysicalCameraInfo(const hidl_string& deviceId, 76 getPhysicalCameraInfo_cb _hidl_cb) override; 77 Return<EvsResult> pauseVideoStream() override; 78 Return<EvsResult> resumeVideoStream() override; 79 Return<EvsResult> doneWithFrame_1_1(const hidl_vec<BufferDesc_1_1>& buffer) override; 80 Return<EvsResult> setMaster() override; 81 Return<EvsResult> forceMaster(const sp<IEvsDisplay_1_0>&) override; 82 Return<EvsResult> unsetMaster() override; 83 Return<void> getParameterList(getParameterList_cb _hidl_cb) override; 84 Return<void> getIntParameterRange(CameraParam id, getIntParameterRange_cb _hidl_cb) override; 85 Return<void> setIntParameter(CameraParam id, int32_t value, 86 setIntParameter_cb _hidl_cb) override; 87 Return<void> getIntParameter(CameraParam id, getIntParameter_cb _hidl_cb) override; 88 Return<EvsResult> setExtendedInfo_1_1(uint32_t opaqueIdentifier, 89 const hidl_vec<uint8_t>& opaqueValue) override; 90 Return<void> getExtendedInfo_1_1(uint32_t opaqueIdentifier, 91 getExtendedInfo_1_1_cb _hidl_cb) override; 92 Return<void> importExternalBuffers(const hidl_vec<BufferDesc_1_1>& buffers, 93 importExternalBuffers_cb _hidl_cb) override; 94 95 static sp<EvsEmulatedCamera> Create(const char* deviceName, const EmulatedCameraDesc& desc); 96 EvsEmulatedCamera(const EvsEmulatedCamera&) = delete; 97 EvsEmulatedCamera& operator=(const EvsEmulatedCamera&) = delete; 98 99 virtual ~EvsEmulatedCamera() override; 100 void shutdown(); 101 getDesc()102 const CameraDesc& getDesc() { return mDescription; }; 103 104 bool openDevice(); 105 106 private: 107 // Constructors 108 EvsEmulatedCamera(const char* deviceName, const EmulatedCameraDesc& desc); 109 110 // These three functions are expected to be called while mAccessLock is held 111 bool setAvailableFrames_Locked(unsigned bufferCount); 112 unsigned increaseAvailableFrames_Locked(unsigned numToAdd); 113 unsigned decreaseAvailableFrames_Locked(unsigned numToRemove); 114 115 void forwardFrame(imageBufferDesc* tgt, void* data); 116 117 sp<IEvsCameraStream_1_1> mStream = nullptr; // The callback used to deliver each frame 118 119 sp<VideoCapture> mVideo; // Virtual video device 120 CameraDesc mDescription; // The properties of this camera 121 122 uint32_t mFormat = 0; // Values from android_pixel_format_t 123 uint32_t mUsage = 0; // Values from from Gralloc.h 124 uint32_t mStride = 0; // Number of bytes from one row of pixels in memory 125 // to the next 126 127 struct BufferRecord { 128 buffer_handle_t handle; 129 bool inUse; 130 BufferRecordBufferRecord131 explicit BufferRecord(buffer_handle_t h) : handle(h), inUse(false) {}; 132 }; 133 134 std::vector<BufferRecord> mBuffers; // Graphics buffers to transfer images 135 unsigned mFramesAllowed; // How many buffers are we currently using 136 unsigned mFramesInUse; // How many buffers are currently outstanding 137 138 EvsResult doneWithFrame_impl(const uint32_t id, const buffer_handle_t handle); 139 140 // Synchronization necessary to deconflict the capture thread from the main service thread 141 // Note that the service interface remains single threaded (ie: not reentrant) 142 std::mutex mAccessLock; 143 144 // Extended information 145 std::unordered_map<uint32_t, std::vector<uint8_t>> mExtInfo; 146 147 // Emulated device description 148 EmulatedCameraDesc mCaptureDeviceDesc; 149 }; 150 151 } // namespace implementation 152 } // namespace V1_1 153 } // namespace evs 154 } // namespace automotive 155 } // namespace android 156 157 #endif // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSEMULATEDCAMERA_H 158