NvV4l2Element.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (c) 2016, 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 "NvV4l2Element.h"
  29. #include "NvLogging.h"
  30. #include <fcntl.h>
  31. #include <cstring>
  32. #include <errno.h>
  33. #include <libv4l2.h>
  34. #define CAT_NAME "V4l2Element"
  35. using namespace std;
  36. /*initialization of mutex for NvV4l2Element*/
  37. pthread_mutex_t initializer_mutex = PTHREAD_MUTEX_INITIALIZER;
  38. NvV4l2Element::NvV4l2Element(const char *comp_name, const char *dev_node, int flags, NvElementProfiler::ProfilerField fields)
  39. :NvElement(comp_name, fields),
  40. output_plane(V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, comp_name,
  41. fd, !(flags & O_NONBLOCK), profiler),
  42. capture_plane(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, comp_name,
  43. fd, !(flags & O_NONBLOCK), profiler)
  44. {
  45. struct v4l2_capability caps;
  46. int ret;
  47. app_data = NULL;
  48. output_plane_pixfmt = 0;
  49. capture_plane_pixfmt = 0;
  50. /*Synchronization issue of libv4l2 open source library fixing here,adding lock for that*/
  51. pthread_mutex_lock(&initializer_mutex);
  52. fd = v4l2_open(dev_node, flags | O_RDWR);
  53. if (fd == -1)
  54. {
  55. COMP_SYS_ERROR_MSG("Could not open device '" << dev_node << "'");
  56. is_in_error = 1;
  57. pthread_mutex_unlock(&initializer_mutex);
  58. return;
  59. }
  60. pthread_mutex_unlock(&initializer_mutex);
  61. COMP_DEBUG_MSG("Opened, fd = " << fd);
  62. ret = v4l2_ioctl(fd, VIDIOC_QUERYCAP, &caps);
  63. if (ret != 0)
  64. {
  65. COMP_SYS_ERROR_MSG("Error in VIDIOC_QUERYCAP");
  66. is_in_error = 1;
  67. return;
  68. }
  69. if (!(caps.capabilities & V4L2_CAP_VIDEO_M2M_MPLANE))
  70. {
  71. COMP_ERROR_MSG("Device does not support V4L2_CAP_VIDEO_M2M_MPLANE");
  72. is_in_error = 1;
  73. return;
  74. }
  75. }
  76. NvV4l2Element::~NvV4l2Element()
  77. {
  78. output_plane.deinitPlane();
  79. capture_plane.deinitPlane();
  80. if (fd != -1)
  81. {
  82. v4l2_close(fd);
  83. CAT_DEBUG_MSG("Device closed, fd = " << fd);
  84. }
  85. }
  86. int
  87. NvV4l2Element::dqEvent(struct v4l2_event &ev, uint32_t max_wait_ms)
  88. {
  89. int ret;
  90. do
  91. {
  92. ret = v4l2_ioctl(fd, VIDIOC_DQEVENT, &ev);
  93. if (ret == 0)
  94. {
  95. COMP_DEBUG_MSG("DQed event " << hex << ev.type << dec);
  96. }
  97. else if (errno != EAGAIN)
  98. {
  99. COMP_SYS_ERROR_MSG("Error while DQing event");
  100. break;
  101. }
  102. else if (max_wait_ms-- == 0)
  103. {
  104. COMP_WARN_MSG("Error while DQing event: Resource temporarily unavailable");
  105. break;
  106. }
  107. else
  108. {
  109. usleep(1000);
  110. }
  111. }
  112. while (ret && (output_plane.getStreamStatus() || capture_plane.getStreamStatus()));
  113. return ret;
  114. }
  115. int
  116. NvV4l2Element::setControl(uint32_t id, int32_t value)
  117. {
  118. struct v4l2_control ctl;
  119. int ret;
  120. ctl.id = id;
  121. ctl.value = value;
  122. ret = v4l2_ioctl(fd, VIDIOC_S_CTRL, &ctl);
  123. if (ret < 0)
  124. {
  125. COMP_SYS_ERROR_MSG("Error setting value " << value << " on control " <<
  126. id);
  127. }
  128. else
  129. {
  130. COMP_DEBUG_MSG("Set value " << value << " on control " << id);
  131. }
  132. return ret;
  133. }
  134. int
  135. NvV4l2Element::getControl(uint32_t id, int32_t &value)
  136. {
  137. struct v4l2_control ctl;
  138. int ret;
  139. ctl.id = id;
  140. ret = v4l2_ioctl(fd, VIDIOC_G_CTRL, &ctl);
  141. if (ret < 0)
  142. {
  143. COMP_SYS_ERROR_MSG("Error getting value of control " << id);
  144. }
  145. else
  146. {
  147. COMP_DEBUG_MSG("Got value " << ctl.value << " for control " << id);
  148. value = ctl.value;
  149. }
  150. return ret;
  151. }
  152. int
  153. NvV4l2Element::setExtControls(v4l2_ext_controls &ctl)
  154. {
  155. int ret;
  156. ret = v4l2_ioctl(fd, VIDIOC_S_EXT_CTRLS, &ctl);
  157. if (ret < 0)
  158. {
  159. COMP_SYS_ERROR_MSG("Error setting controls");
  160. }
  161. else
  162. {
  163. COMP_DEBUG_MSG("Set controls");
  164. }
  165. return ret;
  166. }
  167. int
  168. NvV4l2Element::getExtControls(v4l2_ext_controls &ctl)
  169. {
  170. int ret;
  171. ret = v4l2_ioctl(fd, VIDIOC_G_EXT_CTRLS, &ctl);
  172. if (ret < 0)
  173. {
  174. COMP_SYS_ERROR_MSG("Error getting value of controls");
  175. }
  176. else
  177. {
  178. COMP_DEBUG_MSG("Got controls");
  179. }
  180. return ret;
  181. }
  182. int
  183. NvV4l2Element::subscribeEvent(uint32_t type, uint32_t id, uint32_t flags)
  184. {
  185. struct v4l2_event_subscription sub;
  186. int ret;
  187. memset(&sub, 0, sizeof(struct v4l2_event_subscription));
  188. sub.type = type;
  189. sub.id = id;
  190. sub.flags = flags;
  191. ret = v4l2_ioctl(fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
  192. if (ret == 0)
  193. {
  194. COMP_DEBUG_MSG("Successfully subscribed to event " << type);
  195. }
  196. else
  197. {
  198. COMP_SYS_ERROR_MSG
  199. ("Error while subscribing to event " << type);
  200. }
  201. return ret;
  202. }
  203. int
  204. NvV4l2Element::abort()
  205. {
  206. int ret = 0;
  207. ret |= output_plane.setStreamStatus(false);
  208. ret |= capture_plane.setStreamStatus(false);
  209. return ret;
  210. }
  211. int
  212. NvV4l2Element::waitForIdle(uint32_t max_wait_ms)
  213. {
  214. COMP_ERROR_MSG("wait_for_idle not implemented");
  215. return 0;
  216. }
  217. int
  218. NvV4l2Element::isInError()
  219. {
  220. int error = is_in_error;
  221. error |= capture_plane.isInError();
  222. error |= output_plane.isInError();
  223. return error;
  224. }
  225. void
  226. NvV4l2Element::enableProfiling()
  227. {
  228. if (output_plane_pixfmt || capture_plane_pixfmt)
  229. {
  230. COMP_ERROR_MSG("Profiling must be enabled before setting formats on either plane");
  231. return;
  232. }
  233. profiler.enableProfiling(true);
  234. }