Skip to content

Commit

Permalink
Set media destination from SDP. (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc authored Dec 22, 2023
1 parent d95bd1f commit c5ddec4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/sip/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions pkg/sip/signaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package sip

import (
"math/rand"
"net"
"net/netip"

"github.com/pion/sdp/v2"
)
Expand Down Expand Up @@ -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,
}
}

0 comments on commit c5ddec4

Please sign in to comment.