I have a program that prepares a movie frame using Win2d. The frame is in CanvasBitmap format. I would like to save such frames as a movie. For this, I'm switching from CanvasBitmap to ID3D11Texture2D. I do it as below, but after saving dozens of samples, I have errors of the type 'Device lost'. Probably the reason is the release of the sample. Is there another way to switch from CanvasBitmap to ID3D11Texture2D - so that saving the movie frames efficiently?
int AddWin2DFrame (CanvasBitmap^ frame)
{
ComPtr<ID2D1Device> nativeDevice = GetWrappedResource<ID2D1Device>(frame->Device);
ComPtr<ID2D1Bitmap1> nativeBitmap = GetWrappedResource<ID2D1Bitmap1>(frame);
ComPtr<IDXGISurface> dxgiSurface;
CHK(nativeBitmap->GetSurface(&dxgiSurface));
ComPtr<ID3D11Resource> d3dResource;
CHK(dxgiSurface.As(&d3dResource));
Microsoft::WRL::ComPtr<IMFMediaBuffer> writeBuffer;
CHK(MFCreateDXGISurfaceBuffer(__uuidof(ID3D11Texture2D), d3dResource.Get(), 0, false, &writeBuffer));
ComPtr<IMF2DBuffer> p2DBuffer;
DWORD length;
writeBuffer->QueryInterface(__uuidof(IMF2DBuffer), &p2DBuffer);
CHK(p2DBuffer->GetContiguousLength(&length));
CHK(writeBuffer->SetCurrentLength(length));
Microsoft::WRL::ComPtr<IMFSample> writeSample;
CHK(MFCreateSample(&writeSample));
CHK(writeSample->AddBuffer(writeBuffer.Get()));
CHK(writeSample->SetSampleDuration(sampleDuration));
CHK(writeSample->SetSampleTime(_hnsSampleTime));
CHK(_spSinkWriter->WriteSample(_streamIndex, writeSample.Get()));
}
The problem has already been solved. It is fixed by setting MF_SINK_WRITER_D3D_MANAGER as below:
ComPtr<IMFAttributes> spAttr;
CHK(MFCreateAttributes(&spAttr, 4));
CHK(spAttr->SetUnknown(MF_SINK_WRITER_D3D_MANAGER, deviceManager.Get()));
.........
CHK(MFCreateSinkWriterFromURL(ws1.c_str(), spByteStream.Get(), spAttr.Get(), &_spSinkWriter));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.