Retire legacy sources and verify managed iOS deployment
This commit is contained in:
@@ -1,178 +0,0 @@
|
||||
import Foundation
|
||||
import Darwin
|
||||
|
||||
// Darwin notification names the extension posts and the host observes, so the host can react to
|
||||
// broadcast start/stop promptly instead of only polling the ring's active flag. Shared (compiled
|
||||
// into both targets) so the names can't drift.
|
||||
enum BroadcastNotification {
|
||||
static let started = "cat.voice.VoiceCat.broadcast.started"
|
||||
static let finished = "cat.voice.VoiceCat.broadcast.finished"
|
||||
}
|
||||
|
||||
// BroadcastAudioRing — cross-process single-producer/single-consumer int16 PCM ring over an
|
||||
// mmap'd file in the shared App Group container. Compiled into BOTH the host app and the
|
||||
// ReplayKit broadcast upload extension (docs/voice.md §9, iOS detail).
|
||||
//
|
||||
// producer = the broadcast extension's RPBroadcastSampleHandler (captured system audio)
|
||||
// consumer = the host app's BroadcastAudioPump (drains and feeds the core via feedPcm)
|
||||
//
|
||||
// Why a hand-rolled ring instead of Swift's `Atomic`: the storage lives in shared memory mapped
|
||||
// into two processes, so the synchronization words must sit in that mapping — Swift's managed
|
||||
// atomics can't. For strict SPSC, aligned 64-bit monotonic indices with full memory barriers
|
||||
// (`OSMemoryBarrier`) give correct acquire/release ordering. The extension does NOT link
|
||||
// libvoicecat — it only writes PCM here; all encode/crypto/UDP happens in the host.
|
||||
//
|
||||
// Lifecycle: the host opens the ring at connect (and thus initializes the header first); the
|
||||
// extension opens it later when a broadcast starts. On a rising edge of `isActive` the host
|
||||
// calls `drainStale()` to discard any pre-roll, then drains in whole 20 ms frames.
|
||||
final class BroadcastAudioRing {
|
||||
|
||||
static let appGroupId = "group.me.iamtalon.voicecat"
|
||||
|
||||
enum RingError: Error { case noContainer, openFailed, mapFailed }
|
||||
|
||||
// Header layout (byte offsets into the mmap). Indices are 8-byte aligned; mmap is
|
||||
// page-aligned so offsets 24/32 satisfy that.
|
||||
private static let magic: UInt32 = 0x5643_4252 // "VCBR"
|
||||
private static let version: UInt32 = 1
|
||||
private static let headerBytes = 64
|
||||
/// 1 second of 48 kHz stereo int16 — ample slack for ~10 ms host drains.
|
||||
static let capacitySamples = 48_000 * 2
|
||||
|
||||
private static let offMagic = 0
|
||||
private static let offVersion = 4
|
||||
private static let offChannels = 8
|
||||
private static let offSampleRate = 12
|
||||
private static let offActive = 16
|
||||
private static let offWrite = 24
|
||||
private static let offRead = 32
|
||||
|
||||
private let fd: Int32
|
||||
private let mapBase: UnsafeMutableRawPointer
|
||||
private let mapSize: Int
|
||||
private let data: UnsafeMutablePointer<Int16>
|
||||
private let capacity: Int
|
||||
|
||||
init() throws {
|
||||
guard let container = FileManager.default.containerURL(
|
||||
forSecurityApplicationGroupIdentifier: Self.appGroupId) else {
|
||||
throw RingError.noContainer
|
||||
}
|
||||
let dir = container.appendingPathComponent("voicecat", isDirectory: true)
|
||||
try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
||||
let path = dir.appendingPathComponent("broadcast_audio.ring").path
|
||||
|
||||
capacity = Self.capacitySamples
|
||||
mapSize = Self.headerBytes + capacity * MemoryLayout<Int16>.size
|
||||
|
||||
let f = open(path, O_RDWR | O_CREAT, 0o644)
|
||||
guard f >= 0 else { throw RingError.openFailed }
|
||||
if ftruncate(f, off_t(mapSize)) != 0 { close(f); throw RingError.openFailed }
|
||||
|
||||
let p = mmap(nil, mapSize, PROT_READ | PROT_WRITE, MAP_SHARED, f, 0)
|
||||
guard let p, p != MAP_FAILED else { close(f); throw RingError.mapFailed }
|
||||
|
||||
fd = f
|
||||
mapBase = p
|
||||
data = (p + Self.headerBytes).assumingMemoryBound(to: Int16.self)
|
||||
|
||||
// First opener initializes the header (host opens first, before any broadcast).
|
||||
if load32(Self.offMagic) != Self.magic {
|
||||
store32(Self.offWrite, 0); store32(Self.offWrite + 4, 0)
|
||||
store32(Self.offRead, 0); store32(Self.offRead + 4, 0)
|
||||
store32(Self.offChannels, 0)
|
||||
store32(Self.offSampleRate, 0)
|
||||
store32(Self.offActive, 0)
|
||||
store32(Self.offVersion, Self.version)
|
||||
OSMemoryBarrier()
|
||||
store32(Self.offMagic, Self.magic)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
munmap(mapBase, mapSize)
|
||||
close(fd)
|
||||
}
|
||||
|
||||
// MARK: - Header accessors
|
||||
|
||||
var isActive: Bool { OSMemoryBarrier(); return load32(Self.offActive) != 0 }
|
||||
var channels: UInt32 { load32(Self.offChannels) }
|
||||
var sampleRate: UInt32 { load32(Self.offSampleRate) }
|
||||
|
||||
/// Producer: advertise the canonical capture format and toggle the active flag. Called from
|
||||
/// `broadcastStarted`/`broadcastFinished`.
|
||||
func setActive(_ active: Bool, channels: UInt32 = 0, sampleRate: UInt32 = 0) {
|
||||
if active {
|
||||
store32(Self.offChannels, channels)
|
||||
store32(Self.offSampleRate, sampleRate)
|
||||
}
|
||||
OSMemoryBarrier()
|
||||
store32(Self.offActive, active ? 1 : 0)
|
||||
}
|
||||
|
||||
// MARK: - Producer (extension)
|
||||
|
||||
/// Append interleaved int16 samples. Drops the whole chunk if it doesn't fit — skipping a
|
||||
/// chunk is better than tearing a frame. Single producer only.
|
||||
func push(_ samples: UnsafeBufferPointer<Int16>) {
|
||||
let n = samples.count
|
||||
guard n > 0, n <= capacity, let src = samples.baseAddress else { return }
|
||||
let w = load64(Self.offWrite) // producer owns the write index
|
||||
let r = loadAcquire64(Self.offRead)
|
||||
if capacity - Int(w &- r) < n { return } // full: drop
|
||||
var idx = Int(w % UInt64(capacity))
|
||||
var off = 0
|
||||
var rem = n
|
||||
while rem > 0 {
|
||||
let chunk = min(rem, capacity - idx)
|
||||
(data + idx).update(from: src + off, count: chunk)
|
||||
idx = (idx + chunk) % capacity
|
||||
off += chunk
|
||||
rem -= chunk
|
||||
}
|
||||
storeRelease64(Self.offWrite, w &+ UInt64(n))
|
||||
}
|
||||
|
||||
// MARK: - Consumer (host)
|
||||
|
||||
/// Read up to `out.count` interleaved int16 samples. Returns the number read. Single
|
||||
/// consumer only.
|
||||
func read(into out: UnsafeMutableBufferPointer<Int16>) -> Int {
|
||||
guard let dst = out.baseAddress else { return 0 }
|
||||
let r = load64(Self.offRead) // consumer owns the read index
|
||||
let w = loadAcquire64(Self.offWrite)
|
||||
let available = Int(w &- r)
|
||||
if available <= 0 { return 0 }
|
||||
let n = min(available, out.count)
|
||||
var idx = Int(r % UInt64(capacity))
|
||||
var off = 0
|
||||
var rem = n
|
||||
while rem > 0 {
|
||||
let chunk = min(rem, capacity - idx)
|
||||
(dst + off).update(from: data + idx, count: chunk)
|
||||
idx = (idx + chunk) % capacity
|
||||
off += chunk
|
||||
rem -= chunk
|
||||
}
|
||||
storeRelease64(Self.offRead, r &+ UInt64(n))
|
||||
return n
|
||||
}
|
||||
|
||||
/// Consumer: discard everything currently buffered (catch the read index up to write).
|
||||
func drainStale() { storeRelease64(Self.offRead, loadAcquire64(Self.offWrite)) }
|
||||
|
||||
// MARK: - Memory-ordered accessors
|
||||
|
||||
private func ptr32(_ off: Int) -> UnsafeMutablePointer<UInt32> {
|
||||
(mapBase + off).assumingMemoryBound(to: UInt32.self)
|
||||
}
|
||||
private func ptr64(_ off: Int) -> UnsafeMutablePointer<UInt64> {
|
||||
(mapBase + off).assumingMemoryBound(to: UInt64.self)
|
||||
}
|
||||
private func load32(_ off: Int) -> UInt32 { ptr32(off).pointee }
|
||||
private func store32(_ off: Int, _ v: UInt32) { ptr32(off).pointee = v }
|
||||
private func load64(_ off: Int) -> UInt64 { ptr64(off).pointee }
|
||||
private func loadAcquire64(_ off: Int) -> UInt64 { let v = ptr64(off).pointee; OSMemoryBarrier(); return v }
|
||||
private func storeRelease64(_ off: Int, _ v: UInt64) { OSMemoryBarrier(); ptr64(off).pointee = v }
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDisplayName</key>
|
||||
<string>VoiceCat Screen Audio</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>0.0.1</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>NSExtension</key>
|
||||
<dict>
|
||||
<key>NSExtensionPointIdentifier</key>
|
||||
<string>com.apple.broadcast-services-upload</string>
|
||||
<key>NSExtensionPrincipalClass</key>
|
||||
<string>$(PRODUCT_MODULE_NAME).SampleHandler</string>
|
||||
<key>RPBroadcastProcessMode</key>
|
||||
<string>RPBroadcastProcessModeSampleBuffer</string>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -1,88 +0,0 @@
|
||||
import ReplayKit
|
||||
import AVFoundation
|
||||
|
||||
// SampleHandler — the ReplayKit broadcast upload extension entry point (docs/voice.md §9, iOS).
|
||||
//
|
||||
// This runs in a SEPARATE process with a ~50 MB memory cap. It captures system/app audio
|
||||
// (`.audioApp`), drops video and mic buffers, converts each chunk to the core's canonical
|
||||
// format (48 kHz, int16, stereo interleaved) with AVAudioConverter, and writes it into the
|
||||
// App Group shared-memory ring. The host app's BroadcastAudioPump drains the ring and feeds the
|
||||
// already-connected VoiceCatClient — so all Opus/AEAD/UDP work happens in the host, and the
|
||||
// extension stays tiny and well inside the memory budget (no libvoicecat here).
|
||||
class SampleHandler: RPBroadcastSampleHandler {
|
||||
|
||||
private var ring: BroadcastAudioRing?
|
||||
private var converter: AVAudioConverter?
|
||||
private var inputFormat: AVAudioFormat?
|
||||
private let outputFormat = AVAudioFormat(commonFormat: .pcmFormatInt16,
|
||||
sampleRate: 48_000, channels: 2, interleaved: true)!
|
||||
|
||||
override func broadcastStarted(withSetupInfo setupInfo: [String: NSObject]?) {
|
||||
ring = try? BroadcastAudioRing()
|
||||
ring?.setActive(true, channels: 2, sampleRate: 48_000)
|
||||
postDarwin(BroadcastNotification.started)
|
||||
}
|
||||
|
||||
override func broadcastFinished() {
|
||||
ring?.setActive(false)
|
||||
postDarwin(BroadcastNotification.finished)
|
||||
ring = nil
|
||||
}
|
||||
|
||||
override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer,
|
||||
with sampleBufferType: RPSampleBufferType) {
|
||||
// App/system audio only — drop video (the memory hog) and the device mic (the host
|
||||
// already captures and sends the user's voice).
|
||||
guard sampleBufferType == .audioApp, let ring else { return }
|
||||
guard let input = makeInputBuffer(sampleBuffer),
|
||||
let conv = converter(for: input.format) else { return }
|
||||
|
||||
let ratio = outputFormat.sampleRate / input.format.sampleRate
|
||||
let capacity = AVAudioFrameCount(Double(input.frameLength) * ratio) + 1024
|
||||
guard let output = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: capacity) else { return }
|
||||
|
||||
var supplied = false
|
||||
var error: NSError?
|
||||
let status = conv.convert(to: output, error: &error) { _, outStatus in
|
||||
if supplied { outStatus.pointee = .noDataNow; return nil }
|
||||
supplied = true
|
||||
outStatus.pointee = .haveData
|
||||
return input
|
||||
}
|
||||
guard status != .error, output.frameLength > 0,
|
||||
let mData = output.audioBufferList.pointee.mBuffers.mData else { return }
|
||||
|
||||
// Interleaved int16 → one buffer of frameLength * channels samples.
|
||||
let count = Int(output.frameLength) * Int(outputFormat.channelCount)
|
||||
ring.push(UnsafeBufferPointer(start: mData.assumingMemoryBound(to: Int16.self), count: count))
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
/// Wrap the ReplayKit CMSampleBuffer's PCM in an AVAudioPCMBuffer matching its native format.
|
||||
private func makeInputBuffer(_ sb: CMSampleBuffer) -> AVAudioPCMBuffer? {
|
||||
guard let fmtDesc = CMSampleBufferGetFormatDescription(sb),
|
||||
var asbd = CMAudioFormatDescriptionGetStreamBasicDescription(fmtDesc)?.pointee,
|
||||
let fmt = AVAudioFormat(streamDescription: &asbd) else { return nil }
|
||||
let frames = AVAudioFrameCount(CMSampleBufferGetNumSamples(sb))
|
||||
guard frames > 0, let buf = AVAudioPCMBuffer(pcmFormat: fmt, frameCapacity: frames) else { return nil }
|
||||
buf.frameLength = frames
|
||||
let status = CMSampleBufferCopyPCMDataIntoAudioBufferList(
|
||||
sb, at: 0, frameCount: Int32(frames), into: buf.mutableAudioBufferList)
|
||||
return status == noErr ? buf : nil
|
||||
}
|
||||
|
||||
/// Reuse the converter while the input format is stable; rebuild if ReplayKit changes it.
|
||||
private func converter(for inFmt: AVAudioFormat) -> AVAudioConverter? {
|
||||
if let converter, inputFormat == inFmt { return converter }
|
||||
inputFormat = inFmt
|
||||
converter = AVAudioConverter(from: inFmt, to: outputFormat)
|
||||
return converter
|
||||
}
|
||||
|
||||
private func postDarwin(_ name: String) {
|
||||
CFNotificationCenterPostNotification(
|
||||
CFNotificationCenterGetDarwinNotifyCenter(),
|
||||
CFNotificationName(name as CFString), nil, nil, true)
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>com.apple.security.application-groups</key>
|
||||
<array>
|
||||
<string>group.me.iamtalon.voicecat</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 4.4 KiB |
@@ -1,14 +0,0 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"filename" : "AppIcon-1024.png",
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user