• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd
3  * This file contains confidential and proprietary information of
4  * OSWare Technology Co., Ltd
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef HOS_CAMERA_V4L2_DEV_H
19 #define HOS_CAMERA_V4L2_DEV_H
20 
21 #include <map>
22 #include <mutex>
23 #include <thread>
24 #include <vector>
25 #include <sys/epoll.h>
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <sys/eventfd.h>
30 #include <linux/videodev2.h>
31 #if !defined(V4L2_UTEST) && !defined (V4L2_MAIN_TEST)
32 #include <camera.h>
33 #include <stream.h>
34 #else
35 #include "v4l2_temp.h"
36 #endif
37 #include <v4l2_uvc.h>
38 #include "v4l2_buffer.h"
39 #include "v4l2_fileformat.h"
40 #include "v4l2_stream.h"
41 #include "v4l2_control.h"
42 #include "v4l2_common.h"
43 
44 namespace OHOS::Camera {
45 class HosV4L2Dev {
46 public:
47     HosV4L2Dev();
48     ~HosV4L2Dev();
49 
50     RetCode start(const std::string& cameraID);
51 
52     RetCode stop(const std::string& cameraID);
53 
54     RetCode SetNumberCtrls (const std::string& cameraID, std::vector<DeviceControl>& control);
55 
56     RetCode GetNumberCtrls (const std::string& cameraID, std::vector<DeviceControl>& control);
57 
58     RetCode GetFmtDescs(const std::string& cameraID, std::vector<DeviceFormat>& fmtDesc);
59 
60     RetCode GetControls(const std::string& cameraID, std::vector<DeviceControl>& control);
61 
62     RetCode ConfigSys(const std::string& cameraID, V4l2FmtCmd command, DeviceFormat& format);
63 
64     RetCode UpdateSetting(const std::string& cameraID, AdapterCmd command, const int* args);
65 
66     RetCode QuerySetting(const std::string& cameraID, AdapterCmd command, int* args);
67 
68     RetCode ReqBuffers(const std::string& cameraID, unsigned int buffCont, enum v4l2_memory memoryType);
69 
70     RetCode CreatBuffer(const std::string& cameraID, const std::shared_ptr<FrameSpec>& frameSpec);
71 
72     RetCode StartStream(const std::string& cameraID);
73 
74     RetCode QueueBuffer(const std::string& cameraID, const std::shared_ptr<FrameSpec>& frameSpec);
75 
76     RetCode ReleaseBuffers(const std::string& cameraID);
77 
78     RetCode StopStream(const std::string& cameraID);
79 
80     RetCode SetCallback(BufCallback cb);
81 
82     RetCode Flush(const std::string& cameraID);
83 
84     void SetMemoryType(uint8_t &memType);
85 
86     static RetCode Init(std::vector<std::string>& cameraIDs);
87 
88     static std::map<std::string, std::string> deviceMatch;
89     static std::map<std::string, int> fdMatch;
90     static std::mutex deviceFdLock_;
91 
CreateDevMap(void)92     static std::map<std::string, std::string> CreateDevMap(void)
93     {
94         std::map<std::string, std::string> tmp_map;
95         tmp_map.insert(std::pair<std::string, std::string>("INVALID", "INVALID"));
96         return tmp_map;
97     }
98 
CreateFdMap(void)99     static std::map<std::string, int> CreateFdMap(void)
100     {
101         std::map<std::string, int> tmp_map;
102         tmp_map.insert(std::pair<std::string, int>("INVALID", 0));
103         return tmp_map;
104     }
105 
106 private:
107     int GetCurrentFd(const std::string& cameraID);
108     void loopBuffers(void);
109     RetCode CreateEpoll(int fd, const unsigned int streamNumber);
110     void EraseEpoll(int fd);
111     RetCode ConfigFps(const int fd, DeviceFormat& format, V4l2FmtCmd command);
112 
113     int eventFd_ = 0;
114     std::thread* streamThread_ = nullptr;
115     unsigned int streamNumber_ = 0;
116 
117     int epollFd_ = 0;
118     std::vector<epoll_event> epollEvent_;
119     std::mutex epollLock_;
120 
121     std::shared_ptr<HosV4L2Buffers> myBuffers_ = nullptr;
122     std::shared_ptr<HosV4L2Streams> myStreams_ = nullptr;
123     std::shared_ptr<HosFileFormat> myFileFormat_ = nullptr;
124     std::shared_ptr<HosV4L2Control> myControl_ = nullptr;
125 
126     enum v4l2_memory memoryType_ = V4L2_MEMORY_MMAP;
127     enum v4l2_buf_type bufferType_ = V4L2_BUF_TYPE_PRIVATE;
128 };
129 } // namespace OHOS::Camera
130 #endif // HOS_CAMERA_V4L2_DEV_H
131