mirror of https://bitbucket.org/ausocean/av.git
codec/h264/extract.go: extracter->extractor everywhere
This commit is contained in:
parent
8edfbeb369
commit
47f6dcfe51
|
@ -3,7 +3,7 @@ NAME
|
||||||
extract.go
|
extract.go
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
extract.go provides an extracter to get access units from an RTP stream.
|
extract.go provides an Extractor to get access units from an RTP stream.
|
||||||
|
|
||||||
AUTHOR
|
AUTHOR
|
||||||
Saxon Nelson-Milton <saxon@ausocean.org>
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
||||||
|
@ -72,23 +72,23 @@ const (
|
||||||
// Bytes for an access unit delimeter.
|
// Bytes for an access unit delimeter.
|
||||||
var aud = []byte{0x00, 0x00, 0x01, 0x09, 0xf0}
|
var aud = []byte{0x00, 0x00, 0x01, 0x09, 0xf0}
|
||||||
|
|
||||||
// Extracter is an extracter for extracting H264 access units from RTP stream.
|
// Extractor is an Extractor for extracting H264 access units from RTP stream.
|
||||||
type Extracter struct {
|
type Extractor struct {
|
||||||
buf *bytes.Buffer // Holds the current access unit.
|
buf *bytes.Buffer // Holds the current access unit.
|
||||||
frag bool // Indicates if we're currently dealing with a fragmentation packet.
|
frag bool // Indicates if we're currently dealing with a fragmentation packet.
|
||||||
dst io.Writer // The destination we'll be writing extracted NALUs to.
|
dst io.Writer // The destination we'll be writing extracted NALUs to.
|
||||||
toWrite []byte // Holds the current NAL unit with start code to be written.
|
toWrite []byte // Holds the current NAL unit with start code to be written.
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewExtracter returns a new Extracter.
|
// NewExtractor returns a new Extractor.
|
||||||
func NewExtracter() *Extracter {
|
func NewExtractor() *Extractor {
|
||||||
return &Extracter{
|
return &Extractor{
|
||||||
buf: bytes.NewBuffer(make([]byte, 0, maxAUSize))}
|
buf: bytes.NewBuffer(make([]byte, 0, maxAUSize))}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract extracts H264 access units from an RTP stream. This function
|
// Extract extracts H264 access units from an RTP stream. This function
|
||||||
// expects that each read from src will provide a single RTP packet.
|
// expects that each read from src will provide a single RTP packet.
|
||||||
func (e *Extracter) Extract(dst io.Writer, src io.Reader, delay time.Duration) error {
|
func (e *Extractor) Extract(dst io.Writer, src io.Reader, delay time.Duration) error {
|
||||||
e.toWrite = []byte{0, 0, 0, 1}
|
e.toWrite = []byte{0, 0, 0, 1}
|
||||||
e.buf.Write(aud)
|
e.buf.Write(aud)
|
||||||
e.dst = dst
|
e.dst = dst
|
||||||
|
@ -146,8 +146,8 @@ func (e *Extracter) Extract(dst io.Writer, src io.Reader, delay time.Duration) e
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleSTAPA parses NAL units from an aggregation packet and writes
|
// handleSTAPA parses NAL units from an aggregation packet and writes
|
||||||
// them to the Extracter's buffer buf.
|
// them to the Extractor's buffer buf.
|
||||||
func (e *Extracter) handleSTAPA(d []byte) {
|
func (e *Extractor) handleSTAPA(d []byte) {
|
||||||
// If the length is too small, ignore.
|
// If the length is too small, ignore.
|
||||||
if len(d) < minSTAPALen {
|
if len(d) < minSTAPALen {
|
||||||
return
|
return
|
||||||
|
@ -168,8 +168,8 @@ func (e *Extracter) handleSTAPA(d []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleFUA parses NAL units from fragmentation packets and writes
|
// handleFUA parses NAL units from fragmentation packets and writes
|
||||||
// them to the Extracter's buf.
|
// them to the Extractor's buf.
|
||||||
func (e *Extracter) handleFUA(d []byte) {
|
func (e *Extractor) handleFUA(d []byte) {
|
||||||
// If length is too small, ignore.
|
// If length is too small, ignore.
|
||||||
if len(d) < minFUALen {
|
if len(d) < minFUALen {
|
||||||
return
|
return
|
||||||
|
@ -200,10 +200,10 @@ func (e *Extracter) handleFUA(d []byte) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeWithPrefix writes a NAL unit to the Extracter's buf in byte stream format
|
// writeWithPrefix writes a NAL unit to the Extractor's buf in byte stream format
|
||||||
// using the start code, and sends any ready prior access unit stored in the buf
|
// using the start code, and sends any ready prior access unit stored in the buf
|
||||||
// to the destination.
|
// to the destination.
|
||||||
func (e *Extracter) writeWithPrefix(d []byte) {
|
func (e *Extractor) writeWithPrefix(d []byte) {
|
||||||
e.toWrite = append(e.toWrite, d...)
|
e.toWrite = append(e.toWrite, d...)
|
||||||
curType, _ := NALType(e.toWrite)
|
curType, _ := NALType(e.toWrite)
|
||||||
if e.buf.Len() != 0 && (curType == h264dec.NALTypeSPS || curType == h264dec.NALTypeIDR || curType == h264dec.NALTypeNonIDR) {
|
if e.buf.Len() != 0 && (curType == h264dec.NALTypeSPS || curType == h264dec.NALTypeIDR || curType == h264dec.NALTypeNonIDR) {
|
||||||
|
@ -215,8 +215,8 @@ func (e *Extracter) writeWithPrefix(d []byte) {
|
||||||
e.toWrite = e.toWrite[:4]
|
e.toWrite = e.toWrite[:4]
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeNoPrefix writes data to the Extracter's buf. This is used for non start
|
// writeNoPrefix writes data to the Extractor's buf. This is used for non start
|
||||||
// fragmentations of a NALU.
|
// fragmentations of a NALU.
|
||||||
func (e *Extracter) writeNoPrefix(d []byte) {
|
func (e *Extractor) writeNoPrefix(d []byte) {
|
||||||
e.buf.Write(d)
|
e.buf.Write(d)
|
||||||
}
|
}
|
||||||
|
|
|
@ -159,7 +159,7 @@ func TestRTPLex(t *testing.T) {
|
||||||
for testNum, test := range tests {
|
for testNum, test := range tests {
|
||||||
r := &rtpReader{packets: test.packets}
|
r := &rtpReader{packets: test.packets}
|
||||||
d := &destination{}
|
d := &destination{}
|
||||||
err := NewExtracter().Extract(d, r, 0)
|
err := NewExtractor().Extract(d, r, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error lexing: %v\n", err)
|
t.Fatalf("error lexing: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -322,7 +322,7 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io.
|
||||||
r.setupInput = r.startRTSPCamera
|
r.setupInput = r.startRTSPCamera
|
||||||
switch r.config.InputCodec {
|
switch r.config.InputCodec {
|
||||||
case codecutil.H264:
|
case codecutil.H264:
|
||||||
r.lexTo = h264.NewExtracter().Extract
|
r.lexTo = h264.NewExtractor().Extract
|
||||||
case codecutil.H265:
|
case codecutil.H265:
|
||||||
r.lexTo = h265.NewLexer(false).Lex
|
r.lexTo = h265.NewLexer(false).Lex
|
||||||
case codecutil.MJPEG:
|
case codecutil.MJPEG:
|
||||||
|
|
Loading…
Reference in New Issue