You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
6.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicMediaInput.h"
  2. #define LOG(X) m_pLog->Log("MediaInput", this, X)
  3. #define LOG_DEBUG(X) m_pLog->Log("MediaInput", this, _LOG_DEBUG, X)
  4. #define LOG_ERROR(X) m_pLog->Log("MediaInput", this, _LOG_ERROR, X)
  5. static int xioctl(int fh, int request, void * arg)
  6. {
  7. int r;
  8. do
  9. {
  10. r = ioctl(fh, request, arg);
  11. }
  12. while (-1 == r && EINTR == errno);
  13. return r;
  14. }
  15. // Constructor implementation
  16. basicMediaInput::basicMediaInput() {}
  17. // Destructor implementation
  18. basicMediaInput::~basicMediaInput()
  19. {
  20. m_eType = MT_UNDEFINED;
  21. memset(m_sMediaState.m_pInputName, 0, MAX_PATH_LEN);
  22. }
  23. basicMediaInput::basicMediaInput(_MediaTypes mediaType)
  24. {
  25. m_eType = mediaType;
  26. memset(m_sMediaState.m_pInputName, 0, MAX_PATH_LEN);
  27. }
  28. basicMediaInput::basicMediaInput(CSTR URI, _MediaTypes mediaType)
  29. {
  30. m_eType = mediaType;
  31. LOG_DEBUG(URI);
  32. if (URI == NULL)
  33. {
  34. memset(m_sMediaState.m_pInputName, 0, MAX_PATH_LEN);
  35. }
  36. else
  37. {
  38. strncpy(m_sMediaState.m_pInputName, URI, MAX_PATH_LEN);
  39. m_sMediaState.m_pInputName[MAX_PATH_LEN - 1] = 0;
  40. };
  41. }
  42. // TODO: Uncomment the copy constructor when you need it.
  43. // inline basicMediaInput::basicMediaInput(const basicMediaInput& src)
  44. //{
  45. // // TODO: copy
  46. //}
  47. // TODO: Uncomment the assignment operator when you need it.
  48. // inline basicMediaInput& basicMediaInput::operator=(const basicMediaInput& rhs)
  49. //{
  50. // if (this == &rhs) {
  51. // return *this;
  52. // }
  53. //
  54. // // TODO: assignment
  55. //
  56. // return *this;
  57. //}
  58. bool basicMediaInput::create(CSTR URI, _MediaTypes mediaType)
  59. {
  60. char mBuffer[CLOG];
  61. LOG_DEBUG(URI);
  62. if (URI == NULL)
  63. {
  64. return false;
  65. }
  66. strncpy(m_sMediaState.m_pInputName, URI, MAX_PATH_LEN);
  67. m_sMediaState.m_pInputName[MAX_PATH_LEN - 1] = 0;
  68. if (mediaType > MT_UNDEFINED)
  69. {
  70. m_eType |= mediaType;
  71. }
  72. else
  73. {
  74. if (strncmp(m_sMediaState.m_pInputName, "/dev/", 5) == 0)
  75. {
  76. LOG_DEBUG("Local Device");
  77. m_eType = MT_DEVICE;
  78. }
  79. else if (strncmp(m_sMediaState.m_pInputName, "rtsp://", 7) == 0)
  80. {
  81. m_eType = MT_NETWORK;
  82. LOG_DEBUG("Network RTSP Device");
  83. }
  84. else if (strncmp(m_sMediaState.m_pInputName, "http://", 7) == 0)
  85. {
  86. m_eType = MT_NETWORK;
  87. LOG_DEBUG("Network HTTP Device");
  88. }
  89. else
  90. {
  91. m_eType = MT_FILE;
  92. LOG_DEBUG("Local File");
  93. };
  94. };
  95. switch (m_eType)
  96. {
  97. case MT_DEVICE:
  98. {
  99. struct v4l2_capability cap;
  100. struct v4l2_cropcap cropcap;
  101. struct v4l2_crop crop;
  102. struct v4l2_format fmt;
  103. unsigned int min;
  104. struct stat st;
  105. if (-1 == stat(m_sMediaState.m_pInputName, &st))
  106. {
  107. sprintf(mBuffer, "Cannot identify '%s': %d, %s", m_sMediaState.m_pInputName, errno, strerror(errno));
  108. LOG_ERROR(mBuffer);
  109. return false;
  110. };
  111. if (! S_ISCHR(st.st_mode))
  112. {
  113. sprintf(mBuffer, "%s is no device", m_sMediaState.m_pInputName);
  114. LOG_ERROR(mBuffer);
  115. return false;
  116. };
  117. m_sMediaState.m_iFileDescriptor = open(m_sMediaState.m_pInputName, O_RDWR /* required */ | O_NONBLOCK, 0);
  118. if (-1 == m_sMediaState.m_iFileDescriptor)
  119. {
  120. sprintf(mBuffer, "Cannot open '%s': %d, %s", m_sMediaState.m_pInputName, errno, strerror(errno));
  121. LOG_ERROR(mBuffer);
  122. return false;
  123. }
  124. if (-1 == xioctl(m_sMediaState.m_iFileDescriptor, VIDIOC_QUERYCAP, &cap))
  125. {
  126. if (EINVAL == errno)
  127. {
  128. sprintf(mBuffer, "%s is no V4L2 device", m_sMediaState.m_pInputName);
  129. LOG_ERROR(mBuffer);
  130. }
  131. else
  132. {
  133. LOG_ERROR("VIDIOC_QUERYCAP");
  134. };
  135. close(m_sMediaState.m_iFileDescriptor);
  136. return false;
  137. };
  138. if (! (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
  139. {
  140. sprintf(mBuffer, "%s is no video capture device", m_sMediaState.m_pInputName);
  141. LOG_ERROR(mBuffer);
  142. close(m_sMediaState.m_iFileDescriptor);
  143. return false;
  144. }
  145. if (! (cap.capabilities & V4L2_CAP_READWRITE))
  146. {
  147. sprintf(mBuffer, "%s does not support read i/o", m_sMediaState.m_pInputName);
  148. LOG_ERROR(mBuffer);
  149. close(m_sMediaState.m_iFileDescriptor);
  150. return false;
  151. };
  152. if (! (cap.capabilities & V4L2_CAP_STREAMING))
  153. {
  154. sprintf(mBuffer, "%s does not support streaming i/o", m_sMediaState.m_pInputName);
  155. LOG_ERROR(mBuffer);
  156. close(m_sMediaState.m_iFileDescriptor);
  157. return false;
  158. };
  159. CLEAR(cropcap);
  160. cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  161. if (0 == xioctl(m_sMediaState.m_iFileDescriptor, VIDIOC_CROPCAP, &cropcap))
  162. {
  163. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  164. crop.c = cropcap.defrect; /* reset to default */
  165. if (-1 == xioctl(m_sMediaState.m_iFileDescriptor, VIDIOC_S_CROP, &crop))
  166. {
  167. switch (errno)
  168. {
  169. case EINVAL:
  170. /* Cropping not supported. */
  171. break;
  172. default:
  173. /* Errors ignored. */
  174. break;
  175. };
  176. };
  177. }
  178. else
  179. {
  180. /* Errors ignored. */
  181. };
  182. CLEAR(fmt);
  183. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  184. if (0)
  185. { // force_format ) {
  186. LOG("Set H264");
  187. fmt.fmt.pix.width = 640; // replace
  188. fmt.fmt.pix.height = 480; // replace
  189. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_H264; // replace
  190. fmt.fmt.pix.field = V4L2_FIELD_ANY;
  191. if (-1 == xioctl(m_sMediaState.m_iFileDescriptor, VIDIOC_S_FMT, &fmt))
  192. {
  193. LOG_ERROR("VIDIOC_S_FMT");
  194. close(m_sMediaState.m_iFileDescriptor);
  195. return false;
  196. };
  197. /* Note VIDIOC_S_FMT may change width and height. */
  198. }
  199. else
  200. {
  201. /* Preserve original settings as set by v4l2-ctl for example */
  202. if (-1 == xioctl(m_sMediaState.m_iFileDescriptor, VIDIOC_G_FMT, &fmt))
  203. {
  204. LOG_ERROR("VIDIOC_G_FMT");
  205. close(m_sMediaState.m_iFileDescriptor);
  206. return false;
  207. };
  208. };
  209. /* Buggy driver paranoia. */
  210. min = fmt.fmt.pix.width * 2;
  211. if (fmt.fmt.pix.bytesperline < min)
  212. {
  213. fmt.fmt.pix.bytesperline = min;
  214. }
  215. min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
  216. if (fmt.fmt.pix.sizeimage < min)
  217. {
  218. fmt.fmt.pix.sizeimage = min;
  219. }
  220. break;
  221. };
  222. case MT_NETWORK:
  223. {
  224. break;
  225. };
  226. case MT_FILE:
  227. {
  228. break;
  229. };
  230. }
  231. return true;
  232. }
  233. void * basicMediaInput::m_fStarterProc(void * arg) {}
  234. // http://www.boehmel.de/lanc.htm
  235. /*
  236. * This program displays the names of all files in the current directory.
  237. */
  238. #undef LOG
  239. #undef LOG_DEBUG
  240. #undef LOG_ERROR