Linux + iPhone Continuity / iMessage / SMS
feat: groundwork for camera
Zack Bartel21169059651c
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 3a546de..a00fa91 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -61,6 +61,7 @@ set(CMAKE_WARN_DEPRECATED ON CACHE BOOL "" FORCE)enable_testing()option(TETHER_BUILD_TUI "Build the tether-tui terminal front end (needs ncursesw)" ON)+option(TETHER_CAMERA "Publish the iPhone camera as a PipeWire video source (needs libavcodec, libpipewire)" ON)# subdirectories for the targetsadd_subdirectory(src)diff --git a/PKGBUILD b/PKGBUILDindex c3a8df2..ab3054b 100644--- a/PKGBUILD+++ b/PKGBUILD@@ -5,7 +5,7 @@ pkgdesc="Bridge an iPhone to the Linux desktop: clipboard, files, messages, andarch=('x86_64' 'aarch64')url="https://github.com/zackb/tether"license=('MIT')-depends=('gtk3' 'gtk-layer-shell' 'libnotify' 'ncurses' 'openssl' 'wayland' 'avahi' 'glib2' 'libsecret' 'bluez' 'bluez-utils' 'bluez-obex')+depends=('gtk3' 'gtk-layer-shell' 'libnotify' 'ncurses' 'openssl' 'wayland' 'avahi' 'glib2' 'libsecret' 'bluez' 'bluez-utils' 'bluez-obex' 'ffmpeg' 'pipewire')makedepends=('cmake' 'ninja' 'git')provides=('tether' 'tether-bin')conflicts=('tether' 'tether-bin')diff --git a/apple/Info.plist b/apple/Info.plistindex 9b99c1e..badd3c6 100644--- a/apple/Info.plist+++ b/apple/Info.plist@@ -8,6 +8,8 @@<array><string>_tether._tcp</string></array>+ <key>NSCameraUsageDescription</key>+ <string>Used to stream the camera to your desktop as a webcam.</string><key>NSLocalNetworkUsageDescription</key><string>Used to discover and connect to Tether services on your local network.</string><key>NSPhotoLibraryUsageDescription</key>diff --git a/apple/Tether.xcodeproj/project.pbxproj b/apple/Tether.xcodeproj/project.pbxprojindex 20d9a0b..cdbf577 100644--- a/apple/Tether.xcodeproj/project.pbxproj+++ b/apple/Tether.xcodeproj/project.pbxproj@@ -525,6 +525,7 @@INFOPLIST_FILE = Info.plist;INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";INFOPLIST_KEY_LSSupportsOpeningDocumentsInPlace = YES;+ INFOPLIST_KEY_NSCameraUsageDescription = "Used to stream the camera to your desktop as a webcam.";INFOPLIST_KEY_NSLocalNetworkUsageDescription = "Used to discover and connect to Tether services on your local network.";INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Used to send photos from your library to your desktop.";"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;@@ -596,6 +597,7 @@INFOPLIST_FILE = Info.plist;INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";INFOPLIST_KEY_LSSupportsOpeningDocumentsInPlace = YES;+ INFOPLIST_KEY_NSCameraUsageDescription = "Used to stream the camera to your desktop as a webcam.";INFOPLIST_KEY_NSLocalNetworkUsageDescription = "Used to discover and connect to Tether services on your local network.";INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Used to send photos from your library to your desktop.";"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;diff --git a/apple/Tether/ContentView.swift b/apple/Tether/ContentView.swiftindex dc352ab..64354b4 100644--- a/apple/Tether/ContentView.swift+++ b/apple/Tether/ContentView.swift@@ -27,6 +27,10 @@ struct ContentView: View {FilesView()}+ Tab("Camera", systemImage: "web.camera", value: AppTab.camera) {+ CameraView()+ }+Tab("Settings", systemImage: "gearshape", value: AppTab.settings) {SettingsView()}diff --git a/apple/Tether/ViewModels/TetherViewModel.swift b/apple/Tether/ViewModels/TetherViewModel.swiftindex dea647f..b5d6a6a 100644--- a/apple/Tether/ViewModels/TetherViewModel.swift+++ b/apple/Tether/ViewModels/TetherViewModel.swift@@ -9,6 +9,7 @@import Foundationimport SwiftUIimport UniformTypeIdentifiers+import AVFoundationimport UIKitimport TetherFrameworkinternal import Network@@ -27,6 +28,7 @@ enum AppTab: Hashable {case dashboardcase clipboardcase files+ case cameracase settings}@@ -101,6 +103,9 @@ final class TetherViewModel {// would save a sent image to Downloads instead of the clipboard.private(set) var daemonSupportsClipboardImages = false+ // The port to dial for a camera stream, when the daemon announced camera support.+ private(set) var daemonCameraPort: UInt16?+// Clipboard history (most recent first).private(set) var clipboardHistory: [ClipboardEntry] = []@@ -132,6 +137,7 @@ final class TetherViewModel {let discovery = BonjourDiscovery()let connection = TetherConnection()let server = TetherServer()+ let camera = CameraStreamer()// The chunk size for file transfers (48KB raw → 64KB base64).private let fileChunkSize = 48 * 1024@@ -221,6 +227,8 @@ final class TetherViewModel {pendingReconnectTask = nilconnectTimeoutTask?.cancel()connectTimeoutTask = nil+ // iOS takes the camera from background apps+ camera.stop()suspendForBackground()default:break@@ -736,6 +744,7 @@ final class TetherViewModel {case .hello:daemonSupportsClipboardImages = message.features?.contains("clipboard_image") ?? false+ daemonCameraPort = message.features?.contains("camera") == true ? message.port : nilcase .fileStatus:if let transferId = message.transferId, message.status == "success" {@@ -840,9 +849,24 @@ final class TetherViewModel {// unpinned daemon answers anything else with "unauthorized".private func sendHello() {daemonSupportsClipboardImages = false+ daemonCameraPort = nilconnection.send(.hello(features: ["clipboard_image"]))}+ // MARK: - Camera++ // Streams the camera to the connected daemon, which shows it as a webcam.+ func startCamera(position: AVCaptureDevice.Position) {+ guard appState == .connected, let port = daemonCameraPort,+ let host = connection.resolvedEndpoint?.host else { return }+ guard let identity = certificateManager.getIdentity() else {+ errorMessage = identityErrorMessage+ return+ }+ camera.start(host: host, port: port, pinnedFingerprint: connection.serverFingerprint,+ identity: identity, position: position)+ }+private func finalizeIncomingTransfer(transferId: String) {guard let buffered = incomingTransfers.removeValue(forKey: transferId) else { return }diff --git a/apple/Tether/Views/CameraView.swift b/apple/Tether/Views/CameraView.swiftnew file mode 100644index 0000000..7b8ec2f--- /dev/null+++ b/apple/Tether/Views/CameraView.swift@@ -0,0 +1,107 @@+//+// CameraView.swift+// Tether+//+// Streams the camera to the desktop, where it shows up as a webcam.+//++import AVFoundation+import SwiftUI+import TetherFramework++struct CameraView: View {+ @Environment(TetherViewModel.self) private var viewModel+ @State private var position: AVCaptureDevice.Position = .back++ var body: some View {+ NavigationStack {+ Group {+ if viewModel.appState != .connected {+ message("Connect to a device to use the camera")+ } else if viewModel.daemonCameraPort == nil {+ message("This desktop's Tether does not support the camera")+ } else {+ streamView+ }+ }+ .background(Color(.systemGroupedBackground))+ .navigationTitle("Camera")+ }+ .onChange(of: viewModel.camera.isStreaming) { _, streaming in+ // The phone must stay awake and in front to keep streaming.+ UIApplication.shared.isIdleTimerDisabled = streaming+ }+ }++ private var streamView: some View {+ VStack(spacing: 16) {+ CameraPreview(session: viewModel.camera.session)+ .aspectRatio(16 / 9, contentMode: .fit)+ .background(Color.black)+ .clipShape(RoundedRectangle(cornerRadius: 12))+ .accessibilityLabel("Camera preview")++ Picker("Camera", selection: $position) {+ Text("Back").tag(AVCaptureDevice.Position.back)+ Text("Front").tag(AVCaptureDevice.Position.front)+ }+ .pickerStyle(.segmented)+ .disabled(viewModel.camera.isStreaming)++ if viewModel.camera.isStreaming {+ Button("Stop", role: .destructive) { viewModel.camera.stop() }+ .buttonStyle(.borderedProminent)+ } else {+ Button("Start Streaming") { viewModel.startCamera(position: position) }+ .buttonStyle(.borderedProminent)+ }++ if let error = viewModel.camera.lastError {+ Text(error)+ .font(.footnote)+ .foregroundStyle(.red)+ }++ Text("Keep Tether open while streaming. On the desktop, pick the camera named after this phone.")+ .font(.footnote)+ .foregroundStyle(.secondary)+ .multilineTextAlignment(.center)++ Spacer()+ }+ .padding()+ }++ private func message(_ text: LocalizedStringKey) -> some View {+ VStack(spacing: 16) {+ Spacer()+ Image(systemName: "web.camera")+ .font(.system(size: 48))+ .foregroundStyle(.tertiary)+ .accessibilityHidden(true)+ Text(text)+ .font(.subheadline)+ .foregroundStyle(.secondary)+ Spacer()+ }+ .frame(maxWidth: .infinity)+ }+}++private struct CameraPreview: UIViewRepresentable {+ let session: AVCaptureSession++ final class PreviewView: UIView {+ override class var layerClass: AnyClass { AVCaptureVideoPreviewLayer.self }+ var previewLayer: AVCaptureVideoPreviewLayer { layer as! AVCaptureVideoPreviewLayer }+ }++ func makeUIView(context: Context) -> PreviewView {+ let view = PreviewView()+ view.previewLayer.session = session+ view.previewLayer.videoGravity = .resizeAspect+ return view+ }++ func updateUIView(_ uiView: PreviewView, context: Context) {}+}diff --git a/apple/TetherFramework/Sources/TetherFramework/CameraStreamer.swift b/apple/TetherFramework/Sources/TetherFramework/CameraStreamer.swiftnew file mode 100644index 0000000..dfce0d7--- /dev/null+++ b/apple/TetherFramework/Sources/TetherFramework/CameraStreamer.swift@@ -0,0 +1,289 @@+//+// CameraStreamer.swift+// TetherFramework+//+// Streams the camera to tetherd as H.264 over its own pinned TLS connection,+// which the daemon publishes as a webcam.+//++import AVFoundation+import Foundation+import Network+import Security+import VideoToolbox++@Observable+public final class CameraStreamer: NSObject {+ // True from the connection opening until stop() or a failure.+ public private(set) var isStreaming = false+ // Why the last stream ended, if it failed.+ public private(set) var lastError: String?++ // Feed for a preview layer; runs only while streaming.+ public let session = AVCaptureSession()++ // Everything below is confined to `queue`.+ private let queue = DispatchQueue(label: "tether.camera")+ @ObservationIgnored private var connection: NWConnection?+ @ObservationIgnored private var encoder: VTCompressionSession?+ // Frames handed to the encoder whose bytes the network has not taken yet.+ @ObservationIgnored private var pending = 0++ private static let maxPending = 2+ private static let fps = 30+ private static let bitRate = 6_000_000++ // Streams the camera to the daemon at host:port. The connection is refused+ // unless the server presents `pinnedFingerprint`.+ public func start(+ host: String,+ port: UInt16,+ pinnedFingerprint: String,+ identity: SecIdentity,+ position: AVCaptureDevice.Position = .back+ ) {+ stop()+ lastError = nil+ isStreaming = true++ let params = TetherConnection.tlsParameters(identity: identity) { fingerprint in+ !pinnedFingerprint.isEmpty && fingerprint == pinnedFingerprint+ }+ let conn = NWConnection(+ to: .hostPort(host: NWEndpoint.Host(host), port: NWEndpoint.Port(rawValue: port)!),+ using: params+ )+ conn.stateUpdateHandler = { [weak self] state in+ guard let self else { return }+ switch state {+ case .ready:+ self.sendStart(on: conn)+ self.startCapture(position: position)+ case .failed(let error):+ self.fail(error.localizedDescription)+ case .waiting(let error):+ // Unreachable now; the stream is pointless if it starts minutes later.+ self.fail(error.localizedDescription)+ default:+ break+ }+ }+ queue.async {+ self.connection = conn+ self.pending = 0+ conn.start(queue: self.queue)+ }+ }++ public func stop() {+ isStreaming = false+ queue.async { self.teardown() }+ }++ // MARK: - Private++ private func fail(_ message: String) {+ teardown()+ DispatchQueue.main.async {+ self.lastError = message+ self.isStreaming = false+ }+ }++ private func teardown() {+ if session.isRunning { session.stopRunning() }+ if let encoder {+ VTCompressionSessionInvalidate(encoder)+ }+ encoder = nil+ connection?.stateUpdateHandler = nil+ connection?.cancel()+ connection = nil+ }++ private func sendStart(on conn: NWConnection) {+ let start: [String: Any] = ["command": "camera_start", "codec": "h264", "fps": Self.fps]+ guard var line = try? JSONSerialization.data(withJSONObject: start) else { return }+ line.append(0x0A)+ conn.send(content: line, completion: .contentProcessed { _ in })+ }++ private func startCapture(position: AVCaptureDevice.Position) {+ session.beginConfiguration()+ session.inputs.forEach { session.removeInput($0) }+ session.outputs.forEach { session.removeOutput($0) }+ session.sessionPreset = .hd1920x1080++ guard let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: position),+ let input = try? AVCaptureDeviceInput(device: device),+ session.canAddInput(input)+ else {+ session.commitConfiguration()+ fail("Camera unavailable")+ return+ }+ session.addInput(input)++ let output = AVCaptureVideoDataOutput()+ output.videoSettings = [+ kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange+ ]+ output.alwaysDiscardsLateVideoFrames = true+ output.setSampleBufferDelegate(self, queue: queue)+ session.addOutput(output)+ // Landscape whatever the phone's orientation, so the frame size never changes mid-stream.+ if let videoConnection = output.connection(with: .video), videoConnection.isVideoRotationAngleSupported(0) {+ videoConnection.videoRotationAngle = 0+ }+ session.commitConfiguration()+ session.startRunning()+ }++ private func makeEncoder(width: Int32, height: Int32) -> VTCompressionSession? {+ var session: VTCompressionSession?+ guard VTCompressionSessionCreate(+ allocator: nil,+ width: width,+ height: height,+ codecType: kCMVideoCodecType_H264,+ encoderSpecification: nil,+ imageBufferAttributes: nil,+ compressedDataAllocator: nil,+ outputCallback: nil,+ refcon: nil,+ compressionSessionOut: &session+ ) == noErr, let session else { return nil }++ VTSessionSetProperty(session, key: kVTCompressionPropertyKey_RealTime, value: kCFBooleanTrue)+ VTSessionSetProperty(session, key: kVTCompressionPropertyKey_ProfileLevel,+ value: kVTProfileLevel_H264_Main_AutoLevel)+ VTSessionSetProperty(session, key: kVTCompressionPropertyKey_AllowFrameReordering, value: kCFBooleanFalse)+ // A viewer that joins mid-stream waits at most this long for a picture.+ VTSessionSetProperty(session, key: kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration,+ value: NSNumber(value: 2))+ VTSessionSetProperty(session, key: kVTCompressionPropertyKey_ExpectedFrameRate,+ value: NSNumber(value: Self.fps))+ VTSessionSetProperty(session, key: kVTCompressionPropertyKey_AverageBitRate,+ value: NSNumber(value: Self.bitRate))+ VTCompressionSessionPrepareToEncodeFrames(session)+ return session+ }++ private func encoded(_ status: OSStatus, _ sample: CMSampleBuffer?) {+ guard status == noErr, let sample, let payload = Self.annexB(sample) else {+ queue.async { self.pending -= 1 }+ return+ }+ let keyframe = Self.isKeyframe(sample)+ let pts = CMSampleBufferGetPresentationTimeStamp(sample)+ let ptsUs = pts.isValid ? UInt64(max(0, CMTimeGetSeconds(pts) * 1_000_000)) : 0++ var frame = Data(capacity: 13 + payload.count)+ frame.appendBigEndian(UInt32(9 + payload.count))+ frame.append(keyframe ? 1 : 0)+ frame.appendBigEndian(ptsUs)+ frame.append(payload)++ queue.async {+ guard let conn = self.connection else { return }+ conn.send(content: frame, completion: .contentProcessed { [weak self] error in+ guard let self else { return }+ self.pending -= 1+ if let error { self.fail(error.localizedDescription) }+ })+ }+ }++ private static func isKeyframe(_ sample: CMSampleBuffer) -> Bool {+ guard let attachments = CMSampleBufferGetSampleAttachmentsArray(sample, createIfNecessary: false)+ as? [[CFString: Any]],+ let first = attachments.first+ else { return true }+ return !(first[kCMSampleAttachmentKey_NotSync] as? Bool ?? false)+ }++ // Converts VideoToolbox's AVCC output to Annex B, with SPS/PPS ahead of keyframes.+ static func annexB(_ sample: CMSampleBuffer) -> Data? {+ let startCode: [UInt8] = [0, 0, 0, 1]+ var out = Data()++ if isKeyframe(sample), let format = CMSampleBufferGetFormatDescription(sample) {+ var count = 0+ CMVideoFormatDescriptionGetH264ParameterSetAtIndex(+ format, parameterSetIndex: 0, parameterSetPointerOut: nil, parameterSetSizeOut: nil,+ parameterSetCountOut: &count, nalUnitHeaderLengthOut: nil)+ for i in 0..<count {+ var pointer: UnsafePointer<UInt8>?+ var size = 0+ if CMVideoFormatDescriptionGetH264ParameterSetAtIndex(+ format, parameterSetIndex: i, parameterSetPointerOut: &pointer, parameterSetSizeOut: &size,+ parameterSetCountOut: nil, nalUnitHeaderLengthOut: nil) == noErr, let pointer+ {+ out.append(contentsOf: startCode)+ out.append(pointer, count: size)+ }+ }+ }++ guard let block = CMSampleBufferGetDataBuffer(sample) else { return nil }+ let length = CMBlockBufferGetDataLength(block)+ var avcc = Data(count: length)+ let copied = avcc.withUnsafeMutableBytes {+ CMBlockBufferCopyDataBytes(block, atOffset: 0, dataLength: length, destination: $0.baseAddress!)+ }+ guard copied == kCMBlockBufferNoErr else { return nil }++ // ponytail: assumes 4-byte NAL lengths, which VideoToolbox always emits for H.264+ var offset = 0+ while offset + 4 <= avcc.count {+ let nalLength = avcc[offset..<offset + 4].reduce(0) { Int($0) << 8 | Int($1) }+ offset += 4+ guard nalLength > 0, offset + nalLength <= avcc.count else { return nil }+ out.append(contentsOf: startCode)+ out.append(avcc[offset..<offset + nalLength])+ offset += nalLength+ }+ return out+ }+}++extension CameraStreamer: AVCaptureVideoDataOutputSampleBufferDelegate {+ public func captureOutput(+ _ output: AVCaptureOutput,+ didOutput sampleBuffer: CMSampleBuffer,+ from connection: AVCaptureConnection+ ) {+ guard self.connection != nil, 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 }++ if encoder == nil {+ encoder = makeEncoder(width: Int32(CVPixelBufferGetWidth(image)),+ height: Int32(CVPixelBufferGetHeight(image)))+ }+ guard let encoder else {+ fail("H.264 encoder unavailable")+ return+ }++ pending += 1+ let status = VTCompressionSessionEncodeFrame(+ encoder,+ imageBuffer: image,+ presentationTimeStamp: CMSampleBufferGetPresentationTimeStamp(sampleBuffer),+ duration: .invalid,+ frameProperties: nil,+ infoFlagsOut: nil+ ) { [weak self] status, _, sample in+ self?.encoded(status, sample)+ }+ if status != noErr { pending -= 1 }+ }+}++private extension Data {+ mutating func appendBigEndian<T: FixedWidthInteger>(_ value: T) {+ Swift.withUnsafeBytes(of: value.bigEndian) { append(contentsOf: $0) }+ }+}diff --git a/apple/TetherFramework/Sources/TetherFramework/TetherConnection.swift b/apple/TetherFramework/Sources/TetherFramework/TetherConnection.swiftindex 2adcca0..91d35a4 100644--- a/apple/TetherFramework/Sources/TetherFramework/TetherConnection.swift+++ b/apple/TetherFramework/Sources/TetherFramework/TetherConnection.swift@@ -62,26 +62,12 @@ public final class TetherConnection {public func connect(to endpoint: NWEndpoint, identity: SecIdentity) {teardown()- let tlsOptions = NWProtocolTLS.Options()- let secOptions = tlsOptions.securityProtocolOptions-- // Present our client certificate for mTLS- let secIdentity = sec_identity_create(identity)!- sec_protocol_options_set_local_identity(secOptions, secIdentity)-- // TLS 1.2 only to ensure stability with the daemon's OpenSSL configuration- sec_protocol_options_set_min_tls_protocol_version(secOptions, .TLSv12)- sec_protocol_options_set_max_tls_protocol_version(secOptions, .TLSv12)-// Accept any server certificate (daemon uses self-signed)// and capture the server's fingerprint.- sec_protocol_options_set_verify_block(secOptions, { [weak self] metadata, trust, completion in- self?.captureServerFingerprint(trust: trust)- completion(true)- }, .main)-- let params = NWParameters(tls: tlsOptions, tcp: .init())- params.includePeerToPeer = true+ let params = Self.tlsParameters(identity: identity) { [weak self] fingerprint in+ self?.serverFingerprint = fingerprint+ return true+ }let conn = NWConnection(to: endpoint, using: params)@@ -218,16 +204,34 @@ public final class TetherConnection {}}- // MARK: - Private — TLS Fingerprint+ // MARK: - TLS- // Extract the server's certificate and compute its SHA-256 fingerprint.- private func captureServerFingerprint(trust: sec_trust_t) {- let secTrust = sec_trust_copy_ref(trust).takeRetainedValue()+ // TLS parameters presenting our client certificate for mTLS. `verify` gets the+ // server's SHA-256 fingerprint ("" if it has none) on the main queue and decides+ // whether to trust it.+ static func tlsParameters(identity: SecIdentity, verify: @escaping (String) -> Bool) -> NWParameters {+ let tlsOptions = NWProtocolTLS.Options()+ let secOptions = tlsOptions.securityProtocolOptions- if let certChain = SecTrustCopyCertificateChain(secTrust) as? [SecCertificate],- let serverCert = certChain.first- {- serverFingerprint = CertificateManager.fingerprint(ofCertificate: serverCert)- }+ sec_protocol_options_set_local_identity(secOptions, sec_identity_create(identity)!)++ // TLS 1.2 only to ensure stability with the daemon's OpenSSL configuration+ sec_protocol_options_set_min_tls_protocol_version(secOptions, .TLSv12)+ sec_protocol_options_set_max_tls_protocol_version(secOptions, .TLSv12)++ sec_protocol_options_set_verify_block(secOptions, { _, trust, completion in+ let secTrust = sec_trust_copy_ref(trust).takeRetainedValue()+ var fingerprint = ""+ if let certChain = SecTrustCopyCertificateChain(secTrust) as? [SecCertificate],+ let serverCert = certChain.first+ {+ fingerprint = CertificateManager.fingerprint(ofCertificate: serverCert)+ }+ completion(verify(fingerprint))+ }, .main)++ let params = NWParameters(tls: tlsOptions, tcp: .init())+ params.includePeerToPeer = true+ return params}}diff --git a/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swift b/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swiftindex 40c3dfd..ecae03c 100644--- a/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swift+++ b/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swift@@ -73,6 +73,8 @@ public struct TetherMessage: Codable, Sendable {public var features: [String]?// "phone", "tablet" or "computer", so the desktop can show the right icon.public var device: String?+ // The daemon's TCP port, sent with the camera feature.+ public var port: UInt16?// Pairingpublic var deviceName: String?@@ -89,7 +91,7 @@ public struct TetherMessage: Codable, Sendable {case command, content, filename, sizecase transferId = "transfer_id"case chunkIndex = "chunk_index"- case data, clipboard, features, device+ case data, clipboard, features, device, portcase deviceName = "device_name"case otp, sourcecase status, message@@ -110,7 +112,8 @@ public struct TetherMessage: Codable, Sendable {message: String? = nil,clipboard: String? = nil,features: [String]? = nil,- device: String? = nil+ device: String? = nil,+ port: UInt16? = nil) {self.command = commandself.content = content@@ -127,6 +130,7 @@ public struct TetherMessage: Codable, Sendable {self.clipboard = clipboardself.features = featuresself.device = device+ self.port = port}}diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.mdindex 2b7bea6..b4b326e 100644--- a/docs/PROTOCOL.md+++ b/docs/PROTOCOL.md@@ -40,6 +40,7 @@ ignore this message.| Feature | Meaning ||---|---|| `clipboard_image` | [Clipboard images](#clipboard-images-both-directions) |+| `camera` | [Camera](#7-camera-iphone-as-a-webcam). Daemon only; its `hello` also carries `"port"`, the TCP port to dial for the stream. |---@@ -650,3 +651,35 @@ one device, so a reader can still follow which device a line refers to without lwhich device it is. Message bodies, contact names, and notification content are droppedoutright rather than redacted, and message and notification events never enter the timelinein the first place.++---++## 7. Camera (iPhone as a webcam)++The phone streams its camera to the daemon, which publishes it as a PipeWire+`Video/Source` node (`media.role` `Camera`) named after the phone. The stream uses its+own TLS connection, not the main session, so video never delays other messages.++1. The phone opens a new connection to the daemon's address and the `port` from its+ `hello`, with the same client certificate. The phone must only accept the daemon's pinned+ fingerprint here.+2. Its first line is `camera_start`. The daemon answers nothing; an unpaired session gets+ the usual `unauthorized` error, and a daemon without camera support closes the connection.+3. Everything after that line is binary frames until the connection closes, which removes+ the node. A new `camera_start` replaces any stream already running.++```json+{"command": "camera_start", "codec": "h264", "width": 1920, "height": 1080, "fps": 30}+```++Each frame, big-endian:++| Bytes | Field |+|---|---|+| 4 | length of everything after this field (at most 4 MiB) |+| 1 | flags; bit 0 set on keyframes |+| 8 | presentation time, microseconds |+| rest | one H.264 access unit, Annex B (start codes); keyframes carry SPS and PPS |++The sender sends a keyframe at least every 2 seconds, so a viewer that joins mid-stream+shows video within that time. The daemon reads the frame size from the stream.diff --git a/nix/package.nix b/nix/package.nixindex b8d4772..879189e 100644--- a/nix/package.nix+++ b/nix/package.nix@@ -21,6 +21,8 @@gtk-layer-shell,libnotify,ncurses,+ ffmpeg,+ pipewire,nlohmann_json,gtest,bluez,@@ -97,6 +99,8 @@ stdenv.mkDerivation (finalAttrs: {gtk-layer-shelllibnotifyncurses+ ffmpeg+ pipewire];cmakeFlags = [diff --git a/packaging/appimage/build.sh b/packaging/appimage/build.shindex e25463e..fbab761 100755--- a/packaging/appimage/build.sh+++ b/packaging/appimage/build.sh@@ -19,7 +19,8 @@ export APPIMAGE_EXTRACT_AND_RUN=1cmake -B "$BUILD_DIR" -S "$ROOT" -G Ninja \-DCMAKE_BUILD_TYPE=Release \- -DCMAKE_INSTALL_PREFIX=/usr+ -DCMAKE_INSTALL_PREFIX=/usr \+ -DTETHER_CAMERA=OFFcmake --build "$BUILD_DIR"rm -rf "$APPDIR"diff --git a/packaging/container/Dockerfile b/packaging/container/Dockerfileindex 3e34465..65ccad6 100644--- a/packaging/container/Dockerfile+++ b/packaging/container/Dockerfile@@ -11,7 +11,7 @@ ARG TETHER_VERSION=0.2.32-container-devARG BUILD_JOBS=4RUN cmake -S . -B /build -G Ninja -DCMAKE_BUILD_TYPE=Release \-DCMAKE_INSTALL_PREFIX=/usr/local -DTETHER_VERSION="${TETHER_VERSION}" \- -DTETHER_BUILD_EXTENSIONS=OFF -DTETHER_BUILD_TUI=OFF \+ -DTETHER_BUILD_EXTENSIONS=OFF -DTETHER_BUILD_TUI=OFF -DTETHER_CAMERA=OFF \&& cmake --build /build --target tetherd tether_cli translations -j "${BUILD_JOBS}" \&& mkdir -p /payload/bin /payload/share/locale \&& install -m 755 /build/tether /build/tetherd /payload/bin/ \diff --git a/packaging/flatpak/com.tether.desktop.yml b/packaging/flatpak/com.tether.desktop.ymlindex df17f61..b9d11ab 100644--- a/packaging/flatpak/com.tether.desktop.yml+++ b/packaging/flatpak/com.tether.desktop.yml@@ -69,6 +69,7 @@ modules:- -DCMAKE_BUILD_TYPE=Release- -DTETHER_BUILD_EXTENSIONS=OFF- -DTETHER_BUILD_TUI=OFF+ - -DTETHER_CAMERA=OFFbuild-options:build-args:- --share=networkdiff --git a/packaging/rpm/tether.spec b/packaging/rpm/tether.specindex 789a7e9..cde61ae 100644--- a/packaging/rpm/tether.spec+++ b/packaging/rpm/tether.spec@@ -22,6 +22,8 @@ BuildRequires: gtk3-develBuildRequires: gtk-layer-shell-develBuildRequires: libnotify-develBuildRequires: ncurses-devel+BuildRequires: libavcodec-free-devel+BuildRequires: pipewire-develBuildRequires: gettextBuildRequires: npmBuildRequires: zipdiff --git a/scripts/ci-deps.sh b/scripts/ci-deps.shindex d6e741b..e1bce56 100755--- a/scripts/ci-deps.sh+++ b/scripts/ci-deps.sh@@ -14,7 +14,7 @@ case ${ID_LIKE:-$ID} inpacman -Syu --noconfirm --needed \base-devel cmake ninja git pkgconf \wayland avahi openssl glib2 libsecret \- gtk3 gtk-layer-shell libnotify ncurses \+ gtk3 gtk-layer-shell libnotify ncurses ffmpeg pipewire \npm zip gettext;;*fedora*)@@ -22,6 +22,7 @@ case ${ID_LIKE:-$ID} ingcc-c++ cmake ninja-build git pkgconf-pkg-config \wayland-devel avahi-devel openssl-devel glib2-devel libsecret-devel \gtk3-devel gtk-layer-shell-devel libnotify-devel ncurses-devel \+ libavcodec-free-devel pipewire-devel \npm zip rpm-build gettext;;*debian*|*ubuntu*)@@ -31,6 +32,7 @@ case ${ID_LIKE:-$ID} ing++ cmake ninja-build git pkg-config ca-certificates \libwayland-dev libavahi-client-dev libssl-dev libglib2.0-dev libsecret-1-dev \libgtk-3-dev libgtk-layer-shell-dev libnotify-dev libncurses-dev \+ libavcodec-dev libpipewire-0.3-dev \npm zip gettext;;*)@@ -42,5 +44,5 @@ esaccommand -v msgfmt >/dev/null || { echo "ci-deps: msgfmt missing; translations would be skipped" >&2; exit 1; }pkg-config --print-errors --exists \- "wayland-client avahi-client openssl glib-2.0 gio-2.0 gio-unix-2.0 libsecret-1 gtk+-3.0 gtk-layer-shell-0 libnotify ncursesw"+ "wayland-client avahi-client openssl glib-2.0 gio-2.0 gio-unix-2.0 libsecret-1 gtk+-3.0 gtk-layer-shell-0 libnotify ncursesw libavcodec libpipewire-0.3"echo "==> ready: gtk $(pkg-config --modversion gtk+-3.0), openssl $(pkg-config --modversion openssl)"diff --git a/src/core/include/tether/net.hpp b/src/core/include/tether/net.hppindex 501b2a5..57058d9 100644--- a/src/core/include/tether/net.hpp+++ b/src/core/include/tether/net.hpp@@ -164,6 +164,16 @@ namespace tether {// Invoked when the set of trusted peers gains a member with no live session.void set_peers_changed_callback(std::function<void()> callback);+ // Receives a paired session that sent camera_start. The handler owns fd and ssl;+ // leftover holds bytes already read past the start line.+ using CameraHandler = std::function<void(int fd,+ SSL* ssl,+ const nlohmann::json& start,+ std::string leftover,+ const std::string& device_name)>;+ // Also advertises the camera feature in hello.+ void set_camera_handler(CameraHandler handler);+private:struct ConnectedClientInfo {std::string address;@@ -183,6 +193,8 @@ namespace tether {void promote_session(int client_fd, const std::string& fingerprint, const std::string& device_name);// Frees the SSL session, forgets every per-client table, and closes the fd.void drop_client(int fd);+ // Forgets every per-client table and stops polling fd, leaving fd and SSL open.+ void detach_client(int fd);EpollEventLoop& loop_;int server_fd_ = -1;@@ -199,6 +211,7 @@ namespace tether {std::set<std::string> dialing_;std::function<void()> peers_changed_;+ CameraHandler camera_handler_;// Track spawned dialog child processesstruct PendingPairDialog {diff --git a/src/core/src/net.cpp b/src/core/src/net.cppindex c6ac2dd..3b13815 100644--- a/src/core/src/net.cpp+++ b/src/core/src/net.cpp@@ -1936,12 +1936,14 @@ namespace tether {void TcpServer::drop_client(int fd) {auto ssl_it = active_ssl_.find(fd);- if (ssl_it != active_ssl_.end()) {- if (ssl_it->second)- SSL_free(ssl_it->second);- active_ssl_.erase(ssl_it);- }+ if (ssl_it != active_ssl_.end() && ssl_it->second)+ SSL_free(ssl_it->second);+ detach_client(fd);+ close(fd);+ }+ void TcpServer::detach_client(int fd) {+ active_ssl_.erase(fd);client_buffers_.erase(fd);clipboard_images_.erase(fd);ssl_handshake_complete_.erase(fd);@@ -1952,7 +1954,6 @@ namespace tether {unregister_client_fd(fd);loop_.removeFd(fd);- close(fd);}// Drops peers that vanish without FIN/RST: keepalive covers idle sessions (~90 s),@@ -2401,9 +2402,27 @@ namespace tether {}nlohmann::json resp{{"command", "hello"}};resp["features"] = nlohmann::json::array({"clipboard_image"});+ if (camera_handler_) {+ resp["features"].push_back("camera");+ // the phone dials this for the camera stream+ resp["port"] = bind_port_;+ }std::string payload = resp.dump() + "\n";robust_ssl_write(ssl, payload.c_str(), payload.size());continue;+ } else if (j.contains("command") && j["command"] == "camera_start") {+ if (!camera_handler_) {+ std::string rej = "{\"command\":\"error\",\"message\":\"camera unsupported\"}\n";+ robust_ssl_write(ssl, rej.c_str(), rej.size());+ drop_client(client_fd);+ return;+ }+ // This connection now carries only binary video frames.+ std::string leftover = buffer.substr(start);+ const std::string device_name = client_info_[client_fd].device_name;+ detach_client(client_fd);+ camera_handler_(client_fd, ssl, j, std::move(leftover), device_name);+ return;} else if (j.contains("command") && j["command"] == "open_url" && j.contains("content") &&j["content"].is_string()) {open_web_url(j["content"].get<std::string>());@@ -2529,6 +2548,8 @@ namespace tether {void TcpServer::set_peers_changed_callback(std::function<void()> callback) { peers_changed_ = std::move(callback); }+ void TcpServer::set_camera_handler(CameraHandler handler) { camera_handler_ = std::move(handler); }+bool TcpServer::forget_device(const std::string& fingerprint) {if (fingerprint.empty())return false;diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txtindex 27542ad..24674dd 100644--- a/src/daemon/CMakeLists.txt+++ b/src/daemon/CMakeLists.txt@@ -8,6 +8,13 @@ pkg_check_modules(GIO_UNIX REQUIRED IMPORTED_TARGET gio-unix-2.0)target_link_libraries(tetherd PRIVATE tether PkgConfig::LIBNOTIFY PkgConfig::GIO_UNIX)+if(TETHER_CAMERA)+ pkg_check_modules(CAMERA REQUIRED IMPORTED_TARGET libavcodec libavutil libpipewire-0.3)+ target_sources(tetherd PRIVATE camera.cpp)+ target_compile_definitions(tetherd PRIVATE TETHER_CAMERA)+ target_link_libraries(tetherd PRIVATE PkgConfig::CAMERA)+endif()+# Output into root build dir instead of nested build/src/daemonset_target_properties(tetherd PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")diff --git a/src/daemon/camera.cpp b/src/daemon/camera.cppnew file mode 100644index 0000000..a06b048--- /dev/null+++ b/src/daemon/camera.cpp@@ -0,0 +1,262 @@+#include "camera.hpp"+#include "camera_frame.hpp"++#include <algorithm>+#include <cstring>+#include <fcntl.h>+#include <memory>+#include <mutex>+#include <pipewire/pipewire.h>+#include <spa/param/buffers.h>+#include <spa/param/video/format-utils.h>+#include <sys/socket.h>+#include <tether/log.hpp>+#include <thread>+#include <unistd.h>+#include <vector>++extern "C" {+#include <libavcodec/avcodec.h>+}++namespace tether {++ namespace {++ // fd of the stream currently feeding the node, so a new one can end it.+ std::mutex g_active_mutex;+ int g_active_fd = -1;++ // A PipeWire Video/Source that drives the graph with I420 frames as they arrive.+ class PipeWireCamera {+ public:+ PipeWireCamera(const std::string& description, int width, int height, int fps)+ : width_(width), height_(height) {+ static std::once_flag init;+ std::call_once(init, [] { pw_init(nullptr, nullptr); });++ events_.version = PW_VERSION_STREAM_EVENTS;+ events_.state_changed = on_state_changed;+ events_.param_changed = on_param_changed;+ events_.process = on_process;++ loop_ = pw_thread_loop_new("tether-camera", nullptr);+ pw_properties* props = pw_properties_new(PW_KEY_MEDIA_TYPE,+ "Video",+ PW_KEY_MEDIA_CATEGORY,+ "Capture",+ PW_KEY_MEDIA_CLASS,+ "Video/Source",+ // the xdg-desktop-portal Camera portal only exposes this role+ PW_KEY_MEDIA_ROLE,+ "Camera",+ PW_KEY_NODE_NAME,+ "tether-camera",+ PW_KEY_NODE_DESCRIPTION,+ description.c_str(),+ nullptr);+ stream_ = pw_stream_new_simple(pw_thread_loop_get_loop(loop_), "tether-camera", props, &events_, this);++ uint8_t buf[1024];+ spa_pod_builder b{};+ spa_pod_builder_init(&b, buf, sizeof(buf));+ spa_video_info_raw info{};+ info.format = SPA_VIDEO_FORMAT_I420;+ info.size = {uint32_t(width), uint32_t(height)};+ info.framerate = {uint32_t(fps), 1};+ const spa_pod* params[] = {spa_format_video_raw_build(&b, SPA_PARAM_EnumFormat, &info)};++ if (!stream_ || pw_stream_connect(stream_,+ PW_DIRECTION_OUTPUT,+ PW_ID_ANY,+ pw_stream_flags(PW_STREAM_FLAG_DRIVER | PW_STREAM_FLAG_MAP_BUFFERS),+ params,+ 1) < 0) {+ debug::log(ERR, "camera: could not create the PipeWire node");+ return;+ }+ pw_thread_loop_start(loop_);+ }++ ~PipeWireCamera() {+ pw_thread_loop_stop(loop_);+ if (stream_)+ pw_stream_destroy(stream_);+ pw_thread_loop_destroy(loop_);+ }++ PipeWireCamera(const PipeWireCamera&) = delete;+ PipeWireCamera& operator=(const PipeWireCamera&) = delete;++ bool matches(const AVFrame* f) const { return f->width == width_ && f->height == height_; }++ void push(const AVFrame* f) {+ std::vector<uint8_t> i420(size_t(width_) * height_ * 3 / 2);+ uint8_t* out = i420.data();+ for (int plane = 0; plane < 3; ++plane) {+ const int w = plane ? width_ / 2 : width_;+ const int h = plane ? height_ / 2 : height_;+ for (int y = 0; y < h; ++y, out += w)+ std::memcpy(out, f->data[plane] + size_t(y) * f->linesize[plane], w);+ }++ pw_thread_loop_lock(loop_);+ pending_.swap(i420);+ if (streaming_)+ pw_stream_trigger_process(stream_);+ pw_thread_loop_unlock(loop_);+ }++ private:+ static void on_state_changed(void* data, pw_stream_state, pw_stream_state state, const char* error) {+ auto* self = static_cast<PipeWireCamera*>(data);+ self->streaming_ = state == PW_STREAM_STATE_STREAMING;+ if (state == PW_STREAM_STATE_ERROR)+ debug::log(ERR, "camera: PipeWire stream error: {}", error ? error : "unknown");+ }++ static void on_param_changed(void* data, uint32_t id, const spa_pod* param) {+ auto* self = static_cast<PipeWireCamera*>(data);+ if (!param || id != SPA_PARAM_Format)+ return;+ uint8_t buf[1024];+ spa_pod_builder b{};+ spa_pod_builder_init(&b, buf, sizeof(buf));+ const int size = self->width_ * self->height_ * 3 / 2;+ const spa_pod* params[] = {static_cast<const spa_pod*>(spa_pod_builder_add_object(+ &b,+ SPA_TYPE_OBJECT_ParamBuffers,+ SPA_PARAM_Buffers,+ SPA_PARAM_BUFFERS_buffers,+ SPA_POD_CHOICE_RANGE_Int(4, 2, 8),+ SPA_PARAM_BUFFERS_blocks,+ SPA_POD_Int(1),+ SPA_PARAM_BUFFERS_size,+ SPA_POD_Int(size),+ SPA_PARAM_BUFFERS_stride,+ SPA_POD_Int(self->width_),+ SPA_PARAM_BUFFERS_dataType,+ SPA_POD_CHOICE_FLAGS_Int((1 << SPA_DATA_MemPtr) | (1 << SPA_DATA_MemFd))))};+ pw_stream_update_params(self->stream_, params, 1);+ }++ // Runs on the loop thread with the loop lock held.+ static void on_process(void* data) {+ auto* self = static_cast<PipeWireCamera*>(data);+ if (self->pending_.empty())+ return;+ pw_buffer* b = pw_stream_dequeue_buffer(self->stream_);+ if (!b)+ return;+ spa_data& d = b->buffer->datas[0];+ if (d.data && d.maxsize >= self->pending_.size()) {+ std::memcpy(d.data, self->pending_.data(), self->pending_.size());+ d.chunk->offset = 0;+ d.chunk->size = uint32_t(self->pending_.size());+ d.chunk->stride = self->width_;+ }+ self->pending_.clear();+ pw_stream_queue_buffer(self->stream_, b);+ }++ const int width_;+ const int height_;+ pw_thread_loop* loop_ = nullptr;+ pw_stream* stream_ = nullptr;+ pw_stream_events events_{};+ // Guarded by the loop lock.+ bool streaming_ = false;+ std::vector<uint8_t> pending_;+ };++ struct CodecDeleter {+ void operator()(AVCodecContext* c) const { avcodec_free_context(&c); }+ void operator()(AVPacket* p) const { av_packet_free(&p); }+ void operator()(AVFrame* f) const { av_frame_free(&f); }+ };++ void run_stream(int fd, SSL* ssl, std::string buf, std::string description, int fps) {+ // The epoll loop no longer owns this fd; block on it, and give up on a phone that goes quiet.+ fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);+ timeval timeout{10, 0};+ setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));++ std::unique_ptr<AVCodecContext, CodecDeleter> ctx(+ avcodec_alloc_context3(avcodec_find_decoder(AV_CODEC_ID_H264)));+ std::unique_ptr<AVPacket, CodecDeleter> pkt(av_packet_alloc());+ std::unique_ptr<AVFrame, CodecDeleter> frame(av_frame_alloc());+ std::unique_ptr<PipeWireCamera> camera;++ if (ctx) {+ ctx->flags |= AV_CODEC_FLAG_LOW_DELAY;+ }+ if (!ctx || !pkt || !frame || avcodec_open2(ctx.get(), ctx->codec, nullptr) < 0) {+ debug::log(ERR, "camera: no H.264 decoder");+ } else {+ debug::log(INFO, "camera: streaming \"{}\"", description);+ char rbuf[65536];+ for (;;) {+ CameraFrame f;+ long n;+ while ((n = parse_camera_frame(buf, f)) > 0) {+ if (av_new_packet(pkt.get(), int(f.payload.size())) == 0) {+ std::memcpy(pkt->data, f.payload.data(), f.payload.size());+ avcodec_send_packet(ctx.get(), pkt.get());+ av_packet_unref(pkt.get());+ }+ while (avcodec_receive_frame(ctx.get(), frame.get()) == 0) {+ // ponytail: VideoToolbox only sends 8-bit 4:2:0; add swscale if another format shows up+ if (frame->format != AV_PIX_FMT_YUV420P && frame->format != AV_PIX_FMT_YUVJ420P)+ continue;+ // ponytail: a size change recreates the node; apps may need to reselect it+ if (!camera || !camera->matches(frame.get()))+ camera =+ std::make_unique<PipeWireCamera>(description, frame->width, frame->height, fps);+ camera->push(frame.get());+ }+ buf.erase(0, size_t(n));+ }+ if (n < 0) {+ debug::log(ERR, "camera: malformed frame; ending stream");+ break;+ }+ const int r = SSL_read(ssl, rbuf, sizeof(rbuf));+ if (r <= 0)+ break;+ buf.append(rbuf, size_t(r));+ }+ debug::log(INFO, "camera: stream ended");+ }++ camera.reset();+ {+ std::lock_guard lock(g_active_mutex);+ if (g_active_fd == fd)+ g_active_fd = -1;+ }+ SSL_free(ssl);+ close(fd);+ }++ } // namespace++ void start_camera_stream(+ int fd, SSL* ssl, const nlohmann::json& start, std::string leftover, const std::string& device_name) {+ if (start.value("codec", "h264") != "h264") {+ debug::log(ERR, "camera: unsupported codec {}", start.value("codec", ""));+ SSL_free(ssl);+ close(fd);+ return;+ }+ {+ std::lock_guard lock(g_active_mutex);+ if (g_active_fd >= 0)+ shutdown(g_active_fd, SHUT_RDWR);+ g_active_fd = fd;+ }+ const int fps = std::clamp(start.value("fps", 30), 1, 120);+ std::string description = (device_name.empty() ? std::string("iPhone") : device_name) + " Camera";+ std::thread(run_stream, fd, ssl, std::move(leftover), std::move(description), fps).detach();+ }++} // namespace tetherdiff --git a/src/daemon/camera.hpp b/src/daemon/camera.hppnew file mode 100644index 0000000..35260b6--- /dev/null+++ b/src/daemon/camera.hpp@@ -0,0 +1,15 @@+#pragma once++#include <nlohmann/json.hpp>+#include <openssl/ssl.h>+#include <string>++namespace tether {++ // Takes ownership of a TLS connection that sent camera_start and, on a worker+ // thread, decodes its H.264 frames into a PipeWire Video/Source node. A new+ // stream replaces the running one. The node goes away when the connection ends.+ void start_camera_stream(+ int fd, SSL* ssl, const nlohmann::json& start, std::string leftover, const std::string& device_name);++} // namespace tetherdiff --git a/src/daemon/camera_frame.hpp b/src/daemon/camera_frame.hppnew file mode 100644index 0000000..6a9bd57--- /dev/null+++ b/src/daemon/camera_frame.hpp@@ -0,0 +1,38 @@+#pragma once++#include <cstddef>+#include <cstdint>+#include <string_view>++namespace tether {++ inline constexpr size_t CAMERA_FRAME_MAX_BYTES = 4u * 1024 * 1024;+ inline constexpr size_t CAMERA_FRAME_HEADER_BYTES = 4 + 1 + 8;++ // One camera frame+ struct CameraFrame {+ bool keyframe = false;+ uint64_t pts_us = 0;+ std::string_view payload;+ };++ // Parses the frame at the front of buf into out. Returns the bytes it spans,+ // 0 when buf holds only part of it, or -1 when the length is out of range.+ inline long parse_camera_frame(std::string_view buf, CameraFrame& out) {+ if (buf.size() < 4)+ return 0;+ const auto* p = reinterpret_cast<const unsigned char*>(buf.data());+ const size_t len = (size_t(p[0]) << 24) | (size_t(p[1]) << 16) | (size_t(p[2]) << 8) | p[3];+ if (len < CAMERA_FRAME_HEADER_BYTES - 4 || len > CAMERA_FRAME_MAX_BYTES)+ return -1;+ if (buf.size() < 4 + len)+ return 0;+ out.keyframe = p[4] & 1;+ out.pts_us = 0;+ for (int i = 0; i < 8; ++i)+ out.pts_us = (out.pts_us << 8) | p[5 + i];+ out.payload = buf.substr(CAMERA_FRAME_HEADER_BYTES, len - (CAMERA_FRAME_HEADER_BYTES - 4));+ return static_cast<long>(4 + len);+ }++} // namespace tetherdiff --git a/src/daemon/main.cpp b/src/daemon/main.cppindex 178f8f3..b8f084d 100644--- a/src/daemon/main.cpp+++ b/src/daemon/main.cpp@@ -1,4 +1,7 @@#include "notification.hpp"+#ifdef TETHER_CAMERA+#include "camera.hpp"+#endif#include <algorithm>#include <charconv>#include <csignal>@@ -112,6 +115,9 @@ int main(int argc, char** argv) {}tether::TcpServer tcp_srv(loop, port);+#ifdef TETHER_CAMERA+ tcp_srv.set_camera_handler(tether::start_camera_stream);+#endiftether::UnixServer unix_srv(loop, tcp_srv);if (!unix_srv.start()) {debug::log(ERR, "Failed to start Unix server");diff --git a/test/CMakeLists.txt b/test/CMakeLists.txtindex 080f308..cad0ec0 100644--- a/test/CMakeLists.txt+++ b/test/CMakeLists.txt@@ -1,5 +1,6 @@add_executable(tether_testtest_base64.cpp+ test_camera_frame.cpptest_clipboard.cpptest_airpods.cpptest_audio.cppdiff --git a/test/test_camera_frame.cpp b/test/test_camera_frame.cppnew file mode 100644index 0000000..b9069cc--- /dev/null+++ b/test/test_camera_frame.cpp@@ -0,0 +1,40 @@+#include "../src/daemon/camera_frame.hpp"+#include <gtest/gtest.h>+#include <string>++using tether::CameraFrame;+using tether::parse_camera_frame;++static std::string frame(uint32_t len, uint8_t flags, uint64_t pts, const std::string& payload) {+ std::string s;+ for (int i = 3; i >= 0; --i)+ s += char(len >> (i * 8));+ s += char(flags);+ for (int i = 7; i >= 0; --i)+ s += char(pts >> (i * 8));+ return s + payload;+}++TEST(CameraFrameTest, ParsesWholeFrame) {+ const std::string buf = frame(9 + 3, 1, 0x0102030405060708ull, "abc") + "next";+ CameraFrame f;+ EXPECT_EQ(parse_camera_frame(buf, f), 13 + 3);+ EXPECT_TRUE(f.keyframe);+ EXPECT_EQ(f.pts_us, 0x0102030405060708ull);+ EXPECT_EQ(f.payload, "abc");+}++TEST(CameraFrameTest, WaitsForMoreBytes) {+ const std::string buf = frame(9 + 3, 0, 7, "abc");+ CameraFrame f;+ for (size_t n = 0; n < buf.size(); ++n)+ EXPECT_EQ(parse_camera_frame(std::string_view(buf).substr(0, n), f), 0) << n;+ EXPECT_EQ(parse_camera_frame(buf, f), long(buf.size()));+ EXPECT_FALSE(f.keyframe);+}++TEST(CameraFrameTest, RejectsBadLengths) {+ CameraFrame f;+ EXPECT_EQ(parse_camera_frame(frame(8, 0, 0, ""), f), -1);+ EXPECT_EQ(parse_camera_frame(frame(tether::CAMERA_FRAME_MAX_BYTES + 1, 0, 0, ""), f), -1);+}