-
Notifications
You must be signed in to change notification settings - Fork 0
/
oggwriter_ext.go
64 lines (55 loc) · 1.59 KB
/
oggwriter_ext.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
package suzu
import "github.com/pion/rtp/codecs"
func (i *OggWriter) Write(opusPacket *codecs.OpusPacket) error {
payload := opusPacket.Payload[0:]
toc := payload[0]
config := (toc & 0xf8) >> 3
c := toc & 0x03
var count uint64
switch c {
case 0:
count = 1
case 1, 2:
count = 2
case 3:
m := payload[1] & 0x3f
count = uint64(m)
}
// TODO: 値の決定に他の要素が必要ないか確認する
granulePosition := GranulePosition(config)
i.previousGranulePosition += count * granulePosition
data := i.createPage(payload, pageHeaderTypeContinuationOfStream, i.previousGranulePosition, i.pageIndex)
i.pageIndex++
return i.writeToStream(data)
}
// config | frame size | PCM samples
// ---------------+------------+------------
// 16, 20, 24, 28 | 2.5 ms | 120
// 17, 21, 25, 29 | 5 ms | 240
// 0, 4, 8 | 10 ms | 480
// 12, 14 | 10 ms | 480
// 18, 22, 26, 30 | 10 ms | 480
// 1, 5, 9 | 20 ms | 960
// 13, 15 | 20 ms | 960
// 19, 23, 27, 31 | 20 ms | 960
// 2, 6, 10 | 40 ms | 1920
// 3, 7, 11 | 60 ms | 2880
func GranulePosition(config byte) uint64 {
// TODO: 値の決定に他の要素が必要ないか確認する
var granulePosition uint64
switch config {
case 16, 20, 24, 28:
granulePosition = 120
case 17, 21, 25, 29:
granulePosition = 240
case 0, 4, 8, 12, 14, 18, 22, 26, 30:
granulePosition = 480
case 1, 5, 9, 13, 15, 19, 23, 27, 31:
granulePosition = 960
case 2, 6, 10:
granulePosition = 1920
case 3, 7, 11:
granulePosition = 2880
}
return granulePosition
}