| 115 | } |
| 116 | |
| 117 | Status StartCameraCapture(int camera_handle, int buffer_count, |
| 118 | CameraBuffer** buffers) { |
| 119 | struct v4l2_requestbuffers req; |
| 120 | memset(&req, 0, sizeof(req)); |
| 121 | req.count = buffer_count; |
| 122 | req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 123 | req.memory = V4L2_MEMORY_MMAP; |
| 124 | Status request_buffers_status = |
| 125 | SendCameraCommand(camera_handle, VIDIOC_REQBUFS, &req); |
| 126 | if (!request_buffers_status.ok()) { |
| 127 | LOG(ERROR) << "Request buffers failed with " << request_buffers_status; |
| 128 | return request_buffers_status; |
| 129 | } |
| 130 | |
| 131 | *buffers = (CameraBuffer*)(calloc(buffer_count, sizeof(*buffers))); |
| 132 | for (int n_buffers = 0; n_buffers < buffer_count; ++n_buffers) { |
| 133 | struct v4l2_buffer buf; |
| 134 | memset(&buf, 0, sizeof(buf)); |
| 135 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 136 | buf.memory = V4L2_MEMORY_MMAP; |
| 137 | buf.index = n_buffers; |
| 138 | Status query_buffer_status = |
| 139 | SendCameraCommand(camera_handle, VIDIOC_QUERYBUF, &buf); |
| 140 | if (!query_buffer_status.ok()) { |
| 141 | LOG(ERROR) << "Query buffer failed with " << query_buffer_status; |
| 142 | return query_buffer_status; |
| 143 | } |
| 144 | (*buffers)[n_buffers].length = buf.length; |
| 145 | (*buffers)[n_buffers].start = |
| 146 | v4l2_mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 147 | camera_handle, buf.m.offset); |
| 148 | |
| 149 | if (MAP_FAILED == (*buffers)[n_buffers].start) { |
| 150 | LOG(ERROR) << "Memory-mapping buffer failed"; |
| 151 | return tensorflow::errors::Unknown("Memory-mapping buffer failed"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | for (int i = 0; i < buffer_count; ++i) { |
| 156 | struct v4l2_buffer buf; |
| 157 | memset(&buf, 0, sizeof(buf)); |
| 158 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 159 | buf.memory = V4L2_MEMORY_MMAP; |
| 160 | buf.index = i; |
| 161 | Status set_buffer_status = |
| 162 | SendCameraCommand(camera_handle, VIDIOC_QBUF, &buf); |
| 163 | if (!set_buffer_status.ok()) { |
| 164 | LOG(ERROR) << "Set buffer failed with " << set_buffer_status; |
| 165 | return set_buffer_status; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 170 | Status stream_on_status = |
| 171 | SendCameraCommand(camera_handle, VIDIOC_STREAMON, &type); |
| 172 | if (!stream_on_status.ok()) { |
| 173 | LOG(ERROR) << "Turning stream on failed with " << stream_on_status; |
| 174 | return stream_on_status; |
no test coverage detected