plugin-main.m 10 KB

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