2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
24 #include <common/except.h>
28 #include "interop/DeckLinkAPI.h"
31 #pragma warning(disable : 4996)
40 namespace caspar { namespace decklink {
43 typedef unsigned int UINT32;
45 static void com_initialize()
47 ::CoInitialize(nullptr);
50 static void com_uninitialize()
57 co_init(){ ::CoInitialize(nullptr); }
58 ~co_init(){ ::CoUninitialize(); }
62 using com_ptr = CComPtr<T>;
65 using com_iface_ptr = CComQIPtr<T>;
67 template<template<typename> class P, typename T>
68 static P<T> wrap_raw(T* ptr, bool already_referenced = false)
70 if (already_referenced)
80 static com_ptr<IDeckLinkIterator> create_iterator()
82 CComPtr<IDeckLinkIterator> pDecklinkIterator;
83 if(FAILED(pDecklinkIterator.CoCreateInstance(CLSID_CDeckLinkIterator)))
84 CASPAR_THROW_EXCEPTION(not_supported() << msg_info("Decklink drivers not found."));
85 return pDecklinkIterator;
88 template<typename I, typename T>
89 static com_iface_ptr<I> iface_cast(const com_ptr<T>& ptr, bool optional = false)
91 com_iface_ptr<I> result = ptr;
93 if (!optional && !result)
94 CASPAR_THROW_EXCEPTION(not_supported() << msg_info(std::string("Could not cast from ") + typeid(T).name() + " to " + typeid(I).name() + ". This is probably due to old Decklink drivers."));
100 T* get_raw(const CComPtr<T>& ptr)
109 #include "linux_interop/DeckLinkAPI.h"
110 #include "linux_interop/DeckLinkAPIConfiguration_v10_2.h"
114 namespace caspar { namespace decklink {
116 typedef const char* String;
120 typedef uint32_t UINT32;
122 static void com_initialize()
126 static void com_uninitialize()
137 using com_ptr = std::shared_ptr<T>;
140 using com_iface_ptr = std::shared_ptr<T>;
142 template<template<typename> class P, typename T>
143 static P<T> wrap_raw(T* ptr, bool already_referenced = false)
145 if (!already_referenced && ptr)
148 return P<T>(ptr, [](T* p)
152 auto remaining_refs = p->Release();
154 //CASPAR_LOG(debug) << "Remaining references for " << typeid(p).name() << " = " << remaining_refs;
159 static com_ptr<IDeckLinkIterator> create_iterator()
161 IDeckLinkIterator* iterator = CreateDeckLinkIteratorInstance();
163 if (iterator == nullptr)
164 CASPAR_THROW_EXCEPTION(not_supported() << msg_info("Decklink drivers not found."));
166 return wrap_raw<com_ptr>(iterator, true);
169 template<typename T> static REFIID iface_id() { return T::REFID; }
170 template<> REFIID iface_id<IDeckLink>() { return IID_IDeckLink; }
171 template<> REFIID iface_id<IDeckLinkOutput>() { return IID_IDeckLinkOutput; }
172 template<> REFIID iface_id<IDeckLinkAPIInformation>() { return IID_IDeckLinkAPIInformation; }
173 template<> REFIID iface_id<IDeckLinkConfiguration>() { return IID_IDeckLinkConfiguration; }
174 template<> REFIID iface_id<IDeckLinkConfiguration_v10_2>() { return IID_IDeckLinkConfiguration_v10_2; }
175 template<> REFIID iface_id<IDeckLinkKeyer>() { return IID_IDeckLinkKeyer; }
176 template<> REFIID iface_id<IDeckLinkAttributes>() { return IID_IDeckLinkAttributes; }
177 template<> REFIID iface_id<IDeckLinkInput>() { return IID_IDeckLinkInput; }
179 template<typename I, typename T>
180 static com_iface_ptr<I> iface_cast(com_ptr<T> ptr, bool optional = false)
183 ptr->QueryInterface(iface_id<I>(), reinterpret_cast<void**>(&iface));
185 return wrap_raw<com_iface_ptr>(iface, true);
189 T* get_raw(const std::shared_ptr<T>& ptr)