Skip to content

Commit

Permalink
Moved things around
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Dec 3, 2017
1 parent 1fee264 commit 40e33ae
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 81 deletions.
57 changes: 0 additions & 57 deletions demuxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package astits

import (
"context"
"fmt"
"io"

"github.com/pkg/errors"
Expand Down Expand Up @@ -162,59 +161,3 @@ func (dmx *Demuxer) Rewind() (n int64, err error) {
}
return
}

// rewind rewinds the reader if possible, otherwise n = -1
func rewind(r io.Reader) (n int64, err error) {
if s, ok := r.(io.Seeker); ok {
if n, err = s.Seek(0, 0); err != nil {
err = errors.Wrap(err, "astits: seeking to 0 failed")
return
}
return
}
n = -1
return
}

// autoDetectPacketSize updates the packet size based on the first bytes
// Minimum packet size is 188 and is bounded by 2 sync bytes
// Assumption is made that the first byte of the reader is a sync byte
func autoDetectPacketSize(r io.Reader) (packetSize int, err error) {
// Read first bytes
const l = 193
var b = make([]byte, l)
if _, err = r.Read(b); err != nil {
err = errors.Wrapf(err, "astits: reading first %d bytes failed", l)
return
}

// Packet must start with a sync byte
if b[0] != syncByte {
err = ErrPacketMustStartWithASyncByte
return
}

// Look for sync bytes
for idx, b := range b {
if b == syncByte && idx >= 188 {
// Update packet size
packetSize = idx

// Rewind or sync reader
var n int64
if n, err = rewind(r); err != nil {
err = errors.Wrap(err, "astits: rewinding failed")
return
} else if n == -1 {
var ls = packetSize - (l - packetSize)
if _, err = r.Read(make([]byte, ls)); err != nil {
err = errors.Wrapf(err, "astits: reading %d bytes to sync reader failed", ls)
return
}
}
return
}
}
err = fmt.Errorf("astits: only one sync byte detected in first %d bytes", l)
return
}
24 changes: 0 additions & 24 deletions demuxer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,6 @@ func TestDemuxerNextData(t *testing.T) {
assert.EqualError(t, err, ErrNoMorePackets.Error())
}

func TestAutoDetectPacketSize(t *testing.T) {
// Packet should start with a sync byte
w := astibinary.New()
w.Write(uint8(2))
w.Write(byte(syncByte))
_, err := autoDetectPacketSize(bytes.NewReader(w.Bytes()))
assert.EqualError(t, err, ErrPacketMustStartWithASyncByte.Error())

// Valid packet size
w.Reset()
w.Write(byte(syncByte))
w.Write(make([]byte, 20))
w.Write(byte(syncByte))
w.Write(make([]byte, 166))
w.Write(byte(syncByte))
w.Write(make([]byte, 187))
w.Write([]byte("test"))
r := bytes.NewReader(w.Bytes())
p, err := autoDetectPacketSize(r)
assert.NoError(t, err)
assert.Equal(t, 188, p)
assert.Equal(t, 380, r.Len())
}

func TestDemuxerRewind(t *testing.T) {
r := bytes.NewReader([]byte("content"))
dmx := New(context.Background(), r)
Expand Down
57 changes: 57 additions & 0 deletions packet_buffer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package astits

import (
"fmt"
"io"

"github.com/pkg/errors"
Expand Down Expand Up @@ -32,6 +33,62 @@ func newPacketBuffer(r io.Reader, packetSize int) (pb *packetBuffer, err error)
return
}

// autoDetectPacketSize updates the packet size based on the first bytes
// Minimum packet size is 188 and is bounded by 2 sync bytes
// Assumption is made that the first byte of the reader is a sync byte
func autoDetectPacketSize(r io.Reader) (packetSize int, err error) {
// Read first bytes
const l = 193
var b = make([]byte, l)
if _, err = r.Read(b); err != nil {
err = errors.Wrapf(err, "astits: reading first %d bytes failed", l)
return
}

// Packet must start with a sync byte
if b[0] != syncByte {
err = ErrPacketMustStartWithASyncByte
return
}

// Look for sync bytes
for idx, b := range b {
if b == syncByte && idx >= 188 {
// Update packet size
packetSize = idx

// Rewind or sync reader
var n int64
if n, err = rewind(r); err != nil {
err = errors.Wrap(err, "astits: rewinding failed")
return
} else if n == -1 {
var ls = packetSize - (l - packetSize)
if _, err = r.Read(make([]byte, ls)); err != nil {
err = errors.Wrapf(err, "astits: reading %d bytes to sync reader failed", ls)
return
}
}
return
}
}
err = fmt.Errorf("astits: only one sync byte detected in first %d bytes", l)
return
}

// rewind rewinds the reader if possible, otherwise n = -1
func rewind(r io.Reader) (n int64, err error) {
if s, ok := r.(io.Seeker); ok {
if n, err = s.Seek(0, 0); err != nil {
err = errors.Wrap(err, "astits: seeking to 0 failed")
return
}
return
}
n = -1
return
}

// next fetches the next packet from the buffer
func (pb *packetBuffer) next() (p *Packet, err error) {
// Read
Expand Down
33 changes: 33 additions & 0 deletions packet_buffer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package astits

import (
"bytes"
"testing"

"github.com/asticode/go-astitools/binary"
"github.com/stretchr/testify/assert"
)

func TestAutoDetectPacketSize(t *testing.T) {
// Packet should start with a sync byte
w := astibinary.New()
w.Write(uint8(2))
w.Write(byte(syncByte))
_, err := autoDetectPacketSize(bytes.NewReader(w.Bytes()))
assert.EqualError(t, err, ErrPacketMustStartWithASyncByte.Error())

// Valid packet size
w.Reset()
w.Write(byte(syncByte))
w.Write(make([]byte, 20))
w.Write(byte(syncByte))
w.Write(make([]byte, 166))
w.Write(byte(syncByte))
w.Write(make([]byte, 187))
w.Write([]byte("test"))
r := bytes.NewReader(w.Bytes())
p, err := autoDetectPacketSize(r)
assert.NoError(t, err)
assert.Equal(t, 188, p)
assert.Equal(t, 380, r.Len())
}

0 comments on commit 40e33ae

Please sign in to comment.