Skip to content

Commit

Permalink
Now using astikit instead of astitools
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Dec 31, 2019
1 parent cf4ece6 commit 7d8e720
Show file tree
Hide file tree
Showing 30 changed files with 221 additions and 226 deletions.
23 changes: 8 additions & 15 deletions astits/main.go
Expand Up @@ -13,9 +13,8 @@ import (
"strings"
"syscall"

"github.com/asticode/go-astikit"
"github.com/asticode/go-astilog"
"github.com/asticode/go-astitools/flag"
"github.com/asticode/go-astitools/io"
"github.com/asticode/go-astits"
"github.com/pkg/errors"
"github.com/pkg/profile"
Expand All @@ -25,7 +24,7 @@ import (
var (
ctx, cancel = context.WithCancel(context.Background())
cpuProfiling = flag.Bool("cp", false, "if yes, cpu profiling is enabled")
dataTypes = astiflag.NewStringsMap()
dataTypes = astikit.NewFlagStrings()
format = flag.String("f", "", "the format")
inputPath = flag.String("i", "", "the input path")
memoryProfiling = flag.Bool("mp", false, "if yes, memory profiling is enabled")
Expand All @@ -38,7 +37,8 @@ func main() {
flag.PrintDefaults()
}
flag.Var(dataTypes, "d", "the datatypes whitelist (all, pat, pmt, pes, eit, nit, sdt, tot)")
var s = astiflag.Subcommand()
cmd := astikit.FlagCmd()
astilog.SetHandyFlags()
flag.Parse()
astilog.FlagInit()

Expand Down Expand Up @@ -68,8 +68,8 @@ func main() {
// Create the demuxer
var dmx = astits.New(ctx, r)

// Switch on subcommand
switch s {
// Switch on command
switch cmd {
case "data":
// Fetch data
if err = data(dmx); err != nil {
Expand Down Expand Up @@ -117,8 +117,8 @@ func handleSignals() {
switch s {
case syscall.SIGABRT, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM:
cancel()
return
}
return
}
}()
}
Expand Down Expand Up @@ -154,14 +154,7 @@ func buildReader(ctx context.Context) (r io.Reader, err error) {
return
}
c.SetReadBuffer(4096)

// Initialize linearizer
// It will read 4096 bytes at each iteration, and will store up to 2MB in its buffer
var l = astiio.NewLinearizer(ctx, c, 4096, 2048*1024)

// Start linearizer
go l.Start()
r = l
r = c
default:
// Open file
var f *os.File
Expand Down
6 changes: 3 additions & 3 deletions data.go
@@ -1,8 +1,8 @@
package astits

import (
"github.com/asticode/go-astikit"
"github.com/pkg/errors"
"github.com/asticode/go-astitools/byte"
)

// PIDs
Expand Down Expand Up @@ -52,8 +52,8 @@ func parseData(ps []*Packet, prs PacketsParser, pm programMap) (ds []*Data, err
c += copy(payload[c:], p.Payload)
}

// Create iterator
i := astibyte.NewIterator(payload)
// Create reader
i := astikit.NewBytesIterator(payload)

// Parse PID
pid := ps[0].Header.PID
Expand Down
6 changes: 3 additions & 3 deletions data_eit.go
Expand Up @@ -3,7 +3,7 @@ package astits
import (
"time"

astibyte "github.com/asticode/go-astitools/byte"
"github.com/asticode/go-astikit"
"github.com/pkg/errors"
)

Expand All @@ -29,7 +29,7 @@ type EITDataEvent struct {
}

// parseEITSection parses an EIT section
func parseEITSection(i *astibyte.Iterator, offsetSectionsEnd int, tableIDExtension uint16) (d *EITData, err error) {
func parseEITSection(i *astikit.BytesIterator, offsetSectionsEnd int, tableIDExtension uint16) (d *EITData, err error) {
// Create data
d = &EITData{ServiceID: tableIDExtension}

Expand Down Expand Up @@ -108,7 +108,7 @@ func parseEITSection(i *astibyte.Iterator, offsetSectionsEnd int, tableIDExtensi
e.HasFreeCSAMode = uint8(b&0x10) > 0

// We need to rewind since the current byte is used by the descriptor as well
i.FastForward(-1)
i.Skip(-1)

// Descriptors
if e.Descriptors, err = parseDescriptors(i); err != nil {
Expand Down
11 changes: 6 additions & 5 deletions data_eit_test.go
@@ -1,11 +1,11 @@
package astits

import (
"bytes"
"testing"

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

var eit = &EITData{
Expand All @@ -25,7 +25,8 @@ var eit = &EITData{
}

func eitBytes() []byte {
w := astibinary.New()
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint16(2)) // Transport stream ID
w.Write(uint16(3)) // Original network ID
w.Write(uint8(4)) // Segment last section number
Expand All @@ -36,12 +37,12 @@ func eitBytes() []byte {
w.Write("111") // Event #1 running status
w.Write("1") // Event #1 free CA mode
descriptorsBytes(w) // Event #1 descriptors
return w.Bytes()
return buf.Bytes()
}

func TestParseEITSection(t *testing.T) {
var b = eitBytes()
d, err := parseEITSection(astibyte.NewIterator(b), len(b), uint16(1))
d, err := parseEITSection(astikit.NewBytesIterator(b), len(b), uint16(1))
assert.Equal(t, d, eit)
assert.NoError(t, err)
}
4 changes: 2 additions & 2 deletions data_nit.go
@@ -1,7 +1,7 @@
package astits

import (
astibyte "github.com/asticode/go-astitools/byte"
"github.com/asticode/go-astikit"
"github.com/pkg/errors"
)

Expand All @@ -21,7 +21,7 @@ type NITDataTransportStream struct {
}

// parseNITSection parses a NIT section
func parseNITSection(i *astibyte.Iterator, tableIDExtension uint16) (d *NITData, err error) {
func parseNITSection(i *astikit.BytesIterator, tableIDExtension uint16) (d *NITData, err error) {
// Create data
d = &NITData{NetworkID: tableIDExtension}

Expand Down
11 changes: 6 additions & 5 deletions data_nit_test.go
@@ -1,10 +1,10 @@
package astits

import (
"bytes"
"testing"

astibinary "github.com/asticode/go-astitools/binary"
astibyte "github.com/asticode/go-astitools/byte"
"github.com/asticode/go-astikit"
"github.com/stretchr/testify/assert"
)

Expand All @@ -19,7 +19,8 @@ var nit = &NITData{
}

func nitBytes() []byte {
w := astibinary.New()
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write("0000") // Reserved for future use
descriptorsBytes(w) // Network descriptors
w.Write("0000") // Reserved for future use
Expand All @@ -28,12 +29,12 @@ func nitBytes() []byte {
w.Write(uint16(3)) // Transport stream #1 original network id
w.Write("0000") // Transport stream #1 reserved for future use
descriptorsBytes(w) // Transport stream #1 descriptors
return w.Bytes()
return buf.Bytes()
}

func TestParseNITSection(t *testing.T) {
var b = nitBytes()
d, err := parseNITSection(astibyte.NewIterator(b), uint16(1))
d, err := parseNITSection(astikit.NewBytesIterator(b), uint16(1))
assert.Equal(t, d, nit)
assert.NoError(t, err)
}
4 changes: 2 additions & 2 deletions data_pat.go
@@ -1,7 +1,7 @@
package astits

import (
astibyte "github.com/asticode/go-astitools/byte"
"github.com/asticode/go-astikit"
"github.com/pkg/errors"
)

Expand All @@ -19,7 +19,7 @@ type PATProgram struct {
}

// parsePATSection parses a PAT section
func parsePATSection(i *astibyte.Iterator, offsetSectionsEnd int, tableIDExtension uint16) (d *PATData, err error) {
func parsePATSection(i *astikit.BytesIterator, offsetSectionsEnd int, tableIDExtension uint16) (d *PATData, err error) {
// Create data
d = &PATData{TransportStreamID: tableIDExtension}

Expand Down
11 changes: 6 additions & 5 deletions data_pat_test.go
@@ -1,11 +1,11 @@
package astits

import (
"bytes"
"testing"

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

var pat = &PATData{
Expand All @@ -17,19 +17,20 @@ var pat = &PATData{
}

func patBytes() []byte {
w := astibinary.New()
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint16(2)) // Program #1 number
w.Write("111") // Program #1 reserved bits
w.Write("0000000000011") // Program #1 map ID
w.Write(uint16(4)) // Program #2 number
w.Write("111") // Program #2 reserved bits
w.Write("0000000000101") // Program #3 map ID
return w.Bytes()
return buf.Bytes()
}

func TestParsePATSection(t *testing.T) {
var b = patBytes()
d, err := parsePATSection(astibyte.NewIterator(b), len(b), uint16(1))
d, err := parsePATSection(astikit.NewBytesIterator(b), len(b), uint16(1))
assert.Equal(t, d, pat)
assert.NoError(t, err)
}
12 changes: 6 additions & 6 deletions data_pes.go
@@ -1,7 +1,7 @@
package astits

import (
astibyte "github.com/asticode/go-astitools/byte"
"github.com/asticode/go-astikit"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -102,7 +102,7 @@ type DSMTrickMode struct {
}

// parsePESData parses a PES data
func parsePESData(i *astibyte.Iterator) (d *PESData, err error) {
func parsePESData(i *astikit.BytesIterator) (d *PESData, err error) {
// Create data
d = &PESData{}

Expand Down Expand Up @@ -133,7 +133,7 @@ func hasPESOptionalHeader(streamID uint8) bool {
}

// parsePESData parses a PES header
func parsePESHeader(i *astibyte.Iterator) (h *PESHeader, dataStart, dataEnd int, err error) {
func parsePESHeader(i *astikit.BytesIterator) (h *PESHeader, dataStart, dataEnd int, err error) {
// Create header
h = &PESHeader{}

Expand Down Expand Up @@ -177,7 +177,7 @@ func parsePESHeader(i *astibyte.Iterator) (h *PESHeader, dataStart, dataEnd int,
}

// parsePESOptionalHeader parses a PES optional header
func parsePESOptionalHeader(i *astibyte.Iterator) (h *PESOptionalHeader, dataStart int, err error) {
func parsePESOptionalHeader(i *astikit.BytesIterator) (h *PESOptionalHeader, dataStart int, err error) {
// Create header
h = &PESOptionalHeader{}

Expand Down Expand Up @@ -390,7 +390,7 @@ func parseDSMTrickMode(i byte) (m *DSMTrickMode) {
}

// parsePTSOrDTS parses a PTS or a DTS
func parsePTSOrDTS(i *astibyte.Iterator) (cr *ClockReference, err error) {
func parsePTSOrDTS(i *astikit.BytesIterator) (cr *ClockReference, err error) {
var bs []byte
if bs, err = i.NextBytes(5); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
Expand All @@ -401,7 +401,7 @@ func parsePTSOrDTS(i *astibyte.Iterator) (cr *ClockReference, err error) {
}

// parseESCR parses an ESCR
func parseESCR(i *astibyte.Iterator) (cr *ClockReference, err error) {
func parseESCR(i *astikit.BytesIterator) (cr *ClockReference, err error) {
var bs []byte
if bs, err = i.NextBytes(6); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
Expand Down

0 comments on commit 7d8e720

Please sign in to comment.