VideoCapture.cpp
上传用户:panstart
上传日期:2022-04-12
资源大小:199k
文件大小:6k
源码类别:

IP电话/视频会议

开发平台:

C++ Builder

  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. //
  4. //    Project     : VideoNet version 1.1.
  5. //    Description : Peer to Peer Video Conferencing over the LAN.
  6. //   Author      : Nagareshwar Y Talekar ( nsry2002@yahoo.co.in)
  7. //    Date        : 15-6-2004.
  8. //
  9. //
  10. //    File description : 
  11. //    Name    :  VideoCapture.cpp
  12. //    Details :  Captures the frames from webcam.
  13. //
  14. //
  15. /////////////////////////////////////////////////////////////////////////////
  16. #include "Stdafx.h"
  17. #include "VideoNet.h"
  18. #include "VideoNetDlg.h"
  19. #include "VideoCapture.h"
  20. #include <afxmt.h>
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26. #pragma comment(lib,"vfw32")
  27. #pragma comment(lib,"winmm")
  28. //////////////////////////////////////////////////////////////////////
  29. // Construction/Destruction
  30. //////////////////////////////////////////////////////////////////////
  31. VideoCapture::VideoCapture()
  32. {
  33. m_capwnd=NULL;
  34. isOver=FALSE;
  35. log.Open("videocapture.log",CFile::modeCreate | CFile::modeWrite);
  36. }
  37. VideoCapture::~VideoCapture()
  38. {
  39. }
  40. void VideoCapture::SetDialog(CDialog *dialog)
  41. {
  42. dlg=dialog;
  43. }
  44. BOOL VideoCapture::Initialize()
  45. {
  46. char devname[100],devversion[100];
  47. char str[200];
  48. int index=0;
  49. m_capwnd = capCreateCaptureWindow("Capture",WS_POPUP,0,0,1,1,0,0);
  50. if(m_capwnd==NULL)
  51. {
  52. log.WriteString("n Unable to create capture window");
  53. return FALSE;
  54. }
  55. //connect callback functions
  56. capSetUserData(m_capwnd,this);
  57. //Change destroy functions also........
  58.      capSetCallbackOnVideoStream(m_capwnd,OnCaptureVideo);
  59.  
  60. capGetDriverDescription(index,devname,100,devversion,100);
  61. sprintf(str,"n Driver name = %s version = %s ",devname,devversion);
  62. log.WriteString(str);
  63. // Connect to webcam driver
  64. if( ! capDriverConnect(m_capwnd,index) )
  65. {
  66. // Device may be open already or it may not have been
  67. // closed properly last time.
  68. AfxMessageBox("Unable to open Video Capture Device");
  69. log.WriteString("n Unable to connect driver to the window");
  70. m_capwnd=NULL;
  71. return FALSE;
  72. }
  73. // Set the capture parameters
  74. if(SetCapturePara()==FALSE)
  75. {
  76. log.WriteString("n Setting capture parameters failed");
  77. capDriverDisconnect(m_capwnd);
  78. return FALSE;
  79. }
  80. return TRUE;
  81. }
  82. /**
  83. *   Start capturing frames from webcam
  84. *
  85. */
  86. BOOL VideoCapture::StartCapture()
  87. {
  88. // Start live capturing ...
  89. if(capCaptureSequenceNoFile(m_capwnd)==FALSE)
  90. {
  91. log.WriteString("n Failed to capture Sequence ..");
  92. return FALSE;
  93. }
  94. return TRUE;
  95. }
  96. /**
  97. *   Stop the capturing process
  98. *
  99. */
  100. BOOL VideoCapture::StopCapture()
  101. {
  102. capCaptureStop(m_capwnd);
  103. capCaptureAbort(m_capwnd);
  104. Sleep(500);
  105. return TRUE;
  106. }
  107. /**
  108. *   Stop the catpure process and disconnect the driver
  109. *
  110. */
  111. void VideoCapture::Destroy()
  112. {
  113. if(m_capwnd==NULL) return;
  114.     
  115.     // Stop the capturing process    
  116. capCaptureAbort(m_capwnd);
  117.           
  118.     // Disable the callback function..
  119. capSetCallbackOnVideoStream(m_capwnd, NULL);
  120.      
  121. Sleep(300); // This delay is important...
  122. // Finally disconnect the driver
  123.     capDriverDisconnect(m_capwnd);
  124. }
  125. /**
  126. *
  127. *     Set various capture parameters...
  128. *
  129. */
  130. BOOL  VideoCapture::SetCapturePara()
  131. {
  132. CAPTUREPARMS CapParms={0};
  133. capCaptureGetSetup(m_capwnd,&CapParms,sizeof(CapParms));
  134. CapParms.fAbortLeftMouse = FALSE;
  135. CapParms.fAbortRightMouse = FALSE;
  136. CapParms.fYield = TRUE;
  137. CapParms.fCaptureAudio = FALSE;
  138. CapParms.wPercentDropForError = 50;
  139. if(capCaptureSetSetup(m_capwnd,&CapParms,sizeof(CapParms))==FALSE)
  140. {
  141. log.WriteString("n Failed to set the capture parameters ");
  142. return FALSE;
  143. }
  144. // Set Video Format 
  145. capGetVideoFormat(m_capwnd,&m_bmpinfo,sizeof(m_bmpinfo));
  146. m_bmpinfo.bmiHeader.biWidth=IMAGE_WIDTH;
  147. m_bmpinfo.bmiHeader.biHeight=IMAGE_HEIGHT;
  148. BOOL ret=capSetVideoFormat(m_capwnd,&m_bmpinfo,sizeof(m_bmpinfo));
  149. if(ret==TRUE)
  150. log.WriteString("n Video parameters set properly");
  151. return TRUE;
  152. }
  153. /**
  154. *
  155. *  Allocate Memory for DIB image buffer
  156. */
  157. int VideoCapture::AllocateMemory(PBITMAPINFO &bmpinfo)
  158. {
  159. int size1,size2,size;
  160. BITMAPINFO tbmp;
  161. char  str[200];
  162. capGetVideoFormat(m_capwnd,&tbmp,sizeof(tbmp));
  163. size1 = getFormatSize ( tbmp );
  164. size2 = getImageSize ( tbmp );
  165. size = size1 + size2;
  166. sprintf(str,"n Formatsize = %d imagesize = %d , fun_size = %d ",
  167.  size1,size2, capGetVideoFormatSize(m_capwnd));
  168. log.WriteString(str);
  169. bmpinfo=(BITMAPINFO *) new BYTE[size];
  170. if(bmpinfo==NULL)
  171. {
  172. AfxMessageBox("Unable to allocate memory");
  173.     return -1;
  174. }
  175. memset(bmpinfo,0,sizeof(*bmpinfo));
  176. capGetVideoFormat(m_capwnd,bmpinfo,sizeof(*bmpinfo));
  177. return size1;
  178. }
  179. /**
  180. *
  181. *   Calculates the Format Size for DIB image
  182. *
  183. */
  184. int VideoCapture::getFormatSize(BITMAPINFO bmp)
  185. {
  186. int size;
  187. size=(bmp.bmiHeader.biSize!=0)?bmp.bmiHeader.biSize :sizeof(BITMAPINFOHEADER);
  188. //return (size+ bmp.bmiHeader.biClrUsed *sizeof(RGBQUAD));
  189. return size; //RGBQUAD is absent for 24 bit bmp image.
  190. }
  191. /**
  192. * Calculates the Size of Image
  193. *
  194. */
  195. int VideoCapture::getImageSize(BITMAPINFO bmp)
  196. {
  197. int size;
  198. BITMAPINFOHEADER head=bmp.bmiHeader;
  199. if( head.biSizeImage==0 )
  200. {
  201. size=( head.biWidth * head.biHeight * head.biBitCount)/8;
  202. }
  203. else
  204. size = head.biSizeImage;
  205. return size;
  206. }
  207. /**
  208. *    Invoked when the video frame is captured by the driver
  209. *
  210. *
  211. */
  212. LRESULT CALLBACK OnCaptureVideo(HWND mwnd,LPVIDEOHDR lphdr)
  213. {
  214. VideoCapture *vidcap=(VideoCapture *)capGetUserData(mwnd);
  215.    if(vidcap!=NULL )
  216.    ((CVideoNetDlg*) (vidcap->dlg))->SendVideo(lphdr->lpData,lphdr->dwBytesUsed);
  217.  
  218. return TRUE;
  219. }