]> git.sesse.net Git - casparcg/blob - modules/decklink/decklink_api.h
5d73fbd27d093708a9bbb2859e54535a4687f0aa
[casparcg] / modules / decklink / decklink_api.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #pragma once
23
24 #include <common/except.h>
25
26 #if defined(_MSC_VER)
27
28 #include "interop/DeckLinkAPI.h"
29
30 #pragma warning(push)
31 #pragma warning(disable : 4996)
32
33     #include <atlbase.h>
34
35     #include <atlcom.h>
36     #include <atlhost.h>
37
38 #pragma warning(pop)
39
40 namespace caspar { namespace decklink {
41
42 typedef BSTR String;
43 typedef unsigned int UINT32;
44
45 static void com_initialize()
46 {
47         ::CoInitialize(nullptr);
48 }
49
50 static void com_uninitialize()
51 {
52         ::CoUninitialize();
53 }
54
55 struct co_init
56 {
57     co_init(){ ::CoInitialize(nullptr); }
58     ~co_init(){ ::CoUninitialize(); }
59 };
60
61 template<typename T>
62 using com_ptr = CComPtr<T>;
63
64 template<typename T>
65 using com_iface_ptr = CComQIPtr<T>;
66
67 template<template<typename> class P, typename T>
68 static P<T> wrap_raw(T* ptr, bool already_referenced = false)
69 {
70     if (already_referenced)
71     {
72         P<T> p;
73         p.Attach(ptr);
74         return p;
75     }
76     else
77         return P<T>(ptr);
78 }
79
80 static com_ptr<IDeckLinkIterator> create_iterator()
81 {
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;
86 }
87
88 template<typename I, typename T>
89 static com_iface_ptr<I> iface_cast(const com_ptr<T>& ptr)
90 {
91         com_iface_ptr<I> result = ptr;
92
93         if (!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."));
95
96         return result;
97 }
98
99 template<typename T>
100 T* get_raw(const CComPtr<T>& ptr)
101 {
102         return ptr;
103 }
104
105 }}
106
107 #else
108
109 #include "linux_interop/DeckLinkAPI.h"
110 #include <memory>
111 #include <typeinfo>
112
113 namespace caspar { namespace decklink {
114
115 typedef const char* String;
116 typedef bool BOOL;
117 #define TRUE true
118 #define FALSE false
119 typedef uint32_t UINT32;
120
121 static void com_initialize()
122 {
123 }
124
125 static void com_uninitialize()
126 {
127 }
128
129 struct co_init
130 {
131     co_init(){ }
132     ~co_init(){ }
133 };
134
135 template<typename T>
136 using com_ptr = std::shared_ptr<T>;
137
138 template<typename T>
139 using com_iface_ptr = std::shared_ptr<T>;
140
141 template<template<typename> class P, typename T>
142 static P<T> wrap_raw(T* ptr, bool already_referenced = false)
143 {
144     if (!already_referenced && ptr)
145         ptr->AddRef();
146
147         return P<T>(ptr, [](T* p)
148         {
149                 if (p)
150                 {
151                         auto remaining_refs = p->Release();
152
153                         //CASPAR_LOG(debug) << "Remaining references for " << typeid(p).name() << " = " << remaining_refs;
154                 }
155         });
156 }
157
158 static com_ptr<IDeckLinkIterator> create_iterator()
159 {
160     IDeckLinkIterator* iterator = CreateDeckLinkIteratorInstance();
161
162     if (iterator == nullptr)
163         CASPAR_THROW_EXCEPTION(not_supported() << msg_info("Decklink drivers not found."));
164
165         return wrap_raw<com_ptr>(iterator, true);
166 }
167
168 template<typename T>    static REFIID iface_id() { return T::REFID; }
169 template<>              REFIID iface_id<IDeckLink>() { return IID_IDeckLink; }
170 template<>              REFIID iface_id<IDeckLinkOutput>() { return IID_IDeckLinkOutput; }
171 template<>              REFIID iface_id<IDeckLinkAPIInformation>() { return IID_IDeckLinkAPIInformation; }
172 template<>              REFIID iface_id<IDeckLinkConfiguration>() { return IID_IDeckLinkConfiguration; }
173 template<>              REFIID iface_id<IDeckLinkKeyer>() { return IID_IDeckLinkKeyer; }
174 template<>              REFIID iface_id<IDeckLinkAttributes>() { return IID_IDeckLinkAttributes; }
175 template<>              REFIID iface_id<IDeckLinkInput>() { return IID_IDeckLinkInput; }
176
177 template<typename I, typename T>
178 static com_iface_ptr<I> iface_cast(com_ptr<T> ptr)
179 {
180     I* iface;
181     ptr->QueryInterface(iface_id<I>(), reinterpret_cast<void**>(&iface));
182
183         return wrap_raw<com_iface_ptr>(iface, true);
184 }
185
186 template<typename T>
187 T* get_raw(const std::shared_ptr<T>& ptr)
188 {
189         return ptr.get();
190 }
191
192 }}
193
194 #endif