• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "v4l2_dev.h"
17 #include <sys/prctl.h>
18 
19 namespace OHOS::Camera {
20 std::map<std::string, std::string> HosV4L2Dev::deviceMatch = HosV4L2Dev::CreateDevMap();
21 std::map<std::string, int> HosV4L2Dev::fdMatch = HosV4L2Dev::CreateFdMap();
22 std::mutex HosV4L2Dev::deviceFdLock_ = {};
23 
24 static constexpr uint32_t WATING_TIME = 1000 * 100;
25 
HosV4L2Dev()26 HosV4L2Dev::HosV4L2Dev() {}
~HosV4L2Dev()27 HosV4L2Dev::~HosV4L2Dev() {}
28 
GetCurrentFd(const std::string & cameraID)29 int HosV4L2Dev::GetCurrentFd(const std::string& cameraID)
30 {
31     auto itr = HosV4L2Dev::fdMatch.find(cameraID);
32     if (itr == HosV4L2Dev::fdMatch.end()) {
33         CAMERA_LOGE("error: GetCurrentFd no camera fd\n");
34         return RCERRORFD;
35     }
36 
37     return itr->second;
38 }
39 
start(const std::string & cameraID)40 RetCode HosV4L2Dev::start(const std::string& cameraID)
41 {
42     std::string devName;
43     int fd;
44 
45     CAMERA_LOGD("HosV4L2Dev::start enter %{public}s\n", cameraID.c_str());
46 
47     if (myFileFormat_ == nullptr) {
48         myFileFormat_ = std::make_shared<HosFileFormat>();
49         if (myFileFormat_ == nullptr) {
50             CAMERA_LOGE("error: InitMatch: myFileFormat_ make_shared is NULL\n");
51             return RC_ERROR;
52         }
53     }
54 
55     auto itr = HosV4L2Dev::deviceMatch.find(cameraID);
56     if (itr == HosV4L2Dev::deviceMatch.end()) {
57         CAMERA_LOGE("error:find V4L2 devname fail\n");
58         return RC_ERROR;
59     }
60     devName = itr->second;
61 
62     fd = myFileFormat_->V4L2OpenDevice(devName);
63     if (fd < 0) {
64         CAMERA_LOGE("error:myFileFormat_->V4L2OpenDevice fail fd == %d\n", fd);
65         return RC_ERROR;
66     }
67 
68     bufferType_ = static_cast<enum v4l2_buf_type>(myFileFormat_->V4L2SearchBufType(fd));
69     if (bufferType_ == V4L2_BUF_TYPE_PRIVATE) {
70         CAMERA_LOGE("error:myFileFormat_->V4L2SearchBufType bufferType_ == 0\n");
71         return RC_ERROR;
72     }
73 
74     std::lock_guard<std::mutex> l(HosV4L2Dev::deviceFdLock_);
75     HosV4L2Dev::fdMatch.insert(std::make_pair(cameraID, fd));
76 
77     return RC_OK;
78 }
79 
stop(const std::string & cameraID)80 RetCode HosV4L2Dev::stop(const std::string& cameraID)
81 {
82     int fd;
83 
84     CAMERA_LOGD("HosV4L2Dev::stop enter %{public}s\n", cameraID.c_str());
85 
86     if (myFileFormat_ == nullptr) {
87         CAMERA_LOGE("HosV4L2Dev::stop myFileFormat_ == nullptr\n");
88         return RC_ERROR;
89     }
90 
91     auto itr = HosV4L2Dev::fdMatch.find(cameraID);
92     if (itr == HosV4L2Dev::fdMatch.end()) {
93         CAMERA_LOGE("HosV4L2Dev::stop GetCurrentFd error\n");
94         return RC_ERROR;
95     }
96 
97     fd = itr->second;
98     if (fd < 0) {
99         CAMERA_LOGE("HosV4L2Dev::stop fd error = %d\n", fd);
100         return RC_ERROR;
101     }
102 
103     myFileFormat_->V4L2CloseDevice(fd);
104 
105     std::lock_guard<std::mutex> l(HosV4L2Dev::deviceFdLock_);
106     HosV4L2Dev::fdMatch.erase(itr);
107 
108     return RC_OK;
109 }
110 
Init(std::vector<std::string> & cameraIDs)111 RetCode HosV4L2Dev::Init(std::vector<std::string>& cameraIDs)
112 {
113     auto myFileFormat = std::make_shared<HosFileFormat>();
114     if (myFileFormat == nullptr) {
115         CAMERA_LOGE("error: InitMatch: myFileFormat_ make_shared is NULL\n");
116         return RC_ERROR;
117     }
118 
119     myFileFormat->V4L2MatchDevice(cameraIDs);
120 
121     return RC_OK;
122 }
123 
ReqBuffers(const std::string & cameraID,unsigned int buffCont)124 RetCode HosV4L2Dev::ReqBuffers(const std::string& cameraID, unsigned int buffCont)
125 {
126     int rc, fd;
127 
128     fd = GetCurrentFd(cameraID);
129     if (fd < 0) {
130         CAMERA_LOGE("error: ReqBuffers: GetCurrentFd error\n");
131         return RC_ERROR;
132     }
133 
134     if (myBuffers_ == nullptr) {
135         myBuffers_ = std::make_shared<HosV4L2Buffers>(memoryType_, bufferType_);
136         if (myBuffers_ == nullptr) {
137             CAMERA_LOGE("error: Creatbuffer: myBuffers_ make_shared is NULL\n");
138             return RC_ERROR;
139         }
140     }
141 
142     rc = myBuffers_->V4L2ReqBuffers(fd, buffCont);
143     if (rc == RC_ERROR) {
144         CAMERA_LOGE("error: Creatbuffer: V4L2ReqBuffers error\n");
145         return RC_ERROR;
146     }
147 
148     return RC_OK;
149 }
150 
CreatBuffer(const std::string & cameraID,const std::shared_ptr<FrameSpec> & frameSpec)151 RetCode HosV4L2Dev::CreatBuffer(const std::string& cameraID, const std::shared_ptr<FrameSpec>& frameSpec)
152 {
153     int rc, fd;
154 
155     fd = GetCurrentFd(cameraID);
156     if (fd < 0) {
157         CAMERA_LOGE("error: ReqBuffers: GetCurrentFd error\n");
158         return RC_ERROR;
159     }
160 
161     if (frameSpec == nullptr || myBuffers_ == nullptr) {
162         CAMERA_LOGE("error: Creatbuffer frameSpec or myBuffers_ is NULL\n");
163         return RC_ERROR;
164     }
165 
166     CAMERA_LOGD("Creatbuffer frameSpec->buffer index == %d\n", frameSpec->buffer_->GetIndex());
167 
168     rc = myBuffers_->V4L2AllocBuffer(fd, frameSpec);
169     if (rc == RC_ERROR) {
170         CAMERA_LOGE("error: Creatbuffer: V4L2AllocBuffer error\n");
171         return RC_ERROR;
172     }
173 
174     rc = myBuffers_->V4L2QueueBuffer(fd, frameSpec);
175     if (rc == RC_ERROR) {
176         CAMERA_LOGE("error: Creatbuffer: V4L2QueueBuffer error\n");
177         return RC_ERROR;
178     }
179 
180     return RC_OK;
181 }
182 
QueueBuffer(const std::string & cameraID,const std::shared_ptr<FrameSpec> & frameSpec)183 RetCode HosV4L2Dev::QueueBuffer(const std::string& cameraID, const std::shared_ptr<FrameSpec>& frameSpec)
184 {
185     int rc, fd;
186 
187     fd = GetCurrentFd(cameraID);
188     if (fd < 0) {
189         CAMERA_LOGE("QueueBuffer: GetCurrentFd error\n");
190         return RC_ERROR;
191     }
192 
193     if (frameSpec == nullptr || myBuffers_ == nullptr) {
194         CAMERA_LOGE(" QueueBuffer frameSpec or myBuffers_ is NULL\n");
195         return RC_ERROR;
196     }
197 
198     rc = myBuffers_->V4L2QueueBuffer(fd, frameSpec);
199     if (rc == RC_ERROR) {
200         CAMERA_LOGE("QueueBuffer: V4L2QueueBuffer error\n");
201         return RC_ERROR;
202     }
203 
204     return RC_OK;
205 }
206 
ReleaseBuffers(const std::string & cameraID)207 RetCode HosV4L2Dev::ReleaseBuffers(const std::string& cameraID)
208 {
209     int fd;
210     int rc = 0;
211 
212     if (myBuffers_ == nullptr) {
213         CAMERA_LOGE("ReleaseBuffers myBuffers_ is NULL\n");
214         return RC_ERROR;
215     }
216 
217     fd = GetCurrentFd(cameraID);
218     if (fd < 0) {
219         CAMERA_LOGE("ReleaseBuffers: GetCurrentFd error\n");
220         return RC_ERROR;
221     }
222 
223     rc = myBuffers_->V4L2ReleaseBuffers(fd);
224     if (rc == RC_ERROR) {
225         CAMERA_LOGE("ReleaseBuffers: V4L2ReleaseBuffers error\n");
226         return RC_ERROR;
227     }
228 
229     return RC_OK;
230 }
231 
loopBuffers()232 void HosV4L2Dev::loopBuffers()
233 {
234     int nfds, rc;
235     struct epoll_event events[MAXSTREAMCOUNT];
236 
237     CAMERA_LOGD("!!! loopBuffers enter\n");
238     prctl(PR_SET_NAME, "v4l2_loopbuffer");
239 
240     while (streamNumber_ > 0) {
241         nfds = epoll_wait(epollFd_, events, MAXSTREAMCOUNT, -1);
242         CAMERA_LOGD("loopBuffers: epoll_wait rc = %d streamNumber_ == %d\n", nfds, streamNumber_);
243 
244         for (int n = 0; nfds > 0; ++n, --nfds) {
245             if ((events[n].events & EPOLLIN) && (events[n].data.fd != eventFd_)) {
246                 CHECK_IF_PTR_NULL_RETURN_VOID(myBuffers_);
247                 rc = myBuffers_->V4L2DequeueBuffer(events[n].data.fd);
248                 if (rc == RC_ERROR) {
249                     CAMERA_LOGE("loopBuffers: myBuffers_->V4L2DequeueBuffer return error == %d\n", rc);
250                     continue;
251                 }
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 } // namespace OHOS::Camera
714