av

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package av defines basic interfaces and data structures of container demux/mux and audio encode/decode.

Index

Constants

View Source
const (
	U8   = SampleFormat(iota + 1) // 8-bit unsigned integer
	S16                           // signed 16-bit integer
	S32                           // signed 32-bit integer
	FLT                           // 32-bit float
	DBL                           // 64-bit float
	U8P                           // 8-bit unsigned integer in planar
	S16P                          // signed 16-bit integer in planar
	S32P                          // signed 32-bit integer in planar
	FLTP                          // 32-bit float in planar
	DBLP                          // 64-bit float in planar
	U32                           // unsigned 32-bit integer
)
View Source
const (
	CH_FRONT_CENTER = ChannelLayout(1 << iota)
	CH_FRONT_LEFT
	CH_FRONT_RIGHT
	CH_BACK_CENTER
	CH_BACK_LEFT
	CH_BACK_RIGHT
	CH_SIDE_LEFT
	CH_SIDE_RIGHT
	CH_LOW_FREQ
	CH_NR

	CH_MONO     = ChannelLayout(CH_FRONT_CENTER)
	CH_STEREO   = ChannelLayout(CH_FRONT_LEFT | CH_FRONT_RIGHT)
	CH_2_1      = ChannelLayout(CH_STEREO | CH_BACK_CENTER)
	CH_2POINT1  = ChannelLayout(CH_STEREO | CH_LOW_FREQ)
	CH_SURROUND = ChannelLayout(CH_STEREO | CH_FRONT_CENTER)
	CH_3POINT1  = ChannelLayout(CH_SURROUND | CH_LOW_FREQ)

	FLV_TAG_AUDIO      = 8
	FLV_TAG_VIDEO      = 9
	FLV_TAG_SCRIPTDATA = 18

	AVC_SEQHDR = 0
	AVC_NALU   = 1
	AVC_EOS    = 2

	FRAME_KEY   = 1
	FRAME_INTER = 2

	VIDEO_H264 = 7
)

Variables

View Source
var (
	H264       = MakeVideoCodecType(avCodecTypeMagic + 1)
	H265       = MakeVideoCodecType(avCodecTypeMagic + 2)
	JPEG       = MakeVideoCodecType(avCodecTypeMagic + 3)
	VP8        = MakeVideoCodecType(avCodecTypeMagic + 4)
	VP9        = MakeVideoCodecType(avCodecTypeMagic + 5)
	AV1        = MakeVideoCodecType(avCodecTypeMagic + 6)
	MJPEG      = MakeVideoCodecType(avCodecTypeMagic + 7)
	AAC        = MakeAudioCodecType(avCodecTypeMagic + 1)
	PCM_MULAW  = MakeAudioCodecType(avCodecTypeMagic + 2)
	PCM_ALAW   = MakeAudioCodecType(avCodecTypeMagic + 3)
	SPEEX      = MakeAudioCodecType(avCodecTypeMagic + 4)
	NELLYMOSER = MakeAudioCodecType(avCodecTypeMagic + 5)
	PCM        = MakeAudioCodecType(avCodecTypeMagic + 6)
	OPUS       = MakeAudioCodecType(avCodecTypeMagic + 7)
)
View Source
var (
	HeaderTypeH264 = makeVideoHeaderType(headerTypeBase + 1)
	HeaderTypeAAC  = makeAudioHeaderType(headerTypeBase + 1)
)

header类型

Functions

This section is empty.

Types

type AudioCodecData

type AudioCodecData interface {
	CodecData
	SampleFormat() SampleFormat                   // audio sample format
	SampleRate() int                              // audio sample rate
	ChannelLayout() ChannelLayout                 // audio channel layout
	PacketDuration([]byte) (time.Duration, error) // get audio compressed packet duration
}

type AudioDecoder

type AudioDecoder interface {
	Decode([]byte) (bool, AudioFrame, error) // decode one compressed audio packet
	Close()                                  // close decode, free cgo contexts
}

AudioDecoder can decode compressed audio packets into raw audio frame. use ffmpeg.NewAudioDecoder to create it.

type AudioEncoder

type AudioEncoder interface {
	CodecData() (AudioCodecData, error)   // encoder's codec data can put into container
	Encode(AudioFrame) ([][]byte, error)  // encode raw audio frame into compressed pakcet(s)
	Close()                               // close encoder, free cgo contexts
	SetSampleRate(int) error              // set encoder sample rate
	SetChannelLayout(ChannelLayout) error // set encoder channel layout
	SetSampleFormat(SampleFormat) error   // set encoder sample format
	SetBitrate(int) error                 // set encoder bitrate
	SetOption(string, interface{}) error  // encoder setopt, in ffmpeg is av_opt_set_dict()
	GetOption(string, interface{}) error  // encoder getopt
}

AudioEncoder can encode raw audio frame into compressed audio packets. cgo/ffmpeg inplements AudioEncoder, using ffmpeg.NewAudioEncoder to create it.

type AudioFrame

type AudioFrame struct {
	SampleFormat  SampleFormat  // audio sample format, e.g: S16,FLTP,...
	ChannelLayout ChannelLayout // audio channel layout, e.g: CH_MONO,CH_STEREO,...
	SampleCount   int           // sample count in this frame
	SampleRate    int           // sample rate
	Data          [][]byte      // data array for planar format len(Data) > 1
}

Raw audio frame.

func (AudioFrame) Concat

func (self AudioFrame) Concat(in AudioFrame) (out AudioFrame)

Concat two audio frames.

func (AudioFrame) Duration

func (self AudioFrame) Duration() time.Duration

func (AudioFrame) HasSameFormat

func (self AudioFrame) HasSameFormat(other AudioFrame) bool

Check this audio frame has same format as other audio frame.

func (AudioFrame) Slice

func (self AudioFrame) Slice(start int, end int) (out AudioFrame)

Split sample audio sample from this frame.

type AudioResampler

type AudioResampler interface {
	Resample(AudioFrame) (AudioFrame, error) // convert raw audio frames
}

AudioResampler can convert raw audio frames in different sample rate/format/channel layout.

type ChannelLayout

type ChannelLayout uint16

Audio channel layout.

func (ChannelLayout) Count

func (self ChannelLayout) Count() (n int)

func (ChannelLayout) String

func (self ChannelLayout) String() string

type CodecData

type CodecData interface {
	Type() CodecType // Video/Audio codec type
}

CodecData is some important bytes for initializing audio/video decoder, can be converted to VideoCodecData or AudioCodecData using:

codecdata.(AudioCodecData) or codecdata.(VideoCodecData)

for H264, CodecData is AVCDecoderConfigure bytes, includes SPS/PPS.

type CodecType

type CodecType uint32

Video/Audio codec type. can be H264/AAC/SPEEX/...

func MakeAudioCodecType

func MakeAudioCodecType(base uint32) (c CodecType)

Make a new audio codec type.

func MakeVideoCodecType

func MakeVideoCodecType(base uint32) (c CodecType)

Make a new video codec type.

func (CodecType) IsAudio

func (self CodecType) IsAudio() bool

func (CodecType) IsVideo

func (self CodecType) IsVideo() bool

func (CodecType) String

func (self CodecType) String() string

type DemuxCloser

type DemuxCloser interface {
	Demuxer
	Close() error
}

Demuxer with Close() method

type Demuxer

type Demuxer interface {
	PacketReader                   // read compressed audio/video packets
	Streams() ([]CodecData, error) // reads the file header, contains video/audio meta infomations
}

Demuxer can read compressed audio/video packets from container formats like MP4/FLV/MPEG-TS.

type Header struct {
	Type HeaderType // Video/Audio header type
	Data interface{}
}

Header header数据

type HeaderType

type HeaderType uint32

HeaderType header类型

func (HeaderType) IsAudio

func (ht HeaderType) IsAudio() bool

IsAudio 判断header是否为音频header

func (HeaderType) IsVideo

func (ht HeaderType) IsVideo() bool

IsVideo 判断header是否为视频header

type MuxCloser

type MuxCloser interface {
	Muxer
	Close() error
}

Muxer with Close() method

type Muxer

type Muxer interface {
	WriteHeader([]CodecData) error // write the file header
	PacketWriter                   // write compressed audio/video packets
	WriteTrailer() error           // finish writing file, this func can be called only once
}

Muxer describes the steps of writing compressed audio/video packets into container formats like MP4/FLV/MPEG-TS.

Container formats, rtmp.Conn, and transcode.Muxer implements Muxer interface.

type Option

type Option func(*Options)

func WithAfterReadHeaders

func WithAfterReadHeaders(f func([]CodecData) error) Option

func WithAfterReadPacket

func WithAfterReadPacket(f func(*Packet) error) Option

func WithAfterWriteHeaders

func WithAfterWriteHeaders(f func([]CodecData) error) Option

func WithAfterWritePacket

func WithAfterWritePacket(f func(*Packet) error) Option

func WithConnectedTimestamp

func WithConnectedTimestamp(t time.Time) Option

WithConnectedTimestamp 设置Options的ConnectedTimestamp选项

func WithHandlerName

func WithHandlerName(name string) Option

WithHandlerName 设置Options的HandlerName选项

func WithSID

func WithSID(id string) Option

WithSID 设置Options的sid选项

type Options

type Options struct {
	SID                string
	HandlerName        string
	ConnectedTimestamp time.Time
	AfterReadPacket    func(*Packet) error
	AfterWritePacket   func(*Packet) error
	AfterReadHeaders   func([]CodecData) error
	AfterWriteHeaders  func([]CodecData) error
}

type Packet

type Packet struct {
	IsKeyFrame      bool          // video packet is key frame
	Idx             int8          // stream index in container format
	CompositionTime time.Duration // packet presentation time minus decode time for H264 B-Frame
	Time            time.Duration // packet decode time
	Duration        time.Duration //packet duration
	Data            []byte        // packet data
	DataType        int8          // packet data type: video/audio.
	AVCPacketType   uint8         // avc packet type: AVC_NALU/AVC_SEQHDR/AAC_SEQHDR/AAC_RAW...
	HeaderChanged   bool          // indicates if the sps/pps info changed
	HeaderBeginAt   int           // header pos in queue
	Drop            bool          // whether drop packet
	First           bool          // whether first packet
	AbsoluteTime    time.Duration // the time after jittered, to ensure they are always increased in one stream, ant not affect the normal pkt.Time field

	SliceId        uint32 // slice id
	SliceFrameCnt  uint16 // slice frame cnt
	SliceTimeStamp uint64 // slice timestamp
}

Packet stores compressed audio/video data.

func (*Packet) EarlierThen

func (pkt *Packet) EarlierThen(confirmedPktTime time.Duration) bool

EarlierThen ...

func (*Packet) IsScriptData

func (pkt *Packet) IsScriptData() bool

IsScriptData Packet是否是script data类型的flvTag

func (*Packet) IsSequenceHeader

func (pkt *Packet) IsSequenceHeader() bool

IsSequenceHeader Packet是否是audio/video sequence hdr

func (*Packet) IsVideo

func (pkt *Packet) IsVideo() bool

IsVideoNalu Packet is video

func (*Packet) IsVideoNalu

func (pkt *Packet) IsVideoNalu() bool

IsVideoNalu Packet is video nalu

func (*Packet) String

func (pkt *Packet) String() string

type PacketReader

type PacketReader interface {
	ReadPacket() (Packet, error)
}

type PacketWriter

type PacketWriter interface {
	WritePacket(Packet) error
}

type SampleFormat

type SampleFormat uint8

Audio sample format.

func (SampleFormat) BytesPerSample

func (self SampleFormat) BytesPerSample() int

func (SampleFormat) IsPlanar

func (self SampleFormat) IsPlanar() bool

Check if this sample format is in planar.

func (SampleFormat) String

func (self SampleFormat) String() string

type Transport

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

Transport 从高层次封装了AV传输

func NewTransport

func NewTransport(opt ...Option) *Transport

NewTransport 创建Transport实例

func (*Transport) CopyAV

func (t *Transport) CopyAV(ctx context.Context, dst Muxer, src Demuxer) error

CopyAV ...

func (*Transport) CopyHeaders

func (t *Transport) CopyHeaders(ctx context.Context, dst Muxer, src Demuxer) (err error)

CopyHeaders ...

func (*Transport) CopyPackets

func (t *Transport) CopyPackets(ctx context.Context, dst Muxer, src Demuxer) (err error)

CopyPackets ...

type VideoCodecData

type VideoCodecData interface {
	CodecData
	Width() int  // Video width
	Height() int // Video height
}

Directories

Path Synopsis
Package pktque provides packet Filter interface and structures used by other components.
Package pktque provides packet Filter interface and structures used by other components.
Packege pubsub implements publisher-subscribers model used in multi-channel streaming.
Packege pubsub implements publisher-subscribers model used in multi-channel streaming.
Package transcoder implements Transcoder based on Muxer/Demuxer and AudioEncoder/AudioDecoder interface.
Package transcoder implements Transcoder based on Muxer/Demuxer and AudioEncoder/AudioDecoder interface.

Jump to

Keyboard shortcuts

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