@native-sdk/cli 0.8.3 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/build/app.zig +50 -3
  2. package/build.zig +131 -360
  3. package/native-sdk.d.ts +4 -0
  4. package/package.json +9 -9
  5. package/packages/core/compile-surface/core.ts +90 -1
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/sdk/core.d.ts +65 -2
  9. package/packages/core/sdk/core.ts +189 -33
  10. package/packages/core/sdk/events.d.ts +14 -0
  11. package/packages/core/sdk/events.ts +18 -0
  12. package/packages/core/src/checker.ts +61 -1
  13. package/packages/core/src/devhost.ts +3 -3
  14. package/packages/core/src/diagnostics.ts +4 -4
  15. package/skill-data/core/SKILL.md +4 -2
  16. package/skill-data/core/references/project-anatomy.md +2 -1
  17. package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
  18. package/skill-data/native-ui/SKILL.md +8 -8
  19. package/skill-data/ts-core/SKILL.md +16 -13
  20. package/src/app_runner/ts_core_main.zig +25 -2
  21. package/src/embed/host.zig +6 -0
  22. package/src/embed/ui_host.zig +2 -0
  23. package/src/platform/linux/root.zig +1 -0
  24. package/src/platform/macos/appkit_host.h +10 -0
  25. package/src/platform/macos/appkit_host.m +515 -93
  26. package/src/platform/macos/capture_info_plist.c +25 -0
  27. package/src/platform/macos/cef_host.mm +22 -0
  28. package/src/platform/macos/root.zig +68 -0
  29. package/src/platform/null_platform.zig +58 -0
  30. package/src/platform/root.zig +7 -0
  31. package/src/platform/types.zig +100 -0
  32. package/src/platform/windows/root.zig +56 -0
  33. package/src/platform/windows/webview2_host.cpp +171 -0
  34. package/src/primitives/app_manifest/types.zig +2 -0
  35. package/src/primitives/canvas/code_tests.zig +9 -9
  36. package/src/primitives/canvas/events.zig +12 -11
  37. package/src/primitives/canvas/frame.zig +44 -9
  38. package/src/primitives/canvas/icons/mic.svg +1 -0
  39. package/src/primitives/canvas/icons.zig +2 -0
  40. package/src/primitives/canvas/root.zig +8 -0
  41. package/src/primitives/canvas/text_span_tests.zig +39 -0
  42. package/src/primitives/canvas/ui.zig +23 -4
  43. package/src/primitives/canvas/ui_markup.zig +15 -0
  44. package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
  45. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
  46. package/src/primitives/canvas/ui_markup_tests.zig +5 -0
  47. package/src/primitives/canvas/ui_markup_view.zig +9 -0
  48. package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
  49. package/src/primitives/canvas/ui_schema.zig +19 -10
  50. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  51. package/src/primitives/canvas/ui_tests.zig +18 -4
  52. package/src/primitives/canvas/widget_invalidation.zig +9 -2
  53. package/src/primitives/canvas/widget_render.zig +296 -114
  54. package/src/primitives/canvas/widget_runtime_tests.zig +133 -0
  55. package/src/primitives/canvas/widget_text_input.zig +10 -10
  56. package/src/primitives/canvas/widget_tree.zig +1 -1
  57. package/src/primitives/canvas/widgets.zig +29 -5
  58. package/src/root.zig +10 -0
  59. package/src/runtime/automation_widget_dispatch.zig +1 -0
  60. package/src/runtime/bridge_payload.zig +2 -0
  61. package/src/runtime/canvas_frame.zig +13 -2
  62. package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
  63. package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
  64. package/src/runtime/canvas_widget_events.zig +69 -24
  65. package/src/runtime/canvas_widget_runtime.zig +101 -6
  66. package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
  67. package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
  68. package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
  69. package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
  70. package/src/runtime/canvas_widget_state.zig +6 -1
  71. package/src/runtime/canvas_widget_text_tests.zig +112 -3
  72. package/src/runtime/effects.zig +313 -2
  73. package/src/runtime/effects_audio_capture_tests.zig +251 -0
  74. package/src/runtime/root.zig +3 -0
  75. package/src/runtime/tests.zig +1 -0
  76. package/src/runtime/ts_core_host.zig +401 -32
  77. package/src/runtime/ts_core_host_tests.zig +407 -0
  78. package/src/runtime/ts_ui_app.zig +39 -1
  79. package/src/runtime/ui_app.zig +18 -3
  80. package/src/runtime/ui_app_tests.zig +48 -0
  81. package/src/runtime/view.zig +2 -0
  82. package/src/runtime/view_widget_scroll.zig +56 -5
  83. package/src/runtime/view_widget_text.zig +24 -5
  84. package/src/runtime/view_widget_tree.zig +1 -1
  85. package/src/tooling/manifest.zig +1034 -0
  86. package/src/tooling/package.zig +837 -6
  87. package/src/tooling/raw_manifest.zig +35 -0
  88. package/src/tooling/templates.zig +55 -0
  89. package/tools/corewire/emit_facade.zig +36 -0
@@ -2,6 +2,7 @@
2
2
 
3
3
  #import <AppKit/AppKit.h>
4
4
  #import <AVFoundation/AVFoundation.h>
5
+ #import <ScreenCaptureKit/ScreenCaptureKit.h>
5
6
  /* Spectrum analysis of the app's own playback: MediaToolbox provides
6
7
  * the MTAudioProcessingTap that hands the player's PCM to the host, and
7
8
  * Accelerate (vDSP) provides the FFT that turns it into band
@@ -25,6 +26,8 @@
25
26
  #include <string.h>
26
27
 
27
28
  @class NativeSdkAppKitHost;
29
+ @class NativeSdkAudioCaptureTarget;
30
+ @class NativeSdkScreenAudioCapture;
28
31
 
29
32
  static const NSUInteger NativeSdkMaxChildWebViews = 16;
30
33
  static const NSUInteger NativeSdkMaxNativeViews = 32;
@@ -811,6 +814,13 @@ static NSMutableDictionary *NativeSdkCredentialQuery(NSString *service, NSString
811
814
  * when a new load replaces the stream; orphaned (left to finish) when
812
815
  * the stream completes naturally. */
813
816
  @property(nonatomic, strong) NSURLSessionDownloadTask *audioCacheDownload;
817
+ /* Independent microphone and system-output PCM capture. The target owns
818
+ * callback quiescence; the concrete producers own AVAudioEngine and
819
+ * ScreenCaptureKit respectively. */
820
+ @property(nonatomic, strong) NativeSdkAudioCaptureTarget *microphoneCaptureTarget;
821
+ @property(nonatomic, strong) AVAudioEngine *microphoneCaptureEngine;
822
+ @property(nonatomic, strong) NativeSdkAudioCaptureTarget *systemCaptureTarget;
823
+ @property(nonatomic, strong) NativeSdkScreenAudioCapture *systemCapture;
814
824
  /* The app's single video player and its two timers. One player is the
815
825
  * whole surface, exactly like audio: a video app shows one stream at a
816
826
  * time, and a second concurrent decode would be compositor design the
@@ -1027,6 +1037,9 @@ static NSMutableDictionary *NativeSdkCredentialQuery(NSString *service, NSString
1027
1037
  - (int)audioStop;
1028
1038
  - (int)audioSeekToMs:(uint64_t)positionMs;
1029
1039
  - (int)audioSetVolume:(double)volume;
1040
+ - (int)audioCaptureStartSource:(int)source sampleRate:(uint32_t)sampleRate channels:(uint8_t)channels pushFn:(native_sdk_appkit_audio_capture_push_t)pushFn pushContext:(void *)pushContext;
1041
+ - (int)audioCaptureStopSource:(int)source;
1042
+ - (void)startMicrophoneCaptureTarget:(NativeSdkAudioCaptureTarget *)target;
1030
1043
  - (void)emitAudioEventOfKind:(int)kind;
1031
1044
  - (void)stopAudioPositionTimer;
1032
1045
  - (void)audioInstallSpectrumTapForItem:(AVPlayerItem *)item asset:(AVURLAsset *)asset;
@@ -1749,6 +1762,29 @@ static CGFloat NativeSdkPacketTransformScale(id value) {
1749
1762
  return fmax(0.0001, fmax(xScale, yScale));
1750
1763
  }
1751
1764
 
1765
+ /* Three box filters approximate a Gaussian closely while retaining the
1766
+ * O(pixels) sliding-window implementation in vImage. `sigma` is the CSS-like
1767
+ * blur radius in device pixels; the widths below are the standard optimal
1768
+ * three-box approximation, split between the neighboring odd widths whose
1769
+ * combined variance is closest to sigma squared. */
1770
+ static void NativeSdkGaussianBoxRadii(NSUInteger sigma, NSUInteger radii[3]) {
1771
+ const double passCount = 3.0;
1772
+ const double idealWidth = sqrt((12.0 * (double)sigma * (double)sigma / passCount) + 1.0);
1773
+ NSUInteger lowerWidth = (NSUInteger)floor(idealWidth);
1774
+ if ((lowerWidth & 1u) == 0) lowerWidth -= 1;
1775
+ lowerWidth = MAX((NSUInteger)1, lowerWidth);
1776
+ const NSUInteger upperWidth = lowerWidth + 2;
1777
+ const double lowerCountValue =
1778
+ (12.0 * (double)sigma * (double)sigma - passCount * (double)lowerWidth * (double)lowerWidth -
1779
+ 4.0 * passCount * (double)lowerWidth - 3.0 * passCount) /
1780
+ (-4.0 * (double)lowerWidth - 4.0);
1781
+ const NSInteger lowerCount = MAX((NSInteger)0, MIN((NSInteger)3, (NSInteger)llround(lowerCountValue)));
1782
+ for (NSUInteger pass = 0; pass < 3; pass++) {
1783
+ const NSUInteger width = pass < (NSUInteger)lowerCount ? lowerWidth : upperWidth;
1784
+ radii[pass] = (width - 1) / 2;
1785
+ }
1786
+ }
1787
+
1752
1788
  static BOOL NativeSdkPacketApplyBlur(NSDictionary *effect, CGFloat opacity, CGContextRef context, CGFloat scale, id transformValue, BOOL hasClip, NSRect clipRect) {
1753
1789
  if (!effect || !context) return NO;
1754
1790
  void *contextData = CGBitmapContextGetData(context);
@@ -1780,16 +1816,19 @@ static BOOL NativeSdkPacketApplyBlur(NSDictionary *effect, CGFloat opacity, CGCo
1780
1816
  NSUInteger maxY = (NSUInteger)maxYFloat;
1781
1817
  if (maxX <= minX || maxY <= minY) return YES;
1782
1818
 
1783
- NSUInteger radius = (NSUInteger)llround(fmax(0.0, NativeSdkPacketNumber(effect[@"radius"], 0) * normalizedScale * NativeSdkPacketTransformScale(transformValue)));
1784
- radius = MIN(radius, (NSUInteger)64);
1785
- if (radius == 0) return YES;
1819
+ NSUInteger sigma = (NSUInteger)llround(fmax(0.0, NativeSdkPacketNumber(effect[@"radius"], 0) * normalizedScale * NativeSdkPacketTransformScale(transformValue)));
1820
+ sigma = MIN(sigma, (NSUInteger)64);
1821
+ if (sigma == 0) return YES;
1786
1822
  CGFloat mix = fmax(0.0, fmin(1.0, opacity));
1787
1823
  if (mix <= 0) return YES;
1788
1824
 
1789
- NSUInteger expandedMinX = minX > radius ? minX - radius : 0;
1790
- NSUInteger expandedMaxX = MIN((NSUInteger)width, maxX + radius);
1791
- NSUInteger expandedMinY = minY > radius ? minY - radius : 0;
1792
- NSUInteger expandedMaxY = MIN((NSUInteger)height, maxY + radius);
1825
+ NSUInteger boxRadii[3] = {0, 0, 0};
1826
+ NativeSdkGaussianBoxRadii(sigma, boxRadii);
1827
+ const NSUInteger sampleExtent = boxRadii[0] + boxRadii[1] + boxRadii[2];
1828
+ NSUInteger expandedMinX = minX > sampleExtent ? minX - sampleExtent : 0;
1829
+ NSUInteger expandedMaxX = MIN((NSUInteger)width, maxX + sampleExtent);
1830
+ NSUInteger expandedMinY = minY > sampleExtent ? minY - sampleExtent : 0;
1831
+ NSUInteger expandedMaxY = MIN((NSUInteger)height, maxY + sampleExtent);
1793
1832
  if (expandedMaxX <= expandedMinX || expandedMaxY <= expandedMinY) return YES;
1794
1833
 
1795
1834
  NSUInteger regionWidth = expandedMaxX - expandedMinX;
@@ -1797,11 +1836,13 @@ static BOOL NativeSdkPacketApplyBlur(NSDictionary *effect, CGFloat opacity, CGCo
1797
1836
  size_t regionBytesPerRow = regionWidth * 4;
1798
1837
  size_t regionByteLength = regionBytesPerRow * regionHeight;
1799
1838
  NSMutableData *sourceData = [NSMutableData dataWithLength:regionByteLength];
1800
- NSMutableData *horizontalData = [NSMutableData dataWithLength:regionByteLength];
1801
- if (!sourceData || !horizontalData) return NO;
1839
+ NSMutableData *firstPassData = [NSMutableData dataWithLength:regionByteLength];
1840
+ NSMutableData *secondPassData = [NSMutableData dataWithLength:regionByteLength];
1841
+ if (!sourceData || !firstPassData || !secondPassData) return NO;
1802
1842
  uint8_t *destination = (uint8_t *)contextData;
1803
1843
  uint8_t *source = (uint8_t *)sourceData.mutableBytes;
1804
- uint8_t *horizontal = (uint8_t *)horizontalData.mutableBytes;
1844
+ uint8_t *firstPass = (uint8_t *)firstPassData.mutableBytes;
1845
+ uint8_t *secondPass = (uint8_t *)secondPassData.mutableBytes;
1805
1846
  for (NSUInteger row = 0; row < regionHeight; row++) {
1806
1847
  memcpy(
1807
1848
  source + row * regionBytesPerRow,
@@ -1810,91 +1851,37 @@ static BOOL NativeSdkPacketApplyBlur(NSDictionary *effect, CGFloat opacity, CGCo
1810
1851
  );
1811
1852
  }
1812
1853
 
1813
- /* Both passes keep the ORIGINAL clamped-window box average — the
1814
- * window shrinks at the surface edges exactly as before — but slide
1815
- * the window incrementally (add the entering sample, subtract the
1816
- * leaving one), turning O(region x radius) into O(region). The sums
1817
- * are the same integers the per-pixel rescan produced, so the output
1818
- * is byte-identical; a full-window dirty pass that repaints a
1819
- * backdrop-blurred popover stops costing milliseconds of scalar
1820
- * resampling. */
1821
- for (NSUInteger y = expandedMinY; y < expandedMaxY; y++) {
1822
- const uint8_t *sourceRow = source + (y - expandedMinY) * regionBytesPerRow;
1823
- uint8_t *horizontalRow = horizontal + (y - expandedMinY) * regionBytesPerRow;
1824
- NSUInteger windowMinX = minX > radius ? minX - radius : 0;
1825
- NSUInteger windowMaxX = MIN((NSUInteger)width - 1, minX + radius);
1826
- uint64_t sums[4] = {0, 0, 0, 0};
1827
- for (NSUInteger sx = windowMinX; sx <= windowMaxX; sx++) {
1828
- const uint8_t *pixel = sourceRow + (sx - expandedMinX) * 4;
1829
- sums[0] += pixel[0];
1830
- sums[1] += pixel[1];
1831
- sums[2] += pixel[2];
1832
- sums[3] += pixel[3];
1854
+ vImage_Buffer buffers[3] = {
1855
+ { .data = source, .height = regionHeight, .width = regionWidth, .rowBytes = regionBytesPerRow },
1856
+ { .data = firstPass, .height = regionHeight, .width = regionWidth, .rowBytes = regionBytesPerRow },
1857
+ { .data = secondPass, .height = regionHeight, .width = regionWidth, .rowBytes = regionBytesPerRow },
1858
+ };
1859
+ const Pixel_8888 edgeColor = {0, 0, 0, 0};
1860
+ for (NSUInteger pass = 0; pass < 3; pass++) {
1861
+ const vImage_Buffer *input = pass == 0 ? &buffers[0] : (pass == 1 ? &buffers[1] : &buffers[2]);
1862
+ const vImage_Buffer *output = pass == 1 ? &buffers[2] : &buffers[1];
1863
+ const uint32_t kernelSize = (uint32_t)(boxRadii[pass] * 2 + 1);
1864
+ if (kernelSize == 1) {
1865
+ memcpy(output->data, input->data, regionByteLength);
1866
+ continue;
1833
1867
  }
1868
+ const vImage_Error error = vImageBoxConvolve_ARGB8888(
1869
+ input, output, NULL, 0, 0, kernelSize, kernelSize, edgeColor, kvImageEdgeExtend
1870
+ );
1871
+ if (error != kvImageNoError) return NO;
1872
+ }
1873
+
1874
+ /* The third pass lands in firstPass. Preserve command opacity by
1875
+ * mixing that Gaussian approximation with the untouched backdrop. */
1876
+ for (NSUInteger y = minY; y < maxY; y++) {
1877
+ const NSUInteger regionY = y - expandedMinY;
1834
1878
  for (NSUInteger x = minX; x < maxX; x++) {
1835
- NSUInteger sampleMinX = x > radius ? x - radius : 0;
1836
- NSUInteger sampleMaxX = MIN((NSUInteger)width - 1, x + radius);
1837
- while (windowMaxX < sampleMaxX) {
1838
- windowMaxX += 1;
1839
- const uint8_t *pixel = sourceRow + (windowMaxX - expandedMinX) * 4;
1840
- sums[0] += pixel[0];
1841
- sums[1] += pixel[1];
1842
- sums[2] += pixel[2];
1843
- sums[3] += pixel[3];
1844
- }
1845
- while (windowMinX < sampleMinX) {
1846
- const uint8_t *pixel = sourceRow + (windowMinX - expandedMinX) * 4;
1847
- sums[0] -= pixel[0];
1848
- sums[1] -= pixel[1];
1849
- sums[2] -= pixel[2];
1850
- sums[3] -= pixel[3];
1851
- windowMinX += 1;
1852
- }
1853
- NSUInteger count = sampleMaxX - sampleMinX + 1;
1854
- uint8_t *out = horizontalRow + (x - expandedMinX) * 4;
1855
- out[0] = (uint8_t)(sums[0] / count);
1856
- out[1] = (uint8_t)(sums[1] / count);
1857
- out[2] = (uint8_t)(sums[2] / count);
1858
- out[3] = (uint8_t)(sums[3] / count);
1859
- }
1860
- }
1861
-
1862
- for (NSUInteger x = minX; x < maxX; x++) {
1863
- const uint8_t *horizontalColumn = horizontal + (x - expandedMinX) * 4;
1864
- NSUInteger windowMinY = minY > radius ? minY - radius : 0;
1865
- NSUInteger windowMaxY = MIN((NSUInteger)height - 1, minY + radius);
1866
- uint64_t sums[4] = {0, 0, 0, 0};
1867
- for (NSUInteger sy = windowMinY; sy <= windowMaxY; sy++) {
1868
- const uint8_t *pixel = horizontalColumn + (sy - expandedMinY) * regionBytesPerRow;
1869
- sums[0] += pixel[0];
1870
- sums[1] += pixel[1];
1871
- sums[2] += pixel[2];
1872
- sums[3] += pixel[3];
1873
- }
1874
- for (NSUInteger y = minY; y < maxY; y++) {
1875
- NSUInteger sampleMinY = y > radius ? y - radius : 0;
1876
- NSUInteger sampleMaxY = MIN((NSUInteger)height - 1, y + radius);
1877
- while (windowMaxY < sampleMaxY) {
1878
- windowMaxY += 1;
1879
- const uint8_t *pixel = horizontalColumn + (windowMaxY - expandedMinY) * regionBytesPerRow;
1880
- sums[0] += pixel[0];
1881
- sums[1] += pixel[1];
1882
- sums[2] += pixel[2];
1883
- sums[3] += pixel[3];
1884
- }
1885
- while (windowMinY < sampleMinY) {
1886
- const uint8_t *pixel = horizontalColumn + (windowMinY - expandedMinY) * regionBytesPerRow;
1887
- sums[0] -= pixel[0];
1888
- sums[1] -= pixel[1];
1889
- sums[2] -= pixel[2];
1890
- sums[3] -= pixel[3];
1891
- windowMinY += 1;
1892
- }
1893
- NSUInteger count = sampleMaxY - sampleMinY + 1;
1879
+ const NSUInteger regionX = x - expandedMinX;
1880
+ const size_t regionIndex = regionY * regionBytesPerRow + regionX * 4;
1894
1881
  uint8_t *out = destination + y * bytesPerRow + x * 4;
1895
1882
  for (NSUInteger channel = 0; channel < 4; channel++) {
1896
- CGFloat blurred = (CGFloat)(sums[channel] / count);
1897
- CGFloat original = (CGFloat)source[(y - expandedMinY) * regionBytesPerRow + (x - expandedMinX) * 4 + channel];
1883
+ const CGFloat original = (CGFloat)source[regionIndex + channel];
1884
+ const CGFloat blurred = (CGFloat)firstPass[regionIndex + channel];
1898
1885
  out[channel] = (uint8_t)llround(original + (blurred - original) * mix);
1899
1886
  }
1900
1887
  }
@@ -4033,7 +4020,7 @@ static const char *NativeSdkGpuShotDir(void) {
4033
4020
  * through the same CG code, clipped to the repaint region, and draw
4034
4021
  * as transient textured quads;
4035
4022
  * - blur reads the backdrop, so the pass splits around it: commit +
4036
- * wait, read the target back, run the existing scalar box blur on
4023
+ * wait, read the target back, run the vImage Gaussian approximation on
4037
4024
  * the readback, upload the blurred rect, and continue — a hybrid
4038
4025
  * frame, CPU only where the effect is inherently a backdrop read.
4039
4026
  * Scissor/dirty-rect semantics mirror the CPU path: full passes clear
@@ -4520,7 +4507,7 @@ static BOOL NativeSdkCompositeBlurWriteRegion(NSDictionary *command, CGFloat sca
4520
4507
  if (op->type == 0) continue;
4521
4508
  if (op->type == 3) {
4522
4509
  /* Blur sandwich: flush the pass, read the target back, run
4523
- * the reference scalar blur on the readback, upload the
4510
+ * the vImage Gaussian approximation on the readback, upload the
4524
4511
  * blurred rect, and continue compositing above it. */
4525
4512
  [encoder endEncoding];
4526
4513
  encoder = nil;
@@ -7277,6 +7264,333 @@ static BOOL NativeSdkScrollDriverCanConsumeHorizontally(NativeSdkScrollDriverVie
7277
7264
  @implementation NativeSdkShortcut
7278
7265
  @end
7279
7266
 
7267
+ /* ------------------------------------------------------- audio capture
7268
+ * Both native sources feed this one converter/quiescence target. AVAudioEngine
7269
+ * and ScreenCaptureKit normally hand us planar float PCM; the converter also
7270
+ * accepts interleaved float and signed-16 input, remixes mono/stereo, performs
7271
+ * linear sample-rate conversion, and emits <=20 ms signed-16 LE chunks. */
7272
+ @interface NativeSdkAudioCaptureTarget : NSObject {
7273
+ double _resamplePosition;
7274
+ double _resampleInputRate;
7275
+ uint32_t _resampleInputChannels;
7276
+ BOOL _resampleHasPrevious;
7277
+ BOOL _started;
7278
+ float _resamplePrevious[2];
7279
+ }
7280
+ @property(nonatomic, assign) int source;
7281
+ @property(nonatomic, assign) uint32_t sampleRate;
7282
+ @property(nonatomic, assign) uint8_t channels;
7283
+ @property(nonatomic, assign) native_sdk_appkit_audio_capture_push_t pushFn;
7284
+ @property(nonatomic, assign) void *pushContext;
7285
+ @property(nonatomic, strong) NSCondition *condition;
7286
+ @property(nonatomic, assign) BOOL active;
7287
+ @property(nonatomic, assign) NSUInteger inFlight;
7288
+ - (instancetype)initWithSource:(int)source sampleRate:(uint32_t)sampleRate channels:(uint8_t)channels pushFn:(native_sdk_appkit_audio_capture_push_t)pushFn pushContext:(void *)pushContext;
7289
+ - (BOOL)isCaptureActive;
7290
+ - (void)markInactive;
7291
+ - (void)deactivateAndWait;
7292
+ - (void)emitStarted;
7293
+ - (void)emitKind:(int)kind;
7294
+ - (void)consumeBufferList:(const AudioBufferList *)buffers format:(const AudioStreamBasicDescription *)format frames:(uint32_t)frames timestampNs:(uint64_t)timestampNs;
7295
+ @end
7296
+
7297
+ static float NativeSdkCaptureReadSample(const AudioBufferList *buffers, const AudioStreamBasicDescription *format, uint32_t frame, uint32_t channel) {
7298
+ if (!buffers || buffers->mNumberBuffers == 0) return 0.0f;
7299
+ const uint32_t inputChannels = MAX(1u, format->mChannelsPerFrame);
7300
+ const BOOL nonInterleaved = (format->mFormatFlags & kAudioFormatFlagIsNonInterleaved) != 0;
7301
+ const uint32_t bufferIndex = nonInterleaved ? MIN(channel, buffers->mNumberBuffers - 1) : 0;
7302
+ const AudioBuffer *buffer = &buffers->mBuffers[bufferIndex];
7303
+ if (!buffer->mData) return 0.0f;
7304
+ const uint32_t sampleIndex = nonInterleaved ? frame : frame * inputChannels + MIN(channel, inputChannels - 1);
7305
+ if ((format->mFormatFlags & kAudioFormatFlagIsFloat) && format->mBitsPerChannel == 32) {
7306
+ return ((const float *)buffer->mData)[sampleIndex];
7307
+ }
7308
+ if ((format->mFormatFlags & kAudioFormatFlagIsFloat) && format->mBitsPerChannel == 64) {
7309
+ return (float)((const double *)buffer->mData)[sampleIndex];
7310
+ }
7311
+ if ((format->mFormatFlags & kAudioFormatFlagIsSignedInteger) && format->mBitsPerChannel == 16) {
7312
+ return (float)((const int16_t *)buffer->mData)[sampleIndex] / 32768.0f;
7313
+ }
7314
+ return 0.0f;
7315
+ }
7316
+
7317
+ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, const AudioStreamBasicDescription *format, uint32_t frame, uint8_t outputChannels, uint32_t outputChannel) {
7318
+ const uint32_t inputChannels = MAX(1u, format->mChannelsPerFrame);
7319
+ if (outputChannels == 1) {
7320
+ float sample = 0.0f;
7321
+ for (uint32_t inputChannel = 0; inputChannel < inputChannels; inputChannel += 1) {
7322
+ sample += NativeSdkCaptureReadSample(buffers, format, frame, inputChannel);
7323
+ }
7324
+ return sample / (float)inputChannels;
7325
+ }
7326
+ return NativeSdkCaptureReadSample(buffers, format, frame, MIN(outputChannel, inputChannels - 1));
7327
+ }
7328
+
7329
+ @implementation NativeSdkAudioCaptureTarget
7330
+ - (instancetype)initWithSource:(int)source sampleRate:(uint32_t)sampleRate channels:(uint8_t)channels pushFn:(native_sdk_appkit_audio_capture_push_t)pushFn pushContext:(void *)pushContext {
7331
+ self = [super init];
7332
+ if (!self) return nil;
7333
+ self.source = source;
7334
+ self.sampleRate = sampleRate;
7335
+ self.channels = channels;
7336
+ self.pushFn = pushFn;
7337
+ self.pushContext = pushContext;
7338
+ self.condition = [[NSCondition alloc] init];
7339
+ self.active = YES;
7340
+ return self;
7341
+ }
7342
+ - (BOOL)isCaptureActive {
7343
+ [self.condition lock];
7344
+ BOOL value = self.active;
7345
+ [self.condition unlock];
7346
+ return value;
7347
+ }
7348
+ - (void)markInactive {
7349
+ [self.condition lock];
7350
+ self.active = NO;
7351
+ [self.condition unlock];
7352
+ }
7353
+ - (BOOL)beginPush {
7354
+ [self.condition lock];
7355
+ if (!self.active) { [self.condition unlock]; return NO; }
7356
+ self.inFlight += 1;
7357
+ [self.condition unlock];
7358
+ return YES;
7359
+ }
7360
+ - (BOOL)beginDataPush {
7361
+ [self.condition lock];
7362
+ if (!self.active || !_started) { [self.condition unlock]; return NO; }
7363
+ self.inFlight += 1;
7364
+ [self.condition unlock];
7365
+ return YES;
7366
+ }
7367
+ - (void)endPush {
7368
+ [self.condition lock];
7369
+ self.inFlight -= 1;
7370
+ if (self.inFlight == 0) [self.condition broadcast];
7371
+ [self.condition unlock];
7372
+ }
7373
+ - (void)deactivateAndWait {
7374
+ [self.condition lock];
7375
+ self.active = NO;
7376
+ while (self.inFlight > 0) [self.condition wait];
7377
+ [self.condition unlock];
7378
+ }
7379
+ - (void)emitStarted {
7380
+ [self.condition lock];
7381
+ if (!self.active || _started) { [self.condition unlock]; return; }
7382
+ self.inFlight += 1;
7383
+ [self.condition unlock];
7384
+
7385
+ int result = self.pushFn ? self.pushFn(self.pushContext, 0, self.source,
7386
+ self.sampleRate, self.channels, 0, 0, NULL, 0) : 1;
7387
+
7388
+ [self.condition lock];
7389
+ if (result == 1) self.active = NO;
7390
+ else if (self.active) _started = YES;
7391
+ self.inFlight -= 1;
7392
+ if (self.inFlight == 0) [self.condition broadcast];
7393
+ [self.condition unlock];
7394
+ }
7395
+ - (void)emitKind:(int)kind {
7396
+ if (![self beginPush]) return;
7397
+ int result = self.pushFn ? self.pushFn(self.pushContext, kind, self.source,
7398
+ self.sampleRate, self.channels, 0, 0, NULL, 0) : 1;
7399
+ [self endPush];
7400
+ if (result == 1) [self deactivateAndWait];
7401
+ }
7402
+ - (void)consumeBufferList:(const AudioBufferList *)buffers format:(const AudioStreamBasicDescription *)format frames:(uint32_t)frames timestampNs:(uint64_t)timestampNs {
7403
+ if (!buffers || !format || frames == 0 || format->mSampleRate <= 0 || ![self beginDataPush]) return;
7404
+ const uint32_t inputChannels = MAX(1u, format->mChannelsPerFrame);
7405
+ if (_resampleInputRate != format->mSampleRate || _resampleInputChannels != inputChannels) {
7406
+ _resamplePosition = 0.0;
7407
+ _resampleInputRate = format->mSampleRate;
7408
+ _resampleInputChannels = inputChannels;
7409
+ _resampleHasPrevious = NO;
7410
+ }
7411
+ const double step = format->mSampleRate / (double)self.sampleRate;
7412
+ const uint32_t chunkLimit = MAX(1u, self.sampleRate / 50u);
7413
+ int16_t output[1920];
7414
+ uint32_t chunkFrames = 0;
7415
+ uint64_t emittedFrames = 0;
7416
+ BOOL closed = NO;
7417
+ while (_resamplePosition <= (double)frames - 1.0) {
7418
+ const double flooredPosition = floor(_resamplePosition);
7419
+ const int64_t inputFrame = (int64_t)flooredPosition;
7420
+ const float fraction = (float)(_resamplePosition - flooredPosition);
7421
+ if (inputFrame < 0 && !_resampleHasPrevious) {
7422
+ _resamplePosition = 0.0;
7423
+ continue;
7424
+ }
7425
+ // A fractional sample at the end needs the next callback's first
7426
+ // frame. Defer it instead of repeating the current buffer's tail.
7427
+ if (inputFrame >= 0 && inputFrame + 1 >= frames && fraction > 0.0000001f) break;
7428
+
7429
+ for (uint32_t outChannel = 0; outChannel < self.channels; outChannel += 1) {
7430
+ const float first = inputFrame < 0
7431
+ ? _resamplePrevious[outChannel]
7432
+ : NativeSdkCaptureReadRemixedSample(buffers, format, (uint32_t)inputFrame, self.channels, outChannel);
7433
+ const float second = inputFrame < 0
7434
+ ? NativeSdkCaptureReadRemixedSample(buffers, format, 0, self.channels, outChannel)
7435
+ : (inputFrame + 1 < frames
7436
+ ? NativeSdkCaptureReadRemixedSample(buffers, format, (uint32_t)(inputFrame + 1), self.channels, outChannel)
7437
+ : first);
7438
+ float sample = first + (second - first) * fraction;
7439
+ sample = fmaxf(-1.0f, fminf(1.0f, sample));
7440
+ output[chunkFrames * self.channels + outChannel] = (int16_t)lrintf(sample * 32767.0f);
7441
+ }
7442
+ chunkFrames += 1;
7443
+ _resamplePosition += step;
7444
+
7445
+ if (chunkFrames == chunkLimit) {
7446
+ const size_t byteLen = (size_t)chunkFrames * self.channels * sizeof(int16_t);
7447
+ const int result = self.pushFn ? self.pushFn(self.pushContext, 1, self.source,
7448
+ self.sampleRate, self.channels, timestampNs + emittedFrames * NSEC_PER_SEC / self.sampleRate, chunkFrames,
7449
+ (const uint8_t *)output, byteLen) : 1;
7450
+ emittedFrames += chunkFrames;
7451
+ chunkFrames = 0;
7452
+ if (result == 1) {
7453
+ [self markInactive];
7454
+ closed = YES;
7455
+ break;
7456
+ }
7457
+ }
7458
+ }
7459
+ if (!closed && chunkFrames > 0) {
7460
+ const size_t byteLen = (size_t)chunkFrames * self.channels * sizeof(int16_t);
7461
+ const int result = self.pushFn ? self.pushFn(self.pushContext, 1, self.source,
7462
+ self.sampleRate, self.channels, timestampNs + emittedFrames * NSEC_PER_SEC / self.sampleRate, chunkFrames,
7463
+ (const uint8_t *)output, byteLen) : 1;
7464
+ if (result == 1) [self markInactive];
7465
+ }
7466
+ _resamplePosition -= frames;
7467
+ for (uint32_t outChannel = 0; outChannel < self.channels; outChannel += 1) {
7468
+ _resamplePrevious[outChannel] = NativeSdkCaptureReadRemixedSample(buffers, format, frames - 1, self.channels, outChannel);
7469
+ }
7470
+ _resampleHasPrevious = YES;
7471
+ [self endPush];
7472
+ }
7473
+ @end
7474
+
7475
+ @interface NativeSdkScreenAudioCapture : NSObject <SCStreamOutput, SCStreamDelegate>
7476
+ @property(nonatomic, strong) NativeSdkAudioCaptureTarget *target;
7477
+ @property(nonatomic, strong) SCStream *stream;
7478
+ @property(nonatomic, strong) dispatch_queue_t queue;
7479
+ @property(nonatomic, strong) NSLock *lifecycleLock;
7480
+ - (instancetype)initWithTarget:(NativeSdkAudioCaptureTarget *)target;
7481
+ - (void)start;
7482
+ - (void)stopAndWait;
7483
+ @end
7484
+
7485
+ @implementation NativeSdkScreenAudioCapture
7486
+ - (instancetype)initWithTarget:(NativeSdkAudioCaptureTarget *)target {
7487
+ self = [super init];
7488
+ if (!self) return nil;
7489
+ self.target = target;
7490
+ self.queue = dispatch_queue_create("dev.native-sdk.system-audio", DISPATCH_QUEUE_SERIAL);
7491
+ self.lifecycleLock = [[NSLock alloc] init];
7492
+ return self;
7493
+ }
7494
+ - (void)start {
7495
+ if (@available(macOS 13.0, *)) {
7496
+ __weak NativeSdkScreenAudioCapture *weakSelf = self;
7497
+ [SCShareableContent getShareableContentExcludingDesktopWindows:YES onScreenWindowsOnly:YES completionHandler:^(SCShareableContent *content, NSError *error) {
7498
+ NativeSdkScreenAudioCapture *strongSelf = weakSelf;
7499
+ if (!strongSelf || ![strongSelf.target isCaptureActive]) return;
7500
+ SCDisplay *display = content.displays.firstObject;
7501
+ if (error || !display) { [strongSelf.target emitKind:2]; return; }
7502
+ SCContentFilter *filter = [[SCContentFilter alloc] initWithDisplay:display excludingApplications:@[] exceptingWindows:@[]];
7503
+ SCStreamConfiguration *configuration = [[SCStreamConfiguration alloc] init];
7504
+ configuration.width = 2;
7505
+ configuration.height = 2;
7506
+ configuration.showsCursor = NO;
7507
+ configuration.capturesAudio = YES;
7508
+ configuration.excludesCurrentProcessAudio = NO;
7509
+ configuration.sampleRate = strongSelf.target.sampleRate;
7510
+ /* ScreenCaptureKit on Sonoma can accept a mono request but then
7511
+ * produce no audio sample buffers for a display stream. Capture
7512
+ * the system mix in its native stereo shape and let the shared
7513
+ * target converter downmix when the app requested mono. */
7514
+ configuration.channelCount = 2;
7515
+ configuration.minimumFrameInterval = CMTimeMake(1, 1);
7516
+ configuration.queueDepth = 1;
7517
+ SCStream *stream = [[SCStream alloc] initWithFilter:filter configuration:configuration delegate:strongSelf];
7518
+ NSError *outputError = nil;
7519
+ /* ScreenCaptureKit still creates a remote video queue for an
7520
+ * audio-enabled display stream. Register a tiny ignored screen
7521
+ * output so that queue stays valid; without it Sonoma reports a
7522
+ * recurring "stream output NOT found" error for every frame. */
7523
+ if (![stream addStreamOutput:strongSelf type:SCStreamOutputTypeScreen sampleHandlerQueue:strongSelf.queue error:&outputError] ||
7524
+ ![stream addStreamOutput:strongSelf type:SCStreamOutputTypeAudio sampleHandlerQueue:strongSelf.queue error:&outputError]) {
7525
+ [strongSelf.target emitKind:2];
7526
+ return;
7527
+ }
7528
+ /* Publish and start under the same lock stop uses to detach the
7529
+ * current stream. The shareable-content lookup is asynchronous:
7530
+ * without this final activity check, stop can observe stream=nil,
7531
+ * return, and then have this completion start an orphan capture. */
7532
+ [strongSelf.lifecycleLock lock];
7533
+ if (![strongSelf.target isCaptureActive]) {
7534
+ [strongSelf.lifecycleLock unlock];
7535
+ return;
7536
+ }
7537
+ strongSelf.stream = stream;
7538
+ [stream startCaptureWithCompletionHandler:^(NSError *startError) {
7539
+ if (startError) [strongSelf.target emitKind:2];
7540
+ else [strongSelf.target emitStarted];
7541
+ }];
7542
+ [strongSelf.lifecycleLock unlock];
7543
+ }];
7544
+ } else {
7545
+ [self.target emitKind:2];
7546
+ }
7547
+ }
7548
+ - (void)stream:(SCStream *)stream didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer ofType:(SCStreamOutputType)type API_AVAILABLE(macos(13.0)) {
7549
+ (void)stream;
7550
+ if (type != SCStreamOutputTypeAudio || !CMSampleBufferDataIsReady(sampleBuffer)) return;
7551
+ /* Query the exact AudioBufferList size. ScreenCaptureKit's Sonoma
7552
+ * buffers can require more storage than their two-channel ASBD suggests,
7553
+ * so a fixed "enough channels" allocation loses every audio sample with
7554
+ * kCMSampleBufferError_ArrayTooSmall. */
7555
+ size_t capacity = 0;
7556
+ OSStatus status = CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, &capacity,
7557
+ NULL, 0, NULL, NULL, kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, NULL);
7558
+ if (status != noErr || capacity < sizeof(AudioBufferList) || capacity > 64 * 1024) return;
7559
+ AudioBufferList *buffers = malloc(capacity);
7560
+ if (!buffers) return;
7561
+ CMBlockBufferRef block = NULL;
7562
+ status = CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL,
7563
+ buffers, capacity, NULL, NULL, kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, &block);
7564
+ const AudioStreamBasicDescription *format = CMAudioFormatDescriptionGetStreamBasicDescription((CMAudioFormatDescriptionRef)CMSampleBufferGetFormatDescription(sampleBuffer));
7565
+ if (status == noErr && format) {
7566
+ [self.target consumeBufferList:buffers format:format frames:(uint32_t)CMSampleBufferGetNumSamples(sampleBuffer) timestampNs:NativeSdkTimestampNanoseconds()];
7567
+ }
7568
+ if (block) CFRelease(block);
7569
+ free(buffers);
7570
+ }
7571
+ - (void)stream:(SCStream *)stream didStopWithError:(NSError *)error API_AVAILABLE(macos(12.3)) {
7572
+ (void)stream; (void)error;
7573
+ [self.target emitKind:2];
7574
+ }
7575
+ - (void)stopAndWait {
7576
+ [self.target deactivateAndWait];
7577
+ if (@available(macOS 13.0, *)) {
7578
+ /* Detach under the startup lock. Once this unlocks, an in-flight
7579
+ * shareable-content completion either published its stream here (and
7580
+ * we stop it below), or sees the inactive target and cannot start. */
7581
+ [self.lifecycleLock lock];
7582
+ SCStream *stream = self.stream;
7583
+ self.stream = nil;
7584
+ [self.lifecycleLock unlock];
7585
+ if (stream) {
7586
+ dispatch_semaphore_t done = dispatch_semaphore_create(0);
7587
+ [stream stopCaptureWithCompletionHandler:^(NSError *error) { (void)error; dispatch_semaphore_signal(done); }];
7588
+ dispatch_semaphore_wait(done, dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC));
7589
+ }
7590
+ }
7591
+ }
7592
+ @end
7593
+
7280
7594
  @implementation NativeSdkAppKitHost
7281
7595
 
7282
7596
  - (instancetype)initWithAppName:(NSString *)appName displayName:(NSString *)displayName version:(NSString *)version aboutDescription:(NSString *)aboutDescription hasWebContent:(BOOL)hasWebContent windowTitle:(NSString *)windowTitle bundleIdentifier:(NSString *)bundleIdentifier iconPath:(NSString *)iconPath windowLabel:(NSString *)windowLabel x:(double)x y:(double)y width:(double)width height:(double)height restoreFrame:(BOOL)restoreFrame resizable:(BOOL)resizable titlebarStyle:(int)titlebarStyle showPolicy:(int)showPolicy windowFlags:(uint32_t)windowFlags {
@@ -7551,6 +7865,8 @@ static BOOL NativeSdkScrollDriverCanConsumeHorizontally(NativeSdkScrollDriverVie
7551
7865
 
7552
7866
  - (void)dealloc {
7553
7867
  [self invalidateAppTimers];
7868
+ [self audioCaptureStopSource:0];
7869
+ [self audioCaptureStopSource:1];
7554
7870
  [self audioStop];
7555
7871
  [self videoStop];
7556
7872
  /* The vDSP plan outlives individual playbacks (created lazily
@@ -9381,6 +9697,8 @@ static void NativeSdkApplyProcessDisplayName(NSString *displayName) {
9381
9697
  [self.timer invalidate];
9382
9698
  self.timer = nil;
9383
9699
  [self invalidateAppTimers];
9700
+ [self audioCaptureStopSource:0];
9701
+ [self audioCaptureStopSource:1];
9384
9702
  [self audioStop];
9385
9703
  [self videoStop];
9386
9704
  if (self.shortcutEventMonitor) {
@@ -10420,6 +10738,90 @@ static int NativeSdkSpectrumComputeBands(native_sdk_spectrum_tap_state_t *state,
10420
10738
  return 1;
10421
10739
  }
10422
10740
 
10741
+ - (void)startMicrophoneCaptureTarget:(NativeSdkAudioCaptureTarget *)target {
10742
+ if (![target isCaptureActive] || self.microphoneCaptureTarget != target) return;
10743
+ AVAudioEngine *engine = [[AVAudioEngine alloc] init];
10744
+ AVAudioInputNode *input = engine.inputNode;
10745
+ AVAudioFormat *inputFormat = [input outputFormatForBus:0];
10746
+ if (!input || inputFormat.channelCount == 0 || inputFormat.sampleRate <= 0) {
10747
+ [target emitKind:2];
10748
+ return;
10749
+ }
10750
+ __weak NativeSdkAudioCaptureTarget *weakTarget = target;
10751
+ [input installTapOnBus:0 bufferSize:480 format:nil block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) {
10752
+ (void)when;
10753
+ NativeSdkAudioCaptureTarget *strongTarget = weakTarget;
10754
+ if (!strongTarget) return;
10755
+ [strongTarget consumeBufferList:buffer.audioBufferList format:buffer.format.streamDescription frames:(uint32_t)buffer.frameLength timestampNs:NativeSdkTimestampNanoseconds()];
10756
+ }];
10757
+ [engine prepare];
10758
+ NSError *error = nil;
10759
+ if (![engine startAndReturnError:&error]) {
10760
+ [input removeTapOnBus:0];
10761
+ [target emitKind:2];
10762
+ return;
10763
+ }
10764
+ self.microphoneCaptureEngine = engine;
10765
+ [target emitStarted];
10766
+ }
10767
+
10768
+ - (int)audioCaptureStartSource:(int)source sampleRate:(uint32_t)sampleRate channels:(uint8_t)channels pushFn:(native_sdk_appkit_audio_capture_push_t)pushFn pushContext:(void *)pushContext {
10769
+ if ((source != 0 && source != 1) || !pushFn ||
10770
+ (sampleRate != 16000 && sampleRate != 24000 && sampleRate != 48000) ||
10771
+ (channels != 1 && channels != 2)) return 0;
10772
+ [self audioCaptureStopSource:source];
10773
+ NativeSdkAudioCaptureTarget *target = [[NativeSdkAudioCaptureTarget alloc]
10774
+ initWithSource:source sampleRate:sampleRate channels:channels pushFn:pushFn pushContext:pushContext];
10775
+ if (!target) return 0;
10776
+ if (source == 0) {
10777
+ self.microphoneCaptureTarget = target;
10778
+ AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
10779
+ if (status == AVAuthorizationStatusAuthorized) {
10780
+ [self startMicrophoneCaptureTarget:target];
10781
+ } else if (status == AVAuthorizationStatusNotDetermined) {
10782
+ __weak NativeSdkAppKitHost *weakSelf = self;
10783
+ [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
10784
+ dispatch_async(dispatch_get_main_queue(), ^{
10785
+ NativeSdkAppKitHost *strongSelf = weakSelf;
10786
+ if (!strongSelf || strongSelf.microphoneCaptureTarget != target || ![target isCaptureActive]) return;
10787
+ if (granted) [strongSelf startMicrophoneCaptureTarget:target];
10788
+ else [target emitKind:2];
10789
+ });
10790
+ }];
10791
+ } else {
10792
+ [target emitKind:2];
10793
+ }
10794
+ } else {
10795
+ self.systemCaptureTarget = target;
10796
+ NativeSdkScreenAudioCapture *capture = [[NativeSdkScreenAudioCapture alloc] initWithTarget:target];
10797
+ self.systemCapture = capture;
10798
+ [capture start];
10799
+ }
10800
+ return 1;
10801
+ }
10802
+
10803
+ - (int)audioCaptureStopSource:(int)source {
10804
+ if (source == 0) {
10805
+ NativeSdkAudioCaptureTarget *target = self.microphoneCaptureTarget;
10806
+ [target deactivateAndWait];
10807
+ AVAudioInputNode *input = self.microphoneCaptureEngine.inputNode;
10808
+ if (input) [input removeTapOnBus:0];
10809
+ [self.microphoneCaptureEngine stop];
10810
+ self.microphoneCaptureEngine = nil;
10811
+ self.microphoneCaptureTarget = nil;
10812
+ return target ? 1 : 0;
10813
+ }
10814
+ if (source == 1) {
10815
+ NativeSdkScreenAudioCapture *capture = self.systemCapture;
10816
+ [capture stopAndWait];
10817
+ [self.systemCaptureTarget deactivateAndWait];
10818
+ self.systemCapture = nil;
10819
+ self.systemCaptureTarget = nil;
10820
+ return capture ? 1 : 0;
10821
+ }
10822
+ return 0;
10823
+ }
10824
+
10423
10825
  /* ---------------------------------------------------- video player
10424
10826
  *
10425
10827
  * The app's single video player: one AVPlayer whose
@@ -11757,6 +12159,26 @@ int native_sdk_appkit_audio_set_volume(native_sdk_appkit_host_t *host, double vo
11757
12159
  return [object audioSetVolume:volume];
11758
12160
  }
11759
12161
 
12162
+ int native_sdk_appkit_audio_capture_start(native_sdk_appkit_host_t *host, int source, uint32_t sample_rate, uint8_t channels, native_sdk_appkit_audio_capture_push_t push_fn, void *push_context) {
12163
+ if (!host) return 0;
12164
+ NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
12165
+ return [object audioCaptureStartSource:source sampleRate:sample_rate channels:channels pushFn:push_fn pushContext:push_context];
12166
+ }
12167
+
12168
+ int native_sdk_appkit_audio_capture_stop(native_sdk_appkit_host_t *host, int source) {
12169
+ if (!host) return 0;
12170
+ NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
12171
+ return [object audioCaptureStopSource:source];
12172
+ }
12173
+
12174
+ int native_sdk_appkit_audio_capture_supported(native_sdk_appkit_host_t *host, int source) {
12175
+ (void)host;
12176
+ if (source == 0) return 1;
12177
+ if (source != 1) return 0;
12178
+ if (@available(macOS 13.0, *)) return NSClassFromString(@"SCStream") != nil;
12179
+ return 0;
12180
+ }
12181
+
11760
12182
  int native_sdk_appkit_video_load(native_sdk_appkit_host_t *host, const char *path, size_t path_len, uint64_t token, native_sdk_appkit_video_sink_push_t push_fn, void *push_context) {
11761
12183
  NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
11762
12184
  NSString *path_string = [[NSString alloc] initWithBytes:path length:path_len encoding:NSUTF8StringEncoding];