Bug Report: `(c *Codec) Match()` fails on codec query filter without explicit sample rate (e.g. `?audio=aac`)
Description
When requesting an RTSP stream with an audio codec filter that omits the sample rate (such as ?audio=aac), go2rtc fails to match the stream even if the producer is actively serving that exact codec (e.g. audio:AAC / MPEG4-GENERIC/48000).
go2rtc logs:
WRN [rtsp] error="streams: codecs not matched: video:H264, audio:AAC, audio:OPUS => audio:AAC" stream=...and returns 404 Not Found.
However, explicitly appending the sample rate (e.g. ?audio=aac/48000) succeeds immediately.
Root Cause
In pkg/core/codec.go:
func (c *Codec) Match(remote *Codec) bool {
switch remote.Name {
case CodecAll, CodecAny:
return true
}
return c.Name == remote.Name &&
(c.ClockRate == remote.ClockRate || remote.ClockRate == 0) &&
(c.Channels == remote.Channels || remote.Channels == 0)
}When a consumer requests
?audio=aac,ParseQuery()inpkg/core/media.goparses the query string and creates a consumer codeccwith:c.Name = "MPEG4-GENERIC"(aliased fromAAC)c.ClockRate = 0(unspecified / wildcard)c.Channels = 0(unspecified / wildcard)
In
(c *Codec) Match(remote *Codec):cis the consumer (c.ClockRate = 0).remoteis the producer (remote.ClockRate = 48000).- The condition is:
(c.ClockRate == remote.ClockRate || remote.ClockRate == 0) - This evaluates to:
(0 == 48000 || 48000 == 0)=>false.
The method checks if the producer has an unspecified sample rate (remote.ClockRate == 0), but fails to check if the consumer requested a wildcard sample rate (c.ClockRate == 0).
Proposed Fix
In pkg/core/codec.go:
func (c *Codec) Match(remote *Codec) bool {
switch remote.Name {
case CodecAll, CodecAny:
return true
}
return c.Name == remote.Name &&
- (c.ClockRate == remote.ClockRate || remote.ClockRate == 0) &&
- (c.Channels == remote.Channels || remote.Channels == 0)
+ (c.ClockRate == remote.ClockRate || c.ClockRate == 0 || remote.ClockRate == 0) &&
+ (c.Channels == remote.Channels || c.Channels == 0 || remote.Channels == 0)
}Source: AlexxIT/go2rtc