Add GCC-managed encoded Opus audio track#160
Open
lkang-nuro wants to merge 1 commit 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>
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