Path: blob/master/modules/videoio/src/cap_winrt/MFIncludes.hpp
16348 views
// Header for standard system include files.12// Copyright (c) Microsoft. All rights reserved.3//4// The MIT License (MIT)5//6// Permission is hereby granted, free of charge, to any person obtaining a copy7// of this software and associated documentation files(the "Software"), to deal8// in the Software without restriction, including without limitation the rights9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10// copies of the Software, and to permit persons to whom the Software is11// furnished to do so, subject to the following conditions :12//13// The above copyright notice and this permission notice shall be included in14// all copies or substantial portions of the Software.15//16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN22// THE SOFTWARE.2324#pragma once2526#include <collection.h>27#include <ppltasks.h>2829#include <wrl\implements.h>30#include <wrl\wrappers\corewrappers.h>31#include <Roerrorapi.h>3233#include <queue>34#include <sstream>3536#include <robuffer.h>3738#include <mfapi.h>39#include <mfidl.h>40#include <Mferror.h>4142#include <windows.media.h>43#include <windows.media.mediaproperties.h>4445namespace AWM = ::ABI::Windows::Media;46namespace AWMMp = ::ABI::Windows::Media::MediaProperties;47namespace AWFC = ::ABI::Windows::Foundation::Collections;48namespace MW = ::Microsoft::WRL;49namespace MWD = ::Microsoft::WRL::Details;50namespace MWW = ::Microsoft::WRL::Wrappers;51namespace WMC = ::Windows::Media::Capture;52namespace WF = ::Windows::Foundation;53namespace WMMp = ::Windows::Media::MediaProperties;54namespace WSS = ::Windows::Storage::Streams;5556// Exception-based error handling57#define CHK(statement) {HRESULT _hr = (statement); if (FAILED(_hr)) { throw ref new Platform::COMException(_hr); };}58#define CHKNULL(p) {if ((p) == nullptr) { throw ref new Platform::NullReferenceException(L#p); };}5960// Exception-free error handling61#define CHK_RETURN(statement) {hr = (statement); if (FAILED(hr)) { return hr; };}6263// Cast a C++/CX msartpointer to an ABI smartpointer64template<typename T, typename U>65MW::ComPtr<T> As(U^ in)66{67MW::ComPtr<T> out;68CHK(reinterpret_cast<IInspectable*>(in)->QueryInterface(IID_PPV_ARGS(&out)));69return out;70}7172// Cast an ABI smartpointer73template<typename T, typename U>74Microsoft::WRL::ComPtr<T> As(const Microsoft::WRL::ComPtr<U>& in)75{76Microsoft::WRL::ComPtr<T> out;77CHK(in.As(&out));78return out;79}8081// Cast an ABI smartpointer82template<typename T, typename U>83Microsoft::WRL::ComPtr<T> As(U* in)84{85Microsoft::WRL::ComPtr<T> out;86CHK(in->QueryInterface(IID_PPV_ARGS(&out)));87return out;88}8990// Get access to bytes in IBuffer91inline unsigned char* GetData(_In_ WSS::IBuffer^ buffer)92{93unsigned char* bytes = nullptr;94CHK(As<WSS::IBufferByteAccess>(buffer)->Buffer(&bytes));95return bytes;96}9798// Class to start and shutdown Media Foundation99class AutoMF100{101public:102AutoMF()103: _bInitialized(false)104{105CHK(MFStartup(MF_VERSION));106}107108~AutoMF()109{110if (_bInitialized)111{112(void)MFShutdown();113}114}115116private:117bool _bInitialized;118};119120// Class to track error origin121template <size_t N>122HRESULT OriginateError(__in HRESULT hr, __in wchar_t const (&str)[N])123{124if (FAILED(hr))125{126::RoOriginateErrorW(hr, N - 1, str);127}128return hr;129}130131// Class to track error origin132inline HRESULT OriginateError(__in HRESULT hr)133{134if (FAILED(hr))135{136::RoOriginateErrorW(hr, 0, nullptr);137}138return hr;139}140141// Converts exceptions into HRESULTs142template <typename Lambda>143HRESULT ExceptionBoundary(Lambda&& lambda)144{145try146{147lambda();148return S_OK;149}150catch (Platform::Exception^ e)151{152return e->HResult;153}154catch (const std::bad_alloc&)155{156return E_OUTOFMEMORY;157}158catch (const std::exception&)159{160return E_FAIL;161}162}163164// Wraps an IMFSample in a C++/CX class to be able to define a callback delegate165ref class MediaSample sealed166{167internal:168MW::ComPtr<IMFSample> Sample;169};170171delegate void MediaSampleHandler(MediaSample^ sample);172173