Linux + iPhone Continuity / iMessage / SMS
fix: camrea streaming bug fixes
Zack Bartel286e330e8d03
diff --git a/apple/Tether/ViewModels/TetherViewModel.swift b/apple/Tether/ViewModels/TetherViewModel.swiftindex 179d51a..5d76cea 100644--- a/apple/Tether/ViewModels/TetherViewModel.swift+++ b/apple/Tether/ViewModels/TetherViewModel.swift@@ -873,6 +873,9 @@ final class TetherViewModel {errorMessage = "Allow camera access for Tether in Settings to stream."return}+ // The session may have dropped while the permission prompt was up.+ guard appState == .connected, daemonCameraPort == port,+ connection.serverFingerprint == fingerprint else { return }camera.start(host: host, port: port, pinnedFingerprint: fingerprint,identity: identity, position: position)}diff --git a/apple/TetherFramework/Sources/TetherFramework/CameraStreamer.swift b/apple/TetherFramework/Sources/TetherFramework/CameraStreamer.swiftindex d23ea58..bed7753 100644--- a/apple/TetherFramework/Sources/TetherFramework/CameraStreamer.swift+++ b/apple/TetherFramework/Sources/TetherFramework/CameraStreamer.swift@@ -24,8 +24,13 @@ public final class CameraStreamer: NSObject {// The camera feeding `session`, for rotating a preview to match.public private(set) var device: AVCaptureDevice?+ // Bumped on main by start() and stop(), so a failure from an earlier stream+ // cannot mark the current one stopped.+ @ObservationIgnored private var streamID = 0+// Everything below is confined to `queue`.private let queue = DispatchQueue(label: "tether.camera")+ @ObservationIgnored private var activeStreamID = 0@ObservationIgnored private var connection: NWConnection?@ObservationIgnored private var encoder: VTCompressionSession?// Turns frames upright and pillarboxes portrait ones into the encoder's landscape size.@@ -51,6 +56,8 @@ public final class CameraStreamer: NSObject {position: AVCaptureDevice.Position = .back) {stop()+ streamID += 1+ let id = streamIDlastError = nilisStreaming = true@@ -66,6 +73,7 @@ public final class CameraStreamer: NSObject {switch state {case .ready:self.sendStart(on: conn)+ self.receiveReplies(on: conn)self.startCapture(position: position)case .failed(let error):self.fail(error.localizedDescription)@@ -77,6 +85,7 @@ public final class CameraStreamer: NSObject {}}queue.async {+ self.activeStreamID = idself.connection = connself.pending = 0conn.start(queue: self.queue)@@ -84,6 +93,7 @@ public final class CameraStreamer: NSObject {}public func stop() {+ streamID += 1isStreaming = falsequeue.async { self.teardown() }}@@ -91,8 +101,10 @@ public final class CameraStreamer: NSObject {// MARK: - Privateprivate func fail(_ message: String) {+ let id = activeStreamIDteardown()DispatchQueue.main.async {+ guard self.streamID == id else { return }self.lastError = messageself.isStreaming = false}@@ -120,6 +132,22 @@ public final class CameraStreamer: NSObject {conn.send(content: line, completion: .contentProcessed { _ in })}+ // tetherd sends nothing while a stream is healthy, so any reply is an error.+ private func receiveReplies(on conn: NWConnection) {+ conn.receive(minimumIncompleteLength: 1, maximumLength: 4096) { [weak self] data, _, isComplete, error in+ guard let self, self.connection === conn else { return }+ if let data, !data.isEmpty {+ let line = data.prefix { $0 != 0x0A }+ let reply = try? JSONDecoder().decode(TetherMessage.self, from: line)+ self.fail(reply?.message ?? "The desktop refused the camera stream")+ } else if let error {+ self.fail(error.localizedDescription)+ } else if isComplete {+ self.fail("The desktop ended the camera stream")+ }+ }+ }+private func startCapture(position: AVCaptureDevice.Position) {session.beginConfiguration()session.inputs.forEach { session.removeInput($0) }diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.mdindex b4b326e..c29c58c 100644--- a/docs/PROTOCOL.md+++ b/docs/PROTOCOL.md@@ -665,8 +665,10 @@ own TLS connection, not the main session, so video never delays other messages.fingerprint here.2. Its first line is `camera_start`. The daemon answers nothing; an unpaired session getsthe 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.+3. Everything after that line is binary frames until the connection closes. The node then+ stays and shows black, so apps keep it selected for the next stream. A new `camera_start`+ replaces any stream already running. The daemon sends nothing on success, so any line it+ sends on this connection is an `error`.```json{"command": "camera_start", "codec": "h264", "width": 1920, "height": 1080, "fps": 30}diff --git a/src/daemon/camera.cpp b/src/daemon/camera.cppindex 3704c38..d2fe686 100644--- a/src/daemon/camera.cpp+++ b/src/daemon/camera.cpp@@ -10,6 +10,7 @@#include <spa/param/buffers.h>#include <spa/param/video/format-utils.h>#include <sys/socket.h>+#include <system_error>#include <tether/log.hpp>#include <thread>#include <unistd.h>@@ -211,10 +212,15 @@ namespace tether {} else {debug::log(INFO, "camera: streaming \"{}\"", description);char rbuf[65536];+ // A replaced stream must stop feeding the shared node, even from frames it already holds.+ const auto superseded = [fd] {+ std::lock_guard lock(g_active_mutex);+ return g_active_fd != fd;+ };for (;;) {CameraFrame f;- long n;- while ((n = parse_camera_frame(buf, f)) > 0) {+ long n = 0;+ while (!superseded() && (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());@@ -262,23 +268,41 @@ namespace tether {} // namespace+ bool camera_supported() { return avcodec_find_decoder(AV_CODEC_ID_H264) != nullptr; }++ // Must not throw: the caller has already handed over fd and ssl and forgotten them.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", ""));+ const auto codec = start.find("codec");+ if (codec != start.end() && !(codec->is_string() && *codec == "h264")) {+ debug::log(ERR, "camera: unsupported codec {}", codec->dump());SSL_free(ssl);close(fd);return;}+ const auto fps_field = start.find("fps");+ const int fps =+ fps_field != start.end() && fps_field->is_number_integer() ? std::clamp(fps_field->get<int>(), 1, 120) : 30;+ std::string description = (device_name.empty() ? std::string("iPhone") : device_name) + " Camera";++ int replaced = -1;{std::lock_guard lock(g_active_mutex);- if (g_active_fd >= 0)- shutdown(g_active_fd, SHUT_RDWR);+ replaced = g_active_fd;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();+ try {+ std::thread(run_stream, fd, ssl, std::move(leftover), std::move(description), fps).detach();+ } catch (const std::system_error& e) {+ debug::log(ERR, "camera: could not start the stream thread: {}", e.what());+ std::lock_guard lock(g_active_mutex);+ g_active_fd = replaced;+ SSL_free(ssl);+ close(fd);+ return;+ }+ if (replaced >= 0)+ shutdown(replaced, SHUT_RDWR);}} // namespace tetherdiff --git a/src/daemon/camera.hpp b/src/daemon/camera.hppindex 35260b6..60349ba 100644--- a/src/daemon/camera.hpp+++ b/src/daemon/camera.hpp@@ -6,9 +6,13 @@namespace tether {+ // Whether this build can decode the phone's H.264; without it, camera is not advertised.+ bool camera_supported();+// 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.+ // stream replaces the running one. When a stream ends the node stays and shows+ // black, so apps keep it selected for the next stream. Never throws.void start_camera_stream(int fd, SSL* ssl, const nlohmann::json& start, std::string leftover, const std::string& device_name);diff --git a/src/daemon/main.cpp b/src/daemon/main.cppindex b8f084d..32433c3 100644--- a/src/daemon/main.cpp+++ b/src/daemon/main.cpp@@ -116,7 +116,10 @@ int main(int argc, char** argv) {tether::TcpServer tcp_srv(loop, port);#ifdef TETHER_CAMERA- tcp_srv.set_camera_handler(tether::start_camera_stream);+ if (tether::camera_supported())+ tcp_srv.set_camera_handler(tether::start_camera_stream);+ else+ debug::log(WARN, "camera: this FFmpeg has no H.264 decoder; the phone camera is disabled");#endiftether::UnixServer unix_srv(loop, tcp_srv);if (!unix_srv.start()) {