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

View File

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

View File

@ -30,7 +30,7 @@ package stream
import "io" import "io"
type Encoder interface { type Encoder interface {
Encode([]byte) error Write([]byte) (int, error)
} }
// NopEncoder returns an // NopEncoder returns an
@ -42,7 +42,7 @@ type noop struct {
dst io.Writer dst io.Writer
} }
func (e noop) Encode(p []byte) error { func (e noop) Write(p []byte) (int, error) {
_, err := e.dst.Write(p) n, err := e.dst.Write(p)
return err 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 // generate takes in raw video data from the input chan and packetises it into
// flv tags, which are then passed to the output channel. // 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 frameType byte
var packetType byte var packetType byte
if e.start.IsZero() { 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 // See https://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf
// section E.3. // section E.3.
var zero [4]byte var zero [4]byte
_, err := e.dst.Write(zero[:]) n, err := e.dst.Write(zero[:])
if err != nil { if err != nil {
return err return n, err
} }
} }
timeStamp := e.getNextTimestamp() timeStamp := e.getNextTimestamp()
@ -229,9 +229,9 @@ func (e *Encoder) Encode(frame []byte) error {
Data: frame, Data: frame,
PrevTagSize: uint32(videoHeaderSize + len(frame)), PrevTagSize: uint32(videoHeaderSize + len(frame)),
} }
_, err := e.dst.Write(tag.Bytes()) n, err := e.dst.Write(tag.Bytes())
if err != nil { if err != nil {
return err return n, err
} }
} }
// Do we even have some audio to send off ? // Do we even have some audio to send off ?
@ -250,9 +250,9 @@ func (e *Encoder) Encode(frame []byte) error {
Data: dummyAudioTag1Data, Data: dummyAudioTag1Data,
PrevTagSize: uint32(audioSize), PrevTagSize: uint32(audioSize),
} }
_, err := e.dst.Write(tag.Bytes()) n, err := e.dst.Write(tag.Bytes())
if err != nil { if err != nil {
return err return n, err
} }
tag = AudioTag{ tag = AudioTag{
@ -267,11 +267,11 @@ func (e *Encoder) Encode(frame []byte) error {
Data: dummyAudioTag2Data, Data: dummyAudioTag2Data,
PrevTagSize: uint32(22), PrevTagSize: uint32(22),
} }
_, err = e.dst.Write(tag.Bytes()) n, err = e.dst.Write(tag.Bytes())
if err != nil { 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 - // generate handles the incoming data and generates equivalent mpegts packets -
// sending them to the output channel. // sending them to the output channel.
func (e *Encoder) Encode(nalu []byte) error { func (e *Encoder) Write(nalu []byte) (int, error) {
now := time.Now() now := time.Now()
if (e.timeBasedPsi && (now.Sub(e.psiLastTime) > psiInterval)) || (!e.timeBasedPsi && (e.pktCount >= e.psiSendCount)) { if (e.timeBasedPsi && (now.Sub(e.psiLastTime) > psiInterval)) || (!e.timeBasedPsi && (e.pktCount >= e.psiSendCount)) {
e.pktCount = 0 e.pktCount = 0
err := e.writePSI() err := e.writePSI()
if err != nil { if err != nil {
return err return 0, err
} }
e.psiLastTime = now e.psiLastTime = now
} }
@ -220,16 +220,16 @@ func (e *Encoder) Encode(nalu []byte) error {
pkt.PCR = e.pcr() pkt.PCR = e.pcr()
pusi = false pusi = false
} }
_, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize])) n, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize]))
if err != nil { if err != nil {
return err return n, err
} }
e.pktCount++ e.pktCount++
} }
e.tick() e.tick()
return nil return len(nalu), nil
} }
// writePSI creates mpegts with pat and pmt tables - with pmt table having updated // writePSI creates mpegts with pat and pmt tables - with pmt table having updated