askrobots / network-computer-ios
Public
iPhone client for network-computer: see and drive a remote Linux/Mac desktop over WebRTC, phone as trackpad and keyboard, desktop on a TV via AirPlay.
PeerClient.swift · 6.8 KB · 156 lines
| 1 | import Foundation |
| 2 | import WebRTC |
| 3 | |
| 4 | /// One WebRTC session to a host: receives video and audio, sends input on a data channel. |
| 5 | final class PeerClient: NSObject { |
| 6 | struct Stats { |
| 7 | var path = "?" |
| 8 | var rttMs = 0.0 |
| 9 | var fps = 0.0 |
| 10 | var mbps = 0.0 |
| 11 | var width = 0, height = 0 |
| 12 | var codec = "?" |
| 13 | var relayed = false |
| 14 | } |
| 15 | |
| 16 | var onVideoTrack: ((RTCVideoTrack) -> Void)? |
| 17 | var onIceCandidate: ((IceCandidateJSON) -> Void)? |
| 18 | var onState: ((RTCPeerConnectionState) -> Void)? |
| 19 | var onStats: ((Stats) -> Void)? |
| 20 | |
| 21 | private static let factory: RTCPeerConnectionFactory = { |
| 22 | RTCPeerConnectionFactory(encoderFactory: RTCDefaultVideoEncoderFactory(), |
| 23 | decoderFactory: RTCDefaultVideoDecoderFactory()) |
| 24 | }() |
| 25 | |
| 26 | private var pc: RTCPeerConnection! |
| 27 | private var input: RTCDataChannel? |
| 28 | private var statsTimer: Timer? |
| 29 | private var lastBytes: UInt64 = 0, lastFrames: UInt64 = 0, lastStatsAt = Date() |
| 30 | private let encoder = JSONEncoder() |
| 31 | |
| 32 | init(iceServers: [ICEServerJSON], relayOnly: Bool) { |
| 33 | super.init() |
| 34 | let cfg = RTCConfiguration() |
| 35 | cfg.sdpSemantics = .unifiedPlan |
| 36 | cfg.continualGatheringPolicy = .gatherContinually |
| 37 | cfg.iceServers = iceServers.map { RTCIceServer(urlStrings: $0.urls, username: $0.username, credential: $0.credential) } |
| 38 | if relayOnly { cfg.iceTransportPolicy = .relay } |
| 39 | let constraints = RTCMediaConstraints(mandatoryConstraints: nil, optionalConstraints: nil) |
| 40 | pc = Self.factory.peerConnection(with: cfg, constraints: constraints, delegate: self) |
| 41 | |
| 42 | let recv = RTCRtpTransceiverInit() |
| 43 | recv.direction = .recvOnly |
| 44 | pc.addTransceiver(of: .video, init: recv) |
| 45 | pc.addTransceiver(of: .audio, init: recv) |
| 46 | |
| 47 | let dcCfg = RTCDataChannelConfiguration() |
| 48 | dcCfg.isOrdered = false |
| 49 | dcCfg.maxRetransmits = 0 |
| 50 | input = pc.dataChannel(forLabel: "input", configuration: dcCfg) |
| 51 | } |
| 52 | |
| 53 | func createOffer() async throws -> String { |
| 54 | let constraints = RTCMediaConstraints(mandatoryConstraints: nil, optionalConstraints: nil) |
| 55 | let offer = try await pc.offer(for: constraints) |
| 56 | try await pc.setLocalDescription(offer) |
| 57 | return offer.sdp |
| 58 | } |
| 59 | |
| 60 | func setAnswer(_ sdp: String) async throws { |
| 61 | try await pc.setRemoteDescription(RTCSessionDescription(type: .answer, sdp: sdp)) |
| 62 | } |
| 63 | |
| 64 | func addCandidate(_ c: IceCandidateJSON) { |
| 65 | let cand = RTCIceCandidate(sdp: c.candidate, sdpMLineIndex: c.sdpMLineIndex ?? 0, sdpMid: c.sdpMid) |
| 66 | pc.add(cand) { _ in } |
| 67 | } |
| 68 | |
| 69 | func send(_ e: InputEvent) { |
| 70 | guard let input, input.readyState == .open, let data = try? encoder.encode(e) else { return } |
| 71 | input.sendData(RTCDataBuffer(data: data, isBinary: false)) |
| 72 | } |
| 73 | |
| 74 | func close() { |
| 75 | statsTimer?.invalidate() |
| 76 | input?.close() |
| 77 | pc.close() |
| 78 | } |
| 79 | |
| 80 | // MARK: stats |
| 81 | |
| 82 | private func startStats() { |
| 83 | statsTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in self?.pollStats() } |
| 84 | } |
| 85 | |
| 86 | private func pollStats() { |
| 87 | pc.statistics { [weak self] report in |
| 88 | guard let self else { return } |
| 89 | var s = Stats() |
| 90 | var candidates: [String: RTCStatistics] = [:] |
| 91 | var pair: RTCStatistics? |
| 92 | var inbound: RTCStatistics? |
| 93 | var codecs: [String: String] = [:] |
| 94 | for (id, st) in report.statistics { |
| 95 | switch st.type { |
| 96 | case "candidate-pair": |
| 97 | if (st.values["nominated"] as? Bool) == true, (st.values["state"] as? String) == "succeeded" { pair = st } |
| 98 | case "local-candidate", "remote-candidate": candidates[id] = st |
| 99 | case "inbound-rtp": if (st.values["kind"] as? String) == "video" { inbound = st } |
| 100 | case "codec": codecs[id] = st.values["mimeType"] as? String |
| 101 | default: break |
| 102 | } |
| 103 | } |
| 104 | if let pair { |
| 105 | let l = candidates[pair.values["localCandidateId"] as? String ?? ""]?.values["candidateType"] as? String ?? "?" |
| 106 | let r = candidates[pair.values["remoteCandidateId"] as? String ?? ""]?.values["candidateType"] as? String ?? "?" |
| 107 | s.relayed = (l == "relay" || r == "relay") |
| 108 | s.path = "\(l) -> \(r)" |
| 109 | s.rttMs = ((pair.values["currentRoundTripTime"] as? Double) ?? 0) * 1000 |
| 110 | } |
| 111 | if let inbound { |
| 112 | let now = Date() |
| 113 | let dt = now.timeIntervalSince(self.lastStatsAt) |
| 114 | let bytes = (inbound.values["bytesReceived"] as? UInt64) ?? 0 |
| 115 | let frames = (inbound.values["framesDecoded"] as? UInt64) ?? 0 |
| 116 | if dt > 0 { |
| 117 | s.mbps = Double(bytes &- self.lastBytes) * 8 / dt / 1e6 |
| 118 | s.fps = Double(frames &- self.lastFrames) / dt |
| 119 | } |
| 120 | self.lastBytes = bytes; self.lastFrames = frames; self.lastStatsAt = now |
| 121 | s.width = Int((inbound.values["frameWidth"] as? UInt64) ?? 0) |
| 122 | s.height = Int((inbound.values["frameHeight"] as? UInt64) ?? 0) |
| 123 | s.codec = codecs[inbound.values["codecId"] as? String ?? ""] ?? "?" |
| 124 | } |
| 125 | DispatchQueue.main.async { self.onStats?(s) } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | extension PeerClient: RTCPeerConnectionDelegate { |
| 131 | func peerConnection(_ peerConnection: RTCPeerConnection, didChange stateChanged: RTCSignalingState) {} |
| 132 | func peerConnection(_ peerConnection: RTCPeerConnection, didAdd stream: RTCMediaStream) {} |
| 133 | func peerConnection(_ peerConnection: RTCPeerConnection, didRemove stream: RTCMediaStream) {} |
| 134 | func peerConnectionShouldNegotiate(_ peerConnection: RTCPeerConnection) {} |
| 135 | func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) {} |
| 136 | func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceGatheringState) {} |
| 137 | func peerConnection(_ peerConnection: RTCPeerConnection, didRemove candidates: [RTCIceCandidate]) {} |
| 138 | func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) {} |
| 139 | |
| 140 | func peerConnection(_ peerConnection: RTCPeerConnection, didGenerate candidate: RTCIceCandidate) { |
| 141 | onIceCandidate?(IceCandidateJSON(candidate: candidate.sdp, sdpMid: candidate.sdpMid, sdpMLineIndex: candidate.sdpMLineIndex)) |
| 142 | } |
| 143 | |
| 144 | func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCPeerConnectionState) { |
| 145 | DispatchQueue.main.async { |
| 146 | self.onState?(newState) |
| 147 | if newState == .connected { self.startStats() } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func peerConnection(_ peerConnection: RTCPeerConnection, didAdd rtpReceiver: RTCRtpReceiver, streams mediaStreams: [RTCMediaStream]) { |
| 152 | if let track = rtpReceiver.track as? RTCVideoTrack { |
| 153 | DispatchQueue.main.async { self.onVideoTrack?(track) } |
| 154 | } |
| 155 | } |
| 156 | } |