Linux + iPhone Continuity / iMessage / SMS
fix: connection lifecycle
Zack Barteld8bb5dd1c692
diff --git a/apple/Tether/ViewModels/TetherViewModel.swift b/apple/Tether/ViewModels/TetherViewModel.swiftindex b5d6a6a..179d51a 100644--- a/apple/Tether/ViewModels/TetherViewModel.swift+++ b/apple/Tether/ViewModels/TetherViewModel.swift@@ -94,7 +94,10 @@ final class TetherViewModel {}// Overall connection state.- private(set) var appState: AppConnectionState = .disconnected+ private(set) var appState: AppConnectionState = .disconnected {+ // The Camera tab only offers Stop while connected.+ didSet { if appState != .connected { camera.stop() } }+ }// Name of the connected device, if any.private(set) var connectedDeviceName: String?@@ -863,8 +866,16 @@ final class TetherViewModel {errorMessage = identityErrorMessagereturn}- camera.start(host: host, port: port, pinnedFingerprint: connection.serverFingerprint,- identity: identity, position: position)+ let fingerprint = connection.serverFingerprint+ Task {+ // Without this, a denied permission surfaces as "Camera unavailable".+ guard await AVCaptureDevice.requestAccess(for: .video) else {+ errorMessage = "Allow camera access for Tether in Settings to stream."+ return+ }+ camera.start(host: host, port: port, pinnedFingerprint: fingerprint,+ identity: identity, position: position)+ }}private func finalizeIncomingTransfer(transferId: String) {diff --git a/apple/TetherFramework/Sources/TetherFramework/CameraStreamer.swift b/apple/TetherFramework/Sources/TetherFramework/CameraStreamer.swiftindex dfce0d7..05f29ba 100644--- a/apple/TetherFramework/Sources/TetherFramework/CameraStreamer.swift+++ b/apple/TetherFramework/Sources/TetherFramework/CameraStreamer.swift@@ -112,7 +112,6 @@ public final class CameraStreamer: NSObject {session.beginConfiguration()session.inputs.forEach { session.removeInput($0) }session.outputs.forEach { session.removeOutput($0) }- session.sessionPreset = .hd1920x1080guard let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: position),let input = try? AVCaptureDeviceInput(device: device),@@ -123,6 +122,8 @@ public final class CameraStreamer: NSObject {return}session.addInput(input)+ // Older front cameras top out below 1080p.+ session.sessionPreset = session.canSetSessionPreset(.hd1920x1080) ? .hd1920x1080 : .highlet output = AVCaptureVideoDataOutput()output.videoSettings = [@@ -169,9 +170,11 @@ public final class CameraStreamer: NSObject {return session}- private func encoded(_ status: OSStatus, _ sample: CMSampleBuffer?) {+ // `conn` is the connection the frame was encoded for; frames finishing after+ // it was torn down are dropped.+ private func encoded(_ status: OSStatus, _ sample: CMSampleBuffer?, for conn: NWConnection) {guard status == noErr, let sample, let payload = Self.annexB(sample) else {- queue.async { self.pending -= 1 }+ queue.async { if self.connection === conn { self.pending -= 1 } }return}let keyframe = Self.isKeyframe(sample)@@ -185,9 +188,10 @@ public final class CameraStreamer: NSObject {frame.append(payload)queue.async {- guard let conn = self.connection else { return }+ guard self.connection === conn else { return }conn.send(content: frame, completion: .contentProcessed { [weak self] error in- guard let self else { return }+ // Cancelling a connection fails its queued sends; that is not an error.+ guard let self, self.connection === conn else { return }self.pending -= 1if let error { self.fail(error.localizedDescription) }})@@ -253,7 +257,7 @@ extension CameraStreamer: AVCaptureVideoDataOutputSampleBufferDelegate {didOutput sampleBuffer: CMSampleBuffer,from connection: AVCaptureConnection) {- guard self.connection != nil, let image = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }+ guard let conn = self.connection, let image = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }// Skip frames before encoding while the network is behind, so no// encoded frame is ever lost and the decoder never needs a fresh keyframe.guard pending < Self.maxPending else { return }@@ -276,7 +280,7 @@ extension CameraStreamer: AVCaptureVideoDataOutputSampleBufferDelegate {frameProperties: nil,infoFlagsOut: nil) { [weak self] status, _, sample in- self?.encoded(status, sample)+ self?.encoded(status, sample, for: conn)}if status != noErr { pending -= 1 }}diff --git a/apple/TetherFramework/Tests/TetherFrameworkTests/CameraStreamerTests.swift b/apple/TetherFramework/Tests/TetherFrameworkTests/CameraStreamerTests.swiftnew file mode 100644index 0000000..e89bbfb--- /dev/null+++ b/apple/TetherFramework/Tests/TetherFrameworkTests/CameraStreamerTests.swift@@ -0,0 +1,68 @@+//+// CameraStreamerTests.swift+// TetherFrameworkTests+//++import CoreMedia+import Foundation+import Testing+@testable import TetherFramework++struct CameraStreamerTests {+ // A real 128x96 baseline SPS/PPS; CoreMedia parses them.+ private let sps: [UInt8] = [0x67, 0x42, 0x00, 0x0A, 0xF8, 0x41, 0xA2]+ private let pps: [UInt8] = [0x68, 0xCE, 0x38, 0x80]++ // VideoToolbox emits AVCC (4-byte lengths); the daemon's decoder wants Annex B.+ private func sample(avcc: [UInt8], keyframe: Bool) throws -> CMSampleBuffer {+ var format: CMFormatDescription?+ let status = sps.withUnsafeBufferPointer { s in+ pps.withUnsafeBufferPointer { p in+ let pointers = [s.baseAddress!, p.baseAddress!]+ let sizes = [sps.count, pps.count]+ return CMVideoFormatDescriptionCreateFromH264ParameterSets(+ allocator: nil, parameterSetCount: 2, parameterSetPointers: pointers,+ parameterSetSizes: sizes, nalUnitHeaderLength: 4, formatDescriptionOut: &format)+ }+ }+ try #require(status == noErr)++ var block: CMBlockBuffer?+ try #require(CMBlockBufferCreateWithMemoryBlock(+ allocator: nil, memoryBlock: nil, blockLength: avcc.count, blockAllocator: nil,+ customBlockSource: nil, offsetToData: 0, dataLength: avcc.count, flags: 0,+ blockBufferOut: &block) == noErr)+ try #require(CMBlockBufferReplaceDataBytes(+ with: avcc, blockBuffer: block!, offsetIntoDestination: 0, dataLength: avcc.count) == noErr)++ var out: CMSampleBuffer?+ var size = avcc.count+ try #require(CMSampleBufferCreateReady(+ allocator: nil, dataBuffer: block, formatDescription: format, sampleCount: 1,+ sampleTimingEntryCount: 0, sampleTimingArray: nil, sampleSizeEntryCount: 1,+ sampleSizeArray: &size, sampleBufferOut: &out) == noErr)+ let sample = try #require(out)++ let attachments = CMSampleBufferGetSampleAttachmentsArray(sample, createIfNecessary: true)! as NSArray+ let dict = attachments[0] as! NSMutableDictionary+ dict[kCMSampleAttachmentKey_NotSync] = !keyframe+ return sample+ }++ @Test func keyframeGetsParameterSetsAndStartCodes() throws {+ let nal: [UInt8] = [0x65, 0xAA, 0xBB]+ let payload = try #require(CameraStreamer.annexB(try sample(avcc: [0, 0, 0, 3] + nal, keyframe: true)))+ let start: [UInt8] = [0, 0, 0, 1]+ #expect(Array(payload) == start + sps + start + pps + start + nal)+ }++ @Test func deltaFrameSplitsEveryNal() throws {+ let avcc: [UInt8] = [0, 0, 0, 2, 0x41, 0x01, 0, 0, 0, 1, 0x06]+ let payload = try #require(CameraStreamer.annexB(try sample(avcc: avcc, keyframe: false)))+ #expect(Array(payload) == [0, 0, 0, 1, 0x41, 0x01, 0, 0, 0, 1, 0x06])+ }++ @Test func truncatedNalIsRejected() throws {+ #expect(CameraStreamer.annexB(try sample(avcc: [0, 0, 0, 9, 0x41], keyframe: false)) == nil)+ }+}diff --git a/src/core/src/net.cpp b/src/core/src/net.cppindex 3b13815..a8418f7 100644--- a/src/core/src/net.cpp+++ b/src/core/src/net.cpp@@ -2533,6 +2533,13 @@ namespace tether {remote_it->second.paired = true;remote_it->second.device_name = device_name;remote_it->second.fingerprint = fingerprint;+ // A second connection (the camera stream) sends no hello; keep the kind its device announced.+ for (const auto& [fd, other] : connected_remote_clients) {+ if (fd != client_fd && other.fingerprint == fingerprint) {+ remote_it->second.device = other.device;+ break;+ }+ }// Only a trusted session joins the broadcast registry.register_client_ssl(client_fd, ssl_it->second, fingerprint);