Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly send BYE for inbound. #14

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/sip/config.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ import (
"net"
"net/http"
"time"

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

var source *rand.Rand
@@ -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
}

26 changes: 21 additions & 5 deletions pkg/sip/inbound.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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
}
@@ -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
}