Skip to content

Commit a61278a

Browse files
authored
move jitter buffer before mux (#327)
* move jitter buffer before mux * update test client audio
1 parent 5f0a209 commit a61278a

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

pkg/sip/media_port.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,7 @@ func (p *MediaPort) setupInput() {
239239
// Decoding pipeline (SIP RTP -> LK PCM)
240240
audioHandler := p.conf.Audio.Codec.DecodeRTP(p.audioIn, p.conf.Audio.Type)
241241
p.audioInHandler = audioHandler
242-
if p.jitterEnabled {
243-
audioHandler = rtp.HandleJitter(p.conf.Audio.Codec.Info().RTPClockRate, audioHandler)
244-
}
242+
245243
mux := rtp.NewMux(nil)
246244
mux.SetDefault(newRTPStatsHandler(p.mon, "", nil))
247245
mux.Register(p.conf.Audio.Type, newRTPStatsHandler(p.mon, p.conf.Audio.Codec.Info().SDPName, audioHandler))
@@ -258,7 +256,12 @@ func (p *MediaPort) setupInput() {
258256
return nil
259257
})))
260258
}
261-
p.conn.OnRTP(mux)
259+
260+
if p.jitterEnabled {
261+
p.conn.OnRTP(rtp.HandleJitter(p.conf.Audio.Codec.Info().RTPClockRate, mux))
262+
} else {
263+
p.conn.OnRTP(mux)
264+
}
262265
}
263266

264267
// SetDTMFAudio forces SIP to generate audio dTMF tones in addition to digital signals.

pkg/siptest/client.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/icholy/digest"
3838
"github.com/pion/sdp/v3"
3939

40+
"github.com/livekit/sip/pkg/mixer"
4041
"github.com/livekit/sipgo"
4142
"github.com/livekit/sipgo/sip"
4243

@@ -106,7 +107,7 @@ func NewClient(id string, conf ClientConfig) (*Client, error) {
106107
cli.media = rtp.NewSeqWriter(cli.mediaConn)
107108
cli.mediaAudio = cli.media.NewStream(cli.audioType, codec.Info().RTPClockRate)
108109
cli.mediaDTMF = cli.media.NewStream(101, dtmf.SampleRate)
109-
cli.audioOut = cli.audioCodec.EncodeRTP(cli.mediaAudio)
110+
cli.audioOut = mixer.NewMixer(cli.audioCodec.EncodeRTP(cli.mediaAudio), rtp.DefFrameDur)
110111

111112
cli.setupRTPReceiver()
112113

@@ -174,7 +175,7 @@ type Client struct {
174175
media *rtp.SeqWriter
175176
mediaAudio *rtp.Stream
176177
mediaDTMF *rtp.Stream
177-
audioOut media.PCM16Writer
178+
audioOut *mixer.Mixer
178179
sipClient *sipgo.Client
179180
sipServer *sipgo.Server
180181
inviteReq *sip.Request
@@ -399,7 +400,9 @@ func (c *Client) sendBye() {
399400

400401
func (c *Client) SendDTMF(digits string) error {
401402
c.log.Debug("sending dtmf", "str", digits)
402-
return dtmf.Write(context.Background(), c.audioOut, c.mediaDTMF, c.mediaAudio.GetCurrentTimestamp(), digits)
403+
w := c.audioOut.NewInput()
404+
defer w.Close()
405+
return dtmf.Write(context.Background(), w, c.mediaDTMF, c.mediaAudio.GetCurrentTimestamp(), digits)
403406
}
404407

405408
func (c *Client) SendNotify(eventReq *sip.Request, notifyStatus string) error {
@@ -568,7 +571,9 @@ func (c *Client) SendAudio(path string) error {
568571
}
569572

570573
i := 0
571-
w := c.audioOut
574+
w := c.audioOut.NewInput()
575+
defer w.Close()
576+
572577
for range time.NewTicker(rtp.DefFrameDur).C {
573578
if i >= len(audioFrames) {
574579
break
@@ -584,7 +589,8 @@ func (c *Client) SendAudio(path string) error {
584589
func (c *Client) SendSilence(ctx context.Context) error {
585590
const framesPerSec = int(time.Second / rtp.DefFrameDur)
586591
buf := make(media.PCM16Sample, c.audioCodec.Info().SampleRate/framesPerSec)
587-
wr := c.audioOut
592+
wr := c.audioOut.NewInput()
593+
defer wr.Close()
588594

589595
ticker := time.NewTicker(rtp.DefFrameDur)
590596
defer ticker.Stop()
@@ -612,7 +618,9 @@ func (c *Client) SendSignal(ctx context.Context, n int, val int) error {
612618
const framesPerSec = int(time.Second / rtp.DefFrameDur)
613619
signal := make(media.PCM16Sample, c.audioCodec.Info().SampleRate/framesPerSec)
614620
audiotest.GenSignal(signal, []audiotest.Wave{{Ind: val, Amp: signalAmp}})
615-
wr := c.audioOut
621+
wr := c.audioOut.NewInput()
622+
defer wr.Close()
623+
616624
c.log.Info("sending signal", "len", len(signal), "n", n, "sig", val)
617625

618626
ticker := time.NewTicker(rtp.DefFrameDur)

test/client/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ This is a SIP client that sends an INVITE and then sends PCMU audio for a local
66

77
At this time no options exist, `go run .` is all that is required. It connects to a SIP server running on the local machine and will send audio.
88

9-
This program expects a `audio.mkv` to be in the directory. The file should contain PCMU in 20ms samples with a samplerate of 8000 and one channel.
10-
You can use the command below to convert a file, or use [this](https://siobud.com/audio.mkv) pre-encoded one.
9+
This program expects a `audio.mkv` to be in the directory. The file should contain PCM s16le in 20ms samples with a sample rate of 8000 and one channel.
10+
You can use the command below to convert a file, or convert [this](https://siobud.com/audio.mkv) one.
1111

1212

13-
### Creating a `audio.mkv` with GStreamer
13+
### Converting an audio file
1414

15-
`gst-launch-1.0 filesrc location=$AUDIO_FILE ! decodebin ! audiomixer output-buffer-duration=20000000 ! audiorate ! audioresample ! audioconvert ! audio/x-raw, rate=8000, channels=1 ! mulawenc ! matroskamux ! filesink location=audio.mkv`
15+
`ffmpeg -i $AUDIO_FILE -c:a pcm_s16le audio.mkv`
16+
17+
`gst-launch-1.0 filesrc location=$AUDIO_FILE ! decodebin ! audiomixer output-buffer-duration=20000000 ! audiorate ! audioresample ! audioconvert ! audio/x-raw, format=S16LE, rate=8000, channels=1 ! matroskamux ! filesink location=audio.mkv`

test/cloud/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestSIP(t *testing.T) {
4545
defer cancel()
4646
go b.SendSilence(ctx)
4747

48-
go a.SendAudio("audio-pcm.mkv")
48+
go a.SendAudio("audio.mkv")
4949

5050
time.Sleep(time.Second * 5)
5151
_ = a.SendDTMF("2345")

0 commit comments

Comments
 (0)