plugin-main.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // plugin-main.m
  3. // mac-avcapture
  4. //
  5. // Created by Patrick Heyer on 2023-03-07.
  6. //
  7. #import "plugin-main.h"
  8. #pragma mark av-capture API
  9. const char *av_capture_get_text(const char *text_id)
  10. {
  11. return obs_module_text(text_id);
  12. }
  13. static void *av_capture_create(obs_data_t *settings, obs_source_t *source)
  14. {
  15. OBSAVCaptureInfo *capture_data = bzalloc(sizeof(OBSAVCaptureInfo));
  16. capture_data->isFastPath = false;
  17. capture_data->settings = settings;
  18. capture_data->source = source;
  19. capture_data->videoFrame = bzalloc(sizeof(OBSAVCaptureVideoFrame));
  20. capture_data->audioFrame = bzalloc(sizeof(OBSAVCaptureAudioFrame));
  21. OBSAVCapture *capture = [[OBSAVCapture alloc] initWithCaptureInfo:capture_data];
  22. return (void *) CFBridgingRetain(capture);
  23. }
  24. static void *av_fast_capture_create(obs_data_t *settings, obs_source_t *source)
  25. {
  26. OBSAVCaptureInfo *capture_info = bzalloc(sizeof(OBSAVCaptureInfo));
  27. capture_info->isFastPath = true;
  28. capture_info->settings = settings;
  29. capture_info->source = source;
  30. if (gs_get_device_type() == GS_DEVICE_OPENGL) {
  31. capture_info->effect = obs_get_base_effect(OBS_EFFECT_DEFAULT_RECT);
  32. } else {
  33. capture_info->effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  34. }
  35. capture_info->frameSize = CGRectZero;
  36. if (!capture_info->effect) {
  37. return NULL;
  38. }
  39. pthread_mutex_init(&capture_info->mutex, NULL);
  40. OBSAVCapture *capture = [[OBSAVCapture alloc] initWithCaptureInfo:capture_info];
  41. return (void *) CFBridgingRetain(capture);
  42. }
  43. static const char *av_capture_get_name(void *av_capture __unused)
  44. {
  45. return obs_module_text("AVCapture");
  46. }
  47. static const char *av_fast_capture_get_name(void *av_capture __unused)
  48. {
  49. return obs_module_text("AVCapture_Fast");
  50. }
  51. static void av_capture_set_defaults(obs_data_t *settings)
  52. {
  53. obs_data_set_default_string(settings, "device", "");
  54. obs_data_set_default_bool(settings, "use_preset", true);
  55. obs_data_set_default_string(settings, "preset", AVCaptureSessionPresetHigh.UTF8String);
  56. obs_data_set_default_bool(settings, "enable_audio", true);
  57. }
  58. static void av_fast_capture_set_defaults(obs_data_t *settings)
  59. {
  60. obs_data_set_default_string(settings, "device", "");
  61. obs_data_set_default_bool(settings, "use_preset", false);
  62. obs_data_set_default_bool(settings, "enable_audio", true);
  63. }
  64. static obs_properties_t *av_capture_properties(void *av_capture)
  65. {
  66. OBSAVCapture *capture = (__bridge OBSAVCapture *) (av_capture);
  67. OBSAVCaptureInfo *capture_info = capture.captureInfo;
  68. AVCaptureDevice *device = capture.deviceInput.device;
  69. NSString *effectsWarningKey = [OBSAVCapture effectsWarningForDevice:device];
  70. obs_properties_t *properties = obs_properties_create();
  71. // Create Properties
  72. obs_property_t *device_list = obs_properties_add_list(properties, "device", obs_module_text("Device"),
  73. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  74. obs_property_t *effects_warning;
  75. if (effectsWarningKey) {
  76. effects_warning = obs_properties_add_text(properties, "effects_warning",
  77. obs_module_text(effectsWarningKey.UTF8String), OBS_TEXT_INFO);
  78. obs_property_text_set_info_type(effects_warning, OBS_TEXT_INFO_WARNING);
  79. }
  80. obs_property_t *use_preset = obs_properties_add_bool(properties, "use_preset", obs_module_text("UsePreset"));
  81. obs_property_t *preset_list = obs_properties_add_list(properties, "preset", obs_module_text("Preset"),
  82. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  83. obs_property_t *supported_formats = obs_properties_add_list(
  84. properties, "supported_format", obs_module_text("InputFormat"), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  85. obs_property_t *use_buffering = obs_properties_add_bool(properties, "buffering", obs_module_text("Buffering"));
  86. obs_property_t *frame_rates = obs_properties_add_frame_rate(properties, "frame_rate", obs_module_text("FrameRate"));
  87. if (capture_info) {
  88. bool isFastPath = capture_info->isFastPath;
  89. // Add Property Visibility and Callbacks
  90. configure_property(device_list, true, true, properties_changed, capture);
  91. if (effectsWarningKey) {
  92. configure_property(effects_warning, true, true, NULL, NULL);
  93. }
  94. configure_property(use_preset, !isFastPath, !isFastPath, (!isFastPath) ? properties_changed_use_preset : NULL,
  95. capture);
  96. configure_property(preset_list, !isFastPath, !isFastPath, (!isFastPath) ? properties_changed_preset : NULL,
  97. capture);
  98. configure_property(supported_formats, true, true, properties_changed, capture);
  99. configure_property(use_buffering, !isFastPath, !isFastPath, NULL, NULL);
  100. configure_property(frame_rates, isFastPath, isFastPath, NULL, NULL);
  101. }
  102. return properties;
  103. }
  104. static void av_capture_update(void *av_capture, obs_data_t *settings)
  105. {
  106. OBSAVCapture *capture = (__bridge OBSAVCapture *) (av_capture);
  107. capture.captureInfo->settings = settings;
  108. [capture updateSessionwithError:NULL];
  109. }
  110. static void av_fast_capture_tick(void *av_capture, float seconds __unused)
  111. {
  112. OBSAVCapture *capture = (__bridge OBSAVCapture *) (av_capture);
  113. OBSAVCaptureInfo *capture_info = capture.captureInfo;
  114. if (!capture_info->currentSurface) {
  115. return;
  116. }
  117. if (!obs_source_showing(capture_info->source)) {
  118. return;
  119. }
  120. IOSurfaceRef previousSurface = capture_info->previousSurface;
  121. if (pthread_mutex_lock(&capture_info->mutex)) {
  122. return;
  123. }
  124. capture_info->previousSurface = capture_info->currentSurface;
  125. capture_info->currentSurface = NULL;
  126. pthread_mutex_unlock(&capture_info->mutex);
  127. if (previousSurface == capture_info->previousSurface) {
  128. return;
  129. }
  130. if (capture_info->previousSurface) {
  131. obs_enter_graphics();
  132. if (capture_info->texture) {
  133. gs_texture_rebind_iosurface(capture_info->texture, capture_info->previousSurface);
  134. } else {
  135. capture_info->texture = gs_texture_create_from_iosurface(capture_info->previousSurface);
  136. }
  137. obs_leave_graphics();
  138. }
  139. if (previousSurface) {
  140. IOSurfaceDecrementUseCount(previousSurface);
  141. CFRelease(previousSurface);
  142. }
  143. }
  144. static void av_fast_capture_render(void *av_capture, gs_effect_t *effect __unused)
  145. {
  146. OBSAVCapture *capture = (__bridge OBSAVCapture *) (av_capture);
  147. OBSAVCaptureInfo *capture_info = capture.captureInfo;
  148. if (!capture_info->texture) {
  149. return;
  150. }
  151. const bool linear_srgb = gs_get_linear_srgb();
  152. const bool previous = gs_framebuffer_srgb_enabled();
  153. gs_enable_framebuffer_srgb(linear_srgb);
  154. gs_eparam_t *param = gs_effect_get_param_by_name(capture_info->effect, "image");
  155. gs_effect_set_texture_srgb(param, capture_info->texture);
  156. if (linear_srgb) {
  157. gs_effect_set_texture_srgb(param, capture_info->texture);
  158. } else {
  159. gs_effect_set_texture(param, capture_info->texture);
  160. }
  161. while (gs_effect_loop(capture_info->effect, "Draw")) {
  162. gs_draw_sprite(capture_info->texture, 0, 0, 0);
  163. }
  164. gs_enable_framebuffer_srgb(previous);
  165. }
  166. static UInt32 av_fast_capture_get_width(void *av_capture)
  167. {
  168. OBSAVCapture *capture = (__bridge OBSAVCapture *) (av_capture);
  169. OBSAVCaptureInfo *capture_info = capture.captureInfo;
  170. CGSize frameSize = capture_info->frameSize.size;
  171. return (UInt32) frameSize.width;
  172. }
  173. static UInt32 av_fast_capture_get_height(void *av_capture)
  174. {
  175. OBSAVCapture *capture = (__bridge OBSAVCapture *) (av_capture);
  176. OBSAVCaptureInfo *capture_info = capture.captureInfo;
  177. CGSize frameSize = capture_info->frameSize.size;
  178. return (UInt32) frameSize.height;
  179. }
  180. static void av_capture_destroy(void *av_capture)
  181. {
  182. OBSAVCapture *capture = (__bridge OBSAVCapture *) (av_capture);
  183. if (!capture) {
  184. return;
  185. }
  186. /// It is possible that the source's serial queue is still creating this source, so perform destruction
  187. /// synchronously on that queue to ensure the source is fully initialized before being destroyed.
  188. dispatch_sync(capture.sessionQueue, ^{
  189. OBSAVCaptureInfo *capture_info = capture.captureInfo;
  190. [capture stopCaptureSession];
  191. [capture.deviceInput.device unlockForConfiguration];
  192. if (capture_info->isFastPath) {
  193. pthread_mutex_destroy(&capture_info->mutex);
  194. }
  195. if (capture_info->videoFrame) {
  196. bfree(capture_info->videoFrame);
  197. capture_info->videoFrame = NULL;
  198. }
  199. if (capture_info->audioFrame) {
  200. bfree(capture_info->audioFrame);
  201. capture_info->audioFrame = NULL;
  202. }
  203. if (capture_info->sampleBufferDescription) {
  204. capture_info->sampleBufferDescription = NULL;
  205. }
  206. bfree(capture_info);
  207. CFBridgingRelease((__bridge CFTypeRef _Nullable)(capture));
  208. });
  209. }
  210. #pragma mark - OBS Module API
  211. OBS_DECLARE_MODULE()
  212. OBS_MODULE_USE_DEFAULT_LOCALE("macOS-avcapture", "en-US")
  213. MODULE_EXPORT const char *obs_module_description(void)
  214. {
  215. return "macOS AVFoundation Capture Source";
  216. }
  217. bool obs_module_load(void)
  218. {
  219. struct obs_source_info av_capture_info = {
  220. .id = "macos-avcapture",
  221. .type = OBS_SOURCE_TYPE_INPUT,
  222. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE,
  223. .create = av_capture_create,
  224. .get_name = av_capture_get_name,
  225. .get_defaults = av_capture_set_defaults,
  226. .get_properties = av_capture_properties,
  227. .update = av_capture_update,
  228. .destroy = av_capture_destroy,
  229. .icon_type = OBS_ICON_TYPE_CAMERA,
  230. };
  231. obs_register_source(&av_capture_info);
  232. struct obs_source_info av_capture_sync_info = {
  233. .id = "macos-avcapture-fast",
  234. .type = OBS_SOURCE_TYPE_INPUT,
  235. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW | OBS_SOURCE_AUDIO | OBS_SOURCE_SRGB |
  236. OBS_SOURCE_DO_NOT_DUPLICATE,
  237. .create = av_fast_capture_create,
  238. .get_name = av_fast_capture_get_name,
  239. .get_defaults = av_fast_capture_set_defaults,
  240. .get_properties = av_capture_properties,
  241. .update = av_capture_update,
  242. .destroy = av_capture_destroy,
  243. .video_tick = av_fast_capture_tick,
  244. .video_render = av_fast_capture_render,
  245. .get_width = av_fast_capture_get_width,
  246. .get_height = av_fast_capture_get_height,
  247. .icon_type = OBS_ICON_TYPE_CAMERA,
  248. };
  249. obs_register_source(&av_capture_sync_info);
  250. return true;
  251. }