From c5ddec43f0f12e48979f1a4c2bc3a03ca683a45d Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Fri, 22 Dec 2023 20:12:14 +0200 Subject: [PATCH] Set media destination from SDP. (#36) --- pkg/sip/inbound.go | 11 +++++++---- pkg/sip/signaling.go | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pkg/sip/inbound.go b/pkg/sip/inbound.go index 759a212b..948da57a 100644 --- a/pkg/sip/inbound.go +++ b/pkg/sip/inbound.go @@ -256,16 +256,19 @@ func (c *inboundCall) sendBye() { } func (c *inboundCall) runMediaConn(offerData []byte, conf *config.Config) (answerData []byte, _ error) { + offer := sdp.SessionDescription{} + if err := offer.Unmarshal(offerData); err != nil { + return nil, err + } conn := NewMediaConn() conn.OnRTP(c) + if dst := sdpGetAudioDest(offer); dst != nil { + conn.SetDestAddr(dst) + } if err := conn.Start(conf.RTPPort.Start, conf.RTPPort.End, "0.0.0.0"); err != nil { return nil, err } c.rtpConn = conn - offer := sdp.SessionDescription{} - if err := offer.Unmarshal(offerData); err != nil { - return nil, err - } // Encoding pipeline (LK -> SIP) // Need to be created earlier to send the pin prompts. diff --git a/pkg/sip/signaling.go b/pkg/sip/signaling.go index 30fd81d0..36a235c9 100644 --- a/pkg/sip/signaling.go +++ b/pkg/sip/signaling.go @@ -16,6 +16,8 @@ package sip import ( "math/rand" + "net" + "net/netip" "github.com/pion/sdp/v2" ) @@ -108,3 +110,27 @@ func sdpGenerateAnswer(offer sdp.SessionDescription, publicIp string, rtpListene return answer.Marshal() } + +func sdpGetAudioDest(offer sdp.SessionDescription) *net.UDPAddr { + ci := offer.ConnectionInformation + if ci.NetworkType != "IN" { + return nil + } + ip, err := netip.ParseAddr(ci.Address.Address) + if err != nil { + return nil + } + var audio *sdp.MediaDescription + for _, m := range offer.MediaDescriptions { + if m.MediaName.Media == "audio" { + audio = m + } + } + if audio == nil { + return nil + } + return &net.UDPAddr{ + IP: ip.AsSlice(), + Port: audio.MediaName.Port.Value, + } +}