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

Client example-the example couldn't send voice to other UAC #85

Open
myahuang opened this issue Aug 11, 2022 · 1 comment
Open

Client example-the example couldn't send voice to other UAC #85

myahuang opened this issue Aug 11, 2022 · 1 comment

Comments

@myahuang
Copy link

Hi I have tested the client example ,and it can register to UAS, and can establish a session with other UAC, but the partner could't receive rtp stream, only receive echos as following

[2022-08-11 23:53:45.240] DEBUG Media: Send to 192.168.2.94:7076, length 172
[2022-08-11 23:53:45.258] INFO Client: Rtp recevied: 172, laddr 192.168.2.60:36191 : raddr 192.168.2.94:7076
[2022-08-11 23:53:45.258] INFO Client: Echo rtp to 192.168.2.94:7076

Why? I'm a freshman in SIP protocal.

@myahuang
Copy link
Author

package main

import (
"net"
"os"
"os/signal"
"syscall"
"time"

"github.com/cloudwebrtc/go-sip-ua/examples/mock"
"github.com/cloudwebrtc/go-sip-ua/pkg/account"
"github.com/cloudwebrtc/go-sip-ua/pkg/media/rtp"
"github.com/cloudwebrtc/go-sip-ua/pkg/session"
"github.com/cloudwebrtc/go-sip-ua/pkg/stack"
"github.com/cloudwebrtc/go-sip-ua/pkg/ua"
"github.com/cloudwebrtc/go-sip-ua/pkg/utils"
"github.com/ghettovoice/gosip/log"
"github.com/ghettovoice/gosip/sip"
"github.com/ghettovoice/gosip/sip/parser"

)

var (
logger log.Logger
udp *rtp.RtpUDPStream
)

func init() {
logger = utils.NewLogrusLogger(log.DebugLevel, "Client", nil)
}

func createUdp() *rtp.RtpUDPStream {

udp = rtp.NewRtpUDPStream("192.168.2.60", rtp.DefaultPortMin, rtp.DefaultPortMax, func(data []byte, raddr net.Addr) {
	logger.Infof("Rtp recevied: %v, laddr %s : raddr %s", len(data), udp.LocalAddr().String(), raddr)
	dest, _ := net.ResolveUDPAddr(raddr.Network(), raddr.String())
	logger.Infof("Echo rtp to %v", raddr)
	udp.Send(data, dest)
})

go udp.Read()

return udp

}

func main() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
stack := stack.NewSipStack(&stack.SipStackConfig{
UserAgent: "Go Sip Client/example-client",
Extensions: []string{"replaces", "outbound"},
Dns: "8.8.8.8"})

listen := "0.0.0.0:5080"
logger.Infof("Listen => %s", listen)

if err := stack.Listen("udp", listen); err != nil {
	logger.Panic(err)
}

if err := stack.Listen("tcp", listen); err != nil {
	logger.Panic(err)
}

if err := stack.ListenTLS("wss", "0.0.0.0:5091", nil); err != nil {
	logger.Panic(err)
}

ua := ua.NewUserAgent(&ua.UserAgentConfig{
	SipStack: stack,
})

ua.InviteStateHandler = func(sess *session.Session, req *sip.Request, resp *sip.Response, state session.Status) {
	logger.Infof("InviteStateHandler: state => %v, type => %s", state, sess.Direction())

	switch state {
	case session.InviteReceived:
		udp = createUdp()
		udpLaddr := udp.LocalAddr()
		sdp := mock.BuildLocalSdp(udpLaddr.IP.String(), udpLaddr.Port)
		sess.ProvideAnswer(sdp)
		sess.Accept(200)
	case session.Canceled:
		fallthrough
	case session.Failure:
		fallthrough
	case session.Terminated:
		udp.Close()
	}
}

ua.RegisterStateHandler = func(state account.RegisterState) {
	logger.Infof("RegisterStateHandler: user => %s, state => %v, expires => %v", state.Account.AuthInfo.AuthUser, state.StatusCode, state.Expiration)
}

uri, err := parser.ParseUri("sip:[email protected]")
if err != nil {
	logger.Error(err)
}

profile := account.NewProfile(uri.Clone(), "goSIP/example-client",
	&account.AuthInfo{
		AuthUser: "sa",
		Password: "sa",
		Realm:    "",
	},
	1800,
	stack,
)

recipient, err := parser.ParseSipUri("sip:192.168.2.60")
if err != nil {
	logger.Error(err)
}

register, _ := ua.SendRegister(profile, recipient, profile.Expires, nil)
time.Sleep(time.Second * 3)

udp = createUdp()
udpLaddr := udp.LocalAddr()
sdp := mock.BuildLocalSdp(udpLaddr.IP.String(), udpLaddr.Port)

called, err2 := parser.ParseUri("sip:[email protected]")
if err2 != nil {
	logger.Error(err)
}

recipient, err = parser.ParseSipUri("sip:[email protected]:5060;transport=udp")
if err != nil {
	logger.Error(err)
}

go ua.Invite(profile, called, recipient, &sdp)

<-stop

register.SendRegister(0)

ua.Shutdown()

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant