c++-winrtwinrt-component

Registering to MediaStreamSample Processed event gives 'H': is not a legal base class error


I'm trying to subscribe to MediaStreamSample Processed event, so when ever processed event is triggered OnSampleProcessed method must be triggered Below implementation is what I have tried

StreamSamplePool.h

void OnSampleProcessed(winrt::Windows::Foundation::TypedEventHandler<MediaStreamSample, IInspectable const&> const& handler);

StreamSamplePool.cpp

void StreamSamplePool::OnSampleProcessed(winrt::Windows::Foundation::TypedEventHandler<MediaStreamSample, IInspectable const&> const& handler)
{
}

Tried subscribing using below code

sample.Processed(&StreamSamplePool::OnSampleProcessed);

on adding above line of code getting 'H': is not a legal base class error

Debug output -

1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\Generated Files\winrt\base.h(5231): error C2516: 'H': is not a legal base class
1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\Generated Files\winrt\Windows.Foundation.h(889): message : see reference to class template instantiation 'winrt::impl::implements_delegate<winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>,H>' being compiled
1>        with
1>        [
1>            H=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)
1>        ]
1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\Generated Files\winrt\base.h(5280): message : see reference to class template instantiation 'winrt::impl::delegate<T,H>' being compiled
1>        with
1>        [
1>            T=winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>,
1>            H=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)
1>        ]
1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\Generated Files\winrt\Windows.Foundation.h(2504): message : see reference to function template instantiation 'T winrt::impl::make_delegate<winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>,_Ty>(H &&)' being compiled
1>        with
1>        [
1>            T=winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>,
1>            _Ty=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &),
1>            H=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)
1>        ]
1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\StreamSamplePool.cpp(57): message : see reference to function template instantiation 'winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>::TypedEventHandler<void(__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<D,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)>(L)' being compiled
1>        with
1>        [
1>            D=winrt::WinRTMediaLibrary::implementation::StreamSamplePool,
1>            L=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)
1>        ]
1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\StreamSamplePool.cpp(57): message : see reference to function template instantiation 'winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>::TypedEventHandler<void(__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<D,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)>(L)' being compiled
1>        with
1>        [
1>            D=winrt::WinRTMediaLibrary::implementation::StreamSamplePool,
1>            L=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)
1>        ]
1>C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\XamlCompiler\Microsoft.Windows.UI.Xaml.Common.targets(486,5): error MSB4181: The "CompileXaml" task returned false but did not log an error.
1>Done building project "WinRTMediaLibrary.vcxproj" -- FAILED

.


Solution

  • The event handler has the wrong signature. It needs to be

    void OnSampleProcessed(MediaStreamSample const& sender, IInspectable const& args);
    

    or a similar signature that can be called with arguments of type MediaStreamSample and IInspectable. Subscribing a member function to an event requires a class instance pointer as well. The easiest way to do this is to have the template machinery figure everything out and just pass

    sample.Processed({ this, &StreamSamplePool::OnSampleProcessed });
    

    Handle events by using delegates in C++/WinRT and Author events in C++/WinRT explain the underlying concepts in detail.