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