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
display.go · 2.6 KB · 103 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "math" |
| 8 | "os/exec" |
| 9 | "runtime" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "sync" |
| 13 | |
| 14 | "github.com/askrobots/network-computer/internal/proto" |
| 15 | ) |
| 16 | |
| 17 | // Clients can ask for a screen size and UI scale. On Linux the host resizes |
| 18 | // its X screen and sets the desktop's scale (nc-display, from provisioning); |
| 19 | // every OS then restarts capture at the new size, so what the client sees is |
| 20 | // pixel for pixel instead of scaled. |
| 21 | |
| 22 | const ( |
| 23 | minW, minH = 640, 360 |
| 24 | maxW, maxH = 2560, 1600 |
| 25 | ) |
| 26 | |
| 27 | type displayReq struct { |
| 28 | W, H int |
| 29 | S float64 |
| 30 | } |
| 31 | |
| 32 | // normalize clamps a request to what the host will do: sizes within bounds, |
| 33 | // width a multiple of 8 (display mode timings need it), height even (4:2:0 |
| 34 | // video needs it), scale in 5% steps. |
| 35 | func normalize(e proto.InputEvent) displayReq { |
| 36 | clamp := func(v, lo, hi int) int { return int(math.Max(float64(lo), math.Min(float64(hi), float64(v)))) } |
| 37 | w, h := clamp(e.W, minW, maxW)/8*8, clamp(e.H, minH, maxH)/2*2 |
| 38 | s := e.S |
| 39 | if s <= 0 { |
| 40 | s = 1 |
| 41 | } |
| 42 | s = math.Round(math.Max(1, math.Min(3, s))*20) / 20 |
| 43 | return displayReq{w, h, s} |
| 44 | } |
| 45 | |
| 46 | // bitrateFor scales the configured bitrate (tuned for 1280x720) by pixel count. |
| 47 | func bitrateFor(base string, w, h int) string { |
| 48 | b := parseBitrate(base) |
| 49 | if b <= 0 { |
| 50 | b = 4_000_000 |
| 51 | } |
| 52 | scaled := float64(b) * float64(w*h) / (1280 * 720) |
| 53 | scaled = math.Max(1_500_000, math.Min(16_000_000, scaled)) |
| 54 | return fmt.Sprintf("%dk", int(scaled/1000)) |
| 55 | } |
| 56 | |
| 57 | func parseBitrate(s string) int { |
| 58 | s = strings.TrimSpace(strings.ToLower(s)) |
| 59 | mult := 1 |
| 60 | switch { |
| 61 | case strings.HasSuffix(s, "m"): |
| 62 | mult, s = 1_000_000, strings.TrimSuffix(s, "m") |
| 63 | case strings.HasSuffix(s, "k"): |
| 64 | mult, s = 1_000, strings.TrimSuffix(s, "k") |
| 65 | } |
| 66 | f, err := strconv.ParseFloat(s, 64) |
| 67 | if err != nil { |
| 68 | return 0 |
| 69 | } |
| 70 | return int(f * float64(mult)) |
| 71 | } |
| 72 | |
| 73 | // displayControl applies requests one at a time and skips repeats. The X |
| 74 | // screen is shared, so the most recent request from any client wins. |
| 75 | type displayControl struct { |
| 76 | mu sync.Mutex |
| 77 | last displayReq |
| 78 | } |
| 79 | |
| 80 | func (d *displayControl) apply(req displayReq) (changed bool) { |
| 81 | d.mu.Lock() |
| 82 | defer d.mu.Unlock() |
| 83 | if req == d.last { |
| 84 | return false |
| 85 | } |
| 86 | if runtime.GOOS == "linux" { |
| 87 | out, err := exec.CommandContext(context.Background(), "nc-display", |
| 88 | strconv.Itoa(req.W), strconv.Itoa(req.H), strconv.FormatFloat(req.S, 'f', 2, 64)).CombinedOutput() |
| 89 | if err != nil { |
| 90 | log.Printf("display: nc-display %dx%d @%.2f failed: %v %s", req.W, req.H, req.S, err, strings.TrimSpace(string(out))) |
| 91 | return false |
| 92 | } |
| 93 | } |
| 94 | log.Printf("display: %dx%d at %.0f%% scale", req.W, req.H, req.S*100) |
| 95 | d.last = req |
| 96 | return true |
| 97 | } |
| 98 | |
| 99 | func (d *displayControl) current() displayReq { |
| 100 | d.mu.Lock() |
| 101 | defer d.mu.Unlock() |
| 102 | return d.last |
| 103 | } |