ARX  1.0
The next-generation open source augmented reality toolkit.
Loading...
Searching...
No Matches
MFIncludes.h
Go to the documentation of this file.
1//
2// MFIncludes.h
3// Header for standard system include files.
4//
5
6#pragma once
7
8#include <collection.h>
9#include <ppltasks.h>
10
11#include <wrl\implements.h>
12#include <wrl\wrappers\corewrappers.h>
13#include <Roerrorapi.h>
14
15#include <queue>
16#include <sstream>
17
18#include <robuffer.h>
19
20#include <mfapi.h>
21#include <mfidl.h>
22#include <Mferror.h>
23
24#include <windows.media.h>
25#include <windows.media.mediaproperties.h>
26
27namespace AWM = ::ABI::Windows::Media;
28namespace AWMMp = ::ABI::Windows::Media::MediaProperties;
29namespace AWFC = ::ABI::Windows::Foundation::Collections;
30namespace MW = ::Microsoft::WRL;
31namespace MWD = ::Microsoft::WRL::Details;
32namespace MWW = ::Microsoft::WRL::Wrappers;
33namespace WMC = ::Windows::Media::Capture;
34namespace WF = ::Windows::Foundation;
35namespace WMMp = ::Windows::Media::MediaProperties;
36namespace WSS = ::Windows::Storage::Streams;
37
38// Exception-based error handling
39#define CHK(statement) {HRESULT _hr = (statement); if (FAILED(_hr)) { throw ref new Platform::COMException(_hr); };}
40#define CHKNULL(p) {if ((p) == nullptr) { throw ref new Platform::NullReferenceException(L#p); };}
41
42// Exception-free error handling
43#define CHK_RETURN(statement) {hr = (statement); if (FAILED(hr)) { return hr; };}
44
45// Cast a C++/CX msartpointer to an ABI smartpointer
46template<typename T, typename U>
47MW::ComPtr<T> As(U^ in)
48{
49 MW::ComPtr<T> out;
50 CHK(reinterpret_cast<IInspectable*>(in)->QueryInterface(IID_PPV_ARGS(&out)));
51 return out;
52}
53
54// Cast an ABI smartpointer
55template<typename T, typename U>
56Microsoft::WRL::ComPtr<T> As(const Microsoft::WRL::ComPtr<U>& in)
57{
58 Microsoft::WRL::ComPtr<T> out;
59 CHK(in.As(&out));
60 return out;
61}
62
63// Cast an ABI smartpointer
64template<typename T, typename U>
65Microsoft::WRL::ComPtr<T> As(U* in)
66{
67 Microsoft::WRL::ComPtr<T> out;
68 CHK(in->QueryInterface(IID_PPV_ARGS(&out)));
69 return out;
70}
71
72// Get access to bytes in IBuffer
73inline unsigned char* GetData(_In_ WSS::IBuffer^ buffer)
74{
75 unsigned char* bytes = nullptr;
76 CHK(As<WSS::IBufferByteAccess>(buffer)->Buffer(&bytes));
77 return bytes;
78}
79
80// Class to start and shutdown Media Foundation
81class AutoMF
82{
83public:
85 : _bInitialized(false)
86 {
87 CHK(MFStartup(MF_VERSION));
88 }
89
91 {
92 if (_bInitialized)
93 {
94 (void)MFShutdown();
95 }
96 }
97
98private:
99 bool _bInitialized;
100};
101
102// Class to track error origin
103template <size_t N>
104HRESULT OriginateError(__in HRESULT hr, __in wchar_t const (&str)[N])
105{
106 if (FAILED(hr))
107 {
108 ::RoOriginateErrorW(hr, N - 1, str);
109 }
110 return hr;
111}
112
113// Class to track error origin
114inline HRESULT OriginateError(__in HRESULT hr)
115{
116 if (FAILED(hr))
117 {
118 ::RoOriginateErrorW(hr, 0, nullptr);
119 }
120 return hr;
121}
122
123// Converts exceptions into HRESULTs
124template <typename Lambda>
125HRESULT ExceptionBoundary(Lambda&& lambda)
126{
127 try
128 {
129 lambda();
130 return S_OK;
131 }
132 catch (Platform::Exception^ e)
133 {
134 return e->HResult;
135 }
136 catch (const std::bad_alloc&)
137 {
138 return E_OUTOFMEMORY;
139 }
140 catch (const std::exception&)
141 {
142 return E_FAIL;
143 }
144}
145
146// Wraps an IMFSample in a C++/CX class to be able to define a callback delegate
147
148namespace Media
149{
150ref class MediaSample sealed
151{
152internal:
153 MW::ComPtr<IMFSample> Sample;
154};
155
156delegate void MediaSampleHandler(MediaSample^ sample);
157
158}
159
unsigned char * GetData(_In_ WSS::IBuffer^ buffer)
Definition: MFIncludes.h:73
HRESULT OriginateError(__in HRESULT hr, __in wchar_t const (&str)[N])
Definition: MFIncludes.h:104
HRESULT ExceptionBoundary(Lambda &&lambda)
Definition: MFIncludes.h:125
MW::ComPtr< T > As(U^ in)
Definition: MFIncludes.h:47
#define CHK(statement)
Definition: MFIncludes.h:39
Definition: MFIncludes.h:82
AutoMF()
Definition: MFIncludes.h:84
~AutoMF()
Definition: MFIncludes.h:90
Definition: CaptureFrameGrabber.h:15
delegate void MediaSampleHandler(MediaSample^ sample)