source: trunk/Utilities/DirectShowReferenceSource/Sample/MfGenerate/Application.cpp @ 637

Last change on this file since 637 was 637, checked in by roman, 8 years ago
File size: 4.8 KB
RevLine 
[636]1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2015-2016
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 "romf.h"
10#import "libid:9E3ABA93-C8D8-41D3-B39E-29508FDE5757" raw_interfaces_only // AlaxInfoDirectShowReferenceSource
11
12////////////////////////////////////////////////////////////
13// CModule
14
15class CModule :
16        public CAtlExeModuleT<CModule>
17{
18public:
19// CModule
20        bool ParseCommandLine(LPCTSTR pszCommandLine, HRESULT* pnResult)
21        {
22                _A(pnResult);
23                pszCommandLine;
24                *pnResult = S_OK;
25                return true;
26        }
27        HRESULT PreMessageLoop(int nShowCommand)
28        {
29                // NOTE: Suppress S_FALSE
30                _C(__super::PreMessageLoop(nShowCommand));
31                return S_OK;
32        }
33        VOID RunMessageLoop()
34        {
35                MF::CStartup Statup;
36                CComPtr<IMFSourceResolver> pSourceResolver;
37                __C(MFCreateSourceResolver(&pSourceResolver));
38                #pragma region Media Source
39                CComQIPtr<IMFMediaSource> pMediaSource;
40                //{
41                //      static const LPCTSTR g_pszPath =
42                //              _T("D:\\Projects\\Alax.Info\\Repository-Private\\Utilities\\DirectShow\\ReferenceSource\\_Media\\720p50-Small-V.mp4");
43                //      MF_OBJECT_TYPE ObjectType;
44                //      CComPtr<IUnknown> pMediaSourceUnknown;
45                //      __C(pSourceResolver->CreateObjectFromURL(CStringW(g_pszPath), MF_RESOLUTION_MEDIASOURCE, NULL, &ObjectType, &pMediaSourceUnknown));
46                //      _A(ObjectType == MF_OBJECT_MEDIASOURCE);
47                //      pMediaSource = pMediaSourceUnknown;
48                //      __D(pMediaSource, E_NOINTERFACE);
49                //}
50                {
51                        using namespace AlaxInfoDirectShowReferenceSource;
52                        CComPtr<IVideoMediaSource> pVideoMediaSource;
53                        __C(pVideoMediaSource.CoCreateInstance(__uuidof(VideoMediaSource)));
[637]54                        __C(pVideoMediaSource->SetMediaType(4096, 2304, CComVariant(_PersistHelper::StringFromIdentifier(MEDIASUBTYPE_RGB32))));
[636]55                        //__C(pVideoMediaSource->SetMediaTypeAspectRatio(...));
56                        __C(pVideoMediaSource->SetMediaTypeRate(50, 1));
[637]57                        __C(pVideoMediaSource->put_Duration(5.0));
[636]58                        pMediaSource = pVideoMediaSource;
59                }
60                #pragma endregion
61                CComPtr<IMFSourceReader> pSourceReader;
62                __C(MFCreateSourceReaderFromMediaSource(pMediaSource, NULL, &pSourceReader));
63                MF::CMediaType pMediaType;
64                __C(pSourceReader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, &pMediaType));
65                pMediaType.Trace();
66                SYSTEMTIME Time;
67                GetLocalTime(&Time);
68                const CString sFileName = AtlFormatString(_T("%04d%02d%02d-%02d%02d%02d.mp4"), Time.wYear, Time.wMonth, Time.wDay, Time.wHour, Time.wMinute, Time.wSecond);
69                CComPtr<IMFSinkWriter> pSinkWriter;
70                __C(MFCreateSinkWriterFromURL(Combine(GetPathDirectory(GetModulePath()), sFileName), NULL, NULL, &pSinkWriter));
71                MF::CMediaType pWriterMediaType;
72                pWriterMediaType.Create();
73                // NOTE: H.264 Video Encoder https://msdn.microsoft.com/en-us/library/windows/desktop/dd797816
74                pWriterMediaType[MF_MT_MAJOR_TYPE] = MFMediaType_Video;
75                pWriterMediaType[MF_MT_SUBTYPE] = MFVideoFormat_H264;
[637]76                pWriterMediaType[MF_MT_AVG_BITRATE] = (UINT32) (12 << 10) * 1000; // 12 MBps
[636]77                pWriterMediaType[MF_MT_FRAME_RATE] = pMediaType.GetUINT64(MF_MT_FRAME_RATE);
78                pWriterMediaType[MF_MT_FRAME_SIZE] = pMediaType.GetUINT64(MF_MT_FRAME_SIZE);
79                pWriterMediaType[MF_MT_INTERLACE_MODE] = pMediaType.GetUINT32(MF_MT_INTERLACE_MODE);
80                pWriterMediaType[MF_MT_PIXEL_ASPECT_RATIO] = pMediaType.GetUINT64(MF_MT_PIXEL_ASPECT_RATIO);
81                DWORD nWriterStreamIndex;
82                __C(pSinkWriter->AddStream(pWriterMediaType, &nWriterStreamIndex));
[637]83                // NOTE: Sink Writer Attributes https://msdn.microsoft.com/en-us/library/windows/desktop/dd389284
84                {
85                        MF::CAttributes pAttributes;
86                        pAttributes.Create(10);
87                        pAttributes[MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS] = (UINT32) 1;
88                        __C(pSinkWriter->SetInputMediaType(nWriterStreamIndex, pMediaType, pAttributes));
89                }
[636]90                __C(pSinkWriter->BeginWriting());
91                for(; ; )
92                {
93                        DWORD nStreamIndex, nStreamFlags;
94                        LONGLONG nTime;
95                        MF::CSample pSample;
96                        __C(pSourceReader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, &nStreamIndex, &nStreamFlags, &nTime, &pSample));
97                        _Z4(atlTraceGeneral, 4, _T("nStreamIndex %d, nStreamFlags %s, nTime %s, pSample 0x%p\n"), nStreamIndex, MF::FormatSourceReaderStreamFlags(nStreamFlags), MF::FormatNanoTime(nTime), pSample);
98                        if(pSample)
99                        {
100                                pSample.Trace();
101                                __C(pSinkWriter->WriteSample(nWriterStreamIndex, pSample));
102                        }
103                        if(nStreamFlags & MF_SOURCE_READERF_ENDOFSTREAM)
104                        {
105                                _A(!pSample);
106                                break;
107                        }
108                }
109                __C(pSourceReader->Flush(MF_SOURCE_READER_ALL_STREAMS));
110                __C(pSinkWriter->Finalize());
111        }
112};
113
114int _tmain(int argc, _TCHAR* argv[])
115{
116        _ATLTRY
117        {
118                CModule Module;
119                Module.WinMain(SW_SHOWNORMAL);
120        }
121        _ATLCATCH(Exception)
122        {
123                _tprintf(_T("Fatal Error 0x%08X\n"), (HRESULT) Exception);
124        }
125        return 0;
126}
127
Note: See TracBrowser for help on using the repository browser.