udp

package
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: ISC Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDecryptionFailed = errors.New("decryption failed")

ErrDecryptionFailed is returned from ReadPacket if the received packet fails to decrypt.

View Source
var ErrDialWhileUnpaused = errors.New("dial is called while manager is not paused")

ErrDialWhileUnpaused is returned if Dial is called on the Manager without pausing it first

View Source
var ErrManagerClosed = errors.New("UDP connection manager is closed")

ErrManagerClosed is returned when a Manager that is already closed is dialed, written to or read from.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	GatewayIP   string
	GatewayPort uint16
	// contains filtered or unexported fields
}

Connection represents a voice connection. It is not thread-safe.

func DialConnection

func DialConnection(ctx context.Context, addr string, ssrc uint32) (*Connection, error)

DialConnection dials the UDP connection using the given address and SSRC number.

func DialConnectionCustom

func DialConnectionCustom(
	ctx context.Context, dialer *net.Dialer, addr string, ssrc uint32) (*Connection, error)

DialConnectionCustom dials the UDP connection with a custom dialer.

func (*Connection) Close

func (c *Connection) Close() error

Close closes the connection.

func (*Connection) ReadPacket

func (c *Connection) ReadPacket() (*Packet, error)

ReadPacket reads the UDP connection and returns a packet if successful. The returned packet is invalidated once ReadPacket is called again. To avoid this, manually Copy the packet.

func (*Connection) ResetFrequency

func (c *Connection) ResetFrequency(frameDuration time.Duration, timeIncr uint32)

ResetFrequency resets the internal frequency ticker as well as the timestamp incremental number. For more information, refer to https://tools.ietf.org/html/rfc7587#section-4.2.

frameDuration controls the Opus frame duration used by the UDP connection to control the frequency of packets sent over. 20ms is the default by libopus.

timestampIncr is the timestamp to increment for each Opus packet. This should be consistent with th given frameDuration. For the right combination, refer to the Valid Parameters section below.

Valid Parameters

The following table lists the recommended parameters for these variables.

+---------+-----+-----+------+------+
|   Mode  |  10 |  20 |  40  |  60  |
+---------+-----+-----+------+------+
| ts incr | 480 | 960 | 1920 | 2880 |
+---------+-----+-----+------+------+

Note that audio mode is omitted, as it is not recommended. For the full table, refer to the IETF RFC7587 section 4.2 link above.

func (*Connection) SetReadDeadline

func (c *Connection) SetReadDeadline(deadline time.Time)

SetReadDeadline sets the UDP connection's read deadline.

func (*Connection) SetWriteDeadline

func (c *Connection) SetWriteDeadline(deadline time.Time)

SetWriteDeadline sets the UDP connection's write deadline.

func (*Connection) UseSecret

func (c *Connection) UseSecret(secret [32]byte)

UseSecret uses the given secret. This method is not thread-safe, so it should only be used right after initialization.

func (*Connection) Write

func (c *Connection) Write(b []byte) (int, error)

Write sends a packet of audio into the voice UDP connection. It is made to be stream-compatible: the internal frequency clock will slow Write down to match the real playback time.

type DialFunc

type DialFunc = func(ctx context.Context, addr string, ssrc uint32) (*Connection, error)

DialFunc is the UDP dialer function type. It's the function signature for udp.DialConnection.

func DialFuncWithFrequency

func DialFuncWithFrequency(frameDuration time.Duration, timeIncr uint32) DialFunc

DialFuncWithFrequency creates a new DialFunc with the given frame duration and time increment. See Connection's ResetFrequency method for more information.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages a UDP connection. It allows reconnecting. A Manager instance is thread-safe, meaning it can be used concurrently.

func NewManager

func NewManager() *Manager

NewManager creates a new UDP connection manager with the default dial function DialConnection.

func NewManagerWithDialer

func NewManagerWithDialer(dialer DialFunc) *Manager

NewManagerWithDialer creates a UDP manager with an existing dial function.

func (*Manager) Close

func (m *Manager) Close() (err error)

Close closes the current connection. If the connection is already closed, then nothing is done and ErrManagerClosed is returned. Close does not pause the connection; calls to Close while the user is using the connection will result in the user getting ErrManagerClosed.

func (*Manager) Continue

func (m *Manager) Continue() bool

Continue unpauses and resumes the active user. If the manager has been successfully resumed, then true is returned, otherwise if it's already continued, then false is returned.

func (*Manager) Dial

func (m *Manager) Dial(ctx context.Context, addr string, ssrc uint32) (*Connection, error)

Dial dials the internal connection to the given address and SSRC number. If the Manager is not Paused, then an error is returned. The caller must call Dial after Pause and before Unpause. If the Manager is already being dialed elsewhere, then ErrAlreadyDialing is returned.

func (*Manager) IsClosed

func (m *Manager) IsClosed() bool

IsClosed returns true if the connection is closed.

func (*Manager) Pause

func (m *Manager) Pause(ctx context.Context) error

Pause explicitly pauses the manager. It blocks until the Manager is paused or the context expires.

func (*Manager) ReadPacket

func (m *Manager) ReadPacket() (p *Packet, err error)

ReadPacket reads the current packet. It blocks until a packet arrives or the Manager is closed.

func (*Manager) ResetFrequency

func (m *Manager) ResetFrequency(frameDuration time.Duration, timeIncr uint32)

ResetFrequency sets the current connection and future connections' write frequency. Note that calling this method while Connection is being used in a different goroutine is not thread-safe.

func (*Manager) SetDialer

func (m *Manager) SetDialer(d DialFunc)

SetDialer sets the manager's dialer. Calling this function while the Manager is working will cause a panic. Only call this method directly after construction.

func (*Manager) Write

func (m *Manager) Write(b []byte) (n int, err error)

Write writes to the current connection in the manager. It blocks if the connection is being re-established.

type Packet

type Packet struct {
	Opus []byte
	// contains filtered or unexported fields
}

Packet represents a voice packet.

func (*Packet) Copy

func (p *Packet) Copy(dst *Packet)

Copy copies the current packet into the given packet.

func (*Packet) SSRC

func (p *Packet) SSRC() uint32

SSRC returns the packet's SSRC number.

func (*Packet) Sequence

func (p *Packet) Sequence() uint16

Sequence returns the packet sequence.

func (*Packet) Timestamp

func (p *Packet) Timestamp() uint32

Timestamp returns the packet's timestamp.

func (*Packet) Type

func (p *Packet) Type() byte

Type returns the packet type.

func (*Packet) VersionFlags

func (p *Packet) VersionFlags() byte

VersionFlags returns the version flags of the current packet.

Jump to

Keyboard shortcuts

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