Skip to content

Commit

Permalink
feat: adding media update support on dial
Browse files Browse the repository at this point in the history
  • Loading branch information
emiago committed Jun 19, 2024
1 parent 6dd42ee commit 76c0a46
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
toolchain go1.21.5

require (
github.com/emiago/media v0.1.0
github.com/emiago/media v0.1.1-0.20240619203804-4165fb8ba225
github.com/emiago/sipgo v0.22.0
github.com/icholy/digest v0.1.22
github.com/pion/rtp v1.8.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emiago/media v0.1.0 h1:w+TWK/lTfi2A/Jszl6eRXYF7WRY9Ac5R+Quh+Kpom2o=
github.com/emiago/media v0.1.0/go.mod h1:ZR7F8OydXIBOSFXQwxkMs56fJD3I/KpN/Z+ZOhyaIiY=
github.com/emiago/media v0.1.1-0.20240619203804-4165fb8ba225 h1:4oRmN/GPAx9UUw1EDbk3xNR4Z73WLDpsdf0AJ5RvGGY=
github.com/emiago/media v0.1.1-0.20240619203804-4165fb8ba225/go.mod h1:phTj1EGDttAaMyKVvR/NG2CTdUVnbIDm5QzdH4nZUsU=
github.com/emiago/sipgo v0.22.0 h1:GaQ51m26M9QnVBVY2aDJ/mXqq/BDfZ1A+nW7XgU/4Ts=
github.com/emiago/sipgo v0.22.0/go.mod h1:a77FgPEEjJvfYWYfP3p53u+dNhWEMb/VGVS6guvBzx0=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
Expand Down
61 changes: 60 additions & 1 deletion phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ type DialOptions struct {
// 1st with state NONE and dialog=nil. This is to have caller prepared
// 2nd with state Established or Ended with dialog
OnRefer func(state DialogReferState)

// Experimental
//
// OnMedia handles INVITE updates and passes new MediaSession with new propertie
OnMedia func(sess *media.MediaSession)
}

type DialogReferState struct {
Expand Down Expand Up @@ -571,6 +576,57 @@ func (p *Phone) Dial(dialCtx context.Context, recipient sip.Uri, o DialOptions)
o.OnRefer(DialogReferState{State: sip.DialogStateConfirmed, Dialog: newDialog})
})

var dialogRef *DialogClientSession
// waitingAck := atomic.

server.OnInvite(func(req *sip.Request, tx sip.ServerTransaction) {
id, err := sip.UACReadRequestDialogID(req)
if err != nil {
tx.Respond(sip.NewResponseFromRequest(req, sip.StatusBadRequest, "Bad Request", nil))
return
}

if dialogRef.ID != id {
tx.Respond(sip.NewResponseFromRequest(req, sip.StatusNotFound, "Dialog does not exist", nil))
return
}

// Forking current dialog session and applying new SDP
msess := dialogRef.MediaSession.Fork()

if err := msess.RemoteSDP(req.Body()); err != nil {
tx.Respond(sip.NewResponseFromRequest(req, sip.StatusBadRequest, "SDP applying failed", nil))
return
}

log.Info().
Str("formats", logFormats(msess.Formats)).
Str("localAddr", msess.Laddr.String()).
Str("remoteAddr", msess.Raddr.String()).
Msg("Media/RTP session updated")

if o.OnMedia != nil {
o.OnMedia(msess)
tx.Respond(sip.NewResponseFromRequest(req, sip.StatusOK, "OK", nil))
return
}
tx.Respond(sip.NewResponseFromRequest(req, sip.StatusMethodNotAllowed, "Method not allowed", nil))
})

server.OnAck(func(req *sip.Request, tx sip.ServerTransaction) {
// This gets received when we send 200 on INVITE media update
id, err := sip.UACReadRequestDialogID(req)
if err != nil {
tx.Respond(sip.NewResponseFromRequest(req, sip.StatusBadRequest, "Bad Request", nil))
return
}

if dialogRef.ID != id {
tx.Respond(sip.NewResponseFromRequest(req, sip.StatusNotFound, "Dialog does not exist", nil))
return
}
})

// Start server
// for _, l := range listeners {
// log.Info().Str("network", l.Network).Str("addr", l.Addr).Msg("Listening on")
Expand Down Expand Up @@ -610,6 +666,8 @@ func (p *Phone) Dial(dialCtx context.Context, recipient sip.Uri, o DialOptions)
return nil, err
}

dialogRef = dialog

return dialog, nil
}

Expand Down Expand Up @@ -842,8 +900,9 @@ func (p *Phone) answer(ansCtx context.Context, opts AnswerOptions) (*DialogServe
didAnswered, _ := sip.MakeDialogIDFromResponse(d.InviteResponse)
did, _ := sip.MakeDialogIDFromRequest(req)
if did == didAnswered {

// We received INVITE for update
if err := d.MediaSession.UpdateDestinationSDP(req.Body()); err != nil {
if err := d.MediaSession.RemoteSDP(req.Body()); err != nil {
res := sip.NewResponseFromRequest(req, 400, err.Error(), nil)
if err := tx.Respond(res); err != nil {
log.Error().Err(err).Msg("Fail to send 400")
Expand Down

0 comments on commit 76c0a46

Please sign in to comment.