DeckLinkAPIDispatch.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include <stdio.h>
  28. #include <pthread.h>
  29. #include <dlfcn.h>
  30. #include "DeckLinkAPI.h"
  31. #define kDeckLinkAPI_Name "libDeckLinkAPI.so"
  32. #define KDeckLinkPreviewAPI_Name "libDeckLinkPreviewAPI.so"
  33. typedef IDeckLinkIterator* (*CreateIteratorFunc)(void);
  34. typedef IDeckLinkAPIInformation* (*CreateAPIInformationFunc)(void);
  35. typedef IDeckLinkGLScreenPreviewHelper* (*CreateOpenGLScreenPreviewHelperFunc)(void);
  36. typedef IDeckLinkVideoConversion* (*CreateVideoConversionInstanceFunc)(void);
  37. typedef IDeckLinkDiscovery* (*CreateDeckLinkDiscoveryInstanceFunc)(void);
  38. typedef IDeckLinkVideoFrameAncillaryPackets* (*CreateVideoFrameAncillaryPacketsInstanceFunc)(void);
  39. static pthread_once_t gDeckLinkOnceControl = PTHREAD_ONCE_INIT;
  40. static pthread_once_t gPreviewOnceControl = PTHREAD_ONCE_INIT;
  41. static bool gLoadedDeckLinkAPI = false;
  42. static CreateIteratorFunc gCreateIteratorFunc = NULL;
  43. static CreateAPIInformationFunc gCreateAPIInformationFunc = NULL;
  44. static CreateOpenGLScreenPreviewHelperFunc gCreateOpenGLPreviewFunc = NULL;
  45. static CreateVideoConversionInstanceFunc gCreateVideoConversionFunc = NULL;
  46. static CreateDeckLinkDiscoveryInstanceFunc gCreateDeckLinkDiscoveryFunc = NULL;
  47. static CreateVideoFrameAncillaryPacketsInstanceFunc gCreateVideoFrameAncillaryPacketsFunc = NULL;
  48. void InitDeckLinkAPI (void)
  49. {
  50. void *libraryHandle;
  51. libraryHandle = dlopen(kDeckLinkAPI_Name, RTLD_NOW|RTLD_GLOBAL);
  52. if (!libraryHandle)
  53. {
  54. fprintf(stderr, "%s\n", dlerror());
  55. return;
  56. }
  57. gLoadedDeckLinkAPI = true;
  58. gCreateIteratorFunc = (CreateIteratorFunc)dlsym(libraryHandle, "CreateDeckLinkIteratorInstance_0003");
  59. if (!gCreateIteratorFunc)
  60. fprintf(stderr, "%s\n", dlerror());
  61. gCreateAPIInformationFunc = (CreateAPIInformationFunc)dlsym(libraryHandle, "CreateDeckLinkAPIInformationInstance_0001");
  62. if (!gCreateAPIInformationFunc)
  63. fprintf(stderr, "%s\n", dlerror());
  64. gCreateVideoConversionFunc = (CreateVideoConversionInstanceFunc)dlsym(libraryHandle, "CreateVideoConversionInstance_0001");
  65. if (!gCreateVideoConversionFunc)
  66. fprintf(stderr, "%s\n", dlerror());
  67. gCreateDeckLinkDiscoveryFunc = (CreateDeckLinkDiscoveryInstanceFunc)dlsym(libraryHandle, "CreateDeckLinkDiscoveryInstance_0002");
  68. if (!gCreateDeckLinkDiscoveryFunc)
  69. fprintf(stderr, "%s\n", dlerror());
  70. gCreateVideoFrameAncillaryPacketsFunc = (CreateVideoFrameAncillaryPacketsInstanceFunc)dlsym(libraryHandle, "CreateVideoFrameAncillaryPacketsInstance_0001");
  71. if (!gCreateVideoFrameAncillaryPacketsFunc)
  72. fprintf(stderr, "%s\n", dlerror());
  73. }
  74. void InitDeckLinkPreviewAPI (void)
  75. {
  76. void *libraryHandle;
  77. libraryHandle = dlopen(KDeckLinkPreviewAPI_Name, RTLD_NOW|RTLD_GLOBAL);
  78. if (!libraryHandle)
  79. {
  80. fprintf(stderr, "%s\n", dlerror());
  81. return;
  82. }
  83. gCreateOpenGLPreviewFunc = (CreateOpenGLScreenPreviewHelperFunc)dlsym(libraryHandle, "CreateOpenGLScreenPreviewHelper_0001");
  84. if (!gCreateOpenGLPreviewFunc)
  85. fprintf(stderr, "%s\n", dlerror());
  86. }
  87. bool IsDeckLinkAPIPresent (void)
  88. {
  89. // If the DeckLink API dynamic library was successfully loaded, return this knowledge to the caller
  90. return gLoadedDeckLinkAPI;
  91. }
  92. IDeckLinkIterator* CreateDeckLinkIteratorInstance (void)
  93. {
  94. pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
  95. if (gCreateIteratorFunc == NULL)
  96. return NULL;
  97. return gCreateIteratorFunc();
  98. }
  99. IDeckLinkAPIInformation* CreateDeckLinkAPIInformationInstance (void)
  100. {
  101. pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
  102. if (gCreateAPIInformationFunc == NULL)
  103. return NULL;
  104. return gCreateAPIInformationFunc();
  105. }
  106. IDeckLinkGLScreenPreviewHelper* CreateOpenGLScreenPreviewHelper (void)
  107. {
  108. pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
  109. pthread_once(&gPreviewOnceControl, InitDeckLinkPreviewAPI);
  110. if (gCreateOpenGLPreviewFunc == NULL)
  111. return NULL;
  112. return gCreateOpenGLPreviewFunc();
  113. }
  114. IDeckLinkVideoConversion* CreateVideoConversionInstance (void)
  115. {
  116. pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
  117. if (gCreateVideoConversionFunc == NULL)
  118. return NULL;
  119. return gCreateVideoConversionFunc();
  120. }
  121. IDeckLinkDiscovery* CreateDeckLinkDiscoveryInstance (void)
  122. {
  123. pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
  124. if (gCreateDeckLinkDiscoveryFunc == NULL)
  125. return NULL;
  126. return gCreateDeckLinkDiscoveryFunc();
  127. }
  128. IDeckLinkVideoFrameAncillaryPackets* CreateVideoFrameAncillaryPacketsInstance (void)
  129. {
  130. pthread_once(&gDeckLinkOnceControl, InitDeckLinkAPI);
  131. if (gCreateVideoFrameAncillaryPacketsFunc == NULL)
  132. return NULL;
  133. return gCreateVideoFrameAncillaryPacketsFunc();
  134. }