-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathdfla_test.go
More file actions
180 lines (154 loc) · 4.63 KB
/
dfla_test.go
File metadata and controls
180 lines (154 loc) · 4.63 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package mp4_test
import (
"encoding/hex"
"fmt"
"testing"
"github.com/Eyevinn/mp4ff/bits"
"github.com/Eyevinn/mp4ff/mp4"
)
func TestEncodeDecodeDfLa(t *testing.T) {
t.Run("single-block-dfla", func(t *testing.T) {
dfla := &mp4.DfLaBox{
Version: 0,
Flags: 0,
MetadataBlocks: []mp4.FLACMetadataBlock{
{
LastMetadataBlockFlag: true,
BlockType: 0,
Length: 34,
BlockData: make([]byte, 34),
},
},
}
// Fill with some test data
for i := range dfla.MetadataBlocks[0].BlockData {
dfla.MetadataBlocks[0].BlockData[i] = byte(i)
}
boxDiffAfterEncodeAndDecode(t, dfla)
})
t.Run("multiple-blocks-dfla", func(t *testing.T) {
dfla := &mp4.DfLaBox{
Version: 0,
Flags: 0,
MetadataBlocks: []mp4.FLACMetadataBlock{
{
LastMetadataBlockFlag: false,
BlockType: 0,
Length: 34,
BlockData: make([]byte, 34),
},
{
LastMetadataBlockFlag: true,
BlockType: 4,
Length: 20,
BlockData: make([]byte, 20),
},
},
}
// Fill with some test data
for i := range dfla.MetadataBlocks[0].BlockData {
dfla.MetadataBlocks[0].BlockData[i] = byte(i)
}
for i := range dfla.MetadataBlocks[1].BlockData {
dfla.MetadataBlocks[1].BlockData[i] = byte(i + 100)
}
boxDiffAfterEncodeAndDecode(t, dfla)
})
}
func TestDfLaFromExample(t *testing.T) {
// Test with the provided example:
// Original: 00000000800000221000100000000e0035630ac442f000a2b13ccfe8593b6367139498c2e06f9583a1fa
// This appears to be the box payload starting with version/flags
// Breaking it down:
// 00000000 - version 0, flags 0
// 80000022 - last flag (1) + block type (0) + length (0x000022 = 34 bytes)
// 1000100000000e0035630ac442f000a2b13ccfe8593b6367139498c2e06f9583a1fa - 34 bytes of STREAMINFO data
// Creating a valid dfLa box with the example data:
payload := "00000000" + // version + flags
"80000022" + // last flag (1) + block type (0) + length (34 in 24 bits)
"1000100000000e0035630ac442f000a2b13ccfe8593b6367139498c2e06f9583a1fa" // 34 bytes of STREAMINFO data
payloadBytes, err := hex.DecodeString(payload)
if err != nil {
t.Fatal(err)
}
// Calculate total size: 8 (header) + len(payload)
totalSize := 8 + len(payloadBytes)
// Build complete box with header
dflaHex := fmt.Sprintf("%08x", totalSize) + "64664c61" + payload
dflaBytes, err := hex.DecodeString(dflaHex)
if err != nil {
t.Fatal(err)
}
sr := bits.NewFixedSliceReader(dflaBytes)
box, err := mp4.DecodeBoxSR(0, sr)
if err != nil {
t.Fatal(err)
}
dfla, ok := box.(*mp4.DfLaBox)
if !ok {
t.Fatalf("expected *mp4.DfLaBox, got %T", box)
}
if dfla.Version != 0 {
t.Errorf("got version %d instead of 0", dfla.Version)
}
if dfla.Flags != 0 {
t.Errorf("got flags %d instead of 0", dfla.Flags)
}
if len(dfla.MetadataBlocks) != 1 {
t.Fatalf("expected 1 metadata block, got %d", len(dfla.MetadataBlocks))
}
block := dfla.MetadataBlocks[0]
if !block.LastMetadataBlockFlag {
t.Error("expected last metadata block flag to be true")
}
if block.BlockType != 0 {
t.Errorf("got block type %d instead of 0", block.BlockType)
}
if block.Length != 34 {
t.Errorf("got block length %d instead of 34", block.Length)
}
if len(block.BlockData) != 34 {
t.Errorf("got block data length %d instead of 34", len(block.BlockData))
}
// Verify the data matches the STREAMINFO from the example
expectedData := "1000100000000e0035630ac442f000a2b13ccfe8593b6367139498c2e06f9583a1fa"
expectedBytes, _ := hex.DecodeString(expectedData)
for i, b := range expectedBytes {
if block.BlockData[i] != b {
t.Errorf("block data byte %d: got 0x%02x, expected 0x%02x", i, block.BlockData[i], b)
}
}
}
func TestDfLaEncodeInfo(t *testing.T) {
dfla := &mp4.DfLaBox{
Version: 0,
Flags: 0,
MetadataBlocks: []mp4.FLACMetadataBlock{
{
LastMetadataBlockFlag: true,
BlockType: 0, // STREAMINFO
Length: 34,
BlockData: make([]byte, 34),
},
},
}
// Test encoding
encoded := encodeBox(t, dfla)
// Test decoding the encoded data
sr := bits.NewFixedSliceReader(encoded)
box, err := mp4.DecodeBoxSR(0, sr)
if err != nil {
t.Fatal(err)
}
decoded, ok := box.(*mp4.DfLaBox)
if !ok {
t.Fatalf("expected *mp4.DfLaBox, got %T", box)
}
if decoded.Version != dfla.Version {
t.Errorf("version mismatch: got %d, expected %d", decoded.Version, dfla.Version)
}
if len(decoded.MetadataBlocks) != len(dfla.MetadataBlocks) {
t.Errorf("metadata blocks count mismatch: got %d, expected %d",
len(decoded.MetadataBlocks), len(dfla.MetadataBlocks))
}
}