@native-sdk/cli 0.8.2 → 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 (92) hide show
  1. package/build/app.zig +50 -3
  2. package/build.zig +134 -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/app_assets.zig +138 -0
  21. package/src/app_runner/root.zig +2 -0
  22. package/src/app_runner/ts_core_main.zig +27 -3
  23. package/src/embed/host.zig +6 -0
  24. package/src/embed/ui_host.zig +2 -0
  25. package/src/platform/linux/root.zig +1 -0
  26. package/src/platform/macos/appkit_host.h +10 -0
  27. package/src/platform/macos/appkit_host.m +515 -93
  28. package/src/platform/macos/capture_info_plist.c +25 -0
  29. package/src/platform/macos/cef_host.mm +22 -0
  30. package/src/platform/macos/root.zig +68 -0
  31. package/src/platform/null_platform.zig +58 -0
  32. package/src/platform/root.zig +7 -0
  33. package/src/platform/types.zig +100 -0
  34. package/src/platform/windows/root.zig +56 -0
  35. package/src/platform/windows/webview2_host.cpp +171 -0
  36. package/src/primitives/app_manifest/types.zig +2 -0
  37. package/src/primitives/canvas/code_tests.zig +9 -9
  38. package/src/primitives/canvas/events.zig +12 -11
  39. package/src/primitives/canvas/frame.zig +44 -9
  40. package/src/primitives/canvas/icons/mic.svg +1 -0
  41. package/src/primitives/canvas/icons.zig +2 -0
  42. package/src/primitives/canvas/root.zig +8 -0
  43. package/src/primitives/canvas/text_span_tests.zig +39 -0
  44. package/src/primitives/canvas/ui.zig +23 -4
  45. package/src/primitives/canvas/ui_markup.zig +15 -0
  46. package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
  47. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
  48. package/src/primitives/canvas/ui_markup_tests.zig +5 -0
  49. package/src/primitives/canvas/ui_markup_view.zig +9 -0
  50. package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
  51. package/src/primitives/canvas/ui_schema.zig +19 -10
  52. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  53. package/src/primitives/canvas/ui_tests.zig +18 -4
  54. package/src/primitives/canvas/widget_invalidation.zig +9 -2
  55. package/src/primitives/canvas/widget_render.zig +321 -115
  56. package/src/primitives/canvas/widget_runtime_tests.zig +173 -0
  57. package/src/primitives/canvas/widget_text_input.zig +10 -10
  58. package/src/primitives/canvas/widget_tree.zig +1 -1
  59. package/src/primitives/canvas/widgets.zig +41 -5
  60. package/src/root.zig +10 -0
  61. package/src/runtime/automation_widget_dispatch.zig +1 -0
  62. package/src/runtime/bridge_payload.zig +2 -0
  63. package/src/runtime/canvas_frame.zig +13 -2
  64. package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
  65. package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
  66. package/src/runtime/canvas_widget_event_tests.zig +1 -0
  67. package/src/runtime/canvas_widget_events.zig +69 -24
  68. package/src/runtime/canvas_widget_runtime.zig +101 -6
  69. package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
  70. package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
  71. package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
  72. package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
  73. package/src/runtime/canvas_widget_state.zig +10 -1
  74. package/src/runtime/canvas_widget_text_tests.zig +112 -3
  75. package/src/runtime/effects.zig +313 -2
  76. package/src/runtime/effects_audio_capture_tests.zig +251 -0
  77. package/src/runtime/root.zig +3 -0
  78. package/src/runtime/tests.zig +1 -0
  79. package/src/runtime/ts_core_host.zig +401 -32
  80. package/src/runtime/ts_core_host_tests.zig +407 -0
  81. package/src/runtime/ts_ui_app.zig +39 -1
  82. package/src/runtime/ui_app.zig +18 -3
  83. package/src/runtime/ui_app_tests.zig +48 -0
  84. package/src/runtime/view.zig +2 -0
  85. package/src/runtime/view_widget_scroll.zig +56 -5
  86. package/src/runtime/view_widget_text.zig +24 -5
  87. package/src/runtime/view_widget_tree.zig +1 -1
  88. package/src/tooling/manifest.zig +1034 -0
  89. package/src/tooling/package.zig +841 -10
  90. package/src/tooling/raw_manifest.zig +35 -0
  91. package/src/tooling/templates.zig +55 -0
  92. package/tools/corewire/emit_facade.zig +36 -0
@@ -0,0 +1,25 @@
1
+ /*
2
+ * Hand-wired example build graphs launch bare Mach-O executables and do not
3
+ * have the managed graph's permission-aware plist generation step. Keep the
4
+ * capture usage descriptions in the conventional embedded section so macOS
5
+ * can present consent instead of terminating an example that adopts capture.
6
+ */
7
+ #define NATIVE_SDK_CAPTURE_INFO_PLIST \
8
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" \
9
+ "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" " \
10
+ "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" \
11
+ "<plist version=\"1.0\">\n<dict>\n" \
12
+ " <key>NSMicrophoneUsageDescription</key>\n" \
13
+ " <string>This app captures microphone audio when you start " \
14
+ "recording.</string>\n" \
15
+ " <key>NSAudioCaptureUsageDescription</key>\n" \
16
+ " <string>This app captures system audio when you start " \
17
+ "recording.</string>\n" \
18
+ " <key>NSScreenCaptureUsageDescription</key>\n" \
19
+ " <string>This app captures system audio when you start " \
20
+ "recording.</string>\n" \
21
+ "</dict>\n</plist>\n"
22
+
23
+ __attribute__((used, section("__TEXT,__info_plist")))
24
+ static const unsigned char native_sdk_capture_info_plist
25
+ [sizeof(NATIVE_SDK_CAPTURE_INFO_PLIST) - 1] = NATIVE_SDK_CAPTURE_INFO_PLIST;
@@ -3411,6 +3411,28 @@ void native_sdk_appkit_set_tray_callback(native_sdk_appkit_host_t *host, native_
3411
3411
  object.trayContext = context;
3412
3412
  }
3413
3413
 
3414
+ 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 callback, void *context) {
3415
+ (void)host;
3416
+ (void)source;
3417
+ (void)sample_rate;
3418
+ (void)channels;
3419
+ (void)callback;
3420
+ (void)context;
3421
+ return 0;
3422
+ }
3423
+
3424
+ int native_sdk_appkit_audio_capture_stop(native_sdk_appkit_host_t *host, int source) {
3425
+ (void)host;
3426
+ (void)source;
3427
+ return 0;
3428
+ }
3429
+
3430
+ int native_sdk_appkit_audio_capture_supported(native_sdk_appkit_host_t *host, int source) {
3431
+ (void)host;
3432
+ (void)source;
3433
+ return 0;
3434
+ }
3435
+
3414
3436
  /* Native context menu, CEF engine: same NSMenu presentation as the
3415
3437
  * system-engine host, anchored to the window content view (gpu-surface
3416
3438
  * views are system-engine-only, so `label` resolves to the window). */
@@ -205,6 +205,10 @@ extern fn native_sdk_appkit_audio_pause(host: *AppKitHost) c_int;
205
205
  extern fn native_sdk_appkit_audio_stop(host: *AppKitHost) c_int;
206
206
  extern fn native_sdk_appkit_audio_seek(host: *AppKitHost, position_ms: u64) c_int;
207
207
  extern fn native_sdk_appkit_audio_set_volume(host: *AppKitHost, volume: f64) c_int;
208
+ const AppKitAudioCapturePush = *const fn (context: ?*anyopaque, kind: c_int, source: c_int, sample_rate: u32, channels: u8, timestamp_ns: u64, frames: u32, pcm: ?[*]const u8, pcm_len: usize) callconv(.c) c_int;
209
+ extern fn native_sdk_appkit_audio_capture_start(host: *AppKitHost, source: c_int, sample_rate: u32, channels: u8, push_fn: AppKitAudioCapturePush, push_context: ?*anyopaque) c_int;
210
+ extern fn native_sdk_appkit_audio_capture_stop(host: *AppKitHost, source: c_int) c_int;
211
+ extern fn native_sdk_appkit_audio_capture_supported(host: *AppKitHost, source: c_int) c_int;
208
212
  extern fn native_sdk_appkit_video_load(host: *AppKitHost, path: [*]const u8, path_len: usize, token: u64, push_fn: AppKitVideoSinkPush, push_context: ?*anyopaque) c_int;
209
213
  extern fn native_sdk_appkit_video_load_url(host: *AppKitHost, url: [*]const u8, url_len: usize, token: u64, push_fn: AppKitVideoSinkPush, push_context: ?*anyopaque) c_int;
210
214
  extern fn native_sdk_appkit_video_play(host: *AppKitHost) c_int;
@@ -587,6 +591,7 @@ pub const MacPlatform = struct {
587
591
  /// every push happens on the main thread (the host's frame pump is
588
592
  /// a run-loop timer), so a plain field is race-free.
589
593
  video_sink: platform_mod.VideoFrameSink = .{},
594
+ audio_capture_sinks: [2]platform_mod.AudioCaptureSink = [_]platform_mod.AudioCaptureSink{.{}} ** 2,
590
595
 
591
596
  pub fn init(title: []const u8, size: geometry.SizeF) Error!MacPlatform {
592
597
  return initWithEngine(title, size, .system);
@@ -750,6 +755,8 @@ pub const MacPlatform = struct {
750
755
  .audio_stop_fn = audioStop,
751
756
  .audio_seek_fn = audioSeek,
752
757
  .audio_set_volume_fn = audioSetVolume,
758
+ .audio_capture_start_fn = if (self.web_engine == .system) audioCaptureStart else null,
759
+ .audio_capture_stop_fn = if (self.web_engine == .system) audioCaptureStop else null,
753
760
  .video_load_fn = videoLoad,
754
761
  .video_load_url_fn = videoLoadUrl,
755
762
  .video_play_fn = videoPlay,
@@ -822,6 +829,8 @@ pub const MacPlatform = struct {
822
829
  .audio_streaming,
823
830
  .audio_spectrum,
824
831
  => self.web_engine == .system,
832
+ .microphone_capture => self.web_engine == .system and audioCaptureSupported(self.host, .microphone),
833
+ .system_audio_capture => self.web_engine == .system and audioCaptureSupported(self.host, .system),
825
834
  // AVFoundation video ships in the AppKit host only (one
826
835
  // AVPlayer whose AVPlayerItemVideoOutput frames feed the
827
836
  // media-surface sink); the CEF host stubs the C ABI and
@@ -831,6 +840,17 @@ pub const MacPlatform = struct {
831
840
  };
832
841
  }
833
842
 
843
+ /// The host performs the availability probe because system capture is
844
+ /// version-gated at runtime (the deployment target predates
845
+ /// ScreenCaptureKit audio). Hermetic Zig tests do not link an AppKit
846
+ /// host, so keep the probe behind the same test/target seam as Linux's
847
+ /// runtime-loaded service probes.
848
+ fn audioCaptureSupported(host: *AppKitHost, source: platform_mod.AudioCaptureSource) bool {
849
+ if (comptime @import("builtin").is_test) return false;
850
+ if (@import("builtin").target.os.tag != .macos) return false;
851
+ return native_sdk_appkit_audio_capture_supported(host, @intFromEnum(source)) != 0;
852
+ }
853
+
834
854
  fn run(context: *anyopaque, handler: platform_mod.EventHandler, handler_context: *anyopaque) anyerror!void {
835
855
  const self: *MacPlatform = @ptrCast(@alignCast(context));
836
856
  self.state = .{
@@ -1531,6 +1551,54 @@ fn audioSetVolume(context: ?*anyopaque, volume: f32) anyerror!void {
1531
1551
  _ = native_sdk_appkit_audio_set_volume(self.host, volume);
1532
1552
  }
1533
1553
 
1554
+ fn nativeSdkAudioCapturePush(context: ?*anyopaque, kind: c_int, source_value: c_int, sample_rate: u32, channels: u8, timestamp_ns: u64, frames: u32, pcm: ?[*]const u8, pcm_len: usize) callconv(.c) c_int {
1555
+ const sink: *platform_mod.AudioCaptureSink = @ptrCast(@alignCast(context orelse return 1));
1556
+ const source: platform_mod.AudioCaptureSource = switch (source_value) {
1557
+ 1 => .system,
1558
+ else => .microphone,
1559
+ };
1560
+ const event_kind: platform_mod.AudioCaptureEventKind = switch (kind) {
1561
+ 0 => .started,
1562
+ 1 => .data,
1563
+ else => .failed,
1564
+ };
1565
+ const bytes = if (pcm) |ptr| ptr[0..pcm_len] else "";
1566
+ return switch (sink.push(.{
1567
+ .kind = event_kind,
1568
+ .source = source,
1569
+ .format = .{ .sample_rate = sample_rate, .channels = channels },
1570
+ .timestamp_ns = timestamp_ns,
1571
+ .frames = frames,
1572
+ .pcm_s16le = bytes,
1573
+ })) {
1574
+ .accepted => 0,
1575
+ .closed => 1,
1576
+ .dropped_full => 2,
1577
+ .dropped_oversized => 3,
1578
+ };
1579
+ }
1580
+
1581
+ fn audioCaptureStart(context: ?*anyopaque, source: platform_mod.AudioCaptureSource, format: platform_mod.AudioCaptureFormat, sink: platform_mod.AudioCaptureSink) anyerror!void {
1582
+ const self: *MacPlatform = @ptrCast(@alignCast(context.?));
1583
+ if (self.web_engine != .system) return error.UnsupportedService;
1584
+ const stored = &self.audio_capture_sinks[@intFromEnum(source)];
1585
+ // The native stop is a synchronous callback fence. Quiesce the old
1586
+ // producer before replacing the memory its callback context points at;
1587
+ // otherwise a final old-source callback can be delivered to the new sink.
1588
+ _ = native_sdk_appkit_audio_capture_stop(self.host, @intFromEnum(source));
1589
+ stored.* = sink;
1590
+ if (native_sdk_appkit_audio_capture_start(self.host, @intFromEnum(source), format.sample_rate, format.channels, nativeSdkAudioCapturePush, stored) == 0) {
1591
+ stored.* = .{};
1592
+ return error.AudioCaptureStartFailed;
1593
+ }
1594
+ }
1595
+
1596
+ fn audioCaptureStop(context: ?*anyopaque, source: platform_mod.AudioCaptureSource) anyerror!void {
1597
+ const self: *MacPlatform = @ptrCast(@alignCast(context.?));
1598
+ _ = native_sdk_appkit_audio_capture_stop(self.host, @intFromEnum(source));
1599
+ self.audio_capture_sinks[@intFromEnum(source)] = .{};
1600
+ }
1601
+
1534
1602
  /// The C-callable bridge for `VideoFrameSink.push`: the sink's `push_fn`
1535
1603
  /// is a Zig-calling-convention error-union fn the host cannot invoke, so
1536
1604
  /// the host is handed this trampoline (context = the `MacPlatform`) and
@@ -282,6 +282,14 @@ pub const NullAudio = struct {
282
282
  }
283
283
  };
284
284
 
285
+ pub const NullAudioCapture = struct {
286
+ active: bool = false,
287
+ format: types.AudioCaptureFormat = .{},
288
+ sink: types.AudioCaptureSink = .{},
289
+ start_count: usize = 0,
290
+ stop_count: usize = 0,
291
+ };
292
+
285
293
  pub const NullPlatform = struct {
286
294
  surface_value: Surface = .{},
287
295
  web_engine: WebEngine = .system,
@@ -625,6 +633,9 @@ pub const NullPlatform = struct {
625
633
  audio_stop_count: usize = 0,
626
634
  audio_seek_count: usize = 0,
627
635
  audio_volume_count: usize = 0,
636
+ microphone_capture: bool = true,
637
+ system_audio_capture: bool = true,
638
+ audio_captures: [2]NullAudioCapture = [_]NullAudioCapture{.{}} ** 2,
628
639
  /// Whether this modeled host has a video decoder. On by default (the
629
640
  /// fake below stands in for AVFoundation); tests modelling a staged
630
641
  /// host (Windows/Linux today) set it false BEFORE `platform()` so
@@ -851,6 +862,8 @@ pub const NullPlatform = struct {
851
862
  .audio_stop_fn = if (self.audio_playback) audioStop else null,
852
863
  .audio_seek_fn = if (self.audio_playback) audioSeek else null,
853
864
  .audio_set_volume_fn = if (self.audio_playback) audioSetVolume else null,
865
+ .audio_capture_start_fn = if (self.microphone_capture or self.system_audio_capture) audioCaptureStart else null,
866
+ .audio_capture_stop_fn = if (self.microphone_capture or self.system_audio_capture) audioCaptureStop else null,
854
867
  .video_load_fn = if (self.video_playback) videoLoad else null,
855
868
  .video_load_url_fn = if (self.video_playback) videoLoadUrl else null,
856
869
  .video_play_fn = if (self.video_playback) videoPlay else null,
@@ -915,6 +928,8 @@ pub const NullPlatform = struct {
915
928
  .audio_playback => self.audio_playback,
916
929
  .audio_streaming => self.audio_playback and self.audio_streaming,
917
930
  .audio_spectrum => self.audio_playback and self.audio_spectrum,
931
+ .microphone_capture => self.microphone_capture,
932
+ .system_audio_capture => self.system_audio_capture,
918
933
  .video_playback => self.video_playback,
919
934
  };
920
935
  }
@@ -1667,6 +1682,49 @@ pub const NullPlatform = struct {
1667
1682
  self.audio.volume = volume;
1668
1683
  }
1669
1684
 
1685
+ fn audioCaptureStart(context: ?*anyopaque, source: types.AudioCaptureSource, format: types.AudioCaptureFormat, sink: types.AudioCaptureSink) anyerror!void {
1686
+ const self: *NullPlatform = @ptrCast(@alignCast(context.?));
1687
+ if ((source == .microphone and !self.microphone_capture) or
1688
+ (source == .system and !self.system_audio_capture)) return error.UnsupportedService;
1689
+ const capture = &self.audio_captures[@intFromEnum(source)];
1690
+ capture.active = true;
1691
+ capture.format = format;
1692
+ capture.sink = sink;
1693
+ capture.start_count += 1;
1694
+ _ = sink.push(.{ .kind = .started, .source = source, .format = format });
1695
+ }
1696
+
1697
+ fn audioCaptureStop(context: ?*anyopaque, source: types.AudioCaptureSource) anyerror!void {
1698
+ const self: *NullPlatform = @ptrCast(@alignCast(context.?));
1699
+ const capture = &self.audio_captures[@intFromEnum(source)];
1700
+ capture.active = false;
1701
+ capture.sink = .{};
1702
+ capture.stop_count += 1;
1703
+ }
1704
+
1705
+ /// Push deterministic PCM through a live fake capture.
1706
+ pub fn pushAudioCapture(self: *NullPlatform, source: types.AudioCaptureSource, timestamp_ns: u64, pcm_s16le: []const u8) types.AudioCapturePushResult {
1707
+ const capture = &self.audio_captures[@intFromEnum(source)];
1708
+ if (!capture.active) return .closed;
1709
+ const stride = @as(usize, capture.format.channels) * 2;
1710
+ if (stride == 0 or pcm_s16le.len % stride != 0) return .dropped_oversized;
1711
+ return capture.sink.push(.{
1712
+ .kind = .data,
1713
+ .source = source,
1714
+ .format = capture.format,
1715
+ .timestamp_ns = timestamp_ns,
1716
+ .frames = @intCast(pcm_s16le.len / stride),
1717
+ .pcm_s16le = pcm_s16le,
1718
+ });
1719
+ }
1720
+
1721
+ pub fn failAudioCapture(self: *NullPlatform, source: types.AudioCaptureSource) types.AudioCapturePushResult {
1722
+ const capture = &self.audio_captures[@intFromEnum(source)];
1723
+ if (!capture.active) return .closed;
1724
+ capture.active = false;
1725
+ return capture.sink.push(.{ .kind = .failed, .source = source, .format = capture.format });
1726
+ }
1727
+
1670
1728
  fn audioUrlHash(url: []const u8) u64 {
1671
1729
  return std.hash.Wyhash.hash(0, url);
1672
1730
  }
@@ -133,6 +133,13 @@ pub const TimerEvent = types.TimerEvent;
133
133
  pub const AudioEvent = types.AudioEvent;
134
134
  pub const AudioEventKind = types.AudioEventKind;
135
135
  pub const AudioLoadResolution = types.AudioLoadResolution;
136
+ pub const AudioCaptureSource = types.AudioCaptureSource;
137
+ pub const AudioCaptureFormat = types.AudioCaptureFormat;
138
+ pub const AudioCaptureEventKind = types.AudioCaptureEventKind;
139
+ pub const AudioCaptureEvent = types.AudioCaptureEvent;
140
+ pub const AudioCapturePushResult = types.AudioCapturePushResult;
141
+ pub const AudioCaptureSink = types.AudioCaptureSink;
142
+ pub const max_audio_capture_pcm_bytes = types.max_audio_capture_pcm_bytes;
136
143
  pub const max_audio_path_bytes = types.max_audio_path_bytes;
137
144
  pub const audio_spectrum_band_count = types.audio_spectrum_band_count;
138
145
  pub const audio_spectrum_floor_db = types.audio_spectrum_floor_db;
@@ -158,6 +158,13 @@ pub const PlatformFeature = enum {
158
158
  /// keeps emitting; the null platform models the rule through its
159
159
  /// windows' modeled occlusion so the suites can pin it.
160
160
  audio_spectrum,
161
+ /// Capture from the default microphone as interleaved signed 16-bit
162
+ /// little-endian PCM. The source delivers through `AudioCaptureSink`
163
+ /// from its native audio thread until `audioCaptureStop` returns.
164
+ microphone_capture,
165
+ /// Capture the system output mix (WASAPI loopback on Windows,
166
+ /// ScreenCaptureKit audio on macOS) through the same bounded PCM sink.
167
+ system_audio_capture,
161
168
  /// The `close_policy = .hide` window shape: the host can intercept
162
169
  /// the user's close affordance, keep the window alive off the
163
170
  /// glass, and re-show it later (`show_window_fn`, tray actions, the
@@ -1456,6 +1463,79 @@ pub const AudioLoadResolution = enum(u8) {
1456
1463
  stream,
1457
1464
  };
1458
1465
 
1466
+ /// The two independently-addressable capture sources. A host may run one
1467
+ /// stream of each kind concurrently.
1468
+ pub const AudioCaptureSource = enum(u8) {
1469
+ microphone,
1470
+ system,
1471
+ };
1472
+
1473
+ /// The canonical format requested from a capture host. First-party hosts
1474
+ /// resample and remix their device-native format to this exact shape.
1475
+ pub const AudioCaptureFormat = struct {
1476
+ sample_rate: u32 = 48_000,
1477
+ channels: u8 = 1,
1478
+
1479
+ pub fn valid(self: AudioCaptureFormat) bool {
1480
+ return (self.sample_rate == 16_000 or self.sample_rate == 24_000 or self.sample_rate == 48_000) and
1481
+ (self.channels == 1 or self.channels == 2);
1482
+ }
1483
+ };
1484
+
1485
+ /// Capture reports are small, bounded records. `.data` bytes are interleaved
1486
+ /// signed 16-bit little-endian PCM in the requested format. First-party hosts
1487
+ /// keep chunks at or below 20 ms so the largest supported packet (48 kHz,
1488
+ /// stereo) is 3840 bytes and fits the effects channel's 4 KiB envelope.
1489
+ pub const max_audio_capture_pcm_bytes: usize = 3840;
1490
+
1491
+ pub const AudioCaptureEventKind = enum(u8) {
1492
+ started,
1493
+ data,
1494
+ failed,
1495
+ };
1496
+
1497
+ pub const AudioCaptureEvent = struct {
1498
+ kind: AudioCaptureEventKind,
1499
+ source: AudioCaptureSource,
1500
+ format: AudioCaptureFormat,
1501
+ timestamp_ns: u64 = 0,
1502
+ frames: u32 = 0,
1503
+ pcm_s16le: []const u8 = "",
1504
+ };
1505
+
1506
+ pub const AudioCapturePushResult = enum(u8) {
1507
+ accepted,
1508
+ dropped_full,
1509
+ dropped_oversized,
1510
+ closed,
1511
+ };
1512
+
1513
+ /// Thread-safe, non-blocking destination retained by a native capture host.
1514
+ /// `audio_capture_stop_fn` must quiesce the source before returning: no later
1515
+ /// call may enter this sink. A source exits permanently when `push` answers
1516
+ /// `.closed`; back-pressure drops are counted and reported by the effects
1517
+ /// channel that owns the sink.
1518
+ pub const AudioCaptureSink = struct {
1519
+ context: ?*anyopaque = null,
1520
+ generation: u64 = 0,
1521
+ push_fn: ?*const fn (context: ?*anyopaque, generation: u64, event: AudioCaptureEvent) AudioCapturePushResult = null,
1522
+
1523
+ pub fn push(self: AudioCaptureSink, event: AudioCaptureEvent) AudioCapturePushResult {
1524
+ const push_fn = self.push_fn orelse return .closed;
1525
+ if (!event.format.valid()) return .dropped_oversized;
1526
+ if (event.kind == .data) {
1527
+ if (event.pcm_s16le.len > max_audio_capture_pcm_bytes or
1528
+ event.pcm_s16le.len != @as(usize, event.frames) * @as(usize, event.format.channels) * 2)
1529
+ {
1530
+ return .dropped_oversized;
1531
+ }
1532
+ } else if (event.frames != 0 or event.pcm_s16le.len != 0) {
1533
+ return .dropped_oversized;
1534
+ }
1535
+ return push_fn(self.context, self.generation, event);
1536
+ }
1537
+ };
1538
+
1459
1539
  /// Longest video source string (local path or URL) `videoLoad`/
1460
1540
  /// `videoLoadUrl` accepts; longer strings are rejected with
1461
1541
  /// `error.VideoPathTooLarge` before the platform is asked.
@@ -2465,6 +2545,14 @@ pub const PlatformServices = struct {
2465
2545
  audio_seek_fn: ?*const fn (context: ?*anyopaque, position_ms: u64) anyerror!void = null,
2466
2546
  /// Set the player volume, `0.0` (silent) through `1.0` (full).
2467
2547
  audio_set_volume_fn: ?*const fn (context: ?*anyopaque, volume: f32) anyerror!void = null,
2548
+ /// Start one microphone or system-output capture. Sources are independent,
2549
+ /// but a second start of the same source replaces its current stream.
2550
+ /// Startup and asynchronous failures are reported through `sink`; a
2551
+ /// synchronous error means no source was retained.
2552
+ audio_capture_start_fn: ?*const fn (context: ?*anyopaque, source: AudioCaptureSource, format: AudioCaptureFormat, sink: AudioCaptureSink) anyerror!void = null,
2553
+ /// Stop and synchronously quiesce one source. No sink call from that source
2554
+ /// may occur after this function returns.
2555
+ audio_capture_stop_fn: ?*const fn (context: ?*anyopaque, source: AudioCaptureSource) anyerror!void = null,
2468
2556
  /// Load a local video file into THE app's single video player,
2469
2557
  /// leaving it PAUSED at position zero (transport is a separate
2470
2558
  /// verb, exactly like audio). Loading replaces whatever was loaded
@@ -3068,6 +3156,17 @@ pub const PlatformServices = struct {
3068
3156
  return volume_fn(self.context, volume);
3069
3157
  }
3070
3158
 
3159
+ pub fn audioCaptureStart(self: PlatformServices, source: AudioCaptureSource, format: AudioCaptureFormat, sink: AudioCaptureSink) anyerror!void {
3160
+ if (!format.valid() or sink.push_fn == null) return error.InvalidAudioOptions;
3161
+ const start_fn = self.audio_capture_start_fn orelse return error.UnsupportedService;
3162
+ return start_fn(self.context, source, format, sink);
3163
+ }
3164
+
3165
+ pub fn audioCaptureStop(self: PlatformServices, source: AudioCaptureSource) anyerror!void {
3166
+ const stop_fn = self.audio_capture_stop_fn orelse return error.UnsupportedService;
3167
+ return stop_fn(self.context, source);
3168
+ }
3169
+
3071
3170
  /// Load a local video file into the app's single video player (see
3072
3171
  /// `video_load_fn`). Platforms without video playback answer
3073
3172
  /// `error.UnsupportedService`; bad arguments are rejected here
@@ -3307,6 +3406,7 @@ fn defaultSupportsFeature(services: PlatformServices, feature: PlatformFeature)
3307
3406
  // cannot see it; platforms that analyze answer through their own
3308
3407
  // `supports_fn` (like file_drops and gpu_surfaces above).
3309
3408
  .audio_spectrum => false,
3409
+ .microphone_capture, .system_audio_capture => services.audio_capture_start_fn != null and services.audio_capture_stop_fn != null,
3310
3410
  // Hide-on-close is host close-delegate behavior, not a service
3311
3411
  // verb: hosts that implement it answer through their own
3312
3412
  // `supports_fn`. The generic floor is honest refusal.
@@ -206,6 +206,9 @@ extern fn native_sdk_windows_audio_pause(host: *WindowsHost) c_int;
206
206
  extern fn native_sdk_windows_audio_stop(host: *WindowsHost) c_int;
207
207
  extern fn native_sdk_windows_audio_seek(host: *WindowsHost, position_ms: u64) c_int;
208
208
  extern fn native_sdk_windows_audio_set_volume(host: *WindowsHost, volume: f64) c_int;
209
+ const WindowsAudioCapturePush = *const fn (context: ?*anyopaque, kind: c_int, source: c_int, sample_rate: u32, channels: u8, timestamp_ns: u64, frames: u32, pcm: ?[*]const u8, pcm_len: usize) callconv(.c) c_int;
210
+ extern fn native_sdk_windows_audio_capture_start(host: *WindowsHost, source: c_int, sample_rate: u32, channels: u8, push_fn: WindowsAudioCapturePush, push_context: ?*anyopaque) c_int;
211
+ extern fn native_sdk_windows_audio_capture_stop(host: *WindowsHost, source: c_int) c_int;
209
212
  extern fn native_sdk_windows_audio_spectrum_supported(host: *WindowsHost) c_int;
210
213
  extern fn native_sdk_windows_show_context_menu(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, x: f64, y: f64, token: u64, items: [*]const WindowsContextMenuItem, count: usize) c_int;
211
214
 
@@ -315,6 +318,7 @@ pub const WindowsPlatform = struct {
315
318
  /// `createWithOptions` and retire it through `destroy`, the
316
319
  /// latch-gated free.
317
320
  channel_wake_abandoned: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
321
+ audio_capture_sinks: [2]platform_mod.AudioCaptureSink = [_]platform_mod.AudioCaptureSink{.{}} ** 2,
318
322
 
319
323
  pub fn init(title: []const u8, size: geometry.SizeF) Error!WindowsPlatform {
320
324
  return initWithEngine(title, size, .system);
@@ -476,6 +480,8 @@ pub const WindowsPlatform = struct {
476
480
  .audio_stop_fn = audioStop,
477
481
  .audio_seek_fn = audioSeek,
478
482
  .audio_set_volume_fn = audioSetVolume,
483
+ .audio_capture_start_fn = if (self.web_engine == .system) audioCaptureStart else null,
484
+ .audio_capture_stop_fn = if (self.web_engine == .system) audioCaptureStop else null,
479
485
  // The video load verbs are teaching refusals (see
480
486
  // `videoLoad`); the transport verbs stay null — the
481
487
  // channel never activates without a successful load.
@@ -519,6 +525,8 @@ pub const WindowsPlatform = struct {
519
525
  .gpu_surfaces,
520
526
  .audio_playback,
521
527
  .audio_streaming,
528
+ .microphone_capture,
529
+ .system_audio_capture,
522
530
  => self.web_engine == .system,
523
531
  // close_policy .hide: WM_CLOSE hides (ShowWindow SW_HIDE),
524
532
  // the window stays in the host map, and the tray is the
@@ -1626,6 +1634,54 @@ fn audioSetVolume(context: ?*anyopaque, volume: f32) anyerror!void {
1626
1634
  _ = native_sdk_windows_audio_set_volume(self.host, volume);
1627
1635
  }
1628
1636
 
1637
+ fn nativeSdkAudioCapturePush(context: ?*anyopaque, kind: c_int, source_value: c_int, sample_rate: u32, channels: u8, timestamp_ns: u64, frames: u32, pcm: ?[*]const u8, pcm_len: usize) callconv(.c) c_int {
1638
+ const sink: *platform_mod.AudioCaptureSink = @ptrCast(@alignCast(context orelse return 1));
1639
+ const source: platform_mod.AudioCaptureSource = switch (source_value) {
1640
+ 1 => .system,
1641
+ else => .microphone,
1642
+ };
1643
+ const event_kind: platform_mod.AudioCaptureEventKind = switch (kind) {
1644
+ 0 => .started,
1645
+ 1 => .data,
1646
+ else => .failed,
1647
+ };
1648
+ const bytes = if (pcm) |ptr| ptr[0..pcm_len] else "";
1649
+ return switch (sink.push(.{
1650
+ .kind = event_kind,
1651
+ .source = source,
1652
+ .format = .{ .sample_rate = sample_rate, .channels = channels },
1653
+ .timestamp_ns = timestamp_ns,
1654
+ .frames = frames,
1655
+ .pcm_s16le = bytes,
1656
+ })) {
1657
+ .accepted => 0,
1658
+ .closed => 1,
1659
+ .dropped_full => 2,
1660
+ .dropped_oversized => 3,
1661
+ };
1662
+ }
1663
+
1664
+ fn audioCaptureStart(context: ?*anyopaque, source: platform_mod.AudioCaptureSource, format: platform_mod.AudioCaptureFormat, sink: platform_mod.AudioCaptureSink) anyerror!void {
1665
+ const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
1666
+ if (self.web_engine != .system) return error.UnsupportedService;
1667
+ const stored = &self.audio_capture_sinks[@intFromEnum(source)];
1668
+ // The native stop is a synchronous callback fence. Quiesce the old
1669
+ // producer before replacing the memory its callback context points at;
1670
+ // otherwise a final old-source callback can be delivered to the new sink.
1671
+ _ = native_sdk_windows_audio_capture_stop(self.host, @intFromEnum(source));
1672
+ stored.* = sink;
1673
+ if (native_sdk_windows_audio_capture_start(self.host, @intFromEnum(source), format.sample_rate, format.channels, nativeSdkAudioCapturePush, stored) == 0) {
1674
+ stored.* = .{};
1675
+ return error.AudioCaptureStartFailed;
1676
+ }
1677
+ }
1678
+
1679
+ fn audioCaptureStop(context: ?*anyopaque, source: platform_mod.AudioCaptureSource) anyerror!void {
1680
+ const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
1681
+ _ = native_sdk_windows_audio_capture_stop(self.host, @intFromEnum(source));
1682
+ self.audio_capture_sinks[@intFromEnum(source)] = .{};
1683
+ }
1684
+
1629
1685
  /// The video tier's teaching refusal: the load verbs exist so a video
1630
1686
  /// source fails with a NAMED explanation instead of a bare
1631
1687
  /// unsupported-service, while the transport verbs stay null — the