• 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;
127 
128     int 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;
186 
187     int 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;
235     int rc;
236     struct epoll_event events[MAXSTREAMCOUNT];
237 
238     CAMERA_LOGD("!!! loopBuffers enter, streamNumber_=%{public}d\n", streamNumber_);
239     prctl(PR_SET_NAME, "v4l2_loopbuffer");
240 
241     while (streamNumber_ > 0) {
242         nfds = epoll_wait(epollFd_, events, MAXSTREAMCOUNT, -1);
243         CAMERA_LOGD("loopBuffers: epoll_wait rc = %{public}d streamNumber_ == %{public}d\n", nfds, streamNumber_);
244 
245         for (int n = 0; nfds > 0; ++n, --nfds) {
246             if ((events[n].events & EPOLLIN) && (events[n].data.fd != eventFd_)) {
247                 CHECK_IF_PTR_NULL_RETURN_VOID(myBuffers_);
248                 rc = myBuffers_->V4L2DequeueBuffer(events[n].data.fd);
249                 if (rc == RC_ERROR) {
250                     CAMERA_LOGE("loopBuffers: myBuffers_->V4L2DequeueBuffer return error == %d\n", rc);
251                     continue;
252                 }
253             } else {
254                 CAMERA_LOGD("loopBuffers: epoll invalid events = 0x%x or eventFd exit = %d\n",
255                     events[n].events, (events[n].data.fd == eventFd_));
256                 usleep(WATING_TIME);
257             }
258         }
259     }
260     CAMERA_LOGD("!!! loopBuffers exit, streamNumber_=%{public}d\n", streamNumber_);
261 }
262 
CreateEpoll(int fd,const unsigned int streamNumber)263 RetCode HosV4L2Dev::CreateEpoll(int fd, const unsigned int streamNumber)
264 {
265     struct epoll_event epollevent = {};
266 
267     if (streamNumber == 0) {
268         epollFd_ = epoll_create(MAXSTREAMCOUNT);
269         if (epollFd_ < 0) {
270             CAMERA_LOGE("V4L2 StartStream create_epoll failed\n");
271             return RC_ERROR;
272         }
273         epollevent.events = EPOLLIN;
274         epollevent.data.fd = fd;
275         epoll_ctl(epollFd_, EPOLL_CTL_ADD, fd, &epollevent);
276 
277         std::lock_guard<std::mutex> l(epollLock_);
278         epollEvent_.push_back(epollevent);
279 
280         eventFd_ = eventfd(0, 0);
281         epollevent = {};
282         epollevent.events = EPOLLIN;
283         epollevent.data.fd = eventFd_;
284         epoll_ctl(epollFd_, EPOLL_CTL_ADD, eventFd_, &epollevent);
285     } else {
286         epollevent.events = EPOLLIN;
287         epollevent.data.fd = fd;
288 
289         std::lock_guard<std::mutex> l(epollLock_);
290         epollEvent_.push_back(epollevent);
291 
292         epoll_ctl(epollFd_, EPOLL_CTL_ADD, fd, &epollevent);
293     }
294     return RC_OK;
295 }
296 
EraseEpoll(int fd)297 void HosV4L2Dev::EraseEpoll(int fd)
298 {
299     auto itr = std::find_if(epollEvent_.begin(), epollEvent_.end(), [fd](const epoll_event& event) {
300         if (event.data.fd == fd) {
301             return true;
302         } else {
303             return false;
304         }
305     });
306     if (itr != epollEvent_.end()) {
307         struct epoll_event event = *itr;
308         epoll_ctl(epollFd_, EPOLL_CTL_DEL, fd, &event);
309         std::lock_guard<std::mutex> l(epollLock_);
310         epollEvent_.erase(itr);
311     }
312 }
313 
StartStream(const std::string & cameraID)314 RetCode HosV4L2Dev::StartStream(const std::string& cameraID)
315 {
316     int rc;
317 
318     int fd = GetCurrentFd(cameraID);
319     if (fd < 0) {
320         CAMERA_LOGE("error: ReqBuffers: GetCurrentFd error\n");
321         return RC_ERROR;
322     }
323 
324     if (myStreams_ == nullptr) {
325         myStreams_ = std::make_shared<HosV4L2Streams>(bufferType_);
326         if (myStreams_ == nullptr) {
327             CAMERA_LOGE("error: StartStream: myStreams_ make_shared is NULL\n");
328             return RC_ERROR;
329         }
330     }
331 
332     rc = myStreams_->V4L2StreamOn(fd);
333     if (rc == RC_ERROR) {
334         CAMERA_LOGE("error: StartStream: V4L2StreamOn error\n");
335         return RC_ERROR;
336     }
337 
338     rc = CreateEpoll(fd, streamNumber_);
339     if (rc == RC_ERROR) {
340         CAMERA_LOGE("StartStream: CreateEpoll error\n");
341         return RC_ERROR;
342     }
343 
344     if (streamNumber_ == 0) {
345         streamNumber_++;
346         CAMERA_LOGE("go start thread loopBuffers, streamNumber_=%{public}d\n", streamNumber_);
347         streamThread_ = new (std::nothrow) std::thread(&HosV4L2Dev::loopBuffers, this);
348         if (streamThread_ == nullptr) {
349             CAMERA_LOGE("V4L2 StartStream start thread failed\n");
350             streamNumber_--;
351             return RC_ERROR;
352         }
353     }
354 
355     return RC_OK;
356 }
357 
StopStream(const std::string & cameraID)358 RetCode HosV4L2Dev::StopStream(const std::string& cameraID)
359 {
360     int rc, fd;
361 
362     if (myStreams_ == nullptr) {
363         CAMERA_LOGE("error: StopStream: myStreams_ is NULL\n");
364         return RC_ERROR;
365     }
366 
367     if (streamThread_ == nullptr) {
368         CAMERA_LOGE("StopStream thread is stopped\n");
369         return RC_ERROR;
370     }
371 
372     streamNumber_ -= 1;
373     CAMERA_LOGD("HosV4L2Dev::StopStream streamNumber_ = %d\n", streamNumber_);
374 
375     if (streamNumber_ == 0) {
376         CAMERA_LOGD("waiting loopBuffers stop\n");
377         uint64_t one = 1;
378         write(eventFd_, &one, sizeof(one));
379         streamThread_->join();
380         close(eventFd_);
381     }
382 
383     fd = GetCurrentFd(cameraID);
384     if (fd < 0) {
385         CAMERA_LOGE("error: ReqBuffers: GetCurrentFd error\n");
386         return RC_ERROR;
387     }
388 
389     rc = myStreams_->V4L2StreamOff(fd);
390     if (rc == RC_ERROR) {
391         CAMERA_LOGE("error: StartStream: V4L2StreamOn error\n");
392         return RC_ERROR;
393     }
394 
395     EraseEpoll(fd);
396 
397     if (streamNumber_ == 0) {
398         close(epollFd_);
399         delete streamThread_;
400         streamThread_ = nullptr;
401     }
402 
403     return RC_OK;
404 }
405 
UpdateSetting(const std::string & cameraID,AdapterCmd command,const int * args)406 RetCode HosV4L2Dev::UpdateSetting(const std::string& cameraID, AdapterCmd command, const int* args)
407 {
408     int32_t fd;
409     int rc = 0;
410     if (args == nullptr) {
411         CAMERA_LOGE("HosV4L2Dev::UpdateSetting: args is NULL\n");
412         return RC_ERROR;
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     fd = GetCurrentFd(cameraID);
422     if (fd < 0) {
423         CAMERA_LOGE("UpdateSetting: GetCurrentFd error\n");
424         return RC_ERROR;
425     }
426     switch (command) {
427         case CMD_EXPOSURE_MODE:
428             rc = myControl_->V4L2SetCtrl(fd, V4L2_CID_EXPOSURE_AUTO, *args);
429             break;
430         case CMD_AE_EXPOTIME:
431             rc = myControl_->V4L2SetCtrl(fd, V4L2_CID_EXPOSURE_ABSOLUTE, *args);
432             break;
433         case CMD_EXPOSURE_COMPENSATION:
434             rc = myControl_->V4L2SetCtrl(fd, V4L2_CID_EXPOSURE, *args);
435             break;
436         case CMD_AWB_MODE:
437             rc = myControl_->V4L2SetCtrl(fd, V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE, *args);
438             break;
439         case CMD_FOCUS_MODE:
440             rc = myControl_->V4L2SetCtrl(fd, V4L2_CID_FOCUS_AUTO, *args);
441             break;
442         case CMD_METER_MODE:
443             rc = myControl_->V4L2SetCtrl(fd, V4L2_CID_EXPOSURE_METERING, *args);
444             break;
445         case CMD_FLASH_MODE:
446             rc = myControl_->V4L2SetCtrl(fd, V4L2_CID_FLASH_LED_MODE, *args);
447             break;
448         default:
449             break;
450     }
451     if (rc != RC_OK) {
452         return RC_ERROR;
453     }
454     return RC_OK;
455 }
456 
QuerySetting(const std::string & cameraID,unsigned int command,int * args)457 RetCode HosV4L2Dev::QuerySetting(const std::string& cameraID, unsigned int command, int* args)
458 {
459     int32_t fd;
460     int32_t value = 0;
461     int rc = 0;
462 
463     if (args == nullptr) {
464         CAMERA_LOGE("HosV4L2Dev::QuerySetting: args is NULL\n");
465         return RC_ERROR;
466     }
467 
468     if (myControl_ == nullptr) {
469         myControl_ = std::make_shared<HosV4L2Control>();
470         if (myControl_ == nullptr) {
471             CAMERA_LOGE("HosV4L2Dev::QuerySetting: myControl_ make_shared is NULL\n");
472             return RC_ERROR;
473         }
474     }
475 
476     fd = GetCurrentFd(cameraID);
477     if (fd < 0) {
478         CAMERA_LOGE("QuerySetting: GetCurrentFd error\n");
479         return RC_ERROR;
480     }
481 
482     rc = myControl_->V4L2GetCtrl(fd, command, value);
483     if (rc != RC_OK) {
484         return RC_ERROR;
485     }
486 
487     *(reinterpret_cast<int32_t*>(args)) = value;
488 
489     return RC_OK;
490 }
491 
GetNumberCtrls(const std::string & cameraID,std::vector<DeviceControl> & control)492 RetCode HosV4L2Dev::GetNumberCtrls(const std::string& cameraID, std::vector<DeviceControl>& control)
493 {
494     int32_t fd;
495 
496     if (myControl_ == nullptr) {
497         myControl_ = std::make_shared<HosV4L2Control>();
498         if (myControl_ == nullptr) {
499             CAMERA_LOGE("HosV4L2Dev::GetNumberCtrls: myControl_ make_shared is NULL\n");
500             return RC_ERROR;
501         }
502     }
503 
504     fd = GetCurrentFd(cameraID);
505     if (fd < 0) {
506         CAMERA_LOGE("GetNumberCtrls: GetCurrentFd error\n");
507         return RC_ERROR;
508     }
509 
510     return myControl_->V4L2GetCtrls(fd, control, control.size());
511 }
512 
SetNumberCtrls(const std::string & cameraID,std::vector<DeviceControl> & control)513 RetCode HosV4L2Dev::SetNumberCtrls(const std::string& cameraID, std::vector<DeviceControl>& control)
514 {
515     int32_t fd;
516 
517     if (myControl_ == nullptr) {
518         myControl_ = std::make_shared<HosV4L2Control>();
519         if (myControl_ == nullptr) {
520             CAMERA_LOGE("HosV4L2Dev::SetNumberCtrls: myControl_ make_shared is NULL\n");
521             return RC_ERROR;
522         }
523     }
524 
525     fd = GetCurrentFd(cameraID);
526     if (fd < 0) {
527         CAMERA_LOGE("SetNumberCtrls: GetCurrentFd error\n");
528         return RC_ERROR;
529     }
530 
531     return myControl_->V4L2SetCtrls(fd, control, control.size());
532 }
533 
GetControls(const std::string & cameraID,std::vector<DeviceControl> & control)534 RetCode HosV4L2Dev::GetControls(const std::string& cameraID, std::vector<DeviceControl>& control)
535 {
536     int fd = 0;
537     int rc = 0;
538 
539     if (myControl_ == nullptr) {
540         myControl_ = std::make_shared<HosV4L2Control>();
541         if (myControl_ == nullptr) {
542             CAMERA_LOGE("HosV4L2Dev::GetControls: myControl_ make_shared is NULL\n");
543             return RC_ERROR;
544         }
545     }
546 
547     fd = GetCurrentFd(cameraID);
548     if (fd < 0) {
549         CAMERA_LOGE("GetControls: GetCurrentFd error\n");
550         return RC_ERROR;
551     }
552 
553     rc = myControl_->V4L2GetControls(fd, control);
554     if (rc == RC_ERROR) {
555         CAMERA_LOGE("myControl_->V4L2GetControls fail\n");
556         return RC_ERROR;
557     }
558 
559     return RC_OK;
560 }
561 
GetFmtDescs(const std::string & cameraID,std::vector<DeviceFormat> & fmtDesc)562 RetCode HosV4L2Dev::GetFmtDescs(const std::string& cameraID, std::vector<DeviceFormat>& fmtDesc)
563 {
564     int fd = 0;
565     int rc = 0;
566 
567     if (myFileFormat_ == nullptr) {
568         CAMERA_LOGE("GetFmtDescs: myFileFormat_ == nullptr\n");
569         return RC_ERROR;
570     }
571 
572     fd = GetCurrentFd(cameraID);
573     if (fd < 0) {
574         CAMERA_LOGE("GetFmtDescs: GetCurrentFd error\n");
575         return RC_ERROR;
576     }
577 
578     rc = myFileFormat_->V4L2GetFmtDescs(fd, fmtDesc);
579     if (rc == RC_ERROR) {
580         CAMERA_LOGE("myFileFormat_->V4L2GetFmtDescs fail\n");
581         return RC_ERROR;
582     }
583 
584     return RC_OK;
585 }
586 
ConfigFps(const int fd,DeviceFormat & format,V4l2FmtCmd command)587 RetCode HosV4L2Dev::ConfigFps(const int fd, DeviceFormat& format, V4l2FmtCmd command)
588 {
589     RetCode rc = RC_OK;
590 
591     if (myStreams_ == nullptr) {
592         myStreams_ = std::make_shared<HosV4L2Streams>(bufferType_);
593         if (myStreams_ == nullptr) {
594             CAMERA_LOGE("error: ConfigSys: myStreams_ make_shared is nullptr\n");
595             return RC_ERROR;
596         }
597     }
598 
599     if (command == CMD_V4L2_SET_FPS) {
600         rc = myStreams_->V4L2StreamFPSSet(fd, format);
601     } else {
602         rc = myStreams_->V4L2StreamFPSGet(fd, format);
603     }
604 
605     if (rc != RC_OK) {
606         CAMERA_LOGE("ConfigFps CMD %d fail\n", command);
607     }
608 
609     return rc;
610 }
611 
ConfigSys(const std::string & cameraID,V4l2FmtCmd command,DeviceFormat & format)612 RetCode HosV4L2Dev::ConfigSys(const std::string& cameraID, V4l2FmtCmd command, DeviceFormat& format)
613 {
614     int fd = 0;
615     RetCode rc = RC_OK;
616 
617     if (myFileFormat_ == nullptr) {
618         CAMERA_LOGE("GetFmtDescs: ConfigSys == nullptr\n");
619         return RC_ERROR;
620     }
621 
622     fd = GetCurrentFd(cameraID);
623     if (fd < 0) {
624         CAMERA_LOGE("ConfigSys: GetCurrentFd error\n");
625         return RC_ERROR;
626     }
627 
628     switch (command) {
629         case CMD_V4L2_GET_FORMAT:
630             rc = myFileFormat_->V4L2GetFmt(fd, format);
631             break;
632 
633         case CMD_V4L2_SET_FORMAT:
634             rc = myFileFormat_->V4L2SetFmt(fd, format);
635             break;
636 
637         case CMD_V4L2_GET_CROPCAP:
638             rc = myFileFormat_->V4L2GetCropCap(fd, format);
639             break;
640 
641         case CMD_V4L2_GET_CROP:
642             rc = myFileFormat_->V4L2GetCrop(fd, format);
643             break;
644 
645         case CMD_V4L2_SET_CROP:
646             rc = myFileFormat_->V4L2SetCrop(fd, format);
647             break;
648 
649         case CMD_V4L2_SET_FPS:
650         case CMD_V4L2_GET_FPS:
651             rc = ConfigFps(fd, format, command);
652             break;
653 
654         default:
655             CAMERA_LOGE("HosV4L2Dev::ConfigSys unknown command\n");
656             break;
657     }
658 
659     if (rc != RC_OK) {
660         CAMERA_LOGE("ConfigSys CMD %d fail\n", command);
661     }
662 
663     return rc;
664 }
665 
SetCallback(BufCallback cb)666 RetCode HosV4L2Dev::SetCallback(BufCallback cb)
667 {
668     if (cb == nullptr) {
669         CAMERA_LOGE("HosV4L2Dev::SetCallback is null");
670         return RC_ERROR;
671     }
672     if (myBuffers_ == nullptr) {
673         CAMERA_LOGE("SetCallback myBuffers_ is NULL\n");
674         return RC_ERROR;
675     }
676 
677     myBuffers_->SetCallback(cb);
678 
679     return RC_OK;
680 }
Flush(const std::string & cameraID)681 RetCode HosV4L2Dev::Flush(const std::string& cameraID)
682 {
683     int rc = 0;
684 
685     int fd = GetCurrentFd(cameraID);
686     if (fd < 0) {
687         CAMERA_LOGE("HosV4L2Dev::Flush: GetCurrentFd error\n");
688         return RC_ERROR;
689     }
690 
691     if (myBuffers_ == nullptr) {
692         CAMERA_LOGE(" HosV4L2Dev::Flush myBuffers_ is NULL\n");
693         return RC_ERROR;
694     }
695 
696     rc = myBuffers_->Flush(fd);
697     if (rc == RC_ERROR) {
698         CAMERA_LOGE("HosV4L2Dev::Flush: error\n");
699         return RC_ERROR;
700     }
701 
702     return RC_OK;
703 }
704 
SetMemoryType(uint8_t & memType)705 void HosV4L2Dev::SetMemoryType(uint8_t &memType)
706 {
707     CAMERA_LOGD("func[HosV4L2Dev::%{public}s] memType[%{public}d]", __func__, memType);
708     if (memType == V4L2_MEMORY_MMAP) {
709         memoryType_ = V4L2_MEMORY_MMAP;
710     } else if (memType == V4L2_MEMORY_DMABUF) {
711         memoryType_ = V4L2_MEMORY_DMABUF;
712     }
713 }
714 } // namespace OHOS::Camera
715