AlexxIT/go2rtc · error

webrtc: can't get track

Error message

webrtc: can't get track

What it means

pkg/webrtc AddTrack sets up an outgoing RTP sender for a media/codec pair. After mapping the media kind to a track type, it asks the underlying PeerConnection for the local sender track by media ID; if the lookup returns nil the track was never registered or the media ID is invalid, so AddTrack aborts. This indicates an internal inconsistency: you are adding a track that does not exist on the connection.

Solutions

  1. Ensure the media was actually added/published on the same Consumer/PeerConnection before AddTrack — use IDs the connection itself issued
  2. Verify you are not reusing Media objects across different WebRTC sessions
  3. Add a nil check on the media ID and log it before AddTrack to catch stale references
  4. Update the library — this can indicate an internal invariant violation on unexpected media kinds
Defensive patterns

Strategy: try-catch

Validate before calling

if media == nil || media.ID == "" {
    return errors.New("media or media ID missing before AddTrack")
}

Try / catch

if err := consumer.AddTrack(media, codec); err != nil {
    if strings.Contains(err.Error(), "can't get track") {
        // re-create or renegotiate the connection, don't retry blindly
    }
    return err
}

Prevention

When it happens

Trigger: Calling webrtc Consumer AddTrack with a media whose ID was never negotiated/added on the local PeerConnection — e.g. reusing a Media object from another connection or calling AddTrack before the connection published that track, or hitting the panic branch's unexpected media kind path partially applied.

Common situations: Custom server code wiring medias from a stream into a WebRTC consumer where media IDs were regenerated; adding a track after the connection was closed/replaced; forwarding an audio/video media the PeerConnection never had a transceiver for.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of AlexxIT/go2rtc@c245815e75 (2026-09-07). Data as JSON: /api/errors/272cc5cb348c1cc2. Report an issue: GitHub.

Appendix: source

Thrown at pkg/webrtc/consumer.go:37

	for _, sender := range c.Senders {
		if sender.Codec == codec {
			sender.Bind(track)
			return nil
		}
	}

	switch c.Mode {
	case core.ModePassiveConsumer: // video/audio for browser
	case core.ModeActiveProducer: // go2rtc as WebRTC client (backchannel)
	case core.ModePassiveProducer: // WebRTC/WHIP
	default:
		panic(core.Caller())
	}

	localTrack := c.GetSenderTrack(media.ID)
	if localTrack == nil {
		return errors.New("webrtc: can't get track")
	}

	payloadType := codec.PayloadType

	sender := core.NewSender(media, codec)
	sender.Handler = func(packet *rtp.Packet) {
		c.Send += packet.MarshalSize()
		//important to send with remote PayloadType
		_ = localTrack.WriteRTP(payloadType, packet)
	}

	switch track.Codec.Name {
	case core.CodecH264:
		sender.Handler = h264.RTPPay(1200, sender.Handler)
		if track.Codec.IsRTP() {
			sender.Handler = h264.RTPDepay(track.Codec, sender.Handler)
		} else {
			sender.Handler = h264.RepairAVCC(track.Codec, sender.Handler)

View on GitHub (pinned to c245815e75)