-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathresample_test.go
More file actions
248 lines (222 loc) · 5.82 KB
/
Copy pathresample_test.go
File metadata and controls
248 lines (222 loc) · 5.82 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2024 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package media_test
import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/livekit/media-sdk"
"github.com/livekit/media-sdk/res"
"github.com/livekit/media-sdk/res/testdata"
"github.com/livekit/media-sdk/webm"
)
type paceWriter struct {
w media.PCM16Writer
mu sync.Mutex
cur int
frameT []int
frameSz []int
samples int
}
func (p *paceWriter) Set(i int) {
p.mu.Lock()
p.cur = i
p.mu.Unlock()
}
func (p *paceWriter) String() string {
return fmt.Sprintf("paceWriter(%d) -> %s", p.cur, p.w.String())
}
func (p *paceWriter) SampleRate() int {
return p.w.SampleRate()
}
func (p *paceWriter) WriteSample(sample media.PCM16Sample) error {
p.mu.Lock()
defer p.mu.Unlock()
p.frameT = append(p.frameT, p.cur)
p.frameSz = append(p.frameSz, len(sample))
p.samples += len(sample)
return p.w.WriteSample(sample)
}
func (p *paceWriter) Close() error {
return nil
}
func TestResample(t *testing.T) {
const (
srcFile = "resample.src.s16le"
srcFileWebm = "resample.src.mka"
srcRate = res.SampleRate
)
srcFrames := res.ReadOggAudioFile(testdata.TestAudioOgg, res.SampleRate, 1)
gotSrc := writePCM16s(t, srcFile, srcFrames)
writePCM16sWebm(t, srcFileWebm, srcRate, srcFrames)
require.True(t, gotSrc == "c774af95" || gotSrc == "b04a6a1f")
for _, c := range []struct {
Rate int
Skip int
Buffer int
Bumps []int
}{
{
Rate: 16000, Skip: 1,
Buffer: 1,
},
{
Rate: 8000, Skip: 2,
Buffer: 3,
// TODO: figure out why resampler delays that specific frame; silence?
Bumps: []int{211},
},
} {
t.Run(strconv.Itoa(c.Rate), func(t *testing.T) {
resPref := fmt.Sprintf("resample.out.%d", c.Rate)
resFile := resPref + ".s16le"
resFileWebm := resPref + ".mka"
var dstFrames []media.PCM16Sample
dst := media.NewPCM16FrameWriter(&dstFrames, c.Rate)
pw := &paceWriter{w: dst}
r := media.ResampleWriter(pw, srcRate)
defer r.Close()
totalSamples := 0
for i, src := range srcFrames {
pw.Set(i)
err := r.WriteSample(src)
require.NoError(t, err)
totalSamples += len(src)
}
r.Close()
gotDst := writePCM16s(t, resFile, dstFrames)
// only change if you validated the quality!
// require.Equal(t, "???", gotDst) // TODO: resampler is numerically unstable
_ = gotDst
writePCM16sWebm(t, resFileWebm, c.Rate, dstFrames)
expSamples := totalSamples / (srcRate / c.Rate)
require.Equal(t, expSamples, pw.samples)
require.Equal(t, len(srcFrames), len(pw.frameT))
// Tick at which the first frame was received.
skipped := pw.frameT[0]
require.Equal(t, c.Skip, skipped)
corr := 0
for i, num := range pw.frameT {
if i >= len(pw.frameT)-c.Buffer {
break
}
exp := skipped + i + corr
for _, b := range c.Bumps {
if b == exp {
corr++
exp++
break
}
}
if exp != num {
corr = num - (skipped + i)
t.Errorf("skipped frame: exp %d, got %d\n%v", exp, num, pw.frameT)
}
}
})
}
}
func writePCM16s(t testing.TB, path string, buf []media.PCM16Sample) string {
var out []byte
for _, frame := range buf {
for _, v := range frame {
var b [2]byte
binary.LittleEndian.PutUint16(b[:], uint16(v))
out = append(out, b[:]...)
}
}
err := os.WriteFile(path, out, 0644)
require.NoError(t, err)
h := sha256.Sum256(out)
return hex.EncodeToString(h[:])[:8]
}
func writePCM16sWebm(t testing.TB, path string, rate int, buf []media.PCM16Sample) {
f, err := os.Create(path)
require.NoError(t, err)
defer f.Close()
w := webm.NewPCM16Writer(f, rate, 1, media.DefFrameDur)
defer w.Close()
for _, frame := range buf {
err = w.WriteSample(frame)
require.NoError(t, err)
}
}
func memstats(pid int) int64 {
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/statm", pid))
if err != nil {
panic(err)
}
fields := strings.Fields(string(data))
v, err := strconv.ParseInt(fields[1], 10, 64)
if err != nil {
panic(err)
}
return v
}
func TestResampleLeak(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("only linux supported for this test")
}
pid := os.Getpid()
const (
srcFile = "resample.src.s16le"
srcFileWebm = "resample.src.mka"
srcRate = res.SampleRate
)
srcFrames := res.ReadOggAudioFile(testdata.TestAudioOgg, res.SampleRate, 1)
gotSrc := writePCM16s(t, srcFile, srcFrames)
writePCM16sWebm(t, srcFileWebm, srcRate, srcFrames)
require.True(t, gotSrc == "c774af95" || gotSrc == "b04a6a1f")
const (
runs = 300
pagesThreshold = 100
)
startMem := memstats(pid)
captureStats := func(run int) {
if t.Failed() {
return
}
runtime.GC()
endMem := memstats(pid)
diff := endMem - startMem
if diff > pagesThreshold {
t.Fatalf("resampler memory leak detected (%d Kb / %d runs)", diff, run)
}
}
defer captureStats(runs)
for run := range runs {
func() {
defer captureStats(run + 1)
var dstFrames []media.PCM16Sample
dst := media.NewPCM16FrameWriter(&dstFrames, 16000)
r := media.ResampleWriter(dst, srcRate)
// This test the cleanup function, so intentionally avoid Close.
//defer r.Close()
totalSamples := 0
for _, src := range srcFrames {
err := r.WriteSample(src)
require.NoError(t, err)
totalSamples += len(src)
}
}()
}
}