Fix buffer aliasing and cleanup unused fields in audio

- Copy opus data in ReadMessage to prevent aliasing with internal buffer
- Remove unused ctx field from OutputRelay struct
- Fix OPUS_BANDWIDTH comment accuracy (20kHz passband)
This commit is contained in:
Alex P 2025-11-25 10:01:59 +02:00
parent aeb68810ba
commit fe940cd9ab
3 changed files with 7 additions and 5 deletions

View File

@ -88,7 +88,7 @@ static uint16_t max_packet_size = 1500;
#define OPUS_VBR 1 // Variable bitrate mode enabled
#define OPUS_VBR_CONSTRAINT 1 // Constrained VBR maintains bitrate ceiling
#define OPUS_SIGNAL_TYPE 3002 // OPUS_SIGNAL_MUSIC (optimized for music/audio content)
#define OPUS_BANDWIDTH 1105 // OPUS_BANDWIDTH_FULLBAND (0-20kHz frequency range)
#define OPUS_BANDWIDTH 1105 // OPUS_BANDWIDTH_FULLBAND (20kHz passband)
#define OPUS_LSB_DEPTH 16 // 16-bit PCM sample depth (S16_LE format)
static uint8_t opus_dtx_enabled = 1;

View File

@ -216,7 +216,11 @@ func (c *CgoSource) ReadMessage() (uint8, []byte, error) {
return 0, nil, fmt.Errorf("opus packet too large: %d > %d", opusSize, len(c.opusBuf))
}
return ipcMsgTypeOpus, c.opusBuf[:opusSize], nil
// Return a copy to prevent buffer aliasing - the caller may hold this slice
// while the next ReadMessage overwrites the internal buffer
result := make([]byte, opusSize)
copy(result, c.opusBuf[:opusSize])
return ipcMsgTypeOpus, result, nil
}
func (c *CgoSource) WriteMessage(msgType uint8, payload []byte) error {

View File

@ -15,7 +15,6 @@ import (
type OutputRelay struct {
source *AudioSource
audioTrack *webrtc.TrackLocalStaticSample
ctx context.Context
cancel context.CancelFunc
logger zerolog.Logger
running atomic.Bool
@ -27,13 +26,12 @@ type OutputRelay struct {
}
func NewOutputRelay(source *AudioSource, audioTrack *webrtc.TrackLocalStaticSample) *OutputRelay {
ctx, cancel := context.WithCancel(context.Background())
_, cancel := context.WithCancel(context.Background())
logger := logging.GetDefaultLogger().With().Str("component", "audio-output-relay").Logger()
return &OutputRelay{
source: source,
audioTrack: audioTrack,
ctx: ctx,
cancel: cancel,
logger: logger,
stopped: make(chan struct{}),