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 <sys/ioctl.h>
20 #include <cstring>
21 #include <linux/videodev2.h>
22 #include "v4l2_stream.h"
23
24 namespace OHOS::Camera {
HosV4L2Streams(enum v4l2_buf_type bufferType)25 HosV4L2Streams::HosV4L2Streams(enum v4l2_buf_type bufferType)
26 : bufferType_(bufferType)
27 {
28 }
29
~HosV4L2Streams()30 HosV4L2Streams::~HosV4L2Streams() {}
31
V4L2StreamOn(int fd)32 RetCode HosV4L2Streams::V4L2StreamOn(int fd)
33 {
34 enum v4l2_buf_type buf_type;
35 int rc;
36 CAMERA_LOGD("HosV4L2Streams::V4L2StreamOn\n");
37
38 buf_type = bufferType_;
39 rc = ioctl(fd, VIDIOC_STREAMON, &buf_type);
40 if (rc < 0) {
41 CAMERA_LOGE("error: ioctl VIDIOC_STREAMON failed: %s\n", strerror(errno));
42 return RC_ERROR;
43 }
44
45 return RC_OK;
46 }
47
V4L2StreamOff(int fd)48 RetCode HosV4L2Streams::V4L2StreamOff(int fd)
49 {
50 enum v4l2_buf_type buf_type;
51 int rc;
52 CAMERA_LOGD("HosV4L2Streams::V4L2StreamOff\n");
53
54 buf_type = bufferType_;
55 rc = ioctl(fd, VIDIOC_STREAMOFF, &buf_type);
56 if (rc < 0) {
57 CAMERA_LOGE("error: ioctl VIDIOC_STREAMOFF failed: %s\n", strerror(errno));
58 return RC_ERROR;
59 }
60
61 return RC_OK;
62 }
63
V4L2StreamFPSGet(int fd,DeviceFormat & format)64 RetCode HosV4L2Streams::V4L2StreamFPSGet(int fd, DeviceFormat& format)
65 {
66 struct v4l2_streamparm Stream_Parm = {};
67 int rc;
68
69 Stream_Parm.type = bufferType_;
70
71 rc = ioctl(fd, VIDIOC_G_PARM, &Stream_Parm);
72 if (rc < 0) {
73 CAMERA_LOGE("error: ioctl VIDIOC_G_PARM failed: %s\n", strerror(errno));
74 return RC_ERROR;
75 }
76
77 format.fmtdesc.fps.numerator = Stream_Parm.parm.capture.timeperframe.numerator;
78 format.fmtdesc.fps.denominator = Stream_Parm.parm.capture.timeperframe.denominator;
79
80 return RC_OK;
81 }
82
V4L2StreamFPSSet(int fd,DeviceFormat & format)83 RetCode HosV4L2Streams::V4L2StreamFPSSet(int fd, DeviceFormat& format)
84 {
85 struct v4l2_streamparm Stream_Parm = {};
86 int rc;
87
88 Stream_Parm.type = bufferType_;
89
90 Stream_Parm.parm.capture.capturemode = format.fmtdesc.capturemode;
91 Stream_Parm.parm.capture.timeperframe.denominator = format.fmtdesc.fps.denominator;
92 Stream_Parm.parm.capture.timeperframe.numerator = format.fmtdesc.fps.numerator;
93
94 rc = ioctl(fd, VIDIOC_S_PARM, &Stream_Parm);
95 if (rc < 0) {
96 CAMERA_LOGE("error: ioctl VIDIOC_S_PARM failed: %s\n", strerror(errno));
97 return RC_ERROR;
98 }
99
100 return RC_OK;
101 }
102 } // namespace OHOS::Camera
103