Tiago Magalhães
Tiago Magalhães

Reputation: 51

Using Media Foundation to encode Direct X surfaces

I'm trying to use the MediaFoundation API to encode a video but I'm having problems pushing the samples to the SinkWriter.

I'm getting the frames to encode through the Desktop Duplication API. What I end up with is an ID3D11Texture2D with the desktop image in it.

I'm trying to create an IMFVideoSample containing this surface and then push that video sample to a SinkWriter.

I've tried going about this in different ways:

I ran out of things to try.
Has anyone out there used this API before and can think of things I should check, or of any way on how I can go about debugging this?

Upvotes: 5

Views: 2179

Answers (2)

Steph555
Steph555

Reputation: 21

In case it helps someone, it works by calling MF.CreateDXGISurfaceBuffer(surface, 0, false, &dstBuffer), but, and this doesn't seem to be specified anywhere, you also have to call dstBuffer.SetCurrentLength(length) to set the proper length of the surface buffer.

Upvotes: 0

Soonts
Soonts

Reputation: 21936

First of all you should learn to use mftrace tool. Very likely, it will tell you the problem right away.

But my guess is, following problems are likely.

  1. Probably, some other attributes are required besides SampleTime / SampleDuration.

  2. Probably, SinkWriter needs a texture it can read on CPU. To fix that, when a frame is available, create a staging texture of the same format + size, call CopyResource to copy desktop to staging texture, then pass that staging texture to MF.

  3. Even if you use a hardware encoder so the CPU never tries to read the texture data, I don’t think it’s a good idea to directly pass your desktop texture to MF.

When you set a D3D texture for sample, no data is copied anywhere, the sample merely retains the texture.

MF works asynchronously, it may buffer several samples in its topology nodes if they want to.

DD gives you data synchronously, you may only access the texture between AcquireNextFrame and ReleaseFrame calls.

Upvotes: 2

Related Questions