Skip to content

Commit

Permalink
Not processing incomplete PES data anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Feb 18, 2018
1 parent 78c6a8a commit dd1bd94
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
9 changes: 7 additions & 2 deletions data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package astits

import "github.com/pkg/errors"
import (
"github.com/pkg/errors"
)

// PIDs
const (
Expand Down Expand Up @@ -61,7 +63,10 @@ func parseData(ps []*Packet, prs PacketsParser, pm programMap) (ds []*Data, err
}
ds = psiData.toData(pid)
} else if isPESPayload(payload) {
ds = append(ds, &Data{PES: parsePESData(payload), PID: pid})
d, err := parsePESData(payload)
if err == nil {
ds = append(ds, &Data{PES: d, PID: pid})
}
}
return
}
Expand Down
20 changes: 13 additions & 7 deletions data_pes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package astits

import "github.com/asticode/go-astilog"
import (
"fmt"

"github.com/pkg/errors"
)

// P-STD buffer scales
const (
Expand Down Expand Up @@ -99,13 +103,16 @@ type DSMTrickMode struct {
}

// parsePESData parses a PES data
func parsePESData(i []byte) (d *PESData) {
func parsePESData(i []byte) (d *PESData, err error) {
// Init
d = &PESData{}

// Parse header
var offset, dataStart, dataEnd = 3, 0, 0
d.Header, dataStart, dataEnd = parsePESHeader(i, &offset)
if d.Header, dataStart, dataEnd, err = parsePESHeader(i, &offset); err != nil {
err = errors.Wrap(err, "astits: parsing PES header failed")
return
}

// Parse data
d.Data = i[dataStart:dataEnd]
Expand All @@ -118,7 +125,7 @@ func hasPESOptionalHeader(streamID uint8) bool {
}

// parsePESData parses a PES header
func parsePESHeader(i []byte, offset *int) (h *PESHeader, dataStart, dataEnd int) {
func parsePESHeader(i []byte, offset *int) (h *PESHeader, dataStart, dataEnd int, err error) {
// Init
h = &PESHeader{}

Expand All @@ -138,10 +145,9 @@ func parsePESHeader(i []byte, offset *int) (h *PESHeader, dataStart, dataEnd int
}

// Check for incomplete data
// TODO Throw away the data?
if dataEnd > len(i) {
astilog.Debug("PES dataEnd > len(i), needs fixing")
dataEnd = len(i)
err = fmt.Errorf("astits: pes dataEnd (%d) > len(i) (%d)", dataEnd, len(i))
return
}

// Optional header
Expand Down
6 changes: 4 additions & 2 deletions data_pes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,12 @@ func pesWithHeaderBytes() []byte {

func TestParsePESSection(t *testing.T) {
// No optional header and specific packet length
d := parsePESData(pesWithoutHeaderBytes())
d, err := parsePESData(pesWithoutHeaderBytes())
assert.NoError(t, err)
assert.Equal(t, d, pesWithoutHeader)

// Optional header and no specific header length
d = parsePESData(pesWithHeaderBytes())
d, err = parsePESData(pesWithHeaderBytes())
assert.NoError(t, err)
assert.Equal(t, d, pesWithHeader)
}

0 comments on commit dd1bd94

Please sign in to comment.