Skip to content

Commit

Permalink
Properly send BYE for inbound.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Dec 5, 2023
1 parent 54af3dc commit c53fdf7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pkg/sip/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"net"
"net/http"
"time"

"github.com/livekit/protocol/logger"
)

var source *rand.Rand
Expand Down Expand Up @@ -115,6 +117,7 @@ func listenUDPInPortRange(portMin, portMax int, IP net.IP) (*net.UDPConn, error)
for {
c, e := net.ListenUDP("udp", &net.UDPAddr{IP: IP, Port: portCurrent})
if e == nil {
logger.Debugw("begin listening on UDP", "port", portCurrent)
return c, nil
}

Expand Down
26 changes: 21 additions & 5 deletions pkg/sip/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (c *inboundCall) handleInvite(req *sip.Request, tx sip.ServerTransaction, c
res.AppendHeader(&contentTypeHeaderSDP)
if err = tx.Respond(res); err != nil {
logger.Errorw("Cannot respond to INVITE", err)
// TODO: should we close the call in this case?
c.Close()
return
}
c.inviteReq = req
Expand All @@ -201,8 +201,24 @@ func (c *inboundCall) sendBye() {
if c.inviteReq == nil {
return
}
res := sip.NewByeRequest(c.inviteReq, c.inviteResp, nil)
c.s.sipSrv.TransportLayer().WriteMsg(res)
// This function is for clients, so we need to swap src and dest
bye := sip.NewByeRequest(c.inviteReq, c.inviteResp, nil)
if contact, ok := c.inviteReq.Contact(); ok {
bye.Recipient = &contact.Address
} else {
bye.Recipient = &c.from.Address
}
bye.SetSource(c.inviteResp.Source())
bye.SetDestination(c.inviteResp.Destination())
bye.RemoveHeader("From")
bye.AppendHeader((*sip.FromHeader)(c.to))
bye.RemoveHeader("To")
bye.AppendHeader((*sip.ToHeader)(c.from))
if route, ok := bye.RecordRoute(); ok {
bye.RemoveHeader("Record-Route")
bye.AppendHeader(&sip.RouteHeader{Address: route.Address})
}
_ = c.s.sipSrv.TransportLayer().WriteMsg(bye)
c.inviteReq = nil
c.inviteResp = nil
}
Expand Down Expand Up @@ -274,11 +290,11 @@ func (c *inboundCall) Close() error {
return nil
}
logger.Infow("Closing inbound call", "tag", c.tag, "from", c.from.Address.User, "to", c.to.Address.User)
c.sendBye()
c.closeMedia()
c.s.cmu.Lock()
delete(c.s.activeCalls, c.tag)
c.s.cmu.Unlock()
c.closeMedia()
c.sendBye()
return nil
}

Expand Down

0 comments on commit c53fdf7

Please sign in to comment.