Commit aa3c36f5 authored by GregoryMorse's avatar GregoryMorse

Update cap_msmf.cpp

Add support for WinRT in the MF capture framework by removing the disallowed calls to enumerate devices and create a sample grabber sink and adding framework for the MediaCapture interface and a custom sink which interfaces with the sample grabber callback interface. The change requires discussion for making it completely functional as redundancy is required given that if the source is a video file, the old code pathways must be used. Otherwise all IMFMediaSession, IMFMediaSource, and IMFActivate code must use a MediaCapture code path and all sink code must use the CMediaSink custom sink.

Support for the custom sink is extended to non-WinRT not for compatibility as Windows Vista client is a minimum regardless, but because it offers more flexibility, could be faster and is able to be used as an optionally different code path during sink creation based on a future configuration parameter.

My discussion and proposal to finish this change:
 Devices are so easily enumerated through WinRT Windows.Devices namespace that wrapping the calls in a library is quite a chore for little benefit though to get the various modes and formats could still be a worthwhile project. For now conditional compilation to remove videodevices and any offending non-video file related activity in videodevice. In my opinion, this is a different , far less fundamental and important change which can possibly be done as a future project and also much more easily implemented in C++/CX.

ImageGrabber has the IMFSampleGrabberSinkCallback replaced with a base class (SharedSampleGrabber) which also be is base class for ImageGrabberRT. This change is necessary as the custom sink does not require a thread to pump events which is done through MediaCapture already. IMFSampleGrabberSinkCallback is the common element between both models and that piece can be shared. Initializing the new ImageGrabberRT is as simple as passing an already initialized MediaCapture object and any video format/encoding parameters.

The concurrency event is necessary to wait for completion and is the way the underlying, IAsyncAction wrappers in the task library work as well. Native WIN32 event objects would be an option if HAVE_CONCURRENCY is not defined. I could even imagine doing it with sleep/thread yield and InterlockedCompareExchange yet I am not enthusiastic about that approach either. Since there is a specific compiler HAVE_ for concurrency, I do not like pulling it in though I think for WinRT it is safe to say we will always have it available though should probably conditionally compile with the Interlocked option as WIN32 events would require HAVE_WIN32.

It looks like C++/CX cannot be used for the IMediaExtension sink (which should not be a problem) as using COM objects requires WRL and though deriving from IMediaExtension can be done, there is little purpose without COM. Objects from C++/CX can be swapped to interact with objects from native C++ as Inspectable* can reinterpret_cast to the ref object IInspectable^ and vice-versa. A solution to the COM class with C++/CX would be great so we could have dual support. Also without #define for every WRL object in use, the code will get quite muddy given that the */^ would need to be ifdef'd everywhere.

Update cap_msmf.cpp

Fixed bugs and completed the change.  I believe the new classes need to be moved to a header file as the file has become to large and more classes need to be added for handling all the asynchronous problems (one wrapping IAsyncAction in a task and another for making a task out of IAsyncAction).  Unfortunately, blocking on the UI thread is not an option in WinRT so a synchronous architecture is considered "illegal" by Microsoft's standards even if implementable (C++/CX ppltasks library throws errors if you try it).  Worse, either by design or a bug in the MF MediaCapture class with Custom Sinks causes a crash if stop/start previewing without reinitializing (spPreferredPreviewMediaType is fatally nulled).  After decompiling Windows.Media.dll, I worked around this in my own projects by using an activate-able custom sink ID which strangely assigns 1 to this pointer allowing it to be reinitialized in what can only be described as a hack by Microsoft.  This would add additional overhead to the project to implement especially for static libraries as it requires IDL/DLL exporting followed by manifest declaration.  Better to document that it is not supported.

Furthermore, an additional class for IMFAttributes should be implemented to make clean architecture for passing around attributes as opposed to directly calling non-COM interface calls on the objects and making use of SetProperties which would also be a set up for an object that uses the RuntimeClass activation ID.

The remaining changes are not difficult and will be complete soon along with debug tracing messages.

Update and rename cap_msmf.h to cap_msmf.hpp

Update cap_msmf.cpp

Successful test - samples are grabbed

Update ppltasks_winrt.h

Library updated and cleaned up with comments, marshaling, exceptions and linker settings
Fixed trailing whitespace

Support VS 2013 and consistency cleanup and C++/CX object creation fixed
parent b70332d8
This diff is collapsed.
This diff is collapsed.
......@@ -31,7 +31,6 @@
#include <wrl\async.h>
#include <windows.foundation.h>
#include <ctxtcall.h>
#include <comdef.h>
#ifndef _UITHREADCTXT_SUPPORT
......@@ -1213,21 +1212,20 @@ namespace details
{
}
explicit _ExceptionHolder(const _com_error& _E, void* _SourceAddressHint) :
_M_exceptionObserved(0), _M_disassembleMe(_SourceAddressHint)
explicit _ExceptionHolder(IRestrictedErrorInfo*& _E, void* _SourceAddressHint) :
_M_exceptionObserved(0), _M_disassembleMe(_SourceAddressHint), _M_winRTException(_E)
{
_M_winRTException = std::unique_ptr<_com_error>(new _com_error(_E));
}
__declspec(noinline)
~_ExceptionHolder()
{
if (_M_exceptionObserved == 0)
{
// Disassemble at this->_M_disassembleMe to get to the source location right after either the creation of the task (constructor
// or then method) that encountered this exception, or the set_exception call for a task_completion_event.
Concurrency::details::_ReportUnobservedException();
}
if (_M_exceptionObserved == 0)
{
// Disassemble at this->_M_disassembleMe to get to the source location right after either the creation of the task (constructor
// or then method) that encountered this exception, or the set_exception call for a task_completion_event.
Concurrency::details::_ReportUnobservedException();
}
}
void _RethrowUserException()
{
......@@ -1238,7 +1236,7 @@ namespace details
if (_M_winRTException != nullptr)
{
throw _M_winRTException.get();
throw _M_winRTException.Get();
}
std::rethrow_exception(_M_stdException);
}
......@@ -1249,7 +1247,7 @@ namespace details
// Either _M_stdException or _M_winRTException is populated based on the type of exception encountered.
std::exception_ptr _M_stdException;
std::unique_ptr<_com_error> _M_winRTException;
Microsoft::WRL::ComPtr<IRestrictedErrorInfo> _M_winRTException;
// Disassembling this value will point to a source instruction right after a call instruction. If the call is to create_task,
// a task constructor or the then method, the task created by that method is the one that encountered this exception. If the call
......@@ -1262,6 +1260,7 @@ namespace details
struct _AsyncInfoCompletionHandler : public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags< Microsoft::WRL::RuntimeClassType::ClassicCom>, _CompletionHandlerType>
{
MixInHelper()
public:
_AsyncInfoCompletionHandler(_Function func) : _M_function(func) {}
STDMETHODIMP Invoke(_AsyncOperationType *asyncInfo, ABI::Windows::Foundation::AsyncStatus status)
......@@ -1603,7 +1602,7 @@ namespace details
_M_pTask->_Cancel(true);
throw;
}
catch(const _com_error& _E)
catch(IRestrictedErrorInfo*& _E)
{
_M_pTask->_CancelWithException(_E);
throw;
......@@ -1742,7 +1741,7 @@ namespace details
// the exception and canceled the task. Swallow the exception here.
_CONCRT_ASSERT(_IsCanceled());
}
catch(const _com_error& _E)
catch(IRestrictedErrorInfo*& _E)
{
// Its possible the task body hasn't seen the exception, if so we need to cancel with exception here.
if(!_HasUserException())
......@@ -1818,7 +1817,7 @@ namespace details
return _CancelAndRunContinuations(true, true, _PropagatedFromAncestor, _ExHolder);
}
bool _CancelWithException(const _com_error& _Exception)
bool _CancelWithException(IRestrictedErrorInfo*& _Exception)
{
// This task was canceled because the task body encountered an exception.
_CONCRT_ASSERT(!_HasUserException());
......@@ -1879,7 +1878,7 @@ namespace details
bool _HasUserException()
{
return _M_exceptionHolder;
return _M_exceptionHolder != nullptr;
}
void _SetScheduledEvent()
......@@ -2042,7 +2041,7 @@ namespace details
return S_OK;
});
}
catch(const _com_error& _E)
catch(IRestrictedErrorInfo*& _E)
{
_TaskImplPtr->_CancelWithException(_E);
}
......@@ -2489,7 +2488,7 @@ namespace details
bool _HasUserException()
{
return _M_exceptionHolder;
return _M_exceptionHolder != nullptr;
}
~_Task_completion_event_impl()
......@@ -3524,7 +3523,7 @@ private:
}
//
// Overload 1: returns IAsyncOperation<_InternalReturnType>^ (only uder /ZW)
// Overload 1: returns IAsyncOperation<_InternalReturnType>^ (only under /ZW)
// or
// returns task<_InternalReturnType>
//
......@@ -4485,7 +4484,7 @@ namespace details
template<typename _Ty>
_Ty _GetUnwrappedType(task<_Ty>);
// Unwrap all supportted types
// Unwrap all supported types
template<typename _Ty>
auto _GetUnwrappedReturnType(_Ty _Arg, int) -> decltype(_GetUnwrappedType(_Arg));
// fallback
......@@ -5627,7 +5626,11 @@ namespace details
_M_CompleteDelegateAssigned(0),
_M_CallbackMade(0)
{
#if _MSC_VER < 1800
_M_id = Concurrency::details::_GetNextAsyncId();
#else
_M_id = Concurrency::details::platform::GetNextAsyncId();
#endif
}
virtual STDMETHODIMP GetResults(typename _Attributes::_ReturnType* results)
......@@ -6113,9 +6116,13 @@ namespace details
{
_TryTransitionToCancelled();
}
catch(const _com_error& _Ex)
catch(IRestrictedErrorInfo*& _Ex)
{
_TryTransitionToError(_Ex->HResult);
HRESULT hr;
HRESULT _hr;
hr = _Ex->GetErrorDetails(NULL, &_hr, NULL, NULL);
if (SUCCEEDED(hr)) hr = _hr;
_TryTransitionToError(hr);
}
catch (...)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment