Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Keep guest audio playing after the first stop, at the right rate

Jacky0c0b501ca852
diff --git a/Research/Guest/virtio_sound.md b/Research/Guest/virtio_sound.md
index 57ca8ed4..26b99760 100644
--- a/Research/Guest/virtio_sound.md
+++ b/Research/Guest/virtio_sound.md
@@ -351,6 +351,112 @@ the cheap form of it, and implementing the missing volume/port property
callbacks is the completion of that path. The iOS 27 binary
(`VirtualAudio-ios27`) is saved for the N71-side port of the same fix.
+## 6. Playing past the first stop, at the right rate, without gaps (2026-10-03, night)
+
+Three faults in the plugin itself survived everything above. Each was found on
+a new iPad16,1 / 26.6.2 guest (`audiotest-ipad`) with counters the plugin now
+writes to `/var/mobile/vpquery.log` at every stream stop:
+
+```
+stream 1: 237 writes, 0 starved, 0 bytes dropped, lead 2 period(s)
+stream 1: 20.16 s, in 888832 frames (44094/s, hal 44100), out 967436 frames (47993/s)
+```
+
+`writes` are periods handed to the kernel, `starved` the ones that found the
+device with nothing left in flight (a gap on the host), `in` what the HAL
+handed the mix block and `out` what went to the ring.
+
+### Every start after the first failed
+
+Symptom: the first playback after audiomxd launches works; after any stop —
+the screen turning off, the next ringtone, pausing in Safari — every start
+ends five seconds later in
+
+```
+HALS_IOContext_Legacy_Impl::IsTimeRunning_Helper: Device PuffinOutput is not running.
+HALS_IOContext_Legacy_Impl::IOWorkLoop: could not establish a timeline after waiting 5000000 microseconds
+```
+
+and the plugin's `getZeroTimestampBlock` is never called during those five
+seconds. AudioServerDriver does that itself (guest framework, image base
+`0x249eae000`; offsets in the image):
+
+* `-[ASDAudioDevice performStartIO]` (`0x5b2c`) copies each I/O block from its
+ property — whose getter (`0x5fd4` for `getZeroTimestampBlock`) returns the
+ very ivar being assigned — into that ivar and its unretained twin, starts
+ the streams, sets `_running` and posts `'goin'`.
+* `-[ASDAudioDevice performStopIO]` (`0x7ae8`) stops the streams, clears
+ `_running`, then **releases every one of those blocks and stores nil in both
+ ivars** (`0x7bc4`…`0x7d40`), and posts `'goin'`.
+* `-[ASDStream stopStream]` (`0x7fb8`) does the same to the stream's blocks,
+ `writeMixBlock` among them.
+
+So a block set once at init survives exactly one start. The plugin now sets
+them before every start: `installIOBlocks` at the top of the device's
+`performStartIO`, `installMixBlock` before `[super startStream]`.
+
+This is also what the "frozen seed" reading in
+`virtualaudio_speaker_route_throws.md` had actually seen. The boot chime was
+the first start and played; the ringtone after it was the second and timed
+out. Moving the seed with every period did not fix that — a later run happened
+to make its first start the tested one.
+
+### The timestamp period must not change with the rate
+
+Symptom, once starts worked: choppy playback, the HAL handing over about
+45,700–46,000 frames a second on a 44100 device, and four times a second
+
+```
+HALS_IORawClock::Update: Re-anchoring IO timeline. Sample time is not consecutive,
+HostTime is increasing, Ring buffer size: 12480.
+```
+
+12480 is the period the device published at init (48000 × 0.26 s). The rate
+change to 44100 (`set nsrt`, answered by the device itself, see item 6
+of the validation list below) had also moved `timestampPeriod` to 11466, but nothing makes the HAL
+read that property again, so it kept checking that every zero timestamp
+advanced by 12480 frames, found 11466, and re-anchored. The clock now keeps
+the period's frame count for the life of the device and changes only how long
+a period lasts (`VPClockSetRate`): 12480 frames take 0.26 s at 48000 and
+0.283 s at 44100. Measured after: 44,086–44,094 frames/s in, zero re-anchors.
+
+The seed follows the same rule the HAL states in that log family: it names the
+timeline and moves only at an anchor (`performStartIO`, a rate change). Moved
+with every period it produced `Re-anchoring IO timeline. Zero timestamp seed
+changed` every 0.26 s.
+
+### Nothing was queued ahead of the host
+
+The virtio device plays what it is handed as it arrives, and the mix arrives
+in real time: a period (4096 frames, 85 ms) is submitted when it fills, by a
+timer that runs every 42 ms. With nothing queued ahead, any period later than
+the one before it finished is a gap. Measured with the clock already fixed:
+89 of 513 writes starved in one 44 s tone, 138 of 203 in the next.
+
+The stream now queues `VPhoneVirtIOSoundLeadPeriods` periods of silence
+(default 2, 171 ms) when the device starts, tops the queue back up when a
+start finds the device still draining, and the device reports the lead plus
+one period as `outputLatency` so video is presented against when the sound is
+heard. Measured: 0 of 499 and 0 of 237 writes starved.
+
+### Nothing left to set by hand
+
+Both settings §4 and §5 asked for are now what the plugin does unasked:
+
+* the device UID is `PuffinOutput` unless `VPhoneVirtIOSoundDeviceUID` names
+ another;
+* on an iPad guest (`hw.machine`), `ProductIDOverride` is set to 8010 in the
+ `com.apple.audio.virtualaudio` domain from `halInitializeWithPluginHost:`
+ when nothing stored names one. The plugin loads before VirtualAudio reads
+ its defaults, and both are in audiomxd, so the in-process value is the one
+ it reads; audiomxd's sandbox keeps the write off disk (`settings.get` still
+ shows the domain empty), which is why it is repeated every launch. 8010
+ rather than §4's 198 because 198's category map has no ringtone-preview
+ entry (`virtualaudio_speaker_route_throws.md`, layer 1).
+
+Verified on a guest with all four keys deleted and rebooted: VirtualAudio
+initializes, the boot chime and a ringtone preview start I/O on
+`PuffinOutput (VAD [vdef] AggDev N)`, no `not initialized yet`.
## Reveal and validation
@@ -359,17 +465,18 @@ callbacks is the completion of that path. The iOS 27 binary
2. Plugin loaded: `logs.syslog` for `audiomxd` shows
`com.vphone.audio/virtiosound` lines `stream 0: 48000 Hz, 2 channels…` and
`published 1 virtio sound device(s)`.
-3. VirtualAudio initialized: with `ProductIDOverride = 198` set (§4), a
- restarted `audiomxd` logs `PlugIn initialized ? 2` / `VA Init Status: 0`,
- no `PRECONDITION FAILURE`, and no
- `VirtualAudio PlugIn is not initialized yet` when an app opens a session.
-4. Sound on the host: audible. The speaker route needs three things beyond
- the plugin — `ProductIDOverride` (§4), the `PuffinOutput` UID (§5),
- `system-virtualaudio-cfw-speaker_route_throws` (§ above and
- `virtualaudio_speaker_route_throws.md`), and the zero-timestamp seed
- advancing with the timestamp (`VPClockZeroTimestamp`), without which the
- vdef's aggregate never establishes the device timeline and aborts the
- start with `nope`.
+3. VirtualAudio initialized: `audiomxd` logs `Defaults key ProductIDOverride
+ was defined to 8010`, `PlugIn initialized ? 2` / `VA Init Status: 0`, no
+ `PRECONDITION FAILURE`, and no `VirtualAudio PlugIn is not initialized yet`
+ when an app opens a session. The plugin supplies the override on an iPad
+ guest (§6); `vpquery.log` says so when it had to.
+4. Sound on the host: audible, and still audible after a stop. Beyond the
+ plugin's own defaults (§6) the speaker route needs the VirtualAudio patches
+ (`virtualaudio_speaker_route_throws.md`). In `vpquery.log`, every
+ `performStartIO` is followed by a `stopStream` whose counters show `writes`
+ in proportion to the seconds played, `0 starved`, and `in` at the nominal
+ rate; `logs.syslog` for audiomxd shows no `Re-anchoring IO timeline` and no
+ `could not establish a timeline`.
5. Hardware volume: the plugin answers the pspk route's volume queries with a
real control set — dsrc selector ('ispk' "Speakers"), mute, volume — added
from `halInitializeWithPluginHost:` after device init, before
@@ -395,8 +502,8 @@ callbacks is the completion of that path. The iOS 27 binary
`@[@44100, @48000]` and the 48 kHz stream carries a 44100 physical format;
`setSamplingRate:`/`deviceChangedToSamplingRate:` move an atomic HAL rate
under the mix block, which linearly resamples 44100→48000 onto the wire
- while the clock re-derives its period math. Verified booting and answering
- at both nominal rates — and disproven as the ringtone gate: a 44100-nominal
+ while the clock re-derives how long its period lasts (§6). Verified
+ booting and answering at both nominal rates — and disproven as the ringtone gate: a 44100-nominal
boot fails the tone tap exactly like a 48000 one. The gate is the
RingtonePreview category itself; see the session-5 section of
`virtualaudio_speaker_route_throws.md`.
diff --git a/Research/Guest/virtualaudio_speaker_route_throws.md b/Research/Guest/virtualaudio_speaker_route_throws.md
index 0d8c5fb3..9e57d837 100644
--- a/Research/Guest/virtualaudio_speaker_route_throws.md
+++ b/Research/Guest/virtualaudio_speaker_route_throws.md
@@ -97,6 +97,13 @@ timeline failure and no crash (`~/.vphone/va-analysis/clockfix.json`), and
the system route reads `PVMSetCurrentState [Audio/Video, Default, Speaker,
PuffinOutput]`.
+**Corrected 2026-10-03 (night).** The seed was not the cause. AudioServerDriver
+clears a device's I/O blocks at every stop, so the plugin's zero-timestamp
+block existed for the first start only: the chime was that first start, the
+ringtone the second. A seed that moves with every period is itself a fault —
+the HAL re-anchors its timeline on each one. Both are fixed in the plugin; see
+`virtio_sound.md` §6.
+
## Where the audio path stands after the seed fix (open work)
With the walker silenced and the clock seed advancing, ringtone sessions play
@@ -669,4 +676,12 @@ in `SoftwareHardwareMix` with whatever volume description the map holds
expectation; volume-adjust behavior after it is the next thing to
re-check, and the earlier HardwareOnly volume-mode suspicion is related
but separate. In-guest proof standard unchanged: `:252` = 0 and StartIO
-on the `PuffinOutput` aggregate (`VAD [vdef] AggDev 2`), not Null_Device.
\ No newline at end of file
+on the `PuffinOutput` aggregate (`VAD [vdef] AggDev 2`), not Null_Device.
+**Verified in the guest (2026-10-03, night).** On a new iPad16,1 / 26.6.2
+guest with all four VirtualAudio patches applied by `cfw install`, a ringtone
+preview logs `PerVAD Volume description of scope 1 present for route
+[ Category: 'crnp'; Mode: 'imdf' ]`, no `Precondition failure`, no `nort`, and
+its I/O context is `PuffinOutput (VAD [vdef] AggDev N)`. Volume adjusts
+(`Setting hardware volume to -11.97 dB` on device `PuffinOutput`). What still
+kept the tone silent after that was not routing: see `virtio_sound.md` §6 for
+the plugin faults and §7 for the haptic track.
diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundAudioServerDriver.h b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundAudioServerDriver.h
index eab1057e..2a3fc1d4 100644
--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundAudioServerDriver.h
+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundAudioServerDriver.h
@@ -86,6 +86,7 @@ typedef int (^ASDIOBlock)(
@property (nonatomic) UInt32 timestampPeriod;
@property (nonatomic) UInt32 inputSafetyOffset;
@property (nonatomic) UInt32 outputSafetyOffset;
+@property (nonatomic) UInt32 outputLatency;
@property (nonatomic) UInt32 transportType;
@property (readonly, nonatomic) BOOL hasOutput;
/// The rates the device answers `kAudioDevicePropertyAvailableSampleRates`
diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m
index b021eed2..2dda87dd 100644
--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m
+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m
@@ -12,10 +12,12 @@
// 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.
//
-// Two optional settings in the `com.apple.coreaudio` preference domain, read
+// Optional settings in the `com.apple.coreaudio` preference domain, read
// once when audiomxd loads the plugin, exist for diagnosing routing:
// VPhoneVirtIOSoundTransportType four-character transport ("usb ", "bltn")
-// VPhoneVirtIOSoundDeviceUID HAL device UID
+// VPhoneVirtIOSoundDeviceUID HAL device UID (default "PuffinOutput")
+// VPhoneVirtIOSoundLeadPeriods periods of silence queued ahead of the
+// mix at each start, 0 to 8
#import "VPVirtIOSoundAudioServerDriver.h"
@@ -28,6 +30,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
+#include <sys/sysctl.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
@@ -174,7 +177,66 @@ static NSString *VPDeviceUID(unsigned index) {
if ([uid isKindOfClass:NSString.class] && uid.length > 0) {
return index == 0 ? uid : [NSString stringWithFormat:@"%@:%u", uid, index];
}
- return [NSString stringWithFormat:@"VPhoneVirtIOSound:%u", 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
+ // 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.
+ return index == 0 ? @"PuffinOutput" : [NSString stringWithFormat:@"VPhoneVirtIOSound:%u", index];
+}
+
+/// The ProductID VirtualAudio routes an iPad guest with.
+///
+/// VirtualAudio derives its ProductID from a MobileGestalt class answer, and
+/// vphone600 lands on 196, a simulator class whose routing constructor skips
+/// the sub-port configurations an iPad's routes need: it throws, never
+/// initializes, and every session gets "VirtualAudio PlugIn is not
+/// initialized yet" — no sound anywhere. Its own defaults key,
+/// `ProductIDOverride` in `com.apple.audio.virtualaudio`, comes first, and
+/// 8010 is the one accepted ID whose category map also covers ringtone
+/// previews. This runs in audiomxd before VirtualAudio reads its defaults, so
+/// the key is set here, in the process, on every launch that finds none:
+/// audiomxd's sandbox keeps the write from reaching disk, and it does not need
+/// to. A value someone stored (`settings.set`) is their choice and stays.
+/// iPhone guests are left alone: none of the IDs tried so far initializes
+/// there.
+static void VPEnsureVirtualAudioProduct(void) {
+ char machine[64] = {0};
+ size_t size = sizeof(machine) - 1;
+ if (sysctlbyname("hw.machine", machine, &size, NULL, 0) != 0 || strncmp(machine, "iPad", 4) != 0) {
+ return;
+ }
+ CFStringRef domain = CFSTR("com.apple.audio.virtualaudio");
+ CFStringRef key = CFSTR("ProductIDOverride");
+ CFPropertyListRef existing = CFPreferencesCopyAppValue(key, domain);
+ if (existing) {
+ CFRelease(existing);
+ return;
+ }
+ int product = 8010;
+ CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &product);
+ CFPreferencesSetAppValue(key, value, domain);
+ CFRelease(value);
+ os_log(VPLog(), "ProductIDOverride was unset on %{public}s: %d for this launch", machine, product);
+ VPLogToFile("ProductIDOverride was unset on %s: %d for this launch", machine, product);
+}
+
+/// How many periods of silence go to the device before the mix at each start.
+static const uint32_t kDefaultLeadPeriods = 2;
+static const uint32_t kMaximumLeadPeriods = 8;
+
+static uint32_t VPLeadPeriods(void) {
+ CFPropertyListRef value = CFPreferencesCopyAppValue(CFSTR("VPhoneVirtIOSoundLeadPeriods"), kSettingsDomain);
+ int periods = (int)kDefaultLeadPeriods;
+ if (value && CFGetTypeID(value) == CFNumberGetTypeID()) {
+ CFNumberGetValue(value, kCFNumberIntType, &periods);
+ }
+ if (value) {
+ CFRelease(value);
+ }
+ if (periods < 0) {
+ return 0;
+ }
+ return (uint32_t)periods > kMaximumLeadPeriods ? kMaximumLeadPeriods : (uint32_t)periods;
}
/// The nominal rate the device boots with, for testing how VirtualAudio picks
@@ -199,11 +261,13 @@ static double VPNominalRateOverride(void) {
// MARK: - Clock
/// The free-running device clock. `performStartIO` anchors it; the I/O
-/// thread reads it through `getZeroTimestampBlock`. The period math is
-/// atomic because the nominal rate can move under it: the HAL runs the
-/// device at 44100 for ringtone-preview aggregates and back at 48000 for
-/// everything else, and the timeline must answer in the current rate's
-/// frames (`VPClockSetRate`).
+/// thread reads it through `getZeroTimestampBlock`. The nominal rate can move
+/// under it — the HAL runs the device at 44100 for ringtone previews and web
+/// audio and at 48000 otherwise — but the period stays the frame count the
+/// device published when the HAL first read it: the HAL keeps that number
+/// (it logs it as "Ring buffer size") and re-anchors its timeline on every
+/// timestamp that does not advance by exactly it. A rate change therefore
+/// moves only how long a period lasts (`VPClockSetRate`).
typedef struct {
_Atomic uint64_t anchorHostTime;
_Atomic uint64_t periodCount;
@@ -227,18 +291,17 @@ static void VPClockConfigure(VPClock *clock, double sampleRate) {
atomic_init(&clock->periodFrames, periodFrames);
}
-/// Re-derives the period math for a new nominal rate and re-anchors, so the
-/// timeline restarts in the new rate's frames. Rate changes arrive while
-/// I/O is stopped.
+/// Re-derives how long a period lasts at a new nominal rate and re-anchors.
+/// The period keeps its frame count. Rate changes arrive while I/O is
+/// stopped.
static void VPClockSetRate(VPClock *clock, double sampleRate) {
- uint32_t periodFrames = (UInt32)(sampleRate * kTimestampPeriodSeconds);
+ uint32_t periodFrames = atomic_load(&clock->periodFrames);
mach_timebase_info_data_t timebase;
mach_timebase_info(&timebase);
double hostTicksPerSecond = 1e9 * timebase.denom / timebase.numer;
double hostTicksPerPeriod = hostTicksPerSecond * periodFrames / sampleRate;
uint64_t bits;
memcpy(&bits, &hostTicksPerPeriod, sizeof(bits));
- atomic_store(&clock->periodFrames, periodFrames);
atomic_store(&clock->hostTicksPerPeriod, bits);
atomic_store(&clock->periodCount, 0);
atomic_store(&clock->anchorHostTime, mach_absolute_time());
@@ -262,14 +325,13 @@ static void VPClockZeroTimestamp(VPClock *clock, Float64 *sampleTime, UInt64 *ho
if (now > anchor && (double)(now - anchor) >= (count + 1) * hostTicksPerPeriod) {
count = (uint64_t)((now - anchor) / hostTicksPerPeriod);
atomic_store(&clock->periodCount, count);
- // The HAL drops an advancing timestamp whose seed did not move: a
- // constant seed reads as a frozen clock, and the IO context waits the
- // full five seconds and then fails the start with 'nope'. The seed
- // changes whenever the reported pair does.
- atomic_fetch_add(&clock->seed, 1);
}
*sampleTime = (Float64)count * periodFrames;
*hostTime = anchor + (UInt64)(count * hostTicksPerPeriod);
+ // The seed names the timeline, not the timestamp: it moves only when the
+ // clock is re-anchored. The HAL reads a changed seed as a discontinuity
+ // and re-anchors its own IO timeline, audibly, so a seed that moved with
+ // every period made every period a glitch.
*seed = atomic_load(&clock->seed);
}
@@ -334,12 +396,26 @@ typedef struct {
float *scratch;
uint32_t bytesPerFrame;
uint32_t wireRate;
+ /// Bytes the ring refused because the device had not returned the space.
+ _Atomic uint64_t *dropped;
+ /// Frames the HAL handed over, and bytes that went into the ring.
+ _Atomic uint64_t *framesIn;
+ _Atomic uint64_t *bytesOut;
} VPMixState;
+static void VPMixRingWrite(VPMixState *mix, const void *bytes, uint32_t length) {
+ if (VPVirtIOSoundRingWrite(mix->ring, bytes, length)) {
+ atomic_fetch_add_explicit(mix->bytesOut, length, memory_order_relaxed);
+ } else {
+ atomic_fetch_add_explicit(mix->dropped, length, memory_order_relaxed);
+ }
+}
+
static int VPMixWrite(VPMixState *mix, void *buffer, UInt32 frameCount) {
+ atomic_fetch_add_explicit(mix->framesIn, frameCount, memory_order_relaxed);
uint32_t halRate = atomic_load(mix->halRate);
if (halRate == mix->wireRate) {
- VPVirtIOSoundRingWrite(mix->ring, buffer, frameCount * mix->bytesPerFrame);
+ VPMixRingWrite(mix, buffer, frameCount * mix->bytesPerFrame);
return kAudioHardwareNoError;
}
uint32_t done = 0;
@@ -351,7 +427,7 @@ static int VPMixWrite(VPMixState *mix, void *buffer, UInt32 frameCount) {
uint32_t converted = VPResamplerProcess(mix->resampler,
(const float *)((uint8_t *)buffer + done * mix->bytesPerFrame), chunk,
halRate, mix->wireRate, mix->scratch, kResampleScratchFrames);
- VPVirtIOSoundRingWrite(mix->ring, mix->scratch, converted * mix->bytesPerFrame);
+ VPMixRingWrite(mix, mix->scratch, converted * mix->bytesPerFrame);
done += chunk;
}
return kAudioHardwareNoError;
@@ -373,6 +449,9 @@ typedef NS_ENUM(NSInteger, VPStreamState) {
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
@@ -404,12 +483,41 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
dispatch_source_t _timer;
VPStreamState _state;
BOOL _reportedTransferError;
+ /// Periods of silence queued ahead of the mix at each device start.
+ uint32_t _leadPeriods;
+ void *_silence;
+ /// Since the last device start: writes handed to the device, how many of
+ /// them found it with nothing left to play, and bytes the ring refused.
+ uint64_t _submissions;
+ uint64_t _starved;
+ _Atomic uint64_t _dropped;
+ _Atomic uint64_t _framesIn;
+ _Atomic uint64_t _bytesOut;
+ uint64_t _startedAt;
/// The rate CoreAudio runs the stream at, which is the wire rate until a
/// 44100 aggregate switches the device. The mix block reads it, the rate
/// change writes it, both lock-free.
_Atomic uint32_t _halRate;
VPResampler _resampler;
float *_resampleScratch;
+ VPMixState _mix;
+}
+
+/// ASDStream's `stopStream` releases every I/O block the stream holds and
+/// `startStream` copies them again from the same properties, so a block set
+/// once at init is gone after the first stop: the next start runs with no
+/// write block and nothing reaches the ring. It goes back in before every
+/// start. The I/O thread must not touch Objective-C or take locks, so the
+/// block captures plain state; the stream lives as long as its device.
+- (void)installMixBlock {
+ VPMixState *mix = &_mix;
+ self.writeMixBlock = ^int(UInt32 frameCount, const AudioServerPlugInIOCycleInfo *cycleInfo,
+ void *mainBuffer, void *secondaryBuffer, UInt32 clientID) {
+ (void)cycleInfo;
+ (void)secondaryBuffer;
+ (void)clientID;
+ return VPMixWrite(mix, mainBuffer, frameCount);
+ };
}
- (instancetype)initWithConnection:(io_connect_t)connection
@@ -435,6 +543,8 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
return nil;
}
IONotificationPortSetDispatchQueue(_port, _queue);
+ _leadPeriods = VPLeadPeriods();
+ _silence = calloc(1, _periodBytes);
_resampleScratch = malloc(kResampleScratchFrames * format.bytesPerFrame);
if (!_resampleScratch) {
IONotificationPortDestroy(_port);
@@ -482,23 +592,18 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
atomic_init(&_halRate, nominalRate > 0 ? (uint32_t)nominalRate : (uint32_t)format.sampleRate);
VPResamplerReset(&_resampler);
- // The I/O thread must not touch Objective-C or take locks, so the block
- // captures plain state. The stream lives as long as its device.
- VPMixState mix = {
+ _mix = (VPMixState){
.ring = &_ring,
.halRate = &_halRate,
.resampler = &_resampler,
.scratch = _resampleScratch,
.bytesPerFrame = format.bytesPerFrame,
.wireRate = (uint32_t)format.sampleRate,
+ .dropped = &_dropped,
+ .framesIn = &_framesIn,
+ .bytesOut = &_bytesOut,
};
- self.writeMixBlock = ^int(UInt32 frameCount, const AudioServerPlugInIOCycleInfo *cycleInfo,
- void *mainBuffer, void *secondaryBuffer, UInt32 clientID) {
- (void)cycleInfo;
- (void)secondaryBuffer;
- (void)clientID;
- return VPMixWrite((VPMixState *)&mix, mainBuffer, frameCount);
- };
+ [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);
@@ -515,6 +620,7 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
if (_port) {
IONotificationPortDestroy(_port);
}
+ free(_silence);
free(_resampleScratch);
VPVirtIOSoundRingDestroy(&_ring);
}
@@ -615,9 +721,41 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
return NO;
}
_reportedTransferError = NO;
+ _submissions = 0;
+ _starved = 0;
+ atomic_store(&_dropped, 0);
+ atomic_store(&_framesIn, 0);
+ atomic_store(&_bytesOut, 0);
+ _startedAt = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
+ [self queueLead];
return YES;
}
+/// The device plays what it is handed as it arrives and the mix arrives in
+/// real time, so with nothing queued ahead every late period is a gap on the
+/// host: measured, a third to a half of all writes found the device with
+/// nothing left to play. A few periods of silence first put that much audio
+/// between the guest's writes and the host's playback. A restart that finds
+/// the device still draining tops up what the drain consumed.
+- (void)queueLead {
+ dispatch_assert_queue(_queue);
+ if (!_silence) {
+ return;
+ }
+ uint32_t queued = (uint32_t)(VPVirtIOSoundRingInFlight(&_ring) / _periodBytes);
+ BOOL wrote = NO;
+ for (uint32_t period = queued; period < _leadPeriods; period++) {
+ wrote = VPVirtIOSoundRingWrite(&_ring, _silence, _periodBytes) || wrote;
+ }
+ if (wrote) {
+ [self submitPending:YES];
+ }
+}
+
+- (UInt32)queuedLatencyFrames {
+ return (_leadPeriods + 1) * (_periodBytes / _format.bytesPerFrame);
+}
+
- (void)stopDevice {
dispatch_assert_queue(_queue);
[self callSelector:kVPVirtIOSoundSelectorStop];
@@ -643,6 +781,12 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
reference[kIOAsyncCalloutFuncIndex] = (uint64_t)(uintptr_t)VPWriteCompleted;
reference[kIOAsyncCalloutRefconIndex] = (uint64_t)(uintptr_t)transfer;
uint64_t scalar = _streamID;
+ // Nothing in flight when a period goes out means the device already
+ // played everything it had: a gap on the host.
+ if (_submissions > 0 && VPVirtIOSoundRingInFlight(&_ring) == 0) {
+ _starved++;
+ }
+ _submissions++;
VPVirtIOSoundRingDidSubmit(&_ring, length);
kern_return_t result = IOConnectCallAsyncMethod(_connection, kVPVirtIOSoundSelectorWrite, wakePort,
reference, kAsyncReferenceCount, &scalar, 1, _ring.bytes + offset, length, NULL, NULL, NULL, NULL);
@@ -670,8 +814,14 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
- (void)startStream {
dispatch_sync(_queue, ^{
- if (self->_state == VPStreamStateIdle && ![self startDevice]) {
- return;
+ VPLogToFile("stream %u: startStream in state %d, %llu bytes in flight", self->_streamID,
+ (int)self->_state, (unsigned long long)VPVirtIOSoundRingInFlight(&self->_ring));
+ if (self->_state == VPStreamStateIdle) {
+ if (![self startDevice]) {
+ return;
+ }
+ } else if (self->_state == VPStreamStateDraining) {
+ [self queueLead];
}
self->_state = VPStreamStateRunning;
});
@@ -687,11 +837,23 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI
});
dispatch_resume(_timer);
}
+ [self installMixBlock];
[super startStream];
}
- (void)stopStream {
dispatch_sync(_queue, ^{
+ VPLogToFile("stream %u: stopStream in state %d, %llu bytes in flight", self->_streamID,
+ (int)self->_state, (unsigned long long)VPVirtIOSoundRingInFlight(&self->_ring));
+ VPLogToFile("stream %u: %llu writes, %llu starved, %llu bytes dropped, lead %u period(s)", self->_streamID,
+ (unsigned long long)self->_submissions, (unsigned long long)self->_starved,
+ (unsigned long long)atomic_load(&self->_dropped), self->_leadPeriods);
+ double seconds = (double)(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - self->_startedAt) / 1e9;
+ uint64_t framesIn = atomic_load(&self->_framesIn);
+ uint64_t framesOut = atomic_load(&self->_bytesOut) / self->_format.bytesPerFrame;
+ VPLogToFile("stream %u: %.2f s, in %llu frames (%.0f/s, hal %u), out %llu frames (%.0f/s)", self->_streamID,
+ seconds, (unsigned long long)framesIn, seconds > 0 ? framesIn / seconds : 0, atomic_load(&self->_halRate),
+ (unsigned long long)framesOut, seconds > 0 ? framesOut / seconds : 0);
if (self->_state != VPStreamStateRunning) {
return;
}
@@ -764,6 +926,7 @@ static uint32_t VPStreamCount(io_service_t service) {
_connection = connection;
double sampleRate = 0;
+ UInt32 latencyFrames = 0;
uint32_t count = VPStreamCount(service);
for (uint32_t streamID = 0; streamID < count; streamID++) {
VPVirtIOSoundPCMInfo info = {0};
@@ -796,6 +959,7 @@ static uint32_t VPStreamCount(io_service_t service) {
continue;
}
sampleRate = format.sampleRate;
+ latencyFrames = stream.queuedLatencyFrames;
[self addOutputStream:stream];
}
if (sampleRate == 0) {
@@ -826,9 +990,40 @@ static uint32_t VPStreamCount(io_service_t service) {
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.
+ self.outputLatency = latencyFrames;
self.transportType = VPTransportType();
self.timestampPeriod = 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,
+ (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,
+ (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
+/// once at init therefore survive exactly one start: after the first stop
+/// the device has no zero-timestamp block, the HAL reads "Device … is not
+/// running", waits five seconds for a timeline and fails the start — every
+/// start after the first. They go back in before every start.
+- (void)installIOBlocks {
VPClock *clock = &_clock;
self.getZeroTimestampBlock = ^int(Float64 *sampleTime, UInt64 *hostTime, UInt64 *seed, UInt32 clientID) {
(void)clientID;
@@ -847,30 +1042,21 @@ static uint32_t VPStreamCount(io_service_t service) {
*willDoInPlace = true;
return kAudioHardwareNoError;
};
- 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,
- (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,
- (char)(transport >> 24), (char)(transport >> 16), (char)(transport >> 8), (char)transport);
- return self;
-}
-
-- (void)dealloc {
- if (_connection) {
- IOServiceClose(_connection);
- }
- if (_service) {
- IOObjectRelease(_service);
- }
}
- (int)performStartIO {
+ [self installIOBlocks];
int result = [super performStartIO];
if (result == kAudioHardwareNoError) {
VPClockAnchor(&_clock);
}
+ VPLogToFile("performStartIO -> %d (clock %.0f)", result, _clockRate);
+ return result;
+}
+
+- (int)performStopIO {
+ int result = [super performStopIO];
+ VPLogToFile("performStopIO -> %d", result);
return result;
}
@@ -895,7 +1081,6 @@ static uint32_t VPStreamCount(io_service_t service) {
if (rate > 0 && [self supportsSamplingRate:rate] && rate != _clockRate) {
_clockRate = rate;
VPClockSetRate(&_clock, rate);
- self.timestampPeriod = (UInt32)(rate * kTimestampPeriodSeconds);
os_log(VPLog(), "nominal rate -> %.0f Hz", rate);
}
}
@@ -1146,6 +1331,7 @@ static BOOL VPIsDeviceRateAddress(const AudioObjectPropertyAddress *address) {
- (void)halInitializeWithPluginHost:(AudioServerPlugInHostRef)host {
[super halInitializeWithPluginHost:host];
+ VPEnsureVirtualAudioProduct();
io_iterator_t services = IO_OBJECT_NULL;
kern_return_t result = IOServiceGetMatchingServices(
kIOMainPortDefault, IOServiceMatching("AppleVirtIOSound"), &services);