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 <linux/videodev2.h> 23 #include <sys/epoll.h> 24 #include <sys/ioctl.h> 25 #include <sys/types.h> 26 #include <unistd.h> 27 #include <sys/eventfd.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, AdapterCmd 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 static RetCode Init(std::vector<std::string>& cameraIDs); 81 82 static std::map<std::string, std::string> deviceMatch; 83 static std::map<std::string, int> fdMatch; 84 static std::mutex deviceFdLock_; 85 CreateDevMap()86 static std::map<std::string, std::string> CreateDevMap() 87 { 88 std::map<std::string, std::string> tmp_map; 89 tmp_map.insert(std::pair<std::string, std::string>("INVALID", "INVALID")); 90 return tmp_map; 91 } 92 CreateFdMap()93 static std::map<std::string, int> CreateFdMap() 94 { 95 std::map<std::string, int> tmp_map; 96 tmp_map.insert(std::pair<std::string, int>("INVALID", 0)); 97 return tmp_map; 98 } 99 100 private: 101 int GetCurrentFd(const std::string& cameraID); 102 void loopBuffers(); 103 RetCode CreateEpoll(int fd, const unsigned int streamNumber); 104 void EraseEpoll(int fd); 105 RetCode ConfigFps(const int fd, DeviceFormat& format, V4l2FmtCmd command); 106 107 int eventFd_ = 0; 108 std::thread* streamThread_ = nullptr; 109 unsigned int streamNumber_ = 0; 110 111 int epollFd_ = 0; 112 std::vector<epoll_event> epollEvent_; 113 std::mutex epollLock_; 114 115 std::shared_ptr<HosV4L2Buffers> myBuffers_ = nullptr; 116 std::shared_ptr<HosV4L2Streams> myStreams_ = nullptr; 117 std::shared_ptr<HosFileFormat> myFileFormat_ = nullptr; 118 std::shared_ptr<HosV4L2Control> myControl_ = nullptr; 119 120 enum v4l2_memory memoryType_ = V4L2_MEMORY_USERPTR; 121 enum v4l2_buf_type bufferType_ = V4L2_BUF_TYPE_PRIVATE; 122 }; 123 } // namespace OHOS::Camera 124 #endif // HOS_CAMERA_V4L2_DEV_H 125