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

mc 4,436 Reputation points
2024-09-19T16:02:26.66+00:00

i am using sink writer to write sample.

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();

}

and this is the example code:https://github.com/ljzj2/testdemo1

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,592 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,693 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.