[574] | 1 | //////////////////////////////////////////////////////////// |
---|
| 2 | // Copyright (C) Roman Ryltsov, 2015 |
---|
| 3 | // Created by Roman Ryltsov roman@alax.info |
---|
| 4 | // |
---|
| 5 | // A permission to use the source code is granted as long as reference to |
---|
| 6 | // source website http://alax.info is retained. |
---|
| 7 | |
---|
| 8 | #include "stdafx.h" |
---|
| 9 | #include <dshow.h> |
---|
| 10 | #include <dshowasf.h> |
---|
| 11 | #include <wmsysprf.h> |
---|
| 12 | #import "libid:9E3ABA93-C8D8-41D3-B39E-29508FDE5757" raw_interfaces_only no_namespace // AlaxInfoReferenceSource |
---|
| 13 | |
---|
| 14 | #pragma comment(lib, "strmiids.lib") |
---|
| 15 | |
---|
| 16 | #define _A ATLASSERT |
---|
| 17 | #define __C ATLENSURE_SUCCEEDED |
---|
| 18 | |
---|
| 19 | CComPtr<IPin> GetFilterPin(IBaseFilter* pBaseFilter, SIZE_T nIndex = 0) |
---|
| 20 | { |
---|
| 21 | _A(pBaseFilter); |
---|
| 22 | CComPtr<IEnumPins> pEnumPins; |
---|
| 23 | __C(pBaseFilter->EnumPins(&pEnumPins)); |
---|
| 24 | for(; ; ) |
---|
| 25 | { |
---|
| 26 | CComPtr<IPin> pPin; |
---|
| 27 | if(pEnumPins->Next(1, &pPin, NULL) != S_OK) |
---|
| 28 | break; |
---|
| 29 | _A(pPin); |
---|
| 30 | if(!nIndex--) |
---|
| 31 | return pPin; |
---|
| 32 | } |
---|
| 33 | return NULL; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | int _tmain(int argc, _TCHAR* argv[]) |
---|
| 37 | { |
---|
| 38 | static const LPCTSTR g_pszPath = _T("D:\\Output.asf"); |
---|
| 39 | static const REFERENCE_TIME g_nDuration = 3 * 60 * 1000 * 10000i64; // 15 seconds |
---|
| 40 | _ATLTRY |
---|
| 41 | { |
---|
| 42 | __C(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)); |
---|
| 43 | _ATLTRY |
---|
| 44 | { |
---|
| 45 | CComPtr<IFilterGraph2> pFilterGraph; |
---|
| 46 | __C(pFilterGraph.CoCreateInstance(CLSID_FilterGraph)); |
---|
| 47 | // WARN: Make sure ReferenceSource.dll of matching bitness is registered with the system (or otherwise is COM visible/available) |
---|
| 48 | #pragma region Video |
---|
| 49 | CComPtr<IBaseFilter> pVideoBaseFilter; |
---|
| 50 | __C(pVideoBaseFilter.CoCreateInstance(__uuidof(VideoSourceFilter))); |
---|
| 51 | __C(pFilterGraph->AddFilter(pVideoBaseFilter, NULL)); |
---|
| 52 | { |
---|
| 53 | const CComQIPtr<IAMStreamConfig> pAmStreamConfig = GetFilterPin(pVideoBaseFilter); |
---|
| 54 | const CComQIPtr<IAMStreamControl> pAmStreamControl = pAmStreamConfig; |
---|
| 55 | _A(pAmStreamConfig && pAmStreamControl); |
---|
| 56 | VIDEOINFOHEADER VideoInfoHeader; |
---|
| 57 | ZeroMemory(&VideoInfoHeader, sizeof VideoInfoHeader); |
---|
| 58 | VideoInfoHeader.AvgTimePerFrame = 1001 * 10000i64 / 30; // 29.970 fps |
---|
| 59 | BITMAPINFOHEADER& BitmapInfoHeader = VideoInfoHeader.bmiHeader; |
---|
| 60 | BitmapInfoHeader.biSize = sizeof BitmapInfoHeader; |
---|
| 61 | BitmapInfoHeader.biWidth = 320; //1280; |
---|
| 62 | BitmapInfoHeader.biHeight = -240; //-720; // Important: video source is only capable to geneate top-to-bottom 32-bit RGBs due to Direct2D implementation details |
---|
| 63 | BitmapInfoHeader.biPlanes = 1; |
---|
| 64 | BitmapInfoHeader.biBitCount = 32; |
---|
| 65 | BitmapInfoHeader.biCompression = BI_RGB; |
---|
| 66 | BitmapInfoHeader.biSizeImage = abs(BitmapInfoHeader.biHeight) * BitmapInfoHeader.biWidth * (BitmapInfoHeader.biBitCount >> 3); |
---|
| 67 | AM_MEDIA_TYPE MediaType; |
---|
| 68 | ZeroMemory(&MediaType, sizeof MediaType); |
---|
| 69 | MediaType.majortype = MEDIATYPE_Video; |
---|
| 70 | MediaType.subtype = MEDIASUBTYPE_RGB32; |
---|
| 71 | MediaType.bFixedSizeSamples = TRUE; |
---|
| 72 | MediaType.bTemporalCompression = FALSE; |
---|
| 73 | MediaType.lSampleSize = VideoInfoHeader.bmiHeader.biSizeImage; |
---|
| 74 | MediaType.formattype = FORMAT_VideoInfo; |
---|
| 75 | MediaType.cbFormat = sizeof VideoInfoHeader; |
---|
| 76 | MediaType.pbFormat = (BYTE*) &VideoInfoHeader; |
---|
| 77 | __C(pAmStreamConfig ->SetFormat(&MediaType)); |
---|
| 78 | __C(pAmStreamControl->StopAt(&g_nDuration, FALSE, (DWORD_PTR) (IBaseFilter*) pVideoBaseFilter)); |
---|
| 79 | } |
---|
| 80 | #pragma endregion |
---|
| 81 | #pragma region Audio |
---|
| 82 | CComPtr<IBaseFilter> pAudioBaseFilter; |
---|
| 83 | __C(pAudioBaseFilter.CoCreateInstance(__uuidof(AudioSourceFilter))); |
---|
| 84 | __C(pFilterGraph->AddFilter(pAudioBaseFilter, NULL)); |
---|
| 85 | { |
---|
| 86 | const CComQIPtr<IAMStreamConfig> pAmStreamConfig = GetFilterPin(pAudioBaseFilter); |
---|
| 87 | const CComQIPtr<IAMStreamControl> pAmStreamControl = pAmStreamConfig; |
---|
| 88 | _A(pAmStreamConfig && pAmStreamControl); |
---|
| 89 | WAVEFORMATEX WaveFormatEx; |
---|
| 90 | ZeroMemory(&WaveFormatEx, sizeof WaveFormatEx); |
---|
| 91 | WaveFormatEx.wFormatTag = WAVE_FORMAT_PCM; |
---|
| 92 | WaveFormatEx.nChannels = 1; |
---|
| 93 | WaveFormatEx.nSamplesPerSec = 32000; |
---|
| 94 | WaveFormatEx.wBitsPerSample = 16; |
---|
| 95 | WaveFormatEx.nBlockAlign = WaveFormatEx.nChannels * (WaveFormatEx.wBitsPerSample >> 3); |
---|
| 96 | WaveFormatEx.nAvgBytesPerSec = WaveFormatEx.nSamplesPerSec * WaveFormatEx.nBlockAlign; |
---|
| 97 | AM_MEDIA_TYPE MediaType; |
---|
| 98 | ZeroMemory(&MediaType, sizeof MediaType); |
---|
| 99 | MediaType.majortype = MEDIATYPE_Audio; |
---|
| 100 | MediaType.subtype = MEDIASUBTYPE_PCM; |
---|
| 101 | MediaType.bFixedSizeSamples = TRUE; |
---|
| 102 | MediaType.bTemporalCompression = FALSE; |
---|
| 103 | MediaType.lSampleSize = WaveFormatEx.nBlockAlign; |
---|
| 104 | MediaType.formattype = FORMAT_WaveFormatEx; |
---|
| 105 | MediaType.cbFormat = sizeof WaveFormatEx; |
---|
| 106 | MediaType.pbFormat = (BYTE*) &WaveFormatEx; |
---|
| 107 | __C(pAmStreamConfig ->SetFormat(&MediaType)); |
---|
| 108 | __C(pAmStreamControl->StopAt(&g_nDuration, FALSE, (DWORD_PTR) (IBaseFilter*) pAudioBaseFilter)); |
---|
| 109 | } |
---|
| 110 | #pragma endregion |
---|
| 111 | #pragma region Renderer |
---|
| 112 | CComPtr<IBaseFilter> pRendererBaseFilter; |
---|
| 113 | __C(pRendererBaseFilter.CoCreateInstance(CLSID_WMAsfWriter)); |
---|
| 114 | __C(pFilterGraph->AddFilter(pRendererBaseFilter, NULL)); |
---|
| 115 | const CComQIPtr<IFileSinkFilter> pFileSinkFilter = pRendererBaseFilter; |
---|
| 116 | _A(pFileSinkFilter); |
---|
| 117 | __C(pFileSinkFilter->SetFileName(CStringW(g_pszPath), NULL)); |
---|
| 118 | const CComQIPtr<IConfigAsfWriter> pConfigAsfWriter = pRendererBaseFilter; |
---|
| 119 | _A(pConfigAsfWriter); |
---|
| 120 | // NOTE: System Profiles https://msdn.microsoft.com/en-us/library/windows/desktop/dd743748 |
---|
| 121 | // WARN: This sample using Windows Media 8 system profile for code brevity; the profile overrides video resolution and frame rate and |
---|
| 122 | // causes video scaling regardless of input video format |
---|
| 123 | __C(pConfigAsfWriter->ConfigureFilterUsingProfileGuid(WMProfile_V80_100Video)); |
---|
| 124 | __C(pFilterGraph->Connect(GetFilterPin(pAudioBaseFilter), GetFilterPin(pRendererBaseFilter, 0))); |
---|
| 125 | __C(pFilterGraph->Connect(GetFilterPin(pVideoBaseFilter), GetFilterPin(pRendererBaseFilter, 1))); |
---|
| 126 | #pragma endregion |
---|
| 127 | const CComQIPtr<IMediaFilter> pMediaFilter = pFilterGraph; |
---|
| 128 | const CComQIPtr<IMediaControl> pMediaControl = pFilterGraph; |
---|
| 129 | const CComQIPtr<IMediaEvent> pMediaEvent = pFilterGraph; |
---|
| 130 | _A(pMediaFilter && pMediaControl && pMediaEvent); |
---|
| 131 | __C(pMediaFilter->SetSyncSource(NULL)); |
---|
| 132 | __C(pMediaControl->Run()); |
---|
| 133 | #pragma region Run |
---|
| 134 | //BOOL bVideoStopped = !pVideoSourceBaseFilter, bAudioStopped = !pAudioSourceBaseFilter; |
---|
| 135 | for(BOOL bComplete = FALSE; !bComplete; ) |
---|
| 136 | { |
---|
| 137 | for(; !bComplete; ) |
---|
| 138 | { |
---|
| 139 | LONG nEventCode; |
---|
| 140 | LONG_PTR nParameter1, nParameter2; |
---|
| 141 | const HRESULT nGetEventResult = pMediaEvent->GetEvent(&nEventCode, &nParameter1, &nParameter2, 1000); // 1 second |
---|
| 142 | if(FAILED(nGetEventResult)) |
---|
| 143 | break; |
---|
| 144 | _ATLTRY |
---|
| 145 | { |
---|
| 146 | switch(nEventCode) |
---|
| 147 | { |
---|
| 148 | case EC_COMPLETE: |
---|
| 149 | bComplete = TRUE; |
---|
| 150 | _tprintf(_T("Completed.\n")); |
---|
| 151 | break; |
---|
| 152 | case EC_USERABORT: |
---|
| 153 | __C(E_ABORT); // Actually unexpected |
---|
| 154 | break; |
---|
| 155 | case EC_ERRORABORT: |
---|
| 156 | _A(FAILED((HRESULT) nParameter1)); |
---|
| 157 | __C((HRESULT) nParameter1); |
---|
| 158 | break; |
---|
| 159 | // NOTE: No need to handle EC_STREAM_CONTROL_ events since with stop time defined, the streaming is finite anyway and we get EC_COMPLETE |
---|
| 160 | // case EC_STREAM_CONTROL_STARTED: |
---|
| 161 | // break; |
---|
| 162 | // case EC_STREAM_CONTROL_STOPPED: |
---|
| 163 | // if(nParameter2 == (LONG_PTR) (IBaseFilter*) pVideoBaseFilter) |
---|
| 164 | // bVideoStopped = TRUE; |
---|
| 165 | // else if(nParameter2 == (LONG_PTR) (IBaseFilter*) pAudioBaseFilter) |
---|
| 166 | // bAudioStopped = TRUE; |
---|
| 167 | // else |
---|
| 168 | // _A(FALSE); |
---|
| 169 | // break; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | _ATLCATCHALL() |
---|
| 173 | { |
---|
| 174 | pMediaEvent->FreeEventParams(nEventCode, nParameter1, nParameter2); |
---|
| 175 | _ATLRETHROW; |
---|
| 176 | } |
---|
| 177 | pMediaEvent->FreeEventParams(nEventCode, nParameter1, nParameter2); |
---|
| 178 | //bComplete |= bVideoStopped && bAudioStopped; |
---|
| 179 | } |
---|
| 180 | //bComplete |= bVideoStopped && bAudioStopped; |
---|
| 181 | SwitchToThread(); |
---|
| 182 | } |
---|
| 183 | #pragma endregion |
---|
| 184 | __C(pMediaControl->Stop()); |
---|
| 185 | _tprintf(_T("Done.")); |
---|
| 186 | } |
---|
| 187 | _ATLCATCHALL() |
---|
| 188 | { |
---|
| 189 | CoUninitialize(); |
---|
| 190 | _ATLRETHROW; |
---|
| 191 | } |
---|
| 192 | CoUninitialize(); |
---|
| 193 | } |
---|
| 194 | _ATLCATCH(Exception) |
---|
| 195 | { |
---|
| 196 | _tprintf(_T("Fatal Error 0x%08X\n"), (HRESULT) Exception); |
---|
| 197 | } |
---|
| 198 | return 0; |
---|
| 199 | } |
---|
| 200 | |
---|