core

package
v0.0.0-...-769c010 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: MIT Imports: 18 Imported by: 0

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

func Atoi(s string) (i int)

func Before

func Before(s, sep string) string

func Between

func Between(s, sub1, sub2 string) string

func Caller

func Caller() string

func DecodeH264

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

func FFmpegCodecName

func FFmpegCodecName(name string) string

func GetKind

func GetKind(name string) string

func ID deprecated

func ID(v any) uint32

Deprecated: use NewID instead

func MarshalSDP

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

func MoveNode

func MoveNode(dst, src *Node)

func NewID

func NewID() uint32

func Now90000

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

func (c *Codec) IsAudio() bool

func (*Codec) IsRTP

func (c *Codec) IsRTP() bool

func (*Codec) IsVideo

func (c *Codec) IsVideo() bool

func (*Codec) Kind

func (c *Codec) Kind() string

func (*Codec) MarshalJSON

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

MarshalJSON - return FFprobe compatible output

func (*Codec) Match

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

func (*Codec) PrintName

func (c *Codec) PrintName() string

func (*Codec) String

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

type Connection

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

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

Create like os.Create, init Consumer with existing Transport

func Dial

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

Dial like net.Dial, init Producer via Dialing

func Open

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

Open like os.Open, init Producer from existing Transport

func (*Connection) Codecs deprecated

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

Deprecated:

func (*Connection) GetMedias

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

func (*Connection) GetTrack

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

func (*Connection) SetProtocol

func (c *Connection) SetProtocol(s string)

func (*Connection) SetRemoteAddr

func (c *Connection) SetRemoteAddr(s string)

func (*Connection) SetSource

func (c *Connection) SetSource(s string)

func (*Connection) SetURL

func (c *Connection) SetURL(s string)

func (*Connection) Stop

func (c *Connection) Stop() error

func (*Connection) WithRequest

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

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

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

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

func (Mode) String

func (m Mode) String() string

type Node

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

Node - Receiver or Sender or Filter (transform)

func (*Node) AppendChild

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

func (*Node) Close

func (n *Node) Close()

func (*Node) RemoveChild

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

func (*Node) WithParent

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

type OnceBuffer

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

OnceBuffer will catch only first message

func (*OnceBuffer) Buffer

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

func (*OnceBuffer) Len

func (o *OnceBuffer) Len() int

func (*OnceBuffer) Write

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

func (*OnceBuffer) WriteTo

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

type Packet

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

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

func NewReadBuffer(rd io.Reader) *ReadBuffer

func (*ReadBuffer) Close

func (r *ReadBuffer) Close() error

func (*ReadBuffer) Peek

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

func (*ReadBuffer) Read

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

func (*ReadBuffer) Reset

func (r *ReadBuffer) Reset()

func (*ReadBuffer) Seek

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

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

func (s *Sender) Start()

func (*Sender) State

func (s *Sender) State() string

func (*Sender) Wait

func (s *Sender) Wait()

func (*Sender) WithParent

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

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

func NewWriteBuffer(wr io.Writer) *WriteBuffer

func (*WriteBuffer) Close

func (w *WriteBuffer) Close() error

func (*WriteBuffer) Reset

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

func (*WriteBuffer) Write

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

func (*WriteBuffer) WriteTo

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