| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "net/http" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/gorilla/websocket" |
| 13 | ) |
| 14 | |
| 15 | const audioCaptureCommand = "exec /usr/bin/parec --server=unix:/run/cage-audio/native --record --raw --format=s16le --rate=48000 --channels=2 --device=cage_output.monitor --latency-msec=20" |
| 16 | |
| 17 | var audioFormat = struct { |
| 18 | Format string `json:"format"` |
| 19 | SampleRate int `json:"sample_rate"` |
| 20 | Channels int `json:"channels"` |
| 21 | }{"s16le", 48000, 2} |
| 22 | |
| 23 | type bufferedAudio struct { |
| 24 | io.Reader |
| 25 | io.Closer |
| 26 | } |
| 27 | |
| 28 | func readAudioReady(reader *bufio.Reader) error { |
| 29 | line, err := reader.ReadSlice('\n') |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | var ready struct { |
| 34 | Ready bool `json:"ready"` |
| 35 | Format string `json:"format"` |
| 36 | SampleRate int `json:"sample_rate"` |
| 37 | Channels int `json:"channels"` |
| 38 | } |
| 39 | if json.Unmarshal(line, &ready) != nil || !ready.Ready || ready.Format != "s16le" || ready.SampleRate != 48000 || ready.Channels != 2 { |
| 40 | return errors.New("客机声音格式无效") |
| 41 | } |
| 42 | return nil |
| 43 | } |
| 44 | |
| 45 | |
| 46 | |
| 47 | func (s *Server) audio(w http.ResponseWriter, r *http.Request) { |
| 48 | s.mu.Lock() |
| 49 | available := !s.closing && !s.guestUpdateRequired |
| 50 | s.mu.Unlock() |
| 51 | if !available || !s.vmRunning() { |
| 52 | http.Error(w, "请先启动并更新 Cage 环境", http.StatusConflict) |
| 53 | return |
| 54 | } |
| 55 | if !websocket.IsWebSocketUpgrade(r) { |
| 56 | if s.macosGuest != nil { |
| 57 | s.macOSAudioStatus(w, r) |
| 58 | return |
| 59 | } |
| 60 | if s.windowsGuest != nil { |
| 61 | s.windowsAudioStatus(w, r) |
| 62 | return |
| 63 | } |
| 64 | jsonReply(w, audioFormat) |
| 65 | return |
| 66 | } |
| 67 | s.mu.Lock() |
| 68 | if s.audioClients >= 4 { |
| 69 | s.mu.Unlock() |
| 70 | http.Error(w, "声音连接过多,请关闭重复的工作台窗口", http.StatusConflict) |
| 71 | return |
| 72 | } |
| 73 | s.audioClients++ |
| 74 | s.mu.Unlock() |
| 75 | defer func() { s.mu.Lock(); s.audioClients--; s.mu.Unlock() }() |
| 76 | ctx, cancel := context.WithCancel(r.Context()) |
| 77 | defer cancel() |
| 78 | if s.macosGuest != nil { |
| 79 | s.macOSAudioCapture(w, r, ctx, cancel) |
| 80 | return |
| 81 | } |
| 82 | if s.windowsGuest != nil { |
| 83 | s.windowsAudioCapture(w, r, ctx, cancel) |
| 84 | return |
| 85 | } |
| 86 | cmd := s.sshCommand(ctx, "-T", s.sshTarget(), "--", audioCaptureCommand) |
| 87 | pipe, err := cmd.StdoutPipe() |
| 88 | if err != nil { |
| 89 | http.Error(w, "声音连接未能建立", http.StatusBadGateway) |
| 90 | return |
| 91 | } |
| 92 | if err = cmd.Start(); err != nil { |
| 93 | http.Error(w, "声音连接未能建立", http.StatusBadGateway) |
| 94 | return |
| 95 | } |
| 96 | defer func() { cancel(); pipe.Close(); _ = cmd.Wait() }() |
| 97 | s.streamAudio(w, r.WithContext(ctx), pipe) |
| 98 | } |
| 99 | |
| 100 | func (s *Server) streamAudio(w http.ResponseWriter, r *http.Request, source io.ReadCloser) { |
| 101 | defer source.Close() |
| 102 | ws, err := s.upgrade(w, r) |
| 103 | if err != nil { |
| 104 | return |
| 105 | } |
| 106 | defer ws.Close() |
| 107 | ws.SetReadLimit(1024) |
| 108 | _ = ws.SetWriteDeadline(time.Now().Add(2 * time.Second)) |
| 109 | if ws.WriteJSON(audioFormat) != nil { |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | |
| 114 | done := make(chan struct{}) |
| 115 | go func() { |
| 116 | defer close(done) |
| 117 | defer ws.Close() |
| 118 | _ = sendAudioPCM(ws, source) |
| 119 | }() |
| 120 | defer func() { _ = source.Close(); _ = ws.Close(); <-done }() |
| 121 | _, _, _ = ws.ReadMessage() |
| 122 | } |
| 123 | |
| 124 | func sendAudioPCM(ws *websocket.Conn, source io.Reader) error { |
| 125 | packet := make([]byte, 48000*2*2/50) |
| 126 | for { |
| 127 | n, err := io.ReadFull(source, packet) |
| 128 | n -= n % 4 |
| 129 | if n > 0 { |
| 130 | _ = ws.SetWriteDeadline(time.Now().Add(2 * time.Second)) |
| 131 | if writeErr := ws.WriteMessage(websocket.BinaryMessage, packet[:n]); writeErr != nil { |
| 132 | return writeErr |
| 133 | } |
| 134 | } |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | } |
| 139 | } |