demo / network-computer
Public
Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.
GitStable demo
README: say this is a copy and where the original is
cde82c2
13 hours, 39 minutes ago
History
voice.go · 2.5 KB · 101 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 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"}. |
| 20 | type 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. |
| 26 | func (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 | |
| 46 | func (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 | |
| 81 | func (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. |
| 92 | func (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 | } |