Skip to content

Commit b075ace

Browse files
authored
Merge pull request #84 from kortschak/arraypackets
packet: use [188]byte as Packet type
2 parents b412433 + 94115b6 commit b075ace

13 files changed

+253
-312
lines changed

cli/parsefile.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func main() {
9090
}
9191
}
9292

93-
pkt := make(packet.Packet, packet.PacketSize)
93+
var pkt packet.Packet
9494
var numPackets uint64
9595
ebps := make(map[uint64]ebp.EncoderBoundaryPoint)
9696
scte35PIDs := make(map[uint16]bool)
@@ -107,7 +107,7 @@ func main() {
107107
}
108108

109109
for {
110-
if _, err := io.ReadFull(reader, pkt); err != nil {
110+
if _, err := io.ReadFull(reader, pkt[:]); err != nil {
111111
if err == io.EOF || err == io.ErrUnexpectedEOF {
112112
break
113113
}
@@ -116,13 +116,13 @@ func main() {
116116
}
117117
numPackets++
118118
if *dumpSCTE35 {
119-
currPID, err := packet.Pid(pkt)
119+
currPID, err := packet.Pid(&pkt)
120120
if err != nil {
121121
fmt.Printf("Cannot get packet PID for %d\n", currPID)
122122
continue
123123
}
124124
if scte35PIDs[currPID] {
125-
pay, err := packet.Payload(pkt)
125+
pay, err := packet.Payload(&pkt)
126126
if err != nil {
127127
fmt.Printf("Cannot get payload for packet number %d on PID %d Error=%s\n", numPackets, currPID, err)
128128
continue
@@ -138,7 +138,7 @@ func main() {
138138

139139
}
140140
if *showEbp {
141-
ebpBytes, err := adaptationfield.EncoderBoundaryPoint(pkt)
141+
ebpBytes, err := adaptationfield.EncoderBoundaryPoint(&pkt)
142142
if err != nil {
143143
// Not an EBP
144144
continue
@@ -154,7 +154,7 @@ func main() {
154154
}
155155
if *showPacketNumberOfPID != 0 {
156156
pid := uint16(*showPacketNumberOfPID)
157-
pktPid, err := packet.Pid(pkt)
157+
pktPid, err := packet.Pid(&pkt)
158158
if err != nil {
159159
continue
160160
}

packet/accumulator.go

+14-25
Original file line numberDiff line numberDiff line change
@@ -30,48 +30,37 @@ import (
3030
"github.com/Comcast/gots"
3131
)
3232

33-
var (
34-
emptyByteArray []byte
35-
)
36-
37-
type doneFunc func([]byte) (bool, error)
38-
3933
type accumulator struct {
40-
f doneFunc
41-
packets []Packet
34+
f func([]byte) (bool, error)
35+
packets []*Packet
4236
}
4337

44-
// NewAccumulator creates a new packet accumulator
45-
// that is done when the provided doneFunc returns true.
46-
// PacketAccumulator is not thread safe
47-
func NewAccumulator(f doneFunc) Accumulator {
38+
// NewAccumulator creates a new packet accumulator that is done when
39+
// the provided function returns done as true.
40+
func NewAccumulator(f func(data []byte) (done bool, err error)) Accumulator {
4841
return &accumulator{f: f}
4942
}
5043

5144
// Add a packet to the accumulator. If the added packet completes
5245
// the accumulation, based on the provided doneFunc, true is returned.
5346
// Returns an error if the packet is not valid.
54-
func (a *accumulator) Add(pkt Packet) (bool, error) {
47+
func (a *accumulator) Add(pkt []byte) (bool, error) {
5548
if badLen(pkt) {
5649
return false, gots.ErrInvalidPacketLength
5750
}
51+
var pp Packet
52+
copy(pp[:], pkt)
5853
// technically we could get a packet without a payload. Check this and
5954
// return false if we get one
60-
p, err := ContainsPayload(pkt)
61-
if err != nil {
55+
p, err := ContainsPayload(&pp)
56+
if !p || err != nil {
6257
return false, err
63-
} else if !p {
64-
return false, nil
6558
}
66-
if payloadUnitStartIndicator(pkt) {
67-
a.packets = make([]Packet, 0)
68-
} else if len(a.packets) == 0 {
59+
if !payloadUnitStartIndicator(&pp) && len(a.packets) == 0 {
6960
// First packet must have payload unit start indicator
7061
return false, gots.ErrNoPayloadUnitStartIndicator
7162
}
72-
pktCopy := make(Packet, PacketSize)
73-
copy(pktCopy, pkt)
74-
a.packets = append(a.packets, pktCopy)
63+
a.packets = append(a.packets, &pp)
7564
b, err := a.Parse()
7665
if err != nil {
7766
return false, err
@@ -91,14 +80,14 @@ func (a *accumulator) Parse() ([]byte, error) {
9180
for _, pkt := range a.packets {
9281
pay, err := Payload(pkt)
9382
if err != nil {
94-
return emptyByteArray, err
83+
return nil, err
9584
}
9685
buf.Write(pay)
9786
}
9887
return buf.Bytes(), nil
9988
}
10089

101-
func (a *accumulator) Packets() []Packet {
90+
func (a *accumulator) Packets() []*Packet {
10291
return a.packets
10392
}
10493

packet/accumulator_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func ExamplePacketAccumulator() {
3838

3939
secondPacket, _ := hex.DecodeString("47006411f0002b59bc22ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
4040

41-
packets := []Packet{firstPacket, secondPacket}
41+
packets := [][]byte{firstPacket, secondPacket}
4242
// Just a simple func to accumulate two packets
4343
dFunc := func(b []byte) (bool, error) {
4444
if len(b) <= PacketSize {
@@ -48,9 +48,11 @@ func ExamplePacketAccumulator() {
4848
}
4949

5050
acc := NewAccumulator(dFunc)
51-
done, err := acc.Add(packets[0])
52-
for i := 1; !done; i++ {
53-
done, err = acc.Add(packets[i])
51+
for _, pkt := range packets {
52+
done, err := acc.Add(pkt)
53+
if done {
54+
break
55+
}
5456
if err != nil {
5557
fmt.Printf("%v\n", err)
5658
}

packet/adaptationfield/adaptationfield.go

+42-64
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,59 @@ import (
55
"github.com/Comcast/gots/packet"
66
)
77

8-
var emptyByteSlice []byte
9-
108
// Length returns the length of the adaptation field in bytes
11-
func Length(packet packet.Packet) uint8 {
12-
return uint8(packet[4])
9+
func Length(pkt *packet.Packet) uint8 {
10+
return uint8(pkt[4])
1311
}
1412

1513
// IsDiscontinuous returns the discontinuity indicator for this adaptation field
16-
func IsDiscontinuous(packet packet.Packet) bool {
17-
return (packet[5] & 0x80) != 0
14+
func IsDiscontinuous(pkt *packet.Packet) bool {
15+
return pkt[5]&0x80 != 0
1816
}
1917

2018
// IsRandomAccess returns the random access indicator for this adaptation field
21-
func IsRandomAccess(packet packet.Packet) bool {
22-
return (packet[5] & 0x40) != 0
19+
func IsRandomAccess(pkt *packet.Packet) bool {
20+
return pkt[5]&0x40 != 0
2321
}
2422

2523
// IsESHigherPriority returns true if this elementary stream is
2624
// high priority. Corresponds to the elementary stream
2725
// priority indicator.
28-
func IsESHigherPriority(packet packet.Packet) bool {
29-
return (packet[5] & 0x20) != 0
26+
func IsESHigherPriority(pkt *packet.Packet) bool {
27+
return pkt[5]&0x20 != 0
3028
}
3129

3230
// HasPCR returns true when the PCR flag is set
33-
func HasPCR(packet packet.Packet) bool {
34-
return (packet[5] & 0x10) != 0
31+
func HasPCR(pkt *packet.Packet) bool {
32+
return pkt[5]&0x10 != 0
3533
}
3634

3735
// HasOPCR returns true when the OPCR flag is set
38-
func HasOPCR(packet packet.Packet) bool {
39-
return (packet[5] & 0x08) != 0
36+
func HasOPCR(pkt *packet.Packet) bool {
37+
return pkt[5]&0x08 != 0
4038
}
4139

4240
// HasSplicingPoint returns true when the splicing countdown field is present
43-
func HasSplicingPoint(packet packet.Packet) bool {
44-
return (packet[5] & 0x04) != 0
41+
func HasSplicingPoint(pkt *packet.Packet) bool {
42+
return pkt[5]&0x04 != 0
4543
}
4644

4745
// HasTransportPrivateData returns true when the private data field is present
48-
func HasTransportPrivateData(packet packet.Packet) bool {
49-
return (packet[5] & 0x02) != 0
46+
func HasTransportPrivateData(pkt *packet.Packet) bool {
47+
return pkt[5]&0x02 != 0
5048
}
5149

5250
// HasAdaptationFieldExtension returns true if this adaptation field contains an extension field
53-
func HasAdaptationFieldExtension(packet packet.Packet) bool {
54-
return (packet[5] & 0x01) != 0
51+
func HasAdaptationFieldExtension(pkt *packet.Packet) bool {
52+
return pkt[5]&0x01 != 0
5553
}
5654

5755
// EncoderBoundaryPoint returns the byte array located in the optional TransportPrivateData of the (also optional)
5856
// AdaptationField of the Packet. If either of these optional fields are missing an empty byte array is returned with an error
59-
func EncoderBoundaryPoint(pkt packet.Packet) ([]byte, error) {
60-
if badLen(pkt) {
61-
return emptyByteSlice, gots.ErrInvalidPacketLength
62-
}
57+
func EncoderBoundaryPoint(pkt *packet.Packet) ([]byte, error) {
6358
hasAdapt, err := packet.ContainsAdaptationField(pkt)
6459
if err != nil {
65-
return emptyByteSlice, nil
60+
return nil, nil
6661
}
6762
if hasAdapt && Length(pkt) > 0 && HasTransportPrivateData(pkt) {
6863
ebp, err := TransportPrivateData(pkt)
@@ -78,81 +73,64 @@ func EncoderBoundaryPoint(pkt packet.Packet) ([]byte, error) {
7873
// First 33 bits are PCR base.
7974
// Next 6 bits are reserved.
8075
// Final 9 bits are PCR extension.
81-
func PCR(packet packet.Packet) ([]byte, error) {
82-
if !HasPCR(packet) {
83-
return emptyByteSlice, gots.ErrNoPCR
76+
func PCR(pkt *packet.Packet) ([]byte, error) {
77+
if !HasPCR(pkt) {
78+
return nil, gots.ErrNoPCR
8479
}
8580
offset := 6
86-
return packet[offset : offset+6], nil
81+
return pkt[offset : offset+6], nil
8782
}
8883

8984
// OPCR is the Original Program Clock Reference.
9085
// First 33 bits are original PCR base.
9186
// Next 6 bits are reserved.
9287
// Final 9 bits are original PCR extension.
93-
func OPCR(packet packet.Packet) ([]byte, error) {
94-
if badLen(packet) {
95-
return emptyByteSlice, gots.ErrInvalidPacketLength
96-
}
97-
if !HasOPCR(packet) {
98-
return emptyByteSlice, gots.ErrNoOPCR
88+
func OPCR(pkt *packet.Packet) ([]byte, error) {
89+
if !HasOPCR(pkt) {
90+
return nil, gots.ErrNoOPCR
9991
}
10092
offset := 6
101-
if HasPCR(packet) {
93+
if HasPCR(pkt) {
10294
offset += 6
10395
}
104-
return packet[offset : offset+6], nil
96+
return pkt[offset : offset+6], nil
10597
}
10698

10799
// SpliceCountdown returns a count of how many packets after this one until
108100
// a splice point occurs or an error if none exist. This function calls
109101
// HasSplicingPoint to check for the existence of a splice countdown.
110-
func SpliceCountdown(packet packet.Packet) (uint8, error) {
111-
if badLen(packet) {
112-
return 0, gots.ErrInvalidPacketLength
113-
}
114-
if !HasSplicingPoint(packet) {
102+
func SpliceCountdown(pkt *packet.Packet) (uint8, error) {
103+
if !HasSplicingPoint(pkt) {
115104
return 0, gots.ErrNoSplicePoint
116105
}
117106
offset := 6
118-
if HasPCR(packet) {
107+
if HasPCR(pkt) {
119108
offset += 6
120109
}
121-
if HasOPCR(packet) {
110+
if HasOPCR(pkt) {
122111
offset += 6
123112
}
124-
return packet[offset], nil
113+
return pkt[offset], nil
125114
}
126115

127116
// TransportPrivateData returns the private data from this adaptation field
128117
// or an empty array and an error if there is none. This function calls
129118
// HasTransportPrivateData to check for the existence of private data.
130-
func TransportPrivateData(packet packet.Packet) ([]byte, error) {
131-
if badLen(packet) {
132-
return emptyByteSlice, gots.ErrInvalidPacketLength
133-
}
134-
if !HasTransportPrivateData(packet) {
135-
return emptyByteSlice, gots.ErrNoPrivateTransportData
119+
func TransportPrivateData(pkt *packet.Packet) ([]byte, error) {
120+
if !HasTransportPrivateData(pkt) {
121+
return nil, gots.ErrNoPrivateTransportData
136122
}
137123
offset := 6
138-
if HasPCR(packet) {
124+
if HasPCR(pkt) {
139125
offset += 6
140126
}
141-
if HasOPCR(packet) {
127+
if HasOPCR(pkt) {
142128
offset += 6
143129
}
144-
if HasSplicingPoint(packet) {
130+
if HasSplicingPoint(pkt) {
145131
offset++
146132
}
147-
dataLength := uint8(packet[offset])
133+
dataLength := uint8(pkt[offset])
148134
offset++
149-
return packet[uint8(offset) : uint8(offset)+dataLength], nil
150-
}
151-
152-
// badLen returns true if the packet has invalid length
153-
func badLen(pkt packet.Packet) bool {
154-
if len(pkt) != packet.PacketSize {
155-
return true
156-
}
157-
return false
135+
return pkt[uint8(offset) : uint8(offset)+dataLength], nil
158136
}

packet/adaptationfield/create.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@ import "github.com/Comcast/gots/packet"
44

55
func SetPrivateData(pkt *packet.Packet, af []byte) {
66
offset := 6
7-
if HasPCR(*pkt) {
7+
if HasPCR(pkt) {
88
offset += 6
99
}
10-
if HasOPCR(*pkt) {
10+
if HasOPCR(pkt) {
1111
offset += 6
1212
}
13-
if HasSplicingPoint(*pkt) {
13+
if HasSplicingPoint(pkt) {
1414
offset++
1515
}
16-
(*pkt)[offset] = byte(0x04) // data length
16+
pkt[offset] = byte(0x04) // data length
1717
offset++
18-
for i, b := range af {
19-
(*pkt)[offset+i] = b
20-
}
18+
// FIXME(kortschak): Handle len(af) != 4.
19+
copy(pkt[offset:offset+4], af)
2120
}

0 commit comments

Comments
 (0)