Skip to content

Commit f9508a4

Browse files
committed
update.
1 parent 731cb86 commit f9508a4

File tree

15 files changed

+123
-42
lines changed

15 files changed

+123
-42
lines changed

examples/b2bua/b2bua/b2bua.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var (
4848
)
4949

5050
func init() {
51-
logger = utils.NewLogrusLogger(log.InfoLevel, "B2BUA", nil)
51+
logger = utils.NewLogrusLogger(utils.DefaultLogLevel, "B2BUA", nil)
5252
callConfig = CallConfig{
5353
Codecs: []string{"PCMU", "PCMA", "opus", "H264"},
5454
ExternalRtpAddress: "0.0.0.0",
@@ -103,14 +103,16 @@ func NewB2BUA(disableAuth bool, enableTLS bool) *B2BUA {
103103
}
104104

105105
ua := ua.NewUserAgent(&ua.UserAgentConfig{
106-
107106
SipStack: stack,
108107
})
109108

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

113112
switch state {
113+
// Handle outgoing call.
114+
case session.InviteSent:
115+
114116
// Handle incoming call.
115117
case session.InviteReceived:
116118
to, _ := (*req).To()
@@ -277,6 +279,11 @@ func (b *B2BUA) removeCall(sess *session.Session) {
277279
}
278280
}
279281

282+
// Originate .
283+
func (b *B2BUA) Originate(source string, destination string) {
284+
logger.Infof("Originate %s => %s", source, destination)
285+
}
286+
280287
// Shutdown .
281288
func (b *B2BUA) Shutdown() {
282289
b.ua.Shutdown()
@@ -363,7 +370,6 @@ func (b *B2BUA) handleRegister(request sip.Request, tx sip.ServerTransaction) {
363370
sip.CopyHeaders("Expires", request, resp)
364371
utils.BuildContactHeader("Contact", request, resp, &expires)
365372
tx.Respond(resp)
366-
367373
}
368374

369375
func (b *B2BUA) handleConnectionError(connError *transport.ConnectionError) {

examples/b2bua/b2bua/call.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type B2BCall struct {
4646
}
4747

4848
func (b *B2BCall) ToString() string {
49-
return b.src.Contact() + " => " + b.dest.Contact()
49+
return (*b.src.CallID()).String() + ", src " + b.src.Contact() + " => dest" + b.dest.Contact()
5050
}
5151

5252
func (b *B2BCall) Init() {
@@ -112,7 +112,7 @@ func (b *B2BCall) SetALegOffer(sdp *Desc) error {
112112
if transType == TransportTypeRTC {
113113
trans = NewWebRTCTransport(trackInfos)
114114
} else {
115-
trans = NewUdpTansport(trackInfos)
115+
trans = NewUdpTansport("inc-"+string(*b.src.CallID()), trackInfos)
116116
}
117117

118118
err = trans.Init(callConfig)
@@ -139,7 +139,7 @@ func (b *B2BCall) CreateBLegOffer(tpType TransportType) (*Desc, error) {
139139
if tpType == TransportTypeRTC {
140140
trans = NewWebRTCTransport(b.srcTrackInfos)
141141
} else {
142-
trans = NewUdpTansport(b.srcTrackInfos)
142+
trans = NewUdpTansport("out-"+string(*b.src.CallID()), b.srcTrackInfos)
143143
}
144144

145145
err := trans.Init(callConfig)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package jitterbuffer
2+
3+
import (
4+
"github.com/pion/rtp"
5+
)
6+
7+
type JitterBufferType string
8+
9+
const (
10+
JBOFF JitterBufferType = "OFF"
11+
JBUF_FIXED JitterBufferType = "FIXED"
12+
JBUF_ADAPTIVE JitterBufferType = "ADAPTIVE"
13+
)
14+
15+
var (
16+
JBUF_RDIFF_EMA_COEFF = 1024
17+
JBUF_RDIFF_UP_SPEED = 512
18+
JBUF_PUT_TIMEOUT = 400
19+
)
20+
21+
/** Defines a packet frame */
22+
type packet struct {
23+
hdr rtp.Header /**< RTP Header */
24+
buf []byte /**< RTP Payload */
25+
}
26+
27+
type JitterBuffer struct {
28+
Type JitterBufferType
29+
30+
min int
31+
max int
32+
33+
// Wish size for adaptive mode
34+
wish int
35+
36+
payload int
37+
38+
packets []packet
39+
}
40+
41+
/*
42+
* @param min Minimum delay in [frames]
43+
* @param max Maximum delay in [packets]
44+
*/
45+
func NewJitterBuffer(jbType JitterBufferType, min, max int) *JitterBuffer {
46+
return &JitterBuffer{
47+
Type: jbType,
48+
min: min,
49+
max: max,
50+
wish: min,
51+
packets: make([]packet, max),
52+
}
53+
}

examples/b2bua/b2bua/udp.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ type UdpTansport struct {
2929
closed utils.AtomicBool
3030
ctx context.Context
3131
cancel context.CancelFunc
32+
33+
id string
3234
}
3335

34-
func NewUdpTansport(trackInfos []*TrackInfo) *UdpTansport {
36+
func NewUdpTansport(id string, trackInfos []*TrackInfo) *UdpTansport {
3537
t := &UdpTansport{
3638
trackInfos: trackInfos,
3739
ports: make(map[TrackType]*UdpPort),
3840
videoSSRC: 0,
41+
id: id,
3942
}
4043

4144
t.ctx, t.cancel = context.WithCancel(context.TODO())
@@ -79,7 +82,7 @@ func (c *UdpTansport) Init(config CallConfig) error {
7982
rRtcpAddr, _ = net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", trackInfo.Connection.Address, trackInfo.RtcpPort))
8083
}
8184

82-
udpPort, err := NewUdpPort(trackInfo.TrackType, rAddr, rRtcpAddr, config.ExternalRtpAddress)
85+
udpPort, err := NewUdpPort(c.id, trackInfo.TrackType, rAddr, rRtcpAddr, config.ExternalRtpAddress)
8386
if err != nil {
8487
return err
8588
}
@@ -255,23 +258,9 @@ func (c *UdpTansport) OnAnswer(answer *Desc) error {
255258
if err != nil {
256259
return err
257260
}
258-
/*
259-
=0
260-
o=100 703 1242 IN IP4 192.168.1.154
261-
s=Talk
262-
c=IN IP4 192.168.1.154
263-
t=0 0
264-
m=audio 40063 RTP/AVP 0 8 116
265-
a=rtpmap:116 telephone-event/8000
266-
a=rtcp:49374
267-
m=video 47878 RTP/AVP 96
268-
a=rtpmap:96 H264/90000
269-
a=fmtp:96 profile-level-id=42801F; packetization-mode=1
270-
a=rtcp:37679
271-
*/
272261
conn := sess.Connection
273262
if conn != nil {
274-
logger.Infof("remote connection address: %s", conn.Address)
263+
logger.Debugf("remote connection address: %s", conn.Address)
275264
}
276265
c.remoteDescription = sess
277266
return nil

examples/b2bua/b2bua/udp_port.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88

99
"github.com/cloudwebrtc/go-sip-ua/pkg/utils"
10+
"github.com/ghettovoice/gosip/util"
1011
)
1112

1213
type UdpPort struct {
@@ -21,14 +22,16 @@ type UdpPort struct {
2122
externalRtpAddress string
2223
rAddr *net.UDPAddr
2324
rRtcpAddr *net.UDPAddr
25+
id string
2426
}
2527

26-
func NewUdpPort(trackType TrackType, rAddr, rRtcpAddr *net.UDPAddr, externalRtpAddress string) (*UdpPort, error) {
28+
func NewUdpPort(id string, trackType TrackType, rAddr, rRtcpAddr *net.UDPAddr, externalRtpAddress string) (*UdpPort, error) {
2729
c := &UdpPort{
2830
trackType: trackType,
2931
externalRtpAddress: externalRtpAddress,
3032
rAddr: rAddr,
3133
rRtcpAddr: rRtcpAddr,
34+
id: id,
3235
}
3336
c.ctx, c.cancel = context.WithCancel(context.TODO())
3437
c.closed.Set(false)
@@ -46,7 +49,14 @@ func (c *UdpPort) Init() error {
4649
return err
4750
}
4851

49-
logger.Infof("ListenUDP: rtp %v, rtcp ", rtpConns[0].LocalAddr().String(), rtpConns[1].LocalAddr().String())
52+
host := callConfig.ExternalRtpAddress
53+
if host == "" || host == "0.0.0.0" {
54+
if v, err := util.ResolveSelfIP(); err == nil {
55+
host = v.String()
56+
}
57+
}
58+
59+
logger.Infof("[%s-%s] ListenUDP: udp://%s:%v, udp://%s:%v", c.id, c.trackType, host, rtpConns[0].LocalAddr().(*net.UDPAddr).Port, host, rtpConns[1].LocalAddr().(*net.UDPAddr).Port)
5060

5161
go c.loop(rtpConns[0], func(packet []byte, raddr net.Addr) {
5262
c.mutex.Lock()
@@ -161,7 +171,7 @@ func (c *UdpPort) loop(conn *net.UDPConn, onPacketReceived func(data []byte, rad
161171
}
162172
n, raddr, err := conn.ReadFromUDP(buf)
163173
if err != nil {
164-
logger.Infof("RTP Conn [%v] refused, stop now!", raddr)
174+
logger.Debugf("RTP Conn [%v] refused, stop now!", raddr)
165175
return
166176
}
167177
//logger.Infof("raddr: %v, size %d", raddr, n)

examples/b2bua/main.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func completer(d prompt.Document) []prompt.Suggest {
2020
{Text: "users", Description: "Show sip accounts"},
2121
{Text: "onlines", Description: "Show online sip devices"},
2222
{Text: "calls", Description: "Show active calls"},
23+
{Text: "originate", Description: "Originate a call and bridge to another call"},
2324
{Text: "set debug on", Description: "Show debug msg in console"},
2425
{Text: "set debug off", Description: "Turn off debug msg in console"},
2526
{Text: "show loggers", Description: "Print Loggers"},
@@ -39,6 +40,21 @@ Options:
3940

4041
func consoleLoop(b2bua *b2bua.B2BUA) {
4142

43+
usersCompleter := func(d prompt.Document) []prompt.Suggest {
44+
accounts := b2bua.GetAccounts()
45+
s := make([]prompt.Suggest, 0, len(accounts))
46+
for user := range accounts {
47+
s = append(s, prompt.Suggest{Text: user, Description: "User"})
48+
}
49+
aors := b2bua.GetRegistry().GetAllContacts()
50+
for aor := range aors {
51+
for _, instance := range aors[aor] {
52+
s = append(s, prompt.Suggest{Text: instance.Contact.Address.String(), Description: "online device"})
53+
}
54+
}
55+
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
56+
}
57+
4258
fmt.Println("Please select command.")
4359
for {
4460
t := prompt.Input("CLI> ", completer,
@@ -74,14 +90,20 @@ func consoleLoop(b2bua *b2bua.B2BUA) {
7490
} else {
7591
fmt.Printf("No users\n")
7692
}
93+
case "originate":
94+
fmt.Printf("Please enter the source user: ")
95+
source := prompt.Input("Source> ", usersCompleter)
96+
fmt.Printf("Please enter the destination user: ")
97+
destination := prompt.Input("Destination> ", usersCompleter)
98+
b2bua.Originate(source, destination)
7799
case "calls":
78100
fallthrough
79101
case "cl": /* call list*/
80102
calls := b2bua.Calls()
81103
if len(calls) > 0 {
82104
fmt.Printf("Calls:\n")
83105
for _, call := range calls {
84-
fmt.Printf("%v:\n", call.ToString())
106+
fmt.Printf("%v\n", call.ToString())
85107
}
86108
} else {
87109
fmt.Printf("No active calls\n")

examples/client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var (
2525
)
2626

2727
func init() {
28-
logger = utils.NewLogrusLogger(log.DebugLevel, "Client", nil)
28+
logger = utils.NewLogrusLogger(utils.DefaultLogLevel, "Client", nil)
2929
}
3030

3131
func createUdp() *rtp.RtpUDPStream {

examples/register/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var (
2121
)
2222

2323
func init() {
24-
logger = utils.NewLogrusLogger(log.DebugLevel, "Register", nil)
24+
logger = utils.NewLogrusLogger(utils.DefaultLogLevel, "Register", nil)
2525
}
2626

2727
func main() {

pkg/account/profile.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ var (
1616
)
1717

1818
func init() {
19-
logger = utils.NewLogrusLogger(log.DebugLevel, "UserAgent", nil)
19+
logger = utils.NewLogrusLogger(utils.DefaultLogLevel, "UserAgent", nil)
2020
}
2121

22-
//AuthInfo .
22+
// AuthInfo .
2323
type AuthInfo struct {
2424
AuthUser string
2525
Realm string
@@ -66,7 +66,7 @@ func (p *Profile) Contact() *sip.Address {
6666
return contact
6767
}
6868

69-
//NewProfile .
69+
// NewProfile .
7070
func NewProfile(
7171
uri sip.Uri,
7272
displayName string,
@@ -104,7 +104,7 @@ func NewProfile(
104104
return p
105105
}
106106

107-
//RegisterState .
107+
// RegisterState .
108108
type RegisterState struct {
109109
Account *Profile
110110
StatusCode sip.StatusCode

pkg/auth/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewServerAuthorizer(callback RequestCredentialCallback, realm string, authI
5050
useAuthInt: authInt,
5151
realm: realm,
5252
}
53-
auth.log = utils.NewLogrusLogger(log.DebugLevel, "ServerAuthorizer", nil)
53+
auth.log = utils.NewLogrusLogger(utils.DefaultLogLevel, "ServerAuthorizer", nil)
5454
go func() {
5555
for now := range time.Tick(NonceExpire) {
5656
auth.mx.Lock()

0 commit comments

Comments
 (0)