sipgox

package module
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2024 License: BSD-2-Clause Imports: 18 Imported by: 2

README

sipgox

is experimental/extra area to add more functionality on top sipgo lib, more specifically, to fill gap for building user agents with media. This allows much more easier voip testing or faster way to create UAC/UAS.

To find out more, read also article about E2E testing and check GO Documentation

NOTE: Media package (github.com/emiago/media) is now seperate repo.

If you find it useful, support sipgo lib, open issue etc...

Tools using this lib:

Features:

  • Simple API for UA/phone build with dial answer register actions
  • Minimal SDP package for audio
  • RTP/RTCP receiving and logging
  • Extendable MediaSession handling for RTP/RTCP handling (ex microphone,speaker)
  • Hangup control on caller
  • Timeouts handling
  • Digest auth
  • Transfers on phone answer, dial
  • DTMF on phone with simple API
  • ... who knows

Checkout echome example to see more.

Phone

Phone is wrapper that allows you to build phone in couple of lines. Then you can quickly create/receive SIP call, handle RTP/RTCP, etc... It uses sipgo.Dialog and media package.

NOTE: It has specific design for testing, and it can not be used for full softphone build.

Dialer
ua, _ := sipgo.NewUA()
defer ua.Close()

// Create a phone
phone := sipgox.NewPhone(ua) 

// Run dial
ctx, _ := context.WithTimeout(context.Background(), 60*time.Second)

// Blocks until call is answered
dialog, err := phone.Dial(ctx, sip.Uri{User:"bob", Host: "localhost", Port:5060}, sipgox.DialOptions{})
if err != nil {
    // handle error
    return
}
defer dialog.Close() // Close dialog for cleanup

select {
case <-dialog.Done():
    return
case <-time.After(5 *time.Second):
    dialog.Hangup(context.TODO())
}
Receiver
ctx, _ := context.WithCancel(context.Background())

ua, _ := sipgo.NewUA()
defer ua.Close()

// Create a phone
phone := sipgox.NewPhone(ua)

// Blocks until call is answered
dialog, err := phone.Answer(ctx, sipgox.AnswerOptions{
    Ringtime:  5* time.Second,
})
if err != nil {
    //handle error
    return
}
defer dialog.Close() // Close dialog for cleanup

select {
case <-dialog.Done():
    return
case <-time.After(10 *time.Second):
    dialog.Hangup(context.TODO())
}
Reading/Writing RTP/RTCP on dialog

After you Answer or Dial on phone, you receive dialog.

RTP

buf := make([]byte, media.RTPBufSize) // Has MTU size
pkt := rtp.Packet{}
err := dialog.ReadRTP(buf, &pkt)

err := dialog.WriteRTP(pkt)

similar is for RTCP

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRegisterFail        = fmt.Errorf("register failed")
	ErrRegisterUnathorized = fmt.Errorf("register unathorized")
)
View Source
var (
	// You can use this key with AnswerReadyCtxValue to get signal when
	// Answer is ready to receive traffic
	AnswerReadyCtxKey = "AnswerReadyCtxKey"
)
View Source
var (
	// Value must be zerolog.Logger
	ContextLoggerKey = "logger"
)

Functions

func CheckLazySipUri

func CheckLazySipUri(target string, destOverwrite string) string

We are lazy to write full sip uris

func FindFreeInterfaceHostPort added in v0.6.0

func FindFreeInterfaceHostPort(network string, targetAddr string) (ip net.IP, port int, err error)

func UACRequestBuild added in v0.8.0

func UACRequestBuild(req *sip.Request, lastReq *sip.Request, lastResp *sip.Response)

func UASRequestBuild added in v0.8.0

func UASRequestBuild(req *sip.Request, lastResp *sip.Response)

Types

type AnswerOptions

type AnswerOptions struct {
	Ringtime   time.Duration
	SipHeaders []sip.Header

	// For authorizing INVITE unless RegisterAddr is defined
	Username string
	Password string
	Realm    string //default sipgo

	RegisterAddr string //If defined it will keep registration in background

	// For SDP codec manipulating
	Formats sdp.Formats

	// OnCall is just INVITE request handler that you can use to notify about incoming call
	// After this dialog should be created and you can watch your changes with dialog.State
	// -1 == Cancel
	// 0 == continue
	// >0 different response
	OnCall func(inviteRequest *sip.Request) int

	// Default is 200 (answer a call)
	AnswerCode   sip.StatusCode
	AnswerReason string
}

type AnswerReadyCtxValue added in v0.4.0

type AnswerReadyCtxValue chan struct{}

type DialOptions

type DialOptions struct {
	// Authentication via digest challenge
	Username string
	Password string

	// Custom headers passed on INVITE
	SipHeaders []sip.Header

	// SDP Formats to customize. NOTE: Only ulaw and alaw are fully supported
	Formats sdp.Formats

	// OnResponse is just callback called after INVITE is sent and all responses before final one
	// Useful for tracking call state
	OnResponse func(inviteResp *sip.Response)

	// OnRefer is called 2 times.
	// 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 DialResponseError added in v0.3.0

type DialResponseError struct {
	InviteReq  *sip.Request
	InviteResp *sip.Response

	Msg string
}

func (DialResponseError) Error added in v0.3.0

func (e DialResponseError) Error() string

func (*DialResponseError) StatusCode added in v0.3.0

func (e *DialResponseError) StatusCode() sip.StatusCode

type DialogClientSession added in v0.5.0

type DialogClientSession struct {
	*media.MediaSession

	*sipgo.DialogClientSession
	// contains filtered or unexported fields
}

func (*DialogClientSession) Bye added in v0.5.0

func (*DialogClientSession) Close added in v0.5.0

func (d *DialogClientSession) Close() error

func (*DialogClientSession) Hangup added in v0.5.0

func (d *DialogClientSession) Hangup(ctx context.Context) error

Hangup is alias for Bye

func (*DialogClientSession) Refer added in v0.8.0

func (d *DialogClientSession) Refer(ctx context.Context, referTo sip.Uri) error

Refer tries todo refer (blind transfer) on call

type DialogReferState added in v0.8.0

type DialogReferState struct {
	// Updates current transfer progress with state
	State sip.DialogState
	// Dialog present when state is answered (Confirmed dialog state)
	Dialog *DialogClientSession
}

type DialogServerSession added in v0.5.0

type DialogServerSession struct {
	*media.MediaSession

	*sipgo.DialogServerSession
	// contains filtered or unexported fields
}

func (*DialogServerSession) Bye added in v0.5.0

func (*DialogServerSession) Close added in v0.5.0

func (d *DialogServerSession) Close() error

func (*DialogServerSession) Hangup added in v0.5.0

func (d *DialogServerSession) Hangup(ctx context.Context) error

Hangup is alias for Bye

func (*DialogServerSession) Notify added in v0.8.0

func (d *DialogServerSession) Notify(req *sip.Request) error

func (*DialogServerSession) Refer added in v0.8.0

func (d *DialogServerSession) Refer(ctx context.Context, referTo sip.Uri) error

Refer tries todo refer (blind transfer) on call

type ListenAddr

type ListenAddr struct {
	Network string
	Addr    string
	TLSConf *tls.Config
}

type Listener

type Listener struct {
	ListenAddr
	io.Closer
	Listen func() error
}

type Phone

type Phone struct {
	UA *sipgo.UserAgent
	// contains filtered or unexported fields
}

func NewPhone

func NewPhone(ua *sipgo.UserAgent, options ...PhoneOption) *Phone

func (*Phone) Answer

func (p *Phone) Answer(ansCtx context.Context, opts AnswerOptions) (*DialogServerSession, error)

Answer will answer call Closing ansCtx will close listeners or it will be closed on BYE TODO: reusing listener

func (*Phone) AnswerWithCode added in v0.3.0

func (p *Phone) AnswerWithCode(ansCtx context.Context, code sip.StatusCode, reason string, opts AnswerOptions) (*DialogServerSession, error)

AnswerWithCode will answer with custom code Dialog object is created but it is immediately closed Deprecated: Use Answer with options

func (*Phone) Close

func (p *Phone) Close()

func (*Phone) Dial

func (p *Phone) Dial(dialCtx context.Context, recipient sip.Uri, o DialOptions) (*DialogClientSession, error)

Dial creates dialog with recipient

return DialResponseError in case non 200 responses

func (*Phone) Register

func (p *Phone) Register(ctx context.Context, recipient sip.Uri, opts RegisterOptions) error

type PhoneOption

type PhoneOption func(p *Phone)

func WithPhoneListenAddr

func WithPhoneListenAddr(addr ListenAddr) PhoneOption

WithPhoneListenAddrs NOT TLS supported

func WithPhoneLogger

func WithPhoneLogger(l zerolog.Logger) PhoneOption

type RegisterOptions added in v0.5.0

type RegisterOptions struct {
	Username string
	Password string

	Expiry        int
	AllowHeaders  []string
	UnregisterAll bool
}

Register the phone by sip uri. Pass username and password via opts NOTE: this will block and keep periodic registration. Use context to cancel

type RegisterResponseError added in v0.5.0

type RegisterResponseError struct {
	RegisterReq *sip.Request
	RegisterRes *sip.Response

	Msg string
}

func (RegisterResponseError) Error added in v0.5.0

func (e RegisterResponseError) Error() string

func (*RegisterResponseError) StatusCode added in v0.5.0

func (e *RegisterResponseError) StatusCode() sip.StatusCode

type RegisterTransaction added in v0.5.0

type RegisterTransaction struct {
	Origin *sip.Request
	// contains filtered or unexported fields
}

func NewRegisterTransaction added in v0.6.0

func NewRegisterTransaction(log zerolog.Logger, client *sipgo.Client, recipient sip.Uri, contact sip.ContactHeader, opts RegisterOptions) *RegisterTransaction

func (*RegisterTransaction) QualifyLoop added in v0.6.0

func (t *RegisterTransaction) QualifyLoop(ctx context.Context) error

func (*RegisterTransaction) Register added in v0.6.0

func (p *RegisterTransaction) Register(ctx context.Context) error

func (*RegisterTransaction) Terminate added in v0.5.0

func (t *RegisterTransaction) Terminate() error

func (*RegisterTransaction) Unregister added in v0.6.0

func (t *RegisterTransaction) Unregister(ctx context.Context) error

Directories

Path Synopsis
echome
echouac command
echouas command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL