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.

235 lines
6.7 KiB

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