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.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/network-computer.git
HTTPS
https://gitstable.com/git/askrobots/network-computer.git
SSH
ssh://git@gitstable.com:2222/askrobots/network-computer.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Dan Quellhorst Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353) a1d92f3 1 month, 3 weeks ago History
voice.go · 2.5 KB · 101 lines
1package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "log"
7 "net/http"
8 "sync"
9 "time"
10
11 "github.com/askrobots/network-computer/internal/proto"
12)
13
14// Voice relay between the client's 🎙️ button and nc-voice on the desk, over
15// the host's local socket (the one nc-send uses):
16// - GET /voice/commands: nc-voice's long-lived stream of {"cmd":"on"|"off"}
17// lines ("once": stop after one phrase; and a "ping" every 30 s);
18// - POST /voice/event {"kind","text"}: what nc-voice heard, said or did,
19// passed to every connected client as {"t":"voice","kind","text"}.
20type voiceRelay struct {
21 mu sync.Mutex
22 sub chan string // nil while nc-voice is not connected
23}
24
25// command tells nc-voice to start or stop listening; false if it isn't running.
26func (v *voiceRelay) command(on, once bool) bool {
27 v.mu.Lock()
28 defer v.mu.Unlock()
29 if v.sub == nil {
30 return false
31 }
32 cmd := `{"cmd":"off"}`
33 switch {
34 case on && once:
35 cmd = `{"cmd":"on","once":true}`
36 case on:
37 cmd = `{"cmd":"on"}`
38 }
39 select {
40 case v.sub <- cmd:
41 default:
42 }
43 return true
44}
45
46func (h *host) handleVoiceCommands(w http.ResponseWriter, r *http.Request) {
47 fl, ok := w.(http.Flusher)
48 if !ok {
49 http.Error(w, "no streaming", http.StatusInternalServerError)
50 return
51 }
52 ch := make(chan string, 8)
53 h.voice.mu.Lock()
54 h.voice.sub = ch
55 h.voice.mu.Unlock()
56 defer func() {
57 h.voice.mu.Lock()
58 if h.voice.sub == ch {
59 h.voice.sub = nil
60 }
61 h.voice.mu.Unlock()
62 }()
63 log.Printf("voice: nc-voice connected")
64 w.Header().Set("Content-Type", "application/x-ndjson")
65 fmt.Fprintln(w, `{"cmd":"hello"}`)
66 fl.Flush()
67 for {
68 select {
69 case c := <-ch:
70 fmt.Fprintln(w, c)
71 case <-time.After(30 * time.Second):
72 fmt.Fprintln(w, `{"cmd":"ping"}`)
73 case <-r.Context().Done():
74 log.Printf("voice: nc-voice disconnected")
75 return
76 }
77 fl.Flush()
78 }
79}
80
81func (h *host) handleVoiceEvent(w http.ResponseWriter, r *http.Request) {
82 var ev struct{ Kind, Text string }
83 if r.Method != http.MethodPost || json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10)).Decode(&ev) != nil || ev.Kind == "" {
84 http.Error(w, `POST {"kind":..., "text":...}`, http.StatusBadRequest)
85 return
86 }
87 h.broadcastCtl(proto.InputEvent{T: "voice", Kind: ev.Kind, Text: ev.Text})
88 w.WriteHeader(http.StatusNoContent)
89}
90
91// broadcastCtl sends a message on every connected client's control channel.
92func (h *host) broadcastCtl(ev proto.InputEvent) {
93 b, _ := json.Marshal(ev)
94 h.mu.Lock()
95 defer h.mu.Unlock()
96 for _, s := range h.sessions {
97 if s.ctl != nil {
98 s.ctl.SendText(string(b))
99 }
100 }
101}