core

package
v1.9.6 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 18 Imported by: 3

README

PCM

RTSP

  • PayloadType=10 - L16/44100/2 - Linear PCM 16-bit big endian
  • PayloadType=11 - L16/44100/1 - Linear PCM 16-bit big endian

https://en.wikipedia.org/wiki/RTP_payload_formats

Apple QuickTime

  • raw - 16-bit data is stored in little endian format
  • twos - 16-bit data is stored in big endian format
  • sowt - 16-bit data is stored in little endian format
  • in24 - denotes 24-bit, big endian
  • in32 - denotes 32-bit, big endian
  • fl32 - denotes 32-bit floating point PCM
  • fl64 - denotes 64-bit floating point PCM
  • alaw - denotes A-law logarithmic PCM
  • ulaw - denotes mu-law logarithmic PCM

https://wiki.multimedia.cx/index.php/PCM

FFmpeg RTSP

pcm_s16be, 44100 Hz, stereo => 10
pcm_s16be, 48000 Hz, stereo => 96 L16/48000/2
pcm_s16be, 44100 Hz, mono   => 11

pcm_s16le, 48000 Hz, stereo => 96 (b=AS:1536)
pcm_s16le, 44100 Hz, stereo => 96 (b=AS:1411)
pcm_s16le, 16000 Hz, stereo => 96 (b=AS:512)
pcm_s16le, 8000 Hz, stereo  => 96 (b=AS:256)

pcm_s16le, 48000 Hz, mono   => 96 (b=AS:768)
pcm_s16le, 44100 Hz, mono   => 96 (b=AS:705)
pcm_s16le, 16000 Hz, mono   => 96 (b=AS:256)
pcm_s16le, 8000 Hz, mono    => 96 (b=AS:128)

Documentation

Index

Constants

View Source
const (
	DirectionRecvonly = "recvonly"
	DirectionSendonly = "sendonly"
	DirectionSendRecv = "sendrecv"
)
View Source
const (
	KindVideo = "video"
	KindAudio = "audio"
)
View Source
const (
	CodecH264 = "H264" // payloadType: 96
	CodecH265 = "H265"
	CodecVP8  = "VP8"
	CodecVP9  = "VP9"
	CodecAV1  = "AV1"
	CodecJPEG = "JPEG" // payloadType: 26
	CodecRAW  = "RAW"

	CodecPCMU = "PCMU" // payloadType: 0
	CodecPCMA = "PCMA" // payloadType: 8
	CodecAAC  = "MPEG4-GENERIC"
	CodecOpus = "OPUS" // payloadType: 111
	CodecG722 = "G722"
	CodecMP3  = "MPA" // payload: 14, aka MPEG-1 Layer III
	CodecPCM  = "L16" // Linear PCM (big endian)

	CodecPCML = "PCML" // Linear PCM (little endian)

	CodecELD  = "ELD" // AAC-ELD
	CodecFLAC = "FLAC"

	CodecAll = "ALL"
	CodecAny = "ANY"
)
View Source
const (
	BufferSize      = 64 * 1024 // 64K
	ConnDialTimeout = time.Second * 3
	ConnDeadline    = time.Second * 5
	ProbeTimeout    = time.Second * 3
)
View Source
const (
	BufferDisable       = 0
	BufferDrainAndClear = -1
)
View Source
const PayloadTypeRAW byte = 255
View Source
const ProbeSize = 5 * 1024 * 1024 // 5MB

ProbeSize in my tests MPEG-TS 40Mbit/s 4K-video require more than 1MB for probe

Variables

View Source
var ErrCantGetTrack = errors.New("can't get track")

Functions

func Assert

func Assert(ok bool)

func Atoi added in v1.5.0

func Atoi(s string) (i int)

func Before added in v1.9.3

func Before(s, sep string) string

func Between

func Between(s, sub1, sub2 string) string

func Caller

func Caller() string

func Contains added in v1.9.5

func Contains[S ~[]E, E comparable](s S, v E) bool

Contains reports whether v is present in s.

func DecodeH264

func DecodeH264(fmtp string) (profile string, level byte)

func FFmpegCodecName added in v1.9.3

func FFmpegCodecName(name string) string

func GetKind

func GetKind(name string) string

func ID deprecated added in v1.9.4

func ID(v any) uint32

Deprecated: use NewID instead

func Index added in v1.9.5

func Index[S ~[]E, E comparable](s S, v E) int

Index returns the index of the first occurrence of v in s, or -1 if not present.

func MarshalSDP

func MarshalSDP(name string, medias []*Media) ([]byte, error)

func Max added in v1.9.5

func Max[S ~[]E, E Ordered](x S) E

Max returns the maximal value in x. It panics if x is empty. For floating-point E, Max propagates NaNs (any NaN value in x forces the output to be NaN).

func MoveNode added in v1.9.3

func MoveNode(dst, src *Node)

func NewID added in v1.9.4

func NewID() uint32

func Now90000 added in v1.5.0

func Now90000() uint32

Now90000 - timestamp for Video (clock rate = 90000 samples per second)

func RandString

func RandString(size, base byte) string

RandString base10 - numbers, base16 - hex, base36 - digits+letters base64 - URL safe symbols, base0 - crypto random

Types

type Codec

type Codec struct {
	Name        string // H264, PCMU, PCMA, opus...
	ClockRate   uint32 // 90000, 8000, 16000...
	Channels    uint16 // 0, 1, 2
	FmtpLine    string
	PayloadType uint8
}

func UnmarshalCodec

func UnmarshalCodec(md *sdp.MediaDescription, payloadType string) *Codec

func (*Codec) Clone

func (c *Codec) Clone() *Codec

func (*Codec) IsAudio added in v1.7.0

func (c *Codec) IsAudio() bool

func (*Codec) IsRTP

func (c *Codec) IsRTP() bool

func (*Codec) IsVideo added in v1.7.0

func (c *Codec) IsVideo() bool

func (*Codec) Kind added in v1.7.0

func (c *Codec) Kind() string

func (*Codec) MarshalJSON added in v1.9.3

func (c *Codec) MarshalJSON() ([]byte, error)

MarshalJSON - return FFprobe compatible output

func (*Codec) Match

func (c *Codec) Match(remote *Codec) bool

func (*Codec) PrintName added in v1.7.0

func (c *Codec) PrintName() string

func (*Codec) String

func (c *Codec) String() (s string)

type Connection added in v1.9.4

type Connection struct {
	ID         uint32 `json:"id,omitempty"`
	FormatName string `json:"format_name,omitempty"` // rtsp, webrtc, mp4, mjpeg, mpjpeg...
	Protocol   string `json:"protocol,omitempty"`    // tcp, udp, http, ws, pipe...
	RemoteAddr string `json:"remote_addr,omitempty"` // host:port other info
	Source     string `json:"source,omitempty"`
	URL        string `json:"url,omitempty"`
	SDP        string `json:"sdp,omitempty"`
	UserAgent  string `json:"user_agent,omitempty"`

	Medias    []*Media    `json:"medias,omitempty"`
	Receivers []*Receiver `json:"receivers,omitempty"`
	Senders   []*Sender   `json:"senders,omitempty"`
	Recv      int         `json:"bytes_recv,omitempty"`
	Send      int         `json:"bytes_send,omitempty"`

	Transport any `json:"-"`
}

Connection just like webrtc.PeerConnection - ID and RemoteAddr used for building Connection(s) graph - FormatName, Protocol, RemoteAddr, Source, URL, SDP, UserAgent used for info about Connection - FormatName and Protocol has FFmpeg compatible names - Transport used for auto closing on Stop

func Create added in v1.9.4

func Create(w io.Writer) (*Connection, error)

Create like os.Create, init Consumer with existing Transport

func Dial added in v1.9.4

func Dial(rawURL string) (*Connection, error)

Dial like net.Dial, init Producer via Dialing

func Open added in v1.9.4

func Open(r io.Reader) (*Connection, error)

Open like os.Open, init Producer from existing Transport

func (*Connection) Codecs deprecated added in v1.9.4

func (c *Connection) Codecs() []*Codec

Deprecated:

func (*Connection) GetMedias added in v1.9.4

func (c *Connection) GetMedias() []*Media

func (*Connection) GetTrack added in v1.9.4

func (c *Connection) GetTrack(media *Media, codec *Codec) (*Receiver, error)

func (*Connection) SetProtocol added in v1.9.4

func (c *Connection) SetProtocol(s string)

func (*Connection) SetRemoteAddr added in v1.9.4

func (c *Connection) SetRemoteAddr(s string)

func (*Connection) SetSource added in v1.9.4

func (c *Connection) SetSource(s string)

func (*Connection) SetURL added in v1.9.4

func (c *Connection) SetURL(s string)

func (*Connection) Stop added in v1.9.4

func (c *Connection) Stop() error

func (*Connection) WithRequest added in v1.9.4

func (c *Connection) WithRequest(r *http.Request)

type Consumer

type Consumer interface {
	// GetMedias - return Media(s) with local Media.Direction:
	// - sendonly for Consumer Video/Audio
	// - recvonly for Consumer backchannel
	GetMedias() []*Media

	AddTrack(media *Media, codec *Codec, track *Receiver) error

	// Deprecated: rename to Close()
	Stop() error
}

type EventFunc

type EventFunc func(msg any)

type Filter added in v1.9.3

type Filter func(handler HandlerFunc) HandlerFunc

Filter - a decorator for any HandlerFunc

type HandlerFunc

type HandlerFunc func(packet *Packet)

HandlerFunc - process input packets (just like http.HandlerFunc)

type Info

type Info interface {
	SetProtocol(string)
	SetRemoteAddr(string)
	SetSource(string)
	SetURL(string)
	WithRequest(*http.Request)
}

type Listener

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

Listener base struct for all classes with support feedback

func (*Listener) Fire

func (l *Listener) Fire(msg any)

func (*Listener) Listen

func (l *Listener) Listen(f EventFunc)

type Media

type Media struct {
	Kind      string   `json:"kind,omitempty"`      // video or audio
	Direction string   `json:"direction,omitempty"` // sendonly, recvonly
	Codecs    []*Codec `json:"codecs,omitempty"`

	ID string `json:"id,omitempty"` // MID for WebRTC, Control for RTSP
}

Media take best from: - deepch/vdk/format/rtsp/sdp.Media - pion/sdp.MediaDescription

func ParseQuery

func ParseQuery(query map[string][]string) (medias []*Media)

func UnmarshalMedia

func UnmarshalMedia(md *sdp.MediaDescription) *Media

func (*Media) Clone

func (m *Media) Clone() *Media

func (*Media) Equal added in v1.3.2

func (m *Media) Equal(media *Media) bool

func (*Media) MarshalJSON

func (m *Media) MarshalJSON() ([]byte, error)

func (*Media) MatchAll

func (m *Media) MatchAll() bool

func (*Media) MatchCodec

func (m *Media) MatchCodec(remote *Codec) *Codec

func (*Media) MatchMedia

func (m *Media) MatchMedia(remote *Media) (codec, remoteCodec *Codec)

func (*Media) String

func (m *Media) String() string

type Mode

type Mode byte
const (
	ModeActiveProducer Mode = iota + 1 // typical source (client)
	ModePassiveConsumer
	ModePassiveProducer
	ModeActiveConsumer
)

func (Mode) MarshalJSON added in v1.9.4

func (m Mode) MarshalJSON() ([]byte, error)

func (Mode) String

func (m Mode) String() string

type Node added in v1.9.3

type Node struct {
	Codec  *Codec
	Input  HandlerFunc
	Output HandlerFunc
	// contains filtered or unexported fields
}

Node - Receiver or Sender or Filter (transform)

func (*Node) AppendChild added in v1.9.3

func (n *Node) AppendChild(child *Node)

func (*Node) Close added in v1.9.3

func (n *Node) Close()

func (*Node) RemoveChild added in v1.9.3

func (n *Node) RemoveChild(child *Node)

func (*Node) WithParent added in v1.9.3

func (n *Node) WithParent(parent *Node) *Node

type OnceBuffer added in v1.7.0

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

OnceBuffer will catch only first message

func (*OnceBuffer) Buffer added in v1.7.0

func (o *OnceBuffer) Buffer() []byte

func (*OnceBuffer) Len added in v1.7.0

func (o *OnceBuffer) Len() int

func (*OnceBuffer) Write added in v1.7.0

func (o *OnceBuffer) Write(p []byte) (n int, err error)

func (*OnceBuffer) WriteTo added in v1.7.0

func (o *OnceBuffer) WriteTo(w io.Writer) (n int64, err error)

type Ordered added in v1.9.5

type Ordered interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64 |
		~string
}

type Packet added in v1.7.0

type Packet = rtp.Packet

type Producer

type Producer interface {
	// GetMedias - return Media(s) with local Media.Direction:
	// - recvonly for Producer Video/Audio
	// - sendonly for Producer backchannel
	GetMedias() []*Media

	// GetTrack - return Receiver, that can only produce rtp.Packet(s)
	GetTrack(media *Media, codec *Codec) (*Receiver, error)

	// Deprecated: rename to Run()
	Start() error

	// Deprecated: rename to Close()
	Stop() error
}

type ReadBuffer added in v1.7.0

type ReadBuffer struct {
	io.Reader

	BufferSize int
	// contains filtered or unexported fields
}

ReadBuffer support buffering and Seek over buffer positive BufferSize will enable buffering mode Seek to negative offset will clear buffer Seek with a positive BufferSize will continue buffering after the last read from the buffer Seek with a negative BufferSize will clear buffer after the last read from the buffer Read more than BufferSize will raise error

func NewReadBuffer added in v1.7.0

func NewReadBuffer(rd io.Reader) *ReadBuffer

func (*ReadBuffer) Close added in v1.7.0

func (r *ReadBuffer) Close() error

func (*ReadBuffer) Peek added in v1.7.0

func (r *ReadBuffer) Peek(n int) ([]byte, error)

func (*ReadBuffer) Read added in v1.7.0

func (r *ReadBuffer) Read(p []byte) (n int, err error)

func (*ReadBuffer) Reset added in v1.7.0

func (r *ReadBuffer) Reset()

func (*ReadBuffer) Seek added in v1.7.0

func (r *ReadBuffer) Seek(offset int64, whence int) (int64, error)

type Receiver

type Receiver struct {
	Node

	// Deprecated: should be removed
	Media *Media `json:"-"`
	// Deprecated: should be removed
	ID byte `json:"-"` // Channel for RTSP, PayloadType for MPEG-TS

	Bytes   int `json:"bytes,omitempty"`
	Packets int `json:"packets,omitempty"`
}

func NewReceiver

func NewReceiver(media *Media, codec *Codec) *Receiver

func (*Receiver) Close

func (r *Receiver) Close()

func (*Receiver) MarshalJSON

func (r *Receiver) MarshalJSON() ([]byte, error)

func (*Receiver) Replace deprecated

func (r *Receiver) Replace(target *Receiver)

Deprecated: should be removed

func (*Receiver) Senders deprecated

func (r *Receiver) Senders() []*Sender

Deprecated: should be removed

func (*Receiver) WriteRTP deprecated

func (r *Receiver) WriteRTP(packet *rtp.Packet)

Deprecated: should be removed

type Sender

type Sender struct {
	Node

	// Deprecated:
	Media *Media `json:"-"`
	// Deprecated:
	Handler HandlerFunc `json:"-"`

	Bytes   int `json:"bytes,omitempty"`
	Packets int `json:"packets,omitempty"`
	Drops   int `json:"drops,omitempty"`
	// contains filtered or unexported fields
}

func NewSender

func NewSender(media *Media, codec *Codec) *Sender

func (*Sender) Bind deprecated added in v1.9.2

func (s *Sender) Bind(parent *Receiver)

Deprecated: should be removed

func (*Sender) Close

func (s *Sender) Close()

func (*Sender) HandleRTP deprecated

func (s *Sender) HandleRTP(parent *Receiver)

Deprecated: should be removed

func (*Sender) MarshalJSON

func (s *Sender) MarshalJSON() ([]byte, error)

func (*Sender) Start added in v1.9.2

func (s *Sender) Start()

func (*Sender) State added in v1.9.3

func (s *Sender) State() string

func (*Sender) Wait added in v1.9.3

func (s *Sender) Wait()

func (*Sender) WithParent added in v1.9.3

func (s *Sender) WithParent(parent *Receiver) *Sender

type Waiter

type Waiter struct {
	sync.WaitGroup
	// contains filtered or unexported fields
}

Waiter support: - autotart on first Wait - block new waiters after last Done - safe Done after finish

func (*Waiter) Add

func (w *Waiter) Add(delta int)

func (*Waiter) Done

func (w *Waiter) Done(err error)

func (*Waiter) Wait

func (w *Waiter) Wait() error

func (*Waiter) WaitChan

func (w *Waiter) WaitChan() <-chan error

type Worker

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

func NewWorker

func NewWorker(d time.Duration, f func() time.Duration) *Worker

NewWorker run f after d

func (*Worker) Do

func (w *Worker) Do()

Do - instant timer run

func (*Worker) Stop

func (w *Worker) Stop()

type WriteBuffer added in v1.7.0

type WriteBuffer struct {
	io.Writer
	// contains filtered or unexported fields
}

WriteBuffer by defaul Write(s) to bytes.Buffer. But after WriteTo to new io.Writer - calls Reset. Reset will flush current buffer data to new writer and starts to Write to new io.Writer WriteTo will be locked until Write fails or Close will be called.

func NewWriteBuffer added in v1.7.0

func NewWriteBuffer(wr io.Writer) *WriteBuffer

func (*WriteBuffer) Close added in v1.7.0

func (w *WriteBuffer) Close() error

func (*WriteBuffer) Reset added in v1.7.0

func (w *WriteBuffer) Reset(wr io.Writer)

func (*WriteBuffer) Write added in v1.7.0

func (w *WriteBuffer) Write(p []byte) (n int, err error)

func (*WriteBuffer) WriteTo added in v1.7.0

func (w *WriteBuffer) WriteTo(wr io.Writer) (n int64, err error)

Jump to

Keyboard shortcuts

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