Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/icholy/digest v1.1.0
github.com/jfreymuth/oggvorbis v1.0.5
github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731
github.com/livekit/media-sdk v0.0.0-20250927154350-bd99739b439b
github.com/livekit/media-sdk v0.0.0-20251020120113-a84c3b4b6c31
github.com/livekit/mediatransportutil v0.0.0-20250519131108-fb90f5acfded
github.com/livekit/protocol v1.42.3-0.20251016122026-bc6809b5bdc8
github.com/livekit/psrpc v0.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ github.com/lithammer/shortuuid/v4 v4.2.0 h1:LMFOzVB3996a7b8aBuEXxqOBflbfPQAiVzkI
github.com/lithammer/shortuuid/v4 v4.2.0/go.mod h1:D5noHZ2oFw/YaKCfGy0YxyE7M0wMbezmMjPdhyEFe6Y=
github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731 h1:9x+U2HGLrSw5ATTo469PQPkqzdoU7be46ryiCDO3boc=
github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
github.com/livekit/media-sdk v0.0.0-20250927154350-bd99739b439b h1:dPf9i0JPyf0Sg0VMvkKBhdLPn7G93ERE9VCavlc07iA=
github.com/livekit/media-sdk v0.0.0-20250927154350-bd99739b439b/go.mod h1:7ssWiG+U4xnbvLih9WiZbhQP6zIKMjgXdUtIE1bm/E8=
github.com/livekit/media-sdk v0.0.0-20251020120113-a84c3b4b6c31 h1:i2d3UdmZ7q1F3lEz5+b6Uf5L/+jq3nK7MWiK1k9JprQ=
github.com/livekit/media-sdk v0.0.0-20251020120113-a84c3b4b6c31/go.mod h1:7ssWiG+U4xnbvLih9WiZbhQP6zIKMjgXdUtIE1bm/E8=
github.com/livekit/mediatransportutil v0.0.0-20250519131108-fb90f5acfded h1:ylZPdnlX1RW9Z15SD4mp87vT2D2shsk0hpLJwSPcq3g=
github.com/livekit/mediatransportutil v0.0.0-20250519131108-fb90f5acfded/go.mod h1:mSNtYzSf6iY9xM3UX42VEI+STHvMgHmrYzEHPcdhB8A=
github.com/livekit/protocol v1.42.3-0.20251016122026-bc6809b5bdc8 h1:JSAUNDIK36tYjvnahJihmOXISuco5xvKy5msXLp855A=
Expand Down
1 change: 1 addition & 0 deletions pkg/sip/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ func (c *inboundCall) runMediaConn(offerData []byte, enc livekit.SIPMediaEncrypt
MediaTimeout: c.s.conf.MediaTimeout,
EnableJitterBuffer: c.jitterBuf,
Stats: &c.stats.Port,
NoInputResample: !RoomResample,
}, RoomSampleRate)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions pkg/sip/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (s *Stats) MarshalJSON() ([]byte, error) {
const (
channels = 1
RoomSampleRate = 48000
RoomResample = false
)

func newRTPStatsHandler(mon *stats.CallMonitor, typ string, r rtp.Handler) rtp.Handler {
Expand Down
16 changes: 13 additions & 3 deletions pkg/sip/media_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type MediaOptions struct {
MediaTimeout time.Duration
Stats *PortStats
EnableJitterBuffer bool
NoInputResample bool
}

func NewMediaPort(log logger.Logger, mon *stats.CallMonitor, opts *MediaOptions, sampleRate int) (*MediaPort, error) {
Expand Down Expand Up @@ -157,6 +158,10 @@ func NewMediaPortWith(log logger.Logger, mon *stats.CallMonitor, conn UDPConn, o
conn = c
}
mediaTimeout := make(chan struct{})
inSampleRate := sampleRate
if opts.NoInputResample {
inSampleRate = -1 // set only after SDP is accepted
}
p := &MediaPort{
log: log,
opts: opts,
Expand All @@ -167,7 +172,7 @@ func NewMediaPortWith(log logger.Logger, mon *stats.CallMonitor, conn UDPConn, o
jitterEnabled: opts.EnableJitterBuffer,
port: newUDPConn(log, conn),
audioOut: msdk.NewSwitchWriter(sampleRate),
audioIn: msdk.NewSwitchWriter(sampleRate),
audioIn: msdk.NewSwitchWriter(inSampleRate),
stats: opts.Stats,
}
p.timeoutInitial.Store(&opts.MediaTimeoutInitial)
Expand Down Expand Up @@ -585,14 +590,19 @@ func (p *MediaPort) setupOutput() error {

func (p *MediaPort) setupInput() {
// Decoding pipeline (SIP RTP -> LK PCM)
audioHandler := p.conf.Audio.Codec.DecodeRTP(p.audioIn, p.conf.Audio.Type)
codec := p.conf.Audio.Codec
codecInfo := codec.Info()
if p.opts.NoInputResample {
p.audioIn.SetSampleRate(codecInfo.SampleRate)
}
audioHandler := codec.DecodeRTP(p.audioIn, p.conf.Audio.Type)
p.audioInHandler = audioHandler

mux := rtp.NewMux(nil)
mux.SetDefault(newRTPStatsHandler(p.mon, "", nil))
mux.Register(
p.conf.Audio.Type, newRTPHandlerCount(
newRTPStatsHandler(p.mon, p.conf.Audio.Codec.Info().SDPName, audioHandler),
newRTPStatsHandler(p.mon, codecInfo.SDPName, audioHandler),
&p.stats.AudioPackets, &p.stats.AudioBytes,
),
)
Expand Down
45 changes: 31 additions & 14 deletions pkg/sip/media_port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ func TestMediaPort(t *testing.T) {
log := logger.GetLogger()

m1, err := NewMediaPortWith(log.WithName("one"), nil, c1, &MediaOptions{
IP: newIP("1.1.1.1"),
Ports: rtcconfig.PortRange{Start: 10000},
IP: newIP("1.1.1.1"),
Ports: rtcconfig.PortRange{Start: 10000},
NoInputResample: true,
}, tconf.Rate)
require.NoError(t, err)
defer m1.Close()
Expand Down Expand Up @@ -235,11 +236,13 @@ func TestMediaPort(t *testing.T) {
err = m2.SetConfig(conf)
require.NoError(t, err)

require.Equal(t, info.SDPName, m1.Config().Audio.Codec.Info().SDPName)
require.Equal(t, info.SDPName, m2.Config().Audio.Codec.Info().SDPName)
codec1 := m1.Config().Audio.Codec
codec2 := m2.Config().Audio.Codec
require.Equal(t, info.SDPName, codec1.Info().SDPName)
require.Equal(t, info.SDPName, codec2.Info().SDPName)

var buf1 msdk.PCM16Sample
bw1 := msdk.NewPCM16BufferWriter(&buf1, tconf.Rate)
bw1 := msdk.NewPCM16BufferWriter(&buf1, codec1.Info().SampleRate)
m1.WriteAudioTo(bw1)

var buf2 msdk.PCM16Sample
Expand All @@ -257,7 +260,8 @@ func TestMediaPort(t *testing.T) {
sample2[i] = -5116
}

writes := 1
writes1 := 1
writes2 := 1
if tconf.Rate == nativeRate {
expChain := fmt.Sprintf("Switch(%d) -> %s(encode) -> RTP(%d)", nativeRate, name, nativeRate)
require.Equal(t, expChain, w1.String())
Expand All @@ -271,20 +275,31 @@ func TestMediaPort(t *testing.T) {
require.Equal(t, expChain, w1.String())
require.Equal(t, expChain, w2.String())

expChain = fmt.Sprintf("RTP(%d) -> %s(decode) -> Resample(%d->48000) -> Switch(48000) -> Buffer(48000)", nativeRate, name, nativeRate)
require.Equal(t, expChain, PrintAudioInWriter(m1))
require.Equal(t, expChain, PrintAudioInWriter(m2))

writes += 2 // resampler will buffer a few frames
// This side does not resample the received audio, it uses sample rate of the RTP source.
expChain1 := fmt.Sprintf("RTP(%d) -> %s(decode) -> Switch(%d) -> Buffer(%d)", nativeRate, name, nativeRate, nativeRate)
// This side resamples the received audio to the expected sample rate.
expChain2 := fmt.Sprintf("RTP(%d) -> %s(decode) -> Resample(%d->48000) -> Switch(48000) -> Buffer(48000)", nativeRate, name, nativeRate)
require.Equal(t, expChain1, PrintAudioInWriter(m1))
require.Equal(t, expChain2, PrintAudioInWriter(m2))

// resampler will buffer a few frames
writes1 += 2
writes2 += 2
// a few more because of higher resample quality required
if nativeRate == 8000 {
writes += 3 // a few more because of higher resample quality required
writes1 += 3
writes2 += 5
}
if strings.HasPrefix(info.SDPName, "G722/") {
writes2 += 1
}
}

for range writes {
for range writes1 {
err = w1.WriteSample(sample1)
require.NoError(t, err)

}
for range writes2 {
err = w2.WriteSample(sample2)
require.NoError(t, err)
}
Expand All @@ -309,6 +324,7 @@ func TestMediaPort(t *testing.T) {
}

func checkPCM(t testing.TB, exp, got msdk.PCM16Sample) {
t.Helper()
require.Equal(t, len(exp), len(got))
expSamples := slices.Clone(exp)
slices.Sort(expSamples)
Expand Down Expand Up @@ -338,6 +354,7 @@ func newMediaPair(t testing.TB, opt1, opt2 *MediaOptions) (m1, m2 *MediaPort) {

opt1.IP = newIP("1.1.1.1")
opt1.Ports = rtcconfig.PortRange{Start: 10000}
opt1.NoInputResample = true

opt2.IP = newIP("2.2.2.2")
opt2.Ports = rtcconfig.PortRange{Start: 20000}
Expand Down
1 change: 1 addition & 0 deletions pkg/sip/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (c *Client) newCall(ctx context.Context, conf *config.Config, log logger.Lo
MediaTimeout: c.conf.MediaTimeout,
EnableJitterBuffer: call.jitterBuf,
Stats: &call.stats.Port,
NoInputResample: !RoomResample,
}, RoomSampleRate)
if err != nil {
call.close(errors.Wrap(err, "media failed"), callDropped, "media-failed", livekit.DisconnectReason_UNKNOWN_REASON)
Expand Down