revid: Encoder type now implements io.Writer

This commit is contained in:
Saxon 2019-03-10 13:00:58 +10:30
parent def220daf3
commit 9f4f9e8920
5 changed files with 22 additions and 22 deletions

View File

@ -116,7 +116,7 @@ func TestSegment(t *testing.T) {
for i := 0; i < noOfPacketsToWrite; i++ {
// Insert a payload so that we check that the segmentation works correctly
// in this regard. Packet number will be used.
encoder.Encode([]byte{byte(i)})
encoder.Write([]byte{byte(i)})
rb.Flush()
for {
@ -203,7 +203,7 @@ func TestSendFailDiscontinuity(t *testing.T) {
const noOfPacketsToWrite = 100
for i := 0; i < noOfPacketsToWrite; i++ {
// Our payload will just be packet number.
encoder.Encode([]byte{byte(i)})
encoder.Write([]byte{byte(i)})
rb.Flush()
for {

View File

@ -453,7 +453,7 @@ loop:
// Loop over encoders and hand bytes over to each one.
for _, enc := range r.encoder {
err := enc.Encode(bytes)
_, err := enc.Write(bytes)
if err != nil {
r.err <- err
}

View File

@ -30,7 +30,7 @@ package stream
import "io"
type Encoder interface {
Encode([]byte) error
Write([]byte) (int, error)
}
// NopEncoder returns an
@ -42,7 +42,7 @@ type noop struct {
dst io.Writer
}
func (e noop) Encode(p []byte) error {
_, err := e.dst.Write(p)
return err
func (e noop) Write(p []byte) (int, error) {
n, err := e.dst.Write(p)
return n, err
}

View File

@ -189,7 +189,7 @@ func (s *frameScanner) readByte() (b byte, ok bool) {
// generate takes in raw video data from the input chan and packetises it into
// flv tags, which are then passed to the output channel.
func (e *Encoder) Encode(frame []byte) error {
func (e *Encoder) Write(frame []byte) (int, error) {
var frameType byte
var packetType byte
if e.start.IsZero() {
@ -198,9 +198,9 @@ func (e *Encoder) Encode(frame []byte) error {
// See https://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf
// section E.3.
var zero [4]byte
_, err := e.dst.Write(zero[:])
n, err := e.dst.Write(zero[:])
if err != nil {
return err
return n, err
}
}
timeStamp := e.getNextTimestamp()
@ -229,9 +229,9 @@ func (e *Encoder) Encode(frame []byte) error {
Data: frame,
PrevTagSize: uint32(videoHeaderSize + len(frame)),
}
_, err := e.dst.Write(tag.Bytes())
n, err := e.dst.Write(tag.Bytes())
if err != nil {
return err
return n, err
}
}
// Do we even have some audio to send off ?
@ -250,9 +250,9 @@ func (e *Encoder) Encode(frame []byte) error {
Data: dummyAudioTag1Data,
PrevTagSize: uint32(audioSize),
}
_, err := e.dst.Write(tag.Bytes())
n, err := e.dst.Write(tag.Bytes())
if err != nil {
return err
return n, err
}
tag = AudioTag{
@ -267,11 +267,11 @@ func (e *Encoder) Encode(frame []byte) error {
Data: dummyAudioTag2Data,
PrevTagSize: uint32(22),
}
_, err = e.dst.Write(tag.Bytes())
n, err = e.dst.Write(tag.Bytes())
if err != nil {
return err
return n, err
}
}
return nil
return len(frame), nil
}

View File

@ -180,13 +180,13 @@ func (e *Encoder) TimeBasedPsi(b bool, sendCount int) {
// generate handles the incoming data and generates equivalent mpegts packets -
// sending them to the output channel.
func (e *Encoder) Encode(nalu []byte) error {
func (e *Encoder) Write(nalu []byte) (int, error) {
now := time.Now()
if (e.timeBasedPsi && (now.Sub(e.psiLastTime) > psiInterval)) || (!e.timeBasedPsi && (e.pktCount >= e.psiSendCount)) {
e.pktCount = 0
err := e.writePSI()
if err != nil {
return err
return 0, err
}
e.psiLastTime = now
}
@ -220,16 +220,16 @@ func (e *Encoder) Encode(nalu []byte) error {
pkt.PCR = e.pcr()
pusi = false
}
_, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize]))
n, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize]))
if err != nil {
return err
return n, err
}
e.pktCount++
}
e.tick()
return nil
return len(nalu), nil
}
// writePSI creates mpegts with pat and pmt tables - with pmt table having updated