StdAfx.cpp
Upload User: shining60
Upload Date: 2017-05-30
Package Size: 4714k
Code Size: 3k
Category:

Video Capture

Development Platform:

Visual C++

  1. // stdafx.cpp : source file that includes just the standard includes
  2. // capUsbVideo.pch will be the pre-compiled header
  3. // stdafx.obj will contain the pre-compiled type information
  4. //by edson
  5. //
  6. #include "stdafx.h"
  7. // TODO: reference any additional headers you need in STDAFX.H
  8. // and not in this file
  9. USB_VIDEO::USB_VIDEO(){
  10. pg = NULL; //graph builder
  11. pde = NULL; //device list
  12. pvde = NULL; //video Device enum
  13. pvd = NULL; //video device
  14. pvr = NULL; //base filter MJPG expected...
  15. pip = NULL; //input
  16. pop = NULL; //output
  17. pvc = NULL; //Video capturer
  18. pwc = NULL;
  19. }
  20. USB_VIDEO::~USB_VIDEO(){
  21. }
  22. HWND USB_VIDEO::gethandle(void){
  23. return 0;
  24. }
  25. void USB_VIDEO::StartDevice(HWND mwin){
  26. if (!mwin) return;
  27. myHwnd = mwin; //main window handle;
  28. Start();
  29. }
  30. BOOL USB_VIDEO::Start(void){
  31. HRESULT hr = S_OK;
  32. Started =  ((hr = CoInitialize(NULL)) == S_OK);
  33. if(Started){
  34. //graph builder... stage
  35. hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC,IID_IGraphBuilder, (void **)&pg);
  36. if(hr == S_OK){
  37. //video input device
  38. hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,IID_ICreateDevEnum, (void **)&pde);
  39. if (hr == S_OK){
  40. //get plug and play video devices...
  41. hr = pde->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,&pvde,0);
  42.    
  43. if (hr == S_OK){
  44. hr = pvde->Next(1,&pvd,0);
  45. if(hr == S_OK){
  46. hr = pvd->BindToObject(NULL,NULL,IID_IBaseFilter,(void **)&pvc);
  47. if(hr == S_OK){
  48. hr = pg->AddFilter(pvc,L"VideoCap");
  49. if(hr == S_OK){
  50. hr = CoCreateInstance(CLSID_VideoRenderer, NULL, CLSCTX_INPROC_SERVER, 
  51. IID_IBaseFilter, (LPVOID*) &pvr);
  52. if(hr == S_OK){
  53. hr = pg->AddFilter(pvr,L"VideoRenderer");
  54. if(hr == S_OK){
  55. //connect pins....
  56. hr = pg->QueryInterface(IID_IMediaControl, (void **)&pmc);
  57. hr = pg->QueryInterface(IID_IVideoWindow,(void **)&pwc);
  58. pwc->put_Owner((OAHWND)myHwnd);
  59. pwc->put_WindowStyle(WS_CHILD);
  60. RECT grc;
  61. GetClientRect(myHwnd, &grc);
  62. pwc->SetWindowPosition(0, 0, 100, 100);
  63. pwc->put_Visible(OATRUE);
  64.    
  65. if(hr == S_OK)
  66. MessageBox(0,"Consegui","teste",0);
  67. hr = pvc->EnumPins(&pep);
  68. if(hr == S_OK){
  69. hr = pep->Next(1,&pop,NULL);
  70. //video device output pin
  71. if(hr == S_OK){
  72. //find video render input pin
  73. hr = pvr->EnumPins(&pep);
  74. if(hr == S_OK){
  75. hr = pep->Next(1,&pip,NULL);
  76. if(hr == S_OK){
  77. pg->Connect(pop,pip); //connect output video device to
  78. //input video renderer;
  79. if(hr == S_OK){
  80. pmc->Run();
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. return Started;
  96. }
  97. BOOL USB_VIDEO::End(void){
  98. if(Started)
  99. pmc->Stop();
  100. if(pg){
  101. if (pvc)
  102. pg->RemoveFilter(pvc);
  103. if (pvr)
  104. pg->RemoveFilter(pvc);
  105. }
  106. if(pwc){
  107. pwc->put_Visible(OAFALSE);
  108. pwc->put_Owner(NULL); 
  109. }
  110. SECURE_RELEASE(pg); //graph builder
  111. SECURE_RELEASE(pde); //device list
  112. SECURE_RELEASE(pvde); //video Device enum
  113. SECURE_RELEASE(pvc); //video device
  114. SECURE_RELEASE(pvd); //video device
  115. SECURE_RELEASE(pvr); //base filter MJPG expected...
  116. SECURE_RELEASE(pip); //input
  117. SECURE_RELEASE(pop); //output
  118. //SECURE_RELEASE(pwi); //output
  119. CoUninitialize();
  120. return TRUE;
  121. }
  122. BOOL USB_VIDEO::isStarted(void){
  123. return Started;
  124. }