Microsoft DirectX 8.1 (C++) |
This sample code reads a file and renders it to a primary DirectDraw surface.
#include <stdio.h>
#include "ddraw.h" // DirectDraw interfaces
#include "mmstream.h" // multimedia stream interfaces
#include "amstream.h" // DirectShow multimedia stream interfaces
#include "ddstream.h" // DirectDraw multimedia stream interfaces
void RenderStreamToSurface(IDirectDrawSurface *pSurface,
IMultiMediaStream *pMMStream)
{
IMediaStream *pPrimaryVidStream;
IDirectDrawMediaStream *pDDStream;
IDirectDrawStreamSample *pSample;
RECT rect;
DDSURFACEDESC ddsd;
pMMStream->GetMediaStream(MSPID_PrimaryVideo, &pPrimaryVidStream);
pPrimaryVidStream->QueryInterface(IID_IDirectDrawMediaStream, (void **)&pDDStream);
ddsd.dwSize = sizeof(ddsd);
pDDStream->GetFormat(&ddsd, NULL, NULL, NULL);
rect.top = rect.left = 0;
rect.bottom = ddsd.dwHeight;
rect.right = ddsd.dwWidth;
pDDStream->CreateSample(pSurface, &rect, 0, &pSample);
pMMStream->SetState(STREAMSTATE_RUN);
while (pSample->Update(0, NULL, NULL, NULL) == S_OK);
pMMStream->SetState(STREAMSTATE_STOP);
pSample->Release();
pDDStream->Release();
pPrimaryVidStream->Release();
}
void RenderFileToMMStream(const char * szFileName,
IMultiMediaStream **ppMMStream,
IDirectDraw *pDD)
{
IAMMultiMediaStream *pAMStream;
CoCreateInstance(CLSID_AMMultiMediaStream, NULL, CLSCTX_INPROC_SERVER,
IID_IAMMultiMediaStream, (void **)&pAMStream);
WCHAR wPath[MAX_PATH]; // wide (32-bit) string name
MultiByteToWideChar(CP_ACP, 0, szFileName, -1, wPath,
sizeof(wPath)/sizeof(wPath[0]));
pAMStream->Initialize(STREAMTYPE_READ, AMMSF_NOGRAPHTHREAD, NULL);
pAMStream->AddMediaStream(pDD, &MSPID_PrimaryVideo, 0, NULL);
pAMStream->AddMediaStream(NULL, &MSPID_PrimaryAudio, AMMSF_ADDDEFAULTRENDERER, NULL);
pAMStream->OpenFile(wPath, 0);
*ppMMStream = pAMStream;
}
int _CRTAPI1 main(int argc, char *argv[])
{
if (argc < 2) {
printf("Usage : showstrm movie.ext\n");
exit(0);}
DDSURFACEDESC ddsd;
IDirectDraw *pDD;
IDirectDrawSurface *pPrimarySurface;
IMultiMediaStream *pMMStream;
CoInitialize(NULL);
DirectDrawCreate(NULL, &pDD, NULL);
pDD->SetCooperativeLevel(GetDesktopWindow(), DDSCL_NORMAL);
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
pDD->CreateSurface(&ddsd, &pPrimarySurface, NULL);
RenderFileToMMStream(argv[1], &pMMStream, pDD);
RenderStreamToSurface(pPrimarySurface, pMMStream);
pMMStream->Release();
pPrimarySurface->Release();
pDD->Release();
CoUninitialize();
return 0;
}