| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "io" |
| 9 | "net/http" |
| 10 | "strings" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func (s *Server) macOSAudioStatus(w http.ResponseWriter, r *http.Request) { |
| 15 | ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second) |
| 16 | defer cancel() |
| 17 | cmd := s.sshCommand(ctx, "-T", s.sshTarget(), "--", macOSWindowSocketCommand) |
| 18 | cmd.Stdin = strings.NewReader("{\"action\":\"audio-status\"}\n") |
| 19 | cmd.Stderr = io.Discard |
| 20 | var data bytes.Buffer |
| 21 | cmd.Stdout = &boundedWindowsStatusWriter{buffer: &data, remaining: 4096} |
| 22 | var status struct { |
| 23 | Supported bool `json:"supported"` |
| 24 | Available bool `json:"available"` |
| 25 | Reason string `json:"reason"` |
| 26 | Error string `json:"error"` |
| 27 | } |
| 28 | if cmd.Run() != nil || json.Unmarshal(data.Bytes(), &status) != nil { |
| 29 | http.Error(w, "虚拟 Mac 声音组件尚未连接,请先完成客机组件更新", http.StatusBadGateway) |
| 30 | return |
| 31 | } |
| 32 | if status.Reason == "" { |
| 33 | status.Reason = status.Error |
| 34 | } |
| 35 | if !status.Supported && status.Reason != "requires_gui_login" && status.Reason != "screen_recording_permission" { |
| 36 | http.Error(w, "虚拟 Mac 声音组件尚未连接,请先完成客机组件更新", http.StatusBadGateway) |
| 37 | return |
| 38 | } |
| 39 | if !status.Available { |
| 40 | message := "请先登录并解锁虚拟 Mac 的普通账户" |
| 41 | if status.Reason == "screen_recording_permission" { |
| 42 | message = "请在虚拟 Mac 的系统设置中允许 Cage Guest Window 录制屏幕和系统声音" |
| 43 | } |
| 44 | http.Error(w, message, http.StatusConflict) |
| 45 | return |
| 46 | } |
| 47 | jsonReply(w, audioFormat) |
| 48 | } |
| 49 | |
| 50 | func (s *Server) macOSAudioCapture(w http.ResponseWriter, r *http.Request, ctx context.Context, cancel context.CancelFunc) { |
| 51 | cmd := s.sshCommand(ctx, "-T", s.sshTarget(), "--", macOSWindowSocketCommand) |
| 52 | cmd.Stderr = io.Discard |
| 53 | input, err := cmd.StdinPipe() |
| 54 | if err != nil { |
| 55 | http.Error(w, "声音连接未能建立", http.StatusBadGateway) |
| 56 | return |
| 57 | } |
| 58 | pipe, err := cmd.StdoutPipe() |
| 59 | if err != nil { |
| 60 | input.Close() |
| 61 | http.Error(w, "声音连接未能建立", http.StatusBadGateway) |
| 62 | return |
| 63 | } |
| 64 | if err = cmd.Start(); err != nil { |
| 65 | input.Close() |
| 66 | pipe.Close() |
| 67 | http.Error(w, "声音连接未能建立", http.StatusBadGateway) |
| 68 | return |
| 69 | } |
| 70 | defer func() { cancel(); input.Close(); pipe.Close(); _ = cmd.Wait() }() |
| 71 | |
| 72 | |
| 73 | if _, err = io.WriteString(input, "{\"action\":\"audio-capture\"}\n"); err != nil { |
| 74 | http.Error(w, "声音连接未能建立", http.StatusBadGateway) |
| 75 | return |
| 76 | } |
| 77 | deadlineDone := make(chan struct{}) |
| 78 | deadline := time.AfterFunc(15*time.Second, func() { defer close(deadlineDone); cancel(); pipe.Close() }) |
| 79 | reader := bufio.NewReaderSize(pipe, 4096) |
| 80 | err = readAudioReady(reader) |
| 81 | stopped := cancelWindowsAdmissionDeadline(deadline, deadlineDone) |
| 82 | if err != nil || !stopped { |
| 83 | http.Error(w, "虚拟 Mac 声音未能启动,请确认客机已解锁并允许 Cage Guest Window 录制屏幕和系统声音", http.StatusBadGateway) |
| 84 | return |
| 85 | } |
| 86 | s.streamAudio(w, r.WithContext(ctx), bufferedAudio{Reader: reader, Closer: pipe}) |
| 87 | } |