Angella Yu
Angella Yu

Reputation: 62

the sink writer always stopped or error after 1-3 times success?

I am using sink Writer to write sample which get from desktop duplication. but it will stopped or error after 1-3 times success and here is the code:

while(true){
hr = dxgiOutputDuplication->AcquireNextFrame(0, &frame_info, &resource);
if (SUCCEEDED(hr)) {
    ID3D11Texture2D* texture = NULL;
    hr = resource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&texture);
    if (SUCCEEDED(hr)) {
        IDXGISurface* dxgiSurface = NULL;
        hr = texture->QueryInterface(__uuidof(IDXGISurface), (void**)&dxgiSurface);
        if (SUCCEEDED(hr)) {
            IMFSample* sample = NULL;
            hr = MFCreateSample(&sample);
            if (SUCCEEDED(hr)) {
                IMFMediaBuffer* buffer = NULL;
                hr = MFCreateDXGISurfaceBuffer(__uuidof(ID3D11Texture2D), dxgiSurface, 0, FALSE, &buffer);
                if (SUCCEEDED(hr)) {
                    hr = sample->AddBuffer(buffer);
                    hr = sample->SetSampleDuration(duration);
                    hr = presentation->GetTime(&sampleTime);
                    hr = sample->SetSampleTime(sampleTime);
                    //sampleTime += duration * 10;

                    DWORD length = 0;
                    IMF2DBuffer* imf_buffer = NULL;
                    hr = buffer->QueryInterface(__uuidof(IMF2DBuffer), (void**)&imf_buffer);
                    if (SUCCEEDED(hr)) {
                        hr = imf_buffer->GetContiguousLength(&length);
                        hr = buffer->SetCurrentLength(length);

                        hr = writer->WriteSample(0, sample);

                        if (FAILED(hr)) {
                            OutputDebugString(L"FAILED WRITE SAMPLE\n");
                        }

                        o++;
                        if (o > 1920) {
                            writer->Finalize();

                            OutputDebugString(L"OOOOOOOOOO");
                            break;
                        }
                        imf_buffer->Release();
                        imf_buffer = NULL;
                    }

                    buffer->Release();
                    buffer = NULL;
                }
                sample->Release();
                sample = NULL;
            }
            dxgiSurface->Release();
            dxgiSurface = NULL;
        }
        texture->Release();
        texture = NULL;
    }
    resource->Release();
    resource = NULL;
}
dxgiOutputDuplication->ReleaseFrame();
}

forgive the if else code I just use it for test. I should use THROW_IF_ERROR and if you want the example code here it is :https://github.com/ljzj2/testdemo1

Upvotes: 0

Views: 69

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69724

The API is not going to work this way. You cannot add Desktop Duplication API texture directly as input for Sink Writer API: the texture is ephemeral. You need to make a copy first and feed the Sink Writer pipeline with the copy.

Upvotes: 0

Related Questions