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
audio.go · 3.2 KB · 100 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "log" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "runtime" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/pion/webrtc/v4/pkg/media" |
| 13 | |
| 14 | "github.com/askrobots/network-computer/internal/oggopus" |
| 15 | ) |
| 16 | |
| 17 | // audioArgs captures system audio with ffmpeg and emits Ogg Opus on stdout. |
| 18 | // On macOS there is no system loopback without a virtual device (BlackHole, |
| 19 | // open source) so -audio-device names an avfoundation audio index. |
| 20 | func audioArgs(device string) []string { |
| 21 | var in []string |
| 22 | switch runtime.GOOS { |
| 23 | case "darwin": |
| 24 | in = []string{"-f", "avfoundation", "-i", "none:" + device} |
| 25 | case "linux": |
| 26 | if device == "" { |
| 27 | device = "default" |
| 28 | } |
| 29 | // 10 ms capture fragments (48 kHz, stereo, 16-bit); the default is |
| 30 | // ~50 ms, which arrives in clumps |
| 31 | in = []string{"-f", "pulse", "-fragment_size", "1920", "-i", device} |
| 32 | case "windows": |
| 33 | in = []string{"-f", "dshow", "-i", "audio=" + device} |
| 34 | } |
| 35 | args := []string{"-hide_banner", "-loglevel", "warning", "-fflags", "nobuffer"} |
| 36 | args = append(args, in...) |
| 37 | return append(args, "-c:a", "libopus", "-b:a", "128k", "-ar", "48000", |
| 38 | "-application", "audio", "-fec", "1", "-packet_loss", "10", |
| 39 | "-frame_duration", "20", "-page_duration", "20000", "-flush_packets", "1", "-f", "opus", "pipe:1") |
| 40 | } |
| 41 | |
| 42 | // audioLead is how far ahead of sending audio is scheduled: it absorbs late |
| 43 | // capture at the cost of that much extra audio delay. |
| 44 | const audioLead = 40 * time.Millisecond |
| 45 | |
| 46 | func streamAudio(ctx context.Context, device string, track sampleWriter) { |
| 47 | for ctx.Err() == nil { |
| 48 | args := audioArgs(device) |
| 49 | log.Printf("ffmpeg(audio) %s", strings.Join(args, " ")) |
| 50 | cmd := exec.CommandContext(ctx, "ffmpeg", args...) |
| 51 | cmd.Stderr = os.Stderr |
| 52 | stdout, err := cmd.StdoutPipe() |
| 53 | if err == nil { |
| 54 | err = cmd.Start() |
| 55 | } |
| 56 | if err != nil { |
| 57 | log.Printf("audio ffmpeg: %v", err) |
| 58 | time.Sleep(time.Second) |
| 59 | continue |
| 60 | } |
| 61 | // One Opus packet per RTP packet: an Ogg page can hold two, and a page |
| 62 | // sent whole is not a valid Opus packet (the receiver decodes garbage). |
| 63 | ogg := oggopus.NewReader(stdout) |
| 64 | // Pace packets on their own clock. Capture arrives in clumps; sent as it |
| 65 | // came, the receiver saw packets bunch up then pause and kept speeding up |
| 66 | // and slowing down playback to cover it (a warbly, old-streaming sound). |
| 67 | var start time.Time |
| 68 | var sent time.Duration |
| 69 | for { |
| 70 | pkt, err := ogg.Next() |
| 71 | if err != nil { |
| 72 | break |
| 73 | } |
| 74 | d := oggopus.Duration(pkt) |
| 75 | if d <= 0 || d > 120*time.Millisecond { |
| 76 | d = 20 * time.Millisecond |
| 77 | } |
| 78 | // A small fixed lead: each packet is due audioLead after capture, so |
| 79 | // one that ffmpeg hands over up to audioLead late still leaves on |
| 80 | // time instead of bunching up with the next. |
| 81 | if start.IsZero() { |
| 82 | start = time.Now().Add(audioLead) |
| 83 | } |
| 84 | if wait := time.Until(start.Add(sent)); wait > 0 { |
| 85 | time.Sleep(wait) |
| 86 | } else if -wait > 200*time.Millisecond { |
| 87 | start, sent = time.Now().Add(audioLead), 0 // fell far behind: resync rather than burst to catch up |
| 88 | } |
| 89 | if err := track.WriteSample(media.Sample{Data: pkt, Duration: d}); err != nil { |
| 90 | log.Printf("audio write: %v", err) |
| 91 | } |
| 92 | sent += d |
| 93 | } |
| 94 | cmd.Wait() |
| 95 | if ctx.Err() == nil { |
| 96 | log.Printf("audio ffmpeg exited, restarting") |
| 97 | time.Sleep(time.Second) |
| 98 | } |
| 99 | } |
| 100 | } |