Skip to content

Commit 8409020

Browse files
committed
Use the new codec interfaces.
1 parent 3cd304b commit 8409020

File tree

6 files changed

+47
-29
lines changed

6 files changed

+47
-29
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/icholy/digest v1.1.0
1010
github.com/jfreymuth/oggvorbis v1.0.5
1111
github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731
12-
github.com/livekit/media-sdk v0.0.0-20260401192012-ea94ab340a57
12+
github.com/livekit/media-sdk v0.0.0-20260407164306-d409a9b99b22 // FIXME: update when media-sdk PR is merged
1313
github.com/livekit/mediatransportutil v0.0.0-20260309115634-0e2e24b36ee8
1414
github.com/livekit/protocol v1.45.1
1515
github.com/livekit/psrpc v0.7.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ github.com/lithammer/shortuuid/v4 v4.2.0 h1:LMFOzVB3996a7b8aBuEXxqOBflbfPQAiVzkI
132132
github.com/lithammer/shortuuid/v4 v4.2.0/go.mod h1:D5noHZ2oFw/YaKCfGy0YxyE7M0wMbezmMjPdhyEFe6Y=
133133
github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731 h1:9x+U2HGLrSw5ATTo469PQPkqzdoU7be46ryiCDO3boc=
134134
github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
135-
github.com/livekit/media-sdk v0.0.0-20260401192012-ea94ab340a57 h1:Y0dZHH9gY70h+jPYEonvWUrPXxrp8YC+LWDaUUM/1mA=
136-
github.com/livekit/media-sdk v0.0.0-20260401192012-ea94ab340a57/go.mod h1:7ssWiG+U4xnbvLih9WiZbhQP6zIKMjgXdUtIE1bm/E8=
135+
github.com/livekit/media-sdk v0.0.0-20260407164306-d409a9b99b22 h1:TSJIRN9WZ76jjnlOX4MR5UCbptA/8mR6iU+mR2aN9U8=
136+
github.com/livekit/media-sdk v0.0.0-20260407164306-d409a9b99b22/go.mod h1:7ssWiG+U4xnbvLih9WiZbhQP6zIKMjgXdUtIE1bm/E8=
137137
github.com/livekit/mediatransportutil v0.0.0-20260309115634-0e2e24b36ee8 h1:coWig9fKxdb/nwOaIoGUUAogso12GblAJh/9SA9hcxk=
138138
github.com/livekit/mediatransportutil v0.0.0-20260309115634-0e2e24b36ee8/go.mod h1:RCd46PT+6sEztld6XpkCrG1xskb0u3SqxIjy4G897Ss=
139139
github.com/livekit/protocol v1.45.1 h1:4cbynsPZW32gS2z6nUWfAfr4YaTUwZSKUiLpSpjX+lQ=

pkg/media/opus/opus.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,21 @@ import (
2929
"github.com/livekit/protocol/logger"
3030
)
3131

32-
type Writer = msdk.WriteCloser[[]byte]
32+
type Sample []byte
33+
34+
func (s Sample) Size() int {
35+
return len(s)
36+
}
37+
38+
func (s Sample) CopyTo(dst []byte) (int, error) {
39+
if len(dst) < len(s) {
40+
return 0, io.ErrShortBuffer
41+
}
42+
n := copy(dst, s)
43+
return n, nil
44+
}
45+
46+
type Writer = msdk.WriteCloser[Sample]
3347

3448
type params struct {
3549
SampleRate int
@@ -95,7 +109,7 @@ func (d *decoder) SampleRate() int {
95109
return d.w.SampleRate()
96110
}
97111

98-
func (d *decoder) WriteSample(in []byte) error {
112+
func (d *decoder) WriteSample(in Sample) error {
99113
if len(in) == 0 {
100114
return nil
101115
}
@@ -131,7 +145,7 @@ type encoder struct {
131145
w Writer
132146
enc *opus.Encoder
133147
inbuf msdk.PCM16Sample
134-
buf []byte
148+
buf Sample
135149

136150
successiveErrorCount int
137151
}
@@ -192,6 +206,6 @@ func (e *encoder) Close() error {
192206
return err2
193207
}
194208

195-
func NewWebmWriter(w io.WriteCloser, sampleRate int, sampleDur time.Duration) msdk.WriteCloser[[]byte] {
196-
return webm.NewWriter[[]byte](w, "A_OPUS", 2, sampleRate, sampleDur)
209+
func NewWebmWriter(w io.WriteCloser, sampleRate int, sampleDur time.Duration) msdk.WriteCloser[Sample] {
210+
return webm.NewWriter[Sample](w, "A_OPUS", 2, sampleRate, sampleDur)
197211
}

pkg/sip/media_port.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ func newUDPConn(log logger.Logger, conn UDPConn, symmetricRTP bool) *udpConn {
204204

205205
type udpConn struct {
206206
UDPConn
207-
stopping core.Fuse
208-
stopped chan struct{}
209-
log logger.Logger
207+
stopping core.Fuse
208+
stopped chan struct{}
209+
log logger.Logger
210210
symmetricRTP bool
211-
src atomic.Pointer[netip.AddrPort]
212-
dst atomic.Pointer[netip.AddrPort]
211+
src atomic.Pointer[netip.AddrPort]
212+
dst atomic.Pointer[netip.AddrPort]
213213
}
214214

215215
func (c *udpConn) GetSrc() (netip.AddrPort, bool) {
@@ -813,7 +813,7 @@ func (p *MediaPort) setupOutput(tid traceid.ID) error {
813813
p.audioOutRTP = s.NewStream(p.conf.Audio.Type, codecInfo.RTPClockRate)
814814

815815
// Encoding pipeline (LK PCM -> SIP RTP)
816-
audioOut := p.conf.Audio.Codec.EncodeRTP(p.audioOutRTP)
816+
audioOut := rtp.EncodePCM(p.audioOutRTP, p.conf.Audio.Codec)
817817
if p.stats != nil {
818818
audioOut = newMediaWriterCount(audioOut, &p.stats.AudioOutFrames, &p.stats.AudioOutSamples)
819819
}
@@ -871,7 +871,7 @@ func (p *MediaPort) setupInput() {
871871
audioWriter = signalLogger
872872
}
873873
}
874-
audioHandler := p.conf.Audio.Codec.DecodeRTP(audioWriter, p.conf.Audio.Type)
874+
audioHandler := rtp.DecodePCM(audioWriter, p.conf.Audio.Codec, p.conf.Audio.Type)
875875
// Wrap the decoder with silence suppression handler to fill gaps during silence suppression
876876
audioHandler = newSilenceFiller(audioHandler, audioWriter, codecInfo.RTPClockRate, codecInfo.SampleRate, p.log)
877877
p.audioInHandler = audioHandler

pkg/sip/room.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ func (r *Room) NewParticipantTrack(sampleRate int) (msdk.WriteCloser[msdk.PCM16S
552552
}); err != nil {
553553
return nil, err
554554
}
555-
ow := msdk.FromSampleWriter[[]byte](track, sampleRate, rtp.DefFrameDur)
555+
ow := msdk.FromSampleWriter[opus.Sample](track, sampleRate, rtp.DefFrameDur)
556556
pw, err := opus.Encode(ow, channels, r.log)
557557
if err != nil {
558558
return nil, err

pkg/sip/silence_filler.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ import (
2727
// silenceFiller detects RTP timestamp discontinuities (silence suppression)
2828
// and generates silence samples to fill the gaps before passing packets to the decoder.
2929
type silenceFiller struct {
30-
maxGapSize int
31-
encodedSink rtp.Handler
32-
pcmSink msdk.PCM16Writer
33-
rtpSamplesPerFrame int // For gap detection (based on RTP clock rate)
34-
pcmSamplesPerFrame int // For silence generation (based on PCM output rate)
35-
log logger.Logger
36-
lastTS atomic.Uint64
37-
lastSeq atomic.Uint64
38-
packets atomic.Uint64
39-
gapCount atomic.Uint64
40-
gapSizeSum atomic.Uint64
41-
lastPrintTime time.Time
30+
maxGapSize int
31+
encodedSink rtp.HandlerCloser
32+
pcmSink msdk.PCM16Writer
33+
rtpSamplesPerFrame int // For gap detection (based on RTP clock rate)
34+
pcmSamplesPerFrame int // For silence generation (based on PCM output rate)
35+
log logger.Logger
36+
lastTS atomic.Uint64
37+
lastSeq atomic.Uint64
38+
packets atomic.Uint64
39+
gapCount atomic.Uint64
40+
gapSizeSum atomic.Uint64
41+
lastPrintTime time.Time
4242
}
4343

4444
type SilenceSuppressionOption func(*silenceFiller)
@@ -51,7 +51,7 @@ func WithMaxGapSize(maxGapSize int) SilenceSuppressionOption {
5151
}
5252
}
5353

54-
func newSilenceFiller(encodedSink rtp.Handler, pcmSink msdk.PCM16Writer, rtpClockRate int, pcmSampleRate int, log logger.Logger, options ...SilenceSuppressionOption) rtp.Handler {
54+
func newSilenceFiller(encodedSink rtp.HandlerCloser, pcmSink msdk.PCM16Writer, rtpClockRate int, pcmSampleRate int, log logger.Logger, options ...SilenceSuppressionOption) rtp.HandlerCloser {
5555
// TODO: We assume 20ms frame. We would need to adjust this when:
5656
// - When we add support for other frame durations.
5757
// - When we add support for re-INVITE sdp renegotiation (maybe, if we don't destroy this and start over).
@@ -75,6 +75,10 @@ func newSilenceFiller(encodedSink rtp.Handler, pcmSink msdk.PCM16Writer, rtpCloc
7575
return h
7676
}
7777

78+
func (h *silenceFiller) Close() {
79+
h.encodedSink.Close()
80+
}
81+
7882
func (h *silenceFiller) String() string {
7983
return fmt.Sprintf("SilenceFiller(%d) -> %s", h.maxGapSize, h.encodedSink.String())
8084
}

0 commit comments

Comments
 (0)