Guest User

Video Streaming

a guest
Mar 8th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.08 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <mfapi.h>
  3. #include <mfidl.h>
  4. #include <Mfreadwrite.h>
  5. #include <mferror.h>
  6. #include <d3d9.h>
  7.  
  8. #pragma comment(lib, "mfreadwrite")
  9. #pragma comment(lib, "mfplat")
  10. #pragma comment(lib, "mfuuid")
  11. #pragma comment(lib, "d3d9.lib")
  12.  
  13. template <class T> void SafeRelease(T **ppT) {
  14.  
  15.     if (*ppT) {
  16.         (*ppT)->Release();
  17.         *ppT = NULL;
  18.     }
  19. }
  20.  
  21. #define REVERSE_IMAGE
  22.  
  23. // Format constants
  24. const UINT32 VIDEO_FPS = 19;
  25. const UINT64 VIDEO_FRAME_DURATION = 10 * 1000 * 1000 / VIDEO_FPS;
  26. const UINT32 VIDEO_BIT_RATE = 3000000;
  27. const GUID   VIDEO_ENCODING_FORMAT = MFVideoFormat_H264;
  28. const GUID   VIDEO_INPUT_FORMAT = MFVideoFormat_RGB32;
  29. const UINT32 VIDEO_FRAME_COUNT = 60 * VIDEO_FPS;
  30.  
  31. HRESULT InitializeDirect3D9(IDirect3DDevice9** ppDevice, IDirect3DSurface9** ppSurface, UINT32& uiWidth, UINT32& uiHeight) {
  32.  
  33.     IDirect3D9* d3d = NULL;
  34.  
  35.     d3d = Direct3DCreate9(D3D_SDK_VERSION);
  36.  
  37.     if (d3d == NULL)
  38.         return E_POINTER;
  39.  
  40.     D3DDISPLAYMODE mode;
  41.     HRESULT hr = d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode);
  42.  
  43.     if (FAILED(hr)) {
  44.         SafeRelease(&d3d);
  45.         return hr;
  46.     }
  47.  
  48.     D3DPRESENT_PARAMETERS parameters = { 0 };
  49.  
  50.     parameters.Windowed = TRUE;
  51.     parameters.BackBufferCount = 1;
  52.     uiHeight = parameters.BackBufferHeight = mode.Height;
  53.     uiWidth = parameters.BackBufferWidth = mode.Width;
  54.     parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
  55.     parameters.hDeviceWindow = NULL;
  56.  
  57.     hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &parameters, ppDevice);
  58.  
  59.     if (FAILED(hr)) {
  60.         SafeRelease(&d3d);
  61.         return hr;
  62.     }
  63.  
  64.     hr = (*ppDevice)->CreateOffscreenPlainSurface(mode.Width, mode.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, ppSurface, nullptr);
  65.  
  66.     SafeRelease(&d3d);
  67.  
  68.     return hr;
  69. }
  70.  
  71. HRESULT InitializeSinkWriter(IMFSinkWriter **ppWriter, DWORD *pStreamIndex, const UINT32 uiWidth, const UINT32 uiHeight) {
  72.  
  73.     *ppWriter = NULL;
  74.     *pStreamIndex = NULL;
  75.  
  76.     IMFSinkWriter   *pSinkWriter = NULL;
  77.     IMFMediaType    *pMediaTypeOut = NULL;
  78.     IMFMediaType    *pMediaTypeIn = NULL;
  79.     DWORD           streamIndex;
  80.  
  81.     HRESULT hr = MFCreateSinkWriterFromURL(L"output.mp4", NULL, NULL, &pSinkWriter);
  82.  
  83.     // Set the output media type.
  84.     if (SUCCEEDED(hr)) {
  85.         hr = MFCreateMediaType(&pMediaTypeOut);
  86.     }
  87.     if (SUCCEEDED(hr)) {
  88.         hr = pMediaTypeOut->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
  89.     }
  90.     if (SUCCEEDED(hr)) {
  91.         hr = pMediaTypeOut->SetGUID(MF_MT_SUBTYPE, VIDEO_ENCODING_FORMAT);
  92.     }
  93.     if (SUCCEEDED(hr)) {
  94.         hr = pMediaTypeOut->SetUINT32(MF_MT_AVG_BITRATE, VIDEO_BIT_RATE);
  95.     }
  96.     if (SUCCEEDED(hr)) {
  97.         hr = pMediaTypeOut->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
  98.     }
  99.     if (SUCCEEDED(hr)) {
  100.         hr = MFSetAttributeSize(pMediaTypeOut, MF_MT_FRAME_SIZE, uiWidth, uiHeight);
  101.     }
  102.     if (SUCCEEDED(hr)) {
  103.         hr = MFSetAttributeRatio(pMediaTypeOut, MF_MT_FRAME_RATE, VIDEO_FPS, 1);
  104.     }
  105.     if (SUCCEEDED(hr)) {
  106.         hr = MFSetAttributeRatio(pMediaTypeOut, MF_MT_PIXEL_ASPECT_RATIO, 1, 1);
  107.     }
  108.     if (SUCCEEDED(hr)) {
  109.         hr = pSinkWriter->AddStream(pMediaTypeOut, &streamIndex);
  110.     }
  111.  
  112.     // Set the input media type.
  113.     if (SUCCEEDED(hr)) {
  114.         hr = MFCreateMediaType(&pMediaTypeIn);
  115.     }
  116.     if (SUCCEEDED(hr)) {
  117.         hr = pMediaTypeIn->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
  118.     }
  119.     if (SUCCEEDED(hr)) {
  120.         hr = pMediaTypeIn->SetGUID(MF_MT_SUBTYPE, VIDEO_INPUT_FORMAT);
  121.     }
  122.     if (SUCCEEDED(hr)) {
  123.         hr = pMediaTypeIn->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
  124.     }
  125.     if (SUCCEEDED(hr)) {
  126.         hr = MFSetAttributeSize(pMediaTypeIn, MF_MT_FRAME_SIZE, uiWidth, uiHeight);
  127.     }
  128.     if (SUCCEEDED(hr)) {
  129.         hr = MFSetAttributeRatio(pMediaTypeIn, MF_MT_FRAME_RATE, VIDEO_FPS, 1);
  130.     }
  131.     if (SUCCEEDED(hr)) {
  132.         hr = MFSetAttributeRatio(pMediaTypeIn, MF_MT_PIXEL_ASPECT_RATIO, 1, 1);
  133.     }
  134.     if (SUCCEEDED(hr)) {
  135.         hr = pSinkWriter->SetInputMediaType(streamIndex, pMediaTypeIn, NULL);
  136.     }
  137.  
  138.     // Tell the sink writer to start accepting data.
  139.     if (SUCCEEDED(hr)) {
  140.         hr = pSinkWriter->BeginWriting();
  141.     }
  142.  
  143.     // Return the pointer to the caller.
  144.     if (SUCCEEDED(hr)) {
  145.  
  146.         *ppWriter = pSinkWriter;
  147.         (*ppWriter)->AddRef();
  148.         *pStreamIndex = streamIndex;
  149.     }
  150.  
  151.     SafeRelease(&pSinkWriter);
  152.     SafeRelease(&pMediaTypeOut);
  153.     SafeRelease(&pMediaTypeIn);
  154.     return hr;
  155. }
  156.  
  157. HRESULT WriteFrame(IDirect3DDevice9* pDevice, IDirect3DSurface9* pSurface, IMFSinkWriter* pWriter, DWORD streamIndex, const LONGLONG& rtStart, const UINT32 uiWidth, const UINT32 uiHeight) {
  158.  
  159.     HRESULT hr = pDevice->GetFrontBufferData(0, pSurface);
  160.  
  161.     if (FAILED(hr)) {
  162.         return hr;
  163.     }
  164.  
  165.     D3DLOCKED_RECT rc;
  166.     hr = pSurface->LockRect(&rc, NULL, 0);
  167.  
  168.     if (FAILED(hr)) {
  169.         return hr;
  170.     }
  171.  
  172.     IMFSample *pSample = NULL;
  173.     IMFMediaBuffer *pBuffer = NULL;
  174.  
  175.     const LONG cbWidth = 4 * uiWidth;
  176.     const DWORD cbBuffer = cbWidth * uiHeight;
  177.  
  178.     BYTE *pData = NULL;
  179.  
  180.     // Create a new memory buffer.
  181.     hr = MFCreateMemoryBuffer(cbBuffer, &pBuffer);
  182.  
  183.     // Lock the buffer and copy the video frame to the buffer.
  184.     if (SUCCEEDED(hr)) {
  185.         hr = pBuffer->Lock(&pData, NULL, NULL);
  186.     }
  187.  
  188.     if (SUCCEEDED(hr)) {
  189.  
  190. #ifdef REVERSE_IMAGE
  191.         for (int i = 0, j = uiHeight - 1; i < uiHeight; i++, j--)
  192.             for (int k = 0; k < cbWidth; k++)
  193.                 pData[(i * cbWidth) + k] = ((BYTE*)rc.pBits)[(j * cbWidth) + k];
  194. #else
  195.         hr = MFCopyImage(pData, cbWidth, (BYTE*)rc.pBits, rc.Pitch, cbWidth, uiHeight);
  196. #endif
  197.     }
  198.  
  199.     if (pBuffer) {
  200.         pBuffer->Unlock();
  201.     }
  202.  
  203.     // Set the data length of the buffer.
  204.     if (SUCCEEDED(hr)) {
  205.         hr = pBuffer->SetCurrentLength(cbBuffer);
  206.     }
  207.  
  208.     // Create a media sample and add the buffer to the sample.
  209.     if (SUCCEEDED(hr)) {
  210.         hr = MFCreateSample(&pSample);
  211.     }
  212.  
  213.     if (SUCCEEDED(hr)) {
  214.         hr = pSample->AddBuffer(pBuffer);
  215.     }
  216.  
  217.     // Set the time stamp and the duration.
  218.     if (SUCCEEDED(hr)) {
  219.         hr = pSample->SetSampleTime(rtStart);
  220.     }
  221.  
  222.     if (SUCCEEDED(hr)) {
  223.         hr = pSample->SetSampleDuration(VIDEO_FRAME_DURATION);
  224.     }
  225.  
  226.     // Send the sample to the Sink Writer.
  227.     if (SUCCEEDED(hr)) {
  228.         hr = pWriter->WriteSample(streamIndex, pSample);
  229.     }
  230.  
  231.     hr = pSurface->UnlockRect();
  232.  
  233.     SafeRelease(&pSample);
  234.     SafeRelease(&pBuffer);
  235.     return hr;
  236. }
  237.  
  238. void main() {
  239.  
  240.     HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  241.  
  242.     if (SUCCEEDED(hr)) {
  243.  
  244.         hr = MFStartup(MF_VERSION);
  245.  
  246.         if (SUCCEEDED(hr)) {
  247.  
  248.             UINT32 uiWidth = 0;
  249.             UINT32 uiHeight = 0;
  250.  
  251.             IDirect3DDevice9* pDevice = NULL;
  252.             IDirect3DSurface9* pSurface = NULL;
  253.  
  254.             hr = InitializeDirect3D9(&pDevice, &pSurface, uiWidth, uiHeight);
  255.  
  256.             if (SUCCEEDED(hr)) {
  257.  
  258.                 IMFSinkWriter *pSinkWriter = NULL;
  259.                 DWORD stream;
  260.  
  261.                 hr = InitializeSinkWriter(&pSinkWriter, &stream, uiWidth, uiHeight);
  262.  
  263.                 if (SUCCEEDED(hr)) {
  264.  
  265.                     LONGLONG rtStart = 0;
  266.  
  267.                     for (DWORD i = 0; i < VIDEO_FRAME_COUNT; ++i) {
  268.  
  269.                         hr = WriteFrame(pDevice, pSurface, pSinkWriter, stream, rtStart, uiWidth, uiHeight);
  270.  
  271.                         if (FAILED(hr)) {
  272.                             break;
  273.                         }
  274.  
  275.                         rtStart += VIDEO_FRAME_DURATION;
  276.                     }
  277.                 }
  278.  
  279.                 if (SUCCEEDED(hr)) {
  280.                     hr = pSinkWriter->Finalize();
  281.                 }
  282.  
  283.                 SafeRelease(&pSinkWriter);
  284.             }
  285.  
  286.             SafeRelease(&pDevice);
  287.             SafeRelease(&pSurface);
  288.             MFShutdown();
  289.         }
  290.  
  291.         CoUninitialize();
  292.     }
  293. }
Add Comment
Please, Sign In to add comment