我正在使用接收器编写器来编写从桌面复制获得的示例。 但成功1-3次后就会停止或出错,代码如下:
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();
}
原谅 if else 代码,我只是用它来测试。我应该使用 THROW_IF_ERROR 如果您想要这里的示例代码,它是:https://github.com/ljzj2/testdemo1
API 不会以这种方式工作。您无法直接添加 Desktop Duplication API 作为 Sink Writer API 的输入纹理:该纹理是短暂的。您需要先制作一个副本,然后将副本提供给 Sink Writer 管道。