plugin-main.m 11 KB

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