windows-runtimems-media-foundation

Create IMFMediaType using Windows.Media.MediaProperties namespace


Since the namespaces in Windows.Media just use Media Foundation under the hood, I was hoping to create my codecs with the WinRT convenience methods (on AudioEncodingProperties, VideoEncodingProperties) and then convert them to IMFMediaType. But I cant figure out how to convert them:

using namespace winrt::Windows::Media::MediaProperties;
constexpr auto TryQI = [](auto el)
{
    auto try1 = el.try_as<IMFMediaType>();
    WINRT_ASSERT(not try1);
    auto try2 = el.Properties().try_as<IMFMediaType>();
    WINRT_ASSERT(not try2);
};
TryQI(AudioEncodingProperties::CreateAac(48'000, 2, 128'000));
TryQI(VideoEncodingProperties::CreateUncompressed(MediaEncodingSubtypes::Bgra8(), 1920, 1080));

AudioEncodingProperties.Properties is a IMap<winrt::guid,IInspectable>. Does anyone know what to inspect the IInspectable values for?


Solution

  • They are IPropertyValue objects.

    If you have this:

    auto props = AudioEncodingProperties::CreateAac(48'000, 2, 128'000);
    auto prop = props.Properties().Lookup(MF_MT_AUDIO_SAMPLES_PER_SECOND); // Mfapi.h
    

    You can get the value behind like this:

    auto value = prop.as<Windows::Foundation::IPropertyValue>().GetUInt32();
    

    Or this (C++/WinRT):

    auto value = winrt::unbox_value<UINT32>(prop);
    

    And to get an IMFMediaType reference from that, you can use MFCreateMediaTypeFromProperties like this, here with C++/WinRT:

    com_ptr<IMFMediaType> type;
    winrt::check_hresult(MFCreateMediaTypeFromProperties(
        props.as<IUnknown>().get(),
        type.put()));