NvV4l2ElementPlane.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * Copyright (c) 2016-2022, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of NVIDIA CORPORATION nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "NvV4l2ElementPlane.h"
  29. #include "NvLogging.h"
  30. #include <cstring>
  31. #include <errno.h>
  32. #include <libv4l2.h>
  33. #include <sys/mman.h>
  34. #include <sys/prctl.h>
  35. #include "nvbufsurface.h"
  36. #define CHECK_V4L2_RETURN(ret, str) \
  37. if (ret < 0) { \
  38. PLANE_SYS_ERROR_MSG(str << ": failed"); \
  39. return -1; \
  40. } else { \
  41. PLANE_DEBUG_MSG(str << ": success"); \
  42. return 0; \
  43. }
  44. using namespace std;
  45. NvV4l2ElementPlane::NvV4l2ElementPlane(enum v4l2_buf_type buf_type,
  46. const char *device_name, int &fd, bool blocking,
  47. NvElementProfiler &profiler)
  48. :fd(fd),
  49. v4l2elem_profiler(profiler),
  50. comp_name(device_name)
  51. {
  52. this->buf_type = buf_type;
  53. this->blocking = blocking;
  54. is_in_error = 0;
  55. switch (buf_type)
  56. {
  57. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  58. plane_name = "Output Plane";
  59. break;
  60. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  61. plane_name = "Capture Plane";
  62. break;
  63. default:
  64. ERROR_MSG("Unsupported v4l2_buf_type " << buf_type);
  65. plane_name = "Unknown";
  66. is_in_error = 1;
  67. }
  68. num_buffers = 0;
  69. buffers = NULL;
  70. n_planes = 0;
  71. memset(&planefmts, 0, sizeof(planefmts));
  72. num_queued_buffers = 0;
  73. total_queued_buffers = 0;
  74. total_dequeued_buffers = 0;
  75. streamon = false;
  76. pthread_mutex_init(&plane_lock, NULL);
  77. pthread_cond_init(&plane_cond, NULL);
  78. dqthread_running = false;
  79. stop_dqthread = false;
  80. dq_thread = 0;
  81. callback = NULL;
  82. memory_type = V4L2_MEMORY_MMAP;
  83. dqThread_data = NULL;
  84. }
  85. NvV4l2ElementPlane::~NvV4l2ElementPlane()
  86. {
  87. pthread_mutex_destroy(&plane_lock);
  88. pthread_cond_destroy(&plane_cond);
  89. }
  90. NvBuffer *
  91. NvV4l2ElementPlane::getNthBuffer(uint32_t n)
  92. {
  93. if (n >= num_buffers)
  94. {
  95. PLANE_DEBUG_MSG("WARNING:Requested " << n << "th buffer out of " <<
  96. num_buffers << "buffers. Returning NULL");
  97. return NULL;
  98. }
  99. return buffers[n];
  100. }
  101. int
  102. NvV4l2ElementPlane::dqBuffer(struct v4l2_buffer &v4l2_buf, NvBuffer ** buffer,
  103. NvBuffer ** shared_buffer, uint32_t num_retries)
  104. {
  105. int ret;
  106. v4l2_buf.type = buf_type;
  107. v4l2_buf.memory = memory_type;
  108. do
  109. {
  110. ret = v4l2_ioctl(fd, VIDIOC_DQBUF, &v4l2_buf);
  111. if (ret == 0)
  112. {
  113. pthread_mutex_lock(&plane_lock);
  114. if (buffer)
  115. *buffer = buffers[v4l2_buf.index];
  116. if (shared_buffer && memory_type == V4L2_MEMORY_DMABUF)
  117. {
  118. *shared_buffer =
  119. (NvBuffer *) buffers[v4l2_buf.index]->shared_buffer;
  120. }
  121. for (uint32_t i = 0; i < buffers[v4l2_buf.index]->n_planes; i++)
  122. {
  123. buffers[v4l2_buf.index]->planes[i].bytesused =
  124. v4l2_buf.m.planes[i].bytesused;
  125. }
  126. if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  127. {
  128. v4l2elem_profiler.finishProcessing(0, false);
  129. }
  130. total_dequeued_buffers++;
  131. num_queued_buffers--;
  132. pthread_cond_broadcast(&plane_cond);
  133. PLANE_DEBUG_MSG("DQed buffer " << v4l2_buf.index);
  134. pthread_mutex_unlock(&plane_lock);
  135. }
  136. else if (errno == EAGAIN)
  137. {
  138. pthread_mutex_lock(&plane_lock);
  139. if (v4l2_buf.flags & V4L2_BUF_FLAG_LAST)
  140. {
  141. pthread_mutex_unlock(&plane_lock);
  142. break;
  143. }
  144. pthread_mutex_unlock(&plane_lock);
  145. if (num_retries-- == 0)
  146. {
  147. PLANE_WARN_MSG("Error while DQing buffer: Resource temporarily unavailable");
  148. break;
  149. }
  150. if (!blocking)
  151. {
  152. break;
  153. }
  154. }
  155. else
  156. {
  157. is_in_error = 1;
  158. PLANE_SYS_ERROR_MSG("Error while DQing buffer");
  159. break;
  160. }
  161. }
  162. while (ret && !is_in_error);
  163. return ret;
  164. }
  165. int
  166. NvV4l2ElementPlane::qBuffer(struct v4l2_buffer &v4l2_buf, NvBuffer * shared_buffer)
  167. {
  168. int ret;
  169. uint32_t i;
  170. NvBuffer *buffer;
  171. pthread_mutex_lock(&plane_lock);
  172. buffer = buffers[v4l2_buf.index];
  173. v4l2_buf.type = buf_type;
  174. v4l2_buf.memory = memory_type;
  175. v4l2_buf.length = n_planes;
  176. switch (memory_type)
  177. {
  178. case V4L2_MEMORY_USERPTR:
  179. buffer->shared_buffer = shared_buffer;
  180. for (i = 0; i < buffer->n_planes; i++)
  181. {
  182. if (shared_buffer)
  183. {
  184. v4l2_buf.m.planes[i].m.userptr =
  185. (unsigned long) shared_buffer->planes[i].data;
  186. v4l2_buf.m.planes[i].bytesused =
  187. shared_buffer->planes[i].bytesused;
  188. }
  189. else
  190. {
  191. v4l2_buf.m.planes[i].m.userptr =
  192. (unsigned long) buffer->planes[i].data;
  193. v4l2_buf.m.planes[i].bytesused =
  194. buffer->planes[i].bytesused;
  195. }
  196. }
  197. break;
  198. case V4L2_MEMORY_MMAP:
  199. for (i = 0; i < buffer->n_planes; i++)
  200. {
  201. v4l2_buf.m.planes[i].bytesused = buffer->planes[i].bytesused;
  202. }
  203. break;
  204. case V4L2_MEMORY_DMABUF:
  205. buffer->shared_buffer = shared_buffer;
  206. if (shared_buffer)
  207. {
  208. for (i = 0; i < buffer->n_planes; i++)
  209. {
  210. v4l2_buf.m.planes[i].m.fd = shared_buffer->planes[i].fd;
  211. v4l2_buf.m.planes[i].bytesused =
  212. shared_buffer->planes[i].bytesused;
  213. }
  214. }
  215. break;
  216. default:
  217. pthread_cond_broadcast(&plane_cond);
  218. pthread_mutex_unlock(&plane_lock);
  219. return -1;
  220. }
  221. if(buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  222. {
  223. v4l2elem_profiler.startProcessing();
  224. }
  225. ret = v4l2_ioctl(fd, VIDIOC_QBUF, &v4l2_buf);
  226. if (ret)
  227. {
  228. is_in_error = 1;
  229. PLANE_SYS_ERROR_MSG("Error while Qing buffer");
  230. }
  231. else
  232. {
  233. PLANE_DEBUG_MSG("Qed buffer " << v4l2_buf.index);
  234. pthread_cond_broadcast(&plane_cond);
  235. total_queued_buffers++;
  236. num_queued_buffers++;
  237. }
  238. pthread_mutex_unlock(&plane_lock);
  239. return ret;
  240. }
  241. int
  242. NvV4l2ElementPlane::mapOutputBuffers(struct v4l2_buffer &v4l2_buf, int dmabuff_fd)
  243. {
  244. int ret;
  245. uint32_t i;
  246. pthread_mutex_lock(&plane_lock);
  247. unsigned char *data;
  248. NvBufSurface *nvbuf_surf = 0;
  249. switch (memory_type)
  250. {
  251. case V4L2_MEMORY_DMABUF:
  252. ret = NvBufSurfaceFromFd (dmabuff_fd, (void**)(&nvbuf_surf));
  253. if(ret < 0)
  254. {
  255. PLANE_SYS_ERROR_MSG("Error: NvBufSurfaceFromFd Failed\n");
  256. pthread_mutex_unlock(&plane_lock);
  257. return ret;
  258. }
  259. for (i = 0; i < n_planes; i++)
  260. {
  261. buffers[v4l2_buf.index]->planes[i].fd = dmabuff_fd;
  262. v4l2_buf.m.planes[i].m.fd = buffers[v4l2_buf.index]->planes[i].fd;
  263. buffers[v4l2_buf.index]->planes[i].mem_offset = nvbuf_surf->surfaceList[0].planeParams.offset[i];
  264. ret = NvBufSurfaceMap(nvbuf_surf, 0, i, NVBUF_MAP_READ_WRITE);
  265. if (ret < 0)
  266. {
  267. is_in_error = 1;
  268. PLANE_SYS_ERROR_MSG("Error while Mapping buffer");
  269. pthread_mutex_unlock(&plane_lock);
  270. return ret;
  271. }
  272. data = (unsigned char *)nvbuf_surf->surfaceList[0].mappedAddr.addr[i];
  273. buffers[v4l2_buf.index]->planes[i].data=data;
  274. }
  275. break;
  276. default:
  277. pthread_mutex_unlock(&plane_lock);
  278. return -1;
  279. }
  280. if(ret == 0)
  281. {
  282. PLANE_DEBUG_MSG("Mapped Nvbuffer to buffers " << v4l2_buf.index);
  283. }
  284. pthread_mutex_unlock(&plane_lock);
  285. return ret;
  286. }
  287. int
  288. NvV4l2ElementPlane::unmapOutputBuffers(int index, int dmabuff_fd)
  289. {
  290. int ret = 0;
  291. uint32_t i;
  292. NvBufSurface *nvbuf_surf = 0;
  293. pthread_mutex_lock(&plane_lock);
  294. switch (memory_type)
  295. {
  296. case V4L2_MEMORY_DMABUF:
  297. for (i = 0; i < n_planes; i++)
  298. {
  299. ret = NvBufSurfaceFromFd (dmabuff_fd, (void**)(&nvbuf_surf));
  300. if (ret < 0)
  301. {
  302. is_in_error = 1;
  303. PLANE_SYS_ERROR_MSG("Error while NvBufSurfaceFromFd");
  304. pthread_mutex_unlock(&plane_lock);
  305. return ret;
  306. }
  307. ret = NvBufSurfaceUnMap(nvbuf_surf, 0, i);
  308. if (ret < 0)
  309. {
  310. is_in_error = 1;
  311. PLANE_SYS_ERROR_MSG("Error while Unmapping buffer");
  312. pthread_mutex_unlock(&plane_lock);
  313. return ret;
  314. }
  315. }
  316. break;
  317. default:
  318. pthread_mutex_unlock(&plane_lock);
  319. return -1;
  320. }
  321. if(ret == 0)
  322. {
  323. PLANE_DEBUG_MSG("Unmapped Nvbuffer to buffers " << index);
  324. }
  325. pthread_mutex_unlock(&plane_lock);
  326. return ret;
  327. }
  328. int
  329. NvV4l2ElementPlane::getFormat(struct v4l2_format &format)
  330. {
  331. format.type = buf_type;
  332. CHECK_V4L2_RETURN(v4l2_ioctl(fd, VIDIOC_G_FMT, &format),
  333. "Getting format");
  334. }
  335. int
  336. NvV4l2ElementPlane::setFormat(struct v4l2_format &format)
  337. {
  338. int ret;
  339. int j;
  340. format.type = buf_type;
  341. ret = v4l2_ioctl(fd, VIDIOC_S_FMT, &format);
  342. if (ret)
  343. {
  344. PLANE_SYS_ERROR_MSG("Error in VIDIOC_S_FMT");
  345. is_in_error = 1;
  346. }
  347. else
  348. {
  349. PLANE_DEBUG_MSG("VIDIOC_S_FMT at capture plane successful");
  350. n_planes = format.fmt.pix_mp.num_planes;
  351. for (j = 0; j < n_planes; j++)
  352. {
  353. planefmts[j].stride = format.fmt.pix_mp.plane_fmt[j].bytesperline;
  354. planefmts[j].sizeimage = format.fmt.pix_mp.plane_fmt[j].sizeimage;
  355. }
  356. }
  357. return ret;
  358. }
  359. int
  360. NvV4l2ElementPlane::getCrop(struct v4l2_crop & crop)
  361. {
  362. crop.type = buf_type;
  363. CHECK_V4L2_RETURN(v4l2_ioctl(fd, VIDIOC_G_CROP, &crop),
  364. "Getting crop params");
  365. }
  366. int
  367. NvV4l2ElementPlane::setSelection(uint32_t target, uint32_t flags, struct v4l2_rect & rect)
  368. {
  369. struct v4l2_selection select;
  370. switch (buf_type)
  371. {
  372. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  373. select.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  374. break;
  375. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  376. select.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  377. break;
  378. default:
  379. PLANE_ERROR_MSG("Unsupported v4l2_buf_type " << buf_type);
  380. return -1;
  381. }
  382. select.target = target;
  383. select.flags = flags;
  384. select.r = rect;
  385. CHECK_V4L2_RETURN(v4l2_ioctl(fd, VIDIOC_S_SELECTION, &select),
  386. "Setting selection");
  387. }
  388. void
  389. NvV4l2ElementPlane::setBufferPlaneFormat(int n_planes,
  390. NvBuffer::NvBufferPlaneFormat * planefmts)
  391. {
  392. int i;
  393. this->n_planes = n_planes;
  394. for (i = 0; i < n_planes; i++)
  395. {
  396. this->planefmts[i] = planefmts[i];
  397. }
  398. }
  399. void
  400. NvV4l2ElementPlane::deinitPlane()
  401. {
  402. setStreamStatus(false);
  403. waitForDQThread(-1);
  404. for (uint32_t i = 0; i < num_buffers; i++)
  405. {
  406. switch (memory_type)
  407. {
  408. case V4L2_MEMORY_USERPTR:
  409. buffers[i]->deallocateMemory();
  410. break;
  411. case V4L2_MEMORY_MMAP:
  412. buffers[i]->unmap();
  413. break;
  414. case V4L2_MEMORY_DMABUF:
  415. break;
  416. default:
  417. return;
  418. }
  419. }
  420. reqbufs(memory_type, 0);
  421. PLANE_DEBUG_MSG("deinit successful");
  422. }
  423. int
  424. NvV4l2ElementPlane::reqbufs(enum v4l2_memory mem_type, uint32_t num)
  425. {
  426. struct v4l2_requestbuffers reqbufs;
  427. int ret;
  428. memset(&reqbufs, 0, sizeof(struct v4l2_requestbuffers));
  429. reqbufs.count = num;
  430. reqbufs.type = buf_type;
  431. switch (mem_type)
  432. {
  433. case V4L2_MEMORY_USERPTR:
  434. for (uint32_t i = 0; i < n_planes; i++)
  435. {
  436. planefmts[i].stride =
  437. planefmts[i].width * planefmts[i].bytesperpixel;
  438. if(!planefmts[i].sizeimage)
  439. {
  440. planefmts[i].sizeimage =
  441. planefmts[i].width * planefmts[i].height;
  442. }
  443. }
  444. break;
  445. case V4L2_MEMORY_MMAP:
  446. case V4L2_MEMORY_DMABUF:
  447. break;
  448. default:
  449. PLANE_ERROR_MSG("Error in VIDIOC_REQBUFS:Unknown memory type " <<
  450. mem_type);
  451. return -1;
  452. }
  453. memory_type = mem_type;
  454. reqbufs.memory = mem_type;
  455. ret = v4l2_ioctl(fd, VIDIOC_REQBUFS, &reqbufs);
  456. if (ret)
  457. {
  458. PLANE_SYS_ERROR_MSG("Error in VIDIOC_REQBUFS at output plane");
  459. is_in_error = 1;
  460. }
  461. else
  462. {
  463. if (reqbufs.count)
  464. {
  465. buffers = new NvBuffer *[reqbufs.count];
  466. for (uint32_t i = 0; i < reqbufs.count; i++)
  467. {
  468. buffers[i] =
  469. new NvBuffer(buf_type, mem_type, n_planes, planefmts, i);
  470. }
  471. }
  472. else
  473. {
  474. for (uint32_t i = 0; i < num_buffers; i++)
  475. {
  476. delete buffers[i];
  477. }
  478. delete[] buffers;
  479. buffers = NULL;
  480. }
  481. num_buffers = reqbufs.count;
  482. PLANE_DEBUG_MSG("Reqbuf returned " << reqbufs.count << " buffers");
  483. }
  484. return ret;
  485. }
  486. int
  487. NvV4l2ElementPlane::setStreamStatus(bool status)
  488. {
  489. int ret;
  490. if (status == streamon)
  491. {
  492. PLANE_DEBUG_MSG("Already in " << ((status) ? "STREAMON" : "STREAMOFF"));
  493. return 0;
  494. }
  495. pthread_mutex_lock(&plane_lock);
  496. if (status)
  497. {
  498. ret = v4l2_ioctl(fd, VIDIOC_STREAMON, &buf_type);
  499. }
  500. else
  501. {
  502. ret = v4l2_ioctl(fd, VIDIOC_STREAMOFF, &buf_type);
  503. }
  504. if (ret)
  505. {
  506. PLANE_SYS_ERROR_MSG("Error in " <<
  507. ((status) ? "STREAMON" : "STREAMOFF"));
  508. is_in_error = 1;
  509. }
  510. else
  511. {
  512. PLANE_DEBUG_MSG(((status) ? "STREAMON" : "STREAMOFF") << " successful");
  513. streamon = status;
  514. if (!streamon)
  515. {
  516. num_queued_buffers = 0;
  517. pthread_cond_broadcast(&plane_cond);
  518. }
  519. if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  520. {
  521. if (status == false)
  522. {
  523. v4l2elem_profiler.disableProfiling();
  524. }
  525. }
  526. }
  527. pthread_mutex_unlock(&plane_lock);
  528. return ret;
  529. }
  530. bool
  531. NvV4l2ElementPlane::getStreamStatus()
  532. {
  533. return streamon;
  534. }
  535. int
  536. NvV4l2ElementPlane::setStreamParms(struct v4l2_streamparm &parm)
  537. {
  538. int ret;
  539. parm.type = buf_type;
  540. ret = v4l2_ioctl(fd, VIDIOC_S_PARM, &parm);
  541. if(ret == 0)
  542. {
  543. PLANE_DEBUG_MSG("Successfully set stream parameters");
  544. }
  545. else
  546. {
  547. PLANE_SYS_ERROR_MSG("Error while setting stream parameters");
  548. }
  549. return ret;
  550. }
  551. int
  552. NvV4l2ElementPlane::queryBuffer(uint32_t i)
  553. {
  554. struct v4l2_buffer v4l2_buf;
  555. struct v4l2_plane planes[MAX_PLANES];
  556. int ret;
  557. uint32_t j;
  558. memset(&v4l2_buf, 0, sizeof(struct v4l2_buffer));
  559. memset(planes, 0, sizeof(planes));
  560. v4l2_buf.index = i;
  561. v4l2_buf.type = buf_type;
  562. v4l2_buf.memory = memory_type;
  563. v4l2_buf.m.planes = planes;
  564. v4l2_buf.length = n_planes;
  565. ret = v4l2_ioctl(fd, VIDIOC_QUERYBUF, &v4l2_buf);
  566. if (ret)
  567. {
  568. PLANE_SYS_ERROR_MSG("Error in QueryBuf for " << i << "th buffer");
  569. is_in_error = 1;
  570. }
  571. else
  572. {
  573. PLANE_DEBUG_MSG("QueryBuf for " << i << "th buffer successful");
  574. for (j = 0; j < v4l2_buf.length; j++)
  575. {
  576. buffers[i]->planes[j].length = v4l2_buf.m.planes[j].length;
  577. buffers[i]->planes[j].mem_offset =
  578. v4l2_buf.m.planes[j].m.mem_offset;
  579. }
  580. }
  581. return ret;
  582. }
  583. int
  584. NvV4l2ElementPlane::exportBuffer(uint32_t i)
  585. {
  586. struct v4l2_exportbuffer expbuf;
  587. int ret;
  588. int j;
  589. memset(&expbuf, 0, sizeof(expbuf));
  590. expbuf.type = buf_type;
  591. expbuf.index = i;
  592. for (j = 0; j < n_planes; j++)
  593. {
  594. expbuf.plane = j;
  595. ret = v4l2_ioctl(fd, VIDIOC_EXPBUF, &expbuf);
  596. if (ret)
  597. {
  598. PLANE_SYS_ERROR_MSG("Error in ExportBuf for Buffer " << i <<
  599. ", Plane " << j);
  600. is_in_error = 1;
  601. return -1;
  602. }
  603. else
  604. {
  605. PLANE_DEBUG_MSG("ExportBuf successful for Buffer " << i <<
  606. ", Plane " << j << ", fd = " << expbuf.fd);
  607. buffers[i]->planes[j].fd = expbuf.fd;
  608. }
  609. }
  610. return 0;
  611. }
  612. int
  613. NvV4l2ElementPlane::setupPlane(enum v4l2_memory mem_type, uint32_t num_buffers,
  614. bool map, bool allocate)
  615. {
  616. uint32_t i;
  617. if (reqbufs(mem_type, num_buffers))
  618. {
  619. goto error;
  620. }
  621. for (i = 0; i < this->num_buffers; i++)
  622. {
  623. switch (mem_type)
  624. {
  625. case V4L2_MEMORY_USERPTR:
  626. if (allocate)
  627. {
  628. if (buffers[i]->allocateMemory())
  629. {
  630. goto error;
  631. }
  632. }
  633. break;
  634. case V4L2_MEMORY_MMAP:
  635. if (queryBuffer(i))
  636. {
  637. goto error;
  638. }
  639. if (exportBuffer(i))
  640. {
  641. goto error;
  642. }
  643. if (map)
  644. {
  645. if (buffers[i]->map())
  646. {
  647. goto error;
  648. }
  649. }
  650. break;
  651. default:
  652. continue;
  653. }
  654. }
  655. return 0;
  656. error:
  657. PLANE_ERROR_MSG("Error during setup");
  658. is_in_error = 1;
  659. deinitPlane();
  660. return -1;
  661. }
  662. int
  663. NvV4l2ElementPlane::waitAllBuffersQueued(uint32_t max_wait_ms)
  664. {
  665. struct timespec timeToWait;
  666. struct timeval now;
  667. int return_val = 0;
  668. int ret;
  669. gettimeofday(&now, NULL);
  670. timeToWait.tv_nsec = (now.tv_usec + (max_wait_ms % 1000) * 1000L) * 1000L;
  671. timeToWait.tv_sec = now.tv_sec + max_wait_ms / 1000 +
  672. timeToWait.tv_nsec / 1000000000L;
  673. timeToWait.tv_nsec = timeToWait.tv_nsec % 1000000000L;
  674. pthread_mutex_lock(&plane_lock);
  675. while (num_queued_buffers < num_buffers)
  676. {
  677. ret = pthread_cond_timedwait(&plane_cond, &plane_lock, &timeToWait);
  678. if (ret == ETIMEDOUT)
  679. {
  680. return_val = -1;
  681. break;
  682. }
  683. }
  684. pthread_mutex_unlock(&plane_lock);
  685. CHECK_V4L2_RETURN(return_val, "Waiting for all buffers to get queued");
  686. }
  687. int
  688. NvV4l2ElementPlane::waitAllBuffersDequeued(uint32_t max_wait_ms)
  689. {
  690. struct timespec timeToWait;
  691. struct timeval now;
  692. int return_val = 0;
  693. int ret;
  694. gettimeofday(&now, NULL);
  695. timeToWait.tv_nsec = (now.tv_usec + (max_wait_ms % 1000) * 1000L) * 1000L;
  696. timeToWait.tv_sec = now.tv_sec + max_wait_ms / 1000 +
  697. timeToWait.tv_nsec / 1000000000L;
  698. timeToWait.tv_nsec = timeToWait.tv_nsec % 1000000000L;
  699. pthread_mutex_lock(&plane_lock);
  700. while (num_queued_buffers)
  701. {
  702. ret = pthread_cond_timedwait(&plane_cond, &plane_lock, &timeToWait);
  703. if (ret == ETIMEDOUT)
  704. {
  705. return_val = -1;
  706. break;
  707. }
  708. }
  709. pthread_mutex_unlock(&plane_lock);
  710. CHECK_V4L2_RETURN(return_val, "Waiting for all buffers to get dequeued");
  711. }
  712. bool NvV4l2ElementPlane::setDQThreadCallback(dqThreadCallback callback)
  713. {
  714. if (dqthread_running)
  715. return false;
  716. this->callback = callback;
  717. return true;
  718. }
  719. void *
  720. NvV4l2ElementPlane::dqThread(void *data)
  721. {
  722. NvV4l2ElementPlane *plane = (NvV4l2ElementPlane *) data;
  723. const char *comp_name = plane->comp_name;
  724. const char *plane_name = plane->plane_name;
  725. PLANE_DEBUG_MSG("Starting DQthread");
  726. prctl (PR_SET_NAME, plane_name, 0, 0, 0);
  727. plane->stop_dqthread = false;
  728. while (!plane->stop_dqthread)
  729. {
  730. struct v4l2_buffer v4l2_buf;
  731. struct v4l2_plane planes[MAX_PLANES];
  732. NvBuffer *buffer;
  733. NvBuffer *shared_buffer;
  734. bool ret;
  735. memset(&v4l2_buf, 0, sizeof(v4l2_buf));
  736. memset(planes, 0, sizeof(planes));
  737. v4l2_buf.m.planes = planes;
  738. v4l2_buf.length = plane->n_planes;
  739. if (plane->dqBuffer(v4l2_buf, &buffer, &shared_buffer, -1) < 0)
  740. {
  741. if (errno != EAGAIN)
  742. {
  743. plane->is_in_error = 1;
  744. }
  745. if (errno != EAGAIN || plane->streamon)
  746. {
  747. ret = plane->callback(NULL, NULL, NULL, plane->dqThread_data);
  748. }
  749. if (!plane->streamon)
  750. {
  751. break;
  752. }
  753. }
  754. else
  755. {
  756. ret = plane->callback(&v4l2_buf, buffer, shared_buffer,
  757. plane->dqThread_data);
  758. }
  759. if (!ret)
  760. {
  761. break;
  762. }
  763. }
  764. plane->stop_dqthread = false;
  765. pthread_mutex_lock(&plane->plane_lock);
  766. plane->dqthread_running = false;
  767. pthread_cond_broadcast(&plane->plane_cond);
  768. pthread_mutex_unlock(&plane->plane_lock);
  769. PLANE_DEBUG_MSG("Exiting DQthread");
  770. return NULL;
  771. }
  772. int
  773. NvV4l2ElementPlane::startDQThread(void *data)
  774. {
  775. pthread_mutex_lock(&plane_lock);
  776. if (dqthread_running)
  777. {
  778. PLANE_DEBUG_MSG("DQ Thread already started");
  779. pthread_mutex_unlock(&plane_lock);
  780. return 0;
  781. }
  782. dqThread_data = data;
  783. pthread_create(&dq_thread, NULL, dqThread, this);
  784. dqthread_running = true;
  785. pthread_mutex_unlock(&plane_lock);
  786. PLANE_DEBUG_MSG("Started DQ Thread");
  787. return 0;
  788. }
  789. int
  790. NvV4l2ElementPlane::stopDQThread()
  791. {
  792. if (blocking)
  793. {
  794. PLANE_WARN_MSG("Should not be called in blocking mode");
  795. return 0;
  796. }
  797. stop_dqthread = true;
  798. pthread_join(dq_thread, NULL);
  799. dq_thread = 0;
  800. PLANE_DEBUG_MSG("Stopped DQ Thread");
  801. return 0;
  802. }
  803. int
  804. NvV4l2ElementPlane::waitForDQThread(uint32_t max_wait_ms)
  805. {
  806. struct timespec timeToWait;
  807. struct timeval now;
  808. int return_val = 0;
  809. int ret = 0;
  810. gettimeofday(&now, NULL);
  811. timeToWait.tv_nsec = (now.tv_usec + (max_wait_ms % 1000) * 1000L) * 1000L;
  812. timeToWait.tv_sec = now.tv_sec + max_wait_ms / 1000 +
  813. timeToWait.tv_nsec / 1000000000L;
  814. timeToWait.tv_nsec = timeToWait.tv_nsec % 1000000000L;
  815. pthread_mutex_lock(&plane_lock);
  816. while (dqthread_running)
  817. {
  818. ret = pthread_cond_timedwait(&plane_cond, &plane_lock, &timeToWait);
  819. if (ret == ETIMEDOUT)
  820. {
  821. return_val = -1;
  822. break;
  823. }
  824. }
  825. pthread_mutex_unlock(&plane_lock);
  826. if (ret == 0)
  827. {
  828. pthread_join(dq_thread, NULL);
  829. dq_thread = 0;
  830. PLANE_DEBUG_MSG("Stopped DQ Thread");
  831. }
  832. else
  833. {
  834. PLANE_ERROR_MSG("Timed out waiting for dqthread");
  835. }
  836. return return_val;
  837. }