Add GCC-managed encoded Opus audio track#160
Open
lkang-nuro wants to merge 4 commits into
Open
Conversation
Move Opus audio encoding into the sender so the GCC bandwidth estimator drives audio bitrate alongside video, sharing the bitrate pool. Audio was previously pre-encoded and pushed as a passive track that GCC could not touch. - Generalize EncodedTrack to carry audio (isAudio, audioTrack, audioSource); the existing local track and encodedReader/bitrateTracker are shared by both kinds. - New AddEncodedAudioTrack(trackID, opus.Params): builds the Opus codec selector, AudioBuffer source, mediadevices AudioTrack, and encoded reader, and registers the track in s.tracks so it flows through GCC allocation. - New SendAudioFrame to push raw interleaved PCM. - New sender/audio_buffer.go (AudioBuffer): the audio counterpart to FrameBuffer, with the same initialized-gate non-blocking Read and drop-oldest bounded queue. - updateEncoderBitrate gains a codec.BitRateController branch for Opus, clamped to [8000, 32000] bps. - processEncodedFrames uses a 20ms duration for audio and skips the VP8 keyframe sniff; ForceKeyFrame/recreateEncoder/Close guard on isAudio. - Extract attachTrackToPeerConnection helper (de-dupes AddVideoTrack). The legacy passive AddAudioTrack path is left untouched (additive). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
8918900 to
d3b8ce2
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #160 +/- ##
==========================================
+ Coverage 48.79% 52.84% +4.04%
==========================================
Files 19 21 +2
Lines 1916 2165 +249
==========================================
+ Hits 935 1144 +209
- Misses 908 937 +29
- Partials 73 84 +11
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
These were scratch design notes (dynamic_audio_bitrate_plan.md and the frameready stall analysis) that don't belong in the shipped tree. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The sender encodes each frame's capture instant into the outgoing RTP timestamp (captureUs*ClockRate/1e6) for both video (90 kHz) and encoded Opus audio (48 kHz) via the shared captureTimestampInterceptor. On the receiver, however, video packets flowed through processPackets where the timestamp was available, while audio was read and discarded in handleNonVP8Track. Add a receiver-side recovery of the capture timestamp: - receiver/capture_latency.go: CaptureTimeUsFromRTP + GlassToGlassLatency, the wrap-safe inverse of the sender's encoding (mirrors the browser's rtpTimestamp*1e6/ClockRate recovery). - receiver.go: shared reportGlassToGlassLatency helper called from both the video (processPackets) and audio (handleNonVP8Track) read loops, using the track's negotiated clock rate so audio recovers at 48 kHz and video at 90 kHz. Audio packets are now recovered instead of discarded. - Unit tests for the recovery math and the read-loop helper. Also fold the audio bitrate clamp into a single clampAudioBitrate helper. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Tie the real sender captureTimestampInterceptor (at the 48 kHz Opus clock) to the real receiver recovery (receiver.GlassToGlassLatency): a capture time stamped into the outgoing Opus RTP timestamp must recover, at the receiver, to a glass-to-glass latency matching the injected/elapsed delay within RTP-clock quantization. The audio analog of the video round-trip, exercised without a codec or network. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
The audio stream is effectively fixed bitrate: audio is pre-encoded outside the sender and pushed as a passive track via
AddAudioTrack, so the GCC bandwidth estimator — which dynamically drives video bitrate — never touches it. When the network degrades, video scales down but audio holds its bandwidth.This makes audio a first-class, GCC-managed encoded Opus track inside the sender, so the same control loop (
updateBitrate→updateEncoderBitrate) that drives VP8 also drives Opus, sharing the GCC bitrate pool. Callers push raw PCM instead of pre-encoded Opus.The change is additive: the legacy passive
AddAudioTrackpath is left untouched.Changes
EncodedTrackto carry audio (isAudio,audioTrack,audioSource); the existing local track andencodedReader/bitrateTracker/mimeTypeare shared by both kinds.AddEncodedAudioTrack(trackID, opus.Params)— builds the Opus codec selector,AudioBuffersource, mediadevicesAudioTrack, and encoded reader; registers the track ins.tracksso it flows through GCC allocation.SendAudioFrame(trackID, pcm, sampleRate, channels)— pushes raw interleaved PCM.sender/audio_buffer.go(AudioBuffer) — the audio counterpart toFrameBuffer: sameinitialized-gate non-blockingReadand drop-oldest bounded queue.updateEncoderBitrategains acodec.BitRateControllerbranch for Opus, clamped to[8000, 32000]bps.processEncodedFramesuses a 20 ms duration for audio and skips the VP8 keyframe sniff;ForceKeyFrame/recreateEncoder/Closeguard onisAudio.attachTrackToPeerConnectionhelper (de-dupesAddVideoTrack).Testing
go build ./...,go vet ./...,golangci-lint run ./...(0 issues), andgo test ./...all pass. New tests insender/audio_buffer_test.gocover buffer behavior, track creation/validation, a full PCM→Opus encode path, and the GCC bitrate-driven path.Note
Downstream consumer wiring (cgo
PushAudioPCMexport, C++ raw-PCM push, Bazel.sore-pin) lives in a separate repo and is out of scope here. For GCC to genuinely drive audio, the encoded audio track must beAddTrack'd on the PeerConnection this sender owns so transport-cc feedback reaches the estimator.🤖 Generated with Claude Code