Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves cli helpers to pkgs #31

Merged
merged 8 commits into from
Dec 16, 2016
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 3 additions & 96 deletions cli/parsefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func main() {
}
}(tsFile)
// Verify if sync-byte is present and seek to the first sync-byte
syncIndex, err := sync(tsFile)
syncIndex, err := packet.FindNextSync(tsFile)
if err == nil {
_, err = tsFile.Seek(syncIndex, 0)
if err != nil {
Expand All @@ -72,7 +72,7 @@ func main() {
fmt.Println(err)
return
}
pat, err := extractPat(tsFile)
pat, err := psi.ReadPAT(tsFile)
if err != nil {
println(err)
return
Expand All @@ -82,7 +82,7 @@ func main() {
if *showPmt {
pm := pat.ProgramMap()
for pn, pid := range pm {
pmt, err := extractPmt(tsFile, pid)
pmt, err := psi.ReadPMT(tsFile, pid)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -150,99 +150,6 @@ func printPat(pat psi.PAT) {
printlnf("\tNumber of Programs %v", pat.NumPrograms())
}

func extractPat(buf io.Reader) (psi.PAT, error) {
pkt := make([]byte, packet.PacketSize)
var pat psi.PAT
for read, err := buf.Read(pkt); pat == nil; read, err = buf.Read(pkt) {
if err != nil {
return nil, err
}
if read <= 0 {
return nil, fmt.Errorf("Reached EOF without PAT")
}
pid, err := packet.Pid(pkt)
if err != nil {
return nil, err
}
if pid == 0 {
pay, err := packet.Payload(pkt)
if err != nil {
println(err)
continue
}
cp := make([]byte, len(pay))
copy(cp, pay)
pat, err := psi.NewPAT(cp)
if err != nil {
println(err)
continue
}
return pat, nil
}
}
return nil, fmt.Errorf("No pat found")
}
func printlnf(format string, a ...interface{}) {
fmt.Printf(format+"\n", a...)
}
func extractPmt(buf io.Reader, pid uint16) (psi.PMT, error) {
pkt := make([]byte, packet.PacketSize)
pmtAcc := packet.NewAccumulator(psi.PmtAccumulatorDoneFunc)
var pmt psi.PMT
for read, err := buf.Read(pkt); pmt == nil && read > 0; read, err = buf.Read(pkt) {
if err != nil {
return nil, err
}
currPid, err := packet.Pid(pkt)
if err != nil {
return nil, err
}
if currPid == pid {
done, err := pmtAcc.Add(pkt)
if err != nil {
return nil, err
}
if done {
b, err := pmtAcc.Parse()
if err != nil {
return nil, err
}
pmt, err = psi.NewPMT(b)
if err != nil {
return nil, err
}

}

}
}
return pmt, nil
}
func sync(buf io.Reader) (int64, error) {
// function find the first sync byte of the array
data := make([]byte, 1)
for i := int64(0); ; i++ {
read, err := buf.Read(data)
if err != nil && err != io.EOF {
println(err)
}
if read == 0 {
break
}
if int(data[0]) == packet.SyncByte {
// check next 188th byte
nextData := make([]byte, packet.PacketSize)
nextRead, err := buf.Read(nextData)
if err != nil && err != io.EOF {
println(err)
}
if nextRead == 0 {
break
}
if nextData[187] == packet.SyncByte {
return i, nil
}
}
}
return 0, fmt.Errorf("Sync-byte not found.")
}
9 changes: 9 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ var (
ErrNoPCR = errors.New("adaptation field has no Program Clock Reference")
// ErrNoOPCR is returned when an attempt is made to access an adaptation field OPCR that does not exist
ErrNoOPCR = errors.New("adaptation field has no Original Program Clock Reference")
// ErrPATNotFound is returned when expected PAT packet is not found when
// reading TS packets.
ErrPATNotFound = errors.New("No PAT was found while reading TS")
// ErrPMTNotFound is returned when expected PMT packet(s) are not found when
// reading TS packets.
ErrPMTNotFound = errors.New("No PMT was found while reading TS")
// ErrParsePMTDescriptor is returned when a PMT descriptor cannot be parsed
ErrParsePMTDescriptor = errors.New("unable to parse PMT descriptor")
// ErrInvalidPATLength is returned when a PAT cannot be parsed because there are not enough bytes
Expand Down Expand Up @@ -70,4 +76,7 @@ var (
ErrSCTE35DescriptorNotFound = errors.New("Cannot close descriptor that's not in the open list")
// ErrNilPAT is returned when a PAT is passed into a function for which it cannot be nil.
ErrNilPAT = errors.New("Nil PAT not allowed here.")
// ErrSyncByteNotFound is returned when a packet sync byte could not be found
// when reading.
ErrSyncByteNotFound = errors.New("Sync-byte not found.")
)
62 changes: 62 additions & 0 deletions packet/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
MIT License

Copyright 2016 Comcast Cable Communications Management, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package packet

import (
"io"

"github.com/Comcast/gots"
)

// Sync finds the offset of the next packet sync byte and returns the offset of
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment should start with FindNextSync.

Also, I think that there is a bug here. I know that you didn't introduce it, but it should be fixed. If we are checking the next packet's sync byte after we find the first sync byte, the reader is left skipping the first valid packet. I would argue that this func should only consume those bytes before the first sync byte. A reader is tricky because once we find a sync byte, we have consumed it. I am not sure the best way to solve this, but I would consider using a []byte].

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After checking #18 We could likely solve this with bufio.Reader

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I noticed this and actually that is one reason I changed the name (I missed the comment). In the parsefile example it does the seek to the position returned so the change of the reader position caused here doesn't affect it, but it would be better to have this actually bring the reader to the right point instead.

Out of curiosity, do you know the reason it also checks the 2nd sync after the first found?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why the next sync byte is checked. It could be just to make sure it's actually a packet start and not an arbitrary 0x47. Any additional sync byte verification should be done explicitly by the user of the library though.

// the sync w.r.t. the original reader position. It also checks the next 188th
// byte to ensure a sync is found.
func FindNextSync(r io.Reader) (int64, error) {
data := make([]byte, 1)
for i := int64(0); ; i++ {
_, err := io.ReadFull(r, data)
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
}
if err != nil {
return 0, err
}
if int(data[0]) == SyncByte {
// check next 188th byte
nextData := make([]byte, PacketSize)
_, err := io.ReadFull(r, nextData)
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
}
if err != nil {
return 0, err
}
if nextData[187] == SyncByte {
return i, nil
}
}
}
return 0, gots.ErrSyncByteNotFound
}
80 changes: 80 additions & 0 deletions packet/io_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
MIT License

Copyright 2016 Comcast Cable Communications Management, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package packet

import (
"bytes"
"encoding/hex"
"testing"
)

func TestFindNextSyncForSmoke(t *testing.T) {
bs, _ := hex.DecodeString("474000100000b00d0001c100000001e256f803e71bfffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"ff4742")
r := bytes.NewReader(bs)

offset, err := FindNextSync(r)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if offset != 0 {
t.Errorf("Incorrect offset returned for next sync marker")
}
}

func TestFindNextSyncNonZeroOffset(t *testing.T) {
bs, _ := hex.DecodeString("ffffff474000100000b00d0001c100000001e256f803e71bfffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"ff4742")
r := bytes.NewReader(bs)

offset, err := FindNextSync(r)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if offset != 3 {
t.Errorf("Incorrect offset returned for next sync marker")
}
}

func TestFindNextSyncNotFound(t *testing.T) {
// no sync byte here
bs, _ := hex.DecodeString("ff4000100000b00d0001c100000001e256f803e71bfffffff")
r := bytes.NewReader(bs)

_, err := FindNextSync(r)
if err == nil {
t.Errorf("Expected there to be an error, but there was not")
}
}
37 changes: 37 additions & 0 deletions psi/pat.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ SOFTWARE.
package psi

import (
"io"

"github.com/Comcast/gots"
"github.com/Comcast/gots/packet"
)
Expand Down Expand Up @@ -124,3 +126,38 @@ func (pat pat) ProgramMap() map[uint16]uint16 {

return m
}

// ReadPAT extracts a PAT from a reader of a TS stream. It will read until a
// PAT packet is found or EOF is reached.
// It returns a new PAT object parsed from the packet, if found, and otherwise
// returns an error.
func ReadPAT(r io.Reader) (PAT, error) {
pkt := make(packet.Packet, packet.PacketSize)
var pat PAT
for pat == nil {
if _, err := io.ReadFull(r, pkt); err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
return nil, gots.ErrPATNotFound
}
return nil, err
}
isPat, err := packet.IsPat(pkt)
if err != nil {
return nil, err
}
if isPat {
pay, err := packet.Payload(pkt)
if err != nil {
return nil, err
}
cp := make([]byte, len(pay))
copy(cp, pay)
pat, err := NewPAT(cp)
if err != nil {
return nil, err
}
return pat, nil
}
}
return nil, gots.ErrPATNotFound
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line not needed

}
38 changes: 38 additions & 0 deletions psi/pat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SOFTWARE.
package psi

import (
"bytes"
"encoding/hex"
"reflect"
"testing"
Expand Down Expand Up @@ -105,3 +106,40 @@ func TestProgramMap(t *testing.T) {
}
}
}

func TestReadPATForSmoke(t *testing.T) {
// requires full packets so cannot use test data above
bs, _ := hex.DecodeString("474000100000b00d0001c100000001e256f803e71bfffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"ff474256100002b0300001c10000e131f0060504435545491be121f0042a027e1" +
"f86e225f00f52012a9700e9080c001f41850fa041ee3f6580ffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffff")
r := bytes.NewReader(bs)
pat, err := ReadPAT(r)
if err != nil {
t.Errorf("Unexpected error reading PAT: %v", err)
}
// sanity check (tests integration a bit)
gotMap := pat.ProgramMap()
wantMap := map[uint16]uint16{1: 598}
if !reflect.DeepEqual(wantMap, gotMap) {
t.Errorf("PAT read is invalid, did not have expected program map")
}
}

func TestReadPATIncomplete(t *testing.T) {
bs, _ := hex.DecodeString("47400") // incomplete PAT packet
r := bytes.NewReader(bs)

_, err := ReadPAT(r)
if err == nil {
t.Errorf("Expected to get error reading PAT, but did not")
}
}
Loading