1 /* -LICENSE-START-
2 ** Copyright (c) 2009 Blackmagic Design
3 **
4 ** Permission is hereby granted, free of charge, to any person or organization
5 ** obtaining a copy of the software and accompanying documentation covered by
6 ** this license (the "Software") to use, reproduce, display, distribute,
7 ** execute, and transmit the Software, and to prepare derivative works of the
8 ** Software, and to permit third-parties to whom the Software is furnished to
9 ** do so, all subject to the following:
10 **
11 ** The copyright notices in the Software and this entire statement, including
12 ** the above license grant, this restriction and the following disclaimer,
13 ** must be included in all copies of the Software, in whole or in part, and
14 ** all derivative works of the Software, unless such copies or derivative
15 ** works are solely in the form of machine-executable object code generated by
16 ** a source language processor.
17 **
18 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 ** FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21 ** SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22 ** FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23 ** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 ** DEALINGS IN THE SOFTWARE.
25 ** -LICENSE-END-
26 */
27 /* DeckLinkAPIDispatch.cpp */
28
29 #include "DeckLinkAPI.h"
30 #include <pthread.h>
31
32 #if BLACKMAGIC_DECKLINK_API_MAGIC != 1
33 #error The DeckLink API version of DeckLinkAPIDispatch.cpp is not the same version as DeckLinkAPI.h
34 #endif
35
36 #define kDeckLinkAPI_BundlePath "/Library/Frameworks/DeckLinkAPI.framework"
37
38
39 typedef IDeckLinkIterator* (*CreateIteratorFunc)(void);
40 typedef IDeckLinkAPIInformation* (*CreateAPIInformationFunc)(void);
41 typedef IDeckLinkGLScreenPreviewHelper* (*CreateOpenGLScreenPreviewHelperFunc)(void);
42 typedef IDeckLinkCocoaScreenPreviewCallback* (*CreateCocoaScreenPreviewFunc)(void*);
43 typedef IDeckLinkVideoConversion* (*CreateVideoConversionInstanceFunc)(void);
44 typedef IDeckLinkDiscovery* (*CreateDeckLinkDiscoveryInstanceFunc)(void);
45 typedef IDeckLinkVideoFrameAncillaryPackets* (*CreateVideoFrameAncillaryPacketsInstanceFunc)(void);
46
47 static pthread_once_t gDeckLinkOnceControl = PTHREAD_ONCE_INIT;
48 static CFBundleRef gDeckLinkAPIBundleRef = NULL;
49 static CreateIteratorFunc gCreateIteratorFunc = NULL;
50 static CreateAPIInformationFunc gCreateAPIInformationFunc = NULL;
51 static CreateOpenGLScreenPreviewHelperFunc gCreateOpenGLPreviewFunc = NULL;
52 static CreateCocoaScreenPreviewFunc gCreateCocoaPreviewFunc = NULL;
53 static CreateVideoConversionInstanceFunc gCreateVideoConversionFunc = NULL;
54 static CreateDeckLinkDiscoveryInstanceFunc gCreateDeckLinkDiscoveryFunc= NULL;
55 static CreateVideoFrameAncillaryPacketsInstanceFunc gCreateVideoFrameAncillaryPacketsFunc = NULL;
56
57
InitDeckLinkAPI(void)58 static void InitDeckLinkAPI (void)
59 {
60 CFURLRef bundleURL;
61
62 bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR(kDeckLinkAPI_BundlePath), kCFURLPOSIXPathStyle, true);
63 if (bundleURL != NULL)
64 {
65 gDeckLinkAPIBundleRef = CFBundleCreate(kCFAllocatorDefault, bundleURL);
66 if (gDeckLinkAPIBundleRef != NULL)
67 {
68 gCreateIteratorFunc = (CreateIteratorFunc)CFBundleGetFunctionPointerForName(gDeckLinkAPIBundleRef, CFSTR("CreateDeckLinkIteratorInstance_0004"));
69 gCreateAPIInformationFunc = (CreateAPIInformationFunc)CFBundleGetFunctionPointerForName(gDeckLinkAPIBundleRef, CFSTR("CreateDeckLinkAPIInformationInstance_0001"));
70 gCreateOpenGLPreviewFunc = (CreateOpenGLScreenPreviewHelperFunc)CFBundleGetFunctionPointerForName(gDeckLinkAPIBundleRef, CFSTR("CreateOpenGLScreenPreviewHelper_0001"));
71 gCreateCocoaPreviewFunc = (CreateCocoaScreenPreviewFunc)CFBundleGetFunctionPointerForName(gDeckLinkAPIBundleRef, CFSTR("CreateCocoaScreenPreview_0001"));
72 gCreateVideoConversionFunc = (CreateVideoConversionInstanceFunc)CFBundleGetFunctionPointerForName(gDeckLinkAPIBundleRef, CFSTR("CreateVideoConversionInstance_0001"));
73 gCreateDeckLinkDiscoveryFunc = (CreateDeckLinkDiscoveryInstanceFunc)CFBundleGetFunctionPointerForName(gDeckLinkAPIBundleRef, CFSTR("CreateDeckLinkDiscoveryInstance_0003"));
74 gCreateVideoFrameAncillaryPacketsFunc = (CreateVideoFrameAncillaryPacketsInstanceFunc)CFBundleGetFunctionPointerForName(gDeckLinkAPIBundleRef, CFSTR("CreateVideoFrameAncillaryPacketsInstance_0001"));
75 }
76 CFRelease(bundleURL);
77 }
78 }
79
80 #if 0
81 bool IsDeckLinkAPIPresent (void)
82 {
83 // If the DeckLink API bundle was successfully loaded, return this knowledge to the caller
84 if (gDeckLinkAPIBundleRef != NULL)
85 return true;
86
87 return false;
88 }
89 #endif
90
CreateDeckLinkIteratorInstance(void)91 IDeckLinkIterator* CreateDeckLinkIteratorInstance (void)
92 {
93 pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
94
95 if (gCreateIteratorFunc == NULL)
96 return NULL;
97
98 return gCreateIteratorFunc();
99 }
100
CreateDeckLinkAPIInformationInstance(void)101 IDeckLinkAPIInformation* CreateDeckLinkAPIInformationInstance (void)
102 {
103 pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
104
105 if (gCreateAPIInformationFunc == NULL)
106 return NULL;
107
108 return gCreateAPIInformationFunc();
109 }
110
CreateOpenGLScreenPreviewHelper(void)111 IDeckLinkGLScreenPreviewHelper* CreateOpenGLScreenPreviewHelper (void)
112 {
113 pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
114
115 if (gCreateOpenGLPreviewFunc == NULL)
116 return NULL;
117
118 return gCreateOpenGLPreviewFunc();
119 }
120
CreateCocoaScreenPreview(void * parentView)121 IDeckLinkCocoaScreenPreviewCallback* CreateCocoaScreenPreview (void* parentView)
122 {
123 pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
124
125 if (gCreateCocoaPreviewFunc == NULL)
126 return NULL;
127
128 return gCreateCocoaPreviewFunc(parentView);
129 }
130
CreateVideoConversionInstance(void)131 IDeckLinkVideoConversion* CreateVideoConversionInstance (void)
132 {
133 pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
134
135 if (gCreateVideoConversionFunc == NULL)
136 return NULL;
137
138 return gCreateVideoConversionFunc();
139 }
140
CreateDeckLinkDiscoveryInstance(void)141 IDeckLinkDiscovery* CreateDeckLinkDiscoveryInstance (void)
142 {
143 pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
144
145 if (gCreateDeckLinkDiscoveryFunc == NULL)
146 return NULL;
147
148 return gCreateDeckLinkDiscoveryFunc();
149 }
150
CreateVideoFrameAncillaryPacketsInstance(void)151 IDeckLinkVideoFrameAncillaryPackets* CreateVideoFrameAncillaryPacketsInstance (void)
152 {
153 pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
154
155 if (gCreateVideoFrameAncillaryPacketsFunc == NULL)
156 return NULL;
157
158 return gCreateVideoFrameAncillaryPacketsFunc();
159 }
160
161
162 #define kBMDStreamingAPI_BundlePath "/Library/Application Support/Blackmagic Design/Streaming/BMDStreamingAPI.bundle"
163
164 typedef IBMDStreamingDiscovery* (*CreateDiscoveryFunc)(void);
165 typedef IBMDStreamingH264NALParser* (*CreateNALParserFunc)(void);
166
167 static pthread_once_t gBMDStreamingOnceControl = PTHREAD_ONCE_INIT;
168 static CFBundleRef gBMDStreamingAPIBundleRef = NULL;
169 static CreateDiscoveryFunc gCreateDiscoveryFunc = NULL;
170 static CreateNALParserFunc gCreateNALParserFunc = NULL;
171
InitBMDStreamingAPI(void)172 static void InitBMDStreamingAPI(void)
173 {
174 CFURLRef bundleURL;
175
176 bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR(kBMDStreamingAPI_BundlePath), kCFURLPOSIXPathStyle, true);
177 if (bundleURL != NULL)
178 {
179 gBMDStreamingAPIBundleRef = CFBundleCreate(kCFAllocatorDefault, bundleURL);
180 if (gBMDStreamingAPIBundleRef != NULL)
181 {
182 gCreateDiscoveryFunc = (CreateDiscoveryFunc)CFBundleGetFunctionPointerForName(gBMDStreamingAPIBundleRef, CFSTR("CreateBMDStreamingDiscoveryInstance_0002"));
183 gCreateNALParserFunc = (CreateNALParserFunc)CFBundleGetFunctionPointerForName(gBMDStreamingAPIBundleRef, CFSTR("CreateBMDStreamingH264NALParser_0001"));
184 }
185
186 CFRelease(bundleURL);
187 }
188 }
189
CreateBMDStreamingDiscoveryInstance()190 IBMDStreamingDiscovery* CreateBMDStreamingDiscoveryInstance()
191 {
192 pthread_once(&gBMDStreamingOnceControl, InitBMDStreamingAPI);
193
194 if (gCreateDiscoveryFunc == NULL)
195 return NULL;
196
197 return gCreateDiscoveryFunc();
198 }
199
CreateBMDStreamingH264NALParser()200 IBMDStreamingH264NALParser* CreateBMDStreamingH264NALParser()
201 {
202 pthread_once(&gBMDStreamingOnceControl, InitBMDStreamingAPI);
203
204 if (gCreateNALParserFunc == NULL)
205 return NULL;
206
207 return gCreateNALParserFunc();
208 }
209