IMFSourceReader->ReadSample never returns RRS feed

  • Question

  • Hi,

    I have a media file that causes ReadSample to hang indefinitely when reading from the audio channel of a video file. This appears to be happening at the end of the stream. It successfully reads 156s of samples from the stream before hanging, yet the stream metadata says it is only 137s long.

    Code snippet, sorry about the formatting.

    hr =

    	0,			NULL,		
    	&dwFlags,			NULL,		
    	&pSample			);	
    	if (FAILED(hr)) { break; }	
    			printf("Type change - not supported by WAVE file format.\n");		
    		bool atEof=false;	
    			//printf("End of input file.\n");	

    Monday, October 1, 2012 6:33 PM

All replies

  • Are you never reading from the video stream?  Some of the media sources that come with Media Foundation have a finite limit on how much data they will buffer in memory.  As you read audio data, both audio and video data get read from the file and parsed into samples.  If you never read from the video stream, gradually the video will fill the queue and any audio requests will be blocked indefinitely.  Your best bet would be to read in an interleaved fashion and throw away video samples if you do not need them.
    Thursday, November 1, 2012 9:39 PM
  • I have the same issue

    IMFSample *pSample = NULL;
    //	Waiting until we get a sample.
    while (pSample == NULL && SUCCEEDED(hr) && numTries < 100){
    	hr = pSourceReader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, NULL, NULL, &srcReaderFlag, NULL, &pSample);
    	if (!SUCCEEDED(hr))
    		wcout << L"Couldn't read sample." << endl;
    		wcout << L"Source reader flag : " << srcReaderFlag << endl;

    I can get 10 frames that way before ReadSample doesn't respond, I tried reading audio alongside, even flushing but it didn't solve my problem.

    Thursday, July 16, 2015 1:19 PM
  • Where do you release the pSample?
    Thursday, July 16, 2015 9:11 PM
  • I released it just afterwards.

    However I tried to dig further and I may have found a bug related to buffer reading.

    First, here's what I'm trying to do :

    1. Getting a sample
    2. Getting a buffer from the sample
    3. Processing
    4. Releasing pointers

    I tried that both in synchronous and asynchronous modes and I got the same issue; the capture gets 'clogged'.

    Just to make sure I didn't do something wrong, I double-checked the samples from the docs to make sure I didn't miss any step and I (apparently) didn't miss any.

    To go further, I downloaded the MFCaptureToFile sample (The link from MSDN seems to be broken but someone made it available on an FTP, sorry I can't post links but you can find it easily by googling MFCaptureToFile).

    The code runs perfectly, however, in the OnReadSample callback, if I add those lines :

    IMFMediaBuffer *pMediaBuffer;
    DWORD dwBuffCount;
    if (dwBuffCount)
    	pSample->GetBufferByIndex(0, &pMediaBuffer);

    I will have the exact same issue again : The capture will be 'clogged' after 10 frames.

    Correct me if I'm wrong, but I don't think I should expect this behavior. I really need to get this buffer through MediaFoundation as every capture library converts retreived buffers into RGB and I must retreive those raw YUY2 buffers.

    Friday, July 17, 2015 8:32 AM
  • GetBufferByIndex() AddRef's IMFMediaBuffer. Are you sure you are releasing it?
    Friday, July 17, 2015 7:42 PM
  • I released it just afterwards.

    I'm not sure we are communicating.  Just after what?

    There is no "pSample->Release();" statement in your code.  If the MF code is using a pool of IMFSamples, you are going to quickly exhaust the pool, and your application will hang until one becomes available.

    Looking at your experiment with MFCaptureToFile, you are again leaking IMFSamples (since there is no matching "pMediaBuffer->Release();".

    This may not be your (only) problem, but there's no point looking elsewhere until you fix this.

    Friday, July 17, 2015 10:11 PM
  • LGS, your statement is correct. MF allocates ~10 frames by default for USB cameras. This matches the amount of frames mentioned previously. If IMFSample and IMFMediaBuffer are not released, they are held in the pool and not reused.
    Friday, July 17, 2015 10:23 PM
  • I was trying releasing it just after the pMediaBuffer->Unlock() instruction with SafeRelease, it kept crashing until now. I must've added some prior checks... Thanks for your help.
    Friday, July 24, 2015 8:35 AM
  • Hello, what was the issue that made this happen?

    Today I ran into the same issue (In Delphi though, converting the MFCaptureD3D Sample). OnReadSample stops after 10 frames.

    All values of pSample and pBuffer are released, but this doesn't seem to be the solution..

    I had no expiriences like this behaviour, with the MFCaptureToFile sample.

    Thank you.

    • Edited by factoryX Saturday, March 10, 2018 8:39 PM
    • Proposed as answer by factoryX Saturday, March 10, 2018 11:26 PM
    • Unproposed as answer by factoryX Saturday, March 10, 2018 11:26 PM
    Saturday, March 10, 2018 8:30 PM
  • Ok, after some investigation, the issue was method IMFMediaBuffer::Unlock() never took place.

    This should be done when DrawFrame() is going to be finnished.

    • Edited by factoryX Saturday, March 10, 2018 11:33 PM
    Saturday, March 10, 2018 11:31 PM