[415] | 1 | //////////////////////////////////////////////////////////// |
---|
| 2 | // Copyright (C) Roman Ryltsov, 2015 |
---|
| 3 | // Created by Roman Ryltsov roman@alax.info |
---|
| 4 | |
---|
| 5 | #pragma once |
---|
| 6 | |
---|
| 7 | //#import "libid:B9EC374B-834B-4DA9-BFB5-C1872CE736FF" raw_interfaces_only // AlaxInfoDirectShowSpy |
---|
| 8 | //#import "libid:C8D585EA-23F9-4D3E-9727-23CD02ED96C7" raw_interfaces_only // AlaxInfoDirectShowTools |
---|
| 9 | #include "rodshow.h" |
---|
| 10 | #include "rodmo.h" |
---|
| 11 | |
---|
| 12 | #pragma region Windows SDK (qedit.h) |
---|
| 13 | |
---|
| 14 | struct __declspec(uuid("0579154a-2b53-4994-b0d0-e773148eff85")) |
---|
| 15 | ISampleGrabberCB : IUnknown |
---|
| 16 | { |
---|
| 17 | // |
---|
| 18 | // Raw methods provided by interface |
---|
| 19 | // |
---|
| 20 | |
---|
| 21 | virtual HRESULT __stdcall SampleCB ( |
---|
| 22 | double SampleTime, |
---|
| 23 | struct IMediaSample * pSample ) = 0; |
---|
| 24 | virtual HRESULT __stdcall BufferCB ( |
---|
| 25 | double SampleTime, |
---|
| 26 | unsigned char * pBuffer, |
---|
| 27 | long BufferLen ) = 0; |
---|
| 28 | }; |
---|
| 29 | |
---|
| 30 | struct __declspec(uuid("6b652fff-11fe-4fce-92ad-0266b5d7c78f")) |
---|
| 31 | ISampleGrabber : IUnknown |
---|
| 32 | { |
---|
| 33 | // |
---|
| 34 | // Raw methods provided by interface |
---|
| 35 | // |
---|
| 36 | |
---|
| 37 | virtual HRESULT __stdcall SetOneShot ( |
---|
| 38 | long OneShot ) = 0; |
---|
| 39 | virtual HRESULT __stdcall SetMediaType ( |
---|
| 40 | struct _AMMediaType * pType ) = 0; |
---|
| 41 | virtual HRESULT __stdcall GetConnectedMediaType ( |
---|
| 42 | struct _AMMediaType * pType ) = 0; |
---|
| 43 | virtual HRESULT __stdcall SetBufferSamples ( |
---|
| 44 | long BufferThem ) = 0; |
---|
| 45 | virtual HRESULT __stdcall GetCurrentBuffer ( |
---|
| 46 | /*[in,out]*/ long * pBufferSize, |
---|
| 47 | /*[out]*/ long * pBuffer ) = 0; |
---|
| 48 | virtual HRESULT __stdcall GetCurrentSample ( |
---|
| 49 | /*[out,retval]*/ struct IMediaSample * * ppSample ) = 0; |
---|
| 50 | virtual HRESULT __stdcall SetCallback ( |
---|
| 51 | struct ISampleGrabberCB * pCallback, |
---|
| 52 | long WhichMethodToCallback ) = 0; |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | struct __declspec(uuid("c1f400a0-3f08-11d3-9f0b-006008039e37")) |
---|
| 56 | SampleGrabber; |
---|
| 57 | // [ default ] interface ISampleGrabber |
---|
| 58 | |
---|
| 59 | struct __declspec(uuid("c1f400a4-3f08-11d3-9f0b-006008039e37")) |
---|
| 60 | NullRenderer; |
---|
| 61 | |
---|
| 62 | #pragma endregion |
---|
| 63 | |
---|
| 64 | //////////////////////////////////////////////////////////// |
---|
| 65 | // CModule |
---|
| 66 | |
---|
| 67 | class CModule : |
---|
| 68 | public CAtlExeModuleT<CModule> |
---|
| 69 | { |
---|
| 70 | public: |
---|
| 71 | |
---|
| 72 | public: |
---|
| 73 | // CModule |
---|
| 74 | CModule() |
---|
| 75 | { |
---|
| 76 | AtlTraceSetDefaultSettings(); |
---|
| 77 | _Z4_THIS(); |
---|
| 78 | _W(CExceptionFilter::Initialize()); |
---|
| 79 | } |
---|
| 80 | ~CModule() |
---|
| 81 | { |
---|
| 82 | _Z4_THIS(); |
---|
| 83 | CExceptionFilter::Terminate(); |
---|
| 84 | } |
---|
| 85 | HRESULT PreMessageLoop(INT nShowCommand) |
---|
| 86 | { |
---|
| 87 | const HRESULT nResult = __super::PreMessageLoop(nShowCommand); |
---|
| 88 | return SUCCEEDED(nResult) ? S_OK : nResult; |
---|
| 89 | } |
---|
| 90 | VOID RunMessageLoop() |
---|
| 91 | { |
---|
| 92 | CGenericFilterGraph FilterGraph; |
---|
| 93 | FilterGraph.CoCreateInstance(); |
---|
| 94 | CComPtr<ICaptureGraphBuilder2> pCaptureGraphBuilder2; |
---|
| 95 | __C(pCaptureGraphBuilder2.CoCreateInstance(CLSID_CaptureGraphBuilder2)); |
---|
| 96 | __C(pCaptureGraphBuilder2->SetFiltergraph(FilterGraph.m_pFilterGraph)); |
---|
| 97 | #pragma region Source |
---|
| 98 | static const LPCTSTR g_pszDisplayName = |
---|
| 99 | //_T("@device:sw:{860BB310-5D01-11D0-BD3B-00A0C911CE86}\\{44A8B5C7-13B6-4211-BD40-35B629D9E6DF}"); |
---|
| 100 | _T("@device:sw:{860BB310-5D01-11D0-BD3B-00A0C911CE86}\\{A8DA2ECB-DEF6-414D-8CE2-E651640DBA4F}-001"); |
---|
| 101 | CComPtr<IBaseFilter> pSourceBaseFilter; |
---|
| 102 | { |
---|
| 103 | CComPtr<IBindCtx> pBindCtx; |
---|
| 104 | __C(CreateBindCtx(0, &pBindCtx)); |
---|
| 105 | ULONG nCount; |
---|
| 106 | CComPtr<IMoniker> pMoniker; |
---|
| 107 | __C(MkParseDisplayName(pBindCtx, CStringW(g_pszDisplayName), &nCount, &pMoniker)); |
---|
| 108 | _A(pMoniker); |
---|
| 109 | __C(pMoniker->BindToObject(pBindCtx, NULL, __uuidof(IBaseFilter), (VOID**) &pSourceBaseFilter)); |
---|
| 110 | _A(pSourceBaseFilter); |
---|
| 111 | } |
---|
| 112 | __C(FilterGraph.AddFilter(pSourceBaseFilter, _T("Source"))); |
---|
| 113 | #pragma endregion |
---|
| 114 | #pragma region Sample Grabber |
---|
| 115 | CComPtr<IBaseFilter> pSampleGrabberBaseFilter; |
---|
| 116 | __C(pSampleGrabberBaseFilter.CoCreateInstance(__uuidof(SampleGrabber))); |
---|
| 117 | const CComQIPtr<ISampleGrabber> pSampleGrabber = pSampleGrabberBaseFilter; |
---|
| 118 | __D(pSampleGrabber, E_NOINTERFACE); |
---|
| 119 | AM_MEDIA_TYPE MediaType; |
---|
| 120 | ZeroMemory(&MediaType, sizeof MediaType); |
---|
| 121 | MediaType.majortype = MEDIATYPE_Video; |
---|
| 122 | MediaType.subtype = MEDIASUBTYPE_RGB24; |
---|
| 123 | __C(pSampleGrabber->SetMediaType(&MediaType)); |
---|
| 124 | __C(pSampleGrabber->SetOneShot(TRUE)); |
---|
| 125 | __C(pSampleGrabber->SetBufferSamples(TRUE)); |
---|
| 126 | __C(FilterGraph.AddFilter(pSampleGrabberBaseFilter, _T("Sample Grabber"))); |
---|
| 127 | #pragma endregion |
---|
| 128 | #pragma region Null Renderer |
---|
| 129 | CComPtr<IBaseFilter> pNullRendererBaseFilter; |
---|
| 130 | __C(pNullRendererBaseFilter.CoCreateInstance(__uuidof(NullRenderer))); |
---|
| 131 | __C(FilterGraph.AddFilter(pNullRendererBaseFilter, _T("Renderer"))); |
---|
| 132 | #pragma endregion |
---|
| 133 | __C(pCaptureGraphBuilder2->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, pSourceBaseFilter, pSampleGrabberBaseFilter, pNullRendererBaseFilter)); |
---|
| 134 | //AtlMessageBoxEx(GetActiveWindow(), _T("Break In"), _T("Debug"), MB_ICONINFORMATION | MB_OK); |
---|
| 135 | __C(FilterGraph.m_pMediaControl->Run()); |
---|
| 136 | LONG nEventCode; |
---|
| 137 | __C(FilterGraph.m_pMediaEventEx->WaitForCompletion(INFINITE, &nEventCode)); |
---|
| 138 | LONG nDataSize = 0; |
---|
| 139 | pSampleGrabber->GetCurrentBuffer(&nDataSize, NULL); |
---|
| 140 | _A(nDataSize); |
---|
| 141 | CHeapPtr<BYTE> pnData; |
---|
| 142 | __D(pnData.Allocate(nDataSize), E_OUTOFMEMORY); |
---|
| 143 | __C(pSampleGrabber->GetCurrentBuffer(&nDataSize, (LONG*) (BYTE*) pnData)); |
---|
| 144 | __C(pSampleGrabber->GetConnectedMediaType(&MediaType)); |
---|
| 145 | #pragma region Bitmap |
---|
| 146 | _A(MediaType.formattype == FORMAT_VideoInfo); |
---|
| 147 | VIDEOINFOHEADER& VideoInfoHeader = *((VIDEOINFOHEADER*) MediaType.pbFormat); |
---|
| 148 | _A((SIZE_T) nDataSize == (SIZE_T) VideoInfoHeader.bmiHeader.biSizeImage); |
---|
| 149 | CAtlFile File; |
---|
| 150 | __C(File.Create(Combine(GetPathDirectory(GetModulePath()), _T("Output.bmp")), GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS)); |
---|
| 151 | BITMAPFILEHEADER BitmapFileHeader; |
---|
| 152 | ZeroMemory(&BitmapFileHeader, sizeof BitmapFileHeader); |
---|
| 153 | BitmapFileHeader.bfType = 'MB'; |
---|
| 154 | BitmapFileHeader.bfOffBits = sizeof BitmapFileHeader + sizeof VideoInfoHeader.bmiHeader; |
---|
| 155 | BitmapFileHeader.bfSize = BitmapFileHeader.bfOffBits + nDataSize; |
---|
| 156 | __C(File.Write(&BitmapFileHeader, sizeof BitmapFileHeader)); |
---|
| 157 | __C(File.Write(&VideoInfoHeader.bmiHeader, sizeof VideoInfoHeader.bmiHeader)); |
---|
| 158 | __C(File.Write(pnData, nDataSize)); |
---|
| 159 | #pragma endregion |
---|
| 160 | _A(!MediaType.pUnk); |
---|
| 161 | CoTaskMemFree(MediaType.pbFormat); |
---|
| 162 | } |
---|
| 163 | }; |
---|
| 164 | |
---|