Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Record from the Mac's microphone in the guest

Jacky5daa03ee957c
diff --git a/Research/0_binary_patch_comparison.md b/Research/0_binary_patch_comparison.md
index bdc6ee08..0ce09bb5 100644
--- a/Research/0_binary_patch_comparison.md
+++ b/Research/0_binary_patch_comparison.md
@@ -2509,3 +2509,38 @@ comparison 0x1464e0 → head 0x1475d0, fall-through 0x1464e4), byte-
identical re-runs, and all four VirtualAudio verbs composing on one
staged copy with a clean second pass. Research detail:
`Research/Guest/virtualaudio_speaker_route_throws.md`.
+
+## VirtualAudio's speaker-protection gate, second site — the play-and-record handler (2026-10-04)
+
+`system-virtualaudio-cfw-speaker_protection_gate` (changed: one more site,
+record `system-virtualaudio-cfw-speaker_protection_gate.playback_and_record`),
+same binary, same verb `cfw patch-virtualaudio-sp-gate`, which now runs the
+patch once per handler in `CustomFirmwareVirtualAudio.SPGateHandler`.
+
+**Fault.** The guest's virtio sound plugin now publishes a microphone
+(`Digital Mic`, port `pmbi`; `Research/Guest/virtio_sound_microphone.md`).
+A recording app asks for a route that plays and records (`cpar`), which
+`RoutingHandler_PlaybackAndRecord_GenericConfig1` builds in full —
+aggregate of `Digital Mic` and `PuffinOutput`, a virtual stream on each
+port — and then declines at `:368` with the same "HAL Speaker Protection is
+missing. Failing route %s" the playback handler declined the ringtone route
+with. Before the microphone existed the route never got that far.
+
+**The site.** The decline's format is shared by three handlers; this one
+is the handler whose function also references
+`RoutingHandler_PlaybackAndRecord_GenericConfig1.cpp`
+(`locatePlaybackHandler(in:file:)`, the same resolution with the other file
+name; neither file name contains the other). The gate and its block have
+the playback handler's shape on both builds: a `tbz w9, #0x0` into a log
+block that opens `mov w0, #0x14; bl`, with an unconditional branch above
+the block's head, and a fall-through that reloads what it reads from the
+stack and is also the join of the handler's raw-mode log path.
+
+**The write.** As for the first site: the log block's head becomes a `b`
+to the gate's fall-through; the gate's own branch stays. Verified on the
+real iPadOS 26.6.2 and iOS 27 binaries: gate 0xea274 → head 0xea588,
+fall-through 0xea278; gate 0x10e468 → head 0x10e6f0, fall-through 0x10e46c;
+both sites written in one run, a second run reporting both already patched.
+The handler's volume-mode test (`and x28, x0, #0x1ffffffff` at 0xea3fc) is
+a branch between two builds of the route, not a precondition, and is not
+touched.
diff --git a/Research/Guest/virtio_sound.md b/Research/Guest/virtio_sound.md
index 2746025e..72ad95f9 100644
--- a/Research/Guest/virtio_sound.md
+++ b/Research/Guest/virtio_sound.md
@@ -60,20 +60,26 @@ timestamp period of `rate × 260 / 1000` frames and safety offsets of 100 frames
It is written on AudioServerDriver, the private framework `BuiltinAudioPlugin`
itself uses on iOS (its log shows `ASD_Initialize`); the classes are declared in
`VPVirtIOSoundAudioServerDriver.h` and bound through `AudioServerDriver.tbd`.
-It publishes one HAL device per `AppleVirtIOSound` service and one output stream
-per virtio output stream, at the format the device offers (32-bit float, 48 kHz,
-2 channels from `VZHostAudioOutputStreamSink`). Input streams are left out, so the
-host microphone is never opened. The virtio stream is released when CoreAudio
-stops and the last transfer has come back, so an idle guest does not keep the
-host audio device running.
+It publishes two HAL devices per `AppleVirtIOSound` service, a speaker with one
+output stream per virtio output stream and a microphone with one input stream
+per virtio input stream, each at the format the device offers (32-bit float,
+48 kHz from `VZHostAudioOutputStreamSink` and `VZHostAudioInputStreamSource`).
+A virtio stream is released when CoreAudio stops and the last transfer has come
+back, so an idle guest does not keep the host audio device running and the host
+microphone is open only while the guest records.
+
+The input side (the macOS plugin's receive path, why the microphone is a
+second device named `Digital Mic`, what VirtualAudio needed, and what is
+verified so far) is in `virtio_sound_microphone.md`.
`cfw install` and `cfw update-environment` install it at
`/System/Library/Audio/Plug-Ins/HAL/VPhoneVirtIOSound.driver`
(`system-virtiosound-cfw-hal_plugin`).
-Two optional values in the `com.apple.coreaudio` domain exist for routing work
+Optional values in the `com.apple.coreaudio` domain exist for routing work
and are read when audiomxd loads the plugin: `VPhoneVirtIOSoundTransportType`
-(four characters, default `usb `) and `VPhoneVirtIOSoundDeviceUID`.
+(four characters, default `usb `), `VPhoneVirtIOSoundDeviceUID` and
+`VPhoneVirtIOSoundInputDeviceUID`.
## 2. VirtualAudio never finished initializing (iPad guests)
diff --git a/Research/Guest/virtio_sound_microphone.md b/Research/Guest/virtio_sound_microphone.md
new file mode 100644
index 00000000..a16c4083
--- /dev/null
+++ b/Research/Guest/virtio_sound_microphone.md
@@ -0,0 +1,533 @@
+# Microphone input through virtio-snd
+
+The speaker chain is in `virtio_sound.md`; this is the input side: what the
+macOS plugin does to capture, what the guest plugin does, and what
+VirtualAudio needed before a recording app's route would build. Sources, all
+local:
+
+* macOS `/System/Library/Audio/Plug-Ins/HAL/AppleVirtIOSound.driver`, arm64e
+ slice, disassembled with `otool -tV` (offsets in §1 are that slice's).
+ This is Apple's own virtio-snd HAL plugin, the reference the guest plugin
+ was modeled on for output.
+* The guest's own `AudioServerDriver`, dumped in the guest
+ (`asd-guest-dump.txt`, `asdimp-out.txt` from the speaker work): it has
+ `addInputStream:`, `setReadInputBlock:`, `setInputLatency:` and
+ `setInputSafetyOffset:`, so the input API is there and
+ `AudioServerDriver.tbd` needs nothing new (it binds whole classes).
+* The guest's `VirtualAudio` binaries saved under `~/.vphone/va-analysis/`
+ (iPadOS 26.6.2 and iOS 27.0), and audiomxd logs captured on a test guest
+ (`mictest-ipad`, iPad16,1 / 26.6.2) while this was built.
+
+## 0. Where the chain stands
+
+| Layer | State |
+| --- | --- |
+| Host backend | Was already there: the VM's `VZVirtioSoundDeviceConfiguration` has a `VZHostAudioInputStreamSource`. What was missing is the microphone usage description macOS wants from the process that opens the microphone (§3). |
+| Guest kernel | `AppleVirtIOSound` reports stream 0 as an input stream (`direction 1, formats 0xa0020, rates 0x480, channels 1-2`) and serves selector 9, the receive transfer the macOS plugin uses (§6). |
+| Guest HAL plugin | Publishes a second device, the microphone, with the input stream (§5). |
+| Guest routing | The microphone device must be named `Digital Mic`, the route handler for play-and-record needs its speaker-protection gate opened, and the streams must accept the format that route sets on them (§4). With the three, Voice Memos records on an iPad guest (iPadOS 26.6.2). On an iPhone guest (iOS 27.0) the route builds and Voice Memos fails before any I/O, in the capture stack (§6). |
+
+## 1. The reference implementation: `AVIOStream` input in the macOS plugin
+
+### 1.1 Device construction (`AVIOPlugin halInitializeWithPluginHost:`, 0xe08)
+
+Per `AppleVirtIOSound` service: `IOServiceOpen` on the plugin's own task, then
+three registry counts through a shared helper (`0x1ab8`) —
+`AVIOSoundJackCountKey`, `AVIOSoundStreamCountKey`, `AVIOSoundChannelMapCountKey`
+(strings at 0x6d57/0x6d6d/0x6d85). The **jack count drives the control set**
+(§1.3); the stream count drives the enumeration loop.
+
+Stream enumeration, one `PCM_INFO` (selector 1) per virtio stream ID, parsed
+out of the 32-byte `virtio_snd_pcm_info`:
+
+| Struct offset | Field | Use |
+| --- | --- | --- |
+| +0x08 (u64) | formats | preference ladder, §1.2 |
+| +0x10 (u64) | rates | **gate: bit 7 (48000 Hz) must be set or the stream is skipped entirely** (`tbnz w9,#0x7` at 0x1234; the ASBD is built at a hardcoded 48000.0) |
+| +0x18 (u8) | direction | 0 → scope `'outp'`, stream name "Output Stream"; 1 → scope `'inpt'`, stream name "Input Stream" (0x11a4-0x11d4) |
+| +0x1a (u8) | channelsMax | ASBD `mChannelsPerFrame` |
+
+Each accepted stream becomes one `AVIOStream`
+(`initWithDirection:('inpt'/'outp') withPlugin:`, 0x13f4), with:
+
+* a serial `dispatch_queue_create("com.apple.AVIOStream.data", NULL)`;
+* an `IONotificationPort` whose dispatch queue is that same queue — async
+ completions are delivered on it (this is why every handler begins with
+ `dispatch_assert_queue$V2`);
+* `_volume = 1.0f`, state machine `Initial → SetParameters → Prepared →
+ Started → Stopped → Released`.
+
+For each rate the device advertises, a copy of the 48 kHz
+`ASDStreamFormat` is made with `setSampleRate:` + `setMinimumSampleRate:` +
+`setMaximumSampleRate:` pinned to that rate, then all copies go to
+`setPhysicalFormats:` and the 48 kHz one to `setPhysicalFormat:`.
+
+Registration is by direction (0x1500-0x1550): `[device addInputStream:stream]`
+for `'inpt'`, `[device addOutputStream:stream]` for `'outp'`. The macOS plugin creates and registers
+input streams unconditionally, on the same device as the output streams.
+
+Device-level properties the input path needs: `setCanBeDefaultInputDevice:` is
+among the three `setCanBeDefault*:` calls (0x1080-0x109c),
+`setInputSafetyOffset: 100` (0x10cc), and `setTimestampPeriod:
+rate × 260 / 1000` (0x10f4) — the input side shares the device clock, exactly
+as `virtio_sound.md` §6 already established for output.
+
+### 1.2 Format selection (the formats bitmap, 0x11dc-0x1234)
+
+First matching bit wins, in this order — the winner sets
+`mBitsPerChannel` and the `'lpcm'` flags:
+
+| bit (`VIRTIO_SND_PCM_FMT_*`) | format | bits | flags |
+| --- | --- | --- | --- |
+| 19 | float32 | 32 | 1 (float) |
+| 20 | float64 | 64 | 1 (float) |
+| 18/17 | S32 | 32 | 4 (signed int) |
+| 12/11 | S24 packed / S24 | 24 | 4 |
+| 6/5/4 | S18?/S16 variants | 16 | 4 |
+| 3 | U8 | 8 | 0 |
+
+`VZHostAudioInputStreamSource` offers float32/48 kHz like the sink, so the
+guest plugin's existing `VPVirtIOSoundChooseFormat` preference already
+matches Apple's ladder for the formats that matter.
+
+### 1.3 The input control set (0x159c-0x177c)
+
+After the stream loop, per direction (the guest plugin builds the same set
+for each of its two devices; `virtio_sound.md`, validation item 5):
+
+* Output, when `hasOutput`: `ASDSelectorValue` `'ispk'` named "Speakers" →
+ `ASDSelectorControl` (`initWithIsSettable:YES element:0 scope:'outp' …
+ andObjectClassID:'dsrc'`) → `addValue:` / `setSelectedValues:` →
+ `[device addControl:]` → `_addMuteAndVolumeControlsInScope:'outp'`
+ (0x1b48).
+* Input, when `hasInput` (0x168c onwards): the identical shape with
+ **`ASDSelectorValue` `'imic'` named "Microphone"**, scope `'inpt'`, then
+ `_addMuteAndVolumeControlsInScope:'inpt'` — **mute and volume controls in
+ the input scope too** (the assert at 0x6e4b names both scopes legal).
+
+Same lifecycle constraints as the output controls apply on the guest
+(`virtio_sound.md` item 5): build through the explicit-class initializers,
+add after device activation, never `booleanValue`.
+
+### 1.4 Stream start (`-[AVIOStream startStream]` block invoke, 0x40e0-0x4290)
+
+On the stream queue, the state machine runs
+`SET_PARAMS` (selector 3, 24-byte struct) → `PREPARE` (selector 4, scalar) →
+**if `direction == 'inpt'`: submit the initial receive buffers — the loop at
+0x41a8-0x41b8 calls `_readInputIntoRingBuffer` exactly four times
+(`kInputInitialBuffers = 4`), after asserting the ring holds at least four
+periods** → `START` (selector 5).
+
+Those four periods in flight then keep themselves alive: every completion
+submits one more (§1.6). There is **no timer on the input side** — unlike the
+output side's 1/24 s flush timer, capture is completion-driven.
+
+### 1.5 RX submission (`-[AVIOStream _readInputIntoRingBuffer]`, 0x4410)
+
+Asserts on-queue, `direction == 'inpt'`, `isStreaming`, ring present. Ring
+geometry (the C++ `AVIOStreamRingBuffer`, layout recovered from
+`readInputFrames` and this function):
+
+| offset | field |
+| --- | --- |
+| +0x00 | u32 direction (0 = Output, 1 = Input) |
+| +0x08 | `uint8_t *_buffer` |
+| +0x10 | u32 capacity, bytes (assert `capacity % period == 0`) |
+| +0x14 | u32 period, bytes |
+| +0x18 | u32 bytesPerFrame |
+| +0x20 | u64 readPosition — advanced by the HAL pull |
+| +0x28 | u64 writePosition — advanced by completions, a period at a time |
+| +0x30 | u64 inFlightPosition — advanced at submission |
+| +0x38 | u8 detached |
+
+Submission gate: `capacity − (inFlight − read) ≥ period`, else return
+(a full ring means the HAL is not keeping up; capture pauses, nothing is
+overwritten or dropped). Then:
+
+```
+slot = _buffer + (inFlight % capacity); // never straddles the end
+inFlight += period;
+```
+
+and the transfer itself (0x45c0-0x45ec) — the exact RX counterpart of the
+plugin's TX call:
+
+```c
+uint64_t references[8] = {0}; // kOSAsyncRef64Count, zeroed
+references[1] = paciza(completion_trampoline); // kIOAsyncCalloutFuncIndex
+references[2] = retained_block; // kIOAsyncCalloutRefconIndex
+uint64_t streamID = self->_virtioStreamId; // 1 scalar in
+size_t periodBytes = period;
+IOConnectCallAsyncMethod(
+ connection,
+ 9, // kVPVirtIOSoundSelectorRead
+ IONotificationPortGetMachPort(asyncPort),// wake port (queue-owned)
+ references, 3,
+ &streamID, 1, // input scalars
+ NULL, 0, // no input struct
+ NULL, NULL, // no output scalars
+ slot, &periodBytes); // output struct = the ring slot
+```
+
+Three async reference slots are passed (same `kAsyncReferenceCount` the guest
+plugin already uses for TX), in IOKit's own layout: slot 0 is reserved for
+the wake port, `references[1]` (stored at `sp+0x78`, 0x4584) is the C
+trampoline signed `paciza` (0x4ec4), `references[2]` (`sp+0x80`, 0x4590) the
+retained Objective-C completion block as the refcon. If the call fails
+synchronously, the completion block is invoked immediately with
+`(kern_return, 0)` (0x45f8-0x4620).
+
+The completion block's type encoding is `v16@?0i8I12` —
+`void (^)(kern_return_t result, UInt32 bytesTransferred)`.
+
+### 1.6 RX completion (trampoline 0x4ec4 → block invoke 0x4fd0)
+
+The trampoline receives the refcon (the block) from IOKit's async
+machinery, invokes the block with whatever IOKit passed after it left in
+place (`x1`, `x2`), and releases it. The block **reads neither**: the invoke
+(0x4fd0) keeps only its own captures across `dispatch_assert_queue`. On the
+stream queue:
+
+1. `ring->didWriteInputInFlightBytes()` (0x54dc) — takes the ring alone and
+ advances `writePosition` by **one period** (`+0x14`), whatever the kernel
+ reported. A completion is a period; a failed or short one is counted the
+ same.
+2. Retirement bookkeeping (`0x496c`) for the submitted ring — a stream whose
+ ring was replaced mid-flight (`_ringBuffersToDelete` /
+ `_deleteRingBufferIfDone:` / `_isActiveStreamingRingBuffer:`) retires it
+ without resubmitting.
+3. **If the completing ring is still the stream's current ring → call
+ `_readInputIntoRingBuffer` again.** This is the steady-state loop: four in
+ flight, one resubmitted per completion.
+
+### 1.7 The HAL pull (`readInputBlock` getter 0x32b4 → invoke 0x3350)
+
+The getter **rebuilds and returns the block on every call** — it is a property
+*getter override*, and this is Apple's own answer to the ASD
+releases-blocks-on-stop behavior the vphone plugin works around with
+`installIOBlocks` (`virtio_sound.md` §6): the macOS plugin never lets the
+property read nil. (The device-level `getZeroTimestampBlock` /
+`willDoReadInputBlock` / `willDoWriteMixBlock` getters, 0x2358/0x2434/0x24e0,
+are the same pattern.)
+
+The invoke is `AVIOStreamRingBuffer::readInputFrames` inlined — signature the
+full IO-block shape `(int)(UInt32 frameCount, const
+AudioServerPlugInIOCycleInfo *cycleInfo, void *mainBuffer, void
+*secondaryBuffer, UInt32 clientID)`:
+
+1. `byteCount = bytesPerFrame × frameCount`;
+2. **if `byteCount > capacity` or `byteCount > (writePosition −
+ readPosition)` → `bzero(mainBuffer, byteCount)`** — an underrun delivers
+ silence, never stale audio and never an error;
+3. else `memcpy` out of `_buffer + (readPosition % capacity)`, split in two
+ when the range wraps the end of the buffer;
+4. `readPosition += byteCount` (assert `readPosition <= writePosition`);
+5. optional gain: `volumeProcessor->process(frameCount, mainBuffer,
+ (float)*(double *)((char *)cycleInfo + 0x50))` — the scalar the macOS
+ plugin reads out of the cycle info; skipping this (gain 1.0) is fine for a
+ first implementation.
+
+So the input data path is: kernel fills slot → completion advances
+`writePosition` and resubmits → the HAL's IO cycle pulls
+`readInputFrames` → CoreAudio. The ring absorbs the rate mismatch; overrun
+pauses submission, underrun gives silence.
+
+### 1.8 `willDo*` answers (block invokes 0x3e0 / 0x4b4)
+
+The device's `willDoReadInputBlock` / `willDoWriteMixBlock` answers are built
+by the getters from the device's direction presence: `*willDo = <has that
+direction>`, `*willDoInPlace = true`; the only error path is NULL out-pointers
+(`'unop'`). The guest plugin answers the same way, for each device's own direction.
+
+## 2. The AudioServerDriver input API
+
+Introspected from the host's shared cache with `class_copyMethodList`. The
+`AudioServerDriver.tbd` the guest plugin links needs no additions: it binds
+whole classes (`objc-classes:`), and selectors resolve at runtime.
+
+`ASDAudioDevice`, input-relevant surface: `addInputStream:`,
+`removeInputStream:`, `inputStreams`, `hasInput`, `canBeDefaultInputDevice`
+(+ setter), `inputLatency`, `inputSafetyOffset`, `inputMaximumIOFrameSize`
+(+ setters), `willDoReadInputBlock` (+ setter + `willDoReadInputBlockUnretainedPtr`),
+`startIOForClient:` / `stopIOForClient:`, `beginIOOperationBlock` /
+`endIOOperationBlock`.
+
+`ASDStream`: `initWithDirection:withPlugin:` (direction is the scope
+fourcc), `setDirection:`/`direction`, **`readInputBlock` (+ setter +
+`readInputBlockUnretainedPtr`)**, `readIsolatedInputBlock`, `terminalType`
+(+ setter), `startingChannel`, `latency`, `setIsActive:`,
+`channelCategoryForChannelIndex:` / `channelNameForChannelIndex:` /
+`channelNumberForChannelIndex:`, and a DSP-hook family
+(`processInputBlock`, `convertInputBlock`, `mixOutputBlock`, …) the virtio
+plugin leaves alone.
+
+
+The guest's framework has the same input surface: its own method dump lists
+`addInputStream:`, `removeInputStream:`, `inputStreams`, `hasInput`,
+`setCanBeDefaultInputDevice:`, `setInputLatency:`, `setInputSafetyOffset:`,
+`setWillDoReadInputBlock:` on `ASDAudioDevice` and `readInputBlock`,
+`setReadInputBlock:`, `readInputBlockUnretainedPtr` on `ASDStream`. Every
+block property has an `…UnretainedPtr` twin, the copy the I/O thread calls,
+which `performStopIO` and `stopStream` clear (`virtio_sound.md` §6): the read
+block, like the mix block, has to be set again before every start.
+
+## 3. The host side
+
+`VZHostAudioInputStreamSource` opens the Mac's microphone when the guest
+starts its input stream, in the `vphone-vm` process. macOS asks the user on
+behalf of the process responsible for that, and refuses a process whose
+bundle has no `NSMicrophoneUsageDescription`. `VPhone.bundle` had none.
+
+* `VPhone.bundle/Contents/Info.plist` now carries the description, and
+ `InfoPlist.xcstrings` (the catalog that already translated the location
+ prompt for `VPhoneLocation.app`) is compiled into the bundle's own
+ `Resources` as well, so the prompt is in the user's language.
+ `ValidateBundle.sh` checks both.
+* Launchpad's `Info.plist` and catalog carry the same text. A VM that
+ Launchpad starts is responsible for itself (`responsibility_spawnattrs_setdisclaim`),
+ but when that call is unavailable the VM is attributed to Launchpad, the
+ same arrangement as the location descriptions there.
+* `vphone-vm` is signed ad hoc, so the permission macOS records is tied to
+ that build's code hash: a new bundle version asks again.
+
+The host captures at its own rate and Virtualization.framework hands the
+guest the 48 kHz float frames the stream was set up for.
+
+## 4. The routing side
+
+### The device's name decides the port
+
+VirtualAudio's device factory builds a physical device, and a port, only for
+the UIDs in its table (`0xe1354`… in the 26.6.2 binary): `Speaker`,
+`AppleSongbirdDSP`, `PuffinInput`, `PuffinOutput`, `Actuator`, `AOP Audio-1`,
+`HP16Mic`, `Digital Mic`, `DigitalMic`, `Mic`, `Hawking`, `Flicker`,
+`Penrose`, … One UID names one kind of port, so the speaker (`PuffinOutput`,
+`pspk`) and the microphone cannot be one HAL device: the plugin publishes two
+per `AppleVirtIOSound` service.
+
+Three names were candidates for the microphone:
+
+| UID | What VirtualAudio builds | Result |
+| --- | --- | --- |
+| `Hawking` | a `phki` port, "Hawking Input" (`Device_Hawking_Aspen.cpp`). The earlier experiment that found it ran with an output-only device. | Not the built-in microphone: record routes are written against `pmbi`. Not tried with an input stream. |
+| `PuffinInput` | `Device_Puffin.cpp`'s input twin of the speaker (`0x28f24c`; it throws "Puffin audio device has no input streams" without one). Port type `pmbi` (`0x28f09c`). | The port is published, bare. Every record route asks it for the product's microphone sub-ports and throws: `Port.cpp:776 EXCEPTION (std::logic_error): "No match found for internal sub-port ID: vcrg"` for speech detection (`crec`/`vrom`), `… btm1` for a recording app (`cpar`/`imdf`), then `Route change failed: nort.` |
+| **`Digital Mic`** | the codec microphone (`Device_DigitalMic_Aspen.cpp`), with the sub-ports the routing settings define for the product. | `Adding port [ type: pmbi; uid: Built-In Microphone … rout: 1 ]`, no sub-port exception, and the route source reads `'pmbi'; iPad Microphone; Built-In Microphone`. |
+
+So the microphone device is `Digital Mic` unless
+`VPhoneVirtIOSoundInputDeviceUID` names another. With it, at audiomxd launch
+VirtualAudio reads the input volume control (`Device Digital Mic, hardware
+volume range: -60.000000 0.000000 [inpt/0]`) and builds the speech-detection
+aggregate on it (`VAD [vspd] AggDev`, master `Digital Mic`).
+
+### The route a recording app asks for
+
+Voice Memos records with category `cpar` (play and record), mode `imdf`.
+`RoutingHandler_PlaybackAndRecord_GenericConfig1` builds that route in full:
+
+```
+…GenericConfig1.cpp:160 Activating route [ … { [ source: 'pap ' … / destination: 'pspk'; Speaker; PuffinOutput ],
+ [ source: 'pmbi'; iPad Microphone; Built-In Microphone / destination: 'pap ' … ] } ]
+AggregateDeviceUtilities.cpp:622 Creating HAL Aggregate … master = "Digital Mic"; subdevices = ( "Digital Mic", PuffinOutput ); uid = "VAD [vdef] AggDev 7"
+StreamUtilities.cpp:376 Created virtual stream for [vdef] (cpar/imdf) port pmbi with DSP chain 'none' …
+StreamUtilities.cpp:376 Created virtual stream for [vdef] (cpar/imdf) port pspk with DSP chain 'speaker_general' …
+…GenericConfig1.cpp:368 HAL Speaker Protection is missing. Failing route …
+RoutingManager.cpp:3524 Routing is not supported: attempt to activate the routes failed.
+VirtualAudio_PlugIn.mm:2815 EXCEPTION (routeUpdateInfo.first): "Route change failed: nort."
+```
+
+and then declines it for the capability the playback handler declined the
+ringtone route for (`virtualaudio_speaker_route_throws.md`): HAL Speaker
+Protection, which only a physical codec reports. The gate has the same shape
+in both handlers and on both builds:
+
+| Build | Gate | Decline's log block | Fall-through |
+| --- | --- | --- | --- |
+| iPadOS 26.6.2 | `0xea274 tbz w9, #0x0, 0xea588` | `0xea588 mov w0, #0x14` (above it `b 0xea6d4`) | `0xea278`, which reloads what it reads from the stack |
+| iOS 27.0 | `0x10e468 tbz w9, #0x0, 0x10e6f0` | `0x10e6f0 mov w0, #0x14` (above it `b 0x10e814`) | `0x10e46c`, the same |
+
+`system-virtualaudio-cfw-speaker_protection_gate` therefore writes a second
+site, record `….playback_and_record`: the log block's head becomes a branch
+back to the gate's fall-through, exactly as in the playback handler, found by
+this handler's own file name
+(`RoutingHandler_PlaybackAndRecord_GenericConfig1.cpp`). The verb
+`cfw patch-virtualaudio-sp-gate` patches both; `cfw install` and
+`cfw update-environment` already run it.
+
+This handler also masks a volume-mode lookup with the 33-bit packing mask
+(`0xea3fc`), but branches on it between two ways of building the route. It
+is not a precondition and nothing declines there.
+
+### The format the route sets on the streams
+
+With the gate opened the route reached its last step and failed there:
+
+```
+Stream_HAL_Common.cpp:408 Synchronously setting physical format to [ 32/48000/2; … ] on stream 44.
+HALS_UCPlugIn.cpp:1190 HALS_UCPlugIn::ObjectSetPropertyData: failed: … Error: 2003329396
+HALPropertySynchronizer.h:311 error 2003329396 (what) setting property data for property [pft /glob/0] on id 44.
+AggregateDevice_Duplex.h:163 EXCEPTION (kAudioHardwareUnspecifiedError): "failed to set the new format on the aggregate device"
+```
+
+Stream 44 is the speaker's, left at 44100 by the last system sound. A
+playback route moves the rate through the device (`nsrt`, which the plugin
+already answers itself because `ASDAudioDevice` refuses it for a plugin
+device; `virtio_sound.md`, validation item 6). The duplex aggregate sets the
+stream's physical format instead, and `ASDStream` refuses that the same
+way. The plugin's streams now take a refused `pft `/`sfmt` set whose rate
+the device supports and make it the device's rate change, which reaches
+every stream: `stream 1: format set to 48000 Hz through the device`.
+
+Two messages in the same capture did not stop the route and are left alone:
+`error 'what' setting property data for property [ssrc/inpt/0] on id 37`
+(VirtualAudio selecting a data source on the device; the plugin's selector
+control answers the control-level property, not this device-level one) and
+`We expect this device to report a correct clock domain, but it is coming
+back as NULL/zero`.
+
+## 5. What the guest plugin does
+
+`VPhoneGuestComponents/VirtIOSound`:
+
+* **Two devices per service**, sharing one user-client connection
+ (`VPVirtIOSoundConnection`). The macOS plugin has one device with both
+ directions on one connection and a queue per stream; the guest keeps that
+ and only splits the HAL device. A second `IOServiceOpen` was not tried.
+* **Streams**: `VPVirtIOSoundStream` holds what both directions share (the
+ virtio stream's identity and format, the serial queue, SET_PARAMS and
+ PREPARE, the property inventory); `VPVirtIOSoundOutputStream` is the
+ speaker path as it was; `VPVirtIOSoundInputStream` is new.
+* **Input transfers** (`submitReads`): four periods of the input ring are
+ with the kernel at a time (selector 9, the slot as the output structure),
+ submitted between PREPARE and START as the macOS plugin does, and each
+ completion submits the next. A completion counts as one period, as in the
+ macOS plugin; a failed one is zeroed first, and is not replaced at once but
+ a period later, so a kernel that refuses the selector costs four calls a
+ period and no more.
+* **The ring** (`VPVirtIOSoundInputRing`, `consumed <= filled <= submitted`)
+ and **the reader** (`VPVirtIOSoundInputReader`) are in
+ `VPVirtIOSoundRing.[ch]` and tested on the host
+ (`make test-virtiosound`). The reader is where the guest departs from the
+ macOS plugin, which copies out whatever is there and zero-fills a short
+ read: captured frames arrive a period (85 ms) at a time and the HAL reads a
+ few milliseconds at a time, so read straight through the ring runs empty
+ just before every period lands. Reads are served once two periods are
+ buffered, a read that finds too little waits for two again, and more than
+ four periods buffered are trimmed back to two, which bounds the delay when
+ the host captures faster than the guest's clock reads. The device reports
+ the two periods as `inputLatency`.
+* **Stop**: the reads still out come back as the host fills them, a period
+ apart, and the device is released (STOP, RELEASE) after the last, so the
+ host microphone closes about a third of a second after the guest stops
+ recording. Releasing first would leave the kernel holding ring slots with
+ nothing known about when it returns them; that is the fallback after one
+ second, for a host that has stopped filling them, and the stream then
+ waits for the slots before it can be set up again. A start that finds the
+ stream still draining carries on with the device as it is.
+* **A full ring** (the HAL not reading) leaves fewer than four reads out, and
+ no completion would bring the count back; the stream looks again a period
+ later.
+* **Stream formats**: a format set that `ASDStream` refuses becomes the
+ device's rate change when the device supports the rate (§4), on both
+ kinds of stream.
+* **Device**: `canBeDefaultInputDevice`, `inputSafetyOffset` 100, wire rate
+ only (the 44100 twin is the speaker's; captured frames are not resampled),
+ `willDoReadInputBlock` answering for the device's own direction, and the
+ macOS plugin's input control set: data source `'imic'` "Microphone", mute
+ and volume in the input scope, built the way the speaker's are.
+
+## 6. Reveal and validation
+
+Measured on `mictest-ipad` (iPad16,1, iPadOS 26.6.2), recording in Voice
+Memos:
+
+1. Plugin: `vpquery.log` after `=== plugin load ===` lists both streams'
+ `PCM_INFO`, `stream 0: input, 48000 Hz, 2 channels, 32-bit float, period
+ 32768 bytes, lead 2 period(s)`, `device Digital Mic: 48000 Hz nominal …`,
+ `microphone controls added: dsrc 'imic', mute, volume`, `published 2
+ virtio sound device(s)`.
+2. HAL: `HALS_Device::Activate: activating device 37: Digital Mic`, and the
+ input-scope queries arrive (`'stm#'`, `'saft'`, `'ltnc'`, `'dflt'` on
+ `'inpt'`).
+3. VirtualAudio: `VA Init Status: 0`, `Adding port [ type: pmbi; uid:
+ Built-In Microphone; … rout: 1 ]`, no `No match found for internal
+ sub-port ID`.
+4. Route: `cfw update-environment` logs `SP-gate playbackAndRecord handler
+ 0xe92c0 … 0xec370; log block 0xea588; gate 0xea274`; Record then leaves no
+ `HAL Speaker Protection is missing` and no `Route change failed`, and
+ `vpquery.log` reads `stream 1: format set to 48000 Hz through the device`.
+5. The kernel serves selector 9: `stream 0: first read back: result 0x0,
+ argument 0x0`. The completion carries no byte count, which is why the
+ macOS plugin counts a period.
+6. Sound arrives. A 41.7 s recording, decoded on the host: a noise floor
+ near -47 dBFS and events up to -16 dBFS RMS, 0.19 s of zeros at its start
+ (the lead). Its stop lines:
+
+ ```
+ stream 1: 41.88 s, 492 writes, 0 starved, 0 bytes dropped, in 2009280 frames (47980/s, hal 48000), out 2009280 frames (47980/s)
+ stream 0: 41.88 s, 490 reads, in 2007040 frames (47926/s), out 2000320 frames (47765/s), 8960 silent, 0 bytes skipped
+ ```
+
+ `silent` is the lead (8192 frames) and the reads before the first
+ period; nothing after.
+7. A second recording, minutes later (the device set up again): `8.42 s, 98
+ reads, in … (47665/s) … 8960 silent`.
+8. A recording started 0.34 s after the last stopped, while its reads were
+ still out: `6.22 s, 73 reads, in 299008 frames (48072/s) … 6720 silent`,
+ and no `reads still out, releasing` line. audiomxd left no crash report.
+9. Speaker unchanged: Safari playing for 47.47 s leaves `557 writes, 0
+ starved, 0 bytes dropped, in … (44096/s, hal 44100)`. The same test while
+ another VM on the host was using seven cores read `25 starved`; a busy
+ host shows up there, on either side of this change.
+
+The first recording after the VM starts is different, both times it was
+measured. Its first run ended after 2.9 s (Voice Memos reconfigures once)
+with the first read back 1.9 s and 2.8 s after the start, `16 reads, in
+65536 frames (22427/s)` and `4 reads`, and on the speaker `394240` and
+`752640 bytes dropped`: the host held both directions while it opened the
+microphone. Later starts have no such pause.
+
+Not done:
+
+* Whether the Mac showed its microphone prompt for VPhone, with the usage
+ description, was not observed (the recording had sound, so access was
+ granted one way or the other).
+* An iPhone guest (`mictest-iphone`, iPhone99,11 / iOS 27.0) gets as far as
+ the route and no further. Both devices are published, `cfw install` opens
+ the gate there (`0x10e468` → `0x10e6f0`), and Voice Memos' Record builds
+ its route: `RoutingHandler_Record_N51.cpp:125 Activating route: [ …
+ Category: 'cpar'; Mode: 'spcp' … Device Type: vdfi; { source: 'pmbi';
+ iPhone Microphone; Built-In Microphone … } ]`, aggregate `VAD [vdfi]
+ AggDev` on `Digital Mic`, DSP chain `flexible_video_recording`. No I/O
+ starts, because the app gives up first. On iOS 27 Voice Memos records
+ through an `AVCaptureSession`, and cameracaptured has no capture source to
+ give it:
+
+ ```
+ cameracaptured: BWFigCaptureDeviceVendor _createDevice:…: Cannot create device without create function!
+ cameracaptured: FigCaptureSourceBackingsProvider …: Error -12786 while creating FigCaptureDevice. Wiping com.apple.cameracapture.volatile …
+ cameracaptured: cs_getBackingsForBuiltInCameras: 0 total in-memory backings
+ VoiceMemos: -[VMAudioService _onQueueStartNewRecordingWithRecorder:uuid:completion:]_block_invoke -- Recording failed -- error: Error Domain=VoiceMemos.CaptureSessionRecorder.CaptureSessionRecorderError Code=1
+ ```
+
+ That is the capture stack's device discovery on a VM, not the sound
+ plugin. No sound has been recorded on an iPhone guest: nothing run there
+ records without `AVCaptureSession`.
+
+## 7. Open
+
+* Whether the kernel returns reads still out at RELEASE is not known. The
+ plugin does not depend on it in the normal stop; the one-second fallback
+ does, and was not reached.
+* A session at 44100 that both plays and records sets each stream's format
+ in the aggregate, and the microphone device answers 48000 only, so that
+ set is refused and the route would fail as it did before §4's change.
+ Voice Memos records at 48000. If an app needs it, the reader needs the
+ speaker path's resampler, turned around.
+* `ssrc` on the input device (§4) is refused. Nothing depended on it.
+* The 1.9 s pause at the first capture (§6) is the host's. If it matters,
+ the lead queued on the speaker side is what would cover it.
+* Only the built-in microphone port is published. Speech detection (`vspd`)
+ builds its aggregate on it at launch and does not start I/O; if something
+ in the guest starts listening on its own, the host microphone opens with
+ it, which the Mac's microphone indicator shows. Dictation asks for consent
+ first and was not enabled here.
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/ExecutablePatches/VirtualAudio/CustomFirmwareVirtualAudio.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/ExecutablePatches/VirtualAudio/CustomFirmwareVirtualAudio.swift
index 69470654..69063019 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/ExecutablePatches/VirtualAudio/CustomFirmwareVirtualAudio.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/ExecutablePatches/VirtualAudio/CustomFirmwareVirtualAudio.swift
@@ -91,6 +91,13 @@
// orphaned, and leaving the gate's branch intact is what lets the anchor
// find the site again on an already-patched binary.
//
+// RoutingHandler_PlaybackAndRecord_GenericConfig1 holds the same gate in the
+// same shape, and a route that plays and records ('cpar', what a recording
+// app asks for) is declined by it once the guest has a microphone port to
+// build that route with. It is the patch's second site, found by that
+// handler's own file name. Its volume-mode test is a plain branch between
+// two ways of building the route, not a precondition, and needs nothing.
+//
// The volume-mode precondition
// ----------------------------
// The gate opened; the route survives its own construction and dies one
@@ -163,6 +170,32 @@ public enum CustomFirmwareVirtualAudio {
/// the one the ringtone ('crnp') reconfiguration route runs through.
public static let spGateFile = "RoutingHandler_Playback_GenericConfig1.cpp"
+ /// The handlers whose SP gate the patch opens. A route that plays and
+ /// records ('cpar', what a recording app asks for) runs through the
+ /// second, which declines it for the same missing capability once a
+ /// microphone port makes the route buildable at all.
+ public enum SPGateHandler: String, Sendable, CaseIterable {
+ case playback
+ case playbackAndRecord
+
+ /// The handler's source-file string, which its log blocks reference.
+ public var file: String {
+ switch self {
+ case .playback: spGateFile
+ case .playbackAndRecord: "RoutingHandler_PlaybackAndRecord_GenericConfig1.cpp"
+ }
+ }
+
+ /// The record this handler's write emits: the declaration's own
+ /// identifier, or a site of it.
+ public var patchID: String {
+ switch self {
+ case .playback: spGatePatchID
+ case .playbackAndRecord: spGatePatchID + ".playback_and_record"
+ }
+ }
+ }
+
/// Record identity for the SP-gate patch.
public static let spGatePatchID = "system-virtualaudio-cfw-speaker_protection_gate"
@@ -527,6 +560,7 @@ public enum CustomFirmwareVirtualAudio {
@discardableResult
public static func patchSpeakerProtectionGate(
fileAt url: URL,
+ handler: SPGateHandler = .playback,
reattest: Bool = true,
dryRun: Bool = false,
log: ((String) -> Void)? = stdoutLog,
@@ -536,17 +570,20 @@ public enum CustomFirmwareVirtualAudio {
}
var data = try Data(contentsOfFileToRewrite: url)
let before = data
- let report = try patchSpeakerProtectionGate(&data, reattest: reattest, dryRun: dryRun, log: log)
+ let report = try patchSpeakerProtectionGate(
+ &data, handler: handler, reattest: reattest, dryRun: dryRun, log: log,
+ )
if data != before {
try data.write(to: url)
}
return report
}
- /// In-memory form of ``patchSpeakerProtectionGate(fileAt:reattest:dryRun:log:)``.
+ /// In-memory form of ``patchSpeakerProtectionGate(fileAt:handler:reattest:dryRun:log:)``.
@discardableResult
public static func patchSpeakerProtectionGate(
_ data: inout Data,
+ handler: SPGateHandler = .playback,
reattest: Bool = true,
dryRun: Bool = false,
log: ((String) -> Void)? = stdoutLog,
@@ -555,12 +592,12 @@ public enum CustomFirmwareVirtualAudio {
data = Data(data)
}
- let located = try locateSPGate(in: data)
+ let located = try locateSPGate(in: data, handler: handler)
let head = located.blockHead
let headVMA = located.vma(head)
let site = located.site
let siteVMA = located.vma(site)
- log?(" [.] SP-gate handler 0x\(hex(located.functionStartVMA)) … 0x\(hex(located.functionEndVMA))"
+ log?(" [.] SP-gate \(handler.rawValue) handler 0x\(hex(located.functionStartVMA)) … 0x\(hex(located.functionEndVMA))"
+ "; log block 0x\(hex(headVMA)); gate 0x\(hex(siteVMA))")
let anchor = SPGateAnchor(
branchFileOffset: site,
@@ -619,7 +656,7 @@ public enum CustomFirmwareVirtualAudio {
outcome: .patched,
anchor: anchor,
records: [PatchRecord(
- patchID: spGatePatchID,
+ patchID: handler.patchID,
component: component,
fileOffset: head,
virtualAddress: headVMA,
@@ -1059,7 +1096,7 @@ public enum CustomFirmwareVirtualAudio {
/// the format alone names three routing handlers, and of the functions
/// holding a reference to it, exactly one — ours — also references the
/// GenericConfig1 file name.
- static func locatePlaybackHandler(in data: Data) throws -> PlaybackHandler {
+ static func locatePlaybackHandler(in data: Data, file: String = spGateFile) throws -> PlaybackHandler {
let sections = MachOParser.parseSections(from: data)
guard let text = sections["__TEXT,__text"] else {
throw PatcherError.invalidFormat("\(component): no __TEXT,__text section")
@@ -1074,10 +1111,10 @@ public enum CustomFirmwareVirtualAudio {
"\(component): the string \"\(spGateMessage)\" is nowhere in the binary's sections",
)
}
- let fileNames = cStringVMAs(containing: spGateFile, in: data, sections: sections)
+ let fileNames = cStringVMAs(containing: file, in: data, sections: sections)
guard !fileNames.isEmpty else {
throw PatcherError.patchSiteNotFound(
- "\(component): the string \"\(spGateFile)\" is nowhere in the binary's sections",
+ "\(component): the string \"\(file)\" is nowhere in the binary's sections",
)
}
@@ -1113,7 +1150,7 @@ public enum CustomFirmwareVirtualAudio {
}
guard qualified.count == 1, let handler = qualified.first else {
throw PatcherError.patchSiteNotFound(
- "\(component): expected exactly 1 \"\(spGateFile)\" handler referencing "
+ "\(component): expected exactly 1 \"\(file)\" handler referencing "
+ "\"\(spGateMessage)\", found \(qualified.count)",
)
}
@@ -1129,8 +1166,8 @@ public enum CustomFirmwareVirtualAudio {
}
/// Resolve the SP-gate branch, or say why not.
- static func locateSPGate(in data: Data) throws -> SPGatePlan {
- let handler = try locatePlaybackHandler(in: data)
+ static func locateSPGate(in data: Data, handler which: SPGateHandler = .playback) throws -> SPGatePlan {
+ let handler = try locatePlaybackHandler(in: data, file: which.file)
func vma(_ offset: Int) -> UInt64 { handler.vma(offset) }
func offset(of address: UInt64) -> Int { handler.textStart + Int(address - handler.textAddress) }
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swift
index 65a38e03..1fb5ff60 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareGuestSystemPatchSet.swift
@@ -222,7 +222,8 @@ public enum FirmwareGuestSystemPatchSet {
of HAL Speaker Protection — a capability only a physical codec reports, whose \
query never leaves VirtualAudio. The decline leaves the vdef playing into \
Null_Device, silent with the volume dead; with the gate opened, the route keeps \
- the virtio device it already built.
+ the virtio device it already built. The handler for routes that play and record \
+ has the same gate, and declines every recording app's route; it is opened too.
""",
target: .guestExecutable(path: "/Library/Audio/Plug-Ins/HAL/VirtualAudio.plugin/VirtualAudio"),
),
@@ -304,7 +305,8 @@ public enum FirmwareGuestSystemPatchSet {
/// The branch that declines a finished route for lack of HAL Speaker
/// Protection, opened so the ringtone route keeps the virtio device it
- /// already built instead of falling back to Null_Device.
+ /// already built instead of falling back to Null_Device, and so a route
+ /// that also records is not refused.
public static let virtualAudioSpeakerProtectionGate = "system-virtualaudio-cfw-speaker_protection_gate"
/// The precondition that declines a finished route whose software-volume
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareVirtualAudioTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareVirtualAudioTests.swift
index e84b7d85..71cb1f27 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareVirtualAudioTests.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareVirtualAudioTests.swift
@@ -82,6 +82,37 @@ struct CustomFirmwareVirtualAudioTests {
#expect(CustomFirmwareVirtualAudio.spGatePatchID == "system-virtualaudio-cfw-speaker_protection_gate")
}
+ @Test("each SP-gate handler has its own file anchor and a record the declaration covers")
+ func spGateHandlersAreDistinct() {
+ typealias Handler = CustomFirmwareVirtualAudio.SPGateHandler
+ #expect(Handler.allCases == [.playback, .playbackAndRecord])
+ #expect(Handler.playback.file == CustomFirmwareVirtualAudio.spGateFile)
+ #expect(Handler.playback.patchID == CustomFirmwareVirtualAudio.spGatePatchID)
+ #expect(Handler.playbackAndRecord.file == "RoutingHandler_PlaybackAndRecord_GenericConfig1.cpp")
+ // A site of the same declaration: only a dot starts one.
+ #expect(Handler.playbackAndRecord.patchID
+ == "system-virtualaudio-cfw-speaker_protection_gate.playback_and_record")
+ // Neither file name may be found inside the other, or one handler's
+ // anchor would qualify both.
+ #expect(!Handler.playbackAndRecord.file.contains(Handler.playback.file))
+ #expect(!Handler.playback.file.contains(Handler.playbackAndRecord.file))
+ }
+
+ @Test("a binary without the play-and-record decline is refused, not searched past",
+ arguments: [Self.junk, Data()])
+ func refusesBinariesWithoutTheRecordSPGateSite(_ input: Data) throws {
+ var data = input
+ #expect(throws: PatcherError.self) {
+ try CustomFirmwareVirtualAudio.patchSpeakerProtectionGate(
+ &data,
+ handler: .playbackAndRecord,
+ reattest: false,
+ dryRun: true,
+ log: nil,
+ )
+ }
+ }
+
@Test("a binary without the precondition decline is refused, not searched past",
arguments: [Self.junk, Data()])
func refusesBinariesWithoutTheVolumeGateSite(_ input: Data) throws {
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareMachOVerbsCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareMachOVerbsCommand.swift
index b21a1429..7f3e6b84 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareMachOVerbsCommand.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareMachOVerbsCommand.swift
@@ -378,6 +378,11 @@ struct VPhoneCustomFirmwarePatchVirtualAudioSPGateCommand: ParsableCommand {
serves three routing handlers, ours among them by that file string
alone. No address is literal.
+ The handler for routes that play and record ('cpar', what a
+ recording app asks for) holds the same gate in the same shape and
+ declines the same way once the guest has a microphone port, so it
+ is opened too, as a second site of the same patch.
+
Idempotent: a site already holding the branch is reported and left
alone, byte for byte.
""",
@@ -392,12 +397,15 @@ struct VPhoneCustomFirmwarePatchVirtualAudioSPGateCommand: ParsableCommand {
func run() throws {
try requireUntruncatedMachO(at: binary)
// reattest: false — the caller re-signs.
- try CustomFirmwareVirtualAudio.patchSpeakerProtectionGate(
- fileAt: binary,
- reattest: false,
- dryRun: dryRun,
- log: machOVerbLog,
- )
+ for handler in CustomFirmwareVirtualAudio.SPGateHandler.allCases {
+ try CustomFirmwareVirtualAudio.patchSpeakerProtectionGate(
+ fileAt: binary,
+ handler: handler,
+ reattest: false,
+ dryRun: dryRun,
+ log: machOVerbLog,
+ )
+ }
}
}
diff --git a/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh b/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh
index 3163f377..4dfd36c0 100755
--- a/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh
+++ b/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh
@@ -104,6 +104,10 @@ fi
/usr/bin/xcrun xcstringstool compile \
"$root/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings" \
--output-directory "$resources"
+# The microphone prompt names this bundle, and reads its text from here.
+/usr/bin/xcrun xcstringstool compile \
+ "$root/VPhoneExecutable/VPhoneVirtualization/Resources/InfoPlist.xcstrings" \
+ --output-directory "$resources"
# locationd ignores a client inside a generic bundle, so vphone-vm reads the
# host location through this app. Its InfoPlist.strings translate the
diff --git a/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh b/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh
index cd1eb286..7b71b23a 100755
--- a/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh
+++ b/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh
@@ -57,6 +57,19 @@ for language in en zh-Hans ja ko vi; do
exit 1
}
done
+# The host microphone is opened by vphone-vm itself, so macOS prompts for this
+# bundle and refuses the microphone to one with no usage description.
+/usr/libexec/PlistBuddy -c 'Print :NSMicrophoneUsageDescription' "$bundle/Contents/Info.plist" >/dev/null 2>&1 || {
+ print -u2 "VPhone.bundle has no microphone usage description"
+ exit 1
+}
+for language in en zh-Hans ja ko vi; do
+ strings="$resources/$language.lproj/InfoPlist.strings"
+ /usr/libexec/PlistBuddy -c 'Print :NSMicrophoneUsageDescription' "$strings" >/dev/null 2>&1 || {
+ print -u2 "Missing microphone prompt text in VPhone.bundle: $language"
+ exit 1
+ }
+done
# The patch API ships its interface so an out-of-tree patch set can build against
# the same framework the bundle loads.
[[ -d "$frameworks/VPhonePatchKit.framework/Versions/A/Modules/VPhonePatchKit.swiftmodule" ]] || {
diff --git a/VPhoneExecutable/VPhoneVirtualization/Resources/Info.plist b/VPhoneExecutable/VPhoneVirtualization/Resources/Info.plist
index 978b9fb9..ab893329 100644
--- a/VPhoneExecutable/VPhoneVirtualization/Resources/Info.plist
+++ b/VPhoneExecutable/VPhoneVirtualization/Resources/Info.plist
@@ -12,5 +12,7 @@
<string>27</string>
<key>CFBundleShortVersionString</key>
<string>2.4.0</string>
+ <key>NSMicrophoneUsageDescription</key>
+ <string>Shares this Mac's microphone with the virtual iPhone so apps running inside it can record.</string>
</dict>
</plist>
diff --git a/VPhoneExecutable/VPhoneVirtualization/Resources/InfoPlist.xcstrings b/VPhoneExecutable/VPhoneVirtualization/Resources/InfoPlist.xcstrings
index 65d175b8..89c9e79d 100644
--- a/VPhoneExecutable/VPhoneVirtualization/Resources/InfoPlist.xcstrings
+++ b/VPhoneExecutable/VPhoneVirtualization/Resources/InfoPlist.xcstrings
@@ -102,7 +102,42 @@
}
}
}
+ },
+ "NSMicrophoneUsageDescription": {
+ "localizations": {
+ "en": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Shares this Mac's microphone with the virtual iPhone so apps running inside it can record."
+ }
+ },
+ "ja": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "この Mac のマイクを仮想 iPhone と共有し、その中のアプリで録音できるようにします。"
+ }
+ },
+ "ko": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "가상 iPhone과 이 Mac의 마이크를 공유하여 그 안에서 실행되는 앱이 녹음할 수 있도록 합니다."
+ }
+ },
+ "vi": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Chia sẻ micrô của máy Mac này với iPhone ảo để các ứng dụng bên trong có thể ghi âm."
+ }
+ },
+ "zh-Hans": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "将这台 Mac 的麦克风共享给虚拟 iPhone,使其中运行的应用能够录音。"
+ }
+ }
+ }
}
+
},
"version": "1.3"
}
diff --git a/VPhoneGuestComponents/Makefile b/VPhoneGuestComponents/Makefile
index 70e257ae..4a37810a 100644
--- a/VPhoneGuestComponents/Makefile
+++ b/VPhoneGuestComponents/Makefile
@@ -114,7 +114,8 @@ test-injection-environment:
-o $(OUT)/InjectionEnvironmentTests Tests/InjectionEnvironmentTests.c
@$(OUT)/InjectionEnvironmentTests
-# Host proof harness for the virtio-snd format choice, output ring and clock.
+# Host proof harness for the virtio-snd format choice, the rings, the input
+# reader and the clock.
test-virtiosound:
@mkdir -p $(OUT)
xcrun --sdk macosx clang -O1 -Wall -Wextra -Werror -IVirtIOSound \
diff --git a/VPhoneGuestComponents/README.md b/VPhoneGuestComponents/README.md
index 1f805d6b..ff2920fd 100644
--- a/VPhoneGuestComponents/README.md
+++ b/VPhoneGuestComponents/README.md
@@ -43,5 +43,7 @@ validation and the hook installation prerequisites.
The virtio sound plugin is the CoreAudio half of the VM's virtio-snd device,
which iOS ships no driver for. audiomxd loads it when the kernel publishes
-`AppleVirtIOSound`. `make test-virtiosound` checks its format choice and output
-ring on the host. See `Research/Guest/virtio_sound.md`.
+`AppleVirtIOSound`, and it publishes a speaker and a microphone device.
+`make test-virtiosound` checks its format choice, its rings and how captured
+frames are served, on the host. See `Research/Guest/virtio_sound.md` and
+`Research/Guest/virtio_sound_microphone.md`.
diff --git a/VPhoneGuestComponents/Tests/VirtIOSoundTests.c b/VPhoneGuestComponents/Tests/VirtIOSoundTests.c
index 535a2ecc..7fac2e8e 100644
--- a/VPhoneGuestComponents/Tests/VirtIOSoundTests.c
+++ b/VPhoneGuestComponents/Tests/VirtIOSoundTests.c
@@ -1,5 +1,6 @@
// VirtIOSoundTests.c — host checks for the virtio-snd HAL plugin's pure parts:
-// the format the plugin asks the device for, and the output ring's counters.
+// the format the plugin asks the device for, the rings' counters, and how
+// captured frames are served.
#include <stdio.h>
#include <stdlib.h>
@@ -232,6 +233,201 @@ static void testRingResetAndValidation(void) {
VPVirtIOSoundRingDestroy(&ring);
}
+// MARK: - Input ring
+
+/// Hand the device the next slot and return it filled with `value`, as one
+/// completed read does.
+static void capturePeriod(VPVirtIOSoundInputRing *ring, uint8_t value) {
+ uint32_t offset = 0;
+ CHECK(VPVirtIOSoundInputRingNextSubmission(ring, &offset));
+ VPVirtIOSoundInputRingDidSubmit(ring);
+ fill(ring->bytes + offset, ring->period, value);
+ VPVirtIOSoundInputRingDidComplete(ring);
+}
+
+static void testInputRingHandsOutWholeSlots(void) {
+ VPVirtIOSoundInputRing ring;
+ CHECK(!VPVirtIOSoundInputRingInit(&ring, 100, 64));
+ CHECK(!VPVirtIOSoundInputRingInit(&ring, 128, 0));
+ CHECK(VPVirtIOSoundInputRingInit(&ring, 4 * 64, 64));
+ uint32_t offset = 99;
+ // Four slots, in order, then nothing: a slot in flight is not free.
+ for (uint32_t slot = 0; slot < 4; slot++) {
+ CHECK(VPVirtIOSoundInputRingNextSubmission(&ring, &offset));
+ CHECK(offset == slot * 64);
+ VPVirtIOSoundInputRingDidSubmit(&ring);
+ }
+ CHECK(!VPVirtIOSoundInputRingNextSubmission(&ring, &offset));
+ CHECK(VPVirtIOSoundInputRingInFlight(&ring) == 256);
+ CHECK(VPVirtIOSoundInputRingAvailable(&ring) == 0);
+ // Completion frees nothing either: the frames are not read yet.
+ VPVirtIOSoundInputRingDidComplete(&ring);
+ CHECK(VPVirtIOSoundInputRingInFlight(&ring) == 192);
+ CHECK(VPVirtIOSoundInputRingAvailable(&ring) == 64);
+ CHECK(!VPVirtIOSoundInputRingNextSubmission(&ring, &offset));
+ // Only a whole period read frees a slot, and it is the first one again.
+ uint8_t frames[64];
+ CHECK(VPVirtIOSoundInputRingRead(&ring, frames, 32));
+ CHECK(!VPVirtIOSoundInputRingNextSubmission(&ring, &offset));
+ CHECK(VPVirtIOSoundInputRingRead(&ring, frames, 32));
+ CHECK(VPVirtIOSoundInputRingNextSubmission(&ring, &offset));
+ CHECK(offset == 0);
+ VPVirtIOSoundInputRingDestroy(&ring);
+}
+
+static void testInputRingReadsInOrderAcrossTheEnd(void) {
+ VPVirtIOSoundInputRing ring;
+ CHECK(VPVirtIOSoundInputRingInit(&ring, 2 * 64, 64));
+ uint8_t frames[96];
+ capturePeriod(&ring, 1);
+ // All or none: 65 bytes are not there yet.
+ CHECK(!VPVirtIOSoundInputRingRead(&ring, frames, 65));
+ CHECK(VPVirtIOSoundInputRingAvailable(&ring) == 64);
+ CHECK(VPVirtIOSoundInputRingRead(&ring, frames, 32));
+ capturePeriod(&ring, 2);
+ CHECK(VPVirtIOSoundInputRingRead(&ring, frames, 64));
+ CHECK(frames[0] == 1 && frames[31] == 1 && frames[32] == 2 && frames[63] == 2);
+ // The third period lands in the first slot; a read from 96 wraps into it.
+ capturePeriod(&ring, 3);
+ CHECK(VPVirtIOSoundInputRingRead(&ring, frames, 96));
+ CHECK(frames[0] == 2 && frames[31] == 2 && frames[32] == 3 && frames[95] == 3);
+ CHECK(VPVirtIOSoundInputRingAvailable(&ring) == 0);
+
+ CHECK(!VPVirtIOSoundInputRingSkip(&ring, 1));
+ capturePeriod(&ring, 4);
+ CHECK(VPVirtIOSoundInputRingSkip(&ring, 60));
+ CHECK(VPVirtIOSoundInputRingRead(&ring, frames, 4));
+ CHECK(frames[0] == 4);
+
+ VPVirtIOSoundInputRingReset(&ring);
+ CHECK(VPVirtIOSoundInputRingAvailable(&ring) == 0);
+ CHECK(VPVirtIOSoundInputRingInFlight(&ring) == 0);
+ VPVirtIOSoundInputRingDestroy(&ring);
+}
+
+// MARK: - Input reader
+
+static bool isAll(const uint8_t *bytes, uint32_t length, uint8_t value) {
+ for (uint32_t i = 0; i < length; i++) {
+ if (bytes[i] != value) {
+ return false;
+ }
+ }
+ return true;
+}
+
+/// Eight-byte frames, 64-byte periods, a two-period lead, and at most four
+/// periods buffered.
+static VPVirtIOSoundInputReader reader(VPVirtIOSoundInputRing *ring) {
+ VPVirtIOSoundInputReader value;
+ memset(&value, 0, sizeof(value));
+ value.ring = ring;
+ value.bytesPerFrame = 8;
+ value.leadBytes = 2 * 64;
+ value.maximumBacklogBytes = 4 * 64;
+ return value;
+}
+
+static void testReaderWaitsForTheLead(void) {
+ VPVirtIOSoundInputRing ring;
+ CHECK(VPVirtIOSoundInputRingInit(&ring, 12 * 64, 64));
+ VPVirtIOSoundInputReader input = reader(&ring);
+ uint8_t frames[32];
+
+ // Nothing captured, then one period: silence, and nothing consumed.
+ fill(frames, sizeof(frames), 0xff);
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ CHECK(isAll(frames, 32, 0));
+ capturePeriod(&ring, 1);
+ fill(frames, sizeof(frames), 0xff);
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ CHECK(isAll(frames, 32, 0));
+ CHECK(VPVirtIOSoundInputRingAvailable(&ring) == 64);
+
+ // The second period is the lead: reads are served, oldest first.
+ capturePeriod(&ring, 2);
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ CHECK(isAll(frames, 32, 1));
+ CHECK(input.servedFrames == 4 && input.silentFrames == 8);
+ // And stay served below the lead, down to the last frame.
+ for (int i = 0; i < 3; i++) {
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ }
+ CHECK(isAll(frames, 32, 2));
+ CHECK(VPVirtIOSoundInputRingAvailable(&ring) == 0);
+
+ // Running dry is silence, and the lead is waited for again.
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ CHECK(isAll(frames, 32, 0));
+ capturePeriod(&ring, 3);
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ CHECK(isAll(frames, 32, 0));
+ capturePeriod(&ring, 4);
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ CHECK(isAll(frames, 32, 3));
+ VPVirtIOSoundInputRingDestroy(&ring);
+}
+
+static void testReaderBoundsTheBacklog(void) {
+ VPVirtIOSoundInputRing ring;
+ CHECK(VPVirtIOSoundInputRingInit(&ring, 12 * 64, 64));
+ VPVirtIOSoundInputReader input = reader(&ring);
+ uint8_t frames[32];
+ // Four periods buffered are allowed and read from the oldest.
+ for (uint8_t period = 1; period <= 4; period++) {
+ capturePeriod(&ring, period);
+ }
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ CHECK(isAll(frames, 32, 1));
+ CHECK(input.skippedBytes == 0);
+ // Two more make 352 bytes: over the limit, so all but the lead goes and
+ // the read continues from what is left.
+ capturePeriod(&ring, 5);
+ capturePeriod(&ring, 6);
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ CHECK(input.skippedBytes == 352 - 128);
+ CHECK(isAll(frames, 32, 5));
+ CHECK(VPVirtIOSoundInputRingAvailable(&ring) == 128 - 32);
+ VPVirtIOSoundInputRingDestroy(&ring);
+}
+
+static void testReaderDropsWhatTheLastRunLeft(void) {
+ VPVirtIOSoundInputRing ring;
+ CHECK(VPVirtIOSoundInputRingInit(&ring, 12 * 64, 64));
+ VPVirtIOSoundInputReader input = reader(&ring);
+ uint8_t frames[32];
+ capturePeriod(&ring, 1);
+ capturePeriod(&ring, 2);
+ capturePeriod(&ring, 3);
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ CHECK(isAll(frames, 32, 1));
+
+ // A new start: the 160 bytes still buffered are old, and the lead is
+ // waited for from scratch.
+ input.restart = true;
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ CHECK(isAll(frames, 32, 0));
+ CHECK(VPVirtIOSoundInputRingAvailable(&ring) == 0);
+ capturePeriod(&ring, 4);
+ capturePeriod(&ring, 5);
+ VPVirtIOSoundInputReaderRead(&input, frames, 4);
+ CHECK(isAll(frames, 32, 4));
+
+ // A read larger than the lead is served once that much is there.
+ uint8_t large[192];
+ input.restart = true;
+ capturePeriod(&ring, 6);
+ capturePeriod(&ring, 7);
+ VPVirtIOSoundInputReaderRead(&input, large, 24);
+ CHECK(isAll(large, 192, 0));
+ capturePeriod(&ring, 8);
+ capturePeriod(&ring, 9);
+ capturePeriod(&ring, 10);
+ VPVirtIOSoundInputReaderRead(&input, large, 24);
+ CHECK(isAll(large, 64, 8) && isAll(large + 64, 64, 9) && isAll(large + 128, 64, 10));
+ VPVirtIOSoundInputRingDestroy(&ring);
+}
+
int main(void) {
testPrefersFloat48k();
testFallsBackToInteger();
@@ -243,6 +439,11 @@ int main(void) {
testRingWrapsWritesAndSplitsSubmissions();
testRingQueuesSilenceAhead();
testRingResetAndValidation();
+ testInputRingHandsOutWholeSlots();
+ testInputRingReadsInOrderAcrossTheEnd();
+ testReaderWaitsForTheLead();
+ testReaderBoundsTheBacklog();
+ testReaderDropsWhatTheLastRunLeft();
if (failures) {
fprintf(stderr, "VirtIOSoundTests: %d failure(s)\n", failures);
return 1;
diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundAudioServerDriver.h b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundAudioServerDriver.h
index 5a608946..d1397360 100644
--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundAudioServerDriver.h
+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundAudioServerDriver.h
@@ -38,12 +38,15 @@ enum {
kAudioHardwareNoError = 0,
kAudioHardwareUnspecifiedError = 'what',
kAudioObjectPropertyScopeGlobal = 'glob',
+ kAudioObjectPropertyScopeInput = 'inpt',
kAudioObjectPropertyScopeOutput = 'outp',
kAudioObjectPropertyElementMain = 0,
kAudioDeviceTransportTypeUSB = 'usb ',
kAudioVolumeControlClassID = 'vlme',
kAudioMuteControlClassID = 'mute',
kAudioDataSourceControlClassID = 'dsrc',
+ kAudioStreamPropertyVirtualFormat = 'sfmt',
+ kAudioStreamPropertyPhysicalFormat = 'pft ',
};
#endif
@@ -71,6 +74,7 @@ typedef struct AudioServerPlugInIOCycleInfo {
/// `ASDStreamDirection`: the HAL scope four-character codes.
typedef UInt32 ASDStreamDirection;
static const ASDStreamDirection ASDStreamDirectionOutput = kAudioObjectPropertyScopeOutput;
+static const ASDStreamDirection ASDStreamDirectionInput = kAudioObjectPropertyScopeInput;
@class ASDPlugin;
@@ -141,6 +145,7 @@ typedef int (^ASDIOBlock)(
@property (nonatomic) UInt32 timestampPeriod;
@property (nonatomic) UInt32 inputSafetyOffset;
@property (nonatomic) UInt32 outputSafetyOffset;
+@property (nonatomic) UInt32 inputLatency;
@property (nonatomic) UInt32 outputLatency;
@property (nonatomic) UInt32 transportType;
@property (readonly, nonatomic) BOOL hasOutput;
@@ -151,6 +156,7 @@ typedef int (^ASDIOBlock)(
@property (copy, nonatomic) ASDGetZeroTimestampBlock getZeroTimestampBlock;
@property (copy, nonatomic) ASDWillDoBlock willDoReadInputBlock;
@property (copy, nonatomic) ASDWillDoBlock willDoWriteMixBlock;
+- (void)addInputStream:(id)stream;
- (void)addOutputStream:(id)stream;
- (NSArray *)outputStreams;
- (int)performStartIO;
@@ -243,6 +249,7 @@ typedef int (^ASDIOBlock)(
@property (copy, nonatomic) ASDStreamFormat *physicalFormat;
@property (copy, nonatomic) NSArray<ASDStreamFormat *> *physicalFormats;
@property (nonatomic) BOOL physicalFormatSettable;
+@property (copy, nonatomic) ASDIOBlock readInputBlock;
@property (copy, nonatomic) ASDIOBlock writeMixBlock;
/// Sent to every stream when the device's nominal sample rate changed; the
/// default implementation reconciles the physical format with the new rate.
diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m
index 7531aaf4..484a97a1 100644
--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m
+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m
@@ -2,26 +2,33 @@
//
// audiomxd loads this bundle from /System/Library/Audio/Plug-Ins/HAL when the
// kernel has published `AppleVirtIOSound` (Info.plist's loading condition).
-// One HAL device is created per `AppleVirtIOSound` service, with one output
-// stream per virtio-snd output stream. Input streams are left out: nothing
-// in the guest needs the host microphone yet, and leaving them out keeps
-// the host from opening it.
+// Each `AppleVirtIOSound` service becomes two HAL devices, a speaker with the
+// service's output streams and a microphone with its input streams, because
+// VirtualAudio builds a port from a device's UID and one UID names one kind
+// of port. The host opens its microphone only while the guest records: the
+// virtio input stream is started when CoreAudio starts the microphone device
+// and released when it stops.
//
-// The device clock (VPVirtIOSoundClock.h) is free running, anchored to
+// Each device clock (VPVirtIOSoundClock.h) is free running, anchored to
// mach_absolute_time when I/O starts, as the macOS plugin's is. The mixed
// output goes into a ring (see VPVirtIOSoundRing.h) and a timer hands it to
// the kernel a period at a time with the async write selector; the virtio
// device paces playback on the host.
+// Input runs the other way without a timer: a few periods of the input ring
+// are always with the kernel, and each one that comes back full is replaced.
//
// Optional settings in the `com.apple.coreaudio` preference domain, read
// once when audiomxd loads the plugin:
-// VPhoneVirtIOSoundTransportType four-character transport ("usb ", "bltn")
-// VPhoneVirtIOSoundDeviceUID HAL device UID (default "PuffinOutput" on
-// an iPad or iPhone guest,
-// "VPhoneVirtIOSound:0" elsewhere)
-// VPhoneVirtIOSoundNominalRate 44100 to start at the alternate rate
-// VPhoneVirtIOSoundLeadPeriods periods of silence queued ahead of the
-// mix at each start, 0 to 8
+// VPhoneVirtIOSoundTransportType four-character transport ("usb ", "bltn")
+// VPhoneVirtIOSoundDeviceUID speaker device UID (default "PuffinOutput"
+// on an iPad or iPhone guest,
+// "VPhoneVirtIOSound:0" elsewhere)
+// VPhoneVirtIOSoundInputDeviceUID microphone device UID (default "Digital
+// Mic" on an iPad or iPhone guest,
+// "VPhoneVirtIOSoundInput:0" elsewhere)
+// VPhoneVirtIOSoundNominalRate 44100 to start at the alternate rate
+// VPhoneVirtIOSoundLeadPeriods periods of silence queued ahead of the
+// mix at each start, 0 to 8
#import "VPVirtIOSoundAudioServerDriver.h"
@@ -59,12 +66,24 @@ static const UInt32 kSafetyOffsetFrames = 100;
/// plugin flushes, a little under half a period.
static const uint64_t kFlushIntervalNanoseconds = NSEC_PER_SEC / 24;
static const uint32_t kAsyncReferenceCount = 3;
+/// Periods of the input ring the kernel holds at a time, as the macOS plugin
+/// keeps (`kInputInitialBuffers`).
+static const uint32_t kInputReadsInFlight = 4;
+/// Periods captured before reads are served (VPVirtIOSoundRing.h, the input
+/// reader), and the most that may be buffered before the oldest are dropped.
+static const uint32_t kInputLeadPeriods = 2;
+static const uint32_t kInputMaximumBacklogPeriods = 4;
+/// How long a stopped input stream waits for its reads to come back before
+/// it releases the device with them still out.
+static const uint64_t kInputDrainNanoseconds = NSEC_PER_SEC;
static CFStringRef const kSettingsDomain = CFSTR("com.apple.coreaudio");
-/// The internal-speaker data source the macOS plugin selects for its output
-/// ('ispk'; no SDK constant carries it).
+/// The data sources the macOS plugin selects: the internal speaker for its
+/// output and the internal microphone for its input ('ispk', 'imic'; no SDK
+/// constant carries them).
static const UInt32 kVPDataSourceInternalSpeaker = 0x6973706b;
+static const UInt32 kVPDataSourceInternalMicrophone = 0x696d6963;
/// `kAudioDevicePropertyMute` ('mute'), which the trimmed iPhoneOS CoreAudio
/// headers leave out (AudioHardwareBase.h stops at the server-side set).
@@ -221,22 +240,28 @@ static int VPGuestProductID(void) {
return strncmp(machine, "iPad", 4) == 0 || strncmp(machine, "iPhone", 6) == 0 ? 8010 : 0;
}
-static NSString *VPDeviceUID(unsigned index) {
- NSString *uid = CFBridgingRelease(
- CFPreferencesCopyAppValue(CFSTR("VPhoneVirtIOSoundDeviceUID"), kSettingsDomain));
+static NSString *VPDeviceUID(ASDStreamDirection direction, unsigned index) {
+ BOOL input = direction == ASDStreamDirectionInput;
+ NSString *uid = CFBridgingRelease(CFPreferencesCopyAppValue(
+ input ? CFSTR("VPhoneVirtIOSoundInputDeviceUID") : CFSTR("VPhoneVirtIOSoundDeviceUID"),
+ kSettingsDomain));
if ([uid isKindOfClass:NSString.class] && uid.length > 0) {
return index == 0 ? uid : [NSString stringWithFormat:@"%@:%u", uid, index];
}
- // VirtualAudio builds a physical device, and a speaker port, only for the
- // UIDs its device factory knows. "PuffinOutput" is the built-in output of
+ // VirtualAudio builds a physical device, and a port, only for the UIDs
+ // its device factory knows. "PuffinOutput" is the built-in output of
// Apple-silicon host audio, and the one such UID that publishes a routable
- // speaker; under any name of our own the device is claimed by nothing.
+ // speaker ('pspk'). Its twin "PuffinInput" publishes the built-in
+ // microphone ('pmbi') too, but bare: record routes ask the port for the
+ // product's microphone sub-ports ('btm1', 'vcrg') and throw when it has
+ // none. "Digital Mic" is the codec microphone VirtualAudio builds those
+ // sub-ports for. Under any name of our own a device is claimed by nothing.
// Only where the route is set up: a speaker route on a VirtualAudio that
// was not deadlocked audiomxd and crash-looped it across launches.
if (index == 0 && VPGuestProductID() != 0) {
- return @"PuffinOutput";
+ return input ? @"Digital Mic" : @"PuffinOutput";
}
- return [NSString stringWithFormat:@"VPhoneVirtIOSound:%u", index];
+ return [NSString stringWithFormat:@"VPhoneVirtIOSound%s:%u", input ? "Input" : "", index];
}
/// Gives VirtualAudio `VPGuestProductID()` through its own defaults key,
@@ -444,57 +469,264 @@ static int VPMixWrite(VPMixState *mix, void *buffer, UInt32 frameCount) {
typedef NS_ENUM(NSInteger, VPStreamState) {
/// The virtio stream is released; SET_PARAMS comes first.
VPStreamStateIdle,
- /// Started on the device, and CoreAudio is writing.
+ /// Started on the device, and CoreAudio is doing I/O.
VPStreamStateRunning,
/// CoreAudio stopped; the device stops once what it holds comes back.
VPStreamStateDraining,
+ /// Input only: the device was released with reads still out, and the
+ /// stream waits for them before it can be set up again.
+ VPStreamStateReleasing,
};
-@interface VPVirtIOSoundStream : ASDStream
-- (instancetype)initWithConnection:(io_connect_t)connection
- streamID:(uint32_t)streamID
- format:(VPVirtIOSoundStreamFormat)format
- plugin:(ASDPlugin *)plugin;
-/// Frames, at the wire rate, between a write and the host playing it: the
-/// lead queued ahead plus the period a write waits to fill.
-@property (nonatomic, readonly) UInt32 queuedLatencyFrames;
-- (void)transferCompletedWithLength:(uint32_t)length result:(IOReturn)result;
-@end
-
-/// The refcon of one async write.
+/// The refcon of one async transfer: the stream, and the transfer's bytes in
+/// its ring.
typedef struct {
void *stream;
+ uint32_t offset;
uint32_t length;
} VPTransfer;
+/// What the output and input streams share: the virtio stream's identity and
+/// format, the serial queue its device commands and completions run on, and
+/// the property inventory.
+@interface VPVirtIOSoundStream : ASDStream {
+@protected
+ io_connect_t _connection;
+ ASDStreamDirection _direction;
+ uint32_t _streamID;
+ VPVirtIOSoundStreamFormat _format;
+ uint32_t _periodBytes;
+ uint32_t _bufferBytes;
+ dispatch_queue_t _queue;
+ IONotificationPortRef _port;
+ VPStreamState _state;
+ BOOL _reportedTransferError;
+ /// When CoreAudio last started the stream, for the counters' rates.
+ uint64_t _startedAt;
+}
+- (instancetype)initWithConnection:(io_connect_t)connection
+ streamID:(uint32_t)streamID
+ format:(VPVirtIOSoundStreamFormat)format
+ direction:(ASDStreamDirection)direction
+ plugin:(ASDPlugin *)plugin;
+/// Frames, at the wire rate, between the guest's I/O and the host's.
+@property (nonatomic, readonly) UInt32 queuedLatencyFrames;
+/// The device the stream belongs to, which is where its rate is changed.
+@property (weak, nonatomic) ASDAudioDevice *device;
+@end
+
+@implementation VPVirtIOSoundStream
+
+- (instancetype)initWithConnection:(io_connect_t)connection
+ streamID:(uint32_t)streamID
+ format:(VPVirtIOSoundStreamFormat)format
+ direction:(ASDStreamDirection)direction
+ plugin:(ASDPlugin *)plugin {
+ self = [super initWithDirection:direction withPlugin:plugin];
+ if (!self) {
+ return nil;
+ }
+ _connection = connection;
+ _direction = direction;
+ _streamID = streamID;
+ _format = format;
+ VPVirtIOSoundBufferSizes(&format, (uint32_t)getpagesize(), &_periodBytes, &_bufferBytes);
+ _queue = dispatch_queue_create("com.vphone.audio.virtiosound.stream", DISPATCH_QUEUE_SERIAL);
+ _port = IONotificationPortCreate(kIOMainPortDefault);
+ if (!_port) {
+ return nil;
+ }
+ IONotificationPortSetDispatchQueue(_port, _queue);
+ ASDStreamFormat *physical = [self physicalFormatAtRate:format.sampleRate];
+ self.streamName = direction == ASDStreamDirectionInput ? @"Input Stream" : @"Output Stream";
+ self.physicalFormat = physical;
+ self.physicalFormats = @[physical];
+ return self;
+}
+
+- (void)dealloc {
+ if (_port) {
+ IONotificationPortDestroy(_port);
+ }
+}
+
+/// The stream's format as CoreAudio is told it, at one fixed rate.
+- (ASDStreamFormat *)physicalFormatAtRate:(double)rate {
+ AudioStreamBasicDescription description = {
+ .mSampleRate = rate,
+ .mFormatID = kAudioFormatLinearPCM,
+ .mFormatFlags = (_format.isFloat ? kAudioFormatFlagIsFloat : kAudioFormatFlagIsSignedInteger)
+ | kAudioFormatFlagIsPacked,
+ .mBytesPerPacket = _format.bytesPerFrame,
+ .mFramesPerPacket = 1,
+ .mBytesPerFrame = _format.bytesPerFrame,
+ .mChannelsPerFrame = _format.channels,
+ .mBitsPerChannel = _format.bitsPerChannel,
+ };
+ ASDStreamFormat *format = [[ASDStreamFormat alloc] initWithAudioStreamBasicDescription:description];
+ format.minimumSampleRate = rate;
+ format.maximumSampleRate = rate;
+ return format;
+}
+
+- (void)logFormatWithLeadPeriods:(uint32_t)leadPeriods {
+ const char *direction = _direction == ASDStreamDirectionInput ? "input" : "output";
+ os_log(VPLog(), "stream %u: %{public}s, %.0f Hz, %u channels, %u-bit %{public}s, period %u bytes",
+ _streamID, direction, _format.sampleRate, _format.channels, _format.bitsPerChannel,
+ _format.isFloat ? "float" : "integer", _periodBytes);
+ VPLogToFile("stream %u: %s, %.0f Hz, %u channels, %u-bit %s, period %u bytes, lead %u period(s)",
+ _streamID, direction, _format.sampleRate, _format.channels, _format.bitsPerChannel,
+ _format.isFloat ? "float" : "integer", _periodBytes, leadPeriods);
+}
+
+- (UInt32)queuedLatencyFrames {
+ return 0;
+}
+
+// MARK: Property inventory
+
+// The same five entry points as the device's below, logging what the stream
+// object itself is asked (its physical formats and layouts mostly) before
+// ASDStream answers.
+
+- (BOOL)hasProperty:(AudioObjectPropertyAddress *)address {
+ VPLogSelectorQuery("stream-has", address);
+ return [super hasProperty:address];
+}
+
+- (BOOL)isPropertySettable:(AudioObjectPropertyAddress *)address {
+ VPLogSelectorQuery("stream-settable", address);
+ return [super isPropertySettable:address];
+}
+
+- (UInt32)dataSizeForProperty:(AudioObjectPropertyAddress *)address
+ withQualifierSize:(UInt32)qualifierSize
+ andQualifierData:(const void *)qualifierData {
+ VPLogSelectorQuery("stream-size", address);
+ return [super dataSizeForProperty:address withQualifierSize:qualifierSize
+ andQualifierData:qualifierData];
+}
+
+- (BOOL)getProperty:(AudioObjectPropertyAddress *)address
+ withQualifierSize:(UInt32)qualifierSize
+ qualifierData:(const void *)qualifierData
+ dataSize:(UInt32 *)dataSize
+ andData:(void *)data
+ forClient:(UInt32)clientID {
+ VPLogSelectorQuery("stream-get", address);
+ return [super getProperty:address withQualifierSize:qualifierSize
+ qualifierData:qualifierData dataSize:dataSize
+ andData:data forClient:clientID];
+}
+
+- (BOOL)setProperty:(AudioObjectPropertyAddress *)address
+ withQualifierSize:(UInt32)qualifierSize
+ qualifierData:(const void *)qualifierData
+ dataSize:(UInt32)dataSize
+ andData:(const void *)data
+ forClient:(UInt32)clientID {
+ VPLogSelectorQuery("stream-set", address);
+ if ([super setProperty:address withQualifierSize:qualifierSize
+ qualifierData:qualifierData dataSize:dataSize
+ andData:data forClient:clientID]) {
+ return YES;
+ }
+ // A route that plays and records puts both devices in one aggregate and
+ // sets each stream's format there, where a playback route sets the
+ // device's nominal rate. ASDStream refuses a plugin stream's format the
+ // way ASDAudioDevice refuses its rate (see the device's `setProperty:`):
+ // the HAL reports 'what' and the route fails with "failed to set the new
+ // format on the aggregate device". The formats of a stream differ only
+ // in rate, so the change is the device's rate change, which reaches
+ // every stream.
+ if ((address->mSelector == kAudioStreamPropertyPhysicalFormat
+ || address->mSelector == kAudioStreamPropertyVirtualFormat)
+ && dataSize >= sizeof(AudioStreamBasicDescription) && data != NULL) {
+ double rate = ((const AudioStreamBasicDescription *)data)->mSampleRate;
+ ASDAudioDevice *device = self.device;
+ if (rate > 0 && [device supportsSamplingRate:rate]) {
+ VPLogToFile("stream %u: format set to %.0f Hz through the device", _streamID, rate);
+ device.samplingRate = rate;
+ return YES;
+ }
+ }
+ return NO;
+}
+
+// MARK: Device commands
+
+- (kern_return_t)callSelector:(uint32_t)selector {
+ uint64_t scalar = _streamID;
+ kern_return_t result = IOConnectCallScalarMethod(_connection, selector, &scalar, 1, NULL, NULL);
+ if (result != KERN_SUCCESS) {
+ os_log_error(VPLog(), "stream %u: selector %u failed: 0x%x", _streamID, selector, result);
+ }
+ return result;
+}
+
+/// SET_PARAMS and PREPARE: the device stream is set up and waits for START.
+- (BOOL)prepareDevice {
+ dispatch_assert_queue(_queue);
+ VPVirtIOSoundPCMParameters parameters = VPVirtIOSoundParameters(&_format, _periodBytes, _bufferBytes);
+ uint64_t scalar = _streamID;
+ kern_return_t result = IOConnectCallMethod(_connection, kVPVirtIOSoundSelectorSetParameters,
+ &scalar, 1, &parameters, sizeof(parameters), NULL, NULL, NULL, NULL);
+ if (result != KERN_SUCCESS) {
+ os_log_error(VPLog(), "stream %u: SET_PARAMS failed: 0x%x", _streamID, result);
+ return NO;
+ }
+ if ([self callSelector:kVPVirtIOSoundSelectorPrepare] != KERN_SUCCESS) {
+ [self callSelector:kVPVirtIOSoundSelectorRelease];
+ return NO;
+ }
+ _reportedTransferError = NO;
+ return YES;
+}
+
+- (void)releaseDevice {
+ dispatch_assert_queue(_queue);
+ [self callSelector:kVPVirtIOSoundSelectorStop];
+ [self callSelector:kVPVirtIOSoundSelectorRelease];
+}
+
+- (void)reportTransferError:(IOReturn)result length:(uint32_t)length {
+ if (result != kIOReturnSuccess && !_reportedTransferError) {
+ _reportedTransferError = YES;
+ os_log_error(VPLog(), "stream %u: transfer of %u bytes failed: 0x%x", _streamID, length, result);
+ VPLogToFile("stream %u: transfer of %u bytes failed: 0x%x", _streamID, length, result);
+ }
+}
+
+@end
+
+// MARK: - Output stream
+
+@interface VPVirtIOSoundOutputStream : VPVirtIOSoundStream
+- (instancetype)initWithConnection:(io_connect_t)connection
+ streamID:(uint32_t)streamID
+ format:(VPVirtIOSoundStreamFormat)format
+ plugin:(ASDPlugin *)plugin;
+- (void)transferCompletedWithLength:(uint32_t)length result:(IOReturn)result;
+@end
+
static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UInt32 argumentCount) {
(void)arguments;
(void)argumentCount;
VPTransfer *transfer = refcon;
- VPVirtIOSoundStream *stream = (__bridge VPVirtIOSoundStream *)transfer->stream;
+ VPVirtIOSoundOutputStream *stream = (__bridge VPVirtIOSoundOutputStream *)transfer->stream;
uint32_t length = transfer->length;
free(transfer);
[stream transferCompletedWithLength:length result:result];
}
-@implementation VPVirtIOSoundStream {
- io_connect_t _connection;
- uint32_t _streamID;
- VPVirtIOSoundStreamFormat _format;
- uint32_t _periodBytes;
- uint32_t _bufferBytes;
+@implementation VPVirtIOSoundOutputStream {
VPVirtIOSoundRing _ring;
- dispatch_queue_t _queue;
- IONotificationPortRef _port;
dispatch_source_t _timer;
- VPStreamState _state;
- BOOL _reportedTransferError;
/// Periods of silence queued ahead of the mix at each device start.
uint32_t _leadPeriods;
- /// Since the last start: when it was, what the device still held then,
- /// writes handed to the device, and how many of them found it with
- /// nothing left to play.
- uint64_t _startedAt;
+ /// Since the last start: what the device still held then, writes handed
+ /// to the device, and how many of them found it with nothing left to
+ /// play.
uint64_t _inFlightAtStart;
uint64_t _submissions;
uint64_t _starved;
@@ -539,53 +771,24 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
}
- (instancetype)initWithConnection:(io_connect_t)connection
- streamID:(uint32_t)streamID
- format:(VPVirtIOSoundStreamFormat)format
- plugin:(ASDPlugin *)plugin {
- self = [super initWithDirection:ASDStreamDirectionOutput withPlugin:plugin];
+ streamID:(uint32_t)streamID
+ format:(VPVirtIOSoundStreamFormat)format
+ plugin:(ASDPlugin *)plugin {
+ self = [super initWithConnection:connection streamID:streamID format:format
+ direction:ASDStreamDirectionOutput plugin:plugin];
if (!self) {
return nil;
}
- _connection = connection;
- _streamID = streamID;
- _format = format;
- // Returning nil from here releases self, so `dealloc` runs on whatever
- // was set up so far: it owns every release, and these branches only
- // return.
- VPVirtIOSoundBufferSizes(&format, (uint32_t)getpagesize(), &_periodBytes, &_bufferBytes);
if (!VPVirtIOSoundRingInit(&_ring, _bufferBytes, _periodBytes)) {
os_log_error(VPLog(), "stream %u: cannot allocate a %u-byte ring", streamID, _bufferBytes);
return nil;
}
- _queue = dispatch_queue_create("com.vphone.audio.virtiosound.stream", DISPATCH_QUEUE_SERIAL);
- _port = IONotificationPortCreate(kIOMainPortDefault);
- if (!_port) {
- return nil;
- }
- IONotificationPortSetDispatchQueue(_port, _queue);
_leadPeriods = VPLeadPeriods();
_resampleScratch = malloc(kResampleScratchFrames * format.bytesPerFrame);
if (!_resampleScratch) {
return nil;
}
- AudioStreamBasicDescription description = {
- .mSampleRate = format.sampleRate,
- .mFormatID = kAudioFormatLinearPCM,
- .mFormatFlags = (format.isFloat ? kAudioFormatFlagIsFloat : kAudioFormatFlagIsSignedInteger)
- | kAudioFormatFlagIsPacked,
- .mBytesPerPacket = format.bytesPerFrame,
- .mFramesPerPacket = 1,
- .mBytesPerFrame = format.bytesPerFrame,
- .mChannelsPerFrame = format.channels,
- .mBitsPerChannel = format.bitsPerChannel,
- };
- ASDStreamFormat *physical = [[ASDStreamFormat alloc] initWithAudioStreamBasicDescription:description];
- physical.minimumSampleRate = format.sampleRate;
- physical.maximumSampleRate = format.sampleRate;
- self.streamName = @"Output Stream";
- self.physicalFormat = physical;
- self.physicalFormats = @[physical];
double nominalRate = 0;
// The 44100 twin: a ringtone-preview aggregate runs the vdef at 44100,
// and the aggregate only carries devices that answer that rate. The
@@ -593,12 +796,8 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
// current format follows the nominal-rate override so the device boots
// already answering 44100, matching how the vdef composes.
if (format.sampleRate == 48000.0) {
- AudioStreamBasicDescription alternate = description;
- alternate.mSampleRate = kVPAlternateRate;
- ASDStreamFormat *reduced = [[ASDStreamFormat alloc] initWithAudioStreamBasicDescription:alternate];
- reduced.minimumSampleRate = kVPAlternateRate;
- reduced.maximumSampleRate = kVPAlternateRate;
- self.physicalFormats = @[physical, reduced];
+ ASDStreamFormat *reduced = [self physicalFormatAtRate:kVPAlternateRate];
+ self.physicalFormats = @[self.physicalFormat, reduced];
self.physicalFormatSettable = YES;
double override = VPNominalRateOverride();
if (override == kVPAlternateRate) {
@@ -618,25 +817,14 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
.wireRate = (uint32_t)format.sampleRate,
};
[self installMixBlock];
- os_log(VPLog(), "stream %u: %.0f Hz, %u channels, %u-bit %{public}s, period %u bytes",
- streamID, format.sampleRate, format.channels, format.bitsPerChannel,
- format.isFloat ? "float" : "integer", _periodBytes);
- VPLogToFile("stream %u: %.0f Hz, %u channels, %u-bit %s, period %u bytes, lead %u period(s)",
- streamID, format.sampleRate, format.channels, format.bitsPerChannel,
- format.isFloat ? "float" : "integer", _periodBytes, _leadPeriods);
+ [self logFormatWithLeadPeriods:_leadPeriods];
return self;
}
-/// Also the cleanup of a failed init, so it takes any prefix of it: ivars
-/// start zeroed, `free(NULL)` is a no-op, and a ring that `RingInit` refused
-/// or never saw is zeroed, which `RingDestroy` frees as NULL.
- (void)dealloc {
if (_timer) {
dispatch_source_cancel(_timer);
}
- if (_port) {
- IONotificationPortDestroy(_port);
- }
free(_resampleScratch);
VPVirtIOSoundRingDestroy(&_ring);
}
@@ -658,85 +846,18 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
}
}
-// MARK: Property inventory
-
-// The same five entry points as the device's below, logging what the stream
-// object itself is asked (its physical formats and layouts mostly) before
-// ASDStream answers.
-
-- (BOOL)hasProperty:(AudioObjectPropertyAddress *)address {
- VPLogSelectorQuery("stream-has", address);
- return [super hasProperty:address];
-}
-
-- (BOOL)isPropertySettable:(AudioObjectPropertyAddress *)address {
- VPLogSelectorQuery("stream-settable", address);
- return [super isPropertySettable:address];
-}
-
-- (UInt32)dataSizeForProperty:(AudioObjectPropertyAddress *)address
- withQualifierSize:(UInt32)qualifierSize
- andQualifierData:(const void *)qualifierData {
- VPLogSelectorQuery("stream-size", address);
- return [super dataSizeForProperty:address withQualifierSize:qualifierSize
- andQualifierData:qualifierData];
-}
-
-- (BOOL)getProperty:(AudioObjectPropertyAddress *)address
- withQualifierSize:(UInt32)qualifierSize
- qualifierData:(const void *)qualifierData
- dataSize:(UInt32 *)dataSize
- andData:(void *)data
- forClient:(UInt32)clientID {
- VPLogSelectorQuery("stream-get", address);
- return [super getProperty:address withQualifierSize:qualifierSize
- qualifierData:qualifierData dataSize:dataSize
- andData:data forClient:clientID];
-}
-
-- (BOOL)setProperty:(AudioObjectPropertyAddress *)address
- withQualifierSize:(UInt32)qualifierSize
- qualifierData:(const void *)qualifierData
- dataSize:(UInt32)dataSize
- andData:(const void *)data
- forClient:(UInt32)clientID {
- VPLogSelectorQuery("stream-set", address);
- return [super setProperty:address withQualifierSize:qualifierSize
- qualifierData:qualifierData dataSize:dataSize
- andData:data forClient:clientID];
-}
-
// MARK: Device commands
-- (kern_return_t)callSelector:(uint32_t)selector {
- uint64_t scalar = _streamID;
- kern_return_t result = IOConnectCallScalarMethod(_connection, selector, &scalar, 1, NULL, NULL);
- if (result != KERN_SUCCESS) {
- os_log_error(VPLog(), "stream %u: selector %u failed: 0x%x", _streamID, selector, result);
- }
- return result;
-}
-
- (BOOL)startDevice {
dispatch_assert_queue(_queue);
VPVirtIOSoundRingReset(&_ring);
- VPVirtIOSoundPCMParameters parameters = VPVirtIOSoundParameters(&_format, _periodBytes, _bufferBytes);
- uint64_t scalar = _streamID;
- kern_return_t result = IOConnectCallMethod(_connection, kVPVirtIOSoundSelectorSetParameters,
- &scalar, 1, &parameters, sizeof(parameters), NULL, NULL, NULL, NULL);
- if (result != KERN_SUCCESS) {
- os_log_error(VPLog(), "stream %u: SET_PARAMS failed: 0x%x", _streamID, result);
- return NO;
- }
- if ([self callSelector:kVPVirtIOSoundSelectorPrepare] != KERN_SUCCESS) {
- [self callSelector:kVPVirtIOSoundSelectorRelease];
+ if (![self prepareDevice]) {
return NO;
}
if ([self callSelector:kVPVirtIOSoundSelectorStart] != KERN_SUCCESS) {
[self callSelector:kVPVirtIOSoundSelectorRelease];
return NO;
}
- _reportedTransferError = NO;
return YES;
}
@@ -846,9 +967,7 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
}
- (void)stopDevice {
- dispatch_assert_queue(_queue);
- [self callSelector:kVPVirtIOSoundSelectorStop];
- [self callSelector:kVPVirtIOSoundSelectorRelease];
+ [self releaseDevice];
_state = VPStreamStateIdle;
}
@@ -865,6 +984,7 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
return;
}
transfer->stream = (__bridge void *)self;
+ transfer->offset = offset;
transfer->length = length;
uint64_t reference[kOSAsyncRef64Count] = {0};
reference[kIOAsyncCalloutFuncIndex] = (uint64_t)(uintptr_t)VPWriteCompleted;
@@ -912,10 +1032,7 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
_completedAfterFirst += length;
}
}
- if (result != kIOReturnSuccess && !_reportedTransferError) {
- _reportedTransferError = YES;
- os_log_error(VPLog(), "stream %u: write of %u bytes failed: 0x%x", _streamID, length, result);
- }
+ [self reportTransferError:result length:length];
if (_state == VPStreamStateDraining && VPVirtIOSoundRingInFlight(&_ring) == 0) {
[self stopDevice];
}
@@ -938,7 +1055,7 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _queue);
dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, kFlushIntervalNanoseconds),
kFlushIntervalNanoseconds, kFlushIntervalNanoseconds / 10);
- __unsafe_unretained VPVirtIOSoundStream *unretained = self;
+ __unsafe_unretained VPVirtIOSoundOutputStream *unretained = self;
dispatch_source_set_event_handler(_timer, ^{
if (unretained->_state == VPStreamStateRunning) {
[unretained submitPending:NO];
@@ -970,16 +1087,369 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
@end
+// MARK: - Input stream
+
+@interface VPVirtIOSoundInputStream : VPVirtIOSoundStream
+- (instancetype)initWithConnection:(io_connect_t)connection
+ streamID:(uint32_t)streamID
+ format:(VPVirtIOSoundStreamFormat)format
+ plugin:(ASDPlugin *)plugin;
+- (void)readCompletedAtOffset:(uint32_t)offset result:(IOReturn)result argument:(uint64_t)argument;
+@end
+
+/// IOKit calls an async completion with as many arguments as the kernel
+/// sent, so with one this is `IOAsyncCallback1`. The macOS plugin's block
+/// receives the same pair and reads neither; the argument is only logged
+/// here, once.
+static void VPReadCompleted(void *refcon, IOReturn result, void *argument) {
+ VPTransfer *transfer = refcon;
+ VPVirtIOSoundInputStream *stream = (__bridge VPVirtIOSoundInputStream *)transfer->stream;
+ uint32_t offset = transfer->offset;
+ free(transfer);
+ [stream readCompletedAtOffset:offset result:result argument:(uint64_t)(uintptr_t)argument];
+}
+
+@implementation VPVirtIOSoundInputStream {
+ VPVirtIOSoundInputRing _ring;
+ VPVirtIOSoundInputReader _reader;
+ /// Counts stops, so a drain deadline knows the stop it was set for.
+ uint64_t _stops;
+ /// CoreAudio started the stream while it was `Releasing`.
+ BOOL _startWhenReleased;
+ BOOL _refillScheduled;
+ BOOL _loggedFirstRead;
+ /// Reads the device returned since CoreAudio last started the stream.
+ uint64_t _completions;
+}
+
+/// ASDStream releases this block at every stop, as it does the output
+/// stream's (`installMixBlock`), so it goes back in before every start.
+- (void)installReadBlock {
+ VPVirtIOSoundInputReader *reader = &_reader;
+ self.readInputBlock = ^int(UInt32 frameCount, const AudioServerPlugInIOCycleInfo *cycleInfo,
+ void *mainBuffer, void *secondaryBuffer, UInt32 clientID) {
+ (void)cycleInfo;
+ (void)secondaryBuffer;
+ (void)clientID;
+ VPVirtIOSoundInputReaderRead(reader, mainBuffer, frameCount);
+ return kAudioHardwareNoError;
+ };
+}
+
+- (instancetype)initWithConnection:(io_connect_t)connection
+ streamID:(uint32_t)streamID
+ format:(VPVirtIOSoundStreamFormat)format
+ plugin:(ASDPlugin *)plugin {
+ self = [super initWithConnection:connection streamID:streamID format:format
+ direction:ASDStreamDirectionInput plugin:plugin];
+ if (!self) {
+ return nil;
+ }
+ if (!VPVirtIOSoundInputRingInit(&_ring, _bufferBytes, _periodBytes)) {
+ os_log_error(VPLog(), "stream %u: cannot allocate a %u-byte ring", streamID, _bufferBytes);
+ return nil;
+ }
+ _reader = (VPVirtIOSoundInputReader){
+ .ring = &_ring,
+ .bytesPerFrame = format.bytesPerFrame,
+ .leadBytes = kInputLeadPeriods * _periodBytes,
+ .maximumBacklogBytes = kInputMaximumBacklogPeriods * _periodBytes,
+ };
+ [self installReadBlock];
+ [self logFormatWithLeadPeriods:kInputLeadPeriods];
+ return self;
+}
+
+- (void)dealloc {
+ VPVirtIOSoundInputRingDestroy(&_ring);
+}
+
+/// What the reader keeps between the host's capture and the guest's read.
+- (UInt32)queuedLatencyFrames {
+ return kInputLeadPeriods * (_periodBytes / _format.bytesPerFrame);
+}
+
+// MARK: Device commands
+
+/// The macOS plugin's order: the reads go to the device between PREPARE and
+/// START, so capture has somewhere to land from its first frame.
+- (BOOL)startDevice {
+ dispatch_assert_queue(_queue);
+ VPVirtIOSoundInputRingReset(&_ring);
+ if (![self prepareDevice]) {
+ return NO;
+ }
+ _state = VPStreamStateRunning;
+ [self submitReads];
+ if ([self callSelector:kVPVirtIOSoundSelectorStart] != KERN_SUCCESS) {
+ [self stopDevice];
+ return NO;
+ }
+ return YES;
+}
+
+/// STOP and RELEASE. Reads still out keep their ring slots until the kernel
+/// returns them, so the stream is not `Idle` before then.
+- (void)stopDevice {
+ [self releaseDevice];
+ _state = VPVirtIOSoundInputRingInFlight(&_ring) == 0 ? VPStreamStateIdle : VPStreamStateReleasing;
+}
+
+/// One line per run, the input twin of the output stream's: `in` is what
+/// the host captured, `out` what CoreAudio read of it, `silent` the frames
+/// it was handed as silence instead (the lead at each start, then gaps), and
+/// `skipped` what was dropped to bound the backlog.
+- (void)logCounters {
+ double seconds = (double)(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - _startedAt) / 1e9;
+ uint64_t framesIn = _completions * (_periodBytes / _format.bytesPerFrame);
+ uint64_t framesOut = atomic_load(&_reader.servedFrames);
+ VPLogToFile("stream %u: %.2f s, %llu reads, in %llu frames (%.0f/s), out %llu frames (%.0f/s), "
+ "%llu silent, %llu bytes skipped",
+ _streamID, seconds, (unsigned long long)_completions,
+ (unsigned long long)framesIn, seconds > 0 ? framesIn / seconds : 0,
+ (unsigned long long)framesOut, seconds > 0 ? framesOut / seconds : 0,
+ (unsigned long long)atomic_load(&_reader.silentFrames),
+ (unsigned long long)atomic_load(&_reader.skippedBytes));
+}
+
+// MARK: Transfers
+
+/// Keeps `kInputReadsInFlight` periods with the device. A full ring leaves
+/// fewer out, and a failed read is not replaced at once, so either way the
+/// stream looks again a period later instead of waiting for a completion
+/// that may not come.
+- (void)submitReads {
+ dispatch_assert_queue(_queue);
+ mach_port_t wakePort = IONotificationPortGetMachPort(_port);
+ uint32_t offset = 0;
+ while (VPVirtIOSoundInputRingInFlight(&_ring) < kInputReadsInFlight * _periodBytes
+ && VPVirtIOSoundInputRingNextSubmission(&_ring, &offset)) {
+ VPTransfer *transfer = malloc(sizeof(*transfer));
+ if (!transfer) {
+ break;
+ }
+ transfer->stream = (__bridge void *)self;
+ transfer->offset = offset;
+ transfer->length = _periodBytes;
+ uint64_t reference[kOSAsyncRef64Count] = {0};
+ reference[kIOAsyncCalloutFuncIndex] = (uint64_t)(uintptr_t)VPReadCompleted;
+ reference[kIOAsyncCalloutRefconIndex] = (uint64_t)(uintptr_t)transfer;
+ uint64_t scalar = _streamID;
+ size_t size = _periodBytes;
+ VPVirtIOSoundInputRingDidSubmit(&_ring);
+ kern_return_t result = IOConnectCallAsyncMethod(_connection, kVPVirtIOSoundSelectorRead, wakePort,
+ reference, kAsyncReferenceCount, &scalar, 1, NULL, 0, NULL, NULL, _ring.bytes + offset, &size);
+ if (result != KERN_SUCCESS) {
+ // Nothing will complete this one, so return its slot now.
+ free(transfer);
+ [self readCompletedAtOffset:offset result:result argument:0];
+ return;
+ }
+ }
+ if (VPVirtIOSoundInputRingInFlight(&_ring) < kInputReadsInFlight * _periodBytes) {
+ [self scheduleRefill];
+ }
+}
+
+- (void)scheduleRefill {
+ if (_refillScheduled) {
+ return;
+ }
+ _refillScheduled = YES;
+ uint64_t period = NSEC_PER_SEC * (_periodBytes / _format.bytesPerFrame) / (uint64_t)_format.sampleRate;
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)period), _queue, ^{
+ self->_refillScheduled = NO;
+ if (self->_state == VPStreamStateRunning) {
+ [self submitReads];
+ }
+ });
+}
+
+- (void)readCompletedAtOffset:(uint32_t)offset result:(IOReturn)result argument:(uint64_t)argument {
+ dispatch_assert_queue(_queue);
+ if (!_loggedFirstRead) {
+ _loggedFirstRead = YES;
+ VPLogToFile("stream %u: first read back: result 0x%x, argument 0x%llx",
+ _streamID, result, (unsigned long long)argument);
+ }
+ if (result != kIOReturnSuccess) {
+ // Whatever the slot holds is not this period's capture.
+ memset(_ring.bytes + offset, 0, _periodBytes);
+ [self reportTransferError:result length:_periodBytes];
+ }
+ VPVirtIOSoundInputRingDidComplete(&_ring);
+ _completions++;
+ BOOL returned = VPVirtIOSoundInputRingInFlight(&_ring) == 0;
+ switch (_state) {
+ case VPStreamStateRunning:
+ if (result == kIOReturnSuccess) {
+ [self submitReads];
+ } else {
+ [self scheduleRefill];
+ }
+ break;
+ case VPStreamStateDraining:
+ if (returned) {
+ [self stopDevice];
+ }
+ break;
+ case VPStreamStateReleasing:
+ if (returned) {
+ _state = VPStreamStateIdle;
+ if (_startWhenReleased) {
+ _startWhenReleased = NO;
+ [self startDevice];
+ }
+ }
+ break;
+ case VPStreamStateIdle:
+ break;
+ }
+}
+
+// MARK: ASDStream
+
+- (void)startStream {
+ dispatch_sync(_queue, ^{
+ self->_completions = 0;
+ atomic_store(&self->_reader.servedFrames, 0);
+ atomic_store(&self->_reader.silentFrames, 0);
+ atomic_store(&self->_reader.skippedBytes, 0);
+ self->_startedAt = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
+ // What an earlier run left in the ring is old by now.
+ atomic_store(&self->_reader.restart, true);
+ switch (self->_state) {
+ case VPStreamStateIdle:
+ [self startDevice];
+ break;
+ case VPStreamStateDraining:
+ // Still started on the device: capture carries on.
+ self->_state = VPStreamStateRunning;
+ [self submitReads];
+ break;
+ case VPStreamStateReleasing:
+ self->_startWhenReleased = YES;
+ break;
+ case VPStreamStateRunning:
+ break;
+ }
+ });
+ [self installReadBlock];
+ [super startStream];
+}
+
+/// The reads still out come back as the host fills them, a period apart, and
+/// the device is released after the last. Releasing it first would leave the
+/// kernel holding ring slots with nothing said about when it returns them;
+/// that is the fallback, for a host that has stopped filling them.
+- (void)stopStream {
+ dispatch_sync(_queue, ^{
+ self->_startWhenReleased = NO;
+ if (self->_state != VPStreamStateRunning) {
+ return;
+ }
+ [self logCounters];
+ if (VPVirtIOSoundInputRingInFlight(&self->_ring) == 0) {
+ [self stopDevice];
+ return;
+ }
+ self->_state = VPStreamStateDraining;
+ uint64_t stop = ++self->_stops;
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)kInputDrainNanoseconds), self->_queue, ^{
+ if (self->_state == VPStreamStateDraining && self->_stops == stop) {
+ VPLogToFile("stream %u: %llu bytes of reads still out, releasing",
+ self->_streamID, (unsigned long long)VPVirtIOSoundInputRingInFlight(&self->_ring));
+ [self stopDevice];
+ }
+ });
+ });
+ [super stopStream];
+}
+
+@end
+
// MARK: - Device
+/// One open user client of an `AppleVirtIOSound` service. The service's
+/// speaker and microphone devices share it, as the output and input streams
+/// of the macOS plugin's one device do; each stream still talks to the
+/// kernel from its own queue.
+@interface VPVirtIOSoundConnection : NSObject
+- (instancetype)initWithService:(io_service_t)service;
+@property (nonatomic, readonly) io_connect_t port;
+@property (nonatomic, readonly) uint32_t streamCount;
+/// PCM_INFO for one virtio stream.
+- (BOOL)getInfo:(VPVirtIOSoundPCMInfo *)info forStream:(uint32_t)streamID;
+@end
+
+@implementation VPVirtIOSoundConnection
+
+- (instancetype)initWithService:(io_service_t)service {
+ self = [super init];
+ if (!self) {
+ return nil;
+ }
+ kern_return_t result = IOServiceOpen(service, mach_task_self(), 0, &_port);
+ if (result != KERN_SUCCESS) {
+ os_log_error(VPLog(), "cannot open AppleVirtIOSound: 0x%x", result);
+ return nil;
+ }
+ CFTypeRef value = IORegistryEntryCreateCFProperty(
+ service, CFSTR(kVPVirtIOSoundStreamCountKey), kCFAllocatorDefault, 0);
+ if (value && CFGetTypeID(value) == CFNumberGetTypeID()) {
+ CFNumberGetValue(value, kCFNumberSInt32Type, &_streamCount);
+ }
+ if (value) {
+ CFRelease(value);
+ }
+ // What the device offers, for the record: the format choice below and
+ // the split into two devices both follow from these.
+ for (uint32_t streamID = 0; streamID < _streamCount; streamID++) {
+ VPVirtIOSoundPCMInfo info;
+ if ([self getInfo:&info forStream:streamID]) {
+ VPLogToFile("stream %u: direction %u, formats 0x%llx, rates 0x%llx, channels %u-%u",
+ streamID, info.direction, info.formats, info.rates,
+ info.channelsMinimum, info.channelsMaximum);
+ }
+ }
+ return self;
+}
+
+- (void)dealloc {
+ if (_port) {
+ IOServiceClose(_port);
+ }
+}
+
+- (BOOL)getInfo:(VPVirtIOSoundPCMInfo *)info forStream:(uint32_t)streamID {
+ memset(info, 0, sizeof(*info));
+ uint64_t scalar = streamID;
+ size_t size = sizeof(*info);
+ kern_return_t result = IOConnectCallMethod(_port, kVPVirtIOSoundSelectorPCMInfo,
+ &scalar, 1, NULL, 0, NULL, NULL, info, &size);
+ if (result != KERN_SUCCESS || size != sizeof(*info)) {
+ os_log_error(VPLog(), "stream %u: PCM_INFO failed: 0x%x", streamID, result);
+ return NO;
+ }
+ return YES;
+}
+
+@end
+
@interface VPVirtIOSoundDevice : ASDAudioDevice
-- (instancetype)initWithService:(io_service_t)service index:(unsigned)index plugin:(ASDPlugin *)plugin;
-- (void)addSpeakerControls;
+/// The device for one direction of a service's streams, or nil when the
+/// service has no usable stream that way.
+- (instancetype)initWithConnection:(VPVirtIOSoundConnection *)connection
+ direction:(ASDStreamDirection)direction
+ index:(unsigned)index
+ plugin:(ASDPlugin *)plugin;
+- (void)addControls;
@end
@implementation VPVirtIOSoundDevice {
- io_service_t _service;
- io_connect_t _connection;
+ VPVirtIOSoundConnection *_connection;
+ /// The scope of everything the device has: its streams and its controls.
+ ASDStreamDirection _direction;
VPVirtIOSoundClock _clock;
/// The virtio wire rate, the only rate the device actually runs at.
double _wireRate;
@@ -989,7 +1459,7 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
/// The nominal rate the clock last ran, so a re-set of the current rate
/// does not re-anchor it.
double _clockRate;
- /// The mute control `addSpeakerControls` registered, for the device-level
+ /// The mute control `addControls` registered, for the device-level
/// mute selector's forwarding below.
ASDBooleanControl *_muteControl;
/// The mute state the device-level selector answers; shadowed beside the
@@ -998,51 +1468,26 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
UInt32 _muteState;
}
-static uint32_t VPStreamCount(io_service_t service) {
- CFTypeRef value = IORegistryEntryCreateCFProperty(
- service, CFSTR(kVPVirtIOSoundStreamCountKey), kCFAllocatorDefault, 0);
- uint32_t count = 0;
- if (value && CFGetTypeID(value) == CFNumberGetTypeID()) {
- CFNumberGetValue(value, kCFNumberSInt32Type, &count);
- }
- if (value) {
- CFRelease(value);
- }
- return count;
-}
-
-- (instancetype)initWithService:(io_service_t)service index:(unsigned)index plugin:(ASDPlugin *)plugin {
- io_connect_t connection = IO_OBJECT_NULL;
- kern_return_t result = IOServiceOpen(service, mach_task_self(), 0, &connection);
- if (result != KERN_SUCCESS) {
- os_log_error(VPLog(), "cannot open AppleVirtIOSound: 0x%x", result);
- return nil;
- }
- // The connection is this method's to close until the ivars hold it; from
- // there `dealloc` releases both, including for the nil return below.
- self = [super initWithDeviceUID:VPDeviceUID(index) withPlugin:plugin];
+- (instancetype)initWithConnection:(VPVirtIOSoundConnection *)connection
+ direction:(ASDStreamDirection)direction
+ index:(unsigned)index
+ plugin:(ASDPlugin *)plugin {
+ self = [super initWithDeviceUID:VPDeviceUID(direction, index) withPlugin:plugin];
if (!self) {
- IOServiceClose(connection);
return nil;
}
- IOObjectRetain(service);
- _service = service;
_connection = connection;
+ _direction = direction;
+ BOOL input = direction == ASDStreamDirectionInput;
double sampleRate = 0;
UInt32 latencyFrames = 0;
- uint32_t count = VPStreamCount(service);
- for (uint32_t streamID = 0; streamID < count; streamID++) {
- VPVirtIOSoundPCMInfo info = {0};
- uint64_t scalar = streamID;
- size_t size = sizeof(info);
- result = IOConnectCallMethod(connection, kVPVirtIOSoundSelectorPCMInfo,
- &scalar, 1, NULL, 0, NULL, NULL, &info, &size);
- if (result != KERN_SUCCESS || size != sizeof(info)) {
- os_log_error(VPLog(), "stream %u: PCM_INFO failed: 0x%x", streamID, result);
+ for (uint32_t streamID = 0; streamID < connection.streamCount; streamID++) {
+ VPVirtIOSoundPCMInfo info;
+ if (![connection getInfo:&info forStream:streamID]) {
continue;
}
- if (info.direction != kVPVirtIOSoundDirectionOutput) {
+ if (info.direction != (input ? kVPVirtIOSoundDirectionInput : kVPVirtIOSoundDirectionOutput)) {
continue;
}
VPVirtIOSoundStreamFormat format;
@@ -1055,24 +1500,38 @@ static uint32_t VPStreamCount(io_service_t service) {
if (sampleRate != 0 && format.sampleRate != sampleRate) {
continue;
}
- VPVirtIOSoundStream *stream = [[VPVirtIOSoundStream alloc] initWithConnection:connection
- streamID:streamID
- format:format
- plugin:plugin];
+ VPVirtIOSoundStream *stream;
+ if (input) {
+ stream = [[VPVirtIOSoundInputStream alloc] initWithConnection:connection.port
+ streamID:streamID
+ format:format
+ plugin:plugin];
+ } else {
+ stream = [[VPVirtIOSoundOutputStream alloc] initWithConnection:connection.port
+ streamID:streamID
+ format:format
+ plugin:plugin];
+ }
if (!stream) {
continue;
}
+ stream.device = self;
sampleRate = format.sampleRate;
latencyFrames = stream.queuedLatencyFrames;
- [self addOutputStream:stream];
+ if (input) {
+ [self addInputStream:stream];
+ } else {
+ [self addOutputStream:stream];
+ }
}
if (sampleRate == 0) {
- os_log_error(VPLog(), "AppleVirtIOSound has no usable output stream among %u", count);
return nil;
}
_wireRate = sampleRate;
- _alternateRate = sampleRate == 48000.0 ? kVPAlternateRate : 0;
+ // Only the mix converts between rates; captured frames reach CoreAudio
+ // at the wire rate.
+ _alternateRate = !input && sampleRate == 48000.0 ? kVPAlternateRate : 0;
// The nominal rate the device answers at boot: the wire rate unless the
// override names the alternate (testing VirtualAudio's aggregate-member
// selection, which may compare the current nominal rate, not the list).
@@ -1086,44 +1545,42 @@ static uint32_t VPStreamCount(io_service_t service) {
VPVirtIOSoundClockConfigure(&_clock, (uint32_t)(nominalRate * kTimestampPeriodSeconds), nominalRate,
mach_absolute_time());
_clockRate = nominalRate;
- self.deviceName = @"vphone Speaker";
+ self.deviceName = input ? @"vphone Microphone" : @"vphone Speaker";
self.modelName = @"Virtual Sound Device";
self.manufacturerName = @"vphone";
- self.canBeDefaultOutputDevice = YES;
- self.canBeDefaultSystemDevice = YES;
- self.canBeDefaultInputDevice = NO;
+ self.canBeDefaultOutputDevice = !input;
+ self.canBeDefaultSystemDevice = !input;
+ self.canBeDefaultInputDevice = input;
self.canChangeDeviceName = NO;
self.samplingRates = _alternateRate ? @[@(_alternateRate), @(sampleRate)] : @[@(sampleRate)];
self.samplingRate = nominalRate;
- self.outputSafetyOffset = kSafetyOffsetFrames;
- // What the stream queues ahead of the host, so the HAL presents video
- // against when the sound is heard, not when it is written. Set once, in
- // frames of the rate the device starts at, so after a rate change it is
- // off by the ratio of the rates.
- self.outputLatency = (UInt32)(latencyFrames * nominalRate / sampleRate);
+ // What the stream keeps between the guest's I/O and the host's, so the
+ // HAL presents video against when the sound is heard, not when it is
+ // written, and stamps a recording with when it was captured. Set once,
+ // in frames of the rate the device starts at, so after a rate change it
+ // is off by the ratio of the rates.
+ UInt32 latency = (UInt32)(latencyFrames * nominalRate / sampleRate);
+ if (input) {
+ self.inputSafetyOffset = kSafetyOffsetFrames;
+ self.inputLatency = latency;
+ } else {
+ self.outputSafetyOffset = kSafetyOffsetFrames;
+ self.outputLatency = latency;
+ }
self.transportType = VPTransportType();
self.timestampPeriod = _clock.periodFrames;
[self installIOBlocks];
UInt32 transport = self.transportType;
os_log(VPLog(), "device %{public}@: %.0f Hz nominal (%.0f wire, %.0f advertised too), transport '%c%c%c%c'",
- VPDeviceUID(index), nominalRate, sampleRate, _alternateRate,
+ VPDeviceUID(direction, index), nominalRate, sampleRate, _alternateRate,
(char)(transport >> 24), (char)(transport >> 16), (char)(transport >> 8), (char)transport);
VPLogToFile("device %s: %.0f Hz nominal (%.0f wire, %.0f advertised too), transport '%c%c%c%c'",
- VPDeviceUID(index).UTF8String, nominalRate, sampleRate, _alternateRate,
+ VPDeviceUID(direction, index).UTF8String, nominalRate, sampleRate, _alternateRate,
(char)(transport >> 24), (char)(transport >> 16), (char)(transport >> 8), (char)transport);
return self;
}
-- (void)dealloc {
- if (_connection) {
- IOServiceClose(_connection);
- }
- if (_service) {
- IOObjectRelease(_service);
- }
-}
-
/// ASDAudioDevice's `performStopIO` releases every I/O block the device holds
/// (and clears the unretained copies the I/O thread calls), and
/// `performStartIO` copies them again from the same properties. Blocks set
@@ -1133,6 +1590,7 @@ static uint32_t VPStreamCount(io_service_t service) {
/// start after the first. They go back in before every start.
- (void)installIOBlocks {
VPVirtIOSoundClock *clock = &_clock;
+ Boolean input = _direction == ASDStreamDirectionInput;
self.getZeroTimestampBlock = ^int(Float64 *sampleTime, UInt64 *hostTime, UInt64 *seed, UInt32 clientID) {
(void)clientID;
// False only when anchors kept landing through every read attempt;
@@ -1142,13 +1600,13 @@ static uint32_t VPStreamCount(io_service_t service) {
};
self.willDoReadInputBlock = ^int(UInt32 operationID, Boolean *willDo, Boolean *willDoInPlace) {
(void)operationID;
- *willDo = false;
+ *willDo = input;
*willDoInPlace = true;
return kAudioHardwareNoError;
};
self.willDoWriteMixBlock = ^int(UInt32 operationID, Boolean *willDo, Boolean *willDoInPlace) {
(void)operationID;
- *willDo = true;
+ *willDo = !input;
*willDoInPlace = true;
return kAudioHardwareNoError;
};
@@ -1202,10 +1660,11 @@ static uint32_t VPStreamCount(io_service_t service) {
/// 'mute'. The route's throw that follows is quieted host-side, in the
/// VirtualAudio binary patch; this dispatch stays as macOS's ASD carries it,
/// with the mute control this device registered as the backing store, for
-/// the build that does forward.
-static BOOL VPIsDeviceMuteAddress(const AudioObjectPropertyAddress *address) {
+/// the build that does forward. The microphone device answers the same way
+/// in its own scope.
+static BOOL VPIsDeviceMuteAddress(const AudioObjectPropertyAddress *address, ASDStreamDirection scope) {
return address->mSelector == kVPDevicePropertyMute
- && (address->mScope == kAudioObjectPropertyScopeOutput
+ && (address->mScope == scope
|| address->mScope == kAudioObjectPropertyScopeGlobal)
&& (address->mElement == kAudioObjectPropertyElementMain
|| address->mElement == 0);
@@ -1213,7 +1672,7 @@ static BOOL VPIsDeviceMuteAddress(const AudioObjectPropertyAddress *address) {
/// `kAudioDevicePropertyNominalSampleRate` is a global-scope, main-element
/// property ('nsrt'/glob/0). A device-level mute address check accepts the
-/// output scope as well; the rate has no per-scope form, so global only.
+/// device's own scope as well; the rate has no per-scope form, so global only.
static BOOL VPIsDeviceRateAddress(const AudioObjectPropertyAddress *address) {
return address->mSelector == kVPDevicePropertyNominalSampleRate
&& address->mScope == kAudioObjectPropertyScopeGlobal
@@ -1223,7 +1682,7 @@ static BOOL VPIsDeviceRateAddress(const AudioObjectPropertyAddress *address) {
- (BOOL)hasProperty:(AudioObjectPropertyAddress *)address {
VPLogSelectorQuery("has", address);
- if (VPIsDeviceMuteAddress(address)) {
+ if (VPIsDeviceMuteAddress(address, _direction)) {
return YES;
}
return [super hasProperty:address];
@@ -1231,7 +1690,7 @@ static BOOL VPIsDeviceRateAddress(const AudioObjectPropertyAddress *address) {
- (BOOL)isPropertySettable:(AudioObjectPropertyAddress *)address {
VPLogSelectorQuery("settable", address);
- if (VPIsDeviceMuteAddress(address) || VPIsDeviceRateAddress(address)) {
+ if (VPIsDeviceMuteAddress(address, _direction) || VPIsDeviceRateAddress(address)) {
return YES;
}
return [super isPropertySettable:address];
@@ -1241,7 +1700,7 @@ static BOOL VPIsDeviceRateAddress(const AudioObjectPropertyAddress *address) {
withQualifierSize:(UInt32)qualifierSize
andQualifierData:(const void *)qualifierData {
VPLogSelectorQuery("size", address);
- if (VPIsDeviceMuteAddress(address)) {
+ if (VPIsDeviceMuteAddress(address, _direction)) {
return sizeof(UInt32);
}
return [super dataSizeForProperty:address withQualifierSize:qualifierSize
@@ -1255,7 +1714,7 @@ static BOOL VPIsDeviceRateAddress(const AudioObjectPropertyAddress *address) {
andData:(void *)data
forClient:(UInt32)clientID {
VPLogSelectorQuery("get", address);
- if (VPIsDeviceMuteAddress(address)) {
+ if (VPIsDeviceMuteAddress(address, _direction)) {
if (dataSize == NULL || *dataSize < sizeof(UInt32)) {
return NO;
}
@@ -1295,7 +1754,7 @@ static BOOL VPIsDeviceRateAddress(const AudioObjectPropertyAddress *address) {
andData:(const void *)data
forClient:(UInt32)clientID {
VPLogSelectorQuery("set", address);
- if (VPIsDeviceMuteAddress(address)) {
+ if (VPIsDeviceMuteAddress(address, _direction)) {
if (dataSize < sizeof(UInt32) || data == NULL) {
return NO;
}
@@ -1321,29 +1780,32 @@ static BOOL VPIsDeviceRateAddress(const AudioObjectPropertyAddress *address) {
// MARK: Controls
-- (void)addSpeakerControls {
+- (void)addControls {
// The control set a real speaker answers with: one selected data-source
// value ('ispk', "Speakers"), then mute and volume, all on the master
- // element of the output scope. Mirrored argument for argument from the
+ // element of the output scope; a microphone has the same three in the
+ // input scope, its data source 'imic', "Microphone". Mirrored argument
+ // for argument from the
// macOS plugin, which adds these from its own
// `halInitializeWithPluginHost:` — after the device is built, before it
// is registered — and never from inside the device's init: calling
- // `addControl:` during `initWithService:` fails the whole device
+ // `addControl:` during the device's init fails the whole device
// activation (HALS_PlugIn.cpp:162). The pspk route runs in HardwareOnly
// volume mode and reads this control set; without it every volume query
// on the route fails and the endpoint is retyped "Unspecified" after
// the first playback, silencing every later one.
- ASDSelectorValue *speakers = [[ASDSelectorValue alloc] init];
- [speakers setValue:kVPDataSourceInternalSpeaker];
- [speakers setName:@"Speakers"];
+ BOOL input = _direction == ASDStreamDirectionInput;
+ ASDSelectorValue *source = [[ASDSelectorValue alloc] init];
+ [source setValue:input ? kVPDataSourceInternalMicrophone : kVPDataSourceInternalSpeaker];
+ [source setName:input ? @"Microphone" : @"Speakers"];
ASDSelectorControl *dataSource = [[ASDSelectorControl alloc]
initWithIsSettable:YES
forElement:0
- inScope:ASDStreamDirectionOutput
+ inScope:_direction
withPlugin:self.plugin
andObjectClassID:kAudioDataSourceControlClassID];
- [dataSource addValue:speakers];
- [dataSource setSelectedValues:@[speakers]];
+ [dataSource addValue:source];
+ [dataSource setSelectedValues:@[source]];
[self addControl:dataSource];
// The macOS plugin's `-[AVIODevice _addMuteAndVolumeControlsInScope:]`
@@ -1361,11 +1823,11 @@ static BOOL VPIsDeviceRateAddress(const AudioObjectPropertyAddress *address) {
// class IDs the way the data-source control above pins 'dsrc'.
ASDPlugin *plugin = self.plugin;
ASDBooleanControl *mute = [[ASDBooleanControl alloc] initWithValue:NO
- isSettable:YES forElement:0 inScope:ASDStreamDirectionOutput
+ isSettable:YES forElement:0 inScope:_direction
withPlugin:plugin andObjectClassID:kAudioMuteControlClassID];
ASDLevelControl *volume = [[ASDLevelControl alloc] initWithDecibelValue:-30.0
minimumValue:-60.0 maximumValue:0.0 isSettable:YES
- forElement:0 inScope:ASDStreamDirectionOutput
+ forElement:0 inScope:_direction
withPlugin:plugin andObjectClassID:kAudioVolumeControlClassID];
[self addControl:mute];
[self addControl:volume];
@@ -1373,8 +1835,9 @@ static BOOL VPIsDeviceRateAddress(const AudioObjectPropertyAddress *address) {
[volume setDecibelValue:volume.maximumDecibelValue];
_muteControl = mute;
_muteState = 0;
- os_log(VPLog(), "speaker controls added: dsrc 'ispk', mute, volume");
- VPLogToFile("speaker controls added: dsrc 'ispk', mute, volume");
+ os_log(VPLog(), "%{public}s controls added: dsrc, mute, volume", input ? "microphone" : "speaker");
+ VPLogToFile("%s controls added: dsrc '%s', mute, volume",
+ input ? "microphone" : "speaker", input ? "imic" : "ispk");
}
@end
@@ -1445,20 +1908,32 @@ static BOOL VPIsDeviceRateAddress(const AudioObjectPropertyAddress *address) {
os_log_error(VPLog(), "no AppleVirtIOSound service: 0x%x", result);
return;
}
+ static const ASDStreamDirection directions[] = {ASDStreamDirectionOutput, ASDStreamDirectionInput};
unsigned index = 0;
+ unsigned published = 0;
io_service_t service;
while ((service = IOIteratorNext(services))) {
- VPVirtIOSoundDevice *device = [[VPVirtIOSoundDevice alloc] initWithService:service index:index plugin:self];
+ VPVirtIOSoundConnection *connection = [[VPVirtIOSoundConnection alloc] initWithService:service];
IOObjectRelease(service);
- if (device) {
- [device addSpeakerControls];
- [self addAudioDevice:device];
- index++;
+ if (!connection) {
+ continue;
+ }
+ for (unsigned i = 0; i < sizeof(directions) / sizeof(directions[0]); i++) {
+ VPVirtIOSoundDevice *device = [[VPVirtIOSoundDevice alloc] initWithConnection:connection
+ direction:directions[i]
+ index:index
+ plugin:self];
+ if (device) {
+ [device addControls];
+ [self addAudioDevice:device];
+ published++;
+ }
}
+ index++;
}
IOObjectRelease(services);
- os_log(VPLog(), "published %u virtio sound device(s)", index);
- VPLogToFile("published %u virtio sound device(s)", index);
+ os_log(VPLog(), "published %u virtio sound device(s)", published);
+ VPLogToFile("published %u virtio sound device(s)", published);
}
@end
diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundProtocol.h b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundProtocol.h
index 6ebe940d..14e4515f 100644
--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundProtocol.h
+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundProtocol.h
@@ -31,6 +31,10 @@ enum {
/// Async. 1 scalar in (stream ID), the PCM bytes as the input structure.
/// The kernel keeps the caller's pages until the completion arrives.
kVPVirtIOSoundSelectorWrite = 8,
+ /// Async. 1 scalar in (stream ID), one period of the caller's pages as the
+ /// output structure. The device fills it with captured PCM and the
+ /// completion arrives when the period is full.
+ kVPVirtIOSoundSelectorRead = 9,
};
/// The IORegistry key on the `AppleVirtIOSound` service with the stream count.
diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.c b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.c
index 4876fd83..f2fe2172 100644
--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.c
+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.c
@@ -109,3 +109,127 @@ void VPVirtIOSoundRingDidComplete(VPVirtIOSoundRing *ring, uint32_t length) {
uint64_t VPVirtIOSoundRingInFlight(const VPVirtIOSoundRing *ring) {
return ring->submitted - atomic_load_explicit(&ring->completed, memory_order_acquire);
}
+
+// MARK: - Input ring
+
+bool VPVirtIOSoundInputRingInit(VPVirtIOSoundInputRing *ring, uint32_t capacity, uint32_t period) {
+ memset(ring, 0, sizeof(*ring));
+ if (period == 0 || capacity == 0 || capacity % period != 0) {
+ return false;
+ }
+ void *bytes = NULL;
+ if (posix_memalign(&bytes, (size_t)getpagesize(), capacity) != 0) {
+ return false;
+ }
+ memset(bytes, 0, capacity);
+ ring->bytes = bytes;
+ ring->capacity = capacity;
+ ring->period = period;
+ atomic_init(&ring->consumed, 0);
+ atomic_init(&ring->filled, 0);
+ return true;
+}
+
+void VPVirtIOSoundInputRingDestroy(VPVirtIOSoundInputRing *ring) {
+ free(ring->bytes);
+ memset(ring, 0, sizeof(*ring));
+}
+
+void VPVirtIOSoundInputRingReset(VPVirtIOSoundInputRing *ring) {
+ atomic_store_explicit(&ring->consumed, 0, memory_order_relaxed);
+ atomic_store_explicit(&ring->filled, 0, memory_order_relaxed);
+ ring->submitted = 0;
+}
+
+bool VPVirtIOSoundInputRingNextSubmission(const VPVirtIOSoundInputRing *ring, uint32_t *offset) {
+ uint64_t consumed = atomic_load_explicit(&ring->consumed, memory_order_acquire);
+ if (ring->period > ring->capacity - (ring->submitted - consumed)) {
+ return false;
+ }
+ *offset = (uint32_t)(ring->submitted % ring->capacity);
+ return true;
+}
+
+void VPVirtIOSoundInputRingDidSubmit(VPVirtIOSoundInputRing *ring) {
+ ring->submitted += ring->period;
+}
+
+void VPVirtIOSoundInputRingDidComplete(VPVirtIOSoundInputRing *ring) {
+ atomic_fetch_add_explicit(&ring->filled, ring->period, memory_order_release);
+}
+
+uint64_t VPVirtIOSoundInputRingInFlight(const VPVirtIOSoundInputRing *ring) {
+ return ring->submitted - atomic_load_explicit(&ring->filled, memory_order_relaxed);
+}
+
+uint64_t VPVirtIOSoundInputRingAvailable(const VPVirtIOSoundInputRing *ring) {
+ uint64_t filled = atomic_load_explicit(&ring->filled, memory_order_acquire);
+ return filled - atomic_load_explicit(&ring->consumed, memory_order_relaxed);
+}
+
+bool VPVirtIOSoundInputRingRead(VPVirtIOSoundInputRing *ring, void *destination, uint32_t length) {
+ if (length == 0) {
+ return true;
+ }
+ uint64_t consumed = atomic_load_explicit(&ring->consumed, memory_order_relaxed);
+ uint64_t filled = atomic_load_explicit(&ring->filled, memory_order_acquire);
+ if (length > filled - consumed) {
+ return false;
+ }
+ uint32_t offset = (uint32_t)(consumed % ring->capacity);
+ uint32_t first = ring->capacity - offset;
+ if (first >= length) {
+ memcpy(destination, ring->bytes + offset, length);
+ } else {
+ memcpy(destination, ring->bytes + offset, first);
+ memcpy((uint8_t *)destination + first, ring->bytes, length - first);
+ }
+ atomic_store_explicit(&ring->consumed, consumed + length, memory_order_release);
+ return true;
+}
+
+bool VPVirtIOSoundInputRingSkip(VPVirtIOSoundInputRing *ring, uint64_t length) {
+ uint64_t consumed = atomic_load_explicit(&ring->consumed, memory_order_relaxed);
+ uint64_t filled = atomic_load_explicit(&ring->filled, memory_order_acquire);
+ if (length > filled - consumed) {
+ return false;
+ }
+ atomic_store_explicit(&ring->consumed, consumed + length, memory_order_release);
+ return true;
+}
+
+// MARK: - Input reader
+
+/// Drop the oldest frames until `keep` bytes are left. `consumed` only ever
+/// moves by whole frames, so the frames after it stay aligned.
+static uint64_t skipToBacklog(VPVirtIOSoundInputReader *reader, uint64_t available, uint64_t keep) {
+ uint64_t excess = available - keep;
+ excess -= excess % reader->bytesPerFrame;
+ return VPVirtIOSoundInputRingSkip(reader->ring, excess) ? excess : 0;
+}
+
+void VPVirtIOSoundInputReaderRead(VPVirtIOSoundInputReader *reader, void *destination, uint32_t frames) {
+ VPVirtIOSoundInputRing *ring = reader->ring;
+ uint32_t length = frames * reader->bytesPerFrame;
+ if (atomic_exchange_explicit(&reader->restart, false, memory_order_acq_rel)) {
+ skipToBacklog(reader, VPVirtIOSoundInputRingAvailable(ring), 0);
+ reader->primed = false;
+ }
+ uint64_t available = VPVirtIOSoundInputRingAvailable(ring);
+ // A read larger than the lead needs that much before it can be served.
+ uint64_t lead = reader->leadBytes > length ? reader->leadBytes : length;
+ if (!reader->primed && available >= lead) {
+ reader->primed = true;
+ }
+ if (reader->primed && available > reader->maximumBacklogBytes && available > lead) {
+ uint64_t skipped = skipToBacklog(reader, available, lead);
+ atomic_fetch_add_explicit(&reader->skippedBytes, skipped, memory_order_relaxed);
+ }
+ if (reader->primed && VPVirtIOSoundInputRingRead(ring, destination, length)) {
+ atomic_fetch_add_explicit(&reader->servedFrames, frames, memory_order_relaxed);
+ return;
+ }
+ memset(destination, 0, length);
+ atomic_fetch_add_explicit(&reader->silentFrames, frames, memory_order_relaxed);
+ reader->primed = false;
+}
diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.h b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.h
index 80d42c0e..d6eda85a 100644
--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.h
+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.h
@@ -1,6 +1,6 @@
-// VPVirtIOSoundRing.h — output ring between the HAL I/O thread and virtio.
+// VPVirtIOSoundRing.h — the rings between the HAL I/O thread and virtio.
//
-// Three byte counters, each with one writer:
+// The output ring has three byte counters, each with one writer:
//
// completed <= submitted <= written
//
@@ -66,4 +66,91 @@ void VPVirtIOSoundRingDidComplete(VPVirtIOSoundRing *ring, uint32_t length);
/// Bytes handed to the device and not yet returned.
uint64_t VPVirtIOSoundRingInFlight(const VPVirtIOSoundRing *ring);
+// MARK: - Input ring
+//
+// The same three counters, turned around:
+//
+// consumed <= filled <= submitted
+//
+// The stream's serial queue hands whole period slots to the device and
+// advances `submitted`, and advances `filled` when the device returns one
+// full of captured frames. The real-time I/O thread copies frames out and
+// advances `consumed`. Only `[submitted, consumed + capacity)` may be handed
+// out: the device writes a submitted slot until it completes, and the I/O
+// thread has not read a filled one yet.
+
+typedef struct {
+ uint8_t *bytes;
+ uint32_t capacity;
+ uint32_t period;
+ _Atomic uint64_t consumed;
+ _Atomic uint64_t filled;
+ uint64_t submitted;
+} VPVirtIOSoundInputRing;
+
+/// Allocate a page-aligned, zeroed ring. False when allocation fails or the
+/// capacity is not a whole number of periods.
+bool VPVirtIOSoundInputRingInit(VPVirtIOSoundInputRing *ring, uint32_t capacity, uint32_t period);
+void VPVirtIOSoundInputRingDestroy(VPVirtIOSoundInputRing *ring);
+
+/// Rewind every counter to zero. Only while nothing is in flight and the I/O
+/// thread is not reading.
+void VPVirtIOSoundInputRingReset(VPVirtIOSoundInputRing *ring);
+
+/// The next period slot to hand the device. Slots are whole periods and the
+/// capacity is a whole number of them, so a slot never crosses the end of
+/// the buffer. False when no period is free: the I/O thread fell behind.
+bool VPVirtIOSoundInputRingNextSubmission(const VPVirtIOSoundInputRing *ring, uint32_t *offset);
+
+void VPVirtIOSoundInputRingDidSubmit(VPVirtIOSoundInputRing *ring);
+/// The device returns slots in the order it was handed them, each one a
+/// period of frames.
+void VPVirtIOSoundInputRingDidComplete(VPVirtIOSoundInputRing *ring);
+
+/// Bytes handed to the device and not yet returned.
+uint64_t VPVirtIOSoundInputRingInFlight(const VPVirtIOSoundInputRing *ring);
+
+/// Captured bytes the I/O thread has not read yet.
+uint64_t VPVirtIOSoundInputRingAvailable(const VPVirtIOSoundInputRing *ring);
+
+/// Copy out the oldest `length` bytes, all or none. Real-time safe. False,
+/// and nothing consumed, when fewer are available.
+bool VPVirtIOSoundInputRingRead(VPVirtIOSoundInputRing *ring, void *destination, uint32_t length);
+
+/// Drop the oldest `length` bytes unread, all or none. Real-time safe; the
+/// I/O thread's side of the ring, like a read.
+bool VPVirtIOSoundInputRingSkip(VPVirtIOSoundInputRing *ring, uint64_t length);
+
+// MARK: - Input reader
+//
+// The device returns captured frames a period at a time and the HAL reads
+// them a few milliseconds at a time, each on its own clock. Read straight
+// through, the ring would run empty just before every period lands and any
+// period that lands late would be a gap. So reads are served only once
+// `leadBytes` have been captured, which keeps about that much between the
+// host's capture and the guest's read, and a read that finds too little
+// waits for the lead again. When the HAL reads slower than the host
+// captures, what is buffered grows; past `maximumBacklogBytes` the oldest
+// frames are dropped back to the lead, so the delay stays bounded.
+
+typedef struct {
+ VPVirtIOSoundInputRing *ring;
+ uint32_t bytesPerFrame;
+ uint32_t leadBytes;
+ uint32_t maximumBacklogBytes;
+ /// Set when the stream starts: the next read drops what the last run left.
+ _Atomic bool restart;
+ /// The I/O thread's own.
+ bool primed;
+ /// Since the last start: frames served from the ring, frames served as
+ /// silence, and bytes dropped to bound the backlog.
+ _Atomic uint64_t servedFrames;
+ _Atomic uint64_t silentFrames;
+ _Atomic uint64_t skippedBytes;
+} VPVirtIOSoundInputReader;
+
+/// Fill `destination` with `frames` captured frames, or with silence when
+/// the ring cannot serve them. Real-time safe.
+void VPVirtIOSoundInputReaderRead(VPVirtIOSoundInputReader *reader, void *destination, uint32_t frames);
+
#endif /* VPVirtIOSoundRing_h */
diff --git a/VPhoneLaunchpad/Resources/VPhoneLaunchpad.Info.plist b/VPhoneLaunchpad/Resources/VPhoneLaunchpad.Info.plist
index e5815b79..dc78eea3 100644
--- a/VPhoneLaunchpad/Resources/VPhoneLaunchpad.Info.plist
+++ b/VPhoneLaunchpad/Resources/VPhoneLaunchpad.Info.plist
@@ -36,6 +36,10 @@
<string>Shares your location with the virtual iPhone so apps running inside it see the same location.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Shares your location with the virtual iPhone so apps running inside it see the same location.</string>
+ <!-- The guest's microphone is this Mac's, opened by vphone-vm while the
+ guest records. Same arrangement as the location descriptions above. -->
+ <key>NSMicrophoneUsageDescription</key>
+ <string>Shares this Mac's microphone with the virtual iPhone so apps running inside it can record.</string>
<key>SMPrivilegedExecutables</key>
<dict>
<key>com.vphone.launchpad.helper</key>
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/InfoPlist.xcstrings b/VPhoneLaunchpad/VPhoneLaunchpad/InfoPlist.xcstrings
index 6c11e9ae..434319f9 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/InfoPlist.xcstrings
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/InfoPlist.xcstrings
@@ -177,7 +177,43 @@
}
}
}
+ },
+ "NSMicrophoneUsageDescription" : {
+ "comment" : "Privacy - Microphone Usage Description",
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Shares this Mac's microphone with the virtual iPhone so apps running inside it can record."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "この Mac のマイクを仮想 iPhone と共有し、その中のアプリで録音できるようにします。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "가상 iPhone과 이 Mac의 마이크를 공유하여 그 안에서 실행되는 앱이 녹음할 수 있도록 합니다."
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Chia sẻ micrô của máy Mac này với iPhone ảo để các ứng dụng bên trong có thể ghi âm."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "将这台 Mac 的麦克风共享给虚拟 iPhone,使其中运行的应用能够录音。"
+ }
+ }
+ }
}
+
},
"version" : "1.3"
}
\ No newline at end of file