-
Notifications
You must be signed in to change notification settings - Fork 909
/
helpers.go
66 lines (54 loc) · 1.39 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ogg
import (
"bytes"
)
// PacketDecoder wraps around a decoder to easily read indiv packets
type PacketDecoder struct {
D *Decoder
currentPage Page
currentSegmentIndex int
currentDataPos int
packetBuf bytes.Buffer
}
func NewPacketDecoder(d *Decoder) *PacketDecoder {
return &PacketDecoder{
D: d,
}
}
func (p *PacketDecoder) Decode() (packet []byte, newPage Page, err error) {
if p.currentPage.Data == nil {
newPage, err = p.D.Decode()
if err != nil {
return
}
p.currentPage = newPage
}
for {
// Read the next packet from the segment table
for p.currentSegmentIndex < len(p.currentPage.SegTbl) {
segmentSize := p.currentPage.SegTbl[p.currentSegmentIndex]
// 0 size means its the end of the last packet
if segmentSize != 0 {
p.packetBuf.Write(p.currentPage.Data[p.currentDataPos : p.currentDataPos+int(segmentSize)])
}
p.currentDataPos += int(segmentSize)
p.currentSegmentIndex++
// Anything less than a full packet means its the end of a packet
if segmentSize < 0xff {
packet = make([]byte, p.packetBuf.Len())
p.packetBuf.Read(packet)
p.packetBuf.Reset()
return
}
}
// If we got here then the packet continues in the next page
// so grab the next page
newPage, err = p.D.Decode()
if err != nil {
return
}
p.currentPage = newPage
p.currentDataPos = 0
p.currentSegmentIndex = 0
}
}