plugin-main.m 11 KB

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