Search code examples
c++decodems-media-foundationmedia-type

Windows Media Foundation: current media type change


I am using Windows Media Foundation to decode audio files. I am able to decode most of the files, besides ones that say MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED.

If I have a current media type change at some moment, how to handle it?

Here is part of the code:

    IMFSourceReader *pSourceReader = NULL;
    IMFMediaType *pAudioType= NULL;

    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    MFStartup(MF_VERSION);

    MFCreateSourceReaderFromURL(filePath, NULL, &pSourceReader);
    pSourceReader->SetStreamSelection(MF_SOURCE_READER_FIRST_AUDIO_STREAM, true)
    pSourceReader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, &pAudioType);
    MFCreateMediaType(&pAudioType);
    pAudioType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
    pAudioType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_Float);
    pAudioType->SetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, NULL, pAudioType);

    IMFSample *audioSample = NULL;
    DWORD streamIndex, flags;
    LONGLONG llAudioTimeStamp;

        while (true)
        {
            pSourceReader->ReadSample(
                MF_SOURCE_READER_FIRST_AUDIO_STREAM,
                0,                              // Flags.
                &streamIndex,                   // Receives the actual stream index. 
                &flags,                         // Receives status flags.
                &llAudioTimeStamp,              // Receives the time stamp.
                &audioSample )                  // Receives the sample or NULL.

            if (flags & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED)
            {
                // what to do here?
            }
    }

Many thanks in advance.


Solution

  • I found a way to overcome this.

    When we call GetCurrentMediaType, we have to keep somewhere 2 properties: channels and sample rate. After, when we create a new Media Type, we need to set those properties to the new media type. And then set the new media type on reader.