#2474·go2rtc

Bug Report: `(c *Codec) Match()` fails on codec query filter without explicit sample rate (e.g. `?audio=aac`)

Author: miguelangel-nublaCreated Sep 6, 2026Updated Oct 1, 2026
Labelsenhancementsize/small

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:

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)
}
  1. When a consumer requests ?audio=aac, ParseQuery() in pkg/core/media.go parses the query string and creates a consumer codec c with:

    • c.Name = "MPEG4-GENERIC" (aliased from AAC)
    • c.ClockRate = 0 (unspecified / wildcard)
    • c.Channels = 0 (unspecified / wildcard)
  2. In (c *Codec) Match(remote *Codec):

    • c is the consumer (c.ClockRate = 0).
    • remote is the producer (remote.ClockRate = 48000).
    • The condition is:
      go
      (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:

diff
 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)
 }