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
mic.go · 2.7 KB · 102 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "log" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/pion/webrtc/v4" |
| 13 | "github.com/pion/webrtc/v4/pkg/media/oggwriter" |
| 14 | ) |
| 15 | |
| 16 | // micArgs plays an Ogg Opus stream from stdin into an output device, so the |
| 17 | // client's microphone becomes audio on this machine. On Linux the device is a |
| 18 | // pulse sink (nc-mic) whose monitor is remapped into a source every desktop |
| 19 | // app sees as a microphone. On macOS it is an output device index, normally a |
| 20 | // loopback such as BlackHole that apps then pick as their input. |
| 21 | func micArgs(device string) []string { |
| 22 | in := []string{"-hide_banner", "-loglevel", "warning", |
| 23 | "-fflags", "nobuffer", "-flags", "low_delay", "-probesize", "32", "-analyzeduration", "0", |
| 24 | "-f", "ogg", "-i", "pipe:0"} |
| 25 | switch runtime.GOOS { |
| 26 | case "linux": |
| 27 | return append(in, "-f", "pulse", "-buffer_duration", "40", "-device", device, "network-computer microphone") |
| 28 | case "darwin": |
| 29 | return append(in, "-f", "audiotoolbox", "-audio_device_index", device, "-") |
| 30 | } |
| 31 | return nil |
| 32 | } |
| 33 | |
| 34 | // playMic copies one incoming Opus track into the mic device until the track |
| 35 | // ends or the session closes. Without a device it just drains the track. |
| 36 | func playMic(ctx context.Context, track *webrtc.TrackRemote, device string) { |
| 37 | args := micArgs(device) |
| 38 | if device == "" || args == nil { |
| 39 | if device != "" { |
| 40 | log.Printf("mic: no microphone output on %s yet; dropping client audio", runtime.GOOS) |
| 41 | } |
| 42 | drain(track) |
| 43 | return |
| 44 | } |
| 45 | log.Printf("mic: client microphone -> %s", device) |
| 46 | log.Printf("ffmpeg(mic) %s", strings.Join(args, " ")) |
| 47 | cmd := exec.CommandContext(ctx, "ffmpeg", args...) |
| 48 | cmd.Stderr = os.Stderr |
| 49 | stdin, err := cmd.StdinPipe() |
| 50 | if err == nil { |
| 51 | err = cmd.Start() |
| 52 | } |
| 53 | if err != nil { |
| 54 | log.Printf("mic ffmpeg: %v", err) |
| 55 | drain(track) |
| 56 | return |
| 57 | } |
| 58 | ogg, err := oggwriter.NewWith(stdin, 48000, 2) |
| 59 | if err != nil { |
| 60 | log.Printf("mic ogg: %v", err) |
| 61 | stdin.Close() |
| 62 | cmd.Wait() |
| 63 | drain(track) |
| 64 | return |
| 65 | } |
| 66 | packets, bad := 0, 0 |
| 67 | for { |
| 68 | pkt, _, err := track.ReadRTP() |
| 69 | if err != nil { |
| 70 | break |
| 71 | } |
| 72 | if len(pkt.Payload) == 0 { |
| 73 | continue // padding / keepalive |
| 74 | } |
| 75 | if err := ogg.WriteRTP(pkt); err != nil { |
| 76 | if strings.Contains(err.Error(), "invalid Opus packet") { |
| 77 | if bad++; bad == 1 || bad%500 == 0 { |
| 78 | log.Printf("mic: skipped %d malformed packet(s)", bad) |
| 79 | } |
| 80 | continue |
| 81 | } |
| 82 | if err != io.ErrClosedPipe { |
| 83 | log.Printf("mic write: %v", err) |
| 84 | } |
| 85 | break // ffmpeg is gone |
| 86 | } |
| 87 | if packets++; packets == 1 { |
| 88 | log.Printf("mic: first audio packet from client") |
| 89 | } |
| 90 | } |
| 91 | ogg.Close() |
| 92 | cmd.Wait() |
| 93 | log.Printf("mic: client microphone ended after %d packets", packets) |
| 94 | } |
| 95 | |
| 96 | func drain(track *webrtc.TrackRemote) { |
| 97 | for { |
| 98 | if _, _, err := track.ReadRTP(); err != nil { |
| 99 | return |
| 100 | } |
| 101 | } |
| 102 | } |