Jump to content
  • Advertisement
Sign in to follow this  

Media Foundation video playback

This topic is 860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys,
I am currently porting my code over from DirectShow to Media Foundation.
I am at the point where I can load the video, and start playback. I can hear the audio, so that is great, I am on the right track.
But I can't find any information anywhere on how to get the location in memory for the current video frame, so I can use it as a texture for a quad (like I am currently doing in DirectShow).
Any advice on how to get the location of the current uncompressed frame would be awesome.
This is a simple test function where I am loading and playing the video. But I can't figure out how to get the pointer to the actual frame.

double test()
 IMFSourceResolver* pSourceResolver = NULL;
 IUnknown* pSource = NULL;
 IMFMediaSource* m_pSource = NULL;
 IMFPresentationDescriptor* pPresDescriptor = NULL;
 IMFTopology* pTopology = NULL;
 IMFStreamDescriptor* pStreamDescriptor = NULL;
 IMFTopologyNode* pSourceNode = NULL;
 IMFTopologyNode* pOutputNode = NULL;
 IMFMediaTypeHandler* pHandler = NULL;
 IMFActivate* pRendererActivate = NULL;
 IMFMediaSession* pSession = NULL;

 DWORD nSourceStreams = 0;
 BOOL streamSelected = FALSE;
 HWND hWnd = NULL;
 // IMFClock *m_pPresentationClock=NULL;

 hr = MFStartup(MF_VERSION);       // Start up MF
 if (FAILED(hr)) return 1;
 hr = MFCreateSourceResolver(&pSourceResolver);  // Create the source resolver
 if (FAILED(hr)) return 2;
 hr = pSourceResolver->CreateObjectFromURL(L"D:\\Media\\Movies\\test_file.mp4",
  NULL, &objectType, &pSource);   // Create the media source
 if (FAILED(hr)) return 3;
 m_pSource = static_cast<IMFMediaSource*>(pSource); // Cast the source to a MediaSource (from IUnknown)
 hr = m_pSource->CreatePresentationDescriptor(&pPresDescriptor);  // Create presentation descriptor
 if (FAILED(hr)) return 4;
 hr = pPresDescriptor->GetStreamDescriptorCount(&nSourceStreams); // Get the number of streams in the media source
 if (FAILED(hr)) return 5;
 hr = MFCreateTopology(&pTopology);     // Create a new topology.
 if (FAILED(hr)) return 6;
 for (DWORD x = 0;x<nSourceStreams;x++)  // Create source and sink nodes and add them to the topology.
            //DWORD x=0;
  // Get the stream descriptor for this stream (information about stream).
  hr = pPresDescriptor->GetStreamDescriptorByIndex(x, &streamSelected, &pStreamDescriptor);
  if (FAILED(hr)) return 7;
  // Create the topology branch only if the stream is selected - IE if the user wants
  // to play it.
  if (streamSelected)
   MFCreateTopologyNode(MF_TOPOLOGY_SOURCESTREAM_NODE, &pSourceNode);
   if (FAILED(hr)) return 8;
   hr = pSourceNode->SetUnknown(MF_TOPONODE_SOURCE, m_pSource);
   if (FAILED(hr)) return 9;
   // Set the node presentation descriptor attribute of the node by passing
   // in a pointer to the presentation descriptor
   hr = pSourceNode->SetUnknown(MF_TOPONODE_PRESENTATION_DESCRIPTOR, pPresDescriptor);
   if (FAILED(hr)) return 10;
   hr = pSourceNode->SetUnknown(MF_TOPONODE_STREAM_DESCRIPTOR, pStreamDescriptor);
   if (FAILED(hr)) return 11;
   GUID majorType = GUID_NULL;
   // Get the media type handler for the stream, which will be used to process
   // the media types of the stream.  The handler stores the media type.
   hr = pStreamDescriptor->GetMediaTypeHandler(&pHandler);
   if (FAILED(hr)) return 12;
   // Get the major media type (e.g. video or audio)
   hr = pHandler->GetMajorType(&majorType);
   if (FAILED(hr)) return 13;
   if (majorType == MFMediaType_Audio)
    hr = MFCreateAudioRendererActivate(&pRendererActivate);
    if (FAILED(hr)) return 14;
   else if (majorType == MFMediaType_Video)
    //if(FAILED(hr)) return 15;
    hr = E_FAIL; // fail if the stream type is not video or audio. 
   if (FAILED(hr)) return 16;
   hr = MFCreateTopologyNode(MF_TOPOLOGY_OUTPUT_NODE, &pOutputNode); // Create the node that will represent the renderer
   if (FAILED(hr)) return 17;
   hr = pOutputNode->SetObject(pRendererActivate);  // Store the IActivate object in the sink node
   if (FAILED(hr)) return 18;
   hr = pTopology->AddNode(pSourceNode);     // Add the source node to the topology.
   if (FAILED(hr)) return 19;
   hr = pTopology->AddNode(pOutputNode);     // Add the sink node to the topology.
   if (FAILED(hr)) return 20;
   hr = pSourceNode->ConnectOutput(0, pOutputNode, 0); // Connect the source node to the sink node.  The resolver will find the intermediate nodes needed to convert media types.
   if (FAILED(hr)) return 21;

At this stage it is loading test_file.mp4 from a hard coded location, audio plays fine, and there are no error return codes happening.


Any help would be hugely appreciated.


Thanks in advance smile.png


(P.S. Sorry about the code formatting - GD seems to have mangled it a little)

Edited by DarkRonin

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!