blob: 7bd4b4eb6c9821ad4eaca24bffd9cd5573c5f89c [file] [log] [blame]
Eddie James21b177e2018-12-11 13:14:46 -06001#include "ikvm_video.hpp"
2
Eddie James90d49582018-12-11 13:22:00 -06003#include <err.h>
4#include <errno.h>
5#include <fcntl.h>
6#include <linux/videodev2.h>
7#include <poll.h>
8#include <sys/ioctl.h>
9#include <sys/mman.h>
10#include <sys/select.h>
11#include <sys/stat.h>
12#include <sys/time.h>
13#include <sys/types.h>
14#include <unistd.h>
15
16#include <phosphor-logging/elog-errors.hpp>
17#include <phosphor-logging/elog.hpp>
18#include <phosphor-logging/log.hpp>
19#include <xyz/openbmc_project/Common/Device/error.hpp>
20#include <xyz/openbmc_project/Common/File/error.hpp>
21
Eddie James21b177e2018-12-11 13:14:46 -060022namespace ikvm
23{
24
Eddie James90d49582018-12-11 13:22:00 -060025const int Video::bitsPerSample(8);
26const int Video::bytesPerPixel(4);
27const int Video::samplesPerPixel(3);
28
29using namespace phosphor::logging;
30using namespace sdbusplus::xyz::openbmc_project::Common::File::Error;
31using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
32
Eddie James21b177e2018-12-11 13:14:46 -060033Video::Video(const std::string& p, Input& input, int fr) :
Eddie James23135dd2019-08-09 15:41:37 -050034 resizeAfterOpen(false), timingsError(false), fd(-1), frameRate(fr),
35 lastFrameIndex(-1), height(600), width(800), input(input), path(p)
Eddie James21b177e2018-12-11 13:14:46 -060036{
37}
38
39Video::~Video()
40{
Eddie James90d49582018-12-11 13:22:00 -060041 stop();
42}
43
44char* Video::getData()
45{
46 if (lastFrameIndex >= 0)
47 {
48 return (char*)buffers[lastFrameIndex].data;
49 }
50
51 return nullptr;
52}
53
54void Video::getFrame()
55{
56 int rc(0);
57 int fd_flags;
58 v4l2_buffer buf;
59 fd_set fds;
60 timeval tv;
61
62 if (fd < 0)
63 {
64 return;
65 }
66
67 FD_ZERO(&fds);
68 FD_SET(fd, &fds);
69
70 tv.tv_sec = 1;
71 tv.tv_usec = 0;
72
73 memset(&buf, 0, sizeof(v4l2_buffer));
74 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
75 buf.memory = V4L2_MEMORY_MMAP;
76
77 // Switch to non-blocking in order to safely dequeue all buffers; if the
78 // video signal is lost while blocking to dequeue, the video driver may
79 // wait forever if signal is not re-acquired
80 fd_flags = fcntl(fd, F_GETFL);
81 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
82
83 rc = select(fd + 1, &fds, NULL, NULL, &tv);
84 if (rc > 0)
85 {
86 do
87 {
88 rc = ioctl(fd, VIDIOC_DQBUF, &buf);
89 if (rc >= 0)
90 {
91 buffers[buf.index].queued = false;
92
93 if (!(buf.flags & V4L2_BUF_FLAG_ERROR))
94 {
95 lastFrameIndex = buf.index;
96 buffers[lastFrameIndex].payload = buf.bytesused;
97 break;
98 }
99 else
100 {
101 buffers[buf.index].payload = 0;
102 }
103 }
104 } while (rc >= 0);
105 }
106
107 fcntl(fd, F_SETFL, fd_flags);
108
109 for (unsigned int i = 0; i < buffers.size(); ++i)
110 {
111 if (i == (unsigned int)lastFrameIndex)
112 {
113 continue;
114 }
115
116 if (!buffers[i].queued)
117 {
118 memset(&buf, 0, sizeof(v4l2_buffer));
119 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
120 buf.memory = V4L2_MEMORY_MMAP;
121 buf.index = i;
122
123 rc = ioctl(fd, VIDIOC_QBUF, &buf);
124 if (rc)
125 {
126 log<level::ERR>("Failed to queue buffer",
127 entry("ERROR=%s", strerror(errno)));
128 }
129 else
130 {
131 buffers[i].queued = true;
132 }
133 }
134 }
135}
136
137bool Video::needsResize()
138{
139 int rc;
140 v4l2_dv_timings timings;
141
142 if (fd < 0)
143 {
144 return false;
145 }
146
147 if (resizeAfterOpen)
148 {
149 return true;
150 }
151
152 memset(&timings, 0, sizeof(v4l2_dv_timings));
153 rc = ioctl(fd, VIDIOC_QUERY_DV_TIMINGS, &timings);
154 if (rc < 0)
155 {
Eddie James23135dd2019-08-09 15:41:37 -0500156 if (!timingsError)
157 {
158 log<level::ERR>("Failed to query timings",
159 entry("ERROR=%s", strerror(errno)));
160 timingsError = true;
161 }
162
Jae Hyun Yoof6ed0e72019-03-15 15:21:51 -0700163 restart();
Eddie James90d49582018-12-11 13:22:00 -0600164 return false;
165 }
Jae Hyun Yoo7a89cd22019-08-21 16:52:30 -0700166 else
Eddie James23135dd2019-08-09 15:41:37 -0500167 {
168 timingsError = false;
Eddie James23135dd2019-08-09 15:41:37 -0500169 }
Eddie James90d49582018-12-11 13:22:00 -0600170
171 if (timings.bt.width != width || timings.bt.height != height)
172 {
173 width = timings.bt.width;
174 height = timings.bt.height;
175
176 if (!width || !height)
177 {
178 log<level::ERR>("Failed to get new resolution",
179 entry("WIDTH=%d", width),
180 entry("HEIGHT=%d", height));
181 elog<Open>(
182 xyz::openbmc_project::Common::File::Open::ERRNO(-EPROTO),
183 xyz::openbmc_project::Common::File::Open::PATH(path.c_str()));
184 }
185
186 lastFrameIndex = -1;
187 return true;
188 }
189
190 return false;
191}
192
193void Video::resize()
194{
195 int rc;
196 unsigned int i;
197 bool needsResizeCall(false);
198 v4l2_buf_type type(V4L2_BUF_TYPE_VIDEO_CAPTURE);
199 v4l2_requestbuffers req;
200
201 if (fd < 0)
202 {
203 return;
204 }
205
206 if (resizeAfterOpen)
207 {
208 resizeAfterOpen = false;
209 return;
210 }
211
212 for (i = 0; i < buffers.size(); ++i)
213 {
214 if (buffers[i].data)
215 {
216 needsResizeCall = true;
217 break;
218 }
219 }
220
221 if (needsResizeCall)
222 {
223 rc = ioctl(fd, VIDIOC_STREAMOFF, &type);
224 if (rc)
225 {
226 log<level::ERR>("Failed to stop streaming",
227 entry("ERROR=%s", strerror(errno)));
228 elog<ReadFailure>(
229 xyz::openbmc_project::Common::Device::ReadFailure::
230 CALLOUT_ERRNO(errno),
231 xyz::openbmc_project::Common::Device::ReadFailure::
232 CALLOUT_DEVICE_PATH(path.c_str()));
233 }
234 }
235
236 for (i = 0; i < buffers.size(); ++i)
237 {
238 if (buffers[i].data)
239 {
240 munmap(buffers[i].data, buffers[i].size);
241 buffers[i].data = nullptr;
242 buffers[i].queued = false;
243 }
244 }
245
246 if (needsResizeCall)
247 {
248 v4l2_dv_timings timings;
249
250 memset(&req, 0, sizeof(v4l2_requestbuffers));
251 req.count = 0;
252 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
253 req.memory = V4L2_MEMORY_MMAP;
254 rc = ioctl(fd, VIDIOC_REQBUFS, &req);
255 if (rc < 0)
256 {
257 log<level::ERR>("Failed to zero streaming buffers",
258 entry("ERROR=%s", strerror(errno)));
259 elog<ReadFailure>(
260 xyz::openbmc_project::Common::Device::ReadFailure::
261 CALLOUT_ERRNO(errno),
262 xyz::openbmc_project::Common::Device::ReadFailure::
263 CALLOUT_DEVICE_PATH(path.c_str()));
264 }
265
266 memset(&timings, 0, sizeof(v4l2_dv_timings));
267 rc = ioctl(fd, VIDIOC_QUERY_DV_TIMINGS, &timings);
268 if (rc < 0)
269 {
270 log<level::ERR>("Failed to query timings",
271 entry("ERROR=%s", strerror(errno)));
272 elog<ReadFailure>(
273 xyz::openbmc_project::Common::Device::ReadFailure::
274 CALLOUT_ERRNO(errno),
275 xyz::openbmc_project::Common::Device::ReadFailure::
276 CALLOUT_DEVICE_PATH(path.c_str()));
277 }
278
279 rc = ioctl(fd, VIDIOC_S_DV_TIMINGS, &timings);
280 if (rc < 0)
281 {
282 log<level::ERR>("Failed to set timings",
283 entry("ERROR=%s", strerror(errno)));
284 elog<ReadFailure>(
285 xyz::openbmc_project::Common::Device::ReadFailure::
286 CALLOUT_ERRNO(errno),
287 xyz::openbmc_project::Common::Device::ReadFailure::
288 CALLOUT_DEVICE_PATH(path.c_str()));
289 }
290
291 buffers.clear();
292 }
293
294 memset(&req, 0, sizeof(v4l2_requestbuffers));
295 req.count = 3;
296 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
297 req.memory = V4L2_MEMORY_MMAP;
298 rc = ioctl(fd, VIDIOC_REQBUFS, &req);
299 if (rc < 0 || req.count < 2)
300 {
301 log<level::ERR>("Failed to request streaming buffers",
302 entry("ERROR=%s", strerror(errno)));
303 elog<ReadFailure>(
304 xyz::openbmc_project::Common::Device::ReadFailure::CALLOUT_ERRNO(
305 errno),
306 xyz::openbmc_project::Common::Device::ReadFailure::
307 CALLOUT_DEVICE_PATH(path.c_str()));
308 }
309
310 buffers.resize(req.count);
311
312 for (i = 0; i < buffers.size(); ++i)
313 {
314 v4l2_buffer buf;
315
316 memset(&buf, 0, sizeof(v4l2_buffer));
317 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
318 buf.memory = V4L2_MEMORY_MMAP;
319 buf.index = i;
320
321 rc = ioctl(fd, VIDIOC_QUERYBUF, &buf);
322 if (rc < 0)
323 {
324 log<level::ERR>("Failed to query buffer",
325 entry("ERROR=%s", strerror(errno)));
326 elog<ReadFailure>(
327 xyz::openbmc_project::Common::Device::ReadFailure::
328 CALLOUT_ERRNO(errno),
329 xyz::openbmc_project::Common::Device::ReadFailure::
330 CALLOUT_DEVICE_PATH(path.c_str()));
331 }
332
333 buffers[i].data = mmap(NULL, buf.length, PROT_READ | PROT_WRITE,
334 MAP_SHARED, fd, buf.m.offset);
335 if (buffers[i].data == MAP_FAILED)
336 {
337 log<level::ERR>("Failed to mmap buffer",
338 entry("ERROR=%s", strerror(errno)));
339 elog<ReadFailure>(
340 xyz::openbmc_project::Common::Device::ReadFailure::
341 CALLOUT_ERRNO(errno),
342 xyz::openbmc_project::Common::Device::ReadFailure::
343 CALLOUT_DEVICE_PATH(path.c_str()));
344 }
345
346 buffers[i].size = buf.length;
347
348 rc = ioctl(fd, VIDIOC_QBUF, &buf);
349 if (rc < 0)
350 {
351 log<level::ERR>("Failed to queue buffer",
352 entry("ERROR=%s", strerror(errno)));
353 elog<ReadFailure>(
354 xyz::openbmc_project::Common::Device::ReadFailure::
355 CALLOUT_ERRNO(errno),
356 xyz::openbmc_project::Common::Device::ReadFailure::
357 CALLOUT_DEVICE_PATH(path.c_str()));
358 }
359
360 buffers[i].queued = true;
361 }
362
363 rc = ioctl(fd, VIDIOC_STREAMON, &type);
364 if (rc)
365 {
366 log<level::ERR>("Failed to start streaming",
367 entry("ERROR=%s", strerror(errno)));
368 elog<ReadFailure>(
369 xyz::openbmc_project::Common::Device::ReadFailure::CALLOUT_ERRNO(
370 errno),
371 xyz::openbmc_project::Common::Device::ReadFailure::
372 CALLOUT_DEVICE_PATH(path.c_str()));
373 }
374}
375
376void Video::start()
377{
378 int rc;
379 size_t oldHeight = height;
380 size_t oldWidth = width;
381 v4l2_capability cap;
382 v4l2_format fmt;
383 v4l2_streamparm sparm;
384
385 if (fd >= 0)
386 {
387 return;
388 }
389
Jae Hyun Yoo8ec3e232019-04-15 12:10:04 -0700390 input.sendWakeupPacket();
391
Eddie James90d49582018-12-11 13:22:00 -0600392 fd = open(path.c_str(), O_RDWR);
393 if (fd < 0)
394 {
Jae Hyun Yoo8ec3e232019-04-15 12:10:04 -0700395 log<level::ERR>("Failed to open video device",
396 entry("PATH=%s", path.c_str()),
397 entry("ERROR=%s", strerror(errno)));
398 elog<Open>(
399 xyz::openbmc_project::Common::File::Open::ERRNO(errno),
400 xyz::openbmc_project::Common::File::Open::PATH(path.c_str()));
Eddie James90d49582018-12-11 13:22:00 -0600401 }
402
403 memset(&cap, 0, sizeof(v4l2_capability));
404 rc = ioctl(fd, VIDIOC_QUERYCAP, &cap);
405 if (rc < 0)
406 {
407 log<level::ERR>("Failed to query video device capabilities",
408 entry("ERROR=%s", strerror(errno)));
409 elog<ReadFailure>(
410 xyz::openbmc_project::Common::Device::ReadFailure::CALLOUT_ERRNO(
411 errno),
412 xyz::openbmc_project::Common::Device::ReadFailure::
413 CALLOUT_DEVICE_PATH(path.c_str()));
414 }
415
416 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) ||
417 !(cap.capabilities & V4L2_CAP_STREAMING))
418 {
419 log<level::ERR>("Video device doesn't support this application");
420 elog<Open>(
421 xyz::openbmc_project::Common::File::Open::ERRNO(errno),
422 xyz::openbmc_project::Common::File::Open::PATH(path.c_str()));
423 }
424
425 memset(&fmt, 0, sizeof(v4l2_format));
426 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
427 rc = ioctl(fd, VIDIOC_G_FMT, &fmt);
428 if (rc < 0)
429 {
430 log<level::ERR>("Failed to query video device format",
431 entry("ERROR=%s", strerror(errno)));
432 elog<ReadFailure>(
433 xyz::openbmc_project::Common::Device::ReadFailure::CALLOUT_ERRNO(
434 errno),
435 xyz::openbmc_project::Common::Device::ReadFailure::
436 CALLOUT_DEVICE_PATH(path.c_str()));
437 }
438
439 memset(&sparm, 0, sizeof(v4l2_streamparm));
440 sparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
441 sparm.parm.capture.timeperframe.numerator = 1;
442 sparm.parm.capture.timeperframe.denominator = frameRate;
443 rc = ioctl(fd, VIDIOC_S_PARM, &sparm);
444 if (rc < 0)
445 {
446 log<level::WARNING>("Failed to set video device frame rate",
447 entry("ERROR=%s", strerror(errno)));
448 }
449
450 height = fmt.fmt.pix.height;
451 width = fmt.fmt.pix.width;
452
453 resize();
454
455 if (oldHeight != height || oldWidth != width)
456 {
457 resizeAfterOpen = true;
458 }
459}
460
461void Video::stop()
462{
463 int rc;
464 unsigned int i;
465 v4l2_buf_type type(V4L2_BUF_TYPE_VIDEO_CAPTURE);
466
467 if (fd < 0)
468 {
469 return;
470 }
471
472 lastFrameIndex = -1;
473
474 rc = ioctl(fd, VIDIOC_STREAMOFF, &type);
475 if (rc)
476 {
477 log<level::ERR>("Failed to stop streaming",
478 entry("ERROR=%s", strerror(errno)));
479 }
480
481 for (i = 0; i < buffers.size(); ++i)
482 {
483 if (buffers[i].data)
484 {
485 munmap(buffers[i].data, buffers[i].size);
486 buffers[i].data = nullptr;
487 buffers[i].queued = false;
488 }
489 }
490
491 close(fd);
492 fd = -1;
Eddie James21b177e2018-12-11 13:14:46 -0600493}
494
495} // namespace ikvm