Skip to main content
A back channel is a sendonly media track in the SDP that flows in the reverse direction — from the client to the server. ONVIF IP cameras use back channels to receive audio from a viewer (for example, to drive a speaker mounted on the camera). The codec is almost always G.711 (PCMU or PCMA) at 8 kHz. The ONVIF Streaming Specification requires cameras to advertise a back channel in the SDP when two-way audio is supported. gortsplib handles back channels on both the client side (sending audio to a camera) and the server side (receiving audio from a viewer).
Back channel media entries have IsBackChannel: true in description.Media. The SDP attribute a=sendonly is set on these tracks; the camera receives rather than sends on them.

Client: sending audio to a camera

To send audio through a back channel, set RequestBackChannels: true on the client before connecting. After Describe, iterate the session medias to find a back channel that uses the codec you want to send (G.711 in this case), then call Setup on it and use c.WritePacketRTP to push encoded audio frames.
1

Enable back channel negotiation

Set RequestBackChannels: true on the gortsplib.Client. Without this flag the client will not include the ONVIF Require: www.onvif.org/ver20/backchannel header and the camera will not expose back channel medias in the SDP.
2

Find the G.711 back channel

After Describe, walk desc.Medias looking for entries where media.IsBackChannel == true and the format list contains *format.G711.
3

Setup and play

Call c.Setup(desc.BaseURL, medi, 0, 0) for the back channel media only, then call c.Play(nil).
4

Encode and write

Use forma.CreateEncoder() to get an RTP encoder for G.711. On each tick, generate PCM samples, encode them to G.711 (µ-law or A-law), packetise with the encoder, set the timestamp field, then call c.WritePacketRTP(medi, pkt) for each packet.

Server: receiving audio from a client

To accept back-channel audio from a connecting viewer, declare a description.Media with IsBackChannel: true alongside your normal (direct) media tracks. Register a packet handler inside OnPlay using ctx.Session.OnPacketRTPAny — this fires whenever the client pushes audio into the back channel.
1

Declare a back channel media

Add a description.Media entry with IsBackChannel: true to the description.Session. The SDP will contain a=sendonly on that track, signalling to connecting clients that they should send audio on it.
2

Create the ServerStream

Pass the description.Session (containing both the direct and back channel medias) to gortsplib.ServerStream and call Initialize().
3

Handle inbound packets in OnPlay

In the OnPlay handler, call ctx.Session.OnPacketRTPAny to register a per-session callback. Use ctx.Session.PacketPTS to decode the presentation timestamp from each incoming packet.

ONVIF compliance notes

The ONVIF Streaming Specification (section 5.1.1) requires:
  • The Require: www.onvif.org/ver20/backchannel header in the client’s DESCRIBE request — gortsplib sends this automatically when RequestBackChannels: true.
  • A sendonly attribute on the back channel m= line in the SDP — gortsplib sets this from IsBackChannel: true in the media description.
  • The back channel to be set up and played within the same RTSP session as the forward channel.
To test your implementation without a physical camera, use MediaMTX. It supports ONVIF back channels and can act as both the upstream camera and a test client in a loopback configuration.
Some cameras time out the back channel if no RTP packets are received for several seconds. Send silence (zero-amplitude G.711 frames) periodically as a keepalive if your application does not have continuous audio to transmit.