1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 */ 15 #ifndef HOS_CAMERA_V4L2_DEV_H 16 #define HOS_CAMERA_V4L2_DEV_H 17 18 #include <map> 19 #include <mutex> 20 #include <thread> 21 #include <vector> 22 #include <sys/epoll.h> 23 #include <sys/ioctl.h> 24 #include <sys/types.h> 25 #include <unistd.h> 26 #include <sys/eventfd.h> 27 #include <linux/videodev2.h> 28 #include "v4l2_buffer.h" 29 #include "v4l2_fileformat.h" 30 #include "v4l2_stream.h" 31 #include "v4l2_control.h" 32 #include "v4l2_common.h" 33 #if defined(V4L2_UTEST) || defined (V4L2_MAIN_TEST) 34 #include "v4l2_temp.h" 35 #else 36 #include <camera.h> 37 #include <stream.h> 38 #endif 39 40 namespace OHOS::Camera { 41 class HosV4L2Dev { 42 public: 43 HosV4L2Dev(); 44 ~HosV4L2Dev(); 45 46 RetCode start(const std::string& cameraID); 47 48 RetCode stop(const std::string& cameraID); 49 50 RetCode SetNumberCtrls (const std::string& cameraID, std::vector<DeviceControl>& control); 51 52 RetCode GetNumberCtrls (const std::string& cameraID, std::vector<DeviceControl>& control); 53 54 RetCode GetFmtDescs(const std::string& cameraID, std::vector<DeviceFormat>& fmtDesc); 55 56 RetCode GetControls(const std::string& cameraID, std::vector<DeviceControl>& control); 57 58 RetCode ConfigSys(const std::string& cameraID, V4l2FmtCmd command, DeviceFormat& format); 59 60 RetCode UpdateSetting(const std::string& cameraID, AdapterCmd command, const int* args); 61 62 RetCode QuerySetting(const std::string& cameraID, unsigned int command, int* args); 63 64 RetCode ReqBuffers(const std::string& cameraID, unsigned int buffCont); 65 66 RetCode CreatBuffer(const std::string& cameraID, const std::shared_ptr<FrameSpec>& frameSpec); 67 68 RetCode StartStream(const std::string& cameraID); 69 70 RetCode QueueBuffer(const std::string& cameraID, const std::shared_ptr<FrameSpec>& frameSpec); 71 72 RetCode ReleaseBuffers(const std::string& cameraID); 73 74 RetCode StopStream(const std::string& cameraID); 75 76 RetCode SetCallback(BufCallback cb); 77 78 RetCode Flush(const std::string& cameraID); 79 80 void SetMemoryType(uint8_t &memType); 81 82 static RetCode Init(std::vector<std::string>& cameraIDs); 83 84 static std::map<std::string, std::string> deviceMatch; 85 static std::map<std::string, int> fdMatch; 86 static std::mutex deviceFdLock_; 87 CreateDevMap()88 static std::map<std::string, std::string> CreateDevMap() 89 { 90 std::map<std::string, std::string> tmp_map; 91 tmp_map.insert(std::pair<std::string, std::string>("INVALID", "INVALID")); 92 return tmp_map; 93 } 94 CreateFdMap()95 static std::map<std::string, int> CreateFdMap() 96 { 97 std::map<std::string, int> tmp_map; 98 tmp_map.insert(std::pair<std::string, int>("INVALID", 0)); 99 return tmp_map; 100 } 101 102 private: 103 int GetCurrentFd(const std::string& cameraID); 104 void loopBuffers(); 105 RetCode CreateEpoll(int fd, const unsigned int streamNumber); 106 void EraseEpoll(int fd); 107 RetCode ConfigFps(const int fd, DeviceFormat& format, V4l2FmtCmd command); 108 int ConvertToDevAwbMode(int awbMode); 109 int ConvertToHosAwbMode(int awbMode); 110 111 int eventFd_ = 0; 112 std::thread* streamThread_ = nullptr; 113 unsigned int streamNumber_ = 0; 114 115 int epollFd_ = 0; 116 std::vector<epoll_event> epollEvent_; 117 std::mutex epollLock_; 118 119 std::shared_ptr<HosV4L2Buffers> myBuffers_ = nullptr; 120 std::shared_ptr<HosV4L2Streams> myStreams_ = nullptr; 121 std::shared_ptr<HosFileFormat> myFileFormat_ = nullptr; 122 std::shared_ptr<HosV4L2Control> myControl_ = nullptr; 123 124 enum v4l2_memory memoryType_ = V4L2_MEMORY_MMAP; 125 enum v4l2_buf_type bufferType_ = V4L2_BUF_TYPE_VIDEO_CAPTURE; 126 }; 127 } // namespace OHOS::Camera 128 #endif // HOS_CAMERA_V4L2_DEV_H 129