askrobots / network-computer
Public
Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.
Dan Quellhorst
Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353)
a1d92f3
1 month, 3 weeks ago
History
main.go · 18.6 KB · 598 lines
| 1 | // nc-probe: headless client. Connects to a host through the rendezvous the |
| 2 | // same way the phone will, receives the video track, and reports which ICE |
| 3 | // path was chosen and how much video arrives. Use it to answer the NAT |
| 4 | // questions from any network without a phone or a browser. |
| 5 | package main |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "encoding/base64" |
| 10 | "encoding/json" |
| 11 | "flag" |
| 12 | "fmt" |
| 13 | "log" |
| 14 | "net/http" |
| 15 | "os" |
| 16 | "os/exec" |
| 17 | "path/filepath" |
| 18 | "sort" |
| 19 | "strings" |
| 20 | "time" |
| 21 | "unicode/utf8" |
| 22 | |
| 23 | "github.com/coder/websocket" |
| 24 | "github.com/pion/rtp" |
| 25 | "github.com/pion/webrtc/v4" |
| 26 | "github.com/pion/webrtc/v4/pkg/media" |
| 27 | "github.com/pion/webrtc/v4/pkg/media/ivfreader" |
| 28 | |
| 29 | "github.com/askrobots/network-computer/internal/oggopus" |
| 30 | "github.com/askrobots/network-computer/internal/proto" |
| 31 | "github.com/askrobots/network-computer/internal/tlspin" |
| 32 | ) |
| 33 | |
| 34 | func main() { |
| 35 | rz := flag.String("rendezvous", "http://127.0.0.1:8765", "rendezvous base URL") |
| 36 | hostName := flag.String("host", "", "host name to connect to (default: first registered)") |
| 37 | dur := flag.Duration("duration", 10*time.Second, "how long to receive before exiting") |
| 38 | relayOnly := flag.Bool("relay", false, "force TURN relay (tests the fallback path)") |
| 39 | user := flag.String("user", envOr("NC_USER", "nc"), "rendezvous basic auth username") |
| 40 | password := flag.String("password", os.Getenv("NC_PASSWORD"), "rendezvous basic auth password (env NC_PASSWORD)") |
| 41 | pin := flag.String("pin", os.Getenv("NC_PIN"), "host PIN (env NC_PIN)") |
| 42 | tlsFP := flag.String("tls-fingerprint", os.Getenv("NC_TLS_FP"), "pin the rendezvous certificate by SHA-256 (env NC_TLS_FP)") |
| 43 | pair := flag.String("pair", os.Getenv("NC_PAIR"), "pairing token from an earlier connection, used instead of the PIN (env NC_PAIR)") |
| 44 | sendInput := flag.Bool("input", false, "send a few test input events over the data channel") |
| 45 | tz := flag.String("tz", "", "tell the host this client's time zone, e.g. America/New_York") |
| 46 | clickAt := flag.String("click", "", "click at X,Y (0..1 across the screen) with no move first, as a click whose move arrived late") |
| 47 | keyboard := flag.String("keyboard", "", "tell the host this client's keyboard layout over the control channel, e.g. us:dvorak") |
| 48 | display := flag.String("display", "", "ask the host for a screen size and scale over the control channel, e.g. 1600x900@1.5") |
| 49 | clip := flag.String("clip", "", "put this text (or @file's contents) on the host clipboard, then log what the host sends back; \"-\" only watches, \"?\" asks for the host clipboard as it is") |
| 50 | sendPath := flag.String("send-file", "", "send this file to the host the way a drop on the web client does") |
| 51 | recvDir := flag.String("recv-dir", "", "accept files the host sends (nc-send) and save them here") |
| 52 | camTest := flag.Duration("camera-test", 0, "after this long, send a test pattern as the probe's camera (as the app attaches its camera on demand), to test camera passthrough") |
| 53 | micTone := flag.Int("mic-tone", 0, "send a sine tone of this frequency (Hz) as the probe's microphone, to test mic passthrough") |
| 54 | flag.Parse() |
| 55 | |
| 56 | httpc := tlspin.Client(*tlsFP) |
| 57 | authGet := func(url string) *http.Response { |
| 58 | req, _ := http.NewRequest("GET", url, nil) |
| 59 | req.SetBasicAuth(*user, *password) |
| 60 | resp, err := httpc.Do(req) |
| 61 | if err != nil { |
| 62 | log.Fatal(err) |
| 63 | } |
| 64 | if resp.StatusCode == http.StatusUnauthorized { |
| 65 | log.Fatal("rendezvous rejected the password (use -password or NC_PASSWORD)") |
| 66 | } |
| 67 | return resp |
| 68 | } |
| 69 | var cfg proto.Config |
| 70 | resp := authGet(*rz + "/config") |
| 71 | json.NewDecoder(resp.Body).Decode(&cfg) |
| 72 | resp.Body.Close() |
| 73 | |
| 74 | if *hostName == "" { |
| 75 | var hosts []string |
| 76 | resp := authGet(*rz + "/hosts") |
| 77 | json.NewDecoder(resp.Body).Decode(&hosts) |
| 78 | resp.Body.Close() |
| 79 | if len(hosts) == 0 { |
| 80 | log.Fatal("no hosts registered") |
| 81 | } |
| 82 | *hostName = hosts[0] |
| 83 | } |
| 84 | |
| 85 | var ice []webrtc.ICEServer |
| 86 | for _, s := range cfg.ICEServers { |
| 87 | ice = append(ice, webrtc.ICEServer{URLs: s.URLs, Username: s.Username, Credential: s.Credential}) |
| 88 | } |
| 89 | rtcCfg := webrtc.Configuration{ICEServers: ice} |
| 90 | if *relayOnly { |
| 91 | rtcCfg.ICETransportPolicy = webrtc.ICETransportPolicyRelay |
| 92 | } |
| 93 | |
| 94 | ctx, cancel := context.WithTimeout(context.Background(), *dur+15*time.Second) |
| 95 | defer cancel() |
| 96 | |
| 97 | wsURL := strings.Replace(strings.Replace(*rz, "https://", "wss://", 1), "http://", "ws://", 1) + "/ws" |
| 98 | hdr := http.Header{} |
| 99 | hdr.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(*user+":"+*password))) |
| 100 | ws, _, err := websocket.Dial(ctx, wsURL, &websocket.DialOptions{HTTPHeader: hdr, HTTPClient: httpc}) |
| 101 | if err != nil { |
| 102 | log.Fatal(err) |
| 103 | } |
| 104 | defer ws.CloseNow() |
| 105 | ws.SetReadLimit(4 << 20) |
| 106 | send := func(m proto.Message) { |
| 107 | b, _ := json.Marshal(m) |
| 108 | ws.Write(ctx, websocket.MessageText, b) |
| 109 | } |
| 110 | |
| 111 | pc, err := webrtc.NewPeerConnection(rtcCfg) |
| 112 | if err != nil { |
| 113 | log.Fatal(err) |
| 114 | } |
| 115 | defer pc.Close() |
| 116 | pc.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo, webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionRecvonly}) |
| 117 | var micTrack *webrtc.TrackLocalStaticSample |
| 118 | if *micTone > 0 { |
| 119 | // sendrecv audio: we hear the desktop and send our "microphone" |
| 120 | micTrack, _ = webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 2}, "mic", "probe") |
| 121 | if _, err := pc.AddTrack(micTrack); err != nil { |
| 122 | log.Fatalf("mic track: %v", err) |
| 123 | } |
| 124 | } else { |
| 125 | pc.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio, webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionRecvonly}) |
| 126 | } |
| 127 | // the camera: its own sending video transceiver, empty until "turned on" |
| 128 | var camTrack *webrtc.TrackLocalStaticSample |
| 129 | if *camTest > 0 { |
| 130 | camTrack, _ = webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "camera", "probe-camera") |
| 131 | if _, err := pc.AddTransceiverFromTrack(camTrack, webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly}); err != nil { |
| 132 | log.Fatalf("camera track: %v", err) |
| 133 | } |
| 134 | } |
| 135 | dc, _ := pc.CreateDataChannel("input", nil) |
| 136 | if *keyboard != "" { |
| 137 | kc, _ := pc.CreateDataChannel("control", nil) |
| 138 | kc.OnOpen(func() { |
| 139 | b, _ := json.Marshal(proto.InputEvent{T: "keyboard", Layout: *keyboard}) |
| 140 | kc.Send(b) |
| 141 | log.Printf("told the host: keyboard %s", *keyboard) |
| 142 | }) |
| 143 | } |
| 144 | if *display != "" { |
| 145 | var w, hgt int |
| 146 | scale := 1.0 |
| 147 | if _, err := fmt.Sscanf(strings.Replace(*display, "@", " ", 1), "%dx%d %f", &w, &hgt, &scale); err != nil { |
| 148 | if _, err := fmt.Sscanf(*display, "%dx%d", &w, &hgt); err != nil { |
| 149 | log.Fatalf("-display wants WxH or WxH@scale, got %q", *display) |
| 150 | } |
| 151 | } |
| 152 | ctl, _ := pc.CreateDataChannel("control", nil) // reliable, like the web client's |
| 153 | ctl.OnOpen(func() { |
| 154 | b, _ := json.Marshal(proto.InputEvent{T: "display", W: w, H: hgt, S: scale}) |
| 155 | ctl.Send(b) |
| 156 | log.Printf("asked for screen %dx%d at %.0f%%", w, hgt, scale*100) |
| 157 | }) |
| 158 | } |
| 159 | |
| 160 | if *clip != "" { |
| 161 | watchClipboard(pc, *clip) |
| 162 | } |
| 163 | if *sendPath != "" { |
| 164 | probeSendFile(pc, *sendPath) |
| 165 | } |
| 166 | if *recvDir != "" { |
| 167 | probeRecvFiles(pc, *recvDir) |
| 168 | } |
| 169 | |
| 170 | connected := make(chan struct{}) |
| 171 | pc.OnConnectionStateChange(func(s webrtc.PeerConnectionState) { |
| 172 | log.Printf("pc: %s", s) |
| 173 | if s == webrtc.PeerConnectionStateConnected { |
| 174 | close(connected) |
| 175 | } |
| 176 | if s == webrtc.PeerConnectionStateFailed { |
| 177 | log.Fatal("connection failed") |
| 178 | } |
| 179 | }) |
| 180 | pc.OnICECandidate(func(c *webrtc.ICECandidate) { |
| 181 | if c != nil { |
| 182 | b, _ := json.Marshal(c.ToJSON()) |
| 183 | send(proto.Message{Type: "ice", To: *hostName, Candidate: b}) |
| 184 | } |
| 185 | }) |
| 186 | |
| 187 | type stats struct { |
| 188 | packets, bytes, frames, keyframes int |
| 189 | audioPackets, audioBytes int |
| 190 | audioGaps []time.Duration // time between audio packets |
| 191 | audioTSDelta map[uint32]int // RTP timestamp step -> count (960 = one 20 ms frame) |
| 192 | first time.Time |
| 193 | } |
| 194 | st := &stats{} |
| 195 | start := time.Now() |
| 196 | done := make(chan struct{}) |
| 197 | pc.OnTrack(func(track *webrtc.TrackRemote, r *webrtc.RTPReceiver) { |
| 198 | log.Printf("track: %s %s", track.Kind(), track.Codec().MimeType) |
| 199 | if track.Kind() != webrtc.RTPCodecTypeVideo { |
| 200 | go func() { |
| 201 | var last time.Time |
| 202 | var lastTS uint32 |
| 203 | st.audioTSDelta = map[uint32]int{} |
| 204 | for { |
| 205 | pkt, _, err := track.ReadRTP() |
| 206 | if err != nil { |
| 207 | return |
| 208 | } |
| 209 | now := time.Now() |
| 210 | if !last.IsZero() { |
| 211 | st.audioGaps = append(st.audioGaps, now.Sub(last)) |
| 212 | } |
| 213 | last = now |
| 214 | if st.audioPackets > 0 { |
| 215 | st.audioTSDelta[pkt.Timestamp-lastTS]++ |
| 216 | } |
| 217 | lastTS = pkt.Timestamp |
| 218 | st.audioPackets++ |
| 219 | st.audioBytes += len(pkt.Payload) |
| 220 | } |
| 221 | }() |
| 222 | return |
| 223 | } |
| 224 | go func() { |
| 225 | defer close(done) |
| 226 | for { |
| 227 | pkt, _, err := track.ReadRTP() |
| 228 | if err != nil { |
| 229 | return |
| 230 | } |
| 231 | if st.first.IsZero() { |
| 232 | st.first = time.Now() |
| 233 | log.Printf("first video packet after %s", time.Since(start).Round(time.Millisecond)) |
| 234 | } |
| 235 | st.packets++ |
| 236 | st.bytes += len(pkt.Payload) |
| 237 | if pkt.Marker { |
| 238 | st.frames++ |
| 239 | } |
| 240 | if isKeyframe(pkt) { |
| 241 | st.keyframes++ |
| 242 | } |
| 243 | } |
| 244 | }() |
| 245 | }) |
| 246 | |
| 247 | offer, err := pc.CreateOffer(nil) |
| 248 | if err != nil { |
| 249 | log.Fatal(err) |
| 250 | } |
| 251 | pc.SetLocalDescription(offer) |
| 252 | start = time.Now() |
| 253 | send(proto.Message{Type: "offer", To: *hostName, SDP: offer.SDP, PIN: *pin, Pair: *pair}) |
| 254 | log.Printf("offer sent to %q via %s", *hostName, wsURL) |
| 255 | |
| 256 | go func() { |
| 257 | for { |
| 258 | _, data, err := ws.Read(ctx) |
| 259 | if err != nil { |
| 260 | return |
| 261 | } |
| 262 | var m proto.Message |
| 263 | json.Unmarshal(data, &m) |
| 264 | switch m.Type { |
| 265 | case "answer": |
| 266 | if m.Pair != "" { |
| 267 | fmt.Printf("pairing token: %s\n", m.Pair) |
| 268 | } |
| 269 | pc.SetRemoteDescription(webrtc.SessionDescription{Type: webrtc.SDPTypeAnswer, SDP: m.SDP}) |
| 270 | case "ice": |
| 271 | var c webrtc.ICECandidateInit |
| 272 | if json.Unmarshal(m.Candidate, &c) == nil { |
| 273 | pc.AddICECandidate(c) |
| 274 | } |
| 275 | case "error": |
| 276 | log.Fatalf("rendezvous: %s", m.Error) |
| 277 | } |
| 278 | } |
| 279 | }() |
| 280 | |
| 281 | select { |
| 282 | case <-connected: |
| 283 | case <-time.After(15 * time.Second): |
| 284 | log.Fatal("timeout waiting for connection") |
| 285 | } |
| 286 | log.Printf("connected after %s", time.Since(start).Round(time.Millisecond)) |
| 287 | time.Sleep(300 * time.Millisecond) |
| 288 | if p, err := pc.SCTP().Transport().ICETransport().GetSelectedCandidatePair(); err == nil && p != nil { |
| 289 | kind := "direct" |
| 290 | if p.Local.Typ == webrtc.ICECandidateTypeRelay || p.Remote.Typ == webrtc.ICECandidateTypeRelay { |
| 291 | kind = "RELAYED" |
| 292 | } |
| 293 | fmt.Printf("path: %s local %s %s:%d <-> remote %s %s:%d\n", kind, p.Local.Typ, p.Local.Address, p.Local.Port, p.Remote.Typ, p.Remote.Address, p.Remote.Port) |
| 294 | } |
| 295 | |
| 296 | if micTrack != nil { |
| 297 | go sendTone(ctx, micTrack, *micTone) |
| 298 | log.Printf("sending a %d Hz tone as the microphone", *micTone) |
| 299 | } |
| 300 | if camTrack != nil { |
| 301 | go func() { |
| 302 | select { |
| 303 | case <-time.After(*camTest): |
| 304 | sendCamera(ctx, camTrack) |
| 305 | case <-ctx.Done(): |
| 306 | } |
| 307 | }() |
| 308 | } |
| 309 | |
| 310 | if *tz != "" { |
| 311 | tc, _ := pc.CreateDataChannel("control", nil) |
| 312 | tc.OnOpen(func() { |
| 313 | b, _ := json.Marshal(proto.InputEvent{T: "tz", Text: *tz}) |
| 314 | tc.Send(b) |
| 315 | log.Printf("told the host: time zone %s", *tz) |
| 316 | }) |
| 317 | } |
| 318 | if *clickAt != "" { |
| 319 | var x, y float64 |
| 320 | if _, err := fmt.Sscanf(*clickAt, "%f,%f", &x, &y); err != nil { |
| 321 | log.Fatalf("-click wants X,Y, got %q", *clickAt) |
| 322 | } |
| 323 | go func() { |
| 324 | <-time.After(time.Second) |
| 325 | for _, t := range []string{"md", "mu"} { |
| 326 | b, _ := json.Marshal(proto.InputEvent{T: t, B: 0, X: x, Y: y, At: true}) |
| 327 | dc.Send(b) |
| 328 | } |
| 329 | log.Printf("clicked at %.3f,%.3f", x, y) |
| 330 | }() |
| 331 | } |
| 332 | |
| 333 | if *sendInput { |
| 334 | go func() { |
| 335 | <-time.After(time.Second) |
| 336 | for i := 0; i < 5 && dc.ReadyState() == webrtc.DataChannelStateOpen; i++ { |
| 337 | b, _ := json.Marshal(proto.InputEvent{T: "mm", X: 0.5 + float64(i)/50, Y: 0.5}) |
| 338 | dc.Send(b) |
| 339 | time.Sleep(50 * time.Millisecond) |
| 340 | } |
| 341 | for _, e := range []proto.InputEvent{{T: "md", B: 0}, {T: "mu", B: 0}, {T: "kd", Code: "ShiftLeft"}, {T: "ku", Code: "ShiftLeft"}} { |
| 342 | b, _ := json.Marshal(e) |
| 343 | dc.Send(b) |
| 344 | } |
| 345 | log.Printf("sent test input events") |
| 346 | }() |
| 347 | } |
| 348 | |
| 349 | time.Sleep(*dur) |
| 350 | el := time.Since(st.first).Seconds() |
| 351 | if st.first.IsZero() { |
| 352 | fmt.Println("NO VIDEO RECEIVED") |
| 353 | os.Exit(1) |
| 354 | } |
| 355 | fmt.Printf("video: %d packets, %d frames (%d keyframes), %.1f fps, %.1f Mbit/s over %.1fs\n", |
| 356 | st.packets, st.frames, st.keyframes, float64(st.frames)/el, float64(st.bytes)*8/el/1e6, el) |
| 357 | if st.audioPackets > 0 { |
| 358 | fmt.Printf("audio: %d packets, %.0f kbit/s (opus, ~50 packets/s expected)\n", st.audioPackets, float64(st.audioBytes)*8/el/1e3) |
| 359 | if g := st.audioGaps; len(g) > 10 { |
| 360 | sort.Slice(g, func(i, j int) bool { return g[i] < g[j] }) |
| 361 | pct := func(p float64) time.Duration { return g[int(p*float64(len(g)-1))] } |
| 362 | bursts := 0 |
| 363 | for _, d := range g { |
| 364 | if d < 5*time.Millisecond { |
| 365 | bursts++ |
| 366 | } |
| 367 | } |
| 368 | fmt.Printf("audio timing: gaps p10 %v, p50 %v, p90 %v, p99 %v, max %v; %.0f%% arrive <5ms apart (bursts). 20ms each = smooth\n", |
| 369 | pct(.10).Round(100*time.Microsecond), pct(.5).Round(100*time.Microsecond), pct(.9).Round(100*time.Microsecond), |
| 370 | pct(.99).Round(100*time.Microsecond), g[len(g)-1].Round(100*time.Microsecond), 100*float64(bursts)/float64(len(g))) |
| 371 | fmt.Printf("audio timestamp steps (960 = one 20 ms frame per packet): %v\n", st.audioTSDelta) |
| 372 | } |
| 373 | } else { |
| 374 | fmt.Println("audio: none received (host started without -audio-device, or nothing playing)") |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // isKeyframe checks the H.264 payload for an IDR NAL (single NAL, STAP-A, or FU-A start). |
| 379 | func envOr(k, def string) string { |
| 380 | if v := os.Getenv(k); v != "" { |
| 381 | return v |
| 382 | } |
| 383 | return def |
| 384 | } |
| 385 | |
| 386 | func isKeyframe(p *rtp.Packet) bool { |
| 387 | pl := p.Payload |
| 388 | if len(pl) < 2 { |
| 389 | return false |
| 390 | } |
| 391 | switch t := pl[0] & 0x1f; { |
| 392 | case t == 5: |
| 393 | return true |
| 394 | case t == 24: // STAP-A |
| 395 | i := 1 |
| 396 | for i+2 < len(pl) { |
| 397 | sz := int(pl[i])<<8 | int(pl[i+1]) |
| 398 | i += 2 |
| 399 | if i < len(pl) && pl[i]&0x1f == 5 { |
| 400 | return true |
| 401 | } |
| 402 | i += sz |
| 403 | } |
| 404 | case t == 28: // FU-A |
| 405 | return pl[1]&0x80 != 0 && pl[1]&0x1f == 5 |
| 406 | } |
| 407 | return false |
| 408 | } |
| 409 | |
| 410 | // sendTone encodes a sine wave to Opus with ffmpeg and sends it as a |
| 411 | // microphone track, paced by the Ogg granule positions. |
| 412 | func sendTone(ctx context.Context, track *webrtc.TrackLocalStaticSample, hz int) { |
| 413 | cmd := exec.CommandContext(ctx, "ffmpeg", "-hide_banner", "-loglevel", "error", "-re", |
| 414 | "-f", "lavfi", "-i", fmt.Sprintf("sine=frequency=%d:sample_rate=48000", hz), "-ac", "2", |
| 415 | "-c:a", "libopus", "-b:a", "64k", "-frame_duration", "20", "-page_duration", "20000", |
| 416 | "-f", "opus", "pipe:1") |
| 417 | out, err := cmd.StdoutPipe() |
| 418 | if err == nil { |
| 419 | err = cmd.Start() |
| 420 | } |
| 421 | if err != nil { |
| 422 | log.Printf("tone ffmpeg: %v", err) |
| 423 | return |
| 424 | } |
| 425 | defer cmd.Wait() |
| 426 | ogg := oggopus.NewReader(out) |
| 427 | next := time.Now() |
| 428 | for { |
| 429 | pkt, err := ogg.Next() |
| 430 | if err != nil { |
| 431 | return |
| 432 | } |
| 433 | d := oggopus.Duration(pkt) |
| 434 | if d <= 0 { |
| 435 | d = 20 * time.Millisecond |
| 436 | } |
| 437 | time.Sleep(time.Until(next)) |
| 438 | next = next.Add(d) |
| 439 | if err := track.WriteSample(media.Sample{Data: pkt, Duration: d}); err != nil { |
| 440 | return |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // watchClipboard exercises clipboard sync on its own control channel: it sends |
| 446 | // text the way the web client pastes (in pieces), asks for the host's clipboard |
| 447 | // the way a copy key does, and logs every clipboard the host sends. |
| 448 | func watchClipboard(pc *webrtc.PeerConnection, arg string) { |
| 449 | text := arg |
| 450 | if strings.HasPrefix(arg, "@") { |
| 451 | b, err := os.ReadFile(arg[1:]) |
| 452 | if err != nil { |
| 453 | log.Fatalf("-clip: %v", err) |
| 454 | } |
| 455 | text = string(b) |
| 456 | } |
| 457 | ask := "clipget" |
| 458 | switch arg { |
| 459 | case "-": |
| 460 | text = "" |
| 461 | case "?": |
| 462 | text, ask = "", "clipnow" |
| 463 | } |
| 464 | ctl, _ := pc.CreateDataChannel("control", nil) |
| 465 | send := func(ev proto.InputEvent) { b, _ := json.Marshal(ev); ctl.Send(b) } |
| 466 | ctl.OnOpen(func() { |
| 467 | if text != "" { |
| 468 | rest := text |
| 469 | for rest != "" { |
| 470 | n := len(rest) |
| 471 | if n > 8192 { |
| 472 | n = 8192 |
| 473 | for n > 0 && !utf8.RuneStart(rest[n]) { |
| 474 | n-- |
| 475 | } |
| 476 | } |
| 477 | send(proto.InputEvent{T: "clip", Text: rest[:n], More: n < len(rest)}) |
| 478 | rest = rest[n:] |
| 479 | } |
| 480 | log.Printf("clipboard: sent %d bytes to the host", len(text)) |
| 481 | } |
| 482 | send(proto.InputEvent{T: ask}) |
| 483 | }) |
| 484 | var in strings.Builder |
| 485 | ctl.OnMessage(func(m webrtc.DataChannelMessage) { |
| 486 | var ev proto.InputEvent |
| 487 | if json.Unmarshal(m.Data, &ev) != nil { |
| 488 | return |
| 489 | } |
| 490 | switch ev.T { |
| 491 | case "clipnone": |
| 492 | log.Printf("clipboard: host reports nothing new copied") |
| 493 | case "clip": |
| 494 | in.WriteString(ev.Text) |
| 495 | if ev.More { |
| 496 | return |
| 497 | } |
| 498 | t := in.String() |
| 499 | in.Reset() |
| 500 | preview := t |
| 501 | if len(preview) > 60 { |
| 502 | preview = preview[:60] + "..." |
| 503 | } |
| 504 | log.Printf("clipboard: host sent %d bytes: %q", len(t), preview) |
| 505 | } |
| 506 | }) |
| 507 | } |
| 508 | |
| 509 | // probeSendFile sends a file on its own "file" channel: header, pieces, "end". |
| 510 | func probeSendFile(pc *webrtc.PeerConnection, path string) { |
| 511 | data, err := os.ReadFile(path) |
| 512 | if err != nil { |
| 513 | log.Fatalf("-send-file: %v", err) |
| 514 | } |
| 515 | fc, _ := pc.CreateDataChannel("file", nil) |
| 516 | fc.OnOpen(func() { |
| 517 | start := time.Now() |
| 518 | b, _ := json.Marshal(map[string]any{"name": filepath.Base(path), "size": len(data)}) |
| 519 | fc.SendText(string(b)) |
| 520 | for i := 0; i < len(data); i += 16 << 10 { |
| 521 | for fc.BufferedAmount() > 4<<20 { |
| 522 | time.Sleep(5 * time.Millisecond) |
| 523 | } |
| 524 | fc.Send(data[i:min(i+16<<10, len(data))]) |
| 525 | } |
| 526 | fc.SendText("end") |
| 527 | log.Printf("file: sent %d bytes in %v", len(data), time.Since(start).Round(time.Millisecond)) |
| 528 | }) |
| 529 | fc.OnMessage(func(m webrtc.DataChannelMessage) { log.Printf("file: host says %q", m.Data) }) |
| 530 | } |
| 531 | |
| 532 | // probeRecvFiles saves files the host pushes, answering like the web client. |
| 533 | func probeRecvFiles(pc *webrtc.PeerConnection, dir string) { |
| 534 | pc.OnDataChannel(func(dc *webrtc.DataChannel) { |
| 535 | if dc.Label() != "file" { |
| 536 | return |
| 537 | } |
| 538 | var name string |
| 539 | var size int64 |
| 540 | var buf []byte |
| 541 | dc.OnMessage(func(m webrtc.DataChannelMessage) { |
| 542 | switch { |
| 543 | case m.IsString && name == "": |
| 544 | var h struct { |
| 545 | Name string `json:"name"` |
| 546 | Size int64 `json:"size"` |
| 547 | } |
| 548 | json.Unmarshal(m.Data, &h) |
| 549 | name, size = filepath.Base(h.Name), h.Size |
| 550 | case m.IsString && string(m.Data) == "end": |
| 551 | if int64(len(buf)) != size { |
| 552 | dc.SendText(fmt.Sprintf("error: got %d of %d", len(buf), size)) |
| 553 | return |
| 554 | } |
| 555 | os.WriteFile(filepath.Join(dir, name), buf, 0o644) |
| 556 | dc.SendText("ok " + name) |
| 557 | log.Printf("file: received %s (%d bytes)", name, len(buf)) |
| 558 | case !m.IsString: |
| 559 | buf = append(buf, m.Data...) |
| 560 | } |
| 561 | }) |
| 562 | }) |
| 563 | } |
| 564 | |
| 565 | // sendCamera sends a VP8 test pattern (with a clock, so frames visibly move) |
| 566 | // as the probe's camera, at 30 fps in real time. |
| 567 | func sendCamera(ctx context.Context, track *webrtc.TrackLocalStaticSample) { |
| 568 | cmd := exec.CommandContext(ctx, "ffmpeg", "-hide_banner", "-loglevel", "error", "-re", |
| 569 | "-f", "lavfi", "-i", "testsrc2=size=640x480:rate=30", |
| 570 | "-c:v", "libvpx", "-deadline", "realtime", "-b:v", "1M", "-g", "30", "-f", "ivf", "pipe:1") |
| 571 | out, err := cmd.StdoutPipe() |
| 572 | if err == nil { |
| 573 | err = cmd.Start() |
| 574 | } |
| 575 | if err != nil { |
| 576 | log.Printf("camera ffmpeg: %v", err) |
| 577 | return |
| 578 | } |
| 579 | defer cmd.Wait() |
| 580 | ivf, _, err := ivfreader.NewWith(out) |
| 581 | if err != nil { |
| 582 | log.Printf("camera ivf: %v", err) |
| 583 | return |
| 584 | } |
| 585 | log.Printf("camera: sending a test pattern") |
| 586 | n := 0 |
| 587 | for { |
| 588 | frame, _, err := ivf.ParseNextFrame() |
| 589 | if err != nil { |
| 590 | log.Printf("camera: sent %d frames", n) |
| 591 | return |
| 592 | } |
| 593 | if err := track.WriteSample(media.Sample{Data: frame, Duration: time.Second / 30}); err != nil { |
| 594 | return |
| 595 | } |
| 596 | n++ |
| 597 | } |
| 598 | } |