• 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 
19 #include "v4l2_dev.h"
20 #include <sys/prctl.h>
21 
22 namespace OHOS::Camera {
23 std::map<std::string, std::string> HosV4L2Dev::deviceMatch = HosV4L2Dev::CreateDevMap();
24 std::map<std::string, int> HosV4L2Dev::fdMatch = HosV4L2Dev::CreateFdMap();
25 std::mutex HosV4L2Dev::deviceFdLock_ = {};
26 
27 static constexpr uint32_t WATING_TIME = 1000 * 100;
28 
HosV4L2Dev()29 HosV4L2Dev::HosV4L2Dev() {}
~HosV4L2Dev()30 HosV4L2Dev::~HosV4L2Dev() {}
31 
GetCurrentFd(const std::string & cameraID)32 int HosV4L2Dev::GetCurrentFd(const std::string& cameraID)
33 {
34     auto itr = HosV4L2Dev::fdMatch.find(cameraID);
35     if (itr == HosV4L2Dev::fdMatch.end()) {
36         CAMERA_LOGE("error: GetCurrentFd no camera fd\n");
37         return RCERRORFD;
38     }
39 
40     return itr->second;
41 }
42 
start(const std::string & cameraID)43 RetCode HosV4L2Dev::start(const std::string& cameraID)
44 {
45     std::string devName;
46     int fd;
47 
48     CAMERA_LOGD("HosV4L2Dev::start enter %{public}s\n", cameraID.c_str());
49 
50     if (myFileFormat_ == nullptr) {
51         myFileFormat_ = std::make_shared<HosFileFormat>();
52         if (myFileFormat_ == nullptr) {
53             CAMERA_LOGE("error: InitMatch: myFileFormat_ make_shared is NULL\n");
54             return RC_ERROR;
55         }
56     }
57 
58     auto itr = HosV4L2Dev::deviceMatch.find(cameraID);
59     if (itr == HosV4L2Dev::deviceMatch.end()) {
60         CAMERA_LOGE("error:find V4L2 devname fail\n");
61         return RC_ERROR;
62     }
63     devName = itr->second;
64 
65     fd = myFileFormat_->V4L2OpenDevice(devName);
66     if (fd < 0) {
67         CAMERA_LOGE("error:myFileFormat_->V4L2OpenDevice fail fd == %d\n", fd);
68         return RC_ERROR;
69     }
70 
71     bufferType_ = static_cast<enum v4l2_buf_type>(myFileFormat_->V4L2SearchBufType(fd));
72     if (bufferType_ == V4L2_BUF_TYPE_PRIVATE) {
73         CAMERA_LOGE("error:myFileFormat_->V4L2SearchBufType bufferType_ == 0\n");
74         return RC_ERROR;
75     }
76 
77     std::lock_guard<std::mutex> l(HosV4L2Dev::deviceFdLock_);
78     HosV4L2Dev::fdMatch.insert(std::make_pair(cameraID, fd));
79 
80     return RC_OK;
81 }
82 
stop(const std::string & cameraID)83 RetCode HosV4L2Dev::stop(const std::string& cameraID)
84 {
85     int fd;
86 
87     CAMERA_LOGD("HosV4L2Dev::stop enter %{public}s\n", cameraID.c_str());
88 
89     if (myFileFormat_ == nullptr) {
90         CAMERA_LOGE("HosV4L2Dev::stop myFileFormat_ == nullptr\n");
91         return RC_ERROR;
92     }
93 
94     auto itr = HosV4L2Dev::fdMatch.find(cameraID);
95     if (itr == HosV4L2Dev::fdMatch.end()) {
96         CAMERA_LOGE("HosV4L2Dev::stop GetCurrentFd error\n");
97         return RC_ERROR;
98     }
99 
100     fd = itr->second;
101     if (fd < 0) {
102         CAMERA_LOGE("HosV4L2Dev::stop fd error = %d\n", fd);
103         return RC_ERROR;
104     }
105 
106     myFileFormat_->V4L2CloseDevice(fd);
107 
108     std::lock_guard<std::mutex> l(HosV4L2Dev::deviceFdLock_);
109     HosV4L2Dev::fdMatch.erase(itr);
110 
111     return RC_OK;
112 }
113 
Init(std::vector<std::string> & cameraIDs)114 RetCode HosV4L2Dev::Init(std::vector<std::string>& cameraIDs)
115 {
116     auto myFileFormat = std::make_shared<HosFileFormat>();
117     if (myFileFormat == nullptr) {
118         CAMERA_LOGE("error: InitMatch: myFileFormat_ make_shared is NULL\n");
119         return RC_ERROR;
120     }
121 
122     myFileFormat->V4L2MatchDevice(cameraIDs);
123 
124     return RC_OK;
125 }
126 
ReqBuffers(const std::string & cameraID,unsigned int buffCont,enum v4l2_memory memoryType)127 RetCode HosV4L2Dev::ReqBuffers(const std::string& cameraID, unsigned int buffCont, enum v4l2_memory memoryType)
128 {
129     int rc, fd;
130 
131     fd = GetCurrentFd(cameraID);
132     if (fd < 0) {
133         CAMERA_LOGE("error: ReqBuffers: GetCurrentFd error\n");
134         return RC_ERROR;
135     }
136 
137     if (myBuffers_ == nullptr) {
138         myBuffers_ = std::make_shared<HosV4L2Buffers>(memoryType, bufferType_);
139         if (myBuffers_ == nullptr) {
140             CAMERA_LOGE("error: Creatbuffer: myBuffers_ make_shared is NULL\n");
141             return RC_ERROR;
142         }
143     }
144 
145     rc = myBuffers_->V4L2ReqBuffers(fd, buffCont);
146     if (rc == RC_ERROR) {
147         CAMERA_LOGE("error: Creatbuffer: V4L2ReqBuffers error\n");
148         return RC_ERROR;
149     }
150 
151     return RC_OK;
152 }
153 
CreatBuffer(const std::string & cameraID,const std::shared_ptr<FrameSpec> & frameSpec)154 RetCode HosV4L2Dev::CreatBuffer(const std::string& cameraID, const std::shared_ptr<FrameSpec>& frameSpec)
155 {
156     int rc, fd;
157 
158     fd = GetCurrentFd(cameraID);
159     if (fd < 0) {
160         CAMERA_LOGE("error: ReqBuffers: GetCurrentFd error\n");
161         return RC_ERROR;
162     }
163 
164     if (frameSpec == nullptr || myBuffers_ == nullptr) {
165         CAMERA_LOGE("error: Creatbuffer frameSpec or myBuffers_ is NULL\n");
166         return RC_ERROR;
167     }
168 
169     CAMERA_LOGD("Creatbuffer frameSpec->buffer index == %d\n", frameSpec->buffer_->GetIndex());
170 
171     rc = myBuffers_->V4L2AllocBuffer(fd, frameSpec);
172     if (rc == RC_ERROR) {
173         CAMERA_LOGE("error: Creatbuffer: V4L2AllocBuffer error\n");
174         return RC_ERROR;
175     }
176 
177     rc = myBuffers_->V4L2QueueBuffer(fd, frameSpec);
178     if (rc == RC_ERROR) {
179         CAMERA_LOGE("error: Creatbuffer: V4L2QueueBuffer error\n");
180         return RC_ERROR;
181     }
182 
183     return RC_OK;
184 }
185 
QueueBuffer(const std::string & cameraID,const std::shared_ptr<FrameSpec> & frameSpec)186 RetCode HosV4L2Dev::QueueBuffer(const std::string& cameraID, const std::shared_ptr<FrameSpec>& frameSpec)
187 {
188     int rc, fd;
189 
190     fd = GetCurrentFd(cameraID);
191     if (fd < 0) {
192         CAMERA_LOGE("QueueBuffer: GetCurrentFd error\n");
193         return RC_ERROR;
194     }
195 
196     if (frameSpec == nullptr || myBuffers_ == nullptr) {
197         CAMERA_LOGE(" QueueBuffer frameSpec or myBuffers_ is NULL\n");
198         return RC_ERROR;
199     }
200 
201     rc = myBuffers_->V4L2QueueBuffer(fd, frameSpec);
202     if (rc == RC_ERROR) {
203         CAMERA_LOGE("QueueBuffer: V4L2QueueBuffer error\n");
204         return RC_ERROR;
205     }
206 
207     return RC_OK;
208 }
209 
ReleaseBuffers(const std::string & cameraID)210 RetCode HosV4L2Dev::ReleaseBuffers(const std::string& cameraID)
211 {
212     int fd;
213     int rc = 0;
214 
215     if (myBuffers_ == nullptr) {
216         CAMERA_LOGE("ReleaseBuffers myBuffers_ is NULL\n");
217         return RC_ERROR;
218     }
219 
220     fd = GetCurrentFd(cameraID);
221     if (fd < 0) {
222         CAMERA_LOGE("ReleaseBuffers: GetCurrentFd error\n");
223         return RC_ERROR;
224     }
225 
226     rc = myBuffers_->V4L2ReleaseBuffers(fd);
227     if (rc == RC_ERROR) {
228         CAMERA_LOGE("ReleaseBuffers: V4L2ReleaseBuffers error\n");
229         return RC_ERROR;
230     }
231 
232     return RC_OK;
233 }
234 
loopBuffers()235 void HosV4L2Dev::loopBuffers()
236 {
237     int nfds, rc;
238     struct epoll_event events[MAXSTREAMCOUNT];
239 
240     CAMERA_LOGD("!!! loopBuffers enter\n");
241     prctl(PR_SET_NAME, "v4l2_loopbuffer");
242 
243     while (streamNumber_ > 0) {
244         nfds = epoll_wait(epollFd_, events, MAXSTREAMCOUNT, -1);
245         CAMERA_LOGD("loopBuffers: epoll_wait rc = %d streamNumber_ == %d\n", nfds, streamNumber_);
246 
247         for (int n = 0; nfds > 0; ++n, --nfds) {
248             if ((events[n].events & EPOLLIN) && (events[n].data.fd != eventFd_)) {
249                 CHECK_IF_PTR_NULL_RETURN_VOID(myBuffers_);
250                 rc = myBuffers_->V4L2DequeueBuffer(events[n].data.fd);
251                 CAMERA_LOGD("loopBuffers: myBuffers_->V4L2DequeueBuffer return error == %d\n", rc);
252             } else {
253                 CAMERA_LOGD("loopBuffers: epoll invalid events = 0x%x or eventFd exit = %d\n",
254                     events[n].events, (events[n].data.fd == eventFd_));
255                 usleep(WATING_TIME);
256             }
257         }
258     }
259     CAMERA_LOGD("!!! loopBuffers exit\n");
260 }
261 
CreateEpoll(int fd,const unsigned int streamNumber)262 RetCode HosV4L2Dev::CreateEpoll(int fd, const unsigned int streamNumber)
263 {
264     struct epoll_event epollevent = {};
265 
266     if (streamNumber == 0) {
267         epollFd_ = epoll_create(MAXSTREAMCOUNT);
268         if (epollFd_ < 0) {
269             CAMERA_LOGE("V4L2 StartStream create_epoll failed\n");
270             return RC_ERROR;
271         }
272         epollevent.events = EPOLLIN;
273         epollevent.data.fd = fd;
274         epoll_ctl(epollFd_, EPOLL_CTL_ADD, fd, &epollevent);
275 
276         std::lock_guard<std::mutex> l(epollLock_);
277         epollEvent_.push_back(epollevent);
278 
279         eventFd_ = eventfd(0, 0);
280         epollevent = {};
281         epollevent.events = EPOLLIN;
282         epollevent.data.fd = eventFd_;
283         epoll_ctl(epollFd_, EPOLL_CTL_ADD, eventFd_, &epollevent);
284     } else {
285         epollevent.events = EPOLLIN;
286         epollevent.data.fd = fd;
287 
288         std::lock_guard<std::mutex> l(epollLock_);
289         epollEvent_.push_back(epollevent);
290 
291         epoll_ctl(epollFd_, EPOLL_CTL_ADD, fd, &epollevent);
292     }
293     return RC_OK;
294 }
295 
EraseEpoll(int fd)296 void HosV4L2Dev::EraseEpoll(int fd)
297 {
298     auto itr = std::find_if(epollEvent_.begin(), epollEvent_.end(), [fd](const epoll_event& event) {
299         if (event.data.fd == fd) {
300             return true;
301         } else {
302             return false;
303         }
304     });
305     if (itr != epollEvent_.end()) {
306         struct epoll_event event = *itr;
307         epoll_ctl(epollFd_, EPOLL_CTL_DEL, fd, &event);
308         std::lock_guard<std::mutex> l(epollLock_);
309         epollEvent_.erase(itr);
310     }
311 }
312 
StartStream(const std::string & cameraID)313 RetCode HosV4L2Dev::StartStream(const std::string& cameraID)
314 {
315     int rc, fd;
316 
317     fd = GetCurrentFd(cameraID);
318     if (fd < 0) {
319         CAMERA_LOGE("error: ReqBuffers: GetCurrentFd error\n");
320         return RC_ERROR;
321     }
322 
323     if (myStreams_ == nullptr) {
324         myStreams_ = std::make_shared<HosV4L2Streams>(bufferType_);
325         if (myStreams_ == nullptr) {
326             CAMERA_LOGE("error: StartStream: myStreams_ make_shared is NULL\n");
327             return RC_ERROR;
328         }
329     }
330 
331     rc = myStreams_->V4L2StreamOn(fd);
332     if (rc == RC_ERROR) {
333         CAMERA_LOGE("error: StartStream: V4L2StreamOn error\n");
334         return RC_ERROR;
335     }
336 
337     rc = CreateEpoll(fd, streamNumber_);
338     if (rc == RC_ERROR) {
339         CAMERA_LOGE("StartStream: CreateEpoll error\n");
340         return RC_ERROR;
341     }
342 
343     if (streamNumber_ == 0) {
344         streamThread_ = new (std::nothrow) std::thread(&HosV4L2Dev::loopBuffers, this);
345         if (streamThread_ == nullptr) {
346             CAMERA_LOGE("V4L2 StartStream start thread failed\n");
347             return RC_ERROR;
348         }
349     }
350 
351     streamNumber_++;
352 
353     return RC_OK;
354 }
355 
StopStream(const std::string & cameraID)356 RetCode HosV4L2Dev::StopStream(const std::string& cameraID)
357 {
358     int rc, fd;
359 
360     if (myStreams_ == nullptr) {
361         CAMERA_LOGE("error: StopStream: myStreams_ is NULL\n");
362         return RC_ERROR;
363     }
364 
365     if (streamThread_ == nullptr) {
366         CAMERA_LOGE("StopStream thread is stopped\n");
367         return RC_ERROR;
368     }
369 
370     streamNumber_ -= 1;
371     CAMERA_LOGD("HosV4L2Dev::StopStream streamNumber_ = %d\n", streamNumber_);
372 
373     if (streamNumber_ == 0) {
374         CAMERA_LOGD("waiting loopBuffers stop\n");
375         uint64_t one = 1;
376         write(eventFd_, &one, sizeof(one));
377         streamThread_->join();
378         close(eventFd_);
379     }
380 
381     fd = GetCurrentFd(cameraID);
382     if (fd < 0) {
383         CAMERA_LOGE("error: ReqBuffers: GetCurrentFd error\n");
384         return RC_ERROR;
385     }
386 
387     rc = myStreams_->V4L2StreamOff(fd);
388     if (rc == RC_ERROR) {
389         CAMERA_LOGE("error: StartStream: V4L2StreamOn error\n");
390         return RC_ERROR;
391     }
392 
393     EraseEpoll(fd);
394 
395     if (streamNumber_ == 0) {
396         close(epollFd_);
397         delete streamThread_;
398         streamThread_ = nullptr;
399     }
400 
401     return RC_OK;
402 }
403 
UpdateSetting(const std::string & cameraID,AdapterCmd command,const int * args)404 RetCode HosV4L2Dev::UpdateSetting(const std::string& cameraID, AdapterCmd command, const int* args)
405 {
406     int32_t fd;
407     int rc = 0;
408 
409     if (args == nullptr) {
410         CAMERA_LOGE("HosV4L2Dev::UpdateSetting: args is NULL\n");
411         return RC_ERROR;
412     }
413 
414     if (myControl_ == nullptr) {
415         myControl_ = std::make_shared<HosV4L2Control>();
416         if (myControl_ == nullptr) {
417             CAMERA_LOGE("HosV4L2Dev::UpdateSetting: myControl_ make_shared is NULL\n");
418             return RC_ERROR;
419         }
420     }
421 
422     fd = GetCurrentFd(cameraID);
423     if (fd < 0) {
424         CAMERA_LOGE("UpdateSetting: GetCurrentFd error\n");
425         return RC_ERROR;
426     }
427 
428     switch (command) {
429         case CMD_AE_EXPO:
430             rc = myControl_->V4L2SetCtrl(fd, V4L2_CID_EXPOSURE_AUTO, *(int32_t*)args);
431             break;
432 
433         case CMD_AE_EXPOTIME:
434             rc = myControl_->V4L2SetCtrl(fd, V4L2_CID_EXPOSURE_ABSOLUTE, *(int32_t*)args);
435             break;
436 
437         case CMD_AWB_MODE:
438             rc = myControl_->V4L2SetCtrl(fd, V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE, *(int32_t*)args);
439             break;
440 
441         default:
442             break;
443     }
444 
445     if (rc != RC_OK) {
446         return RC_ERROR;
447     }
448 
449     return RC_OK;
450 }
451 
QuerySetting(const std::string & cameraID,AdapterCmd command,int * args)452 RetCode HosV4L2Dev::QuerySetting(const std::string& cameraID, AdapterCmd command, int* args)
453 {
454     int32_t fd;
455     int32_t value = 0;
456     int rc = 0;
457 
458     if (args == nullptr) {
459         CAMERA_LOGE("HosV4L2Dev::QuerySetting: args is NULL\n");
460         return RC_ERROR;
461     }
462 
463     if (myControl_ == nullptr) {
464         myControl_ = std::make_shared<HosV4L2Control>();
465         if (myControl_ == nullptr) {
466             CAMERA_LOGE("HosV4L2Dev::QuerySetting: myControl_ make_shared is NULL\n");
467             return RC_ERROR;
468         }
469     }
470 
471     fd = GetCurrentFd(cameraID);
472     if (fd < 0) {
473         CAMERA_LOGE("QuerySetting: GetCurrentFd error\n");
474         return RC_ERROR;
475     }
476 
477     switch (command) {
478         case CMD_AE_EXPO:
479             rc = myControl_->V4L2GetCtrl(fd, V4L2_CID_EXPOSURE_AUTO, value);
480             break;
481 
482         case CMD_AE_EXPOTIME:
483             rc = myControl_->V4L2GetCtrl(fd, V4L2_CID_EXPOSURE_ABSOLUTE, value);
484             break;
485 
486         case CMD_AWB_MODE:
487             rc = myControl_->V4L2GetCtrl(fd, V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE, value);
488             break;
489 
490         default:
491             break;
492     }
493 
494     if (rc != RC_OK) {
495         return RC_ERROR;
496     }
497 
498     *(int32_t*)args = value;
499 
500     return RC_OK;
501 }
502 
GetNumberCtrls(const std::string & cameraID,std::vector<DeviceControl> & control)503 RetCode HosV4L2Dev::GetNumberCtrls(const std::string& cameraID, std::vector<DeviceControl>& control)
504 {
505     int32_t fd;
506 
507     if (myControl_ == nullptr) {
508         myControl_ = std::make_shared<HosV4L2Control>();
509         if (myControl_ == nullptr) {
510             CAMERA_LOGE("HosV4L2Dev::GetNumberCtrls: myControl_ make_shared is NULL\n");
511             return RC_ERROR;
512         }
513     }
514 
515     fd = GetCurrentFd(cameraID);
516     if (fd < 0) {
517         CAMERA_LOGE("GetNumberCtrls: GetCurrentFd error\n");
518         return RC_ERROR;
519     }
520 
521     return myControl_->V4L2GetCtrls(fd, control, control.size());
522 }
523 
SetNumberCtrls(const std::string & cameraID,std::vector<DeviceControl> & control)524 RetCode HosV4L2Dev::SetNumberCtrls(const std::string& cameraID, std::vector<DeviceControl>& control)
525 {
526     int32_t fd;
527 
528     if (myControl_ == nullptr) {
529         myControl_ = std::make_shared<HosV4L2Control>();
530         if (myControl_ == nullptr) {
531             CAMERA_LOGE("HosV4L2Dev::SetNumberCtrls: myControl_ make_shared is NULL\n");
532             return RC_ERROR;
533         }
534     }
535 
536     fd = GetCurrentFd(cameraID);
537     if (fd < 0) {
538         CAMERA_LOGE("SetNumberCtrls: GetCurrentFd error\n");
539         return RC_ERROR;
540     }
541 
542     return myControl_->V4L2SetCtrls(fd, control, control.size());
543 }
544 
GetControls(const std::string & cameraID,std::vector<DeviceControl> & control)545 RetCode HosV4L2Dev::GetControls(const std::string& cameraID, std::vector<DeviceControl>& control)
546 {
547     int fd, rc;
548 
549     if (myControl_ == nullptr) {
550         myControl_ = std::make_shared<HosV4L2Control>();
551         if (myControl_ == nullptr) {
552             CAMERA_LOGE("HosV4L2Dev::GetControls: myControl_ make_shared is NULL\n");
553             return RC_ERROR;
554         }
555     }
556 
557     fd = GetCurrentFd(cameraID);
558     if (fd < 0) {
559         CAMERA_LOGE("GetControls: GetCurrentFd error\n");
560         return RC_ERROR;
561     }
562 
563     rc = myControl_->V4L2GetControls(fd, control);
564     if (rc == RC_ERROR) {
565         CAMERA_LOGE("myControl_->V4L2GetControls fail\n");
566         return RC_ERROR;
567     }
568 
569     return RC_OK;
570 }
571 
GetFmtDescs(const std::string & cameraID,std::vector<DeviceFormat> & fmtDesc)572 RetCode HosV4L2Dev::GetFmtDescs(const std::string& cameraID, std::vector<DeviceFormat>& fmtDesc)
573 {
574     int fd, rc;
575 
576     if (myFileFormat_ == nullptr) {
577         CAMERA_LOGE("GetFmtDescs: myFileFormat_ == nullptr\n");
578         return RC_ERROR;
579     }
580 
581     fd = GetCurrentFd(cameraID);
582     if (fd < 0) {
583         CAMERA_LOGE("GetFmtDescs: GetCurrentFd error\n");
584         return RC_ERROR;
585     }
586 
587     rc = myFileFormat_->V4L2GetFmtDescs(fd, fmtDesc);
588     if (rc == RC_ERROR) {
589         CAMERA_LOGE("myFileFormat_->V4L2GetFmtDescs fail\n");
590         return RC_ERROR;
591     }
592 
593     return RC_OK;
594 }
595 
ConfigFps(const int fd,DeviceFormat & format,V4l2FmtCmd command)596 RetCode HosV4L2Dev::ConfigFps(const int fd, DeviceFormat& format, V4l2FmtCmd command)
597 {
598     RetCode rc = RC_OK;
599 
600     if (myStreams_ == nullptr) {
601         myStreams_ = std::make_shared<HosV4L2Streams>(bufferType_);
602         if (myStreams_ == nullptr) {
603             CAMERA_LOGE("error: ConfigSys: myStreams_ make_shared is nullptr\n");
604             return RC_ERROR;
605         }
606     }
607 
608     if (command == CMD_V4L2_SET_FPS) {
609         rc = myStreams_->V4L2StreamFPSSet(fd, format);
610     } else {
611         rc = myStreams_->V4L2StreamFPSGet(fd, format);
612     }
613 
614     if (rc != RC_OK) {
615         CAMERA_LOGE("ConfigFps CMD %d fail\n", command);
616     }
617 
618     return rc;
619 }
620 
ConfigSys(const std::string & cameraID,V4l2FmtCmd command,DeviceFormat & format)621 RetCode HosV4L2Dev::ConfigSys(const std::string& cameraID, V4l2FmtCmd command, DeviceFormat& format)
622 {
623     int fd;
624     RetCode rc = RC_OK;
625 
626     if (myFileFormat_ == nullptr) {
627         CAMERA_LOGE("GetFmtDescs: ConfigSys == nullptr\n");
628         return RC_ERROR;
629     }
630 
631     fd = GetCurrentFd(cameraID);
632     if (fd < 0) {
633         CAMERA_LOGE("ConfigSys: GetCurrentFd error\n");
634         return RC_ERROR;
635     }
636 
637     switch (command) {
638         case CMD_V4L2_GET_FORMAT:
639             rc = myFileFormat_->V4L2GetFmt(fd, format);
640             break;
641 
642         case CMD_V4L2_SET_FORMAT:
643             rc = myFileFormat_->V4L2SetFmt(fd, format);
644             break;
645 
646         case CMD_V4L2_GET_CROPCAP:
647             rc = myFileFormat_->V4L2GetCropCap(fd, format);
648             break;
649 
650         case CMD_V4L2_GET_CROP:
651             rc = myFileFormat_->V4L2GetCrop(fd, format);
652             break;
653 
654         case CMD_V4L2_SET_CROP:
655             rc = myFileFormat_->V4L2SetCrop(fd, format);
656             break;
657 
658         case CMD_V4L2_SET_FPS:
659         case CMD_V4L2_GET_FPS:
660             rc = ConfigFps(fd, format, command);
661             break;
662 
663         default:
664             CAMERA_LOGE("HosV4L2Dev::ConfigSys unknow command\n");
665             break;
666     }
667 
668     if (rc != RC_OK) {
669         CAMERA_LOGE("ConfigSys CMD %d fail\n", command);
670     }
671 
672     return rc;
673 }
674 
SetCallback(BufCallback cb)675 RetCode HosV4L2Dev::SetCallback(BufCallback cb)
676 {
677     if (cb == nullptr) {
678         CAMERA_LOGE("HosV4L2Dev::SetCallback is null");
679         return RC_ERROR;
680     }
681     if (myBuffers_ == nullptr) {
682         CAMERA_LOGE("SetCallback myBuffers_ is NULL\n");
683         return RC_ERROR;
684     }
685 
686     myBuffers_->SetCallback(cb);
687 
688     return RC_OK;
689 }
Flush(const std::string & cameraID)690 RetCode HosV4L2Dev::Flush(const std::string& cameraID)
691 {
692     int rc, fd;
693 
694     fd = GetCurrentFd(cameraID);
695     if (fd < 0) {
696         CAMERA_LOGE("HosV4L2Dev::Flush: GetCurrentFd error\n");
697         return RC_ERROR;
698     }
699 
700     if (myBuffers_ == nullptr) {
701         CAMERA_LOGE(" HosV4L2Dev::Flush myBuffers_ is NULL\n");
702         return RC_ERROR;
703     }
704 
705     rc = myBuffers_->Flush(fd);
706     if (rc == RC_ERROR) {
707         CAMERA_LOGE("HosV4L2Dev::Flush: error\n");
708         return RC_ERROR;
709     }
710 
711     return RC_OK;
712 }
713 
SetMemoryType(uint8_t & memType)714 void HosV4L2Dev::SetMemoryType(uint8_t &memType)
715 {
716     CAMERA_LOGD("func[HosV4L2Dev::%{public}s] memType[%{public}d]", __func__, memType);
717     if (memType == V4L2_MEMORY_MMAP) {
718         memoryType_ = V4L2_MEMORY_MMAP;
719     } else if (memType == V4L2_MEMORY_DMABUF) {
720         memoryType_ = V4L2_MEMORY_DMABUF;
721     }
722 }
723 } // namespace OHOS::Camera
724